aboutsummaryrefslogtreecommitdiff
path: root/src/backend/storage/buffer/bufmgr.c
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2022-11-17 16:54:31 -0500
committerTom Lane <tgl@sss.pgh.pa.us>2022-11-17 16:54:31 -0500
commite21856fd652ad1d6bfcc63bb427d29622359f948 (patch)
tree6069fab2135ee54a9b97e249f1ad91c3ed02ee82 /src/backend/storage/buffer/bufmgr.c
parent36dd0074af9f2abfbd480549e25c38fdc2631f6d (diff)
downloadpostgresql-e21856fd652ad1d6bfcc63bb427d29622359f948.tar.gz
postgresql-e21856fd652ad1d6bfcc63bb427d29622359f948.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/storage/buffer/bufmgr.c')
-rw-r--r--src/backend/storage/buffer/bufmgr.c26
1 files changed, 7 insertions, 19 deletions
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 303f82aa233..e256c005a7a 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -533,9 +533,6 @@ PrefetchBuffer(Relation reln, ForkNumber forkNum, BlockNumber blockNum)
Assert(RelationIsValid(reln));
Assert(BlockNumberIsValid(blockNum));
- /* Open it at the smgr level if not already done */
- RelationOpenSmgr(reln);
-
if (RelationUsesLocalBuffers(reln))
{
/* see comments in ReadBufferExtended */
@@ -545,7 +542,7 @@ PrefetchBuffer(Relation reln, ForkNumber forkNum, BlockNumber blockNum)
errmsg("cannot access temporary tables of other sessions")));
/* pass it off to localbuf.c */
- LocalPrefetchBuffer(reln->rd_smgr, forkNum, blockNum);
+ LocalPrefetchBuffer(RelationGetSmgr(reln), forkNum, blockNum);
}
else
{
@@ -555,7 +552,7 @@ PrefetchBuffer(Relation reln, ForkNumber forkNum, BlockNumber blockNum)
int buf_id;
/* create a tag so we can lookup the buffer */
- INIT_BUFFERTAG(newTag, reln->rd_smgr->smgr_rnode.node,
+ INIT_BUFFERTAG(newTag, RelationGetSmgr(reln)->smgr_rnode.node,
forkNum, blockNum);
/* determine its hash code and partition lock ID */
@@ -569,7 +566,7 @@ PrefetchBuffer(Relation reln, ForkNumber forkNum, BlockNumber blockNum)
/* If not in buffers, initiate prefetch */
if (buf_id < 0)
- smgrprefetch(reln->rd_smgr, forkNum, blockNum);
+ smgrprefetch(RelationGetSmgr(reln), forkNum, blockNum);
/*
* If the block *is* in buffers, we do nothing. This is not really
@@ -645,9 +642,6 @@ ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum,
bool hit;
Buffer buf;
- /* Open it at the smgr level if not already done */
- RelationOpenSmgr(reln);
-
/*
* Reject attempts to read non-local temporary relations; we would be
* likely to get wrong data since we have no visibility into the owning
@@ -663,7 +657,7 @@ ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum,
* miss.
*/
pgstat_count_buffer_read(reln);
- buf = ReadBuffer_common(reln->rd_smgr, reln->rd_rel->relpersistence,
+ buf = ReadBuffer_common(RelationGetSmgr(reln), reln->rd_rel->relpersistence,
forkNum, blockNum, mode, strategy, &hit);
if (hit)
pgstat_count_buffer_hit(reln);
@@ -2814,10 +2808,7 @@ RelationGetNumberOfBlocksInFork(Relation relation, ForkNumber forkNum)
case RELKIND_SEQUENCE:
case RELKIND_INDEX:
case RELKIND_PARTITIONED_INDEX:
- /* Open it at the smgr level if not already done */
- RelationOpenSmgr(relation);
-
- return smgrnblocks(relation->rd_smgr, forkNum);
+ return smgrnblocks(RelationGetSmgr(relation), forkNum);
case RELKIND_RELATION:
case RELKIND_TOASTVALUE:
@@ -3204,9 +3195,6 @@ FlushRelationBuffers(Relation rel)
int i;
BufferDesc *bufHdr;
- /* Open rel at the smgr level if not already done */
- RelationOpenSmgr(rel);
-
if (RelationUsesLocalBuffers(rel))
{
for (i = 0; i < NLocBuffer; i++)
@@ -3231,7 +3219,7 @@ FlushRelationBuffers(Relation rel)
PageSetChecksumInplace(localpage, bufHdr->tag.blockNum);
- smgrwrite(rel->rd_smgr,
+ smgrwrite(RelationGetSmgr(rel),
bufHdr->tag.forkNum,
bufHdr->tag.blockNum,
localpage,
@@ -3272,7 +3260,7 @@ FlushRelationBuffers(Relation rel)
{
PinBuffer_Locked(bufHdr);
LWLockAcquire(BufferDescriptorGetContentLock(bufHdr), LW_SHARED);
- FlushBuffer(bufHdr, rel->rd_smgr);
+ FlushBuffer(bufHdr, RelationGetSmgr(rel));
LWLockRelease(BufferDescriptorGetContentLock(bufHdr));
UnpinBuffer(bufHdr, true);
}