From: Thomas Kellerer <spam_eater@gmx.net>
To: pgsql-general@lists.postgresql.org
Subject: Re: Select "todays" timestamps in an index friendly way
Date: Tue, 23 Oct 2018 12:11:55 +0200
Message-ID: <4f78c8fc-d4bd-4219-2877-12004f4dda90@gmx.net> (raw)
In-Reply-To: <20181023093801.GA21523@lutz-pc.ecm4u.intra>
References: <20181023093801.GA21523@lutz-pc.ecm4u.intra>
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: spam_eater@gmx.net
Subject: Re: Select "todays" timestamps in an index friendly way
In-Reply-To: <4f78c8fc-d4bd-4219-2877-12004f4dda90@gmx.net>
References: <20181023093801.GA21523@lutz-pc.ecm4u.intra> <4f78c8fc-d4bd-4219-2877-12004f4dda90@gmx.net>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox