mirror of
https://github.com/postgres/postgres.git
synced 2025-07-27 12:41:57 +03:00
Replace RelationOpenSmgr() with RelationGetSmgr().
This is a back-patch of the v15-era commitf10f0ae42
into older supported branches. The idea is to design out bugs in which an ill-timed relcache flush clears rel->rd_smgr partway through some code sequence that wasn't expecting that. We had another report today of a corner case that reliably crashes v14 under debug_discard_caches (nee CLOBBER_CACHE_ALWAYS), and therefore would crash once in a blue moon in the field. We're unlikely to get rid of all such code paths unless we adopt the more rigorous coding rules instituted byf10f0ae42
. Therefore, even though this is a bit invasive, it's time to back-patch. Some comfort can be taken in the fact thatf10f0ae42
has been in v15 for 16 months without problems. I left the RelationOpenSmgr macro present in the back branches, even though no core code should use it anymore, in order to not break third-party extensions in minor releases. Such extensions might opt to start using RelationGetSmgr instead, to reduce their code differential between v15 and earlier branches. This carries a hazard of failing to compile against headers from existing minor releases. However, once compiled the extension should work fine even with such releases, because RelationGetSmgr is a "static inline" function so it creates no link-time dependency. So depending on distribution practices, that might be an OK tradeoff. Per report from Spyridon Dimitrios Agathos. Original patch by Amul Sul. Discussion: https://postgr.es/m/CAFM5RaqdgyusQvmWkyPYaWMwoK5gigdtW-7HcgHgOeAw7mqJ_Q@mail.gmail.com Discussion: https://postgr.es/m/CANiYTQsU7yMFpQYnv=BrcRVqK_3U3mtAzAsJCaqtzsDHfsUbdQ@mail.gmail.com
This commit is contained in:
@ -301,8 +301,7 @@ bt_index_check_internal(Oid indrelid, bool parentcheck, bool heapallindexed,
|
||||
{
|
||||
bool heapkeyspace;
|
||||
|
||||
RelationOpenSmgr(indrel);
|
||||
if (!smgrexists(indrel->rd_smgr, MAIN_FORKNUM))
|
||||
if (!smgrexists(RelationGetSmgr(indrel), MAIN_FORKNUM))
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_INDEX_CORRUPTED),
|
||||
errmsg("index \"%s\" lacks a main relation fork",
|
||||
|
@ -179,9 +179,9 @@ blbuildempty(Relation index)
|
||||
* this even when wal_level=minimal.
|
||||
*/
|
||||
PageSetChecksumInplace(metapage, BLOOM_METAPAGE_BLKNO);
|
||||
smgrwrite(index->rd_smgr, INIT_FORKNUM, BLOOM_METAPAGE_BLKNO,
|
||||
smgrwrite(RelationGetSmgr(index), INIT_FORKNUM, BLOOM_METAPAGE_BLKNO,
|
||||
(char *) metapage, true);
|
||||
log_newpage(&index->rd_smgr->smgr_rnode.node, INIT_FORKNUM,
|
||||
log_newpage(&(RelationGetSmgr(index))->smgr_rnode.node, INIT_FORKNUM,
|
||||
BLOOM_METAPAGE_BLKNO, metapage, true);
|
||||
|
||||
/*
|
||||
@ -189,7 +189,7 @@ blbuildempty(Relation index)
|
||||
* write did not go through shared_buffers and therefore a concurrent
|
||||
* checkpoint may have moved the redo pointer past our xlog record.
|
||||
*/
|
||||
smgrimmedsync(index->rd_smgr, INIT_FORKNUM);
|
||||
smgrimmedsync(RelationGetSmgr(index), INIT_FORKNUM);
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -518,15 +518,13 @@ autoprewarm_database_main(Datum main_arg)
|
||||
old_blk->filenode != blk->filenode ||
|
||||
old_blk->forknum != blk->forknum)
|
||||
{
|
||||
RelationOpenSmgr(rel);
|
||||
|
||||
/*
|
||||
* smgrexists is not safe for illegal forknum, hence check whether
|
||||
* the passed forknum is valid before using it in smgrexists.
|
||||
*/
|
||||
if (blk->forknum > InvalidForkNumber &&
|
||||
blk->forknum <= MAX_FORKNUM &&
|
||||
smgrexists(rel->rd_smgr, blk->forknum))
|
||||
smgrexists(RelationGetSmgr(rel), blk->forknum))
|
||||
nblocks = RelationGetNumberOfBlocksInFork(rel, blk->forknum);
|
||||
else
|
||||
nblocks = 0;
|
||||
|
@ -109,8 +109,7 @@ pg_prewarm(PG_FUNCTION_ARGS)
|
||||
aclcheck_error(aclresult, get_relkind_objtype(rel->rd_rel->relkind), get_rel_name(relOid));
|
||||
|
||||
/* Check that the fork exists. */
|
||||
RelationOpenSmgr(rel);
|
||||
if (!smgrexists(rel->rd_smgr, forkNumber))
|
||||
if (!smgrexists(RelationGetSmgr(rel), forkNumber))
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
||||
errmsg("fork \"%s\" does not exist for this relation",
|
||||
@ -178,7 +177,7 @@ pg_prewarm(PG_FUNCTION_ARGS)
|
||||
for (block = first_block; block <= last_block; ++block)
|
||||
{
|
||||
CHECK_FOR_INTERRUPTS();
|
||||
smgrread(rel->rd_smgr, forkNumber, block, blockbuffer.data);
|
||||
smgrread(RelationGetSmgr(rel), forkNumber, block, blockbuffer.data);
|
||||
++blocks_done;
|
||||
}
|
||||
}
|
||||
|
@ -389,8 +389,8 @@ pg_truncate_visibility_map(PG_FUNCTION_ARGS)
|
||||
/* Only some relkinds have a visibility map */
|
||||
check_relation_relkind(rel);
|
||||
|
||||
RelationOpenSmgr(rel);
|
||||
rel->rd_smgr->smgr_vm_nblocks = InvalidBlockNumber;
|
||||
/* Forcibly reset cached file size */
|
||||
RelationGetSmgr(rel)->smgr_vm_nblocks = InvalidBlockNumber;
|
||||
|
||||
visibilitymap_truncate(rel, 0);
|
||||
|
||||
|
Reference in New Issue
Block a user