summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2010-08-16 17:32:53 +0000
committerTom Lane <tgl@sss.pgh.pa.us>2010-08-16 17:32:53 +0000
commitc93b652c2dbafa6b5d3e59ca8c68814c0b03f502 (patch)
tree13d2ec5543901fbe77c9506ab88e1e13d30366dc
parent59ea02a108908e5814e5bda21a86b126b849a837 (diff)
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.
-rw-r--r--src/backend/utils/init/miscinit.c27
1 files changed, 24 insertions, 3 deletions
diff --git a/src/backend/utils/init/miscinit.c b/src/backend/utils/init/miscinit.c
index e6b6676706f..21b9277f384 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.184 2010/04/20 23:48:47 tgl Exp $
+ * $PostgreSQL: pgsql/src/backend/utils/init/miscinit.c,v 1.184.2.1 2010/08/16 17:32:53 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -818,6 +818,9 @@ CreateLockFile(const char *filename, bool amPostmaster,
* admin) but has left orphan backends behind. Check for this by
* looking to see if there is an associated shmem segment that is
* still in use.
+ *
+ * Note: because postmaster.pid is written in two steps, we might not
+ * find the shmem ID values in it; we can't treat that as an error.
*/
if (isDDLock)
{
@@ -881,7 +884,18 @@ CreateLockFile(const char *filename, bool amPostmaster,
(errcode_for_file_access(),
errmsg("could not write lock file \"%s\": %m", filename)));
}
- if (close(fd))
+ if (pg_fsync(fd) != 0)
+ {
+ 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) != 0)
{
int save_errno = errno;
@@ -1042,7 +1056,14 @@ RecordSharedMemoryInLockFile(unsigned long id1, unsigned long id2)
close(fd);
return;
}
- if (close(fd))
+ if (pg_fsync(fd) != 0)
+ {
+ ereport(LOG,
+ (errcode_for_file_access(),
+ errmsg("could not write to file \"%s\": %m",
+ DIRECTORY_LOCK_FILE)));
+ }
+ if (close(fd) != 0)
{
ereport(LOG,
(errcode_for_file_access(),