postgresql有rowid函数吗
小编给大家分享一下postgresql有rowid函数吗,希望大家阅读完这篇文章后大所收获,下面让我们一起去探讨吧!
oracle中可以通过rowid定位到一条数据。索引扫描就是先根据查询条件的到对应数据的rowid,然后通过rowid得到数据,也可以直接使用rowid来查询数据,比如select * from tbl where rowid=xxx; 在没有行迁移的情况下,rowid是固定不变的。
在pg中索引扫描是先查询到数据的ctid,然后根据ctid去得到相应的数据。但是ctid和oracle中的rowid并不完全相同,因为pg中多版本的原因,ctid是会发生变化的,例如:
bill=#selectctid,*fromt1limit5;ctid|id--------+----(0,1)|1(0,2)|2(0,3)|3(0,4)|4(0,5)|5(5rows)bill=#updatet1setid=111whereid=1;UPDATE1bill=#vacuumANALYZEt1;VACUUMbill=#selectctid,*fromt1limit5;ctid|id--------+----(0,2)|2(0,3)|3(0,4)|4(0,5)|5(0,6)|6(5rows)
那在pg中如何实现类似rowid类似的功能呢?
—方法一:sequence 唯一标识
bill=#createtabletbl(rowidserial8notnull,c1int,c2int);CREATETABLEbill=#createuniqueindexidx_tbl_1ontbl(rowid);CREATEINDEXbill=#insertintotbl(c1,c2)values(1,2);INSERT01bill=#insertintotbl(c1,c2)values(1,2);INSERT01bill=#insertintotbl(c1,c2)values(1,2);INSERT01bill=#select*fromtbl;rowid|c1|c2------+----+----1|1|22|1|23|1|2(3rows)
—方法二:identify列
bill=#createtabletbl(rowidint8GENERATEDALWAYSASIDENTITYnotnull,c1int,c2int);createuniqueindexidx_tbl_1ontbl(rowid);CREATETABLEbill=#createuniqueindexidx_tbl_1ontbl(rowid);CREATEINDEXbill=#insertintotbl(c1,c2)values(1,2);INSERT01bill=#insertintotbl(c1,c2)values(1,2);INSERT01bill=#insertintotbl(c1,c2)values(1,2);INSERT01bill=#select*fromtbl;rowid|c1|c2------+----+----1|1|22|1|23|1|2(3rows)
—方法三:oid
bill=#/dToidListofdatatypesSchema|Name|Description-----------+------+-------------------------------------------pg_catalog|oid|objectidentifier(oid),maximum4billion(1row)
不过oid不适合存储超过40亿条记录的表。
postgres=#createtabletbl(c1int,c2int)withoids;CREATETABLEpostgres=#createuniqueindexidx_tbl_oidontbl(oid);CREATEINDEXpostgres=#insertintotbl(c1,c2)values(1,2);INSERT1685281postgres=#insertintotbl(c1,c2)values(1,2);INSERT1685291postgres=#insertintotbl(c1,c2)values(1,2);INSERT1685301postgres=#selectoid,*fromtbl;oid|c1|c2--------+----+----168528|1|2168529|1|2168530|1|2(3rows)
不过需要注意的是,在pg12中已经不支持create table xxx with oids这种语法了。
bill=#createtabletbl(c1int,c2int)withoids;psql:ERROR:syntaxerroratornear"oids"LINE1:createtabletbl(c1int,c2int)withoids;
pg12中是将oid作为一种数据类型来使用:
bill=#createtabletbl(oidoid,c1int,c2int);CREATETABLE
看完了这篇文章,相信你对postgresql有rowid函数吗有了一定的了解,想了解更多相关知识,欢迎关注亿速云行业资讯频道,感谢各位的阅读!
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。