PostgreSQLOracle相容性之-系統列(ctid,oid,cmin,cmax,xmin,xmax)

德哥發表於2017-10-28

標籤

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)  


相關文章