2424 * are treated as not a crash but approximately normal termination;
2525 * the walsender will exit quickly without sending any more XLOG records.
2626 *
27- * If the server is shut down, postmaster sends us SIGUSR2 after all regular
28- * backends have exited. This causes the walsender to switch to the "stopping"
29- * state. In this state, the walsender will reject any replication command
30- * that may generate WAL activity. The checkpointer begins the shutdown
31- * checkpoint once all walsenders are confirmed as stopping. When the shutdown
32- * checkpoint finishes, the postmaster sends us SIGINT. This instructs
33- * walsender to send any outstanding WAL, including the shutdown checkpoint
34- * record, wait for it to be replicated to the standby, and then exit.
27+ * If the server is shut down, postmaster sends us SIGUSR2 after all
28+ * regular backends have exited and the shutdown checkpoint has been written.
29+ * This instructs walsender to send any outstanding WAL, including the
30+ * shutdown checkpoint record, wait for it to be replicated to the standby,
31+ * and then exit.
3532 *
3633 *
3734 * Portions Copyright (c) 2010-2017, PostgreSQL Global Development Group
@@ -180,14 +177,13 @@ static bool WalSndCaughtUp = false;
180177
181178/* Flags set by signal handlers for later service in main loop */
182179static volatile sig_atomic_t got_SIGHUP = false;
183- static volatile sig_atomic_t got_SIGINT = false;
184- static volatile sig_atomic_t got_SIGUSR2 = false;
180+ static volatile sig_atomic_t walsender_ready_to_stop = false;
185181
186182/*
187- * This is set while we are streaming. When not set, SIGINT signal will be
183+ * This is set while we are streaming. When not set, SIGUSR2 signal will be
188184 * handled like SIGTERM. When set, the main loop is responsible for checking
189- * got_SIGINT and terminating when it's set (after streaming any remaining
190- * WAL).
185+ * walsender_ready_to_stop and terminating when it's set (after streaming any
186+ * remaining WAL).
191187 */
192188static volatile sig_atomic_t replication_active = false;
193189
@@ -217,7 +213,6 @@ static struct
217213/* Signal handlers */
218214static void WalSndSigHupHandler (SIGNAL_ARGS );
219215static void WalSndXLogSendHandler (SIGNAL_ARGS );
220- static void WalSndSwitchStopping (SIGNAL_ARGS );
221216static void WalSndLastCycleHandler (SIGNAL_ARGS );
222217
223218/* Prototypes for private functions */
@@ -306,14 +301,11 @@ WalSndErrorCleanup(void)
306301 ReplicationSlotCleanup ();
307302
308303 replication_active = false;
309- if (got_SIGINT )
304+ if (walsender_ready_to_stop )
310305 proc_exit (0 );
311306
312307 /* Revert back to startup state */
313308 WalSndSetState (WALSNDSTATE_STARTUP );
314-
315- if (got_SIGUSR2 )
316- WalSndSetState (WALSNDSTATE_STOPPING );
317309}
318310
319311/*
@@ -686,7 +678,7 @@ StartReplication(StartReplicationCmd *cmd)
686678 WalSndLoop (XLogSendPhysical );
687679
688680 replication_active = false;
689- if (got_SIGINT )
681+ if (walsender_ready_to_stop )
690682 proc_exit (0 );
691683 WalSndSetState (WALSNDSTATE_STARTUP );
692684
@@ -1064,7 +1056,7 @@ StartLogicalReplication(StartReplicationCmd *cmd)
10641056 {
10651057 ereport (LOG ,
10661058 (errmsg ("terminating walsender process after promotion" )));
1067- got_SIGINT = true;
1059+ walsender_ready_to_stop = true;
10681060 }
10691061
10701062 WalSndSetState (WALSNDSTATE_CATCHUP );
@@ -1115,7 +1107,7 @@ StartLogicalReplication(StartReplicationCmd *cmd)
11151107 ReplicationSlotRelease ();
11161108
11171109 replication_active = false;
1118- if (got_SIGINT )
1110+ if (walsender_ready_to_stop )
11191111 proc_exit (0 );
11201112 WalSndSetState (WALSNDSTATE_STARTUP );
11211113
@@ -1326,14 +1318,6 @@ WalSndWaitForWal(XLogRecPtr loc)
13261318 else
13271319 RecentFlushPtr = GetXLogReplayRecPtr (NULL );
13281320
1329- /*
1330- * If postmaster asked us to switch to the stopping state, do so.
1331- * Shutdown is in progress and this will allow the checkpointer to
1332- * move on with the shutdown checkpoint.
1333- */
1334- if (got_SIGUSR2 )
1335- WalSndSetState (WALSNDSTATE_STOPPING );
1336-
13371321 /*
13381322 * If postmaster asked us to stop, don't wait here anymore. This will
13391323 * cause the xlogreader to return without reading a full record, which
@@ -1343,7 +1327,7 @@ WalSndWaitForWal(XLogRecPtr loc)
13431327 * RecentFlushPtr, so we can send all remaining data before shutting
13441328 * down.
13451329 */
1346- if (got_SIGINT )
1330+ if (walsender_ready_to_stop )
13471331 break ;
13481332
13491333 /*
@@ -1417,22 +1401,6 @@ exec_replication_command(const char *cmd_string)
14171401 MemoryContext cmd_context ;
14181402 MemoryContext old_context ;
14191403
1420- /*
1421- * If WAL sender has been told that shutdown is getting close, switch its
1422- * status accordingly to handle the next replication commands correctly.
1423- */
1424- if (got_SIGUSR2 )
1425- WalSndSetState (WALSNDSTATE_STOPPING );
1426-
1427- /*
1428- * Throw error if in stopping mode. We need prevent commands that could
1429- * generate WAL while the shutdown checkpoint is being written. To be
1430- * safe, we just prohibit all new commands.
1431- */
1432- if (MyWalSnd -> state == WALSNDSTATE_STOPPING )
1433- ereport (ERROR ,
1434- (errmsg ("cannot execute new commands while WAL sender is in stopping mode" )));
1435-
14361404 /*
14371405 * CREATE_REPLICATION_SLOT ... LOGICAL exports a snapshot until the next
14381406 * command arrives. Clean up the old stuff if there's anything.
@@ -2155,20 +2123,13 @@ WalSndLoop(WalSndSendDataCallback send_data)
21552123 }
21562124
21572125 /*
2158- * At the reception of SIGUSR2, switch the WAL sender to the
2159- * stopping state.
2160- */
2161- if (got_SIGUSR2 )
2162- WalSndSetState (WALSNDSTATE_STOPPING );
2163-
2164- /*
2165- * When SIGINT arrives, we send any outstanding logs up to the
2126+ * When SIGUSR2 arrives, we send any outstanding logs up to the
21662127 * shutdown checkpoint record (i.e., the latest record), wait for
21672128 * them to be replicated to the standby, and exit. This may be a
21682129 * normal termination at shutdown, or a promotion, the walsender
21692130 * is not sure which.
21702131 */
2171- if (got_SIGINT )
2132+ if (walsender_ready_to_stop )
21722133 WalSndDone (send_data );
21732134 }
21742135
@@ -2907,23 +2868,7 @@ WalSndXLogSendHandler(SIGNAL_ARGS)
29072868 errno = save_errno ;
29082869}
29092870
2910- /* SIGUSR2: set flag to switch to stopping state */
2911- static void
2912- WalSndSwitchStopping (SIGNAL_ARGS )
2913- {
2914- int save_errno = errno ;
2915-
2916- got_SIGUSR2 = true;
2917- SetLatch (MyLatch );
2918-
2919- errno = save_errno ;
2920- }
2921-
2922- /*
2923- * SIGINT: set flag to do a last cycle and shut down afterwards. The WAL
2924- * sender should already have been switched to WALSNDSTATE_STOPPING at
2925- * this point.
2926- */
2871+ /* SIGUSR2: set flag to do a last cycle and shut down afterwards */
29272872static void
29282873WalSndLastCycleHandler (SIGNAL_ARGS )
29292874{
@@ -2938,7 +2883,7 @@ WalSndLastCycleHandler(SIGNAL_ARGS)
29382883 if (!replication_active )
29392884 kill (MyProcPid , SIGTERM );
29402885
2941- got_SIGINT = true;
2886+ walsender_ready_to_stop = true;
29422887 SetLatch (MyLatch );
29432888
29442889 errno = save_errno ;
@@ -2951,14 +2896,14 @@ WalSndSignals(void)
29512896 /* Set up signal handlers */
29522897 pqsignal (SIGHUP , WalSndSigHupHandler ); /* set flag to read config
29532898 * file */
2954- pqsignal (SIGINT , WalSndLastCycleHandler ); /* request a last cycle and
2955- * shutdown */
2899+ pqsignal (SIGINT , SIG_IGN ); /* not used */
29562900 pqsignal (SIGTERM , die ); /* request shutdown */
29572901 pqsignal (SIGQUIT , quickdie ); /* hard crash time */
29582902 InitializeTimeouts (); /* establishes SIGALRM handler */
29592903 pqsignal (SIGPIPE , SIG_IGN );
29602904 pqsignal (SIGUSR1 , WalSndXLogSendHandler ); /* request WAL sending */
2961- pqsignal (SIGUSR2 , WalSndSwitchStopping ); /* switch to stopping state */
2905+ pqsignal (SIGUSR2 , WalSndLastCycleHandler ); /* request a last cycle and
2906+ * shutdown */
29622907
29632908 /* Reset some signals that are accepted by postmaster but not here */
29642909 pqsignal (SIGCHLD , SIG_DFL );
@@ -3036,50 +2981,6 @@ WalSndWakeup(void)
30362981 }
30372982}
30382983
3039- /*
3040- * Wait that all the WAL senders have reached the stopping state. This is
3041- * used by the checkpointer to control when shutdown checkpoints can
3042- * safely begin.
3043- */
3044- void
3045- WalSndWaitStopping (void )
3046- {
3047- for (;;)
3048- {
3049- int i ;
3050- bool all_stopped = true;
3051-
3052- for (i = 0 ; i < max_wal_senders ; i ++ )
3053- {
3054- WalSndState state ;
3055- WalSnd * walsnd = & WalSndCtl -> walsnds [i ];
3056-
3057- SpinLockAcquire (& walsnd -> mutex );
3058-
3059- if (walsnd -> pid == 0 )
3060- {
3061- SpinLockRelease (& walsnd -> mutex );
3062- continue ;
3063- }
3064-
3065- state = walsnd -> state ;
3066- SpinLockRelease (& walsnd -> mutex );
3067-
3068- if (state != WALSNDSTATE_STOPPING )
3069- {
3070- all_stopped = false;
3071- break ;
3072- }
3073- }
3074-
3075- /* safe to leave if confirmation is done for all WAL senders */
3076- if (all_stopped )
3077- return ;
3078-
3079- pg_usleep (10000L ); /* wait for 10 msec */
3080- }
3081- }
3082-
30832984/* Set state for current walsender (only called in walsender) */
30842985void
30852986WalSndSetState (WalSndState state )
@@ -3113,8 +3014,6 @@ WalSndGetStateString(WalSndState state)
31133014 return "catchup" ;
31143015 case WALSNDSTATE_STREAMING :
31153016 return "streaming" ;
3116- case WALSNDSTATE_STOPPING :
3117- return "stopping" ;
31183017 }
31193018 return "UNKNOWN" ;
31203019}
0 commit comments