From f154f028d856bf5ad6ec419d7947865c172d14ae Mon Sep 17 00:00:00 2001 From: Thomas Munro Date: Wed, 8 Jan 2025 07:50:30 +1300 Subject: [PATCH] Restore smgrtruncate() prototype in back-branches. It's possible that external code is calling smgrtruncate(). Any external callers might like to consider the recent changes to RelationTruncate(), but commit 38c579b0 should not have changed the function prototype in the back-branches, per ABI stability policy. Restore smgrtruncate()'s traditional argument list in the back-branches, but make it a wrapper for a new function smgrtruncate2(). The three callers in core can use smgrtruncate2() directly. In master (18-to-be), smgrtruncate2() is effectively renamed to smgrtruncate(), so this wart is cleaned up. Reviewed-by: Michael Paquier Discussion: https://postgr.es/m/CA%2BhUKG%2BThae6x6%2BjmQiuALQBT2Ae1ChjMh1%3DkMvJ8y_SBJZrvA%40mail.gmail.com --- contrib/pg_visibility/pg_visibility.c | 2 +- src/backend/catalog/storage.c | 4 ++-- src/backend/storage/smgr/smgr.c | 24 ++++++++++++++++++++++-- src/include/storage/smgr.h | 4 +++- 4 files changed, 28 insertions(+), 6 deletions(-) diff --git a/contrib/pg_visibility/pg_visibility.c b/contrib/pg_visibility/pg_visibility.c index 13d9eb031dc..5e41e1a9329 100644 --- a/contrib/pg_visibility/pg_visibility.c +++ b/contrib/pg_visibility/pg_visibility.c @@ -429,7 +429,7 @@ pg_truncate_visibility_map(PG_FUNCTION_ARGS) } if (BlockNumberIsValid(block)) - smgrtruncate(RelationGetSmgr(rel), &fork, 1, &old_block, &block); + smgrtruncate2(RelationGetSmgr(rel), &fork, 1, &old_block, &block); END_CRIT_SECTION(); MyProc->delayChkpt = false; diff --git a/src/backend/catalog/storage.c b/src/backend/catalog/storage.c index 0ac91be63dc..f563f2a1690 100644 --- a/src/backend/catalog/storage.c +++ b/src/backend/catalog/storage.c @@ -409,7 +409,7 @@ RelationTruncate(Relation rel, BlockNumber nblocks) * longer exist after truncation is complete, and then truncate the * corresponding files on disk. */ - smgrtruncate(RelationGetSmgr(rel), forks, nforks, old_blocks, blocks); + smgrtruncate2(RelationGetSmgr(rel), forks, nforks, old_blocks, blocks); END_CRIT_SECTION(); @@ -1069,7 +1069,7 @@ smgr_redo(XLogReaderState *record) if (nforks > 0) { START_CRIT_SECTION(); - smgrtruncate(reln, forks, nforks, old_blocks, blocks); + smgrtruncate2(reln, forks, nforks, old_blocks, blocks); END_CRIT_SECTION(); } diff --git a/src/backend/storage/smgr/smgr.c b/src/backend/storage/smgr/smgr.c index 9e6500ff981..6676e0cae5c 100644 --- a/src/backend/storage/smgr/smgr.c +++ b/src/backend/storage/smgr/smgr.c @@ -586,6 +586,26 @@ smgrnblocks_cached(SMgrRelation reln, ForkNumber forknum) * smgrtruncate() -- Truncate the given forks of supplied relation to * each specified numbers of blocks * + * Backward-compatible version of smgrtruncate2() for the benefit of external + * callers. This version isn't used in PostgreSQL core code, and can't be + * used in a critical section. + */ +void +smgrtruncate(SMgrRelation reln, ForkNumber *forknum, int nforks, + BlockNumber *nblocks) +{ + BlockNumber old_nblocks[MAX_FORKNUM + 1]; + + for (int i = 0; i < nforks; ++i) + old_nblocks[i] = smgrnblocks(reln, forknum[i]); + + return smgrtruncate2(reln, forknum, nforks, old_nblocks, nblocks); +} + +/* + * smgrtruncate2() -- Truncate the given forks of supplied relation to + * each specified numbers of blocks + * * The truncation is done immediately, so this can't be rolled back. * * The caller must hold AccessExclusiveLock on the relation, to ensure that @@ -597,8 +617,8 @@ smgrnblocks_cached(SMgrRelation reln, ForkNumber forknum) * to this relation should be called in between. */ void -smgrtruncate(SMgrRelation reln, ForkNumber *forknum, int nforks, - BlockNumber *old_nblocks, BlockNumber *nblocks) +smgrtruncate2(SMgrRelation reln, ForkNumber *forknum, int nforks, + BlockNumber *old_nblocks, BlockNumber *nblocks) { int i; diff --git a/src/include/storage/smgr.h b/src/include/storage/smgr.h index c7dc19d5407..6d404057561 100644 --- a/src/include/storage/smgr.h +++ b/src/include/storage/smgr.h @@ -101,8 +101,10 @@ extern void smgrwriteback(SMgrRelation reln, ForkNumber forknum, extern BlockNumber smgrnblocks(SMgrRelation reln, ForkNumber forknum); extern BlockNumber smgrnblocks_cached(SMgrRelation reln, ForkNumber forknum); extern void smgrtruncate(SMgrRelation reln, ForkNumber *forknum, int nforks, - BlockNumber *old_nblocks, BlockNumber *nblocks); +extern void smgrtruncate2(SMgrRelation reln, ForkNumber *forknum, int nforks, + BlockNumber *old_nblocks, + BlockNumber *nblocks); extern void smgrimmedsync(SMgrRelation reln, ForkNumber forknum); extern void AtEOXact_SMgr(void); -- 2.39.5