閱讀594 返回首頁    go 阿裏雲 go 技術社區[雲棲]


PostgreSQL Oracle 兼容性之 - 係統列(ctid, oid, cmin, cmax, xmin, xmax)

標簽

PostgreSQL , Oracle , 兼容性 , ctid , cmin , cmax , xmin , xmax , oid


背景

PostgreSQL中有一些係統列(即行的頭部信息的列),例如物理行號,COMMAND ID,事務號,以及OID。

當我們建表時,不能使用衝突的列名,否則會報錯:

postgres=# create table a(ctid int);  
錯誤:  42701: 字段名 "ctid" 與係統字段名衝突  
LOCATION:  CheckAttributeNamesTypes, heap.c:439  

當Oracle用戶要遷移到PG,遇到這樣的問題怎麼辦呢?讓用戶改程序好像不太現實。

解決辦法

創建影子表(將衝突字段重命名)

postgres=# create table tbl_shadow(n_ctid int, n_xmin int, n_max int, n_oid int);  
CREATE TABLE  

創建視圖(作為業務程序中用於交互的表名),可以采用衝突字段,解決了兼容性問題。

postgres=# create view tbl1 as select n_ctid as ctid, n_xmin as xmin, n_max as xmax, n_oid as oid from tbl_shadow ;  
CREATE VIEW  

對視圖進行增刪改查,會自動轉換為對表的增刪改查。

postgres=# insert into tbl1 (ctid,xmin,xmax,oid) values (1,1,1,1);  
INSERT 0 1  
  
postgres=# select ctid from tbl1;;  
 ctid   
------  
    1  
(1 row)  
  
postgres=# update tbl1 set xmax=2;  
UPDATE 1  
  
postgres=# select * from tbl1;  
 ctid | xmin | xmax | oid   
------+------+------+-----  
    1 |    1 |    2 |   1  
(1 row)  

最後更新:2017-10-29 00:04:04

  上一篇:go  Linux 批量依賴庫拷貝(ldd)
  下一篇:go  PostgreSQL Oracle 兼容性之 - timestamp 與 numeric 的運算