summaryrefslogtreecommitdiff
path: root/src/backend/statistics
diff options
context:
space:
mode:
authorTomas Vondra2022-01-15 17:17:20 +0000
committerTomas Vondra2022-01-15 18:06:48 +0000
commit20b9fa308ebf7d4a26ac53804fce1c30f781d60c (patch)
tree92ae872e92ccf4f90f42cda6db34e13ede4d8651 /src/backend/statistics
parent74527c3e022d3ace648340b79a6ddec3419f6732 (diff)
Build inherited extended stats on partitioned tables
Commit 859b3003de disabled building of extended stats for inheritance trees, to prevent updating the same catalog row twice. While that resolved the issue, it also means there are no extended stats for declaratively partitioned tables, because there are no data in the non-leaf relations. That also means declaratively partitioned tables were not affected by the issue 859b3003de addressed, which means this is a regression affecting queries that calculate estimates for the whole inheritance tree as a whole (which includes e.g. GROUP BY queries). But because partitioned tables are empty, we can invert the condition and build statistics only for the case with inheritance, without losing anything. And we can consider them when calculating estimates. It may be necessary to run ANALYZE on partitioned tables, to collect proper statistics. For declarative partitioning there should no prior statistics, and it might take time before autoanalyze is triggered. For tables partitioned by inheritance the statistics may include data from child relations (if built 859b3003de), contradicting the current code. Report and patch by Justin Pryzby, minor fixes and cleanup by me. Backpatch all the way back to PostgreSQL 10, where extended statistics were introduced (same as 859b3003de). Author: Justin Pryzby Reported-by: Justin Pryzby Backpatch-through: 10 Discussion: https://postgr.es/m/20210923212624.GI831%40telsasoft.com
Diffstat (limited to 'src/backend/statistics')
-rw-r--r--src/backend/statistics/dependencies.c9
-rw-r--r--src/backend/statistics/extended_stats.c9
2 files changed, 12 insertions, 6 deletions
diff --git a/src/backend/statistics/dependencies.c b/src/backend/statistics/dependencies.c
index f1602cd3a26..bbc29b67117 100644
--- a/src/backend/statistics/dependencies.c
+++ b/src/backend/statistics/dependencies.c
@@ -1418,10 +1418,13 @@ dependencies_clauselist_selectivity(PlannerInfo *root,
int unique_exprs_cnt;
/*
- * When dealing with inheritance trees, ignore extended stats (which were
- * built without data from child rels, and thus do not represent them).
+ * When dealing with regular inheritance trees, ignore extended stats
+ * (which were built without data from child rels, and thus do not
+ * represent them). For partitioned tables data there's no data in the
+ * non-leaf relations, so we build stats only for the inheritance tree.
+ * So for partitioned tables we do consider extended stats.
*/
- if (rte->inh)
+ if (rte->inh && rte->relkind != RELKIND_PARTITIONED_TABLE)
return 1.0;
/* check if there's any stats that might be useful for us. */
diff --git a/src/backend/statistics/extended_stats.c b/src/backend/statistics/extended_stats.c
index b23c15e9db4..5762621673b 100644
--- a/src/backend/statistics/extended_stats.c
+++ b/src/backend/statistics/extended_stats.c
@@ -1698,10 +1698,13 @@ statext_mcv_clauselist_selectivity(PlannerInfo *root, List *clauses, int varReli
RangeTblEntry *rte = planner_rt_fetch(rel->relid, root);
/*
- * When dealing with inheritance trees, ignore extended stats (which were
- * built without data from child rels, and thus do not represent them).
+ * When dealing with regular inheritance trees, ignore extended stats
+ * (which were built without data from child rels, and thus do not
+ * represent them). For partitioned tables data there's no data in the
+ * non-leaf relations, so we build stats only for the inheritance tree.
+ * So for partitioned tables we do consider extended stats.
*/
- if (rte->inh)
+ if (rte->inh && rte->relkind != RELKIND_PARTITIONED_TABLE)
return sel;
/* check if there's any stats that might be useful for us. */