I have the following table:
Table "public.partitioned_11"
Column | Type | Collation | Nullable | Default
----------------+-----------------------------+-----------+----------+----------------------------
id | integer | | not null |
integer_value | integer | | |
invalidated_at | timestamp without time zone | | |
datetime_value | timestamp without time zone | | |
Partition of: partitioned FOR VALUES IN (11)
and the following two indexes:
"idx1" btree (datetime_value) INCLUDE (integer_value) WHERE partitioned_by = 11 AND invalidated_at IS NULL
"idx2" btree (id) INCLUDE (integer_value) WHERE partitioned_by = 11 AND invalidated_at IS NULL
When I filter for integer_value by id, everything works as expected:
explain select integer_value from partitioned where partitioned_by = 11 and invalidated_at is null and id = 1;
QUERY PLAN
--------------------------------------------------------------------------------------------------
Index Only Scan using idx2 on partitioned_11 partitioned (cost=0.43..8.50 rows=4 width=4)
Index Cond: (id = 1)
But when I do the exact same thing with the timestamp, postgres does not like index scan anymore:
explain select integer_value from partitioned where partitioned_by = 11 and invalidated_at is null and datetime_value = '2020-01-01';
QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------------------
Bitmap Heap Scan on partitioned_11 partitioned (cost=5.68..618.81 rows=161 width=4)
Recheck Cond: ((datetime_value = '2020-01-01 00:00:00'::timestamp without time zone) AND (partitioned_by = 11) AND (invalidated_at IS NULL))
-> Bitmap Index Scan on idx1 (cost=0.00..5.64 rows=161 width=0)
Index Cond: (datetime_value = '2020-01-01 00:00:00'::timestamp without time zone)
If I do
set enable_bitmapscan = false;
then an index-only scan is done. I am wondering, why is this the case? Is there a universe where bitmap index scan is better than index-only scan? And does it have anything to do with the timestamp datatype itself?
VACUUM partitioned_11;?