ApsaraDB RDS for PostgreSQL provides the oracle_fdw extension. You can use this extension to connect to an Oracle database and synchronize its data by using foreign tables in PostgreSQL.
Prerequisites
-
Your ApsaraDB RDS for PostgreSQL instance must meet the following requirements:
-
The major version is PostgreSQL 12.
-
The minor engine version is 20200421 or later.
NoteYou can run
SHOW rds_supported_extensions;to check whether oracle_fdw is supported. If not, upgrade the minor engine version. -
-
The Oracle Client version is 11.2 or later.
-
The required Oracle Server version depends on the Oracle Client version. For more information, see the official Oracle documentation.
-
Add the VPC CIDR block of your ApsaraDB RDS for PostgreSQL instance (for example,
172.xx.xx.xx/16) to the IP address whitelist of the Oracle database instance.NoteYou can find the VPC CIDR block on the Database Connection page of your instance. On the Database Connection page, find the CIDR block (for example,
172.x.x.x/16) in the Network Type row. For more information, click the question mark icon next to the CIDR block.
Background information
oracle_fdw is a PostgreSQL foreign table extension. It allows you to read data from an Oracle database and simplifies data synchronization between PostgreSQL and Oracle.
For more information, see the oracle_fdw documentation.
Usage notes
-
To perform
UPDATEandDELETEoperations, you must set thekeyparameter for the primary key columns when you create the foreign table. For more information, see Create a foreign table. -
oracle_fdw must be able to recognize and convert the data types of columns in a foreign table. For information about the data type conversion rules for the oracle_fdw extension, see Data types.
-
WHEREandORDER BYclauses support pushdown, which means that oracle_fdw sends these clauses to the Oracle database for processing. -
JOINoperations support pushdown, but the following limits apply:-
The tables must be defined on the same foreign server.
-
Pushdown is not supported for
JOINoperations that involve three or more tables. -
The
JOINoperation must be part of aSELECTstatement. -
CROSS JOINoperations without aJOINcondition do not support pushdown. -
If a
JOINclause is pushed down, theORDER BYclause is not.
-
-
After you install the PostGIS extension, the oracle_fdw extension supports the following spatial data types:
-
POINT
-
LINE
-
POLYGON
-
MULTIPOINT
-
MULTILINE
-
MULTIPOLYGON
-
Install and uninstall the extension
Before you install the extension, confirm that your ApsaraDB RDS for PostgreSQL instance has a supported major and minor engine version. For more information, see Prerequisites.
Install the extension
CREATE EXTENSION oracle_fdw;
Uninstall the extension
DROP EXTENSION oracle_fdw;
Procedure
-
Create a server to map to the Oracle database. You can use one of the following commands:
CREATE SERVER <server_name> FOREIGN DATA WRAPPER oracle_fdw OPTIONS (dbserver '//<oracle_internal_endpoint>:<oracle_internal_port>/<database_name>');CREATE SERVER oradb FOREIGN DATA WRAPPER oracle_fdw OPTIONS (host '<oracle_internal_endpoint>', port '<oracle_internal_port>', dbname '<database_name>');
-
CREATE USER MAPPING FOR <postgresql_username> SERVER <server_name> OPTIONS (user '<oracle_username>', password '<oracle_password>');NoteIf you do not want to store Oracle user credentials in the PostgreSQL database, you can set the
useroption to an empty string and provide the required external authorization.Example:
CREATE USER MAPPING FOR pguser SERVER oradb OPTIONS (user 'orauser', password 'orapwd'); -
CREATE FOREIGN TABLE oratab ( id integer OPTIONS (key 'true') NOT NULL, text character varying(30), floating double precision NOT NULL ) SERVER oradb OPTIONS (table 'ORATAB', schema 'ORAUSER', max_long '32767', readonly 'false', sample_percent '100', prefetch '200');NoteThe structure of the foreign table must match the structure of the corresponding Oracle table.
The following table describes the parameters in the
OPTIONSclause.Parameter
Description
key
Specifies whether the corresponding column is a primary key. Valid values are
trueandfalse. Default value:false. To performUPDATEandDELETEoperations, you must set this parameter totruefor all primary key columns.table
The name of the table. This parameter is required and is typically in uppercase. You can also set this parameter's value to an Oracle SQL expression. Example:
OPTIONS (table '(SELECT col FROM tab WHERE val = ''string'')'). If you use an SQL expression, do not use theschemaparameter.schema
The owner of the table, which is typically the Oracle username in uppercase. Use this parameter to access tables that do not belong to the current connection user.
max_long
The maximum length of columns of the
LONG,LONG RAW, andXMLTYPEdata types in the Oracle table. Value range: 1 to 1073741823. Default value: 32767.readonly
Sets the Oracle table to read-only, which prevents
INSERT,UPDATE, andDELETEoperations.sample_percent
The sampling percentage of the Oracle table data that is used for PostgreSQL table statistics. Value range: 0.000001 to 100. Default value: 100.
prefetch
The number of rows transferred at a time between PostgreSQL and Oracle during a foreign table scan. Value range: 0 to 1024. Default value: 200. A value of 0 disables the prefetch feature.
After you complete these steps, you can operate on the Oracle table through the foreign table. Basic operations such as DELETE, INSERT, UPDATE, and SELECT are supported. You can also import foreign table definitions by running the following command:
IMPORT FOREIGN SCHEMA <ora_schema_name>
FROM SERVER <server_name>
INTO <schema_name>
OPTIONS (case 'lower');
The case parameter can have the following values:
-
keep: Retains the object names from Oracle, which are typically in uppercase. -
lower: Converts all object names to lowercase. -
smart: Converts object names that consist of all uppercase letters to lowercase.