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

From Alvaro Herrera
Subject Re: file-locking and postmaster.pid
Date
Msg-id 20060524203440.GA6607@surnet.cl
Whole thread Raw
In response to Re: file-locking and postmaster.pid  (korry <korry@appx.com>)
Responses Re: file-locking and postmaster.pid
List pgsql-hackers
korry wrote:

> > The Win32 API for locking seems mighty strange to me.
> 
> Linux/Unix byte locking is advisory (meaning that one lock can block
> another lock, but it can't block a read).

No -- it is advisory meaning that a process that does not try to acquire
the lock is not locked out.  You can certainly block a file in exclusive
mode, using the LOCK_EX flag.  (And at least on my Linux system, there
is mandatory locking too, using the fcntl() interface).

I think the next question is -- how would the lock interface be used?
We could acquire an exclusive lock on postmaster start (to make sure no
backend is running), then reduce it to a shared lock.  Every backend
would inherit the shared lock.  But the lock exchange is not guaranteed
to be atomic so a new postmaster could start just after we acquire the
lock and acquire the shared lock.  It'd need to be complemented with
another lock.

> Win32 locking is mandatory (at least in the most portable form) so a
> lock blocks a reader.

There is also shared/exclusive locking of a file on Win32.  My comment
weas more directed at the fact that you have to "create some sort of
lock handle" from a file handle and then lock the lock handle, or
something like that.  I don't recall the exact details but it was
strange (as opposed to just open and then flock).

-- 
Alvaro Herrera                                http://www.CommandPrompt.com/
The PostgreSQL Company - Command Prompt, Inc.


pgsql-hackers by date:

Previous
From: Andrew Dunstan
Date:
Subject: Re: file-locking and postmaster.pid
Next
From: Tom Lane
Date:
Subject: Re: file-locking and postmaster.pid