pgsql: Keep the relation open while importing its statistics.

Поиск
Список
Период
Сортировка
Искать

pgsql: Keep the relation open while importing its statistics.

От:
Noah Misch <noah@leadboat.com>
Дата:
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 
Author: Masahiko Sawada 
Reviewed-by: Michael Paquier 
Reviewed-by: Andres Freund 
Reviewed-by: Heikki Linnakangas 
Backpatch-through: 18
Security: CVE-2026-116329

Branch
------
REL_19_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/ee7c8f051bd433fb78f85d1c850ebb7b40004eb8
Author: Masahiko Sawada 

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(-)

FAQ