
PostgreSQL global statistics on partitionned table require a manual ANALYZE
PostgreSQL auto-analyze collects statistics on tables with rows. For partitioned tables, it excludes the parent as it has no rows by itself. So how does the query planner estimate cardinality when a query spans multiple partitions? Some statistics are easy to derive: if it knows each partition’s row count, the global count is the total. Column statistics are trickier, especially with the number of distinct values, a key factor to estimate cardinalities with predicates or aggregates. Even with the number of distinct values per partition, it still doesn’t know how much those values overlap across partitions. The global distinct count therefore lies between the maximum per-partition distinct count and the sum of all per-partition counts. Here is an example: create table history ( year int , num serial , x int , y int , primary key ( year , num ) ) partition by list ( year ) ; create table history_2024 partition of history for values in ( 2024 ); create table history_2025 partition of hist
Continue reading on Dev.to
Opens in a new tab

