Commit 703f148e authored by Andres Freund's avatar Andres Freund

Revert "Prevent panic during shutdown checkpoint"

This reverts commit 086221cf, which
was made to master only.

The approach implemented in the above commit has some issues.  While
those could easily be fixed incrementally, doing so would make
backpatching considerably harder, so instead first revert this patch.

Discussion: https://postgr.es/m/20170602002912.tqlwn4gymzlxpvs2@alap3.anarazel.de
parent 41a21bf9
...@@ -1690,11 +1690,6 @@ SELECT pid, wait_event_type, wait_event FROM pg_stat_activity WHERE wait_event i ...@@ -1690,11 +1690,6 @@ SELECT pid, wait_event_type, wait_event FROM pg_stat_activity WHERE wait_event i
<literal>backup</>: This WAL sender is sending a backup. <literal>backup</>: This WAL sender is sending a backup.
</para> </para>
</listitem> </listitem>
<listitem>
<para>
<literal>stopping</>: This WAL sender is stopping.
</para>
</listitem>
</itemizedlist> </itemizedlist>
</entry> </entry>
</row> </row>
......
...@@ -8324,12 +8324,6 @@ ShutdownXLOG(int code, Datum arg) ...@@ -8324,12 +8324,6 @@ ShutdownXLOG(int code, Datum arg)
ereport(IsPostmasterEnvironment ? LOG : NOTICE, ereport(IsPostmasterEnvironment ? LOG : NOTICE,
(errmsg("shutting down"))); (errmsg("shutting down")));
/*
* Wait for WAL senders to be in stopping state. This prevents commands
* from writing new WAL.
*/
WalSndWaitStopping();
if (RecoveryInProgress()) if (RecoveryInProgress())
CreateRestartPoint(CHECKPOINT_IS_SHUTDOWN | CHECKPOINT_IMMEDIATE); CreateRestartPoint(CHECKPOINT_IS_SHUTDOWN | CHECKPOINT_IMMEDIATE);
else else
......
...@@ -2918,7 +2918,7 @@ reaper(SIGNAL_ARGS) ...@@ -2918,7 +2918,7 @@ reaper(SIGNAL_ARGS)
* Waken walsenders for the last time. No regular backends * Waken walsenders for the last time. No regular backends
* should be around anymore. * should be around anymore.
*/ */
SignalChildren(SIGINT); SignalChildren(SIGUSR2);
pmState = PM_SHUTDOWN_2; pmState = PM_SHUTDOWN_2;
...@@ -3656,9 +3656,7 @@ PostmasterStateMachine(void) ...@@ -3656,9 +3656,7 @@ PostmasterStateMachine(void)
/* /*
* If we get here, we are proceeding with normal shutdown. All * If we get here, we are proceeding with normal shutdown. All
* the regular children are gone, and it's time to tell the * the regular children are gone, and it's time to tell the
* checkpointer to do a shutdown checkpoint. All WAL senders * checkpointer to do a shutdown checkpoint.
* are told to switch to a stopping state so that the shutdown
* checkpoint can go ahead.
*/ */
Assert(Shutdown > NoShutdown); Assert(Shutdown > NoShutdown);
/* Start the checkpointer if not running */ /* Start the checkpointer if not running */
...@@ -3667,7 +3665,6 @@ PostmasterStateMachine(void) ...@@ -3667,7 +3665,6 @@ PostmasterStateMachine(void)
/* And tell it to shut down */ /* And tell it to shut down */
if (CheckpointerPID != 0) if (CheckpointerPID != 0)
{ {
SignalSomeChildren(SIGUSR2, BACKEND_TYPE_WALSND);
signal_child(CheckpointerPID, SIGUSR2); signal_child(CheckpointerPID, SIGUSR2);
pmState = PM_SHUTDOWN; pmState = PM_SHUTDOWN;
} }
......
...@@ -24,14 +24,11 @@ ...@@ -24,14 +24,11 @@
* are treated as not a crash but approximately normal termination; * are treated as not a crash but approximately normal termination;
* the walsender will exit quickly without sending any more XLOG records. * the walsender will exit quickly without sending any more XLOG records.
* *
* If the server is shut down, postmaster sends us SIGUSR2 after all regular * If the server is shut down, postmaster sends us SIGUSR2 after all
* backends have exited. This causes the walsender to switch to the "stopping" * regular backends have exited and the shutdown checkpoint has been written.
* state. In this state, the walsender will reject any replication command * This instructs walsender to send any outstanding WAL, including the
* that may generate WAL activity. The checkpointer begins the shutdown * shutdown checkpoint record, wait for it to be replicated to the standby,
* checkpoint once all walsenders are confirmed as stopping. When the shutdown * and then exit.
* checkpoint finishes, the postmaster sends us SIGINT. This instructs
* walsender to send any outstanding WAL, including the shutdown checkpoint
* record, wait for it to be replicated to the standby, and then exit.
* *
* *
* Portions Copyright (c) 2010-2017, PostgreSQL Global Development Group * Portions Copyright (c) 2010-2017, PostgreSQL Global Development Group
...@@ -180,14 +177,13 @@ static bool WalSndCaughtUp = false; ...@@ -180,14 +177,13 @@ static bool WalSndCaughtUp = false;
/* Flags set by signal handlers for later service in main loop */ /* Flags set by signal handlers for later service in main loop */
static volatile sig_atomic_t got_SIGHUP = false; static volatile sig_atomic_t got_SIGHUP = false;
static volatile sig_atomic_t got_SIGINT = false; static volatile sig_atomic_t walsender_ready_to_stop = false;
static volatile sig_atomic_t got_SIGUSR2 = false;
/* /*
* This is set while we are streaming. When not set, SIGINT signal will be * This is set while we are streaming. When not set, SIGUSR2 signal will be
* handled like SIGTERM. When set, the main loop is responsible for checking * handled like SIGTERM. When set, the main loop is responsible for checking
* got_SIGINT and terminating when it's set (after streaming any remaining * walsender_ready_to_stop and terminating when it's set (after streaming any
* WAL). * remaining WAL).
*/ */
static volatile sig_atomic_t replication_active = false; static volatile sig_atomic_t replication_active = false;
...@@ -217,7 +213,6 @@ static struct ...@@ -217,7 +213,6 @@ static struct
/* Signal handlers */ /* Signal handlers */
static void WalSndSigHupHandler(SIGNAL_ARGS); static void WalSndSigHupHandler(SIGNAL_ARGS);
static void WalSndXLogSendHandler(SIGNAL_ARGS); static void WalSndXLogSendHandler(SIGNAL_ARGS);
static void WalSndSwitchStopping(SIGNAL_ARGS);
static void WalSndLastCycleHandler(SIGNAL_ARGS); static void WalSndLastCycleHandler(SIGNAL_ARGS);
/* Prototypes for private functions */ /* Prototypes for private functions */
...@@ -306,14 +301,11 @@ WalSndErrorCleanup(void) ...@@ -306,14 +301,11 @@ WalSndErrorCleanup(void)
ReplicationSlotCleanup(); ReplicationSlotCleanup();
replication_active = false; replication_active = false;
if (got_SIGINT) if (walsender_ready_to_stop)
proc_exit(0); proc_exit(0);
/* Revert back to startup state */ /* Revert back to startup state */
WalSndSetState(WALSNDSTATE_STARTUP); WalSndSetState(WALSNDSTATE_STARTUP);
if (got_SIGUSR2)
WalSndSetState(WALSNDSTATE_STOPPING);
} }
/* /*
...@@ -686,7 +678,7 @@ StartReplication(StartReplicationCmd *cmd) ...@@ -686,7 +678,7 @@ StartReplication(StartReplicationCmd *cmd)
WalSndLoop(XLogSendPhysical); WalSndLoop(XLogSendPhysical);
replication_active = false; replication_active = false;
if (got_SIGINT) if (walsender_ready_to_stop)
proc_exit(0); proc_exit(0);
WalSndSetState(WALSNDSTATE_STARTUP); WalSndSetState(WALSNDSTATE_STARTUP);
...@@ -1064,7 +1056,7 @@ StartLogicalReplication(StartReplicationCmd *cmd) ...@@ -1064,7 +1056,7 @@ StartLogicalReplication(StartReplicationCmd *cmd)
{ {
ereport(LOG, ereport(LOG,
(errmsg("terminating walsender process after promotion"))); (errmsg("terminating walsender process after promotion")));
got_SIGINT = true; walsender_ready_to_stop = true;
} }
WalSndSetState(WALSNDSTATE_CATCHUP); WalSndSetState(WALSNDSTATE_CATCHUP);
...@@ -1115,7 +1107,7 @@ StartLogicalReplication(StartReplicationCmd *cmd) ...@@ -1115,7 +1107,7 @@ StartLogicalReplication(StartReplicationCmd *cmd)
ReplicationSlotRelease(); ReplicationSlotRelease();
replication_active = false; replication_active = false;
if (got_SIGINT) if (walsender_ready_to_stop)
proc_exit(0); proc_exit(0);
WalSndSetState(WALSNDSTATE_STARTUP); WalSndSetState(WALSNDSTATE_STARTUP);
...@@ -1326,14 +1318,6 @@ WalSndWaitForWal(XLogRecPtr loc) ...@@ -1326,14 +1318,6 @@ WalSndWaitForWal(XLogRecPtr loc)
else else
RecentFlushPtr = GetXLogReplayRecPtr(NULL); RecentFlushPtr = GetXLogReplayRecPtr(NULL);
/*
* If postmaster asked us to switch to the stopping state, do so.
* Shutdown is in progress and this will allow the checkpointer to
* move on with the shutdown checkpoint.
*/
if (got_SIGUSR2)
WalSndSetState(WALSNDSTATE_STOPPING);
/* /*
* If postmaster asked us to stop, don't wait here anymore. This will * If postmaster asked us to stop, don't wait here anymore. This will
* cause the xlogreader to return without reading a full record, which * cause the xlogreader to return without reading a full record, which
...@@ -1343,7 +1327,7 @@ WalSndWaitForWal(XLogRecPtr loc) ...@@ -1343,7 +1327,7 @@ WalSndWaitForWal(XLogRecPtr loc)
* RecentFlushPtr, so we can send all remaining data before shutting * RecentFlushPtr, so we can send all remaining data before shutting
* down. * down.
*/ */
if (got_SIGINT) if (walsender_ready_to_stop)
break; break;
/* /*
...@@ -1417,22 +1401,6 @@ exec_replication_command(const char *cmd_string) ...@@ -1417,22 +1401,6 @@ exec_replication_command(const char *cmd_string)
MemoryContext cmd_context; MemoryContext cmd_context;
MemoryContext old_context; MemoryContext old_context;
/*
* If WAL sender has been told that shutdown is getting close, switch its
* status accordingly to handle the next replication commands correctly.
*/
if (got_SIGUSR2)
WalSndSetState(WALSNDSTATE_STOPPING);
/*
* Throw error if in stopping mode. We need prevent commands that could
* generate WAL while the shutdown checkpoint is being written. To be
* safe, we just prohibit all new commands.
*/
if (MyWalSnd->state == WALSNDSTATE_STOPPING)
ereport(ERROR,
(errmsg("cannot execute new commands while WAL sender is in stopping mode")));
/* /*
* CREATE_REPLICATION_SLOT ... LOGICAL exports a snapshot until the next * CREATE_REPLICATION_SLOT ... LOGICAL exports a snapshot until the next
* command arrives. Clean up the old stuff if there's anything. * command arrives. Clean up the old stuff if there's anything.
...@@ -2155,20 +2123,13 @@ WalSndLoop(WalSndSendDataCallback send_data) ...@@ -2155,20 +2123,13 @@ WalSndLoop(WalSndSendDataCallback send_data)
} }
/* /*
* At the reception of SIGUSR2, switch the WAL sender to the * When SIGUSR2 arrives, we send any outstanding logs up to the
* stopping state.
*/
if (got_SIGUSR2)
WalSndSetState(WALSNDSTATE_STOPPING);
/*
* When SIGINT arrives, we send any outstanding logs up to the
* shutdown checkpoint record (i.e., the latest record), wait for * shutdown checkpoint record (i.e., the latest record), wait for
* them to be replicated to the standby, and exit. This may be a * them to be replicated to the standby, and exit. This may be a
* normal termination at shutdown, or a promotion, the walsender * normal termination at shutdown, or a promotion, the walsender
* is not sure which. * is not sure which.
*/ */
if (got_SIGINT) if (walsender_ready_to_stop)
WalSndDone(send_data); WalSndDone(send_data);
} }
...@@ -2907,23 +2868,7 @@ WalSndXLogSendHandler(SIGNAL_ARGS) ...@@ -2907,23 +2868,7 @@ WalSndXLogSendHandler(SIGNAL_ARGS)
errno = save_errno; errno = save_errno;
} }
/* SIGUSR2: set flag to switch to stopping state */ /* SIGUSR2: set flag to do a last cycle and shut down afterwards */
static void
WalSndSwitchStopping(SIGNAL_ARGS)
{
int save_errno = errno;
got_SIGUSR2 = true;
SetLatch(MyLatch);
errno = save_errno;
}
/*
* SIGINT: set flag to do a last cycle and shut down afterwards. The WAL
* sender should already have been switched to WALSNDSTATE_STOPPING at
* this point.
*/
static void static void
WalSndLastCycleHandler(SIGNAL_ARGS) WalSndLastCycleHandler(SIGNAL_ARGS)
{ {
...@@ -2938,7 +2883,7 @@ WalSndLastCycleHandler(SIGNAL_ARGS) ...@@ -2938,7 +2883,7 @@ WalSndLastCycleHandler(SIGNAL_ARGS)
if (!replication_active) if (!replication_active)
kill(MyProcPid, SIGTERM); kill(MyProcPid, SIGTERM);
got_SIGINT = true; walsender_ready_to_stop = true;
SetLatch(MyLatch); SetLatch(MyLatch);
errno = save_errno; errno = save_errno;
...@@ -2951,14 +2896,14 @@ WalSndSignals(void) ...@@ -2951,14 +2896,14 @@ WalSndSignals(void)
/* Set up signal handlers */ /* Set up signal handlers */
pqsignal(SIGHUP, WalSndSigHupHandler); /* set flag to read config pqsignal(SIGHUP, WalSndSigHupHandler); /* set flag to read config
* file */ * file */
pqsignal(SIGINT, WalSndLastCycleHandler); /* request a last cycle and pqsignal(SIGINT, SIG_IGN); /* not used */
* shutdown */
pqsignal(SIGTERM, die); /* request shutdown */ pqsignal(SIGTERM, die); /* request shutdown */
pqsignal(SIGQUIT, quickdie); /* hard crash time */ pqsignal(SIGQUIT, quickdie); /* hard crash time */
InitializeTimeouts(); /* establishes SIGALRM handler */ InitializeTimeouts(); /* establishes SIGALRM handler */
pqsignal(SIGPIPE, SIG_IGN); pqsignal(SIGPIPE, SIG_IGN);
pqsignal(SIGUSR1, WalSndXLogSendHandler); /* request WAL sending */ pqsignal(SIGUSR1, WalSndXLogSendHandler); /* request WAL sending */
pqsignal(SIGUSR2, WalSndSwitchStopping); /* switch to stopping state */ pqsignal(SIGUSR2, WalSndLastCycleHandler); /* request a last cycle and
* shutdown */
/* Reset some signals that are accepted by postmaster but not here */ /* Reset some signals that are accepted by postmaster but not here */
pqsignal(SIGCHLD, SIG_DFL); pqsignal(SIGCHLD, SIG_DFL);
...@@ -3036,50 +2981,6 @@ WalSndWakeup(void) ...@@ -3036,50 +2981,6 @@ WalSndWakeup(void)
} }
} }
/*
* Wait that all the WAL senders have reached the stopping state. This is
* used by the checkpointer to control when shutdown checkpoints can
* safely begin.
*/
void
WalSndWaitStopping(void)
{
for (;;)
{
int i;
bool all_stopped = true;
for (i = 0; i < max_wal_senders; i++)
{
WalSndState state;
WalSnd *walsnd = &WalSndCtl->walsnds[i];
SpinLockAcquire(&walsnd->mutex);
if (walsnd->pid == 0)
{
SpinLockRelease(&walsnd->mutex);
continue;
}
state = walsnd->state;
SpinLockRelease(&walsnd->mutex);
if (state != WALSNDSTATE_STOPPING)
{
all_stopped = false;
break;
}
}
/* safe to leave if confirmation is done for all WAL senders */
if (all_stopped)
return;
pg_usleep(10000L); /* wait for 10 msec */
}
}
/* Set state for current walsender (only called in walsender) */ /* Set state for current walsender (only called in walsender) */
void void
WalSndSetState(WalSndState state) WalSndSetState(WalSndState state)
...@@ -3113,8 +3014,6 @@ WalSndGetStateString(WalSndState state) ...@@ -3113,8 +3014,6 @@ WalSndGetStateString(WalSndState state)
return "catchup"; return "catchup";
case WALSNDSTATE_STREAMING: case WALSNDSTATE_STREAMING:
return "streaming"; return "streaming";
case WALSNDSTATE_STOPPING:
return "stopping";
} }
return "UNKNOWN"; return "UNKNOWN";
} }
......
...@@ -44,7 +44,6 @@ extern void WalSndSignals(void); ...@@ -44,7 +44,6 @@ extern void WalSndSignals(void);
extern Size WalSndShmemSize(void); extern Size WalSndShmemSize(void);
extern void WalSndShmemInit(void); extern void WalSndShmemInit(void);
extern void WalSndWakeup(void); extern void WalSndWakeup(void);
extern void WalSndWaitStopping(void);
extern void WalSndRqstFileReload(void); extern void WalSndRqstFileReload(void);
/* /*
......
...@@ -24,8 +24,7 @@ typedef enum WalSndState ...@@ -24,8 +24,7 @@ typedef enum WalSndState
WALSNDSTATE_STARTUP = 0, WALSNDSTATE_STARTUP = 0,
WALSNDSTATE_BACKUP, WALSNDSTATE_BACKUP,
WALSNDSTATE_CATCHUP, WALSNDSTATE_CATCHUP,
WALSNDSTATE_STREAMING, WALSNDSTATE_STREAMING
WALSNDSTATE_STOPPING
} WalSndState; } WalSndState;
/* /*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment