diff --git a/src/backend/statistics/extended_stats_funcs.c b/src/backend/statistics/extended_stats_funcs.c index 70393d3a904..ab748068225 100644 --- a/src/backend/statistics/extended_stats_funcs.c +++ b/src/backend/statistics/extended_stats_funcs.c @@ -886,7 +886,8 @@ key_in_expr_argnames(JsonbValue *key) Assert(key->type == jbvString); for (int i = 0; i < NUM_ATTRIBUTE_STATS_ELEMS; i++) { - if (strncmp(extexprargname[i], key->val.string.val, key->val.string.len) == 0) + if (strlen(extexprargname[i]) == key->val.string.len && + strncmp(extexprargname[i], key->val.string.val, key->val.string.len) == 0) return true; } return false; @@ -1592,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; } @@ -1816,6 +1817,7 @@ pg_clear_extended_stats(PG_FUNCTION_ARGS) */ if (stxform->stxrelid != relid) { + heap_freetuple(tup); table_close(pg_stext, RowExclusiveLock); ereport(WARNING, errcode(ERRCODE_INVALID_PARAMETER_VALUE), diff --git a/src/test/regress/expected/stats_import.out b/src/test/regress/expected/stats_import.out index f421e83e232..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 @@ -3256,6 +3270,20 @@ most_common_elems | {-1,0,1,2,3} most_common_elem_freqs | {0.25,0.25,0.5,0.25,0.25,0.25,0.5,0.25} elem_count_histogram | {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,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} +-- bad: exprs param which is a prefix of a valid key name +SELECT pg_catalog.pg_restore_extended_stats( + 'schemaname', 'stats_import', + 'relname', 'test', + 'statistics_schemaname', 'stats_import', + 'statistics_name', 'test_stat_mcelem', + 'inherited', false, + 'exprs', '[{ "n": "-1" }]'::jsonb); +WARNING: could not import element in expression -1: invalid key name + pg_restore_extended_stats +--------------------------- + f +(1 row) + -- ok: tsvector exceptions, test just the collation exceptions CREATE STATISTICS stats_import.test_stat_tsvec ON (length(name)), (to_tsvector(name)) FROM stats_import.test; SELECT pg_catalog.pg_restore_extended_stats( diff --git a/src/test/regress/sql/stats_import.sql b/src/test/regress/sql/stats_import.sql index c1bf55690a6..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', @@ -2244,6 +2252,15 @@ WHERE e.statistics_schemaname = 'stats_import' AND e.inherited = false \gx +-- bad: exprs param which is a prefix of a valid key name +SELECT pg_catalog.pg_restore_extended_stats( + 'schemaname', 'stats_import', + 'relname', 'test', + 'statistics_schemaname', 'stats_import', + 'statistics_name', 'test_stat_mcelem', + 'inherited', false, + 'exprs', '[{ "n": "-1" }]'::jsonb); + -- ok: tsvector exceptions, test just the collation exceptions CREATE STATISTICS stats_import.test_stat_tsvec ON (length(name)), (to_tsvector(name)) FROM stats_import.test; SELECT pg_catalog.pg_restore_extended_stats(