Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitadfd802

Browse files
author
Amit Kapila
committed
Fix a deadlock during ALTER SUBSCRIPTION ... DROP PUBLICATION.
A deadlock can occur when the DDL command and the apply worker acquirecatalog locks in different orders while dropping replication origins.The issue is rare in PG16 and higher branches because, in most cases, thetablesync worker performs the origin drop in those branches, and itslocking sequence does not conflict with DDL operations.This patch ensures consistent lock acquisition to prevent such deadlocks.As per buildfarm.Reported-by: Alexander Lakhin <exclusion@gmail.com>Author: Ajin Cherian <itsajin@gmail.com>Reviewed-by: Hayato Kuroda <kuroda.hayato@fujitsu.com>Reviewed-by: vignesh C <vignesh21@gmail.com>Reviewed-by: Amit Kapila <amit.kapila16@gmail.com>Backpatch-through: 14, where it was introducedDiscussion:https://postgr.es/m/bab95e12-6cc5-4ebb-80a8-3e41956aa297@gmail.com
1 parent0ac1581 commitadfd802

File tree

3 files changed

+55
-7
lines changed

3 files changed

+55
-7
lines changed

‎src/backend/catalog/pg_subscription.c‎

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -273,18 +273,33 @@ AddSubscriptionRelState(Oid subid, Oid relid, char state,
273273
* Update the state of a subscription table.
274274
*/
275275
void
276-
UpdateSubscriptionRelState(Oidsubid,Oidrelid,charstate,
277-
XLogRecPtrsublsn)
276+
UpdateSubscriptionRelStateEx(Oidsubid,Oidrelid,charstate,
277+
XLogRecPtrsublsn,boolalready_locked)
278278
{
279279
Relationrel;
280280
HeapTupletup;
281281
boolnulls[Natts_pg_subscription_rel];
282282
Datumvalues[Natts_pg_subscription_rel];
283283
boolreplaces[Natts_pg_subscription_rel];
284284

285-
LockSharedObject(SubscriptionRelationId,subid,0,AccessShareLock);
285+
if (already_locked)
286+
{
287+
#ifdefUSE_ASSERT_CHECKING
288+
LOCKTAGtag;
286289

287-
rel=table_open(SubscriptionRelRelationId,RowExclusiveLock);
290+
Assert(CheckRelationOidLockedByMe(SubscriptionRelRelationId,
291+
RowExclusiveLock, true));
292+
SET_LOCKTAG_OBJECT(tag,InvalidOid,SubscriptionRelationId,subid,0);
293+
Assert(LockHeldByMe(&tag,AccessShareLock));
294+
#endif
295+
296+
rel=table_open(SubscriptionRelRelationId,NoLock);
297+
}
298+
else
299+
{
300+
LockSharedObject(SubscriptionRelationId,subid,0,AccessShareLock);
301+
rel=table_open(SubscriptionRelRelationId,RowExclusiveLock);
302+
}
288303

289304
/* Try finding existing mapping. */
290305
tup=SearchSysCacheCopy2(SUBSCRIPTIONRELMAP,
@@ -318,6 +333,16 @@ UpdateSubscriptionRelState(Oid subid, Oid relid, char state,
318333
table_close(rel,NoLock);
319334
}
320335

336+
/*
337+
* Update the state of a subscription table.
338+
*/
339+
void
340+
UpdateSubscriptionRelState(Oidsubid,Oidrelid,charstate,
341+
XLogRecPtrsublsn)
342+
{
343+
UpdateSubscriptionRelStateEx(subid,relid,state,sublsn, false);
344+
}
345+
321346
/*
322347
* Get state of subscription table.
323348
*

‎src/backend/replication/logical/tablesync.c‎

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,7 @@ process_syncing_tables_for_apply(XLogRecPtr current_lsn)
425425
ListCell*lc;
426426
boolstarted_tx= false;
427427
boolshould_exit= false;
428+
Relationrel=NULL;
428429

429430
Assert(!IsTransactionState());
430431

@@ -492,7 +493,16 @@ process_syncing_tables_for_apply(XLogRecPtr current_lsn)
492493
* worker to remove the origin tracking as if there is any
493494
* error while dropping we won't restart it to drop the
494495
* origin. So passing missing_ok = true.
496+
*
497+
* Lock the subscription and origin in the same order as we
498+
* are doing during DDL commands to avoid deadlocks. See
499+
* AlterSubscription_refresh.
495500
*/
501+
LockSharedObject(SubscriptionRelationId,MyLogicalRepWorker->subid,
502+
0,AccessShareLock);
503+
if (!rel)
504+
rel=table_open(SubscriptionRelRelationId,RowExclusiveLock);
505+
496506
ReplicationOriginNameForLogicalRep(MyLogicalRepWorker->subid,
497507
rstate->relid,
498508
originname,
@@ -502,9 +512,9 @@ process_syncing_tables_for_apply(XLogRecPtr current_lsn)
502512
/*
503513
* Update the state to READY only after the origin cleanup.
504514
*/
505-
UpdateSubscriptionRelState(MyLogicalRepWorker->subid,
506-
rstate->relid,rstate->state,
507-
rstate->lsn);
515+
UpdateSubscriptionRelStateEx(MyLogicalRepWorker->subid,
516+
rstate->relid,rstate->state,
517+
rstate->lsn, true);
508518
}
509519
}
510520
else
@@ -555,7 +565,14 @@ process_syncing_tables_for_apply(XLogRecPtr current_lsn)
555565
* This is required to avoid any undetected deadlocks
556566
* due to any existing lock as deadlock detector won't
557567
* be able to detect the waits on the latch.
568+
*
569+
* Also close any tables prior to the commit.
558570
*/
571+
if (rel)
572+
{
573+
table_close(rel,NoLock);
574+
rel=NULL;
575+
}
559576
CommitTransactionCommand();
560577
pgstat_report_stat(false);
561578
}
@@ -621,6 +638,10 @@ process_syncing_tables_for_apply(XLogRecPtr current_lsn)
621638
}
622639
}
623640

641+
/* Close table if opened */
642+
if (rel)
643+
table_close(rel,NoLock);
644+
624645
if (started_tx)
625646
{
626647
/*

‎src/include/catalog/pg_subscription_rel.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ extern void AddSubscriptionRelState(Oid subid, Oid relid, char state,
8484
XLogRecPtrsublsn);
8585
externvoidUpdateSubscriptionRelState(Oidsubid,Oidrelid,charstate,
8686
XLogRecPtrsublsn);
87+
externvoidUpdateSubscriptionRelStateEx(Oidsubid,Oidrelid,charstate,
88+
XLogRecPtrsublsn,boolalready_locked);
8789
externcharGetSubscriptionRelState(Oidsubid,Oidrelid,XLogRecPtr*sublsn);
8890
externvoidRemoveSubscriptionRel(Oidsubid,Oidrelid);
8991

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp