Fix race condition adding a DEFAULT expression to a function

Because check_composite_type_dependency() didn't hold a lock while
inspecting the pg_proc entry, it was possible that the dependency was
deemed harmless, but concurrently, another backend added a DEFAULT
expression to the function that should've been rejected.  To fix, hold
an AccessShareLock on the pg_proc object while we inspect it, and on
the other side of the story, hold an AccessExclusiveLock on the
pg_proc object when modifying it.

Note that we don't lock functions or other objects except relations
when using them in queries, so the AccessExclusiveLock doesn't prevent
concurrent queries using the function.

This also adds locking to other objects (constraints, types) inspected
in check_composite_type_dependency(), although that is just pro forma,
because there is no DDL to replace the expression of an existing CHECK
constraint, and altering a rowtype already holds a lock that conflicts
with an ALTER DOMAIN SET DEFAULT expression on a domain over the same
rowtype.

Relations don't have this problem, because there is no DDL command to
alter an existing table's PARTITION BY expression, nor an existing
index's expressions or quals.  The OF type can be altered, but AFAICS
we have sufficient locking in place for that.

Reviewed-by: Andres Freund <[email protected]>
Security: CVE-2026-6460
Backpatch-through: 14

Branch
------
REL_15_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/b94b7254f31538bc84684723ae2cdf27e1877589
Author: Heikki Linnakangas <[email protected]>

Modified Files
--------------
src/backend/catalog/pg_proc.c                    | 13 +++++++++++++
src/backend/commands/tablecmds.c                 | 13 +++++++++++--
src/test/isolation/expected/type-ddl-locking.out | 12 ++++++++++++
src/test/isolation/specs/type-ddl-locking.spec   | 15 +++++++++++++++
4 files changed, 51 insertions(+), 2 deletions(-)

Reply via email to