postgresql/src/backend/commands
Amit Kapila a5d4c04150 Fix self-deadlock during DROP SUBSCRIPTION.
The DROP SUBSCRIPTION command performs several operations: it stops the
subscription workers, removes subscription-related entries from system
catalogs, and deletes the replication slot on the publisher server.
Previously, this command acquired an AccessExclusiveLock on
pg_subscription before initiating these steps.

However, while holding this lock, the command attempts to connect to the
publisher to remove the replication slot. In cases where the connection is
made to a newly created database on the same server as subscriber, the
cache-building process during connection tries to acquire an
AccessShareLock on pg_subscription, resulting in a self-deadlock.

To resolve this issue, we reduce the lock level on pg_subscription during
DROP SUBSCRIPTION from AccessExclusiveLock to RowExclusiveLock. Earlier,
the higher lock level was used to prevent the launcher from starting a new
worker during the drop operation, as a restarted worker could become
orphaned.

Now, instead of relying on a strict lock, we acquire an AccessShareLock on
the specific subscription being dropped and re-validate its existence
after acquiring the lock. If the subscription is no longer valid, the
worker exits gracefully. This approach avoids the deadlock while still
ensuring that orphan workers are not created.

Reported-by: Alexander Lakhin <exclusion@gmail.com>
Author: Dilip Kumar <dilipbalaut@gmail.com>
Reviewed-by: vignesh C <vignesh21@gmail.com>
Reviewed-by: Hayato Kuroda <kuroda.hayato@fujitsu.com>
Reviewed-by: Amit Kapila <amit.kapila16@gmail.com>
Backpatch-through: 13
Discussion: https://postgr.es/m/18988-7312c868be2d467f@postgresql.org
2025-08-19 05:18:24 +00:00
..
aggregatecmds.c Update copyright for 2025 2025-01-01 11:21:55 -05:00
alter.c Avoid invalidating all RelationSyncCache entries on publication rename. 2025-03-13 09:16:33 +05:30
amcmds.c Update copyright for 2025 2025-01-01 11:21:55 -05:00
analyze.c read_stream: Introduce and use optional batchmode support 2025-03-30 18:36:41 -04:00
async.c Update copyright for 2025 2025-01-01 11:21:55 -05:00
cluster.c Add relallfrozen to pg_class 2025-03-03 11:18:05 -05:00
collationcmds.c Update copyright for 2025 2025-01-01 11:21:55 -05:00
comment.c Update copyright for 2025 2025-01-01 11:21:55 -05:00
constraint.c Update copyright for 2025 2025-01-01 11:21:55 -05:00
conversioncmds.c Update copyright for 2025 2025-01-01 11:21:55 -05:00
copy.c Use PRI?64 instead of "ll?" in format strings (continued). 2025-03-29 10:43:57 +01:00
copyfrom.c Use PRI?64 instead of "ll?" in format strings (continued). 2025-03-29 10:43:57 +01:00
copyfromparse.c Revert a few small patches that were intended for version 19. 2025-06-11 15:10:12 -07:00
copyto.c Revert "Don't lock partitions pruned by initial pruning" 2025-05-22 17:02:35 +09:00
createas.c Revert "Don't lock partitions pruned by initial pruning" 2025-05-22 17:02:35 +09:00
dbcommands.c Don't copy datlocale from template unless provider matches. 2025-08-05 09:23:57 -07:00
define.c Update copyright for 2025 2025-01-01 11:21:55 -05:00
discard.c Update copyright for 2025 2025-01-01 11:21:55 -05:00
dropcmds.c Update copyright for 2025 2025-01-01 11:21:55 -05:00
event_trigger.c Update copyright for 2025 2025-01-01 11:21:55 -05:00
explain.c Change internal queryid type from uint64 to int64 2025-05-30 22:59:39 +12:00
explain_dr.c Make it possible for loadable modules to add EXPLAIN options. 2025-03-18 08:41:12 -04:00
explain_format.c Make it possible for loadable modules to add EXPLAIN options. 2025-03-18 08:41:12 -04:00
explain_state.c Add an additional hook for EXPLAIN option validation. 2025-03-20 13:47:55 -04:00
extension.c Revert "Don't lock partitions pruned by initial pruning" 2025-05-22 17:02:35 +09:00
foreigncmds.c Disallow "=" in names of reloptions and foreign-data options. 2025-06-02 15:22:44 -04:00
functioncmds.c Fix erroneous construction of functions' dependencies on transforms. 2025-04-07 13:31:37 -04:00
indexcmds.c Correct misleading error messages 2025-06-26 22:02:16 +02:00
lockcmds.c Update copyright for 2025 2025-01-01 11:21:55 -05:00
Makefile Make it possible for loadable modules to add EXPLAIN options. 2025-03-18 08:41:12 -04:00
matview.c Message style improvements 2025-06-28 19:18:06 +02:00
meson.build Make it possible for loadable modules to add EXPLAIN options. 2025-03-18 08:41:12 -04:00
opclasscmds.c Add nbtree skip scan optimization. 2025-04-04 12:27:04 -04:00
operatorcmds.c Update copyright for 2025 2025-01-01 11:21:55 -05:00
policy.c Update copyright for 2025 2025-01-01 11:21:55 -05:00
portalcmds.c Revert "Don't lock partitions pruned by initial pruning" 2025-05-22 17:02:35 +09:00
prepare.c Revert "Don't lock partitions pruned by initial pruning" 2025-05-22 17:02:35 +09:00
proclang.c Update copyright for 2025 2025-01-01 11:21:55 -05:00
publicationcmds.c Throw ERROR when publish_generated_columns is specified without a value. 2025-08-05 09:21:50 +00:00
schemacmds.c Update copyright for 2025 2025-01-01 11:21:55 -05:00
seclabel.c Update copyright for 2025 2025-01-01 11:21:55 -05:00
sequence.c Use PRI?64 instead of "ll?" in format strings (continued). 2025-03-29 10:43:57 +01:00
statscmds.c Virtual generated columns 2025-02-07 09:46:59 +01:00
subscriptioncmds.c Fix self-deadlock during DROP SUBSCRIPTION. 2025-08-19 05:18:24 +00:00
tablecmds.c Obtain required table lock during cross-table updates, redux. 2025-07-03 13:46:07 -04:00
tablespace.c Remove unnecessary (char *) casts [xlog] 2025-02-13 10:57:07 +01:00
trigger.c Fix oversight in FindTriggerIncompatibleWithInheritance. 2025-08-08 17:35:00 +09:00
tsearchcmds.c Update copyright for 2025 2025-01-01 11:21:55 -05:00
typecmds.c Prevent creation of duplicate not-null constraints for domains 2025-07-03 11:46:12 +02:00
user.c Fix guc_malloc calls for consistency and OOM checks 2025-03-27 22:57:34 +01:00
vacuum.c Avoid scribbling of VACUUM options 2025-06-25 10:03:46 +09:00
vacuumparallel.c Change internal queryid type from uint64 to int64 2025-05-30 22:59:39 +12:00
variable.c Fix bug allowing io_combine_limit > io_max_combine_combine limit 2025-04-25 13:31:24 -04:00
view.c Update copyright for 2025 2025-01-01 11:21:55 -05:00