Re: Thanks, naming conventions, and count() - Mailing list pgsql-hackers

From Michael Samuel
Subject Re: Thanks, naming conventions, and count()
Date
Msg-id 20010502212811.A28770@miknet.net
Whole thread Raw
In response to Re: Thanks, naming conventions, and count()  (Alfred Perlstein <bright@wintelcom.net>)
List pgsql-hackers
On Sun, Apr 29, 2001 at 08:17:28PM -0700, Alfred Perlstein wrote:
> Sort of, if that flat file is in the form of:
> 123456;"tablename                   "
> 000033;"another_table               "

Or better yet, since the flat file is unlikely to be large, you could
just do this dance:

1) open file for reading
2) flock() file exclusively, non-blocking.
3) If 2 failed, sleep a bit, then go back to 1, otherwise open new file  for writing
4) Write out new file
5) rename() the temp file over the new file
6) close files, etc

That way, you'll never have the race of 2 programs trying to write the file
at a time (therefore losing changes), and you get total atomicity of the
writing.

You could also do it with an open(O_EXCL) on a fixed temp file, instead of
the flock() call.  The semantics should be the same.

Of course, you could always fork() a child to handle this in the background,
as it's hardly important to the running of the database. (Or if it is, it
can become part of the transaction, which means that at rename() time, there
must be no room for other failures, but it mustn't be too late to roll back)

-- 
Michael Samuel <michael@miknet.net>


pgsql-hackers by date:

Previous
From: Kovacs Zoltan
Date:
Subject: \c connects as another user instead I want in psql
Next
From: Roberto Mello
Date:
Subject: Re: v7.1.1 branched and released on Tuesday ...