Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit f485708

Browse files
committed
Fix planner failure with extended statistics on partitioned tables.
Some cases would result in "cache lookup failed for statistics object", due to trying to fetch inherited statistics when only non-inherited ones are available or vice versa. Richard Guo and Justin Pryzby Discussion: https://postgr.es/m/20221030170520.GM16921@telsasoft.com
1 parent 495e73c commit f485708

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

src/backend/utils/adt/selfuncs.c

+9-2
Original file line numberDiff line numberDiff line change
@@ -3913,7 +3913,7 @@ estimate_multivariate_ndistinct(PlannerInfo *root, RelOptInfo *rel,
39133913
Oid statOid = InvalidOid;
39143914
MVNDistinct *stats;
39153915
StatisticExtInfo *matched_info = NULL;
3916-
RangeTblEntry *rte;
3916+
RangeTblEntry *rte = planner_rt_fetch(rel->relid, root);
39173917

39183918
/* bail out immediately if the table has no extended statistics */
39193919
if (!rel->statlist)
@@ -3933,6 +3933,10 @@ estimate_multivariate_ndistinct(PlannerInfo *root, RelOptInfo *rel,
39333933
if (info->kind != STATS_EXT_NDISTINCT)
39343934
continue;
39353935

3936+
/* skip statistics with mismatching stxdinherit value */
3937+
if (info->inherit != rte->inh)
3938+
continue;
3939+
39363940
/*
39373941
* Determine how many expressions (and variables in non-matched
39383942
* expressions) match. We'll then use these numbers to pick the
@@ -4004,7 +4008,6 @@ estimate_multivariate_ndistinct(PlannerInfo *root, RelOptInfo *rel,
40044008

40054009
Assert(nmatches_vars + nmatches_exprs > 1);
40064010

4007-
rte = planner_rt_fetch(rel->relid, root);
40084011
stats = statext_ndistinct_load(statOid, rte->inh);
40094012

40104013
/*
@@ -5241,6 +5244,10 @@ examine_variable(PlannerInfo *root, Node *node, int varRelid,
52415244
if (info->kind != STATS_EXT_EXPRESSIONS)
52425245
continue;
52435246

5247+
/* skip stats with mismatching stxdinherit value */
5248+
if (info->inherit != rte->inh)
5249+
continue;
5250+
52445251
pos = 0;
52455252
foreach(expr_item, info->exprs)
52465253
{

src/test/regress/expected/stats_ext.out

+7-1
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ DROP TABLE stxdinh, stxdinh1, stxdinh2;
232232
CREATE TABLE stxdinp(i int, a int, b int) PARTITION BY RANGE (i);
233233
CREATE TABLE stxdinp1 PARTITION OF stxdinp FOR VALUES FROM (1) TO (100);
234234
INSERT INTO stxdinp SELECT 1, a/100, a/100 FROM generate_series(1, 999) a;
235-
CREATE STATISTICS stxdinp ON a, b FROM stxdinp;
235+
CREATE STATISTICS stxdinp ON (a + 1), a, b FROM stxdinp;
236236
VACUUM ANALYZE stxdinp; -- partitions are processed recursively
237237
SELECT 1 FROM pg_statistic_ext WHERE stxrelid = 'stxdinp'::regclass;
238238
?column?
@@ -246,6 +246,12 @@ SELECT * FROM check_estimated_rows('SELECT a, b FROM stxdinp GROUP BY 1, 2');
246246
10 | 10
247247
(1 row)
248248

249+
SELECT * FROM check_estimated_rows('SELECT a + 1, b FROM ONLY stxdinp GROUP BY 1, 2');
250+
estimated | actual
251+
-----------+--------
252+
1 | 0
253+
(1 row)
254+
249255
DROP TABLE stxdinp;
250256
-- basic test for statistics on expressions
251257
CREATE TABLE ab1 (a INTEGER, b INTEGER, c TIMESTAMP, d TIMESTAMPTZ);

src/test/regress/sql/stats_ext.sql

+2-1
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,11 @@ DROP TABLE stxdinh, stxdinh1, stxdinh2;
139139
CREATE TABLE stxdinp(i int, a int, b int) PARTITION BY RANGE (i);
140140
CREATE TABLE stxdinp1 PARTITION OF stxdinp FOR VALUES FROM (1) TO (100);
141141
INSERT INTO stxdinp SELECT 1, a/100, a/100 FROM generate_series(1, 999) a;
142-
CREATE STATISTICS stxdinp ON a, b FROM stxdinp;
142+
CREATE STATISTICS stxdinp ON (a + 1), a, b FROM stxdinp;
143143
VACUUM ANALYZE stxdinp; -- partitions are processed recursively
144144
SELECT 1 FROM pg_statistic_ext WHERE stxrelid = 'stxdinp'::regclass;
145145
SELECT * FROM check_estimated_rows('SELECT a, b FROM stxdinp GROUP BY 1, 2');
146+
SELECT * FROM check_estimated_rows('SELECT a + 1, b FROM ONLY stxdinp GROUP BY 1, 2');
146147
DROP TABLE stxdinp;
147148

148149
-- basic test for statistics on expressions

0 commit comments

Comments
 (0)