mirror of
https://github.com/postgres/postgres.git
synced 2025-08-25 20:23:07 +03:00
Replace RelationOpenSmgr() with RelationGetSmgr().
The idea behind this patch is to design out bugs like the one fixed
by commit 9d523119f
. Previously, once one did RelationOpenSmgr(rel),
it was considered okay to access rel->rd_smgr directly for some
not-very-clear interval. But since that pointer will be cleared by
relcache flushes, we had bugs arising from overreliance on a previous
RelationOpenSmgr call still being effective.
Now, very little code except that in rel.h and relcache.c should ever
touch the rd_smgr field directly. The normal coding rule is to use
RelationGetSmgr(rel) and not expect the result to be valid for longer
than one smgr function call. There are a couple of places where using
the function every single time seemed like overkill, but they are now
annotated with large warning comments.
Amul Sul, after an idea of mine.
Discussion: https://postgr.es/m/CANiYTQsU7yMFpQYnv=BrcRVqK_3U3mtAzAsJCaqtzsDHfsUbdQ@mail.gmail.com
This commit is contained in:
@@ -163,9 +163,9 @@ btbuildempty(Relation index)
|
||||
* this even when wal_level=minimal.
|
||||
*/
|
||||
PageSetChecksumInplace(metapage, BTREE_METAPAGE);
|
||||
smgrwrite(index->rd_smgr, INIT_FORKNUM, BTREE_METAPAGE,
|
||||
smgrwrite(RelationGetSmgr(index), INIT_FORKNUM, BTREE_METAPAGE,
|
||||
(char *) metapage, true);
|
||||
log_newpage(&index->rd_smgr->smgr_rnode.node, INIT_FORKNUM,
|
||||
log_newpage(&RelationGetSmgr(index)->smgr_rnode.node, INIT_FORKNUM,
|
||||
BTREE_METAPAGE, metapage, true);
|
||||
|
||||
/*
|
||||
@@ -173,7 +173,7 @@ btbuildempty(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);
|
||||
}
|
||||
|
||||
/*
|
||||
|
@@ -637,9 +637,6 @@ _bt_blnewpage(uint32 level)
|
||||
static void
|
||||
_bt_blwritepage(BTWriteState *wstate, Page page, BlockNumber blkno)
|
||||
{
|
||||
/* Ensure rd_smgr is open (could have been closed by relcache flush!) */
|
||||
RelationOpenSmgr(wstate->index);
|
||||
|
||||
/* XLOG stuff */
|
||||
if (wstate->btws_use_wal)
|
||||
{
|
||||
@@ -659,7 +656,7 @@ _bt_blwritepage(BTWriteState *wstate, Page page, BlockNumber blkno)
|
||||
if (!wstate->btws_zeropage)
|
||||
wstate->btws_zeropage = (Page) palloc0(BLCKSZ);
|
||||
/* don't set checksum for all-zero page */
|
||||
smgrextend(wstate->index->rd_smgr, MAIN_FORKNUM,
|
||||
smgrextend(RelationGetSmgr(wstate->index), MAIN_FORKNUM,
|
||||
wstate->btws_pages_written++,
|
||||
(char *) wstate->btws_zeropage,
|
||||
true);
|
||||
@@ -674,14 +671,14 @@ _bt_blwritepage(BTWriteState *wstate, Page page, BlockNumber blkno)
|
||||
if (blkno == wstate->btws_pages_written)
|
||||
{
|
||||
/* extending the file... */
|
||||
smgrextend(wstate->index->rd_smgr, MAIN_FORKNUM, blkno,
|
||||
smgrextend(RelationGetSmgr(wstate->index), MAIN_FORKNUM, blkno,
|
||||
(char *) page, true);
|
||||
wstate->btws_pages_written++;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* overwriting a block we zero-filled before */
|
||||
smgrwrite(wstate->index->rd_smgr, MAIN_FORKNUM, blkno,
|
||||
smgrwrite(RelationGetSmgr(wstate->index), MAIN_FORKNUM, blkno,
|
||||
(char *) page, true);
|
||||
}
|
||||
|
||||
@@ -1428,10 +1425,7 @@ _bt_load(BTWriteState *wstate, BTSpool *btspool, BTSpool *btspool2)
|
||||
* still not be on disk when the crash occurs.
|
||||
*/
|
||||
if (wstate->btws_use_wal)
|
||||
{
|
||||
RelationOpenSmgr(wstate->index);
|
||||
smgrimmedsync(wstate->index->rd_smgr, MAIN_FORKNUM);
|
||||
}
|
||||
smgrimmedsync(RelationGetSmgr(wstate->index), MAIN_FORKNUM);
|
||||
}
|
||||
|
||||
/*
|
||||
|
Reference in New Issue
Block a user