From 9731567ce2a0e40d3daa740923bf55df40415a6d Mon Sep 17 00:00:00 2001 From: Peter Eisentraut Date: Tue, 18 Nov 2025 08:43:13 +0100 Subject: [PATCH 3/6] Remove most StaticAssertStmt() Similar to commit 75f49221c22, it's better to use StaticAssertDecl() instead of StaticAssertStmt() when possible. In fact, we can get rid of all uses of StaticAssertStmt(), which will eliminate the need to have two different ways to do the same thing, which has clearly been confusing. --- contrib/hstore/hstore_compat.c | 9 ++++----- src/backend/access/heap/vacuumlazy.c | 5 +++-- src/backend/access/table/tableam.c | 6 +++--- src/backend/access/transam/parallel.c | 7 ++++--- src/backend/backup/basebackup.c | 10 ++++++---- src/backend/storage/buffer/bufmgr.c | 4 ++-- src/backend/storage/file/fd.c | 17 ----------------- src/backend/storage/ipc/waiteventset.c | 3 ++- src/backend/storage/lmgr/deadlock.c | 12 +++++++----- src/backend/utils/adt/mac.c | 2 +- src/backend/utils/cache/inval.c | 2 +- src/backend/utils/mmgr/aset.c | 6 ++++-- src/include/storage/fd.h | 16 ++++++++++++++++ 13 files changed, 53 insertions(+), 46 deletions(-) diff --git a/contrib/hstore/hstore_compat.c b/contrib/hstore/hstore_compat.c index d75e9cb23f5..3a9f7f45cb7 100644 --- a/contrib/hstore/hstore_compat.c +++ b/contrib/hstore/hstore_compat.c @@ -94,7 +94,7 @@ * etc. are compatible. * * If the above statement isn't true on some bizarre platform, we're - * a bit hosed (see StaticAssertStmt in hstoreValidOldFormat). + * a bit hosed. */ typedef struct { @@ -105,6 +105,9 @@ typedef struct pos:31; } HOldEntry; +StaticAssertDecl(sizeof(HOldEntry) == 2 * sizeof(HEntry), + "old hstore format is not upward-compatible"); + static int hstoreValidNewFormat(HStore *hs); static int hstoreValidOldFormat(HStore *hs); @@ -179,10 +182,6 @@ hstoreValidOldFormat(HStore *hs) if (hs->size_ & HS_FLAG_NEWVERSION) return 0; - /* New format uses an HEntry for key and another for value */ - StaticAssertStmt(sizeof(HOldEntry) == 2 * sizeof(HEntry), - "old hstore format is not upward-compatible"); - if (count == 0) return 2; diff --git a/src/backend/access/heap/vacuumlazy.c b/src/backend/access/heap/vacuumlazy.c index deb9a3dc0d1..61976c9999b 100644 --- a/src/backend/access/heap/vacuumlazy.c +++ b/src/backend/access/heap/vacuumlazy.c @@ -3369,6 +3369,9 @@ lazy_truncate_heap(LVRelState *vacrel) static BlockNumber count_nondeletable_pages(LVRelState *vacrel, bool *lock_waiter_detected) { + StaticAssertDecl((PREFETCH_SIZE & (PREFETCH_SIZE - 1)) == 0, + "prefetch size must be power of 2"); + BlockNumber blkno; BlockNumber prefetchedUntil; instr_time starttime; @@ -3383,8 +3386,6 @@ count_nondeletable_pages(LVRelState *vacrel, bool *lock_waiter_detected) * in forward direction, so that OS-level readahead can kick in. */ blkno = vacrel->rel_pages; - StaticAssertStmt((PREFETCH_SIZE & (PREFETCH_SIZE - 1)) == 0, - "prefetch size must be power of 2"); prefetchedUntil = InvalidBlockNumber; while (blkno > vacrel->nonempty_pages) { diff --git a/src/backend/access/table/tableam.c b/src/backend/access/table/tableam.c index 5e41404937e..0c90feb0e97 100644 --- a/src/backend/access/table/tableam.c +++ b/src/backend/access/table/tableam.c @@ -423,14 +423,14 @@ table_block_parallelscan_startblock_init(Relation rel, ParallelBlockTableScanWorker pbscanwork, ParallelBlockTableScanDesc pbscan) { + StaticAssertDecl(MaxBlockNumber <= 0xFFFFFFFE, + "pg_nextpower2_32 may be too small for non-standard BlockNumber width"); + BlockNumber sync_startpage = InvalidBlockNumber; /* Reset the state we use for controlling allocation size. */ memset(pbscanwork, 0, sizeof(*pbscanwork)); - StaticAssertStmt(MaxBlockNumber <= 0xFFFFFFFE, - "pg_nextpower2_32 may be too small for non-standard BlockNumber width"); - /* * We determine the chunk size based on the size of the relation. First we * split the relation into PARALLEL_SEQSCAN_NCHUNKS chunks but we then diff --git a/src/backend/access/transam/parallel.c b/src/backend/access/transam/parallel.c index 94db1ec3012..73bd7ad61a0 100644 --- a/src/backend/access/transam/parallel.c +++ b/src/backend/access/transam/parallel.c @@ -266,6 +266,10 @@ InitializeParallelDSM(ParallelContext *pcxt) if (pcxt->nworkers > 0) { + StaticAssertDecl(BUFFERALIGN(PARALLEL_ERROR_QUEUE_SIZE) == + PARALLEL_ERROR_QUEUE_SIZE, + "parallel error queue size not buffer-aligned"); + /* Estimate space for various kinds of state sharing. */ library_len = EstimateLibraryStateSpace(); shm_toc_estimate_chunk(&pcxt->estimator, library_len); @@ -297,9 +301,6 @@ InitializeParallelDSM(ParallelContext *pcxt) shm_toc_estimate_keys(&pcxt->estimator, 12); /* Estimate space need for error queues. */ - StaticAssertStmt(BUFFERALIGN(PARALLEL_ERROR_QUEUE_SIZE) == - PARALLEL_ERROR_QUEUE_SIZE, - "parallel error queue size not buffer-aligned"); shm_toc_estimate_chunk(&pcxt->estimator, mul_size(PARALLEL_ERROR_QUEUE_SIZE, pcxt->nworkers)); diff --git a/src/backend/backup/basebackup.c b/src/backend/backup/basebackup.c index 2be4e069816..c608b1625ca 100644 --- a/src/backend/backup/basebackup.c +++ b/src/backend/backup/basebackup.c @@ -635,10 +635,12 @@ perform_base_backup(basebackup_options *opt, bbsink *sink, } /* Properly terminate the tar file. */ - StaticAssertStmt(2 * TAR_BLOCK_SIZE <= BLCKSZ, - "BLCKSZ too small for 2 tar blocks"); - memset(sink->bbs_buffer, 0, 2 * TAR_BLOCK_SIZE); - bbsink_archive_contents(sink, 2 * TAR_BLOCK_SIZE); + { + StaticAssertDecl(2 * TAR_BLOCK_SIZE <= BLCKSZ, + "BLCKSZ too small for 2 tar blocks"); + memset(sink->bbs_buffer, 0, 2 * TAR_BLOCK_SIZE); + bbsink_archive_contents(sink, 2 * TAR_BLOCK_SIZE); + } /* OK, that's the end of the archive. */ bbsink_end_archive(sink); diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 327ddb7adc8..09405304769 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -6961,9 +6961,9 @@ buffer_readv_encode_error(PgAioResult *result, error_count > 0 ? error_count : zeroed_count; uint8 first_off; - StaticAssertStmt(PG_IOV_MAX <= 1 << READV_COUNT_BITS, + StaticAssertDecl(PG_IOV_MAX <= 1 << READV_COUNT_BITS, "PG_IOV_MAX is bigger than reserved space for error data"); - StaticAssertStmt((1 + 1 + 3 * READV_COUNT_BITS) <= PGAIO_RESULT_ERROR_BITS, + StaticAssertDecl((1 + 1 + 3 * READV_COUNT_BITS) <= PGAIO_RESULT_ERROR_BITS, "PGAIO_RESULT_ERROR_BITS is insufficient for buffer_readv"); /* diff --git a/src/backend/storage/file/fd.c b/src/backend/storage/file/fd.c index e9eaaf9c829..59114632d49 100644 --- a/src/backend/storage/file/fd.c +++ b/src/backend/storage/file/fd.c @@ -1111,23 +1111,6 @@ BasicOpenFilePerm(const char *fileName, int fileFlags, mode_t fileMode) tryAgain: #ifdef PG_O_DIRECT_USE_F_NOCACHE - - /* - * The value we defined to stand in for O_DIRECT when simulating it with - * F_NOCACHE had better not collide with any of the standard flags. - */ - StaticAssertStmt((PG_O_DIRECT & - (O_APPEND | - O_CLOEXEC | - O_CREAT | - O_DSYNC | - O_EXCL | - O_RDWR | - O_RDONLY | - O_SYNC | - O_TRUNC | - O_WRONLY)) == 0, - "PG_O_DIRECT value collides with standard flag"); fd = open(fileName, fileFlags & ~PG_O_DIRECT, fileMode); #else fd = open(fileName, fileFlags, fileMode); diff --git a/src/backend/storage/ipc/waiteventset.c b/src/backend/storage/ipc/waiteventset.c index 465cee40ccc..ebdb22ac3c1 100644 --- a/src/backend/storage/ipc/waiteventset.c +++ b/src/backend/storage/ipc/waiteventset.c @@ -462,7 +462,6 @@ CreateWaitEventSet(ResourceOwner resowner, int nevents) * pending signals are serviced. */ set->handles[0] = pgwin32_signal_event; - StaticAssertStmt(WSA_INVALID_EVENT == NULL, ""); #endif return set; @@ -979,6 +978,8 @@ WaitEventAdjustKqueue(WaitEventSet *set, WaitEvent *event, int old_events) #endif #if defined(WAIT_USE_WIN32) +StaticAssertDecl(WSA_INVALID_EVENT == NULL, ""); + static void WaitEventAdjustWin32(WaitEventSet *set, WaitEvent *event) { diff --git a/src/backend/storage/lmgr/deadlock.c b/src/backend/storage/lmgr/deadlock.c index c4bfaaa67ac..c129d966f7b 100644 --- a/src/backend/storage/lmgr/deadlock.c +++ b/src/backend/storage/lmgr/deadlock.c @@ -192,11 +192,13 @@ InitDeadLockChecking(void) * last MaxBackends entries in possibleConstraints[] are reserved as * output workspace for FindLockCycle. */ - StaticAssertStmt(MAX_BACKENDS_BITS <= (32 - 3), - "MAX_BACKENDS_BITS too big for * 4"); - maxPossibleConstraints = MaxBackends * 4; - possibleConstraints = - (EDGE *) palloc(maxPossibleConstraints * sizeof(EDGE)); + { + StaticAssertDecl(MAX_BACKENDS_BITS <= (32 - 3), + "MAX_BACKENDS_BITS too big for * 4"); + maxPossibleConstraints = MaxBackends * 4; + possibleConstraints = + (EDGE *) palloc(maxPossibleConstraints * sizeof(EDGE)); + } MemoryContextSwitchTo(oldcxt); } diff --git a/src/backend/utils/adt/mac.c b/src/backend/utils/adt/mac.c index bb38ef2f5e4..55c3448e88c 100644 --- a/src/backend/utils/adt/mac.c +++ b/src/backend/utils/adt/mac.c @@ -485,7 +485,7 @@ macaddr_abbrev_convert(Datum original, SortSupport ssup) * There will be two bytes of zero padding on the end of the least * significant bits. */ - StaticAssertStmt(sizeof(res) >= sizeof(macaddr), + StaticAssertDecl(sizeof(res) >= sizeof(macaddr), "Datum is too small for macaddr"); memset(&res, 0, sizeof(res)); memcpy(&res, authoritative, sizeof(macaddr)); diff --git a/src/backend/utils/cache/inval.c b/src/backend/utils/cache/inval.c index 02505c88b8e..cd1686a4202 100644 --- a/src/backend/utils/cache/inval.c +++ b/src/backend/utils/cache/inval.c @@ -1753,7 +1753,7 @@ CacheInvalidateSmgr(RelFileLocatorBackend rlocator) SharedInvalidationMessage msg; /* verify optimization stated above stays valid */ - StaticAssertStmt(MAX_BACKENDS_BITS <= 23, + StaticAssertDecl(MAX_BACKENDS_BITS <= 23, "MAX_BACKENDS_BITS is too big for inval.c"); msg.sm.id = SHAREDINVALSMGR_ID; diff --git a/src/backend/utils/mmgr/aset.c b/src/backend/utils/mmgr/aset.c index bcd09c07533..d84edf716fe 100644 --- a/src/backend/utils/mmgr/aset.c +++ b/src/backend/utils/mmgr/aset.c @@ -503,8 +503,10 @@ AllocSetContextCreateInternal(MemoryContext parent, * * Also, allocChunkLimit must not exceed ALLOCSET_SEPARATE_THRESHOLD. */ - StaticAssertStmt(ALLOC_CHUNK_LIMIT == ALLOCSET_SEPARATE_THRESHOLD, - "ALLOC_CHUNK_LIMIT != ALLOCSET_SEPARATE_THRESHOLD"); + { + StaticAssertDecl(ALLOC_CHUNK_LIMIT == ALLOCSET_SEPARATE_THRESHOLD, + "ALLOC_CHUNK_LIMIT != ALLOCSET_SEPARATE_THRESHOLD"); + } /* * Determine the maximum size that a chunk can be before we allocate an diff --git a/src/include/storage/fd.h b/src/include/storage/fd.h index 3e821ce8fb7..d131fe21e2d 100644 --- a/src/include/storage/fd.h +++ b/src/include/storage/fd.h @@ -93,6 +93,22 @@ extern PGDLLIMPORT int max_safe_fds; #elif defined(F_NOCACHE) #define PG_O_DIRECT 0x80000000 #define PG_O_DIRECT_USE_F_NOCACHE +/* + * The value we defined to stand in for O_DIRECT when simulating it with + * F_NOCACHE had better not collide with any of the standard flags. + */ +StaticAssertDecl((PG_O_DIRECT & + (O_APPEND | + O_CLOEXEC | + O_CREAT | + O_DSYNC | + O_EXCL | + O_RDWR | + O_RDONLY | + O_SYNC | + O_TRUNC | + O_WRONLY)) == 0, + "PG_O_DIRECT value collides with standard flag"); #else #define PG_O_DIRECT 0 #endif -- 2.51.0