Skip to content

Commit 31bc563

Browse files
committed
Perform an immediate shutdown if the postmaster.pid file is removed.
The postmaster now checks every minute or so (worst case, at most two minutes) that postmaster.pid is still there and still contains its own PID. If not, it performs an immediate shutdown, as though it had received SIGQUIT. The original goal behind this change was to ensure that failed buildfarm runs would get fully cleaned up, even if the test scripts had left a postmaster running, which is not an infrequent occurrence. When the buildfarm script removes a test postmaster's $PGDATA directory, its next check on postmaster.pid will fail and cause it to exit. Previously, manual intervention was often needed to get rid of such orphaned postmasters, since they'd block new test postmasters from obtaining the expected socket address. However, by checking postmaster.pid and not something else, we can provide additional robustness: manual removal of postmaster.pid is a frequent DBA mistake, and now we can at least limit the damage that will ensue if a new postmaster is started while the old one is still alive. Back-patch to all supported branches, since we won't get the desired improvement in buildfarm reliability otherwise.
1 parent f5bbaee commit 31bc563

File tree

3 files changed

+112
-11
lines changed

3 files changed

+112
-11
lines changed

src/backend/postmaster/postmaster.c

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1609,9 +1609,10 @@ ServerLoop(void)
16091609
fd_set readmask;
16101610
int nSockets;
16111611
time_t now,
1612+
last_lockfile_recheck_time,
16121613
last_touch_time;
16131614

1614-
last_touch_time = time(NULL);
1615+
last_lockfile_recheck_time = last_touch_time = time(NULL);
16151616

16161617
nSockets = initMasks(&readmask);
16171618

@@ -1761,27 +1762,56 @@ ServerLoop(void)
17611762
if (StartWorkerNeeded || HaveCrashedWorker)
17621763
maybe_start_bgworker();
17631764

1765+
#ifdef HAVE_PTHREAD_IS_THREADED_NP
1766+
1767+
/*
1768+
* With assertions enabled, check regularly for appearance of
1769+
* additional threads. All builds check at start and exit.
1770+
*/
1771+
Assert(pthread_is_threaded_np() == 0);
1772+
#endif
1773+
1774+
/*
1775+
* Lastly, check to see if it's time to do some things that we don't
1776+
* want to do every single time through the loop, because they're a
1777+
* bit expensive. Note that there's up to a minute of slop in when
1778+
* these tasks will be performed, since DetermineSleepTime() will let
1779+
* us sleep at most that long.
1780+
*/
1781+
now = time(NULL);
1782+
1783+
/*
1784+
* Once a minute, verify that postmaster.pid hasn't been removed or
1785+
* overwritten. If it has, we force a shutdown. This avoids having
1786+
* postmasters and child processes hanging around after their database
1787+
* is gone, and maybe causing problems if a new database cluster is
1788+
* created in the same place. It also provides some protection
1789+
* against a DBA foolishly removing postmaster.pid and manually
1790+
* starting a new postmaster. Data corruption is likely to ensue from
1791+
* that anyway, but we can minimize the damage by aborting ASAP.
1792+
*/
1793+
if (now - last_lockfile_recheck_time >= 1 * SECS_PER_MINUTE)
1794+
{
1795+
if (!RecheckDataDirLockFile())
1796+
{
1797+
ereport(LOG,
1798+
(errmsg("performing immediate shutdown because data directory lock file is invalid")));
1799+
kill(MyProcPid, SIGQUIT);
1800+
}
1801+
last_lockfile_recheck_time = now;
1802+
}
1803+
17641804
/*
17651805
* Touch Unix socket and lock files every 58 minutes, to ensure that
17661806
* they are not removed by overzealous /tmp-cleaning tasks. We assume
17671807
* no one runs cleaners with cutoff times of less than an hour ...
17681808
*/
1769-
now = time(NULL);
17701809
if (now - last_touch_time >= 58 * SECS_PER_MINUTE)
17711810
{
17721811
TouchSocketFiles();
17731812
TouchSocketLockFiles();
17741813
last_touch_time = now;
17751814
}
1776-
1777-
#ifdef HAVE_PTHREAD_IS_THREADED_NP
1778-
1779-
/*
1780-
* With assertions enabled, check regularly for appearance of
1781-
* additional threads. All builds check at start and exit.
1782-
*/
1783-
Assert(pthread_is_threaded_np() == 0);
1784-
#endif
17851815
}
17861816
}
17871817

src/backend/utils/init/miscinit.c

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,6 +1151,76 @@ AddToDataDirLockFile(int target_line, const char *str)
11511151
}
11521152

11531153

1154+
/*
1155+
* Recheck that the data directory lock file still exists with expected
1156+
* content. Return TRUE if the lock file appears OK, FALSE if it isn't.
1157+
*
1158+
* We call this periodically in the postmaster. The idea is that if the
1159+
* lock file has been removed or replaced by another postmaster, we should
1160+
* do a panic database shutdown. Therefore, we should return TRUE if there
1161+
* is any doubt: we do not want to cause a panic shutdown unnecessarily.
1162+
* Transient failures like EINTR or ENFILE should not cause us to fail.
1163+
* (If there really is something wrong, we'll detect it on a future recheck.)
1164+
*/
1165+
bool
1166+
RecheckDataDirLockFile(void)
1167+
{
1168+
int fd;
1169+
int len;
1170+
long file_pid;
1171+
char buffer[BLCKSZ];
1172+
1173+
fd = open(DIRECTORY_LOCK_FILE, O_RDWR | PG_BINARY, 0);
1174+
if (fd < 0)
1175+
{
1176+
/*
1177+
* There are many foreseeable false-positive error conditions. For
1178+
* safety, fail only on enumerated clearly-something-is-wrong
1179+
* conditions.
1180+
*/
1181+
switch (errno)
1182+
{
1183+
case ENOENT:
1184+
case ENOTDIR:
1185+
/* disaster */
1186+
ereport(LOG,
1187+
(errcode_for_file_access(),
1188+
errmsg("could not open file \"%s\": %m",
1189+
DIRECTORY_LOCK_FILE)));
1190+
return false;
1191+
default:
1192+
/* non-fatal, at least for now */
1193+
ereport(LOG,
1194+
(errcode_for_file_access(),
1195+
errmsg("could not open file \"%s\": %m; continuing anyway",
1196+
DIRECTORY_LOCK_FILE)));
1197+
return true;
1198+
}
1199+
}
1200+
len = read(fd, buffer, sizeof(buffer) - 1);
1201+
if (len < 0)
1202+
{
1203+
ereport(LOG,
1204+
(errcode_for_file_access(),
1205+
errmsg("could not read from file \"%s\": %m",
1206+
DIRECTORY_LOCK_FILE)));
1207+
close(fd);
1208+
return true; /* treat read failure as nonfatal */
1209+
}
1210+
buffer[len] = '\0';
1211+
close(fd);
1212+
file_pid = atol(buffer);
1213+
if (file_pid == getpid())
1214+
return true; /* all is well */
1215+
1216+
/* Trouble: someone's overwritten the lock file */
1217+
ereport(LOG,
1218+
(errmsg("lock file \"%s\" contains wrong PID: %ld instead of %ld",
1219+
DIRECTORY_LOCK_FILE, file_pid, (long) getpid())));
1220+
return false;
1221+
}
1222+
1223+
11541224
/*-------------------------------------------------------------------------
11551225
* Version checking support
11561226
*-------------------------------------------------------------------------

src/include/miscadmin.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,7 @@ extern void CreateSocketLockFile(const char *socketfile, bool amPostmaster,
449449
const char *socketDir);
450450
extern void TouchSocketLockFiles(void);
451451
extern void AddToDataDirLockFile(int target_line, const char *str);
452+
extern bool RecheckDataDirLockFile(void);
452453
extern void ValidatePgVersion(const char *path);
453454
extern void process_shared_preload_libraries(void);
454455
extern void process_local_preload_libraries(void);

0 commit comments

Comments
 (0)