From 73c986adde5d73a5e2555da9b5c8facedb146dcd Mon Sep 17 00:00:00 2001 From: Alvaro Herrera Date: Wed, 3 Dec 2014 11:53:02 -0300 Subject: [PATCH] Keep track of transaction commit timestamps MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Transactions can now set their commit timestamp directly as they commit, or an external transaction commit timestamp can be fed from an outside system using the new function TransactionTreeSetCommitTsData(). This data is crash-safe, and truncated at Xid freeze point, same as pg_clog. This module is disabled by default because it causes a performance hit, but can be enabled in postgresql.conf requiring only a server restart. A new test in src/test/modules is included. Catalog version bumped due to the new subdirectory within PGDATA and a couple of new SQL functions. Authors: Álvaro Herrera and Petr Jelínek Reviewed to varying degrees by Michael Paquier, Andres Freund, Robert Haas, Amit Kapila, Fujii Masao, Jaime Casanova, Simon Riggs, Steven Singer, Peter Eisentraut --- contrib/pg_upgrade/pg_upgrade.c | 7 + contrib/pg_xlogdump/rmgrdesc.c | 1 + doc/src/sgml/config.sgml | 14 + doc/src/sgml/func.sgml | 39 + doc/src/sgml/ref/pg_resetxlog.sgml | 19 +- doc/src/sgml/storage.sgml | 5 + src/backend/access/rmgrdesc/Makefile | 2 +- src/backend/access/rmgrdesc/committsdesc.c | 82 ++ src/backend/access/rmgrdesc/xlogdesc.c | 3 + src/backend/access/transam/Makefile | 5 +- src/backend/access/transam/README | 2 +- src/backend/access/transam/commit_ts.c | 902 ++++++++++++++++++ src/backend/access/transam/rmgr.c | 1 + src/backend/access/transam/slru.c | 2 +- src/backend/access/transam/varsup.c | 4 +- src/backend/access/transam/xact.c | 27 +- src/backend/access/transam/xlog.c | 43 +- src/backend/commands/vacuum.c | 8 +- src/backend/libpq/hba.c | 2 +- src/backend/replication/logical/decode.c | 1 + src/backend/storage/ipc/ipci.c | 3 + src/backend/storage/lmgr/lwlock.c | 4 + src/backend/utils/misc/guc.c | 10 + src/backend/utils/misc/postgresql.conf.sample | 2 + src/bin/initdb/initdb.c | 1 + src/bin/pg_controldata/pg_controldata.c | 4 + src/bin/pg_resetxlog/pg_resetxlog.c | 75 +- src/include/access/commit_ts.h | 72 ++ src/include/access/rmgrlist.h | 1 + src/include/access/transam.h | 6 + src/include/access/xlog_internal.h | 1 + src/include/catalog/catversion.h | 2 +- src/include/catalog/pg_control.h | 3 + src/include/catalog/pg_proc.h | 6 + src/include/storage/lwlock.h | 5 +- src/include/utils/builtins.h | 4 + src/test/modules/Makefile | 1 + src/test/modules/commit_ts/.gitignore | 4 + src/test/modules/commit_ts/Makefile | 15 + src/test/modules/commit_ts/commit_ts.conf | 1 + .../commit_ts/expected/commit_timestamp.out | 39 + .../commit_ts/expected/commit_timestamp_1.out | 34 + .../commit_ts/sql/commit_timestamp.sql | 24 + 43 files changed, 1458 insertions(+), 28 deletions(-) create mode 100644 src/backend/access/rmgrdesc/committsdesc.c create mode 100644 src/backend/access/transam/commit_ts.c create mode 100644 src/include/access/commit_ts.h create mode 100644 src/test/modules/commit_ts/.gitignore create mode 100644 src/test/modules/commit_ts/Makefile create mode 100644 src/test/modules/commit_ts/commit_ts.conf create mode 100644 src/test/modules/commit_ts/expected/commit_timestamp.out create mode 100644 src/test/modules/commit_ts/expected/commit_timestamp_1.out create mode 100644 src/test/modules/commit_ts/sql/commit_timestamp.sql diff --git a/contrib/pg_upgrade/pg_upgrade.c b/contrib/pg_upgrade/pg_upgrade.c index 3b8241b37e..dd1113ecc2 100644 --- a/contrib/pg_upgrade/pg_upgrade.c +++ b/contrib/pg_upgrade/pg_upgrade.c @@ -430,6 +430,13 @@ copy_clog_xlog_xid(void) "\"%s/pg_resetxlog\" -f -e %u \"%s\"", new_cluster.bindir, old_cluster.controldata.chkpnt_nxtepoch, new_cluster.pgdata); + /* must reset commit timestamp limits also */ + exec_prog(UTILITY_LOG_FILE, NULL, true, + "\"%s/pg_resetxlog\" -f -c %u,%u \"%s\"", + new_cluster.bindir, + old_cluster.controldata.chkpnt_nxtxid, + old_cluster.controldata.chkpnt_nxtxid, + new_cluster.pgdata); check_ok(); /* diff --git a/contrib/pg_xlogdump/rmgrdesc.c b/contrib/pg_xlogdump/rmgrdesc.c index 9397198239..180818d68b 100644 --- a/contrib/pg_xlogdump/rmgrdesc.c +++ b/contrib/pg_xlogdump/rmgrdesc.c @@ -10,6 +10,7 @@ #include "access/brin_xlog.h" #include "access/clog.h" +#include "access/commit_ts.h" #include "access/gin.h" #include "access/gist_private.h" #include "access/hash.h" diff --git a/doc/src/sgml/config.sgml b/doc/src/sgml/config.sgml index ab8c2637d7..e3713d3b49 100644 --- a/doc/src/sgml/config.sgml +++ b/doc/src/sgml/config.sgml @@ -2673,6 +2673,20 @@ include_dir 'conf.d' + + track_commit_timestamp (bool) + + track_commit_timestamp configuration parameter + + + + Record commit time of transactions. This parameter + can only be set in postgresql.conf file or on the server + command line. The default value is off. + + + + diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml index baf81ee040..62ec275a9e 100644 --- a/doc/src/sgml/func.sgml +++ b/doc/src/sgml/func.sgml @@ -15938,6 +15938,45 @@ SELECT collation for ('foo' COLLATE "de_DE"); For example 10:20:10,14,15 means xmin=10, xmax=20, xip_list=10, 14, 15. + + + The functions shown in + provide information about transactions that have been already committed. + These functions mainly provide information about when the transactions + were committed. They only provide useful data when + configuration option is enabled + and only for transactions that were committed after it was enabled. + + + + Committed transaction information + + + Name Return Type Description + + + + + + pg_xact_commit_timestamp + pg_xact_commit_timestamp(xid) + + timestamp with time zone + get commit timestamp of a transaction + + + + + pg_last_committed_xact + pg_last_committed_xact() + + xid xid, timestamp timestamp with time zone + get transaction ID and commit timestamp of latest committed transaction + + + +
+ diff --git a/doc/src/sgml/ref/pg_resetxlog.sgml b/doc/src/sgml/ref/pg_resetxlog.sgml index aba7185f35..59280f01cb 100644 --- a/doc/src/sgml/ref/pg_resetxlog.sgml +++ b/doc/src/sgml/ref/pg_resetxlog.sgml @@ -22,6 +22,7 @@ PostgreSQL documentation pg_resetxlog + xid,xid oid @@ -79,9 +80,12 @@ PostgreSQL documentation The + + + A safe value for the oldest transaction ID for which the commit time can + be retrieved (first part of + + The WAL starting address (