From 613e03112c40b27c9b1185648886b894263e3e8d Mon Sep 17 00:00:00 2001 From: Adam Lee Date: Mon, 23 Nov 2026 15:24:30 +0800 Subject: [PATCH v2 2/2] Forget pending fsyncs when dropping a main fork that is already gone When a relation's main fork is unlinked outside of recovery, mdunlinkfork() truncates the first segment and leaves it as a tombstone until the next checkpoint, instead of unlinking it right away. Because the file is expected to stay around, no forget request is sent, and any pending fsync request for it is simply carried out by the checkpointer. If the file was already missing, though, that fsync fails with ENOENT, and since no cancel ever arrives the checkpointer PANICs: PANIC: could not fsync file "base/5/16413": No such file or directory So if truncating the first segment fails with ENOENT, send a forget request for it. There is nothing left to sync. Backpatch to all supported versions. --- src/backend/storage/smgr/md.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/backend/storage/smgr/md.c b/src/backend/storage/smgr/md.c index de630aa6ef8..9eab465f6ec 100644 --- a/src/backend/storage/smgr/md.c +++ b/src/backend/storage/smgr/md.c @@ -418,8 +418,16 @@ mdunlinkfork(RelFileLocatorBackend rlocator, ForkNumber forknum, bool isRedo) /* Prevent other backends' fds from holding on to the disk space */ ret = do_truncate(path.str); - /* Register request to unlink first segment later */ + /* + * If the file is already gone, nothing is left to sync, so forget + * any pending sync requests. Otherwise the next checkpoint would + * fail to fsync it and PANIC. + */ save_errno = errno; + if (ret < 0 && save_errno == ENOENT) + register_forget_request(rlocator, forknum, 0 /* first seg */ ); + + /* Register request to unlink first segment later */ register_unlink_tombstone(rlocator); errno = save_errno; } -- 2.52.0