Received: from makus.postgresql.org (makus.postgresql.org [98.129.198.125]) by mail.postgresql.org (Postfix) with ESMTP id 3BE8F841FF4 for ; Wed, 25 Apr 2012 19:02:59 -0300 (ADT) Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1SNAIY-00071q-VH for pgsql-bugs@postgresql.org; Wed, 25 Apr 2012 22:02:58 +0000 Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.2/8.14.2) with ESMTP id q3PM2fIl005674; Wed, 25 Apr 2012 18:02:41 -0400 (EDT) To: duncan.burke@orionvm.com.au cc: pgsql-bugs@postgresql.org Subject: Re: BUG #6608: SELECT FOR UPDATE not obtaining row exclusive locks in CTEs In-reply-to: References: Comments: In-reply-to duncan.burke@orionvm.com.au message dated "Mon, 23 Apr 2012 05:32:43 -0000" Date: Wed, 25 Apr 2012 18:02:41 -0400 Message-ID: <5673.1335391361@sss.pgh.pa.us> From: Tom Lane X-Pg-Spam-Score: -1.9 (-) X-Archive-Number: 201204/148 X-Sequence-Number: 32899 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