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 1leHUm-0003Lk-Kp for pgsql-general@arkaria.postgresql.org; Wed, 05 May 2021 13:23:21 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1leHUl-0003K1-E5 for pgsql-general@arkaria.postgresql.org; Wed, 05 May 2021 13:23:19 +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 1leHUl-0003Jr-2T for pgsql-general@lists.postgresql.org; Wed, 05 May 2021 13:23:19 +0000 Received: from mail.postgrespro.ru ([93.174.131.139]) by makus.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1leHUd-0000pX-UR for pgsql-general@lists.postgresql.org; Wed, 05 May 2021 13:23:17 +0000 Received: from [10.0.1.15] (unknown [188.123.231.168]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client did not present a certificate) by mail.postgrespro.ru (Postfix) with ESMTPSA id 53BE321C80DE for ; Wed, 5 May 2021 16:23:09 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mail; t=1620220989; bh=PDZ8N/jx/R3046rZNjXYrggoDdwqtzhtxEXinp0Cri8=; h=To:From:Subject:Date; b=qaGEmZK2O9BT87fnBr02vjGyYWkH4SrbG+fvmDvpZGNGH8fTwhv0PTabYdHQZFuZf Zn5534EFIbwAsaR8Aoc9Y//DWZ43vFcc3scF66Tas5fT2fqZ5c6AArwpzr1fXV+l7P nGWK75vrngggqVCX42oi4Q7RfLJcI9hZs28Sw/fQ= To: pgsql-general@lists.postgresql.org From: Pavel Luzanov Subject: Strange behavior of function date_trunc Message-ID: <6b4f0c89-3c15-d5bd-7909-62cc33b1a106@postgrespro.ru> Date: Wed, 5 May 2021 16:23:09 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.7.1 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hello, 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. EXPLAIN (ANALYZE) SELECT * FROM generate_series('2021-01-01', '2021-06-01', '1 s'::interval) AS g(x) WHERE g.x >= date_trunc('day', '2021-05-05'::timestamptz);                                                           QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------  Function Scan on generate_series g  (cost=0.00..15.00 rows=333 width=8) (actual time=2801.884..3263.328 rows=2332801 loops=1)    Filter: (x >= date_trunc('day'::text, '2021-05-05 00:00:00+03'::timestamp with time zone))    Rows Removed by Filter: 10713600  Planning Time: 0.040 ms  Execution Time: 3336.657 ms When replacing date_trunc with now, the query is much faster: EXPLAIN (ANALYZE) SELECT * FROM generate_series('2021-01-01', '2021-06-01', '1 s'::interval) AS g(x) WHERE g.x >= now();                                                           QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------  Function Scan on generate_series g  (cost=0.00..15.00 rows=333 width=8) (actual time=1648.777..1845.430 rows=2275325 loops=1)    Filter: (x >= now())    Rows Removed by Filter: 10771076  Planning Time: 0.039 ms  Execution Time: 1918.767 ms The variant with now works almost as fast as with the constant. This suggests me that perhaps date_trunc is being executed for every line of the query: EXPLAIN (ANALYZE) SELECT * FROM generate_series('2021-01-01', '2021-06-01', '1 s'::interval) AS g(x) WHERE g.x >= '2021-05-05'::timestamptz;                                                           QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------  Function Scan on generate_series g  (cost=0.00..12.50 rows=333 width=8) (actual time=1628.743..1826.841 rows=2332801 loops=1)    Filter: (x >= '2021-05-05 00:00:00+03'::timestamp with time zone)    Rows Removed by Filter: 10713600  Planning Time: 0.033 ms  Execution Time: 1901.680 ms In this regard, I have two questions: 1. How can I find out exactly how many times the date_trunc function has been executed? So far, these are just my assumptions. 2. If date_trunc is indeed called multiple times, why is this happening? -- Pavel Luzanov Postgres Professional: https://postgrespro.com The Russian Postgres Company