From: Andres Freund <andres@2ndquadrant.com>
To: pgsql-hackers@postgresql.org
Cc: Thom Brown <thom@linux.com>
Cc: Tomas Vondra <tv@fuzzy.cz>
Subject: Re: change in LOCK behavior
Date: Wed, 10 Oct 2012 23:05:26 +0200
Message-ID: <201210102305.26511.andres@2ndquadrant.com> (raw)
In-Reply-To: <CAA-aLv4PF-gM6mt4ZP4GnKGkZE1fheVeUC=8jQCgzvWbaA8+mQ@mail.gmail.com>
References: <5075D8DF.6050500@fuzzy.cz>
<CAA-aLv4PF-gM6mt4ZP4GnKGkZE1fheVeUC=8jQCgzvWbaA8+mQ@mail.gmail.com>
On Wednesday, October 10, 2012 10:43:57 PM Thom Brown wrote:
> On 10 October 2012 21:21, Tomas Vondra <tv@fuzzy.cz> wrote:
> > Hi,
> >
> > I've just noticed a change of LOCK command behavior between 9.1 and 9.2,
> > and I'm not sure whether this is expected or not.
> >
> > Let's use a very simple table
> >
> > CREATE TABLE x (id INT);
> >
> > Say there are two sessions - A and B, where A performs some operations
> > on "x" and needs to protect them with an "ACCESS EXCLUSIVE" lock (e.g.
> > it might be a pg_bulkload that acquires such locks, and we need to do
> > that explicitly on one or two places).
> >
> > Session B is attempting to read the data, but is blocked and waits. On
> > 9.1 it sees the commited data (which is what we need) but on 9.2 it sees
> > only data commited at the time of the lock attemt.
> >
> > Example:
> >
> > A: BEGIN;
> > A: LOCK x IN ACCESS EXCLUSIVE MODE;
> > A: INSERT INTO x VALUES (100);
> > B: SELECT * FROM x;
> > A: COMMIT;
> >
> > Now on 9.1, B receives the value "100" while on 9.2 it gets no rows.
> >
> > Is this expected? I suspect the snapshot is read at different time or
> > something, but I've checked release notes but I haven't seen anything
> > relevant.
> >
> > Without getting the commited version of data, the locking is somehow
> > pointless for us (unless using a different lock, not the table itself).
>
> I suspect it's this commit: d573e239f03506920938bf0be56c868d9c3416da
>
> http://archives.postgresql.org/pgsql-committers/2011-12/msg00167.phpVery likely, yes. In fact you get the same beaviour in 9.1 if you modify the
example slightly:
B: PREPARE foo AS SELECT * FROM x;
A: BEGIN;
A: LOCK x IN ACCESS EXCLUSIVE MODE;
A: INSERT INTO x VALUES (100);
B: EXECUTE foo;
A: COMMIT;
If you think about it for a second its not that surprising anymore. We start to
execute a query, acquire a snapshot for that, and then wait for the locks on
the target relations. We continue executing in the same snapshot for the
duration of the statement and thus cannot see any of the new rows which
committed *after* we assembled our snapshot.
The easy workaround is acquiring a AccessShareLock in the B transaction
separately.
Greetings,
Andres
--
Andres Freund http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-hackers@postgresql.org
Cc: andres@2ndquadrant.com, thom@linux.com, tv@fuzzy.cz
Subject: Re: change in LOCK behavior
In-Reply-To: <201210102305.26511.andres@2ndquadrant.com>
References: <5075D8DF.6050500@fuzzy.cz> <CAA-aLv4PF-gM6mt4ZP4GnKGkZE1fheVeUC=8jQCgzvWbaA8+mQ@mail.gmail.com> <201210102305.26511.andres@2ndquadrant.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox