PostgreSQL 里每张表都有隐含字段,确切地说应该是 system Columns , 本文主要讲述下其中两个 system columns,即为 xmin , xmax。
官网解释
xmin The identity (transaction ID) of the inserting transaction for this row version. (A row version is an individual state of a row; each update of a row creates a new row version for the same logical row.)
xmax The identity (transaction ID) of the deleting transaction, or zero for an undeleted row version. It is possible for this column to be nonzero in a visible row version. That usually indicates that the deleting transaction hasn it committed yet, or that an attempted deletion was rolled back.
根据上面讲解,可以知道 xmin,xmax都指事务ID(transaction ID), 简单的说,xmin记录的是当数据插入( Insert )时的事务ID,xmax记录的是当行上的数据有变动(delete or update )时的事务ID,下面看下详细的实验过程。
测试环境准备
创建测试表并插入数据
1 2 3 4
skytf=> create table test_17 (id integer ,name varchar(32)); CREATE TABLE skytf=> insert into test_17 select generate_series(1,10),'a'; INSERT 0 10
查询表 test_17信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14
skytf=> select xmin,xmax,ctid,* from test_17; xmin | xmax | ctid | id | name ----------+------+--------+----+------ 14240187 | 0 | (0,1) | 1 | a 14240187 | 0 | (0,2) | 2 | a 14240187 | 0 | (0,3) | 3 | a 14240187 | 0 | (0,4) | 4 | a 14240187 | 0 | (0,5) | 5 | a 14240187 | 0 | (0,6) | 6 | a 14240187 | 0 | (0,7) | 7 | a 14240187 | 0 | (0,8) | 8 | a 14240187 | 0 | (0,9) | 9 | a 14240187 | 0 | (0,10) | 10 | a (10 rows)