网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

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=# select ctid,* from t1 limit 5;
 ctid | id 
--------+----
 (0,1) | 1
 (0,2) | 2
 (0,3) | 3
 (0,4) | 4
 (0,5) | 5
(5 rows)
bill=# update t1 set id=111 where id =1;
UPDATE 1
bill=# vacuum ANALYZE t1;
VACUUM
bill=# select ctid,* from t1 limit 5;   
 ctid | id 
--------+----
 (0,2) | 2
 (0,3) | 3
 (0,4) | 4
 (0,5) | 5
 (0,6) | 6
(5 rows)

那在pg中如何实现类似rowid类似的功能呢?

—方法一:sequence 唯一标识

bill=# create table tbl (rowid serial8 not null, c1 int, c2 int);  
CREATE TABLE
bill=# create unique index idx_tbl_1 on tbl(rowid);  
CREATE INDEX
bill=# insert into tbl (c1,c2) values (1,2);  
INSERT 0 1
bill=# insert into tbl (c1,c2) values (1,2);  
INSERT 0 1
bill=# insert into tbl (c1,c2) values (1,2);  
INSERT 0 1
bill=# select * from tbl;
rowid | c1 | c2 
------+----+----
   1 |  1 | 2
   2 |  1 | 2
   3 |  1 | 2
(3 rows)

—方法二:identify列

bill=# create table tbl (rowid int8 GENERATED ALWAYS AS IDENTITY not null, c1 int, c2 int);  
create unique index idx_tbl_1 on tbl(rowid);  CREATE TABLE
bill=# create unique index idx_tbl_1 on tbl(rowid);  
CREATE INDEX
bill=# insert into tbl (c1,c2) values (1,2);  
INSERT 0 1
bill=# insert into tbl (c1,c2) values (1,2);  
INSERT 0 1
bill=# insert into tbl (c1,c2) values (1,2);  
INSERT 0 1
bill=# select * from tbl;  
rowid | c1 | c2 
------+----+----
  1  | 1 |  2
  2  |  1 | 2
  3  | 1 | 2
(3 rows)

—方法三:oid

bill=# /dT oid
              List of data types
Schema   | Name |  Description                
-----------+------+-------------------------------------------
pg_catalog | oid |  object identifier(oid), maximum 4 billion
(1 row)

不过oid不适合存储超过40亿条记录的表。

postgres=# create table tbl (c1 int, c2 int) with oids;  
CREATE TABLE
postgres=# create unique index idx_tbl_oid on tbl(oid); 
CREATE INDEX
postgres=# insert into tbl (c1,c2) values (1,2);  
INSERT 168528 1
postgres=# insert into tbl (c1,c2) values (1,2);  
INSERT 168529 1
postgres=# insert into tbl (c1,c2) values (1,2);  
INSERT 168530 1
postgres=# select oid,* from tbl;  
oid    | c1 | c2 
--------+----+----
168528  | 1 | 2
168529  | 1 | 2
168530  | 1 | 2
(3 rows)

不过需要注意的是,在pg12中已经不支持create table xxx with oids这种语法了。

bill=# create table tbl (c1 int, c2 int) with oids; 
psql: ERROR:  syntax error at or near "oids"
LINE 1: create table tbl (c1 int, c2 int) with oids;

pg12中是将oid作为一种数据类型来使用:

postgresql有rowid函数吗

bill=# create table tbl(oid oid,c1 int,c2 int);
CREATE TABLE

看完了这篇文章,相信你对postgresql有rowid函数吗有了一定的了解,想了解更多相关知识,欢迎关注创新互联行业资讯频道,感谢各位的阅读!


网页名称:postgresql有rowid函数吗
标题链接:http://bjjierui.cn/article/jessis.html

其他资讯