Keep the relation open while importing its statistics. The attribute statistics import function locked the target relation but did not keep it open while converting its input values. Converting a value can run user-defined code, such as a domain's CHECK constraint, which can alter the relation in the same transaction. The lock alone does not prevent that. The import could then store statistics that no longer match the column, which can crash the backend when the planner later reads them.
Keep the relation open across the conversion, so that such a reentrant ALTER TABLE is rejected by CheckTableNotInUse() as it is elsewhere. Also, the same code can drop the target column itself, which doesn't go through that check. The import would then store a pg_statistic row for a column that no longer exists. That only leaves a stale catalog row behind, but it's wrong all the same, so re-check that the column still exists before writing, and error out if it doesn't. The column must not go away while we restore its statistics, and raising an error also rolls back the drop. The extended statistics import function, added in v19, has the same hazard and is fixed in the same way. There the same code can also drop the statistics object being imported, which would leave a pg_statistic_ext_data row with no owner. A statistics object created later with the same OID would pick that row up as its own, so we re-check that as well. Branches before v19 have no such function. Reported-by: Mehmet D. INCE <[email protected]> Author: Masahiko Sawada <[email protected]> Reviewed-by: Michael Paquier <[email protected]> Reviewed-by: Andres Freund <[email protected]> Reviewed-by: Heikki Linnakangas <[email protected]> Backpatch-through: 18 Security: CVE-2026-116329 Branch ------ REL_18_STABLE Details ------- https://git.postgresql.org/pg/commitdiff/780900dcfab7652f510ec60059cf8dbb51bfd7f5 Author: Masahiko Sawada <[email protected]> Modified Files -------------- src/backend/statistics/attribute_stats.c | 26 +++++++++ src/test/regress/expected/stats_import.out | 84 +++++++++++++++++++++++++++++- src/test/regress/sql/stats_import.sql | 73 ++++++++++++++++++++++++++ 3 files changed, 182 insertions(+), 1 deletion(-)
