I create the following table and indexes:
CREATE TABLE test
(
id bigint,
d timestamp without time zone
);
CREATE INDEX f_date4
ON public.test
USING btree
(date(d), id);
CREATE INDEX f_date5
ON public.test
USING btree
(id, date(d));
I fill the table with data and use the following query:
SELECT id, date(d)
FROM test
WHERE date(d) > '2019-09-20'::date;
EXPLAIN shows that f_date4 index is being used on condition d > '2019-09-20'::date, but i can't get INDEX ONLY SCAN. What are the possible reasons, why this happens and how to avoid this?
Execution plan:
Index Scan using f_date4 on test (cost=0.06..0.07 rows=1 width=12) (actual time=0.005..0.005 rows=0 loops=1)
Index Cond: (date(d) > '2019-01-20'::date)
Buffers: shared hit=2
Planning time: 0.131 ms
Execution time: 0.025 ms
I use postgresql 10.6
Thanks in advance!
WHERE d > '2019-09-20'::TIMESTAMP, then you wouldn't need bother with all these date conversions.WHERE d >= '2019-09-21'would suffice, IMHO.