postgres.git / summary / log / commit / refs

commit    6ffd2f91b7400780f146d349dfa31ef3e603c5bb
Author:   Michael Paquier <michael@paquier.xyz>
Date:     Fri Oct 30 08:29:58 2026 +0000
First released in: 14.25, 15.20, 16.16, 17.12, 19.0

    Fix more issues with extended statistics on expressions
    
    When evaluating expressions (through make_build_data()) and building
    stats data for expressions (through build_expr_data()), both routines
    forgot that examine_expression() could return a NULL result, depending
    on the data generated by the typanalyze function assigned to a type.
    This would crash the server.
    
    This commit includes some tests added to test_custom_types that are able
    to reproduce all these issues with two fancy typanalyze callbacks:
    - False result with a valid stats target, for the expression evaluation.
    - False result with a valid stats target on second call, for the stats
    data build.
    
    No typanalyze callback in core have the idea to do that, and I doubt
    that anything out there has the idea to do that.  If they do, this
    switches the expression stats to fail gracefully instead of crashing,
    based on the combinations that are assumed as supported by the core
    code.
    
    This is a continuation of 3b7a6fa15720.
    
    Reported-by: Yuelin Wang <1217816127@qq.com>
    Author: Shihao Zhong <zhong950419@gmail.com>
    Author: Michael Paquier <michael@paquier.xyz>
    Discussion: https://postgr.es/m/19763-14da3f96c02f07e8@postgresql.org
    Backpatch-through: 14


src/backend/statistics/extended_stats.c | 48 ++++++++++++++++++---- src/test/modules/test_custom_types/README | 4 ++ .../expected/test_custom_types.out | 36 ++++++++++++++++ .../test_custom_types/sql/test_custom_types.sql | 28 +++++++++++++ .../test_custom_types/test_custom_types--1.0.sql | 16 ++++++++ .../modules/test_custom_types/test_custom_types.c | 37 +++++++++++++++++ 6 files changed, 161 insertions(+), 8 deletions(-) diff --git a/src/backend/statistics/extended_stats.c b/src/backend/statistics/extended_stats.c index 95812238598..4e348bc9192 100644 --- a/src/backend/statistics/extended_stats.c +++ b/src/backend/statistics/extended_stats.c @@ -102,6 +102,18 @@ static StatsBuildData *make_build_data(Relation onerel, StatExtEntry *stat, int numrows, HeapTuple *rows, VacAttrStats **stats, int stattarget); +static inline void +report_ext_stats_not_computed(Relation onerel, StatExtEntry *stat) +{ + if (!IsAutoVacuumWorkerProcess()) + ereport(WARNING, + (errcode(ERRCODE_INVALID_OBJECT_DEFINITION), + errmsg("statistics object \"%s.%s\" could not be computed for relation \"%s.%s\"", + stat->schema, stat->name, + get_namespace_name(onerel->rd_rel->relnamespace), + RelationGetRelationName(onerel)), + errtable(onerel))); +} /* * Compute requested extended stats, using the rows sampled for the plain @@ -172,14 +184,7 @@ BuildRelationExtStatistics(Relation onerel, double totalrows, natts, vacattrstats); if (!stats) { - if (!IsAutoVacuumWorkerProcess()) - ereport(WARNING, - (errcode(ERRCODE_INVALID_OBJECT_DEFINITION), - errmsg("statistics object \"%s.%s\" could not be computed for relation \"%s.%s\"", - stat->schema, stat->name, - get_namespace_name(onerel->rd_rel->relnamespace), - RelationGetRelationName(onerel)), - errtable(onerel))); + report_ext_stats_not_computed(onerel, stat); continue; } @@ -198,6 +203,11 @@ BuildRelationExtStatistics(Relation onerel, double totalrows, /* evaluate expressions (if the statistics object has any) */ data = make_build_data(onerel, stat, numrows, rows, stats, stattarget); + if (!data) + { + report_ext_stats_not_computed(onerel, stat); + continue; + } /* compute statistic of each requested type */ foreach(lc2, stat->types) @@ -220,6 +230,12 @@ BuildRelationExtStatistics(Relation onerel, double totalrows, elog(ERROR, "requested expression stats, but there are no expressions"); exprdata = build_expr_data(stat->exprs, stattarget); + if (!exprdata) + { + report_ext_stats_not_computed(onerel, stat); + data = NULL; + break; + } nexprs = list_length(stat->exprs); compute_expr_stats(onerel, totalrows, @@ -230,6 +246,9 @@ BuildRelationExtStatistics(Relation onerel, double totalrows, } } + if (!data) + continue; + /* store the statistics in the catalog */ statext_store(stat->statOid, ndistinct, dependencies, mcv, exprstats, stats); @@ -2310,6 +2329,11 @@ build_expr_data(List *exprs, int stattarget) thisdata->expr = expr; thisdata->vacattrstat = examine_expression(expr, stattarget); + if (thisdata->vacattrstat == NULL) + { + pfree(exprdata); + return NULL; + } idx++; } @@ -2505,6 +2529,8 @@ statext_expressions_load(Oid stxoid, int idx) * Evaluate the expressions, so that we can use the results to build * all the requested statistics types. This matters especially for * expensive expressions, of course. + * + * Returns NULL if an expression cannot be analyzed. */ static StatsBuildData * make_build_data(Relation rel, StatExtEntry *stat, int numrows, HeapTuple *rows, @@ -2594,6 +2620,12 @@ make_build_data(Relation rel, StatExtEntry *stat, int numrows, HeapTuple *rows, result->attnums[idx] = k; result->stats[idx] = examine_expression(expr, stattarget); + if (result->stats[idx] == NULL) + { + pfree(result); + return NULL; + } + idx++; k--; } diff --git a/src/test/modules/test_custom_types/README b/src/test/modules/test_custom_types/README index a37d2db577e..d82dcd45a87 100644 --- a/src/test/modules/test_custom_types/README +++ b/src/test/modules/test_custom_types/README @@ -7,3 +7,7 @@ patterns: - typanalyze function registered to a custom type, returning false. - typanalyze function registered to a custom type, registering invalid stats data. +- typanalyze function registered to a custom type, returning false when + given a stats target. +- typanalyze function registered to a custom type, returning false on its + second call with a stats target. diff --git a/src/test/modules/test_custom_types/expected/test_custom_types.out b/src/test/modules/test_custom_types/expected/test_custom_types.out index 2fb6b0b10ee..9633489ea59 100644 --- a/src/test/modules/test_custom_types/expected/test_custom_types.out +++ b/src/test/modules/test_custom_types/expected/test_custom_types.out @@ -167,6 +167,42 @@ SELECT COUNT(*) FROM test_table GROUP BY (func_int_custom(data)); 1 (3 rows) +DROP STATISTICS test_stats; +-- Switch type to use typanalyze function that returns false with stats +-- target. +ALTER TYPE int_custom SET (ANALYZE = int_custom_typanalyze_false_target); +-- Expression rejected at evaluation. +CREATE STATISTICS test_stats ON func_int_custom(data), (id) FROM test_table; +-- Computation of the stats fails, no data generated. +ANALYZE test_table; +WARNING: statistics object "public.test_stats" could not be computed for relation "public.test_table" +SELECT stxname, stxdexpr IS NULL as expr_stats_is_null + FROM pg_statistic_ext s + LEFT JOIN pg_statistic_ext_data d ON s.oid = d.stxoid + WHERE stxname = 'test_stats'; + stxname | expr_stats_is_null +------------+-------------------- + test_stats | t +(1 row) + +DROP STATISTICS test_stats; +-- Switch type to use typanalyze function that returns false on second call +-- with a stats target. +ALTER TYPE int_custom SET (ANALYZE = int_custom_typanalyze_false_second); +-- Expression rejected at build. +CREATE STATISTICS test_stats ON func_int_custom(data), (id) FROM test_table; +-- Computation of the stats fails, no data generated. +ANALYZE test_table; +WARNING: statistics object "public.test_stats" could not be computed for relation "public.test_table" +SELECT stxname, stxdexpr IS NULL as expr_stats_is_null + FROM pg_statistic_ext s + LEFT JOIN pg_statistic_ext_data d ON s.oid = d.stxoid + WHERE stxname = 'test_stats'; + stxname | expr_stats_is_null +------------+-------------------- + test_stats | t +(1 row) + DROP STATISTICS test_stats; -- Cleanup DROP FUNCTION func_int_custom; diff --git a/src/test/modules/test_custom_types/sql/test_custom_types.sql b/src/test/modules/test_custom_types/sql/test_custom_types.sql index 206a17c667f..d72eca44b22 100644 --- a/src/test/modules/test_custom_types/sql/test_custom_types.sql +++ b/src/test/modules/test_custom_types/sql/test_custom_types.sql @@ -98,6 +98,34 @@ SELECT tablename, SELECT COUNT(*) FROM test_table GROUP BY (func_int_custom(data)); DROP STATISTICS test_stats; +-- Switch type to use typanalyze function that returns false with stats +-- target. +ALTER TYPE int_custom SET (ANALYZE = int_custom_typanalyze_false_target); + +-- Expression rejected at evaluation. +CREATE STATISTICS test_stats ON func_int_custom(data), (id) FROM test_table; +-- Computation of the stats fails, no data generated. +ANALYZE test_table; +SELECT stxname, stxdexpr IS NULL as expr_stats_is_null + FROM pg_statistic_ext s + LEFT JOIN pg_statistic_ext_data d ON s.oid = d.stxoid + WHERE stxname = 'test_stats'; +DROP STATISTICS test_stats; + +-- Switch type to use typanalyze function that returns false on second call +-- with a stats target. +ALTER TYPE int_custom SET (ANALYZE = int_custom_typanalyze_false_second); + +-- Expression rejected at build. +CREATE STATISTICS test_stats ON func_int_custom(data), (id) FROM test_table; +-- Computation of the stats fails, no data generated. +ANALYZE test_table; +SELECT stxname, stxdexpr IS NULL as expr_stats_is_null + FROM pg_statistic_ext s + LEFT JOIN pg_statistic_ext_data d ON s.oid = d.stxoid + WHERE stxname = 'test_stats'; +DROP STATISTICS test_stats; + -- Cleanup DROP FUNCTION func_int_custom; DROP TABLE test_table; diff --git a/src/test/modules/test_custom_types/test_custom_types--1.0.sql b/src/test/modules/test_custom_types/test_custom_types--1.0.sql index ce0e905d636..b78ee025cd3 100644 --- a/src/test/modules/test_custom_types/test_custom_types--1.0.sql +++ b/src/test/modules/test_custom_types/test_custom_types--1.0.sql @@ -32,6 +32,22 @@ RETURNS boolean AS 'MODULE_PATHNAME' LANGUAGE C STRICT; +-- +-- Typanalyze function that fails for a non-negative attstattarget +-- +CREATE FUNCTION int_custom_typanalyze_false_target(internal) +RETURNS boolean +AS 'MODULE_PATHNAME' +LANGUAGE C STRICT; + +-- +-- Typanalyze function that fails on its second non-negative attstattarget +-- +CREATE FUNCTION int_custom_typanalyze_false_second(internal) +RETURNS boolean +AS 'MODULE_PATHNAME' +LANGUAGE C STRICT; + -- -- The int_custom type definition -- diff --git a/src/test/modules/test_custom_types/test_custom_types.c b/src/test/modules/test_custom_types/test_custom_types.c index 67e25a80b56..7c775c6f619 100644 --- a/src/test/modules/test_custom_types/test_custom_types.c +++ b/src/test/modules/test_custom_types/test_custom_types.c @@ -28,6 +28,8 @@ PG_FUNCTION_INFO_V1(int_custom_in); PG_FUNCTION_INFO_V1(int_custom_out); PG_FUNCTION_INFO_V1(int_custom_typanalyze_false); PG_FUNCTION_INFO_V1(int_custom_typanalyze_invalid); +PG_FUNCTION_INFO_V1(int_custom_typanalyze_false_target); +PG_FUNCTION_INFO_V1(int_custom_typanalyze_false_second); PG_FUNCTION_INFO_V1(int_custom_eq); PG_FUNCTION_INFO_V1(int_custom_ne); PG_FUNCTION_INFO_V1(int_custom_lt); @@ -112,6 +114,41 @@ int_custom_typanalyze_invalid(PG_FUNCTION_ARGS) PG_RETURN_BOOL(true); } +/* + * int_custom_typanalyze_false_target + * + * Returns false with a stats target. + */ +Datum +int_custom_typanalyze_false_target(PG_FUNCTION_ARGS) +{ + VacAttrStats *stats = (VacAttrStats *) PG_GETARG_POINTER(0); + + if (stats->attr->attstattarget >= 0) + PG_RETURN_BOOL(false); + + PG_RETURN_BOOL(std_typanalyze(stats)); +} + +/* + * int_custom_typanalyze_false_second + * + * Returns false on the second call with a stats target. + */ +Datum +int_custom_typanalyze_false_second(PG_FUNCTION_ARGS) +{ + static int ncalls = 0; + VacAttrStats *stats = (VacAttrStats *) PG_GETARG_POINTER(0); + + if (stats->attr->attstattarget < 0) + ncalls = 0; + else if (++ncalls > 1) + PG_RETURN_BOOL(false); + + PG_RETURN_BOOL(std_typanalyze(stats)); +} + /* * Comparison functions for int_custom type */ [parent: 9e3077ff473e]