diff options
author | Michael Paquier <michael@paquier.xyz> | 2020-07-12 20:47:15 +0900 |
---|---|---|
committer | Michael Paquier <michael@paquier.xyz> | 2020-07-12 20:47:15 +0900 |
commit | b1e48bbe64a411666bb1928b9741e112e267836d (patch) | |
tree | 6c3528afdd848d9614b8f4b64fd9a9a1921c9c1f /src/backend/access/transam/commit_ts.c | |
parent | cd22d3cdb9bd9963c694c01a8c0232bbae3ddcfb (diff) | |
download | postgresql-b1e48bbe64a411666bb1928b9741e112e267836d.tar.gz postgresql-b1e48bbe64a411666bb1928b9741e112e267836d.zip |
Include replication origins in SQL functions for commit timestamp
This includes two changes:
- Addition of a new function pg_xact_commit_timestamp_origin() able, for
a given transaction ID, to return the commit timestamp and replication
origin of this transaction. An equivalent function existed in
pglogical.
- Addition of the replication origin to pg_last_committed_xact().
The commit timestamp manager includes already APIs able to return the
replication origin of a transaction on top of its commit timestamp, but
the code paths for replication origins were never stressed as those
functions have never looked for a replication origin, and the SQL
functions available have never included this information since their
introduction in 73c986a.
While on it, refactor a test of modules/commit_ts/ to use tstzrange() to
check that a transaction timestamp is within the wanted range, making
the test a bit easier to read.
Bump catalog version.
Author: Movead Li
Reviewed-by: Madan Kumar, Michael Paquier
Discussion: https://postgr.es/m/2020051116430836450630@highgo.ca
Diffstat (limited to 'src/backend/access/transam/commit_ts.c')
-rw-r--r-- | src/backend/access/transam/commit_ts.c | 71 |
1 files changed, 66 insertions, 5 deletions
diff --git a/src/backend/access/transam/commit_ts.c b/src/backend/access/transam/commit_ts.c index 182e5391f7b..903280ae92d 100644 --- a/src/backend/access/transam/commit_ts.c +++ b/src/backend/access/transam/commit_ts.c @@ -361,7 +361,7 @@ TransactionIdGetCommitTsData(TransactionId xid, TimestampTz *ts, * is concerned, anyway; it's up to the caller to ensure the value is useful * for its purposes.) * - * ts and extra are filled with the corresponding data; they can be passed + * ts and nodeid are filled with the corresponding data; they can be passed * as NULL if not wanted. */ TransactionId @@ -417,28 +417,38 @@ pg_xact_commit_timestamp(PG_FUNCTION_ARGS) } +/* + * pg_last_committed_xact + * + * SQL-callable wrapper to obtain some information about the latest + * committed transaction: transaction ID, timestamp and replication + * origin. + */ Datum pg_last_committed_xact(PG_FUNCTION_ARGS) { TransactionId xid; + RepOriginId nodeid; TimestampTz ts; - Datum values[2]; - bool nulls[2]; + Datum values[3]; + bool nulls[3]; TupleDesc tupdesc; HeapTuple htup; /* and construct a tuple with our data */ - xid = GetLatestCommitTsData(&ts, NULL); + xid = GetLatestCommitTsData(&ts, &nodeid); /* * Construct a tuple descriptor for the result row. This must match this * function's pg_proc entry! */ - tupdesc = CreateTemplateTupleDesc(2); + tupdesc = CreateTemplateTupleDesc(3); TupleDescInitEntry(tupdesc, (AttrNumber) 1, "xid", XIDOID, -1, 0); TupleDescInitEntry(tupdesc, (AttrNumber) 2, "timestamp", TIMESTAMPTZOID, -1, 0); + TupleDescInitEntry(tupdesc, (AttrNumber) 3, "roident", + OIDOID, -1, 0); tupdesc = BlessTupleDesc(tupdesc); if (!TransactionIdIsNormal(xid)) @@ -452,6 +462,9 @@ pg_last_committed_xact(PG_FUNCTION_ARGS) values[1] = TimestampTzGetDatum(ts); nulls[1] = false; + + values[2] = ObjectIdGetDatum((Oid) nodeid); + nulls[2] = false; } htup = heap_form_tuple(tupdesc, values, nulls); @@ -459,6 +472,54 @@ pg_last_committed_xact(PG_FUNCTION_ARGS) PG_RETURN_DATUM(HeapTupleGetDatum(htup)); } +/* + * pg_xact_commit_timestamp_origin + * + * SQL-callable wrapper to obtain commit timestamp and replication origin + * of a given transaction. + */ +Datum +pg_xact_commit_timestamp_origin(PG_FUNCTION_ARGS) +{ + TransactionId xid = PG_GETARG_UINT32(0); + RepOriginId nodeid; + TimestampTz ts; + Datum values[2]; + bool nulls[2]; + TupleDesc tupdesc; + HeapTuple htup; + bool found; + + found = TransactionIdGetCommitTsData(xid, &ts, &nodeid); + + /* + * Construct a tuple descriptor for the result row. This must match this + * function's pg_proc entry! + */ + tupdesc = CreateTemplateTupleDesc(2); + TupleDescInitEntry(tupdesc, (AttrNumber) 1, "timestamp", + TIMESTAMPTZOID, -1, 0); + TupleDescInitEntry(tupdesc, (AttrNumber) 2, "roident", + OIDOID, -1, 0); + tupdesc = BlessTupleDesc(tupdesc); + + if (!found) + { + memset(nulls, true, sizeof(nulls)); + } + else + { + values[0] = TimestampTzGetDatum(ts); + nulls[0] = false; + + values[1] = ObjectIdGetDatum((Oid) nodeid); + nulls[1] = false; + } + + htup = heap_form_tuple(tupdesc, values, nulls); + + PG_RETURN_DATUM(HeapTupleGetDatum(htup)); +} /* * Number of shared CommitTS buffers. |