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

Commit 1f1865e

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 8b0a5cf commit 1f1865e

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

src/backend/utils/adt/selfuncs.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3912,7 +3912,7 @@ estimate_multivariate_ndistinct(PlannerInfo *root, RelOptInfo *rel,
39123912
Oid statOid = InvalidOid;
39133913
MVNDistinct *stats;
39143914
StatisticExtInfo *matched_info = NULL;
3915-
RangeTblEntry *rte;
3915+
RangeTblEntry *rte = planner_rt_fetch(rel->relid, root);
39163916

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

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

40044008
Assert(nmatches_vars + nmatches_exprs > 1);
40054009

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

40094012
/*
@@ -5240,6 +5243,10 @@ examine_variable(PlannerInfo *root, Node *node, int varRelid,
52405243
if (info->kind != STATS_EXT_EXPRESSIONS)
52415244
continue;
52425245

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

src/test/regress/expected/stats_ext.out

Lines changed: 7 additions & 1 deletion
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

Lines changed: 2 additions & 1 deletion
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)