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 1leIFK-0005CS-HA for pgsql-general@arkaria.postgresql.org; Wed, 05 May 2021 14:11:26 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1leIFI-0004ZG-TY for pgsql-general@arkaria.postgresql.org; Wed, 05 May 2021 14:11:24 +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 1leIFI-0004Z4-KV for pgsql-general@lists.postgresql.org; Wed, 05 May 2021 14:11:24 +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 1leIFC-0006oY-Gw for pgsql-general@lists.postgresql.org; Wed, 05 May 2021 14:11:24 +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 145EBF9G4015565; Wed, 5 May 2021 10:11:16 -0400 From: Tom Lane To: Tomas Vondra cc: Pavel Luzanov , pgsql-general@lists.postgresql.org Subject: Re: Strange behavior of function date_trunc In-reply-to: <8986e9dd-576a-3c3b-b523-3052519387fd@enterprisedb.com> References: <6b4f0c89-3c15-d5bd-7909-62cc33b1a106@postgrespro.ru> <8986e9dd-576a-3c3b-b523-3052519387fd@enterprisedb.com> Comments: In-reply-to Tomas Vondra message dated "Wed, 05 May 2021 15:55:04 +0200" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <4015563.1620223875.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Wed, 05 May 2021 10:11:15 -0400 Message-ID: <4015564.1620223875@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Tomas Vondra writes: > On 5/5/21 3:23 PM, Pavel Luzanov wrote: >> It is very likely that the date_trunc function in the following example= = >> is executed for each line of the query. Although it marked as a STABLE = >> and could only be called once. > It could, but that's just an option - the database may do that, but it's= = > not required to do it. In this case it might be beneficial, but it'd = > make the planner more complex etc. Yeah, there simply is not any provision for caching the results of stable functions in the way Pavel seems to be imagining. People have played around with patches for that, but nothing's been accepted. > You can use CTE to execute it just once, I think: > with x as (select date_trunc('day', '2021-04-01'::timestamptz) as x) > select * from t where a > (select x from x); Actually it's sufficient to write select * from t where a > (select date_trunc('day', '2021-04-01'::timestam= ptz)) Postgres interprets that as an uncorrelated sub-select, so it's only done once per outer query. I think that these days, the CTE form would be flattened into that anyway (without MATERIALIZED). regards, tom lane