Skip to content

Commit 1b4abc2

Browse files
committed
xid_wraparound: Fix XID consumption accounting
When the current transaction had not yet been assigned a top-level XID, consume_xids(n) consumed n + 1 XIDs because the initial top-level XID allocation was not included in the count. Include that allocation so that the function consumes exactly n XIDs. In particular, consume_xids(1) now consumes and returns only the top-level XID in this case, rather than consuming two XIDs and returning the second. consume_xids(0) now raises an error instead of returning the next unallocated XID. With 32kB blocks, bulk XID consumption could also consume more XIDs than requested or return an outdated XID. Restrict this optimization so that consume_xids(n) always consumes exactly n XIDs and returns the last XID it assigned. Backpatch to 17, where the xid_wraparound test module was introduced. Author: Yushi Ogiwara <btogiwarayuushi@oss.nttdata.com> Author: João Rito <joaovictor.rito@gmail.com> Reviewed-by: Masahiko Sawada <sawada.mshk@gmail.com> Reviewed-by: Fujii Masao <masao.fujii@gmail.com> Discussion: https://postgr.es/m/2a62f138c9dcb6fcf5889b076d123aa2@oss.nttdata.com Backpatch-through: 17
1 parent 83cefb7 commit 1b4abc2

1 file changed

Lines changed: 36 additions & 16 deletions

File tree

‎src/test/modules/xid_wraparound/xid_wraparound.c‎

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ static FullTransactionId consume_xids_common(FullTransactionId untilxid, uint64
2626

2727
/*
2828
* Consume the specified number of XIDs.
29+
*
30+
* Returns the last XID assigned by this function.
2931
*/
3032
PG_FUNCTION_INFO_V1(consume_xids);
3133
Datum
@@ -34,19 +36,21 @@ consume_xids(PG_FUNCTION_ARGS)
3436
int64 nxids = PG_GETARG_INT64(0);
3537
FullTransactionId lastxid;
3638

37-
if (nxids < 0)
39+
if (nxids <= 0)
3840
elog(ERROR, "invalid nxids argument: %" PRId64, nxids);
3941

40-
if (nxids == 0)
41-
lastxid = ReadNextFullTransactionId();
42-
else
43-
lastxid = consume_xids_common(InvalidFullTransactionId, (uint64) nxids);
42+
lastxid = consume_xids_common(InvalidFullTransactionId, (uint64) nxids);
4443

4544
PG_RETURN_FULLTRANSACTIONID(lastxid);
4645
}
4746

4847
/*
49-
* Consume XIDs, up to the given XID.
48+
* Advance XID allocation so that the next XID is at least the target.
49+
* This may consume XIDs beyond the target.
50+
*
51+
* Ensures that the current transaction has a top-level XID.
52+
* Returns the last XID assigned by this call, or the next XID observed
53+
* if no XID was assigned.
5054
*/
5155
PG_FUNCTION_INFO_V1(consume_xids_until);
5256
Datum
@@ -64,6 +68,23 @@ consume_xids_until(PG_FUNCTION_ARGS)
6468
PG_RETURN_FULLTRANSACTIONID(lastxid);
6569
}
6670

71+
/*
72+
* These constants copied from .c files, because they're private.
73+
*/
74+
#define COMMIT_TS_XACTS_PER_PAGE (BLCKSZ / 10)
75+
#define SUBTRANS_XACTS_PER_PAGE (BLCKSZ / sizeof(TransactionId))
76+
#define CLOG_XACTS_PER_BYTE 4
77+
#define CLOG_XACTS_PER_PAGE (BLCKSZ * CLOG_XACTS_PER_BYTE)
78+
79+
/*
80+
* Take the shortcut in consume_xids_common() only if we have more than this
81+
* many XIDs left to consume. XidSkip() never skips more than
82+
* COMMIT_TS_XACTS_PER_PAGE XIDs, which is 3276 with the largest supported
83+
* block size (32kB), so this is enough to make sure that the shortcut never
84+
* consumes more XIDs than requested.
85+
*/
86+
#define XID_SHORTCUT_THRESHOLD 4000
87+
6788
/*
6889
* Common functionality between the two public functions.
6990
*/
@@ -89,7 +110,11 @@ consume_xids_common(FullTransactionId untilxid, uint64 nxids)
89110
* the cache overflows, but beyond that, we don't keep track of the
90111
* consumed XIDs.
91112
*/
92-
(void) GetTopTransactionId();
113+
if (!FullTransactionIdIsValid(GetTopFullTransactionIdIfAny()))
114+
{
115+
lastxid = GetTopFullTransactionId();
116+
consumed++;
117+
}
93118

94119
for (;;)
95120
{
@@ -115,7 +140,7 @@ consume_xids_common(FullTransactionId untilxid, uint64 nxids)
115140
* If we still have plenty of XIDs to consume, try to take a shortcut
116141
* and bump up the nextXid counter directly.
117142
*/
118-
if (xids_left > 2000 &&
143+
if (xids_left > XID_SHORTCUT_THRESHOLD &&
119144
consumed - last_reported_at < REPORT_INTERVAL &&
120145
MyProc->subxidStatus.overflowed)
121146
{
@@ -153,14 +178,6 @@ consume_xids_common(FullTransactionId untilxid, uint64 nxids)
153178
return lastxid;
154179
}
155180

156-
/*
157-
* These constants copied from .c files, because they're private.
158-
*/
159-
#define COMMIT_TS_XACTS_PER_PAGE (BLCKSZ / 10)
160-
#define SUBTRANS_XACTS_PER_PAGE (BLCKSZ / sizeof(TransactionId))
161-
#define CLOG_XACTS_PER_BYTE 4
162-
#define CLOG_XACTS_PER_PAGE (BLCKSZ * CLOG_XACTS_PER_BYTE)
163-
164181
/*
165182
* All the interesting action in GetNewTransactionId happens when we extend
166183
* the SLRUs, or at the uint32 wraparound. If the nextXid counter is not close
@@ -193,6 +210,9 @@ XidSkip(FullTransactionId fullxid)
193210
return 0;
194211
distance = Min(distance, CLOG_XACTS_PER_PAGE - rem);
195212

213+
/* The shortcut threshold in consume_xids_common() relies on this */
214+
Assert(distance <= XID_SHORTCUT_THRESHOLD);
215+
196216
return distance;
197217
}
198218

0 commit comments

Comments
 (0)