pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Lutz Horn <lutz.horn@posteo.de>
To: pgsql-general@postgresql.org
Subject: Select "todays" timestamps in an index friendly way
Date: Tue, 23 Oct 2018 11:38:01 +0200
Message-ID: <20181023093801.GA21523@lutz-pc.ecm4u.intra> (raw)

Hi,

I am looking for a way to select all timestamps that are "today" in an
index friendly way. This select should not depend on the concrete value
of "today".

Given a table

    create temporary table t (
      id SERIAL primary key,
      ts timestamp not null default now()
    );

with some data

    insert into t (ts)
    select ts
    from generate_series(
      '2018-01-01T00:00:01'::timestamp,
      '2018-12-31T23:59:59'::timestamp,
      '2 minutes')
    as ts;

and an index

    create index on t (ts, id);

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?

Lutz




view thread (13+ messages)  latest in thread

Message-ID: <20181023093801.GA21523@lutz-pc.ecm4u.intra>
Permalink:  ../20181023093801.GA21523@lutz-pc.ecm4u.intra/
Also on:    postgresql.org/message-id/20181023093801.GA21523@lutz-pc.ecm4u.intra

 · 

reply

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: lutz.horn@posteo.de
  Subject: Re: Select "todays" timestamps in an index friendly way
  In-Reply-To: <20181023093801.GA21523@lutz-pc.ecm4u.intra>
  References: <20181023093801.GA21523@lutz-pc.ecm4u.intra>

* 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