更新时间:2026-11-23 GMT+08:00

事务ID和快照

以下的函数在输出形式中提供服务器事务信息。这些函数的主要用途是为了确定在两个快照之间有哪个事务提交。

pgxc_is_committed(transaction_id)

描述:如果提交或忽略给定的XID(gxid)。NULL表示的状态是未知的(运行,准备,冻结等)。

返回类型:bool

txid_current()

描述:获取当前事务ID。

返回类型:bigint

txid_current_snapshot()

描述:获取当前快照。

返回类型:txid_snapshot

txid_snapshot_xip(txid_snapshot)

描述:在快照中获取正在进行的事务ID。

返回类型:setof bigint

txid_snapshot_xmax(txid_snapshot)

描述:获取快照的xmax。

返回类型:bigint

txid_snapshot_xmin(txid_snapshot)

描述:获取快照的xmin。

返回类型:bigint

txid_visible_in_snapshot(bigint, txid_snapshot)

描述:在快照中事务ID是否可见(不使用子事务ID)。

返回类型:boolean

内部事务ID类型(xid)是32位,每40亿事务一次循环。这些函数使用的数据类型txid_snapshot,存储在特定时刻事务ID可见性的信息。其组件描述在表1。

txid_snapshot的文本表示为:xmin:xmax:xip_list。

例如,10:20:10,14,15表示:xmin=10, xmax=20, xip_list=10, 14, 15。

表1 快照组件

名字

描述

xmin

最早的事务ID(txid)仍然活动。所有较早事务将是已经提交可见的,或者是直接回滚。

xmax

作为尚未分配的txid。所有大于或等于此txids的都是尚未开始的快照时间,因此不可见。

xip_list

当前快照中活动的txids。这个列表只包含在xmin和xmax之间活动的txids;有可能活动的txids高于xmax。 介于大于等于xmin、小于xmax,并且不在这个列表中的txid,在这个时间快照已经完成的,因此按照提交状态查看他是可见还是回滚。这个列表不包含子事务的txids。

pgxc_gtm_txn_status()

描述:获取GTM上当前所有事务的信息。该函数仅9.1.1.300及以上集群版本支持。

表2 pgxc_gtm_txn_status()返回字段

名称

类型

描述

slotnum

integer

GTM上的槽位号。

threadid

bigint

GTM上的线程号。

gxid

xid

全局事务ID。

state

text

事务状态。

xmin

xid

最小事务ID。

xmax

xid

最大事务ID。

isolevel

text

事务的隔离级别。

  • read committed:读已提交隔离级别。
  • repeatable read:可重复读隔离级别。

isreadonly

boolean

是否是只读事务。

isvacuum

boolean

是否是Vacuum事务。

nodename

text

节点名称。该字段仅9.1.2及以上集群版本支持。

remotepid

bigint

业务线程号。该字段仅9.1.2及以上集群版本支持。

示例:

1
2
3
4
5
6
7
SELECT * FROM pgxc_gtm_txn_status();
 slotnum |    threadid     |  gxid   |  state   |  xmin   |  xmax   |    isolevel     | isreadonly | isvacuum |   nodename   |    remotepid
---------+-----------------+---------+----------+---------+---------+-----------------+------------+----------+--------------+-----------------
       5 | 139836075788032 |       0 | STARTING | 1410028 |       0 | read committed  | f          | f        | coordinator1 | 139956894525952
       4 | 139836073686784 |       0 | STARTING | 1410028 |       0 | repeatable read | f          | f        | coordinator2 | 140466892542744
       2 | 139836077889280 | 1410028 | STARTING | 1410028 | 1410028 | read committed  | f          | f        | coordinator1 | 139956894535808
(3 rows)