人大金仓 DBA 金仓数据库KingbaseES 中的xmin,xmax等系统字段说明 - test(# partition p1 values (1,2,3,4,5),

📅 2026-12-01 👤 拼多多助力营销技巧 📂 拼多多助力2026 👁️ 256 次阅读

在KingbaseES中,当我们创建一个数据表时,数据库会隐式增加几个系统字段。这些字段由系统进行维护,用户一般不会感知它们的存在。例如,以下语句创建了一个简单的表:

create table test(col number);

insert into test(col) values (1),(2),(3);

从定义上来看,表 test 中只有一个字段;但是当我们查询数据字典表 sys_attribute 时,结果却不是如此:

test=# \d test

数据表 "public.test"

栏位 | 类型 | 校对规则 | 可空的 | 预设

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

col | numeric

test=# select attname, attnum, atttypid::regtype from sys_attribute where attrelid = 'test'::regclass;

attname | attnum | atttypid

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

tableoid | -6 | oid

cmax | -5 | cid

xmax | -4 | xid

cmin | -3 | cid

xmin | -2 | xid

ctid | -1 | tid

col | 1 | numeric

(7 行记录)

查询结果显示,表test一共包含7个字段。KingbaseES为我们增加了6个额外的系统字段,它们的attnum属性都是负数。下面让我们分别看看这些系统字段的作用。

tableoidtableoid 字段代表了数据所在表的对象 id(OID),也就是数据字典表 sys_class 中与该表信息相关的数据行。

test=# select oid, relname from sys_class where relname = 'test';

oid | relname

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

51137 | test

(1 行记录)

test=# select tableoid, col from test ;

tableoid | col

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

51137 | 1

51137 | 2

51137 | 3

(3 行记录)

tableoid 的另一个用途就是在涉及分区表查询或者UNION操作时标识数据行所在的具体表(分区)。例如存在以下分区表:

test=# create table t_list(a int,b int)

test-# partition by list(a)

test-# (

test(# partition p1 values (1,2,3,4,5),

test(# partition p2 values (6,7,8,9,10)

test(# );

CREATE TABLE

test=# insert into t_list values(1,1);

INSERT 0 1

test=# insert into t_list values(7,1);

INSERT 0 1

test=# select tableoid::regclass,a from t_list;

tableoid | a

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

t_list_p1 | 1

t_list_p2 | 7

(2 行记录)

ctidctid 字段代表了数据行在表中的物理位置,也就是行标识(tuple identifier),由一对数值组成(块编号和行索引)。ctid 类似于 Oracle 中的伪列 ROWID。需要注意的是,ctid的值有可能会改变(例如 VACUUM FULL);因此ctid不适合作为一个长期的行标识,应该使用主键作为行的逻辑标识。

test=# select ctid ,col from test;

ctid | col

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

(0,1) | 1

(0,2) | 2

(0,3) | 3

(3 行记录)

test=# delete from test where col = 1;

DELETE 1

test=# select ctid ,col from test;

ctid | col

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

(0,2) | 2

(0,3) | 3

(2 行记录)

test=# vacuum full test;

VACUUM

test=# select ctid ,col from test;

ctid | col

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

(0,1) | 2

(0,2) | 3

(2 行记录)

xminxmin代表了该行版本(row version)的插入事务ID(XID)。行版本是数据行的具体状态,每次更新操作都会为相同的逻辑行创建一个新的行版本(多版本并发控制,MVCC)。xmin 字段可以用于查看数据行的插入时间,需要事先打开track_commit_timestamp参数(前两行插入时参数未设置所以没有查询到时间)。

test=# insert into test(col) values (4),(5),(6);

INSERT 0 3

test=# select col,to_char(sys_xact_commit_timestamp(xmin) ,'YYYY-MM-DD HH24:MI:SS') AS insert_time from test;

col | insert_time

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

2 |

3 |

4 | 2023-01-06 11:24:47

5 | 2023-01-06 11:24:47

6 | 2023-01-06 11:24:47

(5 行记录)

xmaxxmax 字段代表了删除该行的事务 ID,对于未删除的行版本显示为 0。非零的 xmax 通常意味着删除事务还没有提交,或者删除操作被回滚。

session 1:

test=# begin;

BEGIN

test=# update test set col=1 where col =4; 拼多多帮助力入口

UPDATE 1

test=# select xmax,col from test;

xmax | col

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

0 | 2

0 | 3

0 | 1

(3 行记录)

test=# delete from test where col =3;

DELETE 1

在事务未提交的情况下,在开一个新会话进行查询:

session 2

test=# select xmax,col from test;

xmax | col

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

0 | 2

42292 | 3

42292 | 4

(3 行记录)

KingbaseES的update操作是一个先delete后insert的过程,所以在事务未提交的情况下,session 2能看到记录4被42292事务删除的信息。

session 1事务提交后,session 2 查询结果:

test=# select xmax,col from test;

xmax | col

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

0 | 2

0 | 1

(2 行记录)

cmin/cmax代表元组变更的命令在插入事务中的命令标识(从0开始累加)。

test=# begin;

BEGIN

test=# insert into test values (3);

INSERT 0 1

test=# insert into test values (4);

INSERT 0 1

test=# insert into test values (5);

INSERT 0 1

test=# commit;

COMMIT

test=# insert into test values (6);

INSERT 0 1

test=# select cmin,cmax,col,xmin from test;

cmin | cmax | col | xmin

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

0 | 0 | 2 | 42290

0 | 0 | 1 | 42292

0 | 0 | 3 | 42293

1 | 1 | 4 | 42293

2 | 2 | 5 | 42293

0 | 0 | 6 | 42294

更多信息,参见https://help.kingbase.com.cn/v8/index.html

精彩内容,人大金仓

标签: 拼多多助力网站平台 赚钱项目 拼多多助力网 首码项目 手赚APP
拼多多助力营销技巧