Re: flock user defined function

From: Doug McNaught <doug(at)mcnaught(dot)org>
To: Chris Goughnour <cgoughnour(at)hotp(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: flock user defined function
Date: 2004-06-22 22:50:33
Message-ID: 87zn6vdx6u.fsf@asmodeus.mcnaught.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Martijn van Oosterhout <kleptog(at)svana(dot)org> writes:

> On Tue, Jun 22, 2004 at 02:49:27PM -0700, Chris Goughnour wrote:
>> I'm trying to write two C language user defined functions, lockfile() and
>> unlockfile(), that call flock using LOCK_EX and LOCK_UN respectively. If I
>> call lockfile from a first psql process it returns successfully. Calling
>> lockfile from a second psql process blocks. However, when I call unlockfile
>> from the first psql process, the second process still blocks. The lockfile
>> call from the second psql proccess doesn't return until I kill the first
>> psql process.
>> Any suggestions? Thanks in advance.
>> Chris Goughnour
>
> <snip>
>
> Where do you close the file? That might cause some issues.

Yeah, it's generally best not to call LOCK_UN at all, but just to
close the file (which will release the locks). Otherwise, if you are
using stdio, you can get a situation where the file is unlocked but
its stdio buffer has not been flushed, leading to the corruption you
were trying to avoid by locking the file...

-Doug

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Chris Goughnour 2004-06-22 23:02:37 Re: flock user defined function
Previous Message Tom Lane 2004-06-22 22:49:05 Re: flock user defined function