作者
digoal
日期
2017-10-24
标签
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)
```
PostgreSQL 许愿链接
您的愿望将传达给PG kernel hacker、数据库厂商等, 帮助提高数据库产品质量和功能, 说不定下一个PG版本就有您提出的功能点. 针对非常好的提议,奖励限量版PG文化衫、纪念品、贴纸、PG热门书籍等,奖品丰富,快来许愿。开不开森.