From 961bb9aa5139b303a88b195a3994e0cf7b7d5e63 Mon Sep 17 00:00:00 2001 From: "Chao Li (Evan)" Date: Mon, 18 May 2026 10:03:24 +0800 Subject: [PATCH v1 2/2] Fix required expression count in stats import warning When importing expression statistics, the warning for an incorrect number of expression elements reported the number of elements found in the input as the required count. This made the message misleading, especially when the input contained too few elements. Report the expected number of expressions instead. Also extend the regression test to cover both too-few and too-many expression elements. Author: Chao Li --- src/backend/statistics/extended_stats_funcs.c | 2 +- src/test/regress/expected/stats_import.out | 18 ++++++++++++++++-- src/test/regress/sql/stats_import.sql | 10 +++++++++- 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/backend/statistics/extended_stats_funcs.c b/src/backend/statistics/extended_stats_funcs.c index 15f131724e9..8fc9c4c6f45 100644 --- a/src/backend/statistics/extended_stats_funcs.c +++ b/src/backend/statistics/extended_stats_funcs.c @@ -1593,7 +1593,7 @@ import_expressions(Relation pgsd, int numexprs, ereport(WARNING, errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("could not parse \"%s\": incorrect number of elements (%d required)", - argname, num_root_elements)); + argname, numexprs)); goto exprs_error; } diff --git a/src/test/regress/expected/stats_import.out b/src/test/regress/expected/stats_import.out index d73bc96039f..183e68d65c2 100644 --- a/src/test/regress/expected/stats_import.out +++ b/src/test/regress/expected/stats_import.out @@ -2455,7 +2455,7 @@ WARNING: could not parse "exprs": root-level array required f (1 row) --- wrong number of exprs +-- wrong number of exprs, too few SELECT pg_catalog.pg_restore_extended_stats( 'schemaname', 'stats_import', 'relname', 'test_clone', @@ -2463,7 +2463,21 @@ SELECT pg_catalog.pg_restore_extended_stats( 'statistics_name', 'test_stat_clone', 'inherited', false, 'exprs', '[ { "avg_width": "4" } ]'::jsonb); -WARNING: could not parse "exprs": incorrect number of elements (1 required) +WARNING: could not parse "exprs": incorrect number of elements (2 required) + pg_restore_extended_stats +--------------------------- + f +(1 row) + +-- wrong number of exprs, too many +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', '[ { "avg_width": "4" }, { "avg_width": "4" }, { "avg_width": "4" } ]'::jsonb); +WARNING: could not parse "exprs": incorrect number of elements (2 required) pg_restore_extended_stats --------------------------- f diff --git a/src/test/regress/sql/stats_import.sql b/src/test/regress/sql/stats_import.sql index b74f6395a5e..6064b7722da 100644 --- a/src/test/regress/sql/stats_import.sql +++ b/src/test/regress/sql/stats_import.sql @@ -1750,7 +1750,7 @@ SELECT pg_catalog.pg_restore_extended_stats( 'statistics_name', 'test_stat_clone', 'inherited', false, 'exprs', '{ "avg_width": "4", "null_frac": "0" }'::jsonb); --- wrong number of exprs +-- wrong number of exprs, too few SELECT pg_catalog.pg_restore_extended_stats( 'schemaname', 'stats_import', 'relname', 'test_clone', @@ -1758,6 +1758,14 @@ SELECT pg_catalog.pg_restore_extended_stats( 'statistics_name', 'test_stat_clone', 'inherited', false, 'exprs', '[ { "avg_width": "4" } ]'::jsonb); +-- wrong number of exprs, too many +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', '[ { "avg_width": "4" }, { "avg_width": "4" }, { "avg_width": "4" } ]'::jsonb); -- incorrect type of value: should be a string or a NULL. SELECT pg_catalog.pg_restore_extended_stats( 'schemaname', 'stats_import', -- 2.50.1 (Apple Git-155)