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 1pXOIG-0002Tb-26 for pgsql-bugs@arkaria.postgresql.org; Wed, 01 Mar 2023 15:23:00 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pXOIE-00086n-Rb for pgsql-bugs@arkaria.postgresql.org; Wed, 01 Mar 2023 15:22:58 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pXOIE-000828-J9 for pgsql-bugs@lists.postgresql.org; Wed, 01 Mar 2023 15:22:58 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pXOIC-0006BM-BZ for pgsql-bugs@lists.postgresql.org; Wed, 01 Mar 2023 15:22:57 +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 321FMrmc1390595; Wed, 1 Mar 2023 10:22:53 -0500 From: Tom Lane To: Julien Rouhaud cc: "David G. Johnston" , Keyerror Smart , pgsql-bugs@lists.postgresql.org Subject: Re: BUG #17812: LOCK TABLE IN ACCESS EXCLUSIVE MODE with a view returns an empty tuple set In-reply-to: <20230301071512.b3ratpciflkrzkeb@jrouhaud> References: <17812-206eaec1d133a94a@postgresql.org> <20230228135252.agaj42dik5qfjjxi@jrouhaud> <20230301025229.rxmmjxfvg45w2cj4@jrouhaud> <1311639.1677643997@sss.pgh.pa.us> <1328697.1677653602@sss.pgh.pa.us> <20230301071512.b3ratpciflkrzkeb@jrouhaud> Comments: In-reply-to Julien Rouhaud message dated "Wed, 01 Mar 2023 15:15:12 +0800" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1390593.1677684173.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Wed, 01 Mar 2023 10:22:53 -0500 Message-ID: <1390594.1677684173@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Julien Rouhaud writes: > On Wed, Mar 01, 2023 at 01:53:22AM -0500, Tom Lane wrote: >> Maybe it isn't. The code flow for CREATE TABLE AS is a bit weird >> IIRC, and maybe it's missing a step where we should update the >> active snapshot. > I think it comes from this chunk in ExecCreateTableAs(): Hm. There are a lot of places that do this: > PushCopiedSnapshot(GetActiveSnapshot()); > UpdateActiveSnapshotCommandId(); rather than PushActiveSnapshot(GetTransactionSnapshot()); which is what would have the effect of noticing changes from other sessions. Digging into the history of that, I found this commit: Author: Tom Lane Branch: master Release: REL9_1_BR [c0b007603] 2011-02-28 23:28:06 -0500 Rearrange snapshot handling to make rule expansion more consistent. = With this patch, portals, SQL functions, and SPI all agree that there should be only a CommandCounterIncrement between the queries that are generated from a single SQL command by rule expansion. Fetching a who= le new snapshot now happens only between original queries. This is equiv= alent to the existing behavior of EXPLAIN ANALYZE, and it was judged to be t= he best choice since it eliminates one source of concurrency hazards for rules. The patch should also make things marginally faster by reducin= g the number of snapshot push/pop operations. So I guess it's intended behavior that we don't notice other-session changes between steps of a single command. Whether that rule should apply to CREATE TABLE AS is perhaps debatable --- but I see several other places that are doing it exactly like this, so it's not like CREATE TABLE AS is alone in its folly. I'm pretty hesitant to change this without substantial thought. regards, tom lane