Skip to content

Commit

Permalink
Ensure that creation of an empty relfile is fsync'd at checkpoint.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
hlinnaka committed Jul 4, 2023
1 parent c2cff44 commit 6377f70
Showing 1 changed file with 3 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/backend/storage/smgr/md.c
Expand Up @@ -334,6 +334,9 @@ mdcreate(SMgrRelation reln, ForkNumber forkNum, bool isRedo)
mdfd = &reln->md_seg_fds[forkNum][0];
mdfd->mdfd_vfd = fd;
mdfd->mdfd_segno = 0;

if (!SmgrIsTemp(reln))
register_dirty_segment(reln, forkNum, mdfd);
}

/*
Expand Down

0 comments on commit 6377f70

Please sign in to comment.