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 1leHzh-0004Y1-UA for pgsql-general@arkaria.postgresql.org; Wed, 05 May 2021 13:55:18 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1leHzg-000508-SJ for pgsql-general@arkaria.postgresql.org; Wed, 05 May 2021 13:55:16 +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 1leHzg-0004zz-Jg for pgsql-general@lists.postgresql.org; Wed, 05 May 2021 13:55:16 +0000 Received: from mail-wr1-x436.google.com ([2a00:1450:4864:20::436]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1leHzZ-0006gz-PU for pgsql-general@lists.postgresql.org; Wed, 05 May 2021 13:55:16 +0000 Received: by mail-wr1-x436.google.com with SMTP id a4so1957755wrr.2 for ; Wed, 05 May 2021 06:55:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb-com.20150623.gappssmtp.com; s=20150623; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-language:content-transfer-encoding; bh=KkKQ9b6OHOqJlPdySzzoV25dOYHnDpYtCQLndxmth00=; b=oKghWXA0XBPcaYtPJfV5I/IJ/UydiqVrqc0PQ2GNkib//7xtG3vUSB3IBgtv1PWDQT QCc4Kxy538eoz8f/8FBQRufTLFH/yM1FS8OzgCQykhzEu78KcTL2aeTvE0ZRAo3lq3Ek L7Qk9mCcLDFTeGlrvZ88KJg4QyPk3lDIFNerca2UBEKwlOWB+5wByUxT6unMiUJqC5ee EjWAJFpaJohFmcfovJDZ0GYrGKQrxfGLq04qoTss81WlFgvdr5zk85Xx1mGl/ajOwC8e 8Oq764CPHfYtjcg7REp9fYxkJdF3laqUQlBU7yl9zrsJ8KG4Z9Qu+c3qXsDid2K2OC7S Dmvw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=KkKQ9b6OHOqJlPdySzzoV25dOYHnDpYtCQLndxmth00=; b=LlNp6jRfi/XNWerVhVKjKP4dKgMqAQ6TEpWVQW2tiyEnzCeFyEVnOYcQAVtT0KXWjV wvijHKPlUZ1USD0DFwXsRIRrlDQ1NFhNHhX8VjwxntyLUZ2GaoD4VaW5OIicBpFQe8eG YAq6+fp68lyixhvQJ+Uqi1IZ87MJtep2uX7hmZBRpy9gD9U70r6+0zvHhBvWzvAlE5yC cKkTYykbOSy4KcDipIhMqliagWsPIOMVLpnK6i2qYAqJ6jWZYyWjq5XXGHv7ws367Mbj i4WRWf8VD9HeYfFAj0own5kaCnt3VgYNga1/J6Rlji6u9DP3ub7YzkHJOtgHmVxAAuau cjqw== X-Gm-Message-State: AOAM533TLdVD+H6CR7h6RYVGBFS7hd1VVx/TmkuM4VK6sUoqZk+4WbAw j1DhPW+cLwQEguirHBEFmbq00ki+DiwRDVGXw6VX8IVcVmsQ1OtJ1tax68sjvYyeiT1VvpTKDre RyQTUHbrU/JUY4ARxBXNmd5jKFY3Q8J2Vy2evAtGIBd4DHSsFOkv/nxwGsf90RlAgGnsKNNmKOs YrUQyp56WZ9mV8ZCjIOZkE7H7WCp9/S4IEAvWizGeqXBPiHcWNjFWeYUCXMgRmPFxuN1h3bTqGZ l/AzKPIGqTCRwFbuIZwO7C0KDd7kVVXI+xYbm8Lssl8+h3wjzFnmw== X-Google-Smtp-Source: ABdhPJwtPoMJ0IhT2R26s23PrPq2ZMx/XaUyKllEJSxiCLrj3tUFurG/nSY9yku1LaFI+V1dNcHFig== X-Received: by 2002:adf:f6c5:: with SMTP id y5mr39205124wrp.121.1620222908062; Wed, 05 May 2021 06:55:08 -0700 (PDT) Received: from [10.137.0.18] (static-84-42-175-93.net.upcbroadband.cz. [84.42.175.93]) by smtp.gmail.com with ESMTPSA id o13sm5110338wmh.34.2021.05.05.06.55.07 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 05 May 2021 06:55:07 -0700 (PDT) Subject: Re: Strange behavior of function date_trunc To: Pavel Luzanov , pgsql-general@lists.postgresql.org References: <6b4f0c89-3c15-d5bd-7909-62cc33b1a106@postgrespro.ru> From: Tomas Vondra Message-ID: <8986e9dd-576a-3c3b-b523-3052519387fd@enterprisedb.com> Date: Wed, 5 May 2021 15:55:04 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.8.1 MIME-Version: 1.0 In-Reply-To: <6b4f0c89-3c15-d5bd-7909-62cc33b1a106@postgrespro.ru> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit X-CLOUD-SEC-AV-Info: enterprisedb,google_mail,monitor X-CLOUD-SEC-AV-Sent: true X-Gm-Spam: 0 X-Gm-Phishy: 0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 5/5/21 3:23 PM, Pavel Luzanov wrote: > 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. > 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. > 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? > Well, it'd not like date_trunc is executed for each row while now() is executed only once. The functions are executed for each row in both cases, but now() is simply much cheaper - it just returns a value that is already calculated, while date_trunc has to parse and truncate the value, etc. 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); regards Tomas