aboutsummaryrefslogtreecommitdiff
path: root/src/backend/access/heap/visibilitymap.c
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2022-11-17 16:54:30 -0500
committerTom Lane <tgl@sss.pgh.pa.us>2022-11-17 16:54:30 -0500
commit9a299cf7c21f30ca5d045faaf725e8216d3cc5f5 (patch)
tree416f0c231fe4fbdf9046cda3aba4359b3e056cb5 /src/backend/access/heap/visibilitymap.c
parentd419d391fc215cd682953394bb8e2a48bd7deb7b (diff)
downloadpostgresql-9a299cf7c21f30ca5d045faaf725e8216d3cc5f5.tar.gz
postgresql-9a299cf7c21f30ca5d045faaf725e8216d3cc5f5.zip
Replace RelationOpenSmgr() with RelationGetSmgr().
This is a back-patch of the v15-era commit f10f0ae42 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 by f10f0ae42. Therefore, even though this is a bit invasive, it's time to back-patch. Some comfort can be taken in the fact that f10f0ae42 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
Diffstat (limited to 'src/backend/access/heap/visibilitymap.c')
-rw-r--r--src/backend/access/heap/visibilitymap.c54
1 files changed, 28 insertions, 26 deletions
diff --git a/src/backend/access/heap/visibilitymap.c b/src/backend/access/heap/visibilitymap.c
index 0a51678c40d..6a14039ea7b 100644
--- a/src/backend/access/heap/visibilitymap.c
+++ b/src/backend/access/heap/visibilitymap.c
@@ -455,13 +455,11 @@ visibilitymap_prepare_truncate(Relation rel, BlockNumber nheapblocks)
elog(DEBUG1, "vm_truncate %s %d", RelationGetRelationName(rel), nheapblocks);
#endif
- RelationOpenSmgr(rel);
-
/*
* If no visibility map has been created yet for this relation, there's
* nothing to truncate.
*/
- if (!smgrexists(rel->rd_smgr, VISIBILITYMAP_FORKNUM))
+ if (!smgrexists(RelationGetSmgr(rel), VISIBILITYMAP_FORKNUM))
return InvalidBlockNumber;
/*
@@ -528,7 +526,7 @@ visibilitymap_prepare_truncate(Relation rel, BlockNumber nheapblocks)
else
newnblocks = truncBlock;
- if (smgrnblocks(rel->rd_smgr, VISIBILITYMAP_FORKNUM) <= newnblocks)
+ if (smgrnblocks(RelationGetSmgr(rel), VISIBILITYMAP_FORKNUM) <= newnblocks)
{
/* nothing to do, the file was already smaller than requested size */
return InvalidBlockNumber;
@@ -547,31 +545,30 @@ static Buffer
vm_readbuf(Relation rel, BlockNumber blkno, bool extend)
{
Buffer buf;
+ SMgrRelation reln;
/*
- * We might not have opened the relation at the smgr level yet, or we
- * might have been forced to close it by a sinval message. The code below
- * won't necessarily notice relation extension immediately when extend =
- * false, so we rely on sinval messages to ensure that our ideas about the
- * size of the map aren't too far out of date.
+ * Caution: re-using this smgr pointer could fail if the relcache entry
+ * gets closed. It's safe as long as we only do smgr-level operations
+ * between here and the last use of the pointer.
*/
- RelationOpenSmgr(rel);
+ reln = RelationGetSmgr(rel);
/*
* If we haven't cached the size of the visibility map fork yet, check it
* first.
*/
- if (rel->rd_smgr->smgr_vm_nblocks == InvalidBlockNumber)
+ if (reln->smgr_vm_nblocks == InvalidBlockNumber)
{
- if (smgrexists(rel->rd_smgr, VISIBILITYMAP_FORKNUM))
- rel->rd_smgr->smgr_vm_nblocks = smgrnblocks(rel->rd_smgr,
- VISIBILITYMAP_FORKNUM);
+ if (smgrexists(reln, VISIBILITYMAP_FORKNUM))
+ reln->smgr_vm_nblocks = smgrnblocks(reln,
+ VISIBILITYMAP_FORKNUM);
else
- rel->rd_smgr->smgr_vm_nblocks = 0;
+ reln->smgr_vm_nblocks = 0;
}
/* Handle requests beyond EOF */
- if (blkno >= rel->rd_smgr->smgr_vm_nblocks)
+ if (blkno >= reln->smgr_vm_nblocks)
{
if (extend)
vm_extend(rel, blkno + 1);
@@ -619,6 +616,7 @@ vm_extend(Relation rel, BlockNumber vm_nblocks)
{
BlockNumber vm_nblocks_now;
PGAlignedBlock pg;
+ SMgrRelation reln;
PageInit((Page) pg.data, BLCKSZ, 0);
@@ -634,26 +632,30 @@ vm_extend(Relation rel, BlockNumber vm_nblocks)
*/
LockRelationForExtension(rel, ExclusiveLock);
- /* Might have to re-open if a cache flush happened */
- RelationOpenSmgr(rel);
+ /*
+ * Caution: re-using this smgr pointer could fail if the relcache entry
+ * gets closed. It's safe as long as we only do smgr-level operations
+ * between here and the last use of the pointer.
+ */
+ reln = RelationGetSmgr(rel);
/*
* Create the file first if it doesn't exist. If smgr_vm_nblocks is
* positive then it must exist, no need for an smgrexists call.
*/
- if ((rel->rd_smgr->smgr_vm_nblocks == 0 ||
- rel->rd_smgr->smgr_vm_nblocks == InvalidBlockNumber) &&
- !smgrexists(rel->rd_smgr, VISIBILITYMAP_FORKNUM))
- smgrcreate(rel->rd_smgr, VISIBILITYMAP_FORKNUM, false);
+ if ((reln->smgr_vm_nblocks == 0 ||
+ reln->smgr_vm_nblocks == InvalidBlockNumber) &&
+ !smgrexists(reln, VISIBILITYMAP_FORKNUM))
+ smgrcreate(reln, VISIBILITYMAP_FORKNUM, false);
- vm_nblocks_now = smgrnblocks(rel->rd_smgr, VISIBILITYMAP_FORKNUM);
+ vm_nblocks_now = smgrnblocks(reln, VISIBILITYMAP_FORKNUM);
/* Now extend the file */
while (vm_nblocks_now < vm_nblocks)
{
PageSetChecksumInplace((Page) pg.data, vm_nblocks_now);
- smgrextend(rel->rd_smgr, VISIBILITYMAP_FORKNUM, vm_nblocks_now,
+ smgrextend(reln, VISIBILITYMAP_FORKNUM, vm_nblocks_now,
pg.data, false);
vm_nblocks_now++;
}
@@ -665,10 +667,10 @@ vm_extend(Relation rel, BlockNumber vm_nblocks)
* to keep checking for creation or extension of the file, which happens
* infrequently.
*/
- CacheInvalidateSmgr(rel->rd_smgr->smgr_rnode);
+ CacheInvalidateSmgr(reln->smgr_rnode);
/* Update local cache with the up-to-date size */
- rel->rd_smgr->smgr_vm_nblocks = vm_nblocks_now;
+ reln->smgr_vm_nblocks = vm_nblocks_now;
UnlockRelationForExtension(rel, ExclusiveLock);
}