pgsql: Fix concurrency issues with DROP TABLESPACE

From: Andrew Dunstan <andrew(at)dunslane(dot)net>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Fix concurrency issues with DROP TABLESPACE
Date: 2026-09-09 15:34:50
Message-ID: E1x4KK1-0000000499B-2Z5x@gemulon.postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Fix concurrency issues with DROP TABLESPACE

DROP TABLESPACE checked pg_shdepend for dependent objects without
first locking the tablespace. A concurrent command that recorded a
shared dependency on the tablespace right after that check could still
commit, leaving an object whose pg_shdepend entry (or, for a relation,
pg_class.reltablespace) pointed to a tablespace that no longer existed.

Close the race by having DropTableSpace() take an AccessExclusiveLock
on the tablespace before calling checkSharedDependencies(). That
conflicts with the AccessShareLock shdepLockAndCheckObject() takes when
recording a new dependency, so the loser of the race blocks and
rechecks once the winner commits.

That AccessExclusiveLock creates a new deadlock: ALTER TABLESPACE
RENAME/SET and the internal ACL/owner updates in DROP OWNED and
REASSIGN OWNED touched the catalog tuple without locking the
tablespace, risking a lock-order cycle with DROP. Fix by taking an
AccessShareLock first in all four paths, rechecking pg_shdepend after
any wait in the DROP OWNED and REASSIGN OWNED cases. GRANT, REVOKE,
and ALTER TABLESPACE ... OWNER TO already lock the object.

Add isolation tests covering both orderings of the original race
(dependency-first and drop-first) and all four previously-unlocked
update paths.

Author: Ayush Tiwari <ayushtiwari(dot)slg01(at)gmail(dot)com>
Reviewed-by: Andrew Dunstan <andrew(at)dunslane(dot)net>
Discussion: https://postgr.es/m/CAJTYsWXjAQFnGKzXsht3XK8KHhyhontwFJw4JAHsUSfs_ptR4g@mail.gmail.com
Backpatch-through: 14

Branch
------
REL_16_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/749dd8d1f2cf8ae261122b8db397bc5dafcb99bb

Modified Files
--------------
src/backend/catalog/pg_shdepend.c | 42 ++++++++--
src/backend/commands/tablespace.c | 17 +++-
.../expected/tablespace-dependency-locking.out | 84 +++++++++++++++++++
src/test/isolation/isolation_schedule | 1 +
.../specs/tablespace-dependency-locking.spec | 98 ++++++++++++++++++++++
5 files changed, 235 insertions(+), 7 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Masahiko Sawada 2026-09-09 20:46:14 pgsql: Unify REPACK (CONCURRENTLY) error messages.
Previous Message Álvaro Herrera 2026-09-09 10:58:47 pgsql: Fix error message for concurrent repack on materialized views