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 1lUoJC-0006RZ-UI for pgsql-general@arkaria.postgresql.org; Fri, 09 Apr 2021 10:24:15 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lUoJA-0003rt-73 for pgsql-general@arkaria.postgresql.org; Fri, 09 Apr 2021 10:24:12 +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 1lUoJ9-0003rl-RQ for pgsql-general@lists.postgresql.org; Fri, 09 Apr 2021 10:24:11 +0000 Received: from lana.depesz.com ([88.198.49.178] helo=depesz.com) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lUoJ1-0007mK-AX for pgsql-general@postgresql.org; Fri, 09 Apr 2021 10:24:09 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=depesz.com; s=20170201; h=In-Reply-To:Content-Transfer-Encoding:Content-Type: MIME-Version:References:Reply-To:Message-ID:Subject:Cc:To:Sender:From:Date: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=9dufTQZyTaWd+oMinK/sQ1faN9OWvfPfXbqI21tydLA=; b=vVYXCwAZxCaSfh7klXPjn/mYTc +HjaLXUUmsjQN8XWiGXnhQ/k6Z2IWocOiqvS57zoFtH2ZJQUMOLVRjwA9/kRXKoFIBn+MWMJKGY/Z Hqo5vMadTwSmD9hmGCYDvcy097BGmo7q6JGQk+HHS8AYbP0PnOuZT0umj5SCWmnsrzqE=; Received: from lana.depesz.com ([88.198.49.178] helo=depesz.com) by depesz.com with esmtpa (Exim 4.92) (envelope-from ) id 1lUoIy-0008Py-Lu; Fri, 09 Apr 2021 12:24:00 +0200 Date: Fri, 9 Apr 2021 12:24:00 +0200 From: hubert depesz lubaczewski Sender: depesz@depesz.com To: Niels Jespersen Cc: "pgsql-general@postgresql.org" Subject: Re: where clauses including timstamptz and intervals Message-ID: <20210409102400.GA2310@depesz.com> Reply-To: depesz@depesz.com References: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: User-Agent: Mutt/1.10.1 (2018-07-13) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Fri, Apr 09, 2021 at 07:24:54AM +0000, Niels Jespersen wrote: > Hello all > > Are these two queries exactly eqivalent? The table is partitioned on > r_time, which is a timestamptz. The explain plans are not exactly the > same. The first wants to scan a partition more than the latter. > > select f.xx from f > where f.r_time >= '2020-10-01 00:00:00+00'::timestamptz > and f.r_time < ('2020-10-01 00:00:00+00'::timestamptz + interval '1 month'); > > select f.xx from f > where f.r_time >= '2020-10-01 00:00:00+00'::timestamptz > and f.r_time < ('2020-11-01 00:00:00+00'::timestamptz); It depends on timezone. For example, in my timezone: $ select ('2020-10-01 00:00:00+00'::timestamptz + interval '1 month'), '2020-11-01 00:00:00+00'::timestamptz; ?column? │ timestamptz ────────────────────────┼──────────────────────── 2020-11-01 02:00:00+01 │ 2020-11-01 01:00:00+01 (1 row) Please note that there is 1 hour difference. The reason is that somewhere in there we change time due to daylight savings. If I'd set timezone to UTC, suddently it's the same: $ set timezone=UTC; SET $ select ('2020-10-01 00:00:00+00'::timestamptz + interval '1 month') = '2020-11-01 00:00:00+00'::timestamptz; ?column? ────────── t (1 row) As usual - time is more complicated than one could expect. Best regards, depesz