postgresql/src/backend
Heikki Linnakangas c8cd3d6976 Avoid orphaned objects dependencies
Concurrent DDL can leave behind objects referencing other objects that
no longer exist. This can happen if an object is dropped, while a new
object that depends on it is created concurrently. For example:

session 1: BEGIN; CREATE FUNCTION myschema.myfunc() ...;
session 2: DROP SCHEMA myschema;
session 1: COMMIT;

DROP SCHEMA does check that there are no objects dependending on the
schema being dropped, but it does not see objects being concurrently
created by other sessions. Even if it did, this scenario would still
fail:

session 1: BEGIN: DROP SCHEMA myschema;
session 2: CREATE FUNCTION myschema.myfunc() ...;
session 1: COMMIT;

When the DROP SCHEMA runs, the schema was empty, but the new function
is created in it before the dropping transaction completes. The CREATE
FUNCTION does not see that the schema is concurrently being dropped.

In both of these scenarios, the function is left behind in the schema
that no longer exists.

To fix, acquire AccessShareLock on all referenced objects when
recording dependencies. This conflicts with the AccessExclusiveLock
taken by DROP, preventing the race. After acquiring the lock, verify
that the object still exists, and if it was dropped concurrently,
report an error. We already had such a mechanism for shared
dependencies, but for some reason we didn't do it for in-database
dependendies.

Ideally the locks would be acquired much earlier when creating a new
object, but that will require modifying a lot of callers. This check
while recording the dependency is a nice wholesale protection, and
even if we change all the CREATE commands to acquire locks earlier,
it's still good to have this as a backstop to catch any cases where we
forgot to do so.

The patch adds a few tests for some cases that left behind orphaned
objects before this. It also adds a test for roles, which already had
such protection, although that test is partially disabled because the
error message includes an OID which is not predictable.

Author: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
Reviewed-by: Heikki Linnakangas <heikki.linnakangas@iki.fi>
Discussion: https://postgr.es/m/ZiYjn0eVc7pxVY45@ip-10-97-1-34.eu-west-3.compute.internal
Backpatch-through: 14
2026-05-27 18:36:28 +03:00
..
access Fix nbtree skip array parallel alloc accounting. 2026-04-29 11:22:21 -04:00
archive Update copyright for 2025 2025-01-01 11:21:55 -05:00
backup Fix incorrect errno in OpenWalSummaryFile() 2026-02-03 11:25:14 +09:00
bootstrap Remove unnecessary (char *) casts [mem] 2025-02-12 08:50:13 +01:00
catalog Avoid orphaned objects dependencies 2026-05-27 18:36:28 +03:00
commands Skip pg_database.dathasloginevt cleanup on standby 2026-05-27 02:28:39 +03:00
executor Fix incorrect logic for hashed IN / NOT IN with non-strict operators 2026-04-24 14:03:41 +12:00
foreign Track the number of presorted outer pathkeys in MergePath 2025-05-08 18:21:32 +09:00
jit jit: No backport::SectionMemoryManager for LLVM 22. 2026-04-03 14:58:59 +13:00
lib Correct list of files in src/backend/lib/README 2025-06-27 09:31:23 +09:00
libpq Apply timingsafe_bcmp() in authentication paths 2026-05-11 05:13:47 -07:00
main Avoid possible crash within libsanitizer. 2025-11-05 11:09:30 -05:00
nodes Fix internal error from CollateExpr in SQL/JSON DEFAULT expressions 2025-10-09 01:07:52 -04:00
optimizer Consider opfamily and collation when removing redundant GROUP BY columns 2026-05-08 12:47:26 +09:00
parser Fix attnum remapping in generateClonedExtStatsStmt() 2026-04-30 11:05:29 -04:00
partitioning Strip PlaceHolderVars from partition pruning operands 2026-04-09 16:43:28 +09:00
po Translation updates 2026-05-11 13:03:08 +02:00
port Don't treat EINVAL from semget() as a hard failure. 2025-08-13 11:59:47 -04:00
postmaster Fix orphaned processes when startup process fails during PM_STARTUP 2026-04-21 09:40:03 +09:00
regex Harden our regex engine against integer overflow in size calculations. 2026-05-11 05:13:47 -07:00
replication Avoid exposing WAL receiver raw conninfo during timeline jumps 2026-05-23 08:10:12 +09:00
rewrite Fix UPDATE/DELETE ... WHERE CURRENT OF on a table with virtual columns. 2026-04-22 11:50:18 +01:00
snowball Use PG_MODULE_MAGIC_EXT in our installable shared libraries. 2025-03-26 11:11:02 -04:00
statistics Fix size check in statext_dependencies_deserialize() 2026-05-25 14:38:59 +09:00
storage Fix procLatch ownership race in ProcKill() 2026-05-27 17:19:53 +09:00
tcop Fix unbounded recursive handling of SSL/GSS in ProcessStartupPacket() 2026-05-11 05:13:47 -07:00
tsearch Fix overflows with ts_headline() 2026-05-11 05:13:47 -07:00
utils Fix integer overflow in array_agg(), when the array grows too large 2026-05-11 05:13:48 -07:00
.gitignore Add .gitignore entries for AIX-specific intermediate build artifacts. 2015-07-08 20:44:22 -04:00
common.mk Blind attempt to fix LLVM dependency in the backend 2022-09-15 10:53:48 +07:00
Makefile Fix build inconsistency due to the generation of wait-event code 2026-02-02 08:02:59 +09:00
meson.build Update copyright for 2025 2025-01-01 11:21:55 -05:00
nls.mk Return yyparse() result not via global variable 2025-01-24 06:55:39 +01:00