Received: from magus.postgresql.org (magus.postgresql.org [87.238.57.229]) by mail.postgresql.org (Postfix) with ESMTP id BC81632FEDF for ; Mon, 23 Apr 2012 02:33:02 -0300 (ADT) Received: from mahout.postgresql.org ([98.129.198.123]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1SMBtS-0006pf-5h for pgsql-bugs@postgresql.org; Mon, 23 Apr 2012 05:33:01 +0000 Received: from [204.145.124.246] (helo=wrigleys.postgresql.org) by mahout.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1SMBtE-00089N-9b for pgsql-bugs@postgresql.org; Mon, 23 Apr 2012 05:32:45 +0000 Received: from www-data by wrigleys.postgresql.org with local (Exim 4.72) (envelope-from ) id 1SMBtD-0006lv-FG for pgsql-bugs@postgresql.org; Mon, 23 Apr 2012 05:32:43 +0000 Date: Mon, 23 Apr 2012 05:32:43 +0000 Message-Id: Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Subject: BUG #6608: SELECT FOR UPDATE not obtaining row exclusive locks in CTEs To: pgsql-bugs@postgresql.org From: duncan.burke@orionvm.com.au X-Pg-Spam-Score: -1.9 (-) X-Archive-Number: 201204/129 X-Sequence-Number: 32880 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 =3D $1 FOR UPDATE) SELECT 1 $$ LANGUAGE SQL; CREATE FUNCTION lock_1(int) returns int as $$ WITH locked as ( UPDATE FOO SET y =3D y WHERE x =3D $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;