From 07d6798edac7e2a7427745ce8ac22c5405573139 Mon Sep 17 00:00:00 2001 From: Adam Lee Date: Mon, 23 Nov 2026 15:24:21 +0800 Subject: [PATCH v2 1/2] Avoid orphaning buffers when a relation's main fork is missing When relations are dropped, DropRelationsAllBuffers() avoids scanning the whole buffer pool if it can find the size of every fork in the cache. When a fork's size is not cached it calls smgrexists(), and if the fork's file does not exist it skips the fork, treating it as having no buffers. If a relation's main fork file has gone missing while dirty buffers for it are still resident, and the relation has no other forks (for example a small table that has no FSM yet), every fork is skipped and nothing is dropped. The buffers are left orphaned, and every later checkpoint fails trying to write them back: ERROR: could not open file "base/5/16413": No such file or directory CONTEXT: writing block 0 of relation "base/5/16413" A clean shutdown fails the same way. If any other fork exists, its size is not cached either, so the code already falls back to the full scan and the problem doesn't show up. Before the targeted-drop optimization (commit bea449c635c, v14) the buffer pool was always scanned in full, so such buffers were dropped. Restore that for the main fork: if it is missing, fall back to the full scan. Only the main fork is treated this way, because the FSM, VM and init forks are routinely absent on healthy relations. Backpatch to 14, where the optimization was added. --- src/backend/storage/buffer/bufmgr.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 4dbc25c6c50..819a32551bd 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -4973,7 +4973,21 @@ DropRelationsAllBuffers(SMgrRelation *smgr_reln, int nlocators) if (block[i][j] == InvalidBlockNumber) { if (!smgrexists(rels[i], j)) + { + /* + * Every relation with storage has a main fork, so if it's + * missing, its file was lost while buffers for it may + * still be resident. Fall back to the full scan so those + * buffers get dropped instead of orphaned. Other forks + * are routinely absent, so just skip them. + */ + if (j == MAIN_FORKNUM) + { + cached = false; + break; + } continue; + } cached = false; break; } -- 2.52.0