postgres.git / summary / log / commit / refs
commit 87524d0dd8470819ce8aa3708a9bf3fa3f1d34fd
Author: Michael Paquier <michael@paquier.xyz>
Date: Fri Nov 13 04:25:34 2026 +0000
First released in: 19.0
Allow empty arrays of values in pg_restore_extended_stats()
ANALYZE can store an empty array of values for range_length_histogram
and most_common_elems in the statistics of an expression, which pg_dump
emits as '{}', since 261f89a976b. These were rejected by
pg_restore_extended_stats(), as an empty array has no dimensions, and
the statistics of the expression were lost after a dump/restore or an
upgrade *from* v19 or HEAD. Empty arrays are not possible for these two
properties in v18 and older versions.
This commit allows empty arrays for these two cases only, like
2656f664616d did for pg_restore_attribute_stats(). Arrays stored as
stanumbers are still required to be non-empty, as ANALYZE never
generates them. In terms of range_length_histogram, this commit
completes 13eb56409f5b, as ANALYZE never generates a bounds histogram
when the length histogram is empty.
Some tests are added to cover all the new patterns involving empty
arrays.
Reported-by: Chunling Qin <303677365@qq.com>
Author: Corey Huinker <corey.huinker@gmail.com>
Author: Michael Paquier <michael@paquier.xyz>
Discussion: https://postgr.es/m/19842-ca042326dd2430a4@postgresql.org
Backpatch-through: 19
src/backend/statistics/extended_stats_funcs.c | 42 +++++++++-----
src/test/regress/expected/stats_import.out | 82 +++++++++++++++++++++++++++
src/test/regress/sql/stats_import.sql | 54 ++++++++++++++++++
3 files changed, 163 insertions(+), 15 deletions(-)
diff --git a/src/backend/statistics/extended_stats_funcs.c b/src/backend/statistics/extended_stats_funcs.c
index 73251a1b70c..e8bdcf10a61 100644
--- a/src/backend/statistics/extended_stats_funcs.c
+++ b/src/backend/statistics/extended_stats_funcs.c
@@ -166,7 +166,7 @@ static bool key_in_expr_argnames(JsonbValue *key);
static bool check_all_expr_argnames_valid(JsonbContainer *cont, AttrNumber exprnum);
static Datum array_in_safe(FmgrInfo *array_in, const char *s, Oid typid,
int32 typmod, AttrNumber exprnum,
- const char *element_name, bool *ok);
+ const char *element_name, bool allow_empty, bool *ok);
static Datum import_pg_statistic(Relation pgsd, JsonbContainer *cont,
AttrNumber exprnum, FmgrInfo *array_in_fn,
Oid typid, int32 typmod, Oid typcoll,
@@ -1059,13 +1059,18 @@ jbv_to_infunc_datum(JsonbValue *jval, PGFunction func, AttrNumber exprnum,
*
* If an error is encountered, capture it, and reduce the elevel to WARNING.
*
+ * The array must be one-dimensional. If "allow_empty" is true, an empty
+ * array is also accepted.
+ *
* This is an adaptation of statatt_build_stavalues().
*/
static Datum
array_in_safe(FmgrInfo *array_in, const char *s, Oid typid, int32 typmod,
- AttrNumber exprnum, const char *element_name, bool *ok)
+ AttrNumber exprnum, const char *element_name, bool allow_empty,
+ bool *ok)
{
Datum result;
+ int ndim;
ErrorSaveContext escontext = {
.type = T_ErrorSaveContext,
@@ -1096,12 +1101,19 @@ array_in_safe(FmgrInfo *array_in, const char *s, Oid typid, int32 typmod,
return (Datum) 0;
}
- if (ARR_NDIM(DatumGetArrayTypeP(result)) != 1)
+ ndim = ARR_NDIM(DatumGetArrayTypeP(result));
+ if (ndim != 1 && !(allow_empty && ndim == 0))
{
- ereport(WARNING,
- (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
- errmsg("could not import element \"%s\" in expression %d: must be a one-dimensional array",
- element_name, exprnum)));
+ if (allow_empty)
+ ereport(WARNING,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+ errmsg("could not import element \"%s\" in expression %d: must be an empty or one-dimensional array",
+ element_name, exprnum)));
+ else
+ ereport(WARNING,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+ errmsg("could not import element \"%s\" in expression %d: must be a one-dimensional array",
+ element_name, exprnum)));
return (Datum) 0;
}
@@ -1354,14 +1366,14 @@ import_pg_statistic(Relation pgsd, JsonbContainer *cont,
s = jbv_string_get_cstr(&val[MOST_COMMON_VALS_ELEM]);
stavalues = array_in_safe(array_in_fn, s, typid, typmod, exprnum,
extexprargname[MOST_COMMON_VALS_ELEM],
- &val_ok);
+ false, &val_ok);
pfree(s);
s = jbv_string_get_cstr(&val[MOST_COMMON_FREQS_ELEM]);
stanumbers = array_in_safe(array_in_fn, s, FLOAT4OID, -1, exprnum,
extexprargname[MOST_COMMON_FREQS_ELEM],
- &num_ok);
+ false, &num_ok);
pfree(s);
/* Only set the slot if both datums have been built */
@@ -1400,7 +1412,7 @@ import_pg_statistic(Relation pgsd, JsonbContainer *cont,
stavalues = array_in_safe(array_in_fn, s, typid, typmod, exprnum,
extexprargname[HISTOGRAM_BOUNDS_ELEM],
- &val_ok);
+ false, &val_ok);
pfree(s);
if (val_ok)
@@ -1444,14 +1456,14 @@ import_pg_statistic(Relation pgsd, JsonbContainer *cont,
s = jbv_string_get_cstr(&val[MOST_COMMON_ELEMS_ELEM]);
stavalues = array_in_safe(array_in_fn, s, elemtypid, typmod, exprnum,
extexprargname[MOST_COMMON_ELEMS_ELEM],
- &val_ok);
+ true, &val_ok);
pfree(s);
s = jbv_string_get_cstr(&val[MOST_COMMON_ELEM_FREQS_ELEM]);
stanumbers = array_in_safe(array_in_fn, s, FLOAT4OID, -1, exprnum,
extexprargname[MOST_COMMON_ELEM_FREQS_ELEM],
- &num_ok);
+ false, &num_ok);
pfree(s);
/* Only set the slot if both datums have been built */
@@ -1474,7 +1486,7 @@ import_pg_statistic(Relation pgsd, JsonbContainer *cont,
s = jbv_string_get_cstr(&val[ELEM_COUNT_HISTOGRAM_ELEM]);
stanumbers = array_in_safe(array_in_fn, s, FLOAT4OID, -1, exprnum,
extexprargname[ELEM_COUNT_HISTOGRAM_ELEM],
- &num_ok);
+ false, &num_ok);
pfree(s);
if (num_ok)
@@ -1503,7 +1515,7 @@ import_pg_statistic(Relation pgsd, JsonbContainer *cont,
stavalues = array_in_safe(array_in_fn, s, rtypid, typmod, exprnum,
extexprargname[RANGE_BOUNDS_HISTOGRAM_ELEM],
- &val_ok);
+ false, &val_ok);
if (val_ok && statatt_check_bounds_histogram(stavalues))
statatt_set_slot(values, nulls, replaces,
@@ -1536,7 +1548,7 @@ import_pg_statistic(Relation pgsd, JsonbContainer *cont,
s = jbv_string_get_cstr(&val[RANGE_LENGTH_HISTOGRAM_ELEM]);
stavalues = array_in_safe(array_in_fn, s, FLOAT8OID, -1, exprnum,
extexprargname[RANGE_LENGTH_HISTOGRAM_ELEM],
- &val_ok);
+ true, &val_ok);
if (val_ok)
statatt_set_slot(values, nulls, replaces,
diff --git a/src/test/regress/expected/stats_import.out b/src/test/regress/expected/stats_import.out
index 0921a4ba5c7..004f35b8ad7 100644
--- a/src/test/regress/expected/stats_import.out
+++ b/src/test/regress/expected/stats_import.out
@@ -2812,6 +2812,20 @@ SELECT pg_catalog.pg_restore_extended_stats(
t
(1 row)
+-- ok: range_length_histogram defined as an empty array, range_bounds_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_length_histogram": "{}", "range_empty_frac": "0.5"}]'::jsonb);
+ pg_restore_extended_stats
+---------------------------
+ t
+(1 row)
+
-- warn: only range_empty_frac, range_length_histogram missing
SELECT pg_catalog.pg_restore_extended_stats(
'schemaname', 'stats_import',
@@ -2856,6 +2870,20 @@ WARNING: "range_bounds_histogram" must have its lower and upper bounds sorted i
f
(1 row)
+-- warn: range bounds histogram as an empty array
+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": "{}"}]'::jsonb);
+WARNING: could not import element "range_bounds_histogram" in expression -1: must be a one-dimensional array
+ pg_restore_extended_stats
+---------------------------
+ f
+(1 row)
+
-- warn: range bounds histogram with empty range
SELECT pg_catalog.pg_restore_extended_stats(
'schemaname', 'stats_import',
@@ -3264,6 +3292,23 @@ WARNING: could not import element "most_common_vals" in expression -1: must be
f
(1 row)
+-- warn: exprs most_common_vals as an empty array
+SELECT pg_catalog.pg_restore_extended_stats(
+ 'schemaname', 'stats_import',
+ 'relname', 'test_clone',
+ 'statistics_schemaname', 'stats_import',
+ 'statistics_name', 'test_stat_clone',
+ 'inherited', false,
+ 'exprs', '[
+ { "most_common_vals": "{}", "most_common_freqs": "{0.5}" },
+ { "most_common_vals": "{2}", "most_common_freqs": "{0.5}" }
+ ]'::jsonb);
+WARNING: could not import element "most_common_vals" in expression -1: must be a one-dimensional array
+ pg_restore_extended_stats
+---------------------------
+ f
+(1 row)
+
-- exprs most_common_vals element wrong type
SELECT pg_catalog.pg_restore_extended_stats(
'schemaname', 'stats_import',
@@ -3802,6 +3847,43 @@ HINT: Element "elem_count_histogram" in expression -1 could not be parsed.
f
(1 row)
+-- warn: exprs most_common_elem_freqs as an empty array
+SELECT pg_catalog.pg_restore_extended_stats(
+ 'schemaname', 'stats_import',
+ 'relname', 'test',
+ 'statistics_schemaname', 'stats_import',
+ 'statistics_name', 'test_stat_mcelem',
+ 'inherited', false,
+ 'exprs', '[
+ {
+ "most_common_elems": "{}",
+ "most_common_elem_freqs": "{}"
+ }
+ ]'::jsonb);
+WARNING: could not import element "most_common_elem_freqs" in expression -1: must be a one-dimensional array
+ pg_restore_extended_stats
+---------------------------
+ f
+(1 row)
+
+-- ok: exprs most_common_elems as an empty array
+SELECT pg_catalog.pg_restore_extended_stats(
+ 'schemaname', 'stats_import',
+ 'relname', 'test',
+ 'statistics_schemaname', 'stats_import',
+ 'statistics_name', 'test_stat_mcelem',
+ 'inherited', false,
+ 'exprs', '[
+ {
+ "most_common_elems": "{}",
+ "most_common_elem_freqs": "{0.25,0.25,0}"
+ }
+ ]'::jsonb);
+ pg_restore_extended_stats
+---------------------------
+ t
+(1 row)
+
-- ok: exprs mcelem
SELECT pg_catalog.pg_restore_extended_stats(
'schemaname', 'stats_import',
diff --git a/src/test/regress/sql/stats_import.sql b/src/test/regress/sql/stats_import.sql
index 91ea7f31321..fc50da45513 100644
--- a/src/test/regress/sql/stats_import.sql
+++ b/src/test/regress/sql/stats_import.sql
@@ -2032,6 +2032,15 @@ 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);
+-- ok: range_length_histogram defined as an empty array, range_bounds_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_length_histogram": "{}", "range_empty_frac": "0.5"}]'::jsonb);
-- warn: only range_empty_frac, range_length_histogram missing
SELECT pg_catalog.pg_restore_extended_stats(
'schemaname', 'stats_import',
@@ -2056,6 +2065,14 @@ SELECT pg_catalog.pg_restore_extended_stats(
'statistics_name', 'test_mr_stat',
'inherited', false,
'exprs', '[{"range_length_histogram": "{10179,10189,10199}", "range_empty_frac": "0", "range_bounds_histogram": "{\"[50,60)\",\"[1,2)\",\"[90,100)\"}"}]'::jsonb);
+-- warn: range bounds histogram as an empty array
+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": "{}"}]'::jsonb);
-- warn: range bounds histogram with empty range
SELECT pg_catalog.pg_restore_extended_stats(
'schemaname', 'stats_import',
@@ -2308,6 +2325,17 @@ SELECT pg_catalog.pg_restore_extended_stats(
{ "most_common_vals": "{{1,2},{3,4}}", "most_common_freqs": "{0.3,0.25,0.05,0.04}" },
{ "most_common_vals": "{2}", "most_common_freqs": "{0.5}" }
]'::jsonb);
+-- warn: exprs most_common_vals as an empty array
+SELECT pg_catalog.pg_restore_extended_stats(
+ 'schemaname', 'stats_import',
+ 'relname', 'test_clone',
+ 'statistics_schemaname', 'stats_import',
+ 'statistics_name', 'test_stat_clone',
+ 'inherited', false,
+ 'exprs', '[
+ { "most_common_vals": "{}", "most_common_freqs": "{0.5}" },
+ { "most_common_vals": "{2}", "most_common_freqs": "{0.5}" }
+ ]'::jsonb);
-- exprs most_common_vals element wrong type
SELECT pg_catalog.pg_restore_extended_stats(
'schemaname', 'stats_import',
@@ -2640,6 +2668,32 @@ SELECT pg_catalog.pg_restore_extended_stats(
"elem_count_histogram": "{BADELEMHIST,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1.5}"
}
]'::jsonb);
+-- warn: exprs most_common_elem_freqs as an empty array
+SELECT pg_catalog.pg_restore_extended_stats(
+ 'schemaname', 'stats_import',
+ 'relname', 'test',
+ 'statistics_schemaname', 'stats_import',
+ 'statistics_name', 'test_stat_mcelem',
+ 'inherited', false,
+ 'exprs', '[
+ {
+ "most_common_elems": "{}",
+ "most_common_elem_freqs": "{}"
+ }
+ ]'::jsonb);
+-- ok: exprs most_common_elems as an empty array
+SELECT pg_catalog.pg_restore_extended_stats(
+ 'schemaname', 'stats_import',
+ 'relname', 'test',
+ 'statistics_schemaname', 'stats_import',
+ 'statistics_name', 'test_stat_mcelem',
+ 'inherited', false,
+ 'exprs', '[
+ {
+ "most_common_elems": "{}",
+ "most_common_elem_freqs": "{0.25,0.25,0}"
+ }
+ ]'::jsonb);
-- ok: exprs mcelem
SELECT pg_catalog.pg_restore_extended_stats(
'schemaname', 'stats_import',
[parent: 13eb56409f5b]