Skip to content

Commit 521fd47

Browse files
committed
Use weaker locks when updating pg_subscription_rel
The previously used ShareRowExclusiveLock, while technically probably more correct, led to deadlocks during seemingly unrelated operations and thus a poor experience. Use RowExclusiveLock, like for most similar catalog operations. In some care cases, the user might see an error from DDL commands. Discussion: https://www.postgresql.org/message-id/flat/13592.1490851519%40sss.pgh.pa.us Author: Petr Jelinek <petr.jelinek@2ndquadrant.com>
1 parent c45b1d2 commit 521fd47

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/backend/catalog/pg_subscription.c

+7-5
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,10 @@ textarray_to_stringlist(ArrayType *textarray)
214214

215215
/*
216216
* Set the state of a subscription table.
217+
*
218+
* The insert-or-update logic in this function is not concurrency safe so it
219+
* might raise an error in rare circumstances. But if we took a stronger lock
220+
* such as ShareRowExclusiveLock, we would risk more deadlocks.
217221
*/
218222
Oid
219223
SetSubscriptionRelState(Oid subid, Oid relid, char state,
@@ -225,8 +229,7 @@ SetSubscriptionRelState(Oid subid, Oid relid, char state,
225229
bool nulls[Natts_pg_subscription_rel];
226230
Datum values[Natts_pg_subscription_rel];
227231

228-
/* Prevent concurrent changes. */
229-
rel = heap_open(SubscriptionRelRelationId, ShareRowExclusiveLock);
232+
rel = heap_open(SubscriptionRelRelationId, RowExclusiveLock);
230233

231234
/* Try finding existing mapping. */
232235
tup = SearchSysCacheCopy2(SUBSCRIPTIONRELMAP,
@@ -357,8 +360,7 @@ RemoveSubscriptionRel(Oid subid, Oid relid)
357360
HeapTuple tup;
358361
int nkeys = 0;
359362

360-
/* Prevent concurrent changes (see SetSubscriptionRelState()). */
361-
rel = heap_open(SubscriptionRelRelationId, ShareRowExclusiveLock);
363+
rel = heap_open(SubscriptionRelRelationId, RowExclusiveLock);
362364

363365
if (OidIsValid(subid))
364366
{
@@ -386,7 +388,7 @@ RemoveSubscriptionRel(Oid subid, Oid relid)
386388
}
387389
heap_endscan(scan);
388390

389-
heap_close(rel, ShareRowExclusiveLock);
391+
heap_close(rel, RowExclusiveLock);
390392
}
391393

392394

0 commit comments

Comments
 (0)