Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1gEtfU-0005XG-AO for pgsql-general@arkaria.postgresql.org; Tue, 23 Oct 2018 10:12:08 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gEtfS-0007R9-CS for pgsql-general@arkaria.postgresql.org; Tue, 23 Oct 2018 10:12:06 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1gEtfS-0007R1-0l for pgsql-general@lists.postgresql.org; Tue, 23 Oct 2018 10:12:06 +0000 Received: from mout.kundenserver.de ([212.227.126.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gEtfK-0003v2-O1 for pgsql-general@lists.postgresql.org; Tue, 23 Oct 2018 10:12:04 +0000 Received: from [10.8.0.146] ([185.40.248.10]) by mrelayeu.kundenserver.de (mreue011 [212.227.15.129]) with ESMTPSA (Nemesis) id 1MMoOy-1fw8KG0TeW-00Im9B for ; Tue, 23 Oct 2018 12:11:56 +0200 Received: from [10.8.0.146] ([185.40.248.10]) by mrelayeu.kundenserver.de (mreue011 [212.227.15.129]) with ESMTPSA (Nemesis) id 1MMoOy-1fw8KG0TeW-00Im9B for ; Tue, 23 Oct 2018 12:11:56 +0200 Subject: Re: Select "todays" timestamps in an index friendly way To: pgsql-general@lists.postgresql.org Newsgroups: gmane.comp.db.postgresql.general References: <20181023093801.GA21523@lutz-pc.ecm4u.intra> From: Thomas Kellerer Message-ID: <4f78c8fc-d4bd-4219-2877-12004f4dda90@gmx.net> Date: Tue, 23 Oct 2018 12:11:55 +0200 User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.23) Gecko/20090812 Thunderbird/2.0.0.23 Mnenhy/0.7.6.666 MIME-Version: 1.0 In-Reply-To: <20181023093801.GA21523@lutz-pc.ecm4u.intra> Content-Type: text/plain; charset=utf-8 Content-Language: de-DE Content-Transfer-Encoding: 7bit X-Provags-ID: V03:K1:XUOzpfuHYR0+xWRPonaA5tCWidn/9GW9v51JWPcs1X+zfaVvKFs VNvtUqvyZ03YtJUq/aVrVqr4N7WepyN44sduYQb4yCqW4/pfUzK/9I71Ve9D4uiySIxtDXo daB4DtdhTHOXls/JGd4+Cz9xqfqkns1xi5C0nlu9CEMPfSQkCETGgey7jOc0VKGoYYdiS7t zYcxIsIMeIuW4K9FzuWJw== X-Spam-Flag: NO X-UI-Out-Filterresults: notjunk:1;V01:K0:HnQVl9+Z/Kc=:P73tfiXrVk5IGvBfBO9F7l ZUoRYp1+1dfgnCjRLOntU+OoXgXU4lVXQFPahtr5QDFkodA4GH/AecxitnMAmuGmbCCskYnUg p7BqnQcsmnIMwWSZwQUxzeH9hQ344FpoS7OGEB2KeKCK6xX9gtOhC/NFKO5/o2SF86zMSQfOW hzKKVXz973qj2gSXYMwIx1yti0QUdNFd4vCXFFACkfciodJBgso5dDgBflfIUjf+uLnfUHk8D 5hsA2ZRWsVDRrCwt/izefwC1rP52vaKM0/7+pzE829wjb/+pz6/ce2+ZXUuFRxsBlBw9dj6TE F1fB19/V4xnhny2SggzcoaZGOJHOOpkWk084kQzFl10vJ3WgMhDUqDN51eksJ749M8Bkif6D7 Kz5ni09fhRK7dxDRIL2qULCc2zIYqTB+64Tkro0maJQapO5fqpEEiw0ZK3o1miR2vYjaouExM VMT5Hd19tkE+xDfYAYjqfJdBEpOPmMLDmjy4bhuswsybNDHGf0u8M2CxaQ+wonhQSkUbP5Gjm AT9HEClOchMnjw2PsWGBcnogSaBNPMKGzzPTl+VCdqVp8XbgRC0StG0no55e5U/+2ul1sguAx DEmaUSV9dcEXAlI9G0FRlieLkQ6RzWv9pgoEGQYfoPyxUB/w40X2Jzp/w0jsiSdfidHnEcOcR 83wAyeAoBR5hrGUtywVvzZhuCAoQH1P2Le11WNUMOeuNVPGAjP9kxCs/jU8gtp0lhxdJO1sSe URRSQRaPMn9yv/Hw List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Lutz Horn schrieb am 23.10.2018 um 11:38: > I can of course make an explicit select for `ts` values that are > "today": > > select ts, id > from t > where ts >= '2018-10-23T00:00:00'::timestamp > and ts <= '2018-10-23T23:59:59'::timestamp; > > This uses an Bitmap Index Scan on `t_ts_id_idx`. Good. > > But the where conditions depends on concrete values of "today" which > will not return the intended result if I execute it tomorrow. I will > have to change the where condition. Not good. > > I am looking for a way to make the where condition independed of the > date of execution. I can create a function > > create function is_today(timestamp) returns boolean as $$ > select to_char(now(), 'YYYY-MM-DD') = to_char($1, 'YYYY-MM-DD'); > $$ language sql; > > that converts the timestamps to text. But using this function > > select * from t where is_today(ts); > > will not benefit from the existing index. A Seq Scan on `t` will be > used. Not good. > > Is there a way to have both: be independed of the concrete value of > "today" *and* use the index on the timestamp column? I typically use: where ts >= date '2018-10-23' and ts < date '2018-10-23' + 1