PostgreSQL 9.5新增了配置项track_commit_timestamp,它是用来开启跟踪记录事务提交的时间戳。
配置
编辑postgresql.conf,添加配置先如下:
track_commit_timestamp = on
接着重启PostgreSQL。
查询
使用函数pg_xact_commit_timestamp查询记录更新或插入的时间。具体事务可以传transaction_id,也可以使用系统列xmin作为函数的参数,如下:
# INSERT INTO colours VALUES ('mauve'),('cyan'),('indigo'); INSERT # SELECT pg_xact_commit_timestamp(xmin), * FROM colours; pg_xact_commit_timestamp | id | name -------------------------------+----+-------- | 1 | red | 2 | green | 3 | blue 2015-10-02 11:16:34.678267+01 | 4 | mauve 2015-10-02 11:16:34.678267+01 | 5 | cyan 2015-10-02 11:16:34.678267+01 | 6 | indigo
需要注意的是,跟踪的时间只对在配置重启PostgreSQL后的记录有效,之前的记录是没有跟踪时间。
使用pg_last_committed_xact函数查看最后提交的事务时间戳:
# SELECT * FROM pg_last_committed_xact(); xid | timestamp ------+------------------------------- 2039 | 2015-10-02 11:16:34.678267+01 (1 row)