From 387fdc4aa0869815dd0e758b12de84ea157334d0 Mon Sep 17 00:00:00 2001 From: Peter Eisentraut Date: Wed, 16 Sep 2026 09:49:28 +0200 Subject: [PATCH v3 4/5] Add an assertion and comments for HEADER_SCRATCH_SIZE HEADER_SCRATCH_SIZE made a complicated calculation without any comments, and there was no check that the calculation was correct. Add some comments and assertion. Suggested-by: Heikki Linnakangas Discussion: https://www.postgresql.org/message-id/flat/94a128da-bf3e-46bd-9e2d-609573c484da%40eisentraut.org --- src/backend/access/transam/xloginsert.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/backend/access/transam/xloginsert.c b/src/backend/access/transam/xloginsert.c index 70cbe9d709b..73ea8117c47 100644 --- a/src/backend/access/transam/xloginsert.c +++ b/src/backend/access/transam/xloginsert.c @@ -108,6 +108,18 @@ static uint8 curinsert_flags = 0; #define SizeOfXlogOrigin (sizeof(ReplOriginId) + sizeof(char)) #define SizeOfXLogTransactionId (sizeof(TransactionId) + sizeof(char)) +/* + * Size of the workspace used to hold the record header while constructing a + * record. It is large enough for the header of any WAL record: the + * fixed-size XLogRecord, a block header for every possible block ID, the main + * data header, and one term for each of the "special" block IDs. + * + * Every "special" block ID needs its own term here. If you add one alongside + * XLR_BLOCK_ID_ORIGIN (SizeOfXlogOrigin) and XLR_BLOCK_ID_TOPLEVEL_XID + * (SizeOfXLogTransactionId), you must add a corresponding term below as well. + * + * XLogRecordAssemble() asserts that the assembled header actually fits. + */ #define HEADER_SCRATCH_SIZE \ (SizeOfXLogRecord + \ MaxSizeOfXLogRecordBlockHeader * (XLR_MAX_BLOCK_ID + 1) + \ @@ -965,6 +977,7 @@ XLogRecordAssemble(RmgrId rmid, uint8 info, rdt_datas_last->next = NULL; hdr_rdt.len = (scratch - hdr_scratch); + Assert(hdr_rdt.len <= HEADER_SCRATCH_SIZE); total_len += hdr_rdt.len; /* -- 2.55.0