From 8f4245c7ac44a4e246e896290cbf089eb7c8f566 Mon Sep 17 00:00:00 2001 From: Heikki Linnakangas Date: Tue, 4 Jul 2023 17:57:03 +0300 Subject: [PATCH] Ensure that creation of an empty relfile is fsync'd at checkpoint. If you create a table and don't insert any data into it, the relation file is never fsync'd. You don't lose data, because an empty table doesn't have any data to begin with, but if you crash and lose the file, subsequent operations on the table will fail with "could not open file" error. To fix, register an fsync request in mdcreate(), like we do for mdwrite(). Per discussion, we probably should also fsync the containing directory after creating a new file. But that's a separate and much wider issue. Backpatch to all supported versions. Reviewed-by: Andres Freund, Thomas Munro Discussion: https://www.postgresql.org/message-id/d47d8122-415e-425c-d0a2-e0160829702d%40iki.fi (cherry picked from commit 1b4f1c6f8a6c0b764636f25e17d37d8cab7e82be) Changes comparing to the original commit: 1. 'mdfd' variable is not presented in 'mdcreate()' in this version, so use a proper alternative instead of it. --- src/backend/storage/smgr/md.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/backend/storage/smgr/md.c b/src/backend/storage/smgr/md.c index 820dc166edfc..8b39c5f49ba1 100644 --- a/src/backend/storage/smgr/md.c +++ b/src/backend/storage/smgr/md.c @@ -342,6 +342,9 @@ mdcreate(SMgrRelation reln, ForkNumber forkNum, bool isRedo) reln->md_fd[forkNum]->mdfd_vfd = fd; reln->md_fd[forkNum]->mdfd_segno = 0; reln->md_fd[forkNum]->mdfd_chain = NULL; + + if (!SmgrIsTemp(reln)) + register_dirty_segment(reln, forkNum, reln->md_fd[forkNum]); } /*