天天看點

PostgreSQL系統列 System Columns

每個表都有隐藏的系統列,建立表列的時候不能和系統列名相同,下面講解一下PostgreSQL有哪些系統列.

(1)oid(4 bytes)

object identifier(即object ID)主要用于系統表如pg_class(記錄table的一個表),pg_namespace(記錄schema的一個表),

建立表時,如果指定with oids,則存在oid列。還可以由參數default_with_oids控制,預設是off,表示不加with oids建表時,沒有oid列。

eg:

#檢視pg_class這個條記錄對應的oid

postgres=# select oid,relname from pg_class where oid='pg_class'::regclass;

oid  | relname  

------+----------

1259 | pg_class

#建立一個新的表

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)

表對象的一個唯一辨別符,一個表隻對應一個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)

在表中的一個實體位置辨別符,和oracle的rowid類似,但有一點不同,當表被vacuum full或該行值被update時該值可能會改變。是以定義表值的唯一性最好還是自己建立一個序列值的主鍵列來辨別比較合适

(4)xmin

是插入的事務辨別符transaction ID,是用來辨別不同僚務下的一個版本控制。每一次更新該行都會改變這個值。可以和mvcc版本結合起來看

(5)xmax

是删除更新的事務辨別符transaction ID,如果該值不為0,則說明該行資料目前還未送出或復原。比如設定begin...commit事務時可以明顯看到該值的變化

(6)cmin

插入事務的指令辨別符command identifier,從0開始

(7)cmax

删除事務的指令辨別符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一樣,表示是同一個事物

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也都發生變化了

    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;

#此時的ctid變化了:

    0 |    0 | 1808 |    0 | (0,7) |  5 | cdhu

    1 |    1 | 1808 |    0 | (0,8) |  6 | cdhucdhu

(6 rows)

再開一個會話

session2:

#上面update事物還沒有結束,是以xmax現在不為0:

    0 |    0 | 1807 | 1808 | (0,5) |  5 | bbb

    1 |    1 | 1807 | 1808 | (0,6) |  6 | ccc

postgres=# commit;

繼續閱讀