postgres.git / summary / log / commit / refs
commit 13eb56409f5b6ed15e3ab5fa2b1fa98f66932c10
Author: Michael Paquier <michael@paquier.xyz>
Date: Fri Nov 13 02:37:06 2026 +0000
First released in: 19.0
Lift range_bounds_histogram requirement in pg_restore_extended_stats()
pg_restore_extended_stats() required range_bounds_histogram,
range_length_histogram and range_empty_frac to be either all set or
all missing for an expression. This is an incorrect assumption, due to:
* range_length_histogram and range_empty_frac sharing the
STATISTIC_KIND_RANGE_LENGTH_HISTOGRAM slot.
* range_bounds_histogram having its own STATISTIC_KIND_BOUNDS_HISTOGRAM
slot.
Only the first two need to be given together. Note that
pg_restore_attribute_stats() already works this way.
ANALYZE generates a bounds histogram only when there are at least two
non-empty range values, and it always stores the length histogram
slot with the fraction of empty ranges. An empty array of values for
the histogram is used if there are fewer than two non-empty values.
Hence, the statistics of an expression with fewer than two non-empty
range values could never be restored, as the bounds histogram was
missing.
This is a pretty narrow case, unlikely going to be hit in practice. Its
consequence was a failure when restoring the statistics of the extstats
object being processed, in the shape of a warning and missing stats
after an upgrade or the restore of a dump.
Issue noticed by Corey, while digging into a separate issue.
Author: Corey Huinker <corey.huinker@gmail.com>
Discussion: https://postgr.es/m/19842-ca042326dd2430a4@postgresql.org
Backpatch-through: 19
src/backend/statistics/extended_stats_funcs.c | 20 ++++++-----------
src/test/regress/expected/stats_import.out | 31 +++++++++++----------------
src/test/regress/sql/stats_import.sql | 19 ++++++++--------
3 files changed, 29 insertions(+), 41 deletions(-)
diff --git a/src/backend/statistics/extended_stats_funcs.c b/src/backend/statistics/extended_stats_funcs.c
index e6fe3ecd00a..73251a1b70c 100644
--- a/src/backend/statistics/extended_stats_funcs.c
+++ b/src/backend/statistics/extended_stats_funcs.c
@@ -1196,10 +1196,10 @@ import_pg_statistic(Relation pgsd, JsonbContainer *cont,
goto pg_statistic_error;
/*
- * There are two arg pairs, MCV+MCF and MCEV+MCEF. Both values must
- * either be found or not be found. Any disagreement is a warning. Once
- * we have ruled out disagreeing pairs, we can use either found flag as a
- * proxy for the other.
+ * There are three arg pairs, MCV+MCF, MCEV+MCEF, and RLH+REF. Both
+ * values must either be found or not be found. Any disagreement is a
+ * warning. Once we have ruled out disagreeing pairs, we can use either
+ * found flag as a proxy for the other.
*/
if (found[MOST_COMMON_VALS_ELEM] != found[MOST_COMMON_FREQS_ELEM])
{
@@ -1224,21 +1224,15 @@ import_pg_statistic(Relation pgsd, JsonbContainer *cont,
goto pg_statistic_error;
}
- /*
- * Range types may expect three values to be set. All three of them must
- * either be found or not be found. Any disagreement is a warning.
- */
- if (found[RANGE_LENGTH_HISTOGRAM_ELEM] != found[RANGE_EMPTY_FRAC_ELEM] ||
- found[RANGE_LENGTH_HISTOGRAM_ELEM] != found[RANGE_BOUNDS_HISTOGRAM_ELEM])
+ if (found[RANGE_LENGTH_HISTOGRAM_ELEM] != found[RANGE_EMPTY_FRAC_ELEM])
{
ereport(WARNING,
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("could not parse \"%s\": invalid element in expression %d",
argname, exprnum),
- errhint("\"%s\", \"%s\", and \"%s\" must be all either strings or all nulls.",
+ errhint("\"%s\" and \"%s\" must be both either strings or nulls.",
extexprargname[RANGE_LENGTH_HISTOGRAM_ELEM],
- extexprargname[RANGE_EMPTY_FRAC_ELEM],
- extexprargname[RANGE_BOUNDS_HISTOGRAM_ELEM]));
+ extexprargname[RANGE_EMPTY_FRAC_ELEM]));
goto pg_statistic_error;
}
diff --git a/src/test/regress/expected/stats_import.out b/src/test/regress/expected/stats_import.out
index e46310fefe5..0921a4ba5c7 100644
--- a/src/test/regress/expected/stats_import.out
+++ b/src/test/regress/expected/stats_import.out
@@ -2783,8 +2783,9 @@ CREATE TABLE stats_import.test_mr_clone ( LIKE stats_import.test_mr )
CREATE STATISTICS stats_import.test_mr_stat_clone
ON name, mrange, ( mrange + '{[10000,10200)}'::int4multirange)
FROM stats_import.test_mr_clone;
--- Check for invalid value combinations for range types.
--- Only range_bounds_histogram (other two missing)
+-- Check value combinations for range types. range_length_histogram and
+-- range_empty_frac must be given together.
+-- ok: only range_bounds_histogram, other two missing
SELECT pg_catalog.pg_restore_extended_stats(
'schemaname', 'stats_import',
'relname', 'test_mr',
@@ -2792,15 +2793,13 @@ SELECT pg_catalog.pg_restore_extended_stats(
'statistics_name', 'test_mr_stat',
'inherited', false,
'exprs', '[{"range_bounds_histogram": "{\"[1,10200)\",\"[11,10200)\"}"}]'::jsonb);
-WARNING: could not parse "exprs": invalid element in expression -1
-HINT: "range_length_histogram", "range_empty_frac", and "range_bounds_histogram" must be all either strings or all nulls.
pg_restore_extended_stats
---------------------------
- f
+ t
(1 row)
--- Only range_length_histogram and range_empty_frac
--- (range_bounds_histogram missing)
+-- ok: range_length_histogram and range_empty_frac defined,
+-- range_bounds_histogram missing.
SELECT pg_catalog.pg_restore_extended_stats(
'schemaname', 'stats_import',
'relname', 'test_mr',
@@ -2808,40 +2807,36 @@ SELECT pg_catalog.pg_restore_extended_stats(
'statistics_name', 'test_mr_stat',
'inherited', false,
'exprs', '[{"range_length_histogram": "{10179,10189}", "range_empty_frac": "0"}]'::jsonb);
-WARNING: could not parse "exprs": invalid element in expression -1
-HINT: "range_length_histogram", "range_empty_frac", and "range_bounds_histogram" must be all either strings or all nulls.
pg_restore_extended_stats
---------------------------
- f
+ t
(1 row)
--- Only range_bounds_histogram and range_empty_frac
--- (range_length_histogram missing)
+-- warn: only range_empty_frac, range_length_histogram missing
SELECT pg_catalog.pg_restore_extended_stats(
'schemaname', 'stats_import',
'relname', 'test_mr',
'statistics_schemaname', 'stats_import',
'statistics_name', 'test_mr_stat',
'inherited', false,
- 'exprs', '[{"range_bounds_histogram": "{\"[1,10200)\"}", "range_empty_frac": "0"}]'::jsonb);
+ 'exprs', '[{"range_empty_frac": "0"}]'::jsonb);
WARNING: could not parse "exprs": invalid element in expression -1
-HINT: "range_length_histogram", "range_empty_frac", and "range_bounds_histogram" must be all either strings or all nulls.
+HINT: "range_length_histogram" and "range_empty_frac" must be both either strings or nulls.
pg_restore_extended_stats
---------------------------
f
(1 row)
--- Only range_bounds_histogram and range_length_histogram
--- (range_empty_frac missing)
+-- warn: only range_length_histogram, range_empty_frac missing
SELECT pg_catalog.pg_restore_extended_stats(
'schemaname', 'stats_import',
'relname', 'test_mr',
'statistics_schemaname', 'stats_import',
'statistics_name', 'test_mr_stat',
'inherited', false,
- 'exprs', '[{"range_bounds_histogram": "{\"[1,10200)\"}", "range_length_histogram": "{10179}"}]'::jsonb);
+ 'exprs', '[{"range_length_histogram": "{10179}"}]'::jsonb);
WARNING: could not parse "exprs": invalid element in expression -1
-HINT: "range_length_histogram", "range_empty_frac", and "range_bounds_histogram" must be all either strings or all nulls.
+HINT: "range_length_histogram" and "range_empty_frac" must be both either strings or nulls.
pg_restore_extended_stats
---------------------------
f
diff --git a/src/test/regress/sql/stats_import.sql b/src/test/regress/sql/stats_import.sql
index 22690503ede..91ea7f31321 100644
--- a/src/test/regress/sql/stats_import.sql
+++ b/src/test/regress/sql/stats_import.sql
@@ -2013,8 +2013,9 @@ CREATE STATISTICS stats_import.test_mr_stat_clone
ON name, mrange, ( mrange + '{[10000,10200)}'::int4multirange)
FROM stats_import.test_mr_clone;
--- Check for invalid value combinations for range types.
--- Only range_bounds_histogram (other two missing)
+-- Check value combinations for range types. range_length_histogram and
+-- range_empty_frac must be given together.
+-- ok: only range_bounds_histogram, other two missing
SELECT pg_catalog.pg_restore_extended_stats(
'schemaname', 'stats_import',
'relname', 'test_mr',
@@ -2022,8 +2023,8 @@ SELECT pg_catalog.pg_restore_extended_stats(
'statistics_name', 'test_mr_stat',
'inherited', false,
'exprs', '[{"range_bounds_histogram": "{\"[1,10200)\",\"[11,10200)\"}"}]'::jsonb);
--- Only range_length_histogram and range_empty_frac
--- (range_bounds_histogram missing)
+-- ok: range_length_histogram and range_empty_frac defined,
+-- range_bounds_histogram missing.
SELECT pg_catalog.pg_restore_extended_stats(
'schemaname', 'stats_import',
'relname', 'test_mr',
@@ -2031,24 +2032,22 @@ SELECT pg_catalog.pg_restore_extended_stats(
'statistics_name', 'test_mr_stat',
'inherited', false,
'exprs', '[{"range_length_histogram": "{10179,10189}", "range_empty_frac": "0"}]'::jsonb);
--- Only range_bounds_histogram and range_empty_frac
--- (range_length_histogram missing)
+-- warn: only range_empty_frac, range_length_histogram missing
SELECT pg_catalog.pg_restore_extended_stats(
'schemaname', 'stats_import',
'relname', 'test_mr',
'statistics_schemaname', 'stats_import',
'statistics_name', 'test_mr_stat',
'inherited', false,
- 'exprs', '[{"range_bounds_histogram": "{\"[1,10200)\"}", "range_empty_frac": "0"}]'::jsonb);
--- Only range_bounds_histogram and range_length_histogram
--- (range_empty_frac missing)
+ 'exprs', '[{"range_empty_frac": "0"}]'::jsonb);
+-- warn: only range_length_histogram, range_empty_frac missing
SELECT pg_catalog.pg_restore_extended_stats(
'schemaname', 'stats_import',
'relname', 'test_mr',
'statistics_schemaname', 'stats_import',
'statistics_name', 'test_mr_stat',
'inherited', false,
- 'exprs', '[{"range_bounds_histogram": "{\"[1,10200)\"}", "range_length_histogram": "{10179}"}]'::jsonb);
+ 'exprs', '[{"range_length_histogram": "{10179}"}]'::jsonb);
-- warn: range bounds histogram with unsorted elements
SELECT pg_catalog.pg_restore_extended_stats(
'schemaname', 'stats_import',
[parent: 48938780c357]