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