pgsql: Introduce durable_rename() and durable_link_or_rename().

From: Andres Freund <andres(at)anarazel(dot)de>
To: pgsql-committers(at)postgresql(dot)org
Subject: pgsql: Introduce durable_rename() and durable_link_or_rename().
Date: 2016-03-10 03:25:21
Message-ID: E1adrE1-0001PO-Vt@gemulon.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-committers

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(dot)9060302(at)2ndquadrant(dot)com
Backpatch: All supported branches

Branch
------
REL9_1_STABLE

Details
-------
http://git.postgresql.org/pg/commitdiff/d0e47bcd47bcfa4a3be020965c93980f5fad6e80

Modified Files
--------------
src/backend/storage/file/fd.c | 255 +++++++++++++++++++++++++++++++-------
src/backend/storage/file/reinit.c | 2 +-
src/include/storage/fd.h | 4 +-
3 files changed, 211 insertions(+), 50 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Tom Lane 2016-03-10 04:29:11 pgsql: Remove a couple of useless pstrdup() calls.
Previous Message Andres Freund 2016-03-10 03:25:20 pgsql: Introduce durable_rename() and durable_link_or_rename().