pgsql: Keep the relation open while importing its statistics.

From: Noah Misch <noah(at)leadboat(dot)com>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Keep the relation open while importing its statistics.
Date: 2026-11-09 14:21:53
Message-ID: E1xQQFt-000000003RT-1Rt2@gemulon.postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-committers

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 <mehmet(at)mehmetince(dot)net>
Author: Masahiko Sawada <sawada(dot)mshk(at)gmail(dot)com>
Reviewed-by: Michael Paquier <michael(at)paquier(dot)xyz>
Reviewed-by: Andres Freund <andres(at)anarazel(dot)de>
Reviewed-by: Heikki Linnakangas <hlinnaka(at)iki(dot)fi>
Backpatch-through: 18
Security: CVE-2026-116329

Branch
------
REL_19_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/ee7c8f051bd433fb78f85d1c850ebb7b40004eb8
Author: Masahiko Sawada <msawada(at)postgresql(dot)org>

Modified Files
--------------
src/backend/statistics/attribute_stats.c | 26 ++++
src/backend/statistics/extended_stats_funcs.c | 25 ++++
src/test/regress/expected/stats_import.out | 175 +++++++++++++++++++++++++-
src/test/regress/sql/stats_import.sql | 156 +++++++++++++++++++++++
4 files changed, 381 insertions(+), 1 deletion(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Noah Misch 2026-11-09 14:21:54 pgsql: Fix use-after-free in pg_show_all_settings().
Previous Message Noah Misch 2026-11-09 14:21:52 pgsql: Avoid use-after-free hazards in re-entrant use of pltcl.