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 1kqMp2-0005Id-JO for pgsql-general@arkaria.postgresql.org; Fri, 18 Dec 2020 20:57:56 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kqMp1-00018p-8W for pgsql-general@arkaria.postgresql.org; Fri, 18 Dec 2020 20:57:55 +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 1kqMp1-00018i-13 for pgsql-general@lists.postgresql.org; Fri, 18 Dec 2020 20:57:55 +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 1kqMow-0005G7-Qa for pgsql-general@postgresql.org; Fri, 18 Dec 2020 20:57:54 +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 0BIKvjiE212892; Fri, 18 Dec 2020 15:57:45 -0500 From: Tom Lane To: Michael Lewis cc: Laurenz Albe , Craig McIlwee , pgsql-general Subject: Re: Unexpected result count from update statement on partitioned table In-reply-to: References: <5bd2c45ec838ae7bd87e6940b344b5e2638d124d.camel@cybertec.at> <167832.1608318979@sss.pgh.pa.us> Comments: In-reply-to Michael Lewis message dated "Fri, 18 Dec 2020 13:29:15 -0700" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <212890.1608325065.1@sss.pgh.pa.us> Date: Fri, 18 Dec 2020 15:57:45 -0500 Message-ID: <212891.1608325065@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Michael Lewis writes: > On Fri, Dec 18, 2020 at 12:16 PM Tom Lane wrote: >> Yeah, this is a fairly fundamental shortcoming in inheritance_planner(): >> it supposes that it can duplicate the whole query for each target table. > Are there other examples of gotchas with this? Would it be any volatile > function (or behavior like skip locked) in a sub-query? Right, anything that causes multiple executions to not deliver identical results. The different executions will use the same snapshot, so there's not a hazard from external changes to the DB, but internal sources of nonrepeatability are a problem. > ... what is the > factor that means the sub-query would be executed multiple times? If it's in the FROM clause of an UPDATE or DELETE on a table with inheritance children (either traditional inheritance or partitioning). Actually, after further thought, I'm not entirely sure that the issue is confined to inherited UPDATE/DELETE. If you had such a sub-SELECT in an ordinary join, and the planner chose to put it on the inside of a nestloop, you'd have a problem. I do not think there's any check to avoid doing that just because the subquery's results are potentially volatile. Probably evaluation-cost considerations would discourage such a plan in most cases, but there's no direct defense AFAIR. > With the behavior change for CTEs to no longer be materialized by default > in PG12... why does the CTE still mean it is executed only once? Is it > because it is NOT side effect free (locking) so it cannot be in-lined? Exactly. regards, tom lane