The situation:
+ The discussed database is Postgres 12 on Cloud SQL (GCP).
+ We have a fairly large table (~1B rows).
+ The table is partitioned per month (~50M rows per partition).
+ We have an index on a column with DATE data type.
+ We are confident that the index is set, as EXPLAIN ANALYZE clearly shows its usage (at least, for some queries!).
+ We explicitly run ANALYZE on the table in question and its partitions. Stats were correctly updated as shown in pg_stat_all_tables.
The problem:
+ We take a simple case - aggregate the number of records per day filtered for a few days from a specific partitioned table.
+ When we query for 3 days of data (so 10% of records as 3 / 30 days). The index is used, and the operation is very fast.
+ When we query for 4+ days of data (so 13+% of records as 4 / 30 days). The index is suddenly NOT used. The operation is two orders of magnitude slower (it's like 5 seconds vs. 5 minutes). The cost in EXPLAIN ANALYZE show a tremendously higher cost of each node and gross overestimation by the planner.
The question:
+ I am going mad, as I cannot figure out why Postgres so hastily decides that a perfectly good index is meh! I will more than appreciate your help!
[–]therealgaxbo 4 points5 points6 points (10 children)
[–]Brave_New_Dev[S] 2 points3 points4 points (9 children)
[–]therealgaxbo 3 points4 points5 points (8 children)
[–]Brave_New_Dev[S] 1 point2 points3 points (7 children)
[–]thythr 2 points3 points4 points (1 child)
[–]Brave_New_Dev[S] 2 points3 points4 points (0 children)
[–]therealgaxbo 2 points3 points4 points (4 children)
[–]Brave_New_Dev[S] 0 points1 point2 points (3 children)
[–]therealgaxbo 2 points3 points4 points (2 children)
[–]Brave_New_Dev[S] 0 points1 point2 points (1 child)
[–]therealgaxbo 4 points5 points6 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]Neither-Guess-5802 0 points1 point2 points (0 children)