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 1lAMaX-0008In-QQ for pgsql-general@arkaria.postgresql.org; Fri, 12 Feb 2021 00:45:37 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lAMaW-0003PA-OH for pgsql-general@arkaria.postgresql.org; Fri, 12 Feb 2021 00:45:36 +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 1lAMaW-0003P3-Ga for pgsql-general@lists.postgresql.org; Fri, 12 Feb 2021 00:45:36 +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 1lAMaT-0007Xc-UC for pgsql-general@lists.postgresql.org; Fri, 12 Feb 2021 00:45:36 +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 11C0jUpe1511207; Thu, 11 Feb 2021 19:45:30 -0500 From: Tom Lane To: Steve Baldwin cc: Michael Lewis , "David G. Johnston" , "pgsql-generallists.postgresql.org" Subject: Re: Consequence of changes to CTE's in 12 In-reply-to: References: Comments: In-reply-to Steve Baldwin message dated "Fri, 12 Feb 2021 11:36:52 +1100" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1511205.1613090730.1@sss.pgh.pa.us> Date: Thu, 11 Feb 2021 19:45:30 -0500 Message-ID: <1511206.1613090730@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Steve Baldwin writes: > Is there a chance that the query optimiser should 'notice' the > pg_try_advisory_xact_lock function, and not be so clever when it sees it? The general policy with respect to volatile functions in WHERE quals is "here be dragons". You don't have enough control over when a WHERE clause will be evaluated to be sure about what the semantics will be; and we don't want to tie the optimizer's hands to the extent that would be needed to make it fully predictable. In this particular case, you can make it fairly safe by making sure there are optimization fences both above and below where the WHERE clause is. You have one above from the LIMIT 1, but (with the new interpretation of CTEs) not one below it. Adding a fence -- either OFFSET 0 or LIMIT ALL -- to the first CTE should fix it in a reasonably version-independent fashion. regards, tom lane