Skip to content

Commit fd51941

Browse files
committed
Prevent excessive delays before launching new logrep workers.
The logical replication launcher process would sometimes sleep for as much as 3 minutes before noticing that it is supposed to launch a new worker. This could happen if (1) WaitForReplicationWorkerAttach absorbed a process latch wakeup that was meant to cause ApplyLauncherMain to do work, or (2) logicalrep_worker_launch reported failure, either because of resource limits or because the new worker terminated immediately. In case (2), the expected behavior is that we retry the launch after wal_retrieve_retry_interval, but that didn't reliably happen. It's not clear how often such conditions would occur in the field, but in our subscription test suite they are somewhat common, especially in tests that exercise cases that cause quick worker failure. That causes the tests to take substantially longer than they ought to do on typical setups. To fix (1), make WaitForReplicationWorkerAttach re-set the latch before returning if it cleared it while looping. To fix (2), ensure that we reduce wait_time to no more than wal_retrieve_retry_interval when logicalrep_worker_launch reports failure. In passing, fix a couple of perhaps-hypothetical race conditions, e.g. examining worker->in_use without a lock. Backpatch to v16. Problem (2) didn't exist before commit 5a3a953 because the previous code always set wait_time to wal_retrieve_retry_interval when launching a worker, regardless of success or failure of the launch. That behavior also greatly mitigated problem (1), so I'm not excited about adapting the remainder of the patch to the substantially-different code in older branches. Author: Tom Lane <tgl@sss.pgh.pa.us> Reviewed-by: Amit Kapila <amit.kapila16@gmail.com> Reviewed-by: Ashutosh Bapat <ashutosh.bapat.oss@gmail.com> Discussion: https://postgr.es/m/817604.1750723007@sss.pgh.pa.us Backpatch-through: 16
1 parent c2da1a5 commit fd51941

File tree

2 files changed

+44
-15
lines changed

2 files changed

+44
-15
lines changed

src/backend/replication/logical/launcher.c

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -175,12 +175,14 @@ WaitForReplicationWorkerAttach(LogicalRepWorker *worker,
175175
uint16 generation,
176176
BackgroundWorkerHandle *handle)
177177
{
178-
BgwHandleStatus status;
179-
int rc;
178+
bool result = false;
179+
bool dropped_latch = false;
180180

181181
for (;;)
182182
{
183+
BgwHandleStatus status;
183184
pid_t pid;
185+
int rc;
184186

185187
CHECK_FOR_INTERRUPTS();
186188

@@ -189,8 +191,9 @@ WaitForReplicationWorkerAttach(LogicalRepWorker *worker,
189191
/* Worker either died or has started. Return false if died. */
190192
if (!worker->in_use || worker->proc)
191193
{
194+
result = worker->in_use;
192195
LWLockRelease(LogicalRepWorkerLock);
193-
return worker->in_use;
196+
break;
194197
}
195198

196199
LWLockRelease(LogicalRepWorkerLock);
@@ -205,7 +208,7 @@ WaitForReplicationWorkerAttach(LogicalRepWorker *worker,
205208
if (generation == worker->generation)
206209
logicalrep_worker_cleanup(worker);
207210
LWLockRelease(LogicalRepWorkerLock);
208-
return false;
211+
break; /* result is already false */
209212
}
210213

211214
/*
@@ -220,8 +223,18 @@ WaitForReplicationWorkerAttach(LogicalRepWorker *worker,
220223
{
221224
ResetLatch(MyLatch);
222225
CHECK_FOR_INTERRUPTS();
226+
dropped_latch = true;
223227
}
224228
}
229+
230+
/*
231+
* If we had to clear a latch event in order to wait, be sure to restore
232+
* it before exiting. Otherwise caller may miss events.
233+
*/
234+
if (dropped_latch)
235+
SetLatch(MyLatch);
236+
237+
return result;
225238
}
226239

227240
/*
@@ -1194,10 +1207,21 @@ ApplyLauncherMain(Datum main_arg)
11941207
(elapsed = TimestampDifferenceMilliseconds(last_start, now)) >= wal_retrieve_retry_interval)
11951208
{
11961209
ApplyLauncherSetWorkerStartTime(sub->oid, now);
1197-
logicalrep_worker_launch(WORKERTYPE_APPLY,
1198-
sub->dbid, sub->oid, sub->name,
1199-
sub->owner, InvalidOid,
1200-
DSM_HANDLE_INVALID);
1210+
if (!logicalrep_worker_launch(WORKERTYPE_APPLY,
1211+
sub->dbid, sub->oid, sub->name,
1212+
sub->owner, InvalidOid,
1213+
DSM_HANDLE_INVALID))
1214+
{
1215+
/*
1216+
* We get here either if we failed to launch a worker
1217+
* (perhaps for resource-exhaustion reasons) or if we
1218+
* launched one but it immediately quit. Either way, it
1219+
* seems appropriate to try again after
1220+
* wal_retrieve_retry_interval.
1221+
*/
1222+
wait_time = Min(wait_time,
1223+
wal_retrieve_retry_interval);
1224+
}
12011225
}
12021226
else
12031227
{

src/backend/replication/logical/tablesync.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -603,14 +603,19 @@ process_syncing_tables_for_apply(XLogRecPtr current_lsn)
603603
TimestampDifferenceExceeds(hentry->last_start_time, now,
604604
wal_retrieve_retry_interval))
605605
{
606-
logicalrep_worker_launch(WORKERTYPE_TABLESYNC,
607-
MyLogicalRepWorker->dbid,
608-
MySubscription->oid,
609-
MySubscription->name,
610-
MyLogicalRepWorker->userid,
611-
rstate->relid,
612-
DSM_HANDLE_INVALID);
606+
/*
607+
* Set the last_start_time even if we fail to start
608+
* the worker, so that we won't retry until
609+
* wal_retrieve_retry_interval has elapsed.
610+
*/
613611
hentry->last_start_time = now;
612+
(void) logicalrep_worker_launch(WORKERTYPE_TABLESYNC,
613+
MyLogicalRepWorker->dbid,
614+
MySubscription->oid,
615+
MySubscription->name,
616+
MyLogicalRepWorker->userid,
617+
rstate->relid,
618+
DSM_HANDLE_INVALID);
614619
}
615620
}
616621
}

0 commit comments

Comments
 (0)