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)  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值