Avoid unlikely data-loss scenarios due to rename() without fsync.
authorAndres Freund
Thu, 10 Mar 2016 02:53:54 +0000 (18:53 -0800)
committerAndres Freund
Thu, 10 Mar 2016 02:53:54 +0000 (18:53 -0800)
commit4f37d091694d77b15f36a8a220025f5faa1ed296
treed480e56ad59dd977a1b8ab475341d567ae1ca475
parent43b491a817fb494bef22c9e8374a9fddfd30ebda
Avoid unlikely data-loss scenarios due to rename() without fsync.

Renaming a file using rename(2) is not guaranteed to be durable in face
of crashes. Use the previously added durable_rename()/durable_link_or_rename()
in various places where we previously just renamed files.

Most of the changed call sites are arguably not critical, but it seems
better to err on the side of too much durability.  The most prominent
known case where the previously missing fsyncs could cause data loss is
crashes at the end of a checkpoint. After the actual checkpoint has been
performed, old WAL files are recycled. When they're filled, their
contents are fdatasynced, but we did not fsync the containing
directory. An OS/hardware crash in an unfortunate moment could then end
up leaving that file with its old name, but new content; WAL replay
would thus not replay it.

Reported-By: Tomas Vondra
Author: Michael Paquier, Tomas Vondra, Andres Freund
Discussion: 56583BDD.9060302@2ndquadrant.com
Backpatch: All supported branches
contrib/pg_stat_statements/pg_stat_statements.c
src/backend/access/transam/timeline.c
src/backend/access/transam/xlog.c
src/backend/access/transam/xlogarchive.c
src/backend/postmaster/pgarch.c
src/backend/utils/misc/guc.c