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 @@ -387,6 +387,28 @@ GetCurrentTransactionIdIfAny(void)
387
387
}
388
388
389
389
390
+ /*
391
+ * GetStableLatestTransactionIdIfAny
392
+ *
393
+ * Get the latest XID once and then return same value for rest of transaction.
394
+ * Acts as a useful reference point for maintenance tasks.
395
+ */
396
+ TransactionId
397
+ GetStableLatestTransactionId (void )
398
+ {
399
+ static LocalTransactionId lxid = InvalidLocalTransactionId ;
400
+ static TransactionId stablexid = InvalidTransactionId ;
401
+
402
+ if (lxid != MyProc -> lxid ||
403
+ !TransactionIdIsValid (stablexid ))
404
+ {
405
+ lxid = MyProc -> lxid ;
406
+ stablexid = ReadNewTransactionId ();
407
+ }
408
+
409
+ return stablexid ;
410
+ }
411
+
390
412
/*
391
413
* AssignTransactionId
392
414
*
Original file line number Diff line number Diff line change 19
19
#include "access/transam.h"
20
20
#include "access/xact.h"
21
21
#include "libpq/pqformat.h"
22
+ #include "storage/proc.h"
22
23
#include "utils/builtins.h"
23
24
24
25
#define PG_GETARG_TRANSACTIONID (n ) DatumGetTransactionId(PG_GETARG_DATUM(n))
@@ -87,16 +88,13 @@ xideq(PG_FUNCTION_ARGS)
87
88
}
88
89
89
90
/*
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 )
91
92
*/
92
93
Datum
93
94
xid_age (PG_FUNCTION_ARGS )
94
95
{
95
96
TransactionId xid = PG_GETARG_TRANSACTIONID (0 );
96
- TransactionId now = GetTopTransactionIdIfAny ();
97
-
98
- if (!TransactionIdIsValid (now ))
99
- now = ReadNewTransactionId ();
97
+ TransactionId now = GetStableLatestTransactionId ();
100
98
101
99
/* Permanent XIDs are always infinitely old */
102
100
if (!TransactionIdIsNormal (xid ))
Original file line number Diff line number Diff line change @@ -177,6 +177,7 @@ extern TransactionId GetTopTransactionId(void);
177
177
extern TransactionId GetTopTransactionIdIfAny (void );
178
178
extern TransactionId GetCurrentTransactionId (void );
179
179
extern TransactionId GetCurrentTransactionIdIfAny (void );
180
+ extern TransactionId GetStableLatestTransactionId (void );
180
181
extern SubTransactionId GetCurrentSubTransactionId (void );
181
182
extern CommandId GetCurrentCommandId (bool used );
182
183
extern TimestampTz GetCurrentTransactionStartTimestamp (void );
You can’t perform that action at this time.
0 commit comments