From 808e1e75fb376ff7b39d0c3712fa85ff6e0f0720 Mon Sep 17 00:00:00 2001 From: Noah Misch Date: Fri, 12 Apr 2019 22:36:38 -0700 Subject: Consistently test for in-use shared memory. postmaster startup scrutinizes any shared memory segment recorded in postmaster.pid, exiting if that segment matches the current data directory and has an attached process. When the postmaster.pid file was missing, a starting postmaster used weaker checks. Change to use the same checks in both scenarios. This increases the chance of a startup failure, in lieu of data corruption, if the DBA does "kill -9 `head -n1 postmaster.pid` && rm postmaster.pid && pg_ctl -w start". A postmaster will no longer stop if shmat() of an old segment fails with EACCES. A postmaster will no longer recycle segments pertaining to other data directories. That's good for production, but it's bad for integration tests that crash a postmaster and immediately delete its data directory. Such a test now leaks a segment indefinitely. No "make check-world" test does that. win32_shmem.c already avoided all these problems. In 9.6 and later, enhance PostgresNode to facilitate testing. Back-patch to 9.4 (all supported versions). Reviewed (in earlier versions) by Daniel Gustafsson and Kyotaro HORIGUCHI. Discussion: https://postgr.es/m/20190408064141.GA2016666@rfd.leadboat.com --- src/backend/utils/init/postinit.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'src/backend/utils/init/postinit.c') diff --git a/src/backend/utils/init/postinit.c b/src/backend/utils/init/postinit.c index 0a4295b4181..5fe47b9f716 100644 --- a/src/backend/utils/init/postinit.c +++ b/src/backend/utils/init/postinit.c @@ -415,9 +415,11 @@ InitCommunication(void) { /* * We're running a postgres bootstrap process or a standalone backend. - * Create private "shmem" and semaphores. + * Though we won't listen on PostPortNumber, use it to select a shmem + * key. This increases the chance of detecting a leftover live + * backend of this DataDir. */ - CreateSharedMemoryAndSemaphores(true, 0); + CreateSharedMemoryAndSemaphores(PostPortNumber); } } -- cgit v1.2.3