Re: file-locking and postmaster.pid - Mailing list pgsql-hackers

From Tom Lane
Subject Re: file-locking and postmaster.pid
Date
Msg-id 15187.1148401548@sss.pgh.pa.us
Whole thread Raw
In response to Re: file-locking and postmaster.pid  (Andreas Joseph Krogh <andreak@officenet.no>)
List pgsql-hackers
Andreas Joseph Krogh <andreak@officenet.no> writes:
> On Tuesday 23 May 2006 17:54, Tom Lane wrote:
>> The postmaster does check to see whether the PID mentioned in the file
>> is still alive, so it's not that easy for the above to happen.  If you
>> can provide details of a scenario where a failure is likely, we'd like
>> to know about it.  Also, what PG version are you talking about?

> I have experienced this with PG-8.1.3 and will provide details if I can make 
> it happen. Basically it has happened when I have had to "hard-reset" my 
> laptop due to some strange bugs in Linux which have made it hang.

If you're talking about a postmaster that's auto-started during the boot
sequence, then there is a risk depending on what start script you use.
The problem is that depending on what else runs during the system
startup, the PID assigned to the postmaster might be the same as in the
last boot cycle, or it might be different by one or two counts.  The
postmaster disregards a pidfile containing its own PID, or its parent
process' PID, or a PID not belonging to a postgres-owned process.
That covers most cases but if your start script does something like
su -l postgres -c "pg_ctl start ..."

then you have a situation where not only the parent process (pg_ctl)
but also the grandparent (a shell) is postgres-owned, and if the pidfile
PID happens to match the grandparent then you lose.  Solution is to
either not use pg_ctl here, or write "exec pg_ctl start ...", so that
there's only one postgres-owned process besides the postmaster itself.

Initscripts published by PGDG itself and by Red Hat have gotten this
right for awhile, but I suspect the word has not propagated to all
distros.
        regards, tom lane


pgsql-hackers by date:

Previous
From: Marc Munro
Date:
Subject: Re: New feature proposal
Next
From: Simon Riggs
Date:
Subject: Re: error-free disabling of individual child partition