通过oracle_fdw可以访问oracle中的一些表和视图,也可以进行修改,尤其是给比较复杂的系统使用非常方便。

(但不能使用oracle_fdw来访问oracle的存储过程、包、函数、序列等对象) 

1.安装oracle_fdw:

1)编译安装oracle_fdw之前,需要安装Oracle的客户端程序;下载地址:http://www.oracle.com/technetwork/database/database-technologies/instant-client/overview/index.html

主要是sdk和basic:

instantclient-basic-linux.x64-18.5.0.0.0dbru.zip

instantclient-sdk-linux.x64-18.5.0.0.0dbru.zip

2)下载地址:http://pgxn.org/dist/oracle_fdw/

unzip oracle_fdw-2.1.0.zip

cd oracle_fdw-2.1.0

3)配置环境变量:

[root@fce40690-0e46-4603-e80e-ca351bda31ec ~]# cat ora-env.sh

export ORACLE_HOME=/opt/oracle/instantclient

export OCI_LIB_DIR=$ORACLE_HOME

export OCI_INC_DIR=$ORACLE_HOME/sdk/include

[root@fce40690-0e46-4603-e80e-ca351bda31ec ~]# cat pg-env.sh

[ -f /etc/profile ] && source /etc/profile

PGDATA=/var/lib/pgsql/11/data

export PGDATA

# If you want to customize your settings,

# Use the file below. This is not overridden

# by the RPMS.

[ -f /var/lib/pgsql/.pgsql_profile ] && source /var/lib/pgsql/.pgsql_profile

export PGHOME=/usr/pgsql-11/

export LD_LIBRARY_PATH=$PGHOME/lib:$LD_LIBRARY_PATH:/opt/oracle/instantclient/

export PATH=$PATH:$PGHOME/bin

4)编译安装:

make && make install

5)切换到postgres创建扩展

postgres=# create extension oracle_fdw;

如果出现找不到libclntsh.so.12.1等库,则考虑将缺少的库从/opt/oracle/instantclient/中拷贝到postgres安装目录的lib下面,有些情况下,指定了LD_LIBRARY_PATH目录都会提示找不到。

postgres=# \dx
List of installed extensions
Name | Version | Schema | Description
------------+---------+------------+----------------------------------------
oracle_fdw | 1.1 | public | foreign data wrapper for Oracle access
plpgsql | 1.0 | pg_catalog | PL/pgSQL procedural language
(2 rows)

2.配置oracle数据库tnsnames.ora

可以将Oracle那边的对应文件拷贝过来即可,注意里面的IP地址:

[postgres@fce40690-0e46-4603-e80e-ca351bda31ec ~]$ cat /opt/oracle/instantclient/network/admin/tnsnames.ora

# tnsnames.ora Network Configuration File: /ora/oracle/product/11.2.0/dbhome_1/network/admin/tnsnames.ora

# Generated by Oracle configuration tools.

ORCL =

(DESCRIPTION =

(ADDRESS = (PROTOCOL = TCP)(HOST = 10.9.10.236)(PORT = 1521))

(CONNECT_DATA =

(SERVER = DEDICATED)

(SERVICE_NAME = orcl)

)

)

3.创建外部表

1)创建server:

create server oracle_test foreign data wrapper oracle_fdw options(dbserver 'ORCL');

2)创建mapping:

create user mapping for postgres server oracle_test options (user 'oracle', password 'oracle');

3)创建外部表:

CREATE FOREIGN TABLE test(

id int

) SERVER oracle_test options (schema 'ORACLE', table 'TEST');

4)检查:

postgres=# select oracle_diag();

oracle_diag

-------------------------------------------------------------

oracle_fdw 2.1.0, PostgreSQL 11.2, Oracle client 18.5.0.0.0

(1 row)

5)查询:

postgres=# select * from test;

id

----

1

(1 row)

6)查看外部表相关对象:

postgres=# \des
List of foreign servers
Name | Owner | Foreign-data wrapper
-------------+----------+----------------------
oracle_test | postgres | oracle_fdw
(1 row) postgres=# \deu
List of user mappings
Server | User name
-------------+-----------
oracle_test | postgres
(1 row) postgres=# \det
List of foreign tables
Schema | Table | Server
--------+-------------+-------------
public | test | oracle_test
public | test_no_fdw | oracle_test
public | test_seq | oracle_test
(3 rows)

4.在查询时如果提示错误:ORA-12154

postgres=# select * from test;

ERROR:  connection for foreign table "test" cannot be established

DETAIL:  ORA-12154: TNS:could not resolve the connect identifier specified

1)确认环境变量是否在postgres用户下设置

2)确认oracle的instantclient是否有postgres的访问权限

5.外部表的一些配置选项:

Foreign table options

table (required)

The Oracle table name. This name must be written exactly as it occurs in Oracle's system catalog, so normally consist of uppercase letters only.

To define a foreign table based on an arbitrary Oracle query, set this option to the query enclosed in parentheses, e.g.

OPTIONS (table '(SELECT col FROM tab WHERE val = ''string'')')
Do not set the schema option in this case.
INSERT, UPDATE and DELETE will work on foreign tables defined on simple queries; if you want to avoid that (or confusing Oracle error messages for more complicated queries), use the table option readonly. schema (optional) The table's schema (or owner). Useful to access tables that do not belong to the connecting Oracle user. This name must be written exactly as it occurs in Oracle's system catalog, so normally consist of uppercase letters only. max_long (optional, defaults to "32767") The maximal length of any LONG or LONG RAW columns in the Oracle table. Possible values are integers between 1 and 1073741823 (the maximal size of a bytea in PostgreSQL). This amount of memory will be allocated at least twice, so large values will consume a lot of memory.
If max_long is less than the length of the longest value retrieved, you will receive the error message ORA-01406: fetched column value was truncated. readonly (optional, defaults to "false") INSERT, UPDATE and DELETE is only allowed on tables where this option is not set to yes/on/true. Since these statements can only be executed from PostgreSQL 9.3 on, setting this option has no effect on earlier versions. It might still be a good idea to set it in PostgreSQL 9.2 and earlier on tables that you do not wish to be changed, to be prepared for an upgrade to PostgreSQL 9.3 or later. sample_percent (optional, defaults to "100") This option only influences ANALYZE processing and can be useful to ANALYZE very large tables in a reasonable time. The value must be between 0.000001 and 100 and defines the percentage of Oracle table blocks that will be randomly selected to calculate PostgreSQL table statistics. This is accomplished using the SAMPLE BLOCK (x) clause in Oracle. ANALYZE will fail with ORA-00933 for tables defined with Oracle queries and may fail with ORA-01446 for tables defined with complex Oracle views. prefetch (optional, defaults to "200") Sets the number of rows that will be fetched with a single round-trip between PostgreSQL and Oracle during a foreign table scan. This is implemented using Oracle row prefetching. The value must be between 0 and 10240, where a value of zero disables prefetching. Higher values can speed up performance, but will use more memory on the PostgreSQL server.

但是在实际操作过程中,往往会遇到oracle中通dblink访问其他数据库创建的synonym(同义词),这些同义词包括了表、视图、序列、包,那么是否可以通过oracle_fdw来访问序列和包呢?

oracle_fdw的外部表可以是一个SQL语句,基于此,下面做一些实验:

6.通过oracle_fdw访问oracle的序列、包和函数等对象:

1)测试序列

oracle端:

create sequence test_id_seq

start with 1

increment by 1

nomaxvalue

nominvalue

nocycle

nocache;

select test_id_seq.nextval from dual;

pg端:

drop foreign table test_seq;

CREATE FOREIGN TABLE test_seq(

id int

) SERVER oracle_test options (table '(select test_id_seq.nextval from dual)', readonly 'yes');

不能配置schema名称,SQL要用括号括起来。

但还是报错:

postgres=# select * from test_seq;

ERROR:  error describing remote table: OCIStmtExecute failed to describe table

DETAIL:  ORA-02287: sequence number not allowed here

说明不能通过外部表的方式访问序列???

测试访问表的SQL:

drop foreign table test_seq;

CREATE FOREIGN TABLE test_seq(

id int

) SERVER oracle_test options (table '(select * from test)', readonly 'yes');

select * from test_seq;

postgres=#

id

----

1

(1 row)

测试访问一个没有定义为外部表的表:

drop foreign table test_no_fdw;

CREATE FOREIGN TABLE test_no_fdw(

id int

) SERVER oracle_test options (table '(select * from test2)', readonly 'yes');

select * from test_no_fdw;

postgres=#

id

----

1

1

1

(3 rows)

可以访问,说明可以通过这种方式来下放表的jion,因为说明文档中说了,两个外部表的jion会下推到oracle端,增强效率,但是两个表都应该是外部表。

现在又多了一种选择。

2)测试函数:

#如果函数有入参怎么处理?

下面实验一下没有入参的函数,看看能否访问:

实验存储过程:

Oracle端:

create or replace PROCEDURE firstPro

IS

BEGIN

DBMS_OUTPUT.PUT_LINE('Hello World!');

END;

pg端:

drop foreign table test_procdure;

CREATE FOREIGN TABLE test_procdure(

info text

) SERVER oracle_test options (table '(begin firstPro(); end;)', readonly 'yes');

select * from test_procdure;

postgres=# select * from test_procdure;

ERROR:  error describing remote table: OCIStmtExecute failed to describe table

DETAIL:  ORA-00907: missing right parenthesis

postgres=# \d+ test_procdure

Foreign table "public.test_procdure"

Column | Type | Collation | Nullable | Default | FDW options | Storage  | Stats target | Description

--------+------+-----------+----------+---------+-------------+----------+--------------+-------------

info   | text |           |          |         |             | extended |              |

Server: oracle_test

FDW options: ("table" '(begin firstPro(); end;)', readonly 'yes')

结论是不能用。

oracle_fdw安装及使用(无法访问oracle存储过程等对象)的更多相关文章

  1. [转]使用ADO.NET访问Oracle存储过程

    本文转自:http://www.cnblogs.com/datasky/archive/2007/11/07/952141.html 本文讨论了如何使用 ADO.NET 访问 Oracle 存储过程( ...

  2. 在PostgreSQL中使用oracle_fdw访问Oracle

    本文讲述如何在PostgreSQL中使用oracle_fdw访问Oracle上的数据. 1. 安装oracle_fdw 可以参照:oracle_fdw in github 编译安装oracle_fdw ...

  3. Debian下无root权限使用Python访问Oracle

    这篇文章的起因是,在公司的服务器上没有root权限,但是需要使用 Python 访问 Oracle,而不管是使用 pip 安装组件还是安装 Oracle 的 client,都需要相应权限.本文即解决该 ...

  4. Oracle存储过程创建及调用(转)

    在大型数据库系统中,有两个很重要作用的功能,那就是存储过程和触发器.在数据库系统中无论是存储过程还是触发器,都是通过SQL 语句和控制流程语句的集合来完成的.相对来说,数据库系统中的触发器也是一种存储 ...

  5. Oracle03——游标、异常、存储过程、存储函数、触发器和Java代码访问Oracle对象

    作者: kent鹏 转载请注明出处: http://www.cnblogs.com/xieyupeng/p/7476717.html 1.游标(光标)Cursor 在写java程序中有集合的概念,那么 ...

  6. 安装并使用Oracle SQL Developer访问Oracle

    ---问题 如何安装并使用Oracle SQL Developer访问Oracle. ---步骤 Oracle SQL Developer是Oracle官方出品的免费图形化开发工具,相对SQL*Plu ...

  7. .net(C#)访问Oracle数据库的几种免安装组件的对比

    Oracle 数据存取组件(ODAC) 库为Borland Delphi,C++ Builder 以及 Kylix提供了一些非可视化的组件.它们用来存取Oracle关系数据库系统.与BDE类似, OD ...

  8. .net(C#)访问Oracle数据库的几种免安装组件的对比(转)

    原文地址 [内容为转载,个人推荐还是用官方的组件,推荐使用 Oracle.DataAccess.dll ] .net(C#)编程过程中,使用到了以下三种免安装的Oracle访问组件,能够不安装Orac ...

  9. 在代码生成工具Database2Sharp中使用ODP.NET(Oracle.ManagedDataAccess.dll)访问Oracle数据库,实现免安装Oracle客户端,兼容32位64位Oracle驱动

    由于我们开发的辅助工具Database2Sharp需要支持多种数据库,虽然我们一般使用SQLServer来开发应用较多,但是Oracle等其他数据库也是常用的数据库之一,因此也是支持使用Oracle等 ...

随机推荐

  1. python selenium 相关操作

    selenium : 是一个用于Web应用程序测试的工具.Selenium测试直接运行在浏览器中,就像真正的用户在操作一样.支持的浏览器包括IE(7, 8, 9, 10, 11),Mozilla Fi ...

  2. 【NOIP2017提高组模拟12.24】B

    题目 现在你有N个数,分别为A1,A2,-,AN,现在有M组询问需要你回答.每个询问将会给你一个L和R(L<=R),保证Max{Ai}-Min{Ai}<=R-L,你需要找出并输出最小的K( ...

  3. Ubuntu18.04下更改apt源为阿里云源

    1.复制源文件备份,以防万一 我们要修改的文件是sources.list,它在目录/etc/apt/下,sources.list是包管理工具apt所用的记录软件包仓库位置的配置文件,同样类型的还有位于 ...

  4. 苹果cms模板文件不存在:public/jump.html

    1,模板文件不存在很显然就是模板缺少文件导致,缺少什么文件一般都会提示.(如上图)点击首页的时,有的能进入播放页  有的提示(上图) 模版文件不存在:public/jump.html 通过查询苹果cm ...

  5. 限制 button 在 3 秒内不可重复点击

    在下载或者上传文件过程中避免重复点击带来的多次同样的请求造成资源浪费,限制 button 的点击次数是很有必要的. 1. 增强用户体验,2. 减轻服务器压力. HTML 代码 <button i ...

  6. 分布式架构基石-TCP通信协议

    为什么会有TCP/IP协议 在世界上各地,各种各样的电脑运行着各自不同的操作系统为大家服务,这些电脑在表达同一种信息的时候所使用的方法是千差万别.就好像圣经中上帝打乱了各地人的口音,让他们无法合作一样 ...

  7. Bug管理工具MantisBT-2.18.0安装教程

    Bug管理工具MantisBT安装教程 MantisBT官网下载地址:https://sourceforge.net/projects/mantisbt/# 写于:2018.12.1 如上传博客资料图 ...

  8. 使用注解装配Bean

    注解@Component代表Spring Ioc 会把 这个类扫描生产Bean 实例,而其中 value属性代表这个类在Spring 中的id,这就相当于XML方式定义的Bean  的 id 现在有了 ...

  9. 基于球分割的空间二叉树检索算法(sphere-kdtree)

    sphere-kdtree算法思路说明 具体思路如下: 第一.球半径分割,即利用不同的球半径,将三维空间点(向量)分割成多块.所以首先要求确定的就是分割多少块,怎么设置半径最合理. 第二.三维空间点平 ...

  10. 常见iPhone设备尺寸及分辨率(持续更新)

    开发中常用的px与pt区别 px就是表示pixel,像素,是屏幕上显示数据的最基本的点: pt就是point,是印刷行业常用单位,等于1/72英寸. px全称为pixel,是一个点,它不是自然界的长度 ...