Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rsmZe-009Deg-BD for pgsql-sql@arkaria.postgresql.org; Fri, 05 Apr 2024 16:37:54 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1rsmZc-002Uxe-V6 for pgsql-sql@arkaria.postgresql.org; Fri, 05 Apr 2024 16:37:52 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rsmZc-002UxW-Lw for pgsql-sql@lists.postgresql.org; Fri, 05 Apr 2024 16:37:52 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rsmZa-000ord-7m for pgsql-sql@postgresql.org; Fri, 05 Apr 2024 16:37:52 +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 435Gbl1x1344784; Fri, 5 Apr 2024 12:37:47 -0400 From: Tom Lane To: Miguel Angel Prada cc: pgsql-sql@postgresql.org Subject: Re: Help with error date_trunc() function. In-reply-to: <425370c4-c278-48fc-a1db-ef5aa980da8e@hoplasoftware.com> References: <425370c4-c278-48fc-a1db-ef5aa980da8e@hoplasoftware.com> Comments: In-reply-to Miguel Angel Prada message dated "Fri, 05 Apr 2024 18:23:42 +0200" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1344782.1712335067.1@sss.pgh.pa.us> Date: Fri, 05 Apr 2024 12:37:47 -0400 Message-ID: <1344783.1712335067@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Miguel Angel Prada writes: > I would need help to know what could be happening to cause the error > when using the /date_trunc/ function. EXPLAIN would be informative, but I bet what is happening is that the date_trunc condition is being pushed down to the scan of pg_class, since it mentions no variables that aren't available there. Then it can get evaluated on tables whose names don't match the pattern you expect. I doubt it's more than luck that PG 15 isn't doing the same thing. The usual recommendation for fixing this kind of thing is to insert an optimization fence to keep the WHERE clause from being pushed down. You could add "OFFSET 0" in the sub-select, or convert it into a materialized CTE. regards, tom lane