postgres.git / summary / log / commit / refs

commit    344322da515e7eed56300f52f95ef320753c12d1
Author:   Fujii Masao <fujii@postgresql.org>
Date:     Wed Nov 18 04:58:30 2026 +0000

    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


src/test/modules/xid_wraparound/xid_wraparound.c | 52 ++++++++++++++++-------- 1 file changed, 36 insertions(+), 16 deletions(-) diff --git a/src/test/modules/xid_wraparound/xid_wraparound.c b/src/test/modules/xid_wraparound/xid_wraparound.c index dce81c0c6d6..660c1b16064 100644 --- a/src/test/modules/xid_wraparound/xid_wraparound.c +++ b/src/test/modules/xid_wraparound/xid_wraparound.c @@ -26,6 +26,8 @@ static FullTransactionId consume_xids_common(FullTransactionId untilxid, uint64 /* * Consume the specified number of XIDs. + * + * Returns the last XID assigned by this function. */ PG_FUNCTION_INFO_V1(consume_xids); Datum @@ -34,19 +36,21 @@ consume_xids(PG_FUNCTION_ARGS) int64 nxids = PG_GETARG_INT64(0); FullTransactionId lastxid; - if (nxids < 0) + if (nxids <= 0) elog(ERROR, "invalid nxids argument: %lld", (long long) nxids); - if (nxids == 0) - lastxid = ReadNextFullTransactionId(); - else - lastxid = consume_xids_common(InvalidFullTransactionId, (uint64) nxids); + lastxid = consume_xids_common(InvalidFullTransactionId, (uint64) nxids); PG_RETURN_FULLTRANSACTIONID(lastxid); } /* - * Consume XIDs, up to the given XID. + * Advance XID allocation so that the next XID is at least the target. + * This may consume XIDs beyond the target. + * + * Ensures that the current transaction has a top-level XID. + * Returns the last XID assigned by this call, or the next XID observed + * if no XID was assigned. */ PG_FUNCTION_INFO_V1(consume_xids_until); Datum @@ -64,6 +68,23 @@ consume_xids_until(PG_FUNCTION_ARGS) PG_RETURN_FULLTRANSACTIONID(lastxid); } +/* + * These constants copied from .c files, because they're private. + */ +#define COMMIT_TS_XACTS_PER_PAGE (BLCKSZ / 10) +#define SUBTRANS_XACTS_PER_PAGE (BLCKSZ / sizeof(TransactionId)) +#define CLOG_XACTS_PER_BYTE 4 +#define CLOG_XACTS_PER_PAGE (BLCKSZ * CLOG_XACTS_PER_BYTE) + +/* + * Take the shortcut in consume_xids_common() only if we have more than this + * many XIDs left to consume. XidSkip() never skips more than + * COMMIT_TS_XACTS_PER_PAGE XIDs, which is 3276 with the largest supported + * block size (32kB), so this is enough to make sure that the shortcut never + * consumes more XIDs than requested. + */ +#define XID_SHORTCUT_THRESHOLD 4000 + /* * Common functionality between the two public functions. */ @@ -89,7 +110,11 @@ consume_xids_common(FullTransactionId untilxid, uint64 nxids) * the cache overflows, but beyond that, we don't keep track of the * consumed XIDs. */ - (void) GetTopTransactionId(); + if (!FullTransactionIdIsValid(GetTopFullTransactionIdIfAny())) + { + lastxid = GetTopFullTransactionId(); + consumed++; + } for (;;) { @@ -115,7 +140,7 @@ consume_xids_common(FullTransactionId untilxid, uint64 nxids) * If we still have plenty of XIDs to consume, try to take a shortcut * and bump up the nextXid counter directly. */ - if (xids_left > 2000 && + if (xids_left > XID_SHORTCUT_THRESHOLD && consumed - last_reported_at < REPORT_INTERVAL && MyProc->subxidStatus.overflowed) { @@ -153,14 +178,6 @@ consume_xids_common(FullTransactionId untilxid, uint64 nxids) return lastxid; } -/* - * These constants copied from .c files, because they're private. - */ -#define COMMIT_TS_XACTS_PER_PAGE (BLCKSZ / 10) -#define SUBTRANS_XACTS_PER_PAGE (BLCKSZ / sizeof(TransactionId)) -#define CLOG_XACTS_PER_BYTE 4 -#define CLOG_XACTS_PER_PAGE (BLCKSZ * CLOG_XACTS_PER_BYTE) - /* * All the interesting action in GetNewTransactionId happens when we extend * the SLRUs, or at the uint32 wraparound. If the nextXid counter is not close @@ -193,6 +210,9 @@ XidSkip(FullTransactionId fullxid) return 0; distance = Min(distance, CLOG_XACTS_PER_PAGE - rem); + /* The shortcut threshold in consume_xids_common() relies on this */ + Assert(distance <= XID_SHORTCUT_THRESHOLD); + return distance; } [parent: 2bce773e2e84]