PostgreSQL Oracle 兼容性係列之 - orafce
PostgreSQL是和Oracle最接近的企業數據庫,包括數據類型,功能,架構和語法等幾個方麵。甚至大多數的日常應用的性能也不會輸給Oracle。
但是Oracle有些函數或者包,默認PostgreSQL是沒有的,需要安裝orafce包來實現這些兼容性。
例如現在orafce已經包含了如下內容。
1. 類型 date, varchar2 and nvarchar2
2. 函數 concat, nvl, nvl2, lnnvl, decode, bitand, nanvl, sinh, cosh, tanh and oracle.substr
3. dual 表
4. package :
dbms_output
utl_file
dbms_pipe
dbms_alert
PLVdate
PLVstr and PLVchr
PLVsubst
DBMS_utility
PLVlex
DBMS_ASSERT
PLUnit
DBMS_random
orafce的安裝步驟如下:
https://pgxn.org/dist/orafce/
下載最新版本。
wget https://api.pgxn.org/dist/orafce/3.1.2/orafce-3.1.2.zip
安裝
unzip orafce-3.1.2.zip
mv orafce-3.1.2 /opt/soft_bak/postgresql-9.4.5/contrib
cd /opt/soft_bak/postgresql-9.4.5/contrib/orafce-3.1.2
把pg_config命令放到當前路徑,之後就可以編譯安裝。
export PATH=/opt/pgsql/bin:$PATH
make clean
make
make install
創建extension 。
su - postgres
psql
postgres=# create extension orafce;
CREATE EXTENSION
Oracle兼容 函數列表:
postgres=# \df
List of functions
Schema | Name | Result data type | Argument data types | Type
--------+---------------------+-----------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+--------
public | bitand | bigint | bigint, bigint | normal
public | cosh | double precision | double precision | normal
public | decode | bigint | anyelement, anyelement, bigint | normal
public | decode | bigint | anyelement, anyelement, bigint, anyelement, bigint | normal
public | decode | bigint | anyelement, anyelement, bigint, anyelement, bigint, anyelement, bigint | normal
public | decode | bigint | anyelement, anyelement, bigint, anyelement, bigint, anyelement, bigint, bigint | normal
public | decode | bigint | anyelement, anyelement, bigint, anyelement, bigint, bigint | normal
public | decode | bigint | anyelement, anyelement, bigint, bigint | normal
public | decode | character | anyelement, anyelement, character | normal
public | decode | character | anyelement, anyelement, character, anyelement, character | normal
public | decode | character | anyelement, anyelement, character, anyelement, character, anyelement, character | normal
public | decode | character | anyelement, anyelement, character, anyelement, character, anyelement, character, character | normal
public | decode | character | anyelement, anyelement, character, anyelement, character, character | normal
public | decode | character | anyelement, anyelement, character, character | normal
public | decode | date | anyelement, anyelement, date | normal
public | decode | date | anyelement, anyelement, date, anyelement, date | normal
public | decode | date | anyelement, anyelement, date, anyelement, date, anyelement, date | normal
public | decode | date | anyelement, anyelement, date, anyelement, date, anyelement, date, date | normal
public | decode | date | anyelement, anyelement, date, anyelement, date, date | normal
public | decode | date | anyelement, anyelement, date, date | normal
public | decode | integer | anyelement, anyelement, integer | normal
public | decode | integer | anyelement, anyelement, integer, anyelement, integer | normal
public | decode | integer | anyelement, anyelement, integer, anyelement, integer, anyelement, integer | normal
public | decode | integer | anyelement, anyelement, integer, anyelement, integer, anyelement, integer, integer | normal
public | decode | integer | anyelement, anyelement, integer, anyelement, integer, integer | normal
public | decode | integer | anyelement, anyelement, integer, integer | normal
public | decode | numeric | anyelement, anyelement, numeric | normal
public | decode | numeric | anyelement, anyelement, numeric, anyelement, numeric | normal
public | decode | numeric | anyelement, anyelement, numeric, anyelement, numeric, anyelement, numeric | normal
public | decode | numeric | anyelement, anyelement, numeric, anyelement, numeric, anyelement, numeric, numeric | normal
public | decode | numeric | anyelement, anyelement, numeric, anyelement, numeric, numeric | normal
public | decode | numeric | anyelement, anyelement, numeric, numeric | normal
public | decode | text | anyelement, anyelement, text | normal
public | decode | text | anyelement, anyelement, text, anyelement, text | normal
public | decode | text | anyelement, anyelement, text, anyelement, text, anyelement, text | normal
public | decode | text | anyelement, anyelement, text, anyelement, text, anyelement, text, text | normal
public | decode | text | anyelement, anyelement, text, anyelement, text, text | normal
public | decode | text | anyelement, anyelement, text, text | normal
public | decode | time without time zone | anyelement, anyelement, time without time zone | normal
public | decode | time without time zone | anyelement, anyelement, time without time zone, anyelement, time without time zone | normal
public | decode | time without time zone | anyelement, anyelement, time without time zone, anyelement, time without time zone, anyelement, time without time zone | normal
public | decode | time without time zone | anyelement, anyelement, time without time zone, anyelement, time without time zone, anyelement, time without time zone, time without time zone | normal
public | decode | time without time zone | anyelement, anyelement, time without time zone, anyelement, time without time zone, time without time zone | normal
public | decode | time without time zone | anyelement, anyelement, time without time zone, time without time zone | normal
public | decode | timestamp with time zone | anyelement, anyelement, timestamp with time zone | normal
public | decode | timestamp with time zone | anyelement, anyelement, timestamp with time zone, anyelement, timestamp with time zone | normal
public | decode | timestamp with time zone | anyelement, anyelement, timestamp with time zone, anyelement, timestamp with time zone, anyelement, timestamp with time zone | normal
public | decode | timestamp with time zone | anyelement, anyelement, timestamp with time zone, anyelement, timestamp with time zone, anyelement, timestamp with time zone, timestamp with time zone | normal
public | decode | timestamp with time zone | anyelement, anyelement, timestamp with time zone, anyelement, timestamp with time zone, timestamp with time zone | normal
public | decode | timestamp with time zone | anyelement, anyelement, timestamp with time zone, timestamp with time zone | normal
public | decode | timestamp without time zone | anyelement, anyelement, timestamp without time zone | normal
public | decode | timestamp without time zone | anyelement, anyelement, timestamp without time zone, anyelement, timestamp without time zone | normal
public | decode | timestamp without time zone | anyelement, anyelement, timestamp without time zone, anyelement, timestamp without time zone, anyelement, timestamp without time zone | normal
public | decode | timestamp without time zone | anyelement, anyelement, timestamp without time zone, anyelement, timestamp without time zone, anyelement, timestamp without time zone, timestamp without time zone | normal
public | decode | timestamp without time zone | anyelement, anyelement, timestamp without time zone, anyelement, timestamp without time zone, timestamp without time zone | normal
public | decode | timestamp without time zone | anyelement, anyelement, timestamp without time zone, timestamp without time zone | normal
public | dump | character varying | "any" | normal
public | dump | character varying | "any", integer | normal
public | dump | character varying | text | normal
public | dump | character varying | text, integer | normal
public | nanvl | double precision | double precision, character varying | normal
public | nanvl | double precision | double precision, double precision | normal
public | nanvl | numeric | numeric, character varying | normal
public | nanvl | numeric | numeric, numeric | normal
public | nanvl | real | real, character varying | normal
public | nanvl | real | real, real | normal
public | nvarchar2 | nvarchar2 | nvarchar2, integer, boolean | normal
public | nvarchar2_transform | internal | internal | normal
public | nvarchar2in | nvarchar2 | cstring, oid, integer | normal
public | nvarchar2out | cstring | nvarchar2 | normal
public | nvarchar2recv | nvarchar2 | internal, oid, integer | normal
public | nvarchar2send | bytea | nvarchar2 | normal
public | nvarchar2typmodin | integer | cstring[] | normal
public | nvarchar2typmodout | cstring | integer | normal
public | nvl | anyelement | anyelement, anyelement | normal
public | nvl2 | anyelement | anyelement, anyelement, anyelement | normal
public | sinh | double precision | double precision | normal
public | tanh | double precision | double precision | normal
public | to_multi_byte | text | str text | normal
public | to_single_byte | text | str text | normal
public | varchar2 | varchar2 | varchar2, integer, boolean | normal
public | varchar2_transform | internal | internal | normal
public | varchar2in | varchar2 | cstring, oid, integer | normal
public | varchar2out | cstring | varchar2 | normal
public | varchar2recv | varchar2 | internal, oid, integer | normal
public | varchar2send | bytea | varchar2 | normal
public | varchar2typmodin | integer | cstring[] | normal
public | varchar2typmodout | cstring | integer | normal
(88 rows)
Oracle兼容 dual表,在PG裏用了一個視圖來實現。
postgres=# \dv
List of relations
Schema | Name | Type | Owner
--------+------+------+----------
public | dual | view | postgres
(1 row)
postgres=# \d+ dual
View "public.dual"
Column | Type | Modifiers | Storage | Description
--------+-------------------+-----------+----------+-------------
dummy | character varying | | extended |
View definition:
SELECT 'X'::character varying AS dummy;
postgres=# select * from dual;
dummy
-------
X
(1 row)
postgres=# select 1 from dual;
?column?
----------
1
(1 row)
Oracle兼容 包列表:
在PostgreSQL裏用schema+函數來實現。
postgres=# \dn
List of schemas
Name | Owner
--------------+----------
dbms_alert | postgres
dbms_assert | postgres
dbms_output | postgres
dbms_pipe | postgres
dbms_random | postgres
dbms_utility | postgres
madlib | postgres
oracle | postgres
plunit | postgres
plvchr | postgres
plvdate | postgres
plvlex | postgres
plvstr | postgres
plvsubst | postgres
public | postgres
utl_file | postgres
(16 rows)
例如dbms_alert包:
postgres=# \df dbms_alert.*
List of functions
Schema | Name | Result data type | Argument data types | Type
------------+----------------+------------------+-------------------------------------------------------------------------------+---------
dbms_alert | _signal | void | name text, message text | normal
dbms_alert | defered_signal | trigger | | trigger
dbms_alert | register | void | name text | normal
dbms_alert | remove | void | name text | normal
dbms_alert | removeall | void | | normal
dbms_alert | set_defaults | void | sensitivity double precision | normal
dbms_alert | signal | void | _event text, _message text | normal
dbms_alert | waitany | record | OUT name text, OUT message text, OUT status integer, timeout double precision | normal
dbms_alert | waitone | record | name text, OUT message text, OUT status integer, timeout double precision | normal
(9 rows)
最後更新:2017-04-01 13:44:33