Re: BUG #2830: Wrong results for prepared statements - Mailing list pgsql-bugs

From Kevin Grittner
Subject Re: BUG #2830: Wrong results for prepared statements
Date
Msg-id 458664F4.EE98.0025.0@wicourts.gov
Whole thread Raw
In response to Re: BUG #2830: Wrong results for prepared statements while clustering target table  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-bugs
>>> On Sat, Dec 16, 2006 at  5:15 PM, in message
<8738.1166310917@sss.pgh.pa.us>,
Tom Lane <tgl@sss.pgh.pa.us> wrote:
> "" <martin.pihlak@gmail.com> writes:
>> Prepared SELECT/UPDATE/DELETE statements produce wrong results if
executed
>> while target table is being clustered.
>
> The short answer is "don't CLUSTER while the table is in live use"
...
>
> CLUSTER re- inserts all the rows in the table into a fresh table.
This
> means that all the rows appear to have been inserted by the CLUSTER
> transaction, and therefore that a transaction that scans the table
> afterward with a snapshot taken before the CLUSTER committed will
not
> see those rows.

This really should be documented in the CLUSTER command.  Having been
falsely reassured by the following statement in the documentation, I
have clustered tables in live use.

"When a table is being clustered, an ACCESS EXCLUSIVE lock is acquired
on it. This prevents any other database operations (both reads and
writes) from operating on the table until the CLUSTER is finished."

We have crontab jobs to do daily clusters on some of our small tables
with high update rates.  The databases are 24/7 with fairly high usage
(query requests in the 10s of millions per day).  Should we be load
shifting off of a server before doing the sub-second CLUSTER on a small,
high update table?

> The difference between EXECUTE and SELECT behavior here is just a
chance
> matter of exactly where the snap is taken during the parse/execute
code
> path ---  your SELECT works because it blocks for AccessShareLock on
the
> table before it sets the snap.  But SELECT would fail just the same
way
> within a serializable transaction that had already set its snapshot.

Ouch!  I don't suppose there is any reasonable way to cover this with
locks?

-Kevin

pgsql-bugs by date:

Previous
From: "CN"
Date:
Subject: BUG #2827
Next
From: "William ZHANG"
Date:
Subject: BUG #2835: Table inheritance and statement level trigger.