Skip to content

Commit 2c48f5d

Browse files
committed
Use standard interrupt handling in logical replication launcher.
Previously the exit handling was only able to exit from within the main loop, and not from within the backend code it calls. Fix that by using the standard die() SIGTERM handler, and adding the necessary CHECK_FOR_INTERRUPTS() call. This requires adding yet another process-type-specific branch to ProcessInterrupts(), which hints that we probably should generalize that handling. But that's work for another day. Author: Petr Jelinek Reviewed-By: Andres Freund Discussion: https://postgr.es/m/fe072153-babd-3b5d-8052-73527a6eb657@2ndquadrant.com
1 parent 5fd56b9 commit 2c48f5d

File tree

3 files changed

+27
-25
lines changed

3 files changed

+27
-25
lines changed

src/backend/replication/logical/launcher.c

+16-25
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ static void logicalrep_worker_cleanup(LogicalRepWorker *worker);
8181

8282
/* Flags set by signal handlers */
8383
static volatile sig_atomic_t got_SIGHUP = false;
84-
static volatile sig_atomic_t got_SIGTERM = false;
8584

8685
static bool on_commit_launcher_wakeup = false;
8786

@@ -634,20 +633,6 @@ logicalrep_worker_onexit(int code, Datum arg)
634633
ApplyLauncherWakeup();
635634
}
636635

637-
/* SIGTERM: set flag to exit at next convenient time */
638-
static void
639-
logicalrep_launcher_sigterm(SIGNAL_ARGS)
640-
{
641-
int save_errno = errno;
642-
643-
got_SIGTERM = true;
644-
645-
/* Waken anything waiting on the process latch */
646-
SetLatch(MyLatch);
647-
648-
errno = save_errno;
649-
}
650-
651636
/* SIGHUP: set flag to reload configuration at next convenient time */
652637
static void
653638
logicalrep_launcher_sighup(SIGNAL_ARGS)
@@ -809,21 +794,22 @@ ApplyLauncherMain(Datum main_arg)
809794

810795
before_shmem_exit(logicalrep_launcher_onexit, (Datum) 0);
811796

797+
Assert(LogicalRepCtx->launcher_pid == 0);
798+
LogicalRepCtx->launcher_pid = MyProcPid;
799+
812800
/* Establish signal handlers. */
813801
pqsignal(SIGHUP, logicalrep_launcher_sighup);
814-
pqsignal(SIGTERM, logicalrep_launcher_sigterm);
802+
pqsignal(SIGTERM, die);
815803
BackgroundWorkerUnblockSignals();
816804

817-
LogicalRepCtx->launcher_pid = MyProcPid;
818-
819805
/*
820806
* Establish connection to nailed catalogs (we only ever access
821807
* pg_subscription).
822808
*/
823809
BackgroundWorkerInitializeConnection(NULL, NULL);
824810

825811
/* Enter main loop */
826-
while (!got_SIGTERM)
812+
for (;;)
827813
{
828814
int rc;
829815
List *sublist;
@@ -833,6 +819,8 @@ ApplyLauncherMain(Datum main_arg)
833819
TimestampTz now;
834820
long wait_time = DEFAULT_NAPTIME_PER_CYCLE;
835821

822+
CHECK_FOR_INTERRUPTS();
823+
836824
now = GetCurrentTimestamp();
837825

838826
/* Limit the start retry to once a wal_retrieve_retry_interval */
@@ -909,13 +897,16 @@ ApplyLauncherMain(Datum main_arg)
909897
}
910898
}
911899

912-
LogicalRepCtx->launcher_pid = 0;
913-
914-
/* ... and if it returns, we're done */
915-
ereport(DEBUG1,
916-
(errmsg("logical replication launcher shutting down")));
900+
/* Not reachable */
901+
}
917902

918-
proc_exit(0);
903+
/*
904+
* Is current process the logical replication launcher?
905+
*/
906+
bool
907+
IsLogicalLauncher(void)
908+
{
909+
return LogicalRepCtx->launcher_pid == MyProcPid;
919910
}
920911

921912
/*

src/backend/tcop/postgres.c

+9
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
#include "pg_getopt.h"
5656
#include "postmaster/autovacuum.h"
5757
#include "postmaster/postmaster.h"
58+
#include "replication/logicallauncher.h"
5859
#include "replication/logicalworker.h"
5960
#include "replication/slot.h"
6061
#include "replication/walsender.h"
@@ -2848,6 +2849,14 @@ ProcessInterrupts(void)
28482849
ereport(FATAL,
28492850
(errcode(ERRCODE_ADMIN_SHUTDOWN),
28502851
errmsg("terminating logical replication worker due to administrator command")));
2852+
else if (IsLogicalLauncher())
2853+
{
2854+
ereport(DEBUG1,
2855+
(errmsg("logical replication launcher shutting down")));
2856+
2857+
/* The logical replication launcher can be stopped at any time. */
2858+
proc_exit(0);
2859+
}
28512860
else if (RecoveryConflictPending && RecoveryConflictRetryable)
28522861
{
28532862
pgstat_report_recovery_conflict(RecoveryConflictReason);

src/include/replication/logicallauncher.h

+2
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,6 @@ extern void ApplyLauncherShmemInit(void);
2424
extern void ApplyLauncherWakeupAtCommit(void);
2525
extern void AtEOXact_ApplyLauncher(bool isCommit);
2626

27+
extern bool IsLogicalLauncher(void);
28+
2729
#endif /* LOGICALLAUNCHER_H */

0 commit comments

Comments
 (0)