Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit de63f8d

Browse files
author
Amit Kapila
committed
Fix assertion failure in apply worker.
During exit, the logical replication apply worker tries to release session level locks, if any. However, if the apply worker exits due to an error before its connection is initialized, trying to release locks can lead to assertion failure. The locks will be acquired once the worker is initialized, so we don't need to release them till the worker initialization is complete. Reported-by: Alexander Lakhin Author: Hou Zhijie based on inputs from Sawada Masahiko and Amit Kapila Reviewed-by: Amit Kapila Discussion: https://postgr.es/m/2185d65f-5aae-3efa-c48f-fb42b173ef5c@gmail.com
1 parent 6489875 commit de63f8d

File tree

4 files changed

+17
-1
lines changed

4 files changed

+17
-1
lines changed

src/backend/replication/logical/applyparallelworker.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -873,6 +873,8 @@ ParallelApplyWorkerMain(Datum main_arg)
873873
int worker_slot = DatumGetInt32(main_arg);
874874
char originname[NAMEDATALEN];
875875

876+
InitializingApplyWorker = true;
877+
876878
/* Setup signal handling. */
877879
pqsignal(SIGHUP, SignalHandlerForConfigReload);
878880
pqsignal(SIGINT, SignalHandlerForShutdownRequest);
@@ -940,6 +942,8 @@ ParallelApplyWorkerMain(Datum main_arg)
940942

941943
InitializeApplyWorker();
942944

945+
InitializingApplyWorker = false;
946+
943947
/* Setup replication origin tracking. */
944948
StartTransactionCommand();
945949
ReplicationOriginNameForLogicalRep(MySubscription->oid, InvalidOid,

src/backend/replication/logical/launcher.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -797,8 +797,11 @@ logicalrep_worker_onexit(int code, Datum arg)
797797
* Session level locks may be acquired outside of a transaction in
798798
* parallel apply mode and will not be released when the worker
799799
* terminates, so manually release all locks before the worker exits.
800+
*
801+
* The locks will be acquired once the worker is initialized.
800802
*/
801-
LockReleaseAll(DEFAULT_LOCKMETHOD, true);
803+
if (!InitializingApplyWorker)
804+
LockReleaseAll(DEFAULT_LOCKMETHOD, true);
802805

803806
ApplyLauncherWakeup();
804807
}

src/backend/replication/logical/worker.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,9 @@ static TransactionId stream_xid = InvalidTransactionId;
331331
*/
332332
static uint32 parallel_stream_nchanges = 0;
333333

334+
/* Are we initializing a apply worker? */
335+
bool InitializingApplyWorker = false;
336+
334337
/*
335338
* We enable skipping all data modification changes (INSERT, UPDATE, etc.) for
336339
* the subscription if the remote transaction's finish LSN matches the subskiplsn.
@@ -4526,6 +4529,8 @@ ApplyWorkerMain(Datum main_arg)
45264529
WalRcvStreamOptions options;
45274530
int server_version;
45284531

4532+
InitializingApplyWorker = true;
4533+
45294534
/* Attach to slot */
45304535
logicalrep_worker_attach(worker_slot);
45314536

@@ -4548,6 +4553,8 @@ ApplyWorkerMain(Datum main_arg)
45484553

45494554
InitializeApplyWorker();
45504555

4556+
InitializingApplyWorker = false;
4557+
45514558
/* Connect to the origin and start the replication. */
45524559
elog(DEBUG1, "connecting to publisher using connection string \"%s\"",
45534560
MySubscription->conninfo);

src/include/replication/worker_internal.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,8 @@ extern PGDLLIMPORT LogicalRepWorker *MyLogicalRepWorker;
225225

226226
extern PGDLLIMPORT bool in_remote_transaction;
227227

228+
extern PGDLLIMPORT bool InitializingApplyWorker;
229+
228230
extern void logicalrep_worker_attach(int slot);
229231
extern LogicalRepWorker *logicalrep_worker_find(Oid subid, Oid relid,
230232
bool only_running);

0 commit comments

Comments
 (0)