pg.ddx.io pgsql-bugs@postgresql.org mailing list archive
help / color / mirror / Atom feedBUG #6608: SELECT FOR UPDATE not obtaining row exclusive locks in CTEs
2+ messages / 2 participants
[nested] [flat]
* BUG #6608: SELECT FOR UPDATE not obtaining row exclusive locks in CTEs
@ 2012-04-23 05:32 duncan.burke@orionvm.com.au
0 siblings, 1 reply; 2+ messages in thread
From: duncan.burke@orionvm.com.au @ 2012-04-23 05:32 UTC (permalink / raw)
To: pgsql-bugs
The following bug has been logged on the website:
Bug reference: 6608
Logged by: Duncan Burke
Email address: duncan.burke@orionvm.com.au
PostgreSQL version: 9.1.3
Operating system: gentoo
Description:
I found that running a SELECT FOR UPDATE query in a CTE does not block
simultaneous transactions from running the same query. i.e it appears to not
be obtaining an exclusive row lock as expected.
CREATE TABLE foo (
x int PRIMARY KEY,
y int
);
INSERT INTO foo VALUES (0,0);
CREATE FUNCTION lock_0(int) returns int as $$
WITH locked as (
SELECT 1 FROM foo
WHERE x = $1
FOR UPDATE)
SELECT 1
$$ LANGUAGE SQL;
CREATE FUNCTION lock_1(int) returns int as $$
WITH locked as (
UPDATE FOO
SET y = y
WHERE x = $1)
SELECT 1
$$ LANGUAGE SQL;
--run in two simultaneous transactions, lock_0 does not block
BEGIN;
SELECT lock_0(0);
COMMIT;
--run in two simultaneous transactions, lock_1 blocks
BEGIN;
SELECT lock_1(0);
COMMIT;
^ permalink raw reply [nested|flat] 2+ messages in thread
* Re: BUG #6608: SELECT FOR UPDATE not obtaining row exclusive locks in CTEs
@ 2012-04-25 22:02 Tom Lane <tgl@sss.pgh.pa.us>
parent: duncan.burke@orionvm.com.au
0 siblings, 0 replies; 2+ messages in thread
From: Tom Lane @ 2012-04-25 22:02 UTC (permalink / raw)
To: duncan.burke@orionvm.com.au; +Cc: pgsql-bugs
duncan.burke@orionvm.com.au writes:
> I found that running a SELECT FOR UPDATE query in a CTE does not block
> simultaneous transactions from running the same query.
The reason this test case doesn't do anything:
> CREATE FUNCTION lock_0(int) returns int as $$
> WITH locked as (
> SELECT 1 FROM foo
> WHERE x = $1
> FOR UPDATE)
> SELECT 1
> $$ LANGUAGE SQL;
is that the CTE is unreferenced. While we force INSERT/UPDATE/DELETE
CTEs to be executed even when not referenced, that does not apply to
SELECTs; see
http://www.postgresql.org/docs/9.1/static/queries-with.html
which states "execution of a SELECT is carried only as far as the
primary query demands its output".
If I change the function to say "WITH ... SELECT * FROM locked" then
blocking occurs as expected.
regards, tom lane
^ permalink raw reply [nested|flat] 2+ messages in thread
end of thread, other threads:[~2012-04-25 22:02 UTC | newest]
Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2012-04-23 05:32 BUG #6608: SELECT FOR UPDATE not obtaining row exclusive locks in CTEs duncan.burke@orionvm.com.au
2012-04-25 22:02 ` Tom Lane <tgl@sss.pgh.pa.us>
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