postgres.git / summary / log / commit / refs
commit 4b93eec05e63f2195eb2a673f706cfd1c4dc7a2c
Author: Michael Paquier <michael@paquier.xyz>
Date: Fri Oct 30 08:29:35 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 2b83355d26e..1f9a25a3af0 100644
--- a/src/backend/statistics/extended_stats.c
+++ b/src/backend/statistics/extended_stats.c
@@ -100,6 +100,18 @@ static StatsBuildData *make_build_data(Relation rel, StatExtEntry *stat,
int numrows, HeapTuple *rows,
VacAttrStats **stats, int stattarget);
+static inline void
+report_ext_stats_not_computed(Relation onerel, StatExtEntry *stat)
+{
+ if (!AmAutoVacuumWorkerProcess())
+ 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
@@ -170,14 +182,7 @@ BuildRelationExtStatistics(Relation onerel, bool inh, double totalrows,
natts, vacattrstats);
if (!stats)
{
- if (!AmAutoVacuumWorkerProcess())
- 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;
}
@@ -196,6 +201,11 @@ BuildRelationExtStatistics(Relation onerel, bool inh, 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)
@@ -218,6 +228,12 @@ BuildRelationExtStatistics(Relation onerel, bool inh, 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, exprdata, nexprs, rows, numrows);
@@ -226,6 +242,9 @@ BuildRelationExtStatistics(Relation onerel, bool inh, double totalrows,
}
}
+ if (!data)
+ continue;
+
/* store the statistics in the catalog */
statext_store(stat->statOid, inh,
ndistinct, dependencies, mcv, exprstats, stats);
@@ -2309,6 +2328,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++;
}
@@ -2502,6 +2526,8 @@ statext_expressions_load(Oid stxoid, bool inh, 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,
@@ -2591,6 +2617,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 59f0917c2d7..b1e6e08885d 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
@@ -173,6 +173,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, d.stxoid IS NULL AS no_data
+ FROM pg_statistic_ext s
+ LEFT JOIN pg_statistic_ext_data d ON s.oid = d.stxoid
+ WHERE stxname = 'test_stats';
+ stxname | no_data
+------------+---------
+ 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, d.stxoid IS NULL AS no_data
+ FROM pg_statistic_ext s
+ LEFT JOIN pg_statistic_ext_data d ON s.oid = d.stxoid
+ WHERE stxname = 'test_stats';
+ stxname | no_data
+------------+---------
+ 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 f1c75e4ba5b..94eb35d30a8 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
@@ -101,6 +101,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, d.stxoid IS NULL AS no_data
+ 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, d.stxoid IS NULL AS no_data
+ 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 41262044b47..d7f76e4acab 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);
@@ -110,6 +112,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->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->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: b0d482b33ffd]