Re: Re: LOCK TABLE

From: Stephan Szabo <sszabo(at)megazone23(dot)bigpanda(dot)com>
To: Neil Conway <nconway(at)klamath(dot)dyndns(dot)org>
Cc: pgsql-general(at)hub(dot)org
Subject: Re: Re: LOCK TABLE
Date: 2000-09-17 19:50:26
Message-ID: Pine.BSF.4.10.10009171246320.44482-100000@megazone23.bigpanda.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Sun, 17 Sep 2000, Neil Conway wrote:

> This table contains a list of messages. One of the fields is the
> path to where the message is stored in plaintext on the filesystem
> (it's not in the DB because it could be > 8K, I also don't want to
> take up a lot of space). Another field is the boolean field 'archived'
> - in other words, has this message been compressed - y/n?
>
> So the app is doing regular maintainence. It goes through the messages,
> and selects a list of the really old ones that are going to be compressed
> to save space. Now, the app goes through the list, and does the actual
> compression on disk. *Here* is where everything could get screwed
> up - if someone queries the DB looking for a message that has been
> compressed on the FS but the transaction hasn't yet been committed,
> they'll read the compressed file off the HD and try to display it -
> but it will be garbage. Of course, once the transaction is committed
> and the 'archived' flag is toggled, the client app will know to
> decompress the file before trying to read it. However, I'm worried
> about the time in between the initial 'SELECT ... FOR UPDATE' and
> when the transaction is committed.
>
> One thing - the filename is the same whether the file is compressed or
> not. So the client app can only look @ the 'archived' boolean to judge
> if it needs to decompress the file.
>
> If I wrap this inside a transaction, other queries will see the state of
> the DB *before* the transaction was started. Which is bad - they will
> see 'archived' is false, and read the compressed file from the FS as
> though it was plaintext.

If you always SELECT ... FOR UPDATE (in all transactions that access it),
then the second one will not see the DB state before the transaction is
started, because the row is locked and the second transaction won't be
able to get its lock and will instead wait. Admittedly this lowers your
ability to have concurrent reads of the same rows as well, so you would
want the other transactions to hold the lock for as short a time as
possible.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Stephan Szabo 2000-09-17 19:51:01 Re: Regression test failures
Previous Message Haroldo Stenger 2000-09-17 19:45:42 Re: Transactions aborting when a query fails