File tree Expand file tree Collapse file tree 3 files changed +26
-5
lines changed Expand file tree Collapse file tree 3 files changed +26
-5
lines changed Original file line number Diff line number Diff line change @@ -391,6 +391,28 @@ GetCurrentTransactionIdIfAny(void)
391391}
392392
393393
394+ /*
395+ * GetStableLatestTransactionIdIfAny
396+ *
397+ * Get the latest XID once and then return same value for rest of transaction.
398+ * Acts as a useful reference point for maintenance tasks.
399+ */
400+ TransactionId
401+ GetStableLatestTransactionId (void )
402+ {
403+ static LocalTransactionId lxid = InvalidLocalTransactionId ;
404+ static TransactionId stablexid = InvalidTransactionId ;
405+
406+ if (lxid != MyProc -> lxid ||
407+ !TransactionIdIsValid (stablexid ))
408+ {
409+ lxid = MyProc -> lxid ;
410+ stablexid = ReadNewTransactionId ();
411+ }
412+
413+ return stablexid ;
414+ }
415+
394416/*
395417 * AssignTransactionId
396418 *
Original file line number Diff line number Diff line change 1919#include "access/transam.h"
2020#include "access/xact.h"
2121#include "libpq/pqformat.h"
22+ #include "storage/proc.h"
2223#include "utils/builtins.h"
2324
2425#define PG_GETARG_TRANSACTIONID (n ) DatumGetTransactionId(PG_GETARG_DATUM(n))
@@ -87,16 +88,13 @@ xideq(PG_FUNCTION_ARGS)
8788}
8889
8990/*
90- * xid_age - compute age of an XID (relative to current xact )
91+ * xid_age - compute age of an XID (relative to latest stable xid )
9192 */
9293Datum
9394xid_age (PG_FUNCTION_ARGS )
9495{
9596 TransactionId xid = PG_GETARG_TRANSACTIONID (0 );
96- TransactionId now = GetTopTransactionIdIfAny ();
97-
98- if (!TransactionIdIsValid (now ))
99- now = ReadNewTransactionId ();
97+ TransactionId now = GetStableLatestTransactionId ();
10098
10199 /* Permanent XIDs are always infinitely old */
102100 if (!TransactionIdIsNormal (xid ))
Original file line number Diff line number Diff line change @@ -209,6 +209,7 @@ extern TransactionId GetTopTransactionId(void);
209209extern TransactionId GetTopTransactionIdIfAny (void );
210210extern TransactionId GetCurrentTransactionId (void );
211211extern TransactionId GetCurrentTransactionIdIfAny (void );
212+ extern TransactionId GetStableLatestTransactionId (void );
212213extern SubTransactionId GetCurrentSubTransactionId (void );
213214extern CommandId GetCurrentCommandId (bool used );
214215extern TimestampTz GetCurrentTransactionStartTimestamp (void );
You can’t perform that action at this time.
0 commit comments