Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mRELS-0000dm-Uj for pgsql-general@arkaria.postgresql.org; Fri, 17 Sep 2021 13:56:02 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mRELR-0008Rm-U9 for pgsql-general@arkaria.postgresql.org; Fri, 17 Sep 2021 13:56:01 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mRELR-0008Rc-LZ for pgsql-general@lists.postgresql.org; Fri, 17 Sep 2021 13:56:01 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mRELP-0007r2-G2 for pgsql-general@lists.postgresql.org; Fri, 17 Sep 2021 13:56:01 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 18HDtuHg1295468; Fri, 17 Sep 2021 09:55:56 -0400 From: Tom Lane To: Alban Hertroys cc: Roman Guryanov , pgsql-general@lists.postgresql.org Subject: Re: SELECT FOR UPDATE returns zero rows with CTE In-reply-to: References: Comments: In-reply-to Alban Hertroys message dated "Fri, 17 Sep 2021 11:36:31 +0200" MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-ID: <1295466.1631886956.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Fri, 17 Sep 2021 09:55:56 -0400 Message-ID: <1295467.1631886956@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Alban Hertroys writes: >> On 17 Sep 2021, at 8:32, Roman Guryanov = wrote: >> Why does SELECT FOR UPDATE return 0 rows in the scenario below? (execut= ion in transaction) > Most likely the outer select returns 0 rows because you locked the rows = you expected in cte2 and didn=E2=80=99t perform an update on those locked = rows yet. I might be wrong (ENOCAFFEINE), but I think what is happening is that the UPDATE updates the row and then the FOR UPDATE filter skips the row on the grounds that the row is already-updated-by-self. In an ordinary UPDATE, there's a hard restriction not to update a row already updated in the same command, to avoid possibly-infinite loops if the same row is visited more than once due to join behavior or the like. I think that we use the same semantics in FOR UPDATE, and I'm pretty sure that the two WITH clauses would be treated as all one command. I'd have to say that overall this example is one of the worst bits of SQL I've seen lately. Aside from the issues Alban noted, the "t1c2 =3D (SELECT t1c2 FROM cte1)" part will fail outright if cte1 returns more than one row, because that's a scalar sub-select not a join. And there's a real question of which WITH clause acts first: yeah, cte2 can't *complete* without running cte1, but it might act partially, including performing the other half of its WHERE. If cte1 were updating t1c1 then I think it'd be pretty close to undefined what results you get. What's the point of doing it like this, rather than just having cte1 return all the columns needed? regards, tom lane