]> granicus.if.org Git - postgresql/commit
Introduce durable_rename() and durable_link_or_rename().
authorAndres Freund <andres@anarazel.de>
Thu, 10 Mar 2016 02:53:54 +0000 (18:53 -0800)
committerAndres Freund <andres@anarazel.de>
Thu, 10 Mar 2016 02:53:54 +0000 (18:53 -0800)
commitc224d44f77247a2eb8ce43fcaeb519f214c1b7d1
tree97c0fddbe36c15ee5b8fd745d32c8f3663984901
parentc8e059724f35b32e4c54d24349a2e3675067e38c
Introduce durable_rename() and durable_link_or_rename().

Renaming a file using rename(2) is not guaranteed to be durable in face
of crashes; especially on filesystems like xfs and ext4 when mounted
with data=writeback. To be certain that a rename() atomically replaces
the previous file contents in the face of crashes and different
filesystems, one has to fsync the old filename, rename the file, fsync
the new filename, fsync the containing directory.  This sequence is not
generally adhered to currently; which exposes us to data loss risks. To
avoid having to repeat this arduous sequence, introduce
durable_rename(), which wraps all that.

Also add durable_link_or_rename(). Several places use link() (with a
fallback to rename()) to rename a file, trying to avoid replacing the
target file out of paranoia. Some of those rename sequences need to be
durable as well. There seems little reason extend several copies of the
same logic, so centralize the link() callers.

This commit does not yet make use of the new functions; they're used in
a followup commit.

Author: Michael Paquier, Andres Freund
Discussion: 56583BDD.9060302@2ndquadrant.com
Backpatch: All supported branches
src/backend/storage/file/fd.c
src/backend/storage/file/reinit.c
src/include/storage/fd.h