From 96435b05c9546b6da829043fb10b2a7309216bd2 Mon Sep 17 00:00:00 2001 From: Andres Freund Date: Mon, 21 May 2018 15:43:30 -0700 Subject: [PATCH v2 1/6] freespace: Don't constantly close files when reading buffer. fsm_readbuf() used to always do an smgrexists() when reading a buffer beyond the known file size. That currently implies closing the md.c handle, loosing all the data cached therein. Change this to only check for file existance when not already known to be larger than 0 blocks. Author: Andres Freund Reviewed-By: Discussion: https://postgr.es/m/ Backpatch: --- src/backend/storage/freespace/freespace.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/backend/storage/freespace/freespace.c b/src/backend/storage/freespace/freespace.c index 65c4e74999f..d7569cec5ed 100644 --- a/src/backend/storage/freespace/freespace.c +++ b/src/backend/storage/freespace/freespace.c @@ -556,7 +556,7 @@ fsm_readbuf(Relation rel, FSMAddress addr, bool extend) * not on extension.) */ if (rel->rd_smgr->smgr_fsm_nblocks == InvalidBlockNumber || - blkno >= rel->rd_smgr->smgr_fsm_nblocks) + rel->rd_smgr->smgr_fsm_nblocks == 0) { if (smgrexists(rel->rd_smgr, FSM_FORKNUM)) rel->rd_smgr->smgr_fsm_nblocks = smgrnblocks(rel->rd_smgr, @@ -564,6 +564,9 @@ fsm_readbuf(Relation rel, FSMAddress addr, bool extend) else rel->rd_smgr->smgr_fsm_nblocks = 0; } + else if (blkno >= rel->rd_smgr->smgr_fsm_nblocks) + rel->rd_smgr->smgr_fsm_nblocks = smgrnblocks(rel->rd_smgr, + FSM_FORKNUM); /* Handle requests beyond EOF */ if (blkno >= rel->rd_smgr->smgr_fsm_nblocks) -- 2.17.0.rc1.dirty