1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-30 11:03:19 +03:00

Add stxdinherit flag to pg_statistic_ext_data

Add pg_statistic_ext_data.stxdinherit flag, so that for each extended
statistics definition we can store two versions of data - one for the
relation alone, one for the whole inheritance tree. This is analogous to
pg_statistic.stainherit, but we failed to include such flag in catalogs
for extended statistics, and we had to work around it (see commits
859b3003de, 36c4bc6e72 and 20b9fa308e).

This changes the relationship between the two catalogs storing extended
statistics objects (pg_statistic_ext and pg_statistic_ext_data). Until
now, there was a simple 1:1 mapping - for each definition there was one
pg_statistic_ext_data row, and this row was inserted while creating the
statistics (and then updated during ANALYZE). With the stxdinherit flag,
we don't know how many rows there will be (child relations may be added
after the statistics object is defined), so there may be up to two rows.

We could make CREATE STATISTICS to always create both rows, but that
seems wasteful - without partitioning we only need stxdinherit=false
rows, and declaratively partitioned tables need only stxdinherit=true.
So we no longer initialize pg_statistic_ext_data in CREATE STATISTICS,
and instead make that a responsibility of ANALYZE. Which is what we do
for regular statistics too.

Patch by me, with extensive improvements and fixes by Justin Pryzby.

Author: Tomas Vondra, Justin Pryzby
Reviewed-by: Tomas Vondra, Justin Pryzby
Discussion: https://postgr.es/m/20210923212624.GI831%40telsasoft.com
This commit is contained in:
Tomas Vondra
2022-01-16 13:37:56 +01:00
parent e701bdd2f0
commit 269b532aef
19 changed files with 251 additions and 236 deletions

View File

@ -3919,17 +3919,6 @@ estimate_multivariate_ndistinct(PlannerInfo *root, RelOptInfo *rel,
if (!rel->statlist)
return false;
/*
* 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.
*/
rte = planner_rt_fetch(rel->relid, root);
if (rte->inh && rte->relkind != RELKIND_PARTITIONED_TABLE)
return false;
/* look for the ndistinct statistics object matching the most vars */
nmatches_vars = 0; /* we require at least two matches */
nmatches_exprs = 0;
@ -4015,7 +4004,8 @@ estimate_multivariate_ndistinct(PlannerInfo *root, RelOptInfo *rel,
Assert(nmatches_vars + nmatches_exprs > 1);
stats = statext_ndistinct_load(statOid);
rte = planner_rt_fetch(rel->relid, root);
stats = statext_ndistinct_load(statOid, rte->inh);
/*
* If we have a match, search it for the specific item that matches (there
@ -5245,17 +5235,6 @@ examine_variable(PlannerInfo *root, Node *node, int varRelid,
if (vardata->statsTuple)
break;
/*
* 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 && rte->relkind != RELKIND_PARTITIONED_TABLE)
break;
/* skip stats without per-expression stats */
if (info->kind != STATS_EXT_EXPRESSIONS)
continue;
@ -5274,22 +5253,16 @@ examine_variable(PlannerInfo *root, Node *node, int varRelid,
/* found a match, see if we can extract pg_statistic row */
if (equal(node, expr))
{
HeapTuple t = statext_expressions_load(info->statOid, pos);
/* Get statistics object's table for permission check */
RangeTblEntry *rte;
Oid userid;
vardata->statsTuple = t;
/*
* XXX Not sure if we should cache the tuple somewhere.
* Now we just create a new copy every time.
*/
vardata->freefunc = ReleaseDummy;
vardata->statsTuple =
statext_expressions_load(info->statOid, rte->inh, pos);
rte = planner_rt_fetch(onerel->relid, root);
Assert(rte->rtekind == RTE_RELATION);
vardata->freefunc = ReleaseDummy;
/*
* Use checkAsUser if it's set, in case we're accessing