每個(gè)表都有隱藏的系統(tǒng)列,創(chuàng)建表列的時(shí)候不能和系統(tǒng)列名相同,下面講解一下PostgreSQL有哪些系統(tǒng)列.
創(chuàng)新互聯(lián)公司于2013年開始,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都網(wǎng)站制作、網(wǎng)站建設(shè)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢想脫穎而出為使命,1280元象州做網(wǎng)站,已為上家服務(wù),為象州各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:13518219792
(1)oid(4 bytes)
object identifier(即object ID)主要用于系統(tǒng)表如pg_class(記錄table的一個(gè)表),pg_namespace(記錄schema的一個(gè)表),
創(chuàng)建表時(shí),如果指定with oids,則存在oid列。還可以由參數(shù)default_with_oids控制,默認(rèn)是off,表示不加with oids建表時(shí),沒有oid列。
eg:
#查看pg_class這個(gè)條記錄對應(yīng)的oid
postgres=# select oid,relname from pg_class where oid='pg_class'::regclass;
oid | relname
------+----------
1259 | pg_class
#創(chuàng)建一個(gè)新的表
postgres=# create table t1(c1 integer,c2 varchar(20)) with oids;
CREATE TABLE
postgres=# insert into t1 select 1,'aaa';
INSERT 16456 1
postgres=# insert into t1 values(2,'bbb');
INSERT 16457 1
postgres=# \d+ t1;
Table "public.t1"
Column | Type | Modifiers | Storage | Stats target | Description
--------+-----------------------+-----------+----------+--------------+-------------
c1 | integer | | plain | |
c2 | character varying(20) | | extended | |
Has OIDs: yes
postgres=# select oid,c1,c2 from t1;
oid | c1 | c2
-------+----+-----
16456 | 1 | aaa
16457 | 2 | bbb
(2)tableid(4 bytes)
表對象的一個(gè)唯一標(biāo)識符,一個(gè)表只對應(yīng)一個(gè)tableoid,可以將tableoid與pgclass的oid列連接起來,以獲得表名
postgres=# select oid,tableoid from t1;
oid | tableoid
-------+----------
16456 | 16453
16457 | 16453
16458 | 16453
postgres=# select tableoid from t2;
tableoid
----------
16464
postgres=# select oid,relname from pg_class ;
oid | relname
-------+-----------------------------------------
16453 | t1
16464 | t2
postgres=# select relname from pg_class where oid in (16453,16464);
relname
---------
t1
t2
(3)ctid(6 bytes)
在表中的一個(gè)物理位置標(biāo)識符,和oracle的rowid類似,但有一點(diǎn)不同,當(dāng)表被vacuum full或該行值被update時(shí)該值可能會改變。所以定義表值的唯一性最好還是自己創(chuàng)建一個(gè)序列值的主鍵列來標(biāo)識比較合適
(4)xmin
是插入的事務(wù)標(biāo)識符transaction ID,是用來標(biāo)識不同事務(wù)下的一個(gè)版本控制。每一次更新該行都會改變這個(gè)值??梢院蚼vcc版本結(jié)合起來看
(5)xmax
是刪除更新的事務(wù)標(biāo)識符transaction ID,如果該值不為0,則說明該行數(shù)據(jù)當(dāng)前還未提交或回滾。比如設(shè)置begin...commit事務(wù)時(shí)可以明顯看到該值的變化
(6)cmin
插入事務(wù)的命令標(biāo)識符command identifier,從0開始
(7)cmax
刪除事務(wù)的命令標(biāo)識符command identifier,或者為0
eg:
postgres=# create table t1(c1 integer,c2 varchar(20));
postgres=# insert into t1 select generate_series(1,3),repeat('hello',2);
#三行記錄的xmin一樣,表示是同一個(gè)事物
postgres=# select cmin,cmax,xmin,xmax,ctid,* from t1;
cmin | cmax | xmin | xmax | ctid | c1 | c2
------+------+------+------+-------+----+------------
0 | 0 | 1806 | 0 | (0,1) | 1 | hellohello
0 | 0 | 1806 | 0 | (0,2) | 2 | hellohello
0 | 0 | 1806 | 0 | (0,3) | 3 | hellohello
begin;
insert into t1 values(4,'aaa');
insert into t1 values(5,'bbb');
insert into t1 values(6,'ccc');
commit;
#第四行,第五行,第六行的xmin不同,表示是不同的事物,cmin和cmax也都發(fā)生變化了
postgres=# select cmin,cmax,xmin,xmax,ctid,* from t1;
cmin | cmax | xmin | xmax | ctid | c1 | c2
------+------+------+------+-------+----+------------
0 | 0 | 1806 | 0 | (0,1) | 1 | hellohello
0 | 0 | 1806 | 0 | (0,2) | 2 | hellohello
0 | 0 | 1806 | 0 | (0,3) | 3 | hellohello
0 | 0 | 1807 | 0 | (0,4) | 4 | aaa
1 | 1 | 1807 | 0 | (0,5) | 5 | bbb
2 | 2 | 1807 | 0 | (0,6) | 6 | ccc
session1:
postgres=# begin;
postgres=# update t1 set c2='cdhu' where c1=5;
postgres=# update t1 set c2='cdhucdhu' where c1=6;
#此時(shí)的ctid變化了:
postgres=# select cmin,cmax,xmin,xmax,ctid,* from t1;
cmin | cmax | xmin | xmax | ctid | c1 | c2
------+------+------+------+-------+----+------------
0 | 0 | 1806 | 0 | (0,1) | 1 | hellohello
0 | 0 | 1806 | 0 | (0,2) | 2 | hellohello
0 | 0 | 1806 | 0 | (0,3) | 3 | hellohello
0 | 0 | 1807 | 0 | (0,4) | 4 | aaa
0 | 0 | 1808 | 0 | (0,7) | 5 | cdhu
1 | 1 | 1808 | 0 | (0,8) | 6 | cdhucdhu
(6 rows)
再開一個(gè)會話
session2:
#上面update事物還沒有結(jié)束,所以xmax現(xiàn)在不為0:
postgres=# select cmin,cmax,xmin,xmax,ctid,* from t1;
cmin | cmax | xmin | xmax | ctid | c1 | c2
------+------+------+------+-------+----+------------
0 | 0 | 1806 | 0 | (0,1) | 1 | hellohello
0 | 0 | 1806 | 0 | (0,2) | 2 | hellohello
0 | 0 | 1806 | 0 | (0,3) | 3 | hellohello
0 | 0 | 1807 | 0 | (0,4) | 4 | aaa
0 | 0 | 1807 | 1808 | (0,5) | 5 | bbb
1 | 1 | 1807 | 1808 | (0,6) | 6 | ccc
session1:
postgres=# commit;
session2:
postgres=# select cmin,cmax,xmin,xmax,ctid,* from t1;
cmin | cmax | xmin | xmax | ctid | c1 | c2
------+------+------+------+-------+----+------------
0 | 0 | 1806 | 0 | (0,1) | 1 | hellohello
0 | 0 | 1806 | 0 | (0,2) | 2 | hellohello
0 | 0 | 1806 | 0 | (0,3) | 3 | hellohello
0 | 0 | 1807 | 0 | (0,4) | 4 | aaa
0 | 0 | 1808 | 0 | (0,7) | 5 | cdhu
1 | 1 | 1808 | 0 | (0,8) | 6 | cdhucdhu
分享文章:PostgreSQL系統(tǒng)列SystemColumns
URL標(biāo)題:http://chinadenli.net/article12/joiddc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App開發(fā)、網(wǎng)站導(dǎo)航、網(wǎng)頁設(shè)計(jì)公司、自適應(yīng)網(wǎng)站、小程序開發(fā)、網(wǎng)站營銷
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)