Query Plan choice with timestamps - Mailing list pgsql-performance

From Giorgio Valoti
Subject Query Plan choice with timestamps
Date
Msg-id 321506D5-4400-470C-909E-C7700D49A540@mac.com
Whole thread Raw
Responses Re: Query Plan choice with timestamps  (Richard Huxton <dev@archonet.com>)
Re: Query Plan choice with timestamps  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-performance
Hi, I have a timestamptz field that I want to use with a query, but I
don’t need the full timestamp resolution, so I’ve created a
day_trunc(timestamptz) immutable function which I’ll use with the
query and with a new index:

logs=> create index test_idx on blackbox (day_trunc(ts));

However, the query plan doesn’t use the index:

logs=>explain select count(*) from blackbox group by day_trunc(ts)
order by day_trunc(ts);
                                         QUERY PLAN
------------------------------------------------------------------------------------------
  GroupAggregate  (cost=98431.58..119773.92 rows=74226 width=8)
    ->  Sort  (cost=98431.58..99050.92 rows=247736 width=8)
          Sort Key: (day_trunc(ts))
          ->  Seq Scan on blackbox  (cost=0.00..72848.36 rows=247736
width=8)
(4 rows)

while with this index:

logs=>create index test_2_idx on blackbox (ts);

the query plan is the expected one:

logs=>explain select count(*) from blackbox group by ts order by ts;
                                         QUERY PLAN
------------------------------------------------------------------------------------------
  GroupAggregate  (cost=0.00..19109.66 rows=74226 width=8)
    ->  Index Scan using test_2_idx on blackbox  (cost=0.00..16943.16
rows=247736 width=8)

But I fail to see why. Any hints?

Thank you in advance
--
Giorgio Valoti

pgsql-performance by date:

Previous
From: dforum
Date:
Subject: Re: Plz Heeeelp! performance settings
Next
From: Richard Huxton
Date:
Subject: Re: Unexpectedly Long DELETE Wait