Re: Thanks, naming conventions, and count()

From: Michael Samuel <michael(at)miknet(dot)net>
To: pgsql-hackers(at)postgresql(dot)org
Subject: Re: Thanks, naming conventions, and count()
Date: 2001-05-02 11:28:11
Message-ID: 20010502212811.A28770@miknet.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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(at)miknet(dot)net>

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Olivier PRENANT 2001-05-02 11:51:21 change database owner
Previous Message Hannu Krosing 2001-05-02 09:58:38 Re: Thanks, naming conventions, and count()