From: Tom Lane Date: Mon, 16 Aug 2010 17:33:22 +0000 (+0000) Subject: Arrange to fsync the contents of lockfiles (both postmaster.pid and the X-Git-Tag: REL8_0_26~18 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=e8b4a23b12f99fb15f2aabd1d6b649d5bdf9fb83;p=thirdparty%2Fpostgresql.git Arrange to fsync the contents of lockfiles (both postmaster.pid and the socket lockfile) when writing them. The lack of an fsync here may well explain two different reports we've seen of corrupted lockfile contents, which doesn't particularly bother the running server but can prevent a new server from starting if the old one crashes. Per suggestion from Alvaro. Back-patch to all supported versions. --- diff --git a/src/backend/utils/init/miscinit.c b/src/backend/utils/init/miscinit.c index eb98c95c158..f566fa4f977 100644 --- a/src/backend/utils/init/miscinit.c +++ b/src/backend/utils/init/miscinit.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/utils/init/miscinit.c,v 1.137.4.3 2009/12/09 21:58:55 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/utils/init/miscinit.c,v 1.137.4.4 2010/08/16 17:33:22 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -787,6 +787,17 @@ CreateLockFile(const char *filename, bool amPostmaster, (errcode_for_file_access(), errmsg("could not write lock file \"%s\": %m", filename))); } + if (pg_fsync(fd)) + { + int save_errno = errno; + + close(fd); + unlink(filename); + errno = save_errno; + ereport(FATAL, + (errcode_for_file_access(), + errmsg("could not write lock file \"%s\": %m", filename))); + } if (close(fd)) { int save_errno = errno; @@ -950,6 +961,13 @@ RecordSharedMemoryInLockFile(unsigned long id1, unsigned long id2) close(fd); return; } + if (pg_fsync(fd)) + { + ereport(LOG, + (errcode_for_file_access(), + errmsg("could not write to file \"%s\": %m", + directoryLockFile))); + } if (close(fd)) { ereport(LOG,