1
0
mirror of https://github.com/postgres/postgres.git synced 2025-12-06 00:02:13 +03:00

Ignore extended statistics for inheritance trees

Since commit 859b3003de we only build extended statistics for individual
relations, ignoring the child relations. This resolved the issue with
updating catalog tuple twice, but we still tried to use the statistics
when calculating estimates for the whole inheritance tree. When the
relations contain very distinct data, it may produce bogus estimates.

This is roughly the same issue 427c6b5b9 addressed ~15 years ago, and we
fix it the same way - by ignoring extended statistics when calculating
estimates for the inheritance tree as a whole. We still consider
extended statistics when calculating estimates for individual child
relations, of course.

This may result in plan changes due to different estimates, but if the
old statistics were not describing the inheritance tree particularly
well it's quite likely the new plans is actually better.

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
This commit is contained in:
Tomas Vondra
2022-01-15 02:15:23 +01:00
parent 3433a1fc76
commit ff0e7c7e84
5 changed files with 130 additions and 0 deletions

View File

@@ -23,6 +23,7 @@
#include "optimizer/var.h"
#include "nodes/nodes.h"
#include "nodes/relation.h"
#include "parser/parsetree.h"
#include "statistics/extended_stats_internal.h"
#include "statistics/statistics.h"
#include "utils/bytea.h"
@@ -961,10 +962,18 @@ dependencies_clauselist_selectivity(PlannerInfo *root,
MVDependencies *dependencies;
AttrNumber *list_attnums;
int listidx;
RangeTblEntry *rte = planner_rt_fetch(rel->relid, root);
/* initialize output argument */
*estimatedclauses = NULL;
/*
* When dealing with inheritance trees, ignore extended stats (which were
* built without data from child rels, and thus do not represent them).
*/
if (rte->inh)
return 1.0;
/* check if there's any stats that might be useful for us. */
if (!has_stats_of_kind(rel->statlist, STATS_EXT_DEPENDENCIES))
return 1.0;

View File

@@ -23,6 +23,7 @@
#include "catalog/pg_collation.h"
#include "catalog/pg_statistic_ext.h"
#include "nodes/relation.h"
#include "parser/parsetree.h"
#include "postmaster/autovacuum.h"
#include "statistics/extended_stats_internal.h"
#include "statistics/statistics.h"

View File

@@ -3749,6 +3749,14 @@ estimate_multivariate_ndistinct(PlannerInfo *root, RelOptInfo *rel,
Oid statOid = InvalidOid;
MVNDistinct *stats;
Bitmapset *matched = NULL;
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).
*/
if (rte->inh)
return false;
/* bail out immediately if the table has no extended statistics */
if (!rel->statlist)