From: Noah Misch <noah@leadboat.com>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Keep the relation open while importing its statistics.
Date: Mon, 09 Nov 2026 14:21:53 +0000
Message-ID: <E1xQQFt-000000003RT-1Rt2@gemulon.postgresql.org> (raw)
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@mehmetince.net>
Author: Masahiko Sawada <sawada.mshk@gmail.com>
Reviewed-by: Michael Paquier <michael@paquier.xyz>
Reviewed-by: Andres Freund <andres@anarazel.de>
Reviewed-by: Heikki Linnakangas <hlinnaka@iki.fi>
Backpatch-through: 18
Security: CVE-2026-116329
Branch
------
REL_19_STABLE
Details
-------
https://git.postgresql.org/pg/commitdiff/ee7c8f051bd433fb78f85d1c850ebb7b40004eb8
Author: Masahiko Sawada <msawada@postgresql.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(-)
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-committers@postgresql.org
Cc: noah@leadboat.com
Subject: Re: pgsql: Keep the relation open while importing its statistics.
In-Reply-To: <E1xQQFt-000000003RT-1Rt2@gemulon.postgresql.org>
References: <E1xQQFt-000000003RT-1Rt2@gemulon.postgresql.org>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox