summaryrefslogtreecommitdiff
path: root/src/backend
diff options
context:
space:
mode:
authorSimon Riggs <simon@2ndQuadrant.com>2012-05-11 14:38:53 +0100
committerSimon Riggs <simon@2ndQuadrant.com>2012-05-11 14:38:53 +0100
commit67ff11b42b2811c18fc9dfa54ded02303a082f7c (patch)
treef76cc0d84cf7dded49bdfc60cf396f5da7bec180 /src/backend
parentb149d1f90e7d42f719babc0c26addaeffa18df8c (diff)
Ensure age() returns a stable value rather than the latest value
Diffstat (limited to 'src/backend')
-rw-r--r--src/backend/access/transam/xact.c22
-rw-r--r--src/backend/utils/adt/xid.c8
2 files changed, 25 insertions, 5 deletions
diff --git a/src/backend/access/transam/xact.c b/src/backend/access/transam/xact.c
index 750725a43af..df790c0693e 100644
--- a/src/backend/access/transam/xact.c
+++ b/src/backend/access/transam/xact.c
@@ -388,6 +388,28 @@ GetCurrentTransactionIdIfAny(void)
/*
+ * GetStableLatestTransactionIdIfAny
+ *
+ * Get the latest XID once and then return same value for rest of transaction.
+ * Acts as a useful reference point for maintenance tasks.
+ */
+TransactionId
+GetStableLatestTransactionId(void)
+{
+ static LocalTransactionId lxid = InvalidLocalTransactionId;
+ static TransactionId stablexid = InvalidTransactionId;
+
+ if (lxid != MyProc->lxid ||
+ !TransactionIdIsValid(stablexid))
+ {
+ lxid = MyProc->lxid;
+ stablexid = ReadNewTransactionId();
+ }
+
+ return stablexid;
+}
+
+/*
* AssignTransactionId
*
* Assigns a new permanent XID to the given TransactionState.
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 7db6bb1a660..a9046cc964c 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/transam.h"
#include "access/xact.h"
#include "libpq/pqformat.h"
+#include "storage/proc.h"
#include "utils/builtins.h"
#define PG_GETARG_TRANSACTIONID(n) DatumGetTransactionId(PG_GETARG_DATUM(n))
@@ -87,16 +88,13 @@ xideq(PG_FUNCTION_ARGS)
}
/*
- * xid_age - compute age of an XID (relative to current xact)
+ * xid_age - compute age of an XID (relative to latest stable xid)
*/
Datum
xid_age(PG_FUNCTION_ARGS)
{
TransactionId xid = PG_GETARG_TRANSACTIONID(0);
- TransactionId now = GetTopTransactionIdIfAny();
-
- if (!TransactionIdIsValid(now))
- now = ReadNewTransactionId();
+ TransactionId now = GetStableLatestTransactionId();
/* Permanent XIDs are always infinitely old */
if (!TransactionIdIsNormal(xid))