pg_last_committed_xact() 是一个系统函数,用于返回最后已提交事务的提交时间戳。
pg_last_committed_xact() 在 PostgreSQL 9.5 中添加。
用法
pg_last_committed_xact () → record (xidxid,timestamptimestamp with time zone,roidentoid)
PostgreSQL 13 及更早版本
pg_last_committed_xact () → record (xidxid,timestamptimestamp with time zone)
必须启用 track_commit_timestamp,否则 pg_last_committed_xact() 将会发出 ERROR。
变更历史
- PostgreSQL 14
- 添加了输出列
roident(提交 b1e48bbe)
- 添加了输出列
- PostgreSQL 9.5
- 添加于 (提交 73c986ad)
示例
pg_last_committed_xact() 的用法示例(PostgreSQL 14 及更高版本)
postgres=# SELECT * FROM pg_last_committed_xact();
xid | timestamp | roident
-----+-------------------------------+---------
742 | 2023-04-25 14:29:20.253801+01 | 1
(1 row)
postgres=# INSERT INTO foo VALUES (12, current_setting('cluster_name'), clock_timestamp());
INSERT 0 1
postgres=# SELECT * FROM pg_last_committed_xact();
xid | timestamp | roident
-----+------------------------------+---------
743 | 2023-04-25 14:29:41.15406+01 | 0
(1 row)
请注意,第一个语句结果中非零的 roident 值表示事务起源于不同的节点。
在 PostgreSQL 13 及更早版本
postgres=# SELECT xid, timestamp FROM pg_last_committed_xact(); xid | timestamp -----+------------------------------- 744 | 2021-07-07 09:07:37.811665+01 (1 row) postgres=# INSERT INTO xact_test VALUES (4, 'boo'); INSERT 0 1 postgres=# SELECT xid, timestamp FROM pg_last_committed_xact(); xid | timestamp -----+------------------------------- 745 | 2021-07-07 09:07:43.036451+01 (1 row)
在一个刚启动且尚未发生任何提交的系统中,将返回 NULL 值。
postgres=# SELECT xid, timestamp FROM pg_last_committed_xact();
xid | timestamp
-----+-----------
|
(1 row)
如果 track_commit_timestamp 被设置为 off,pg_last_committed_xact() 将会以 ERROR 失败。
postgres=# SELECT xid, timestamp FROM pg_last_committed_xact(); ERROR: could not get commit timestamp data HINT: Make sure the configuration parameter "track_commit_timestamp" is set.
参考资料
- PostgreSQL 文档: 已提交事务信息函数
