postgresql/src/backend/access
Andres Freund c6c3334364 Prevent possibility of panics during shutdown checkpoint.
When the checkpointer writes the shutdown checkpoint, it checks
afterwards whether any WAL has been written since it started and
throws a PANIC if so.  At that point, only walsenders are still
active, so one might think this could not happen, but walsenders can
also generate WAL, for instance in BASE_BACKUP and logical decoding
related commands (e.g. via hint bits).  So they can trigger this panic
if such a command is run while the shutdown checkpoint is being
written.

To fix this, divide the walsender shutdown into two phases.  First,
checkpointer, itself triggered by postmaster, sends a
PROCSIG_WALSND_INIT_STOPPING signal to all walsenders.  If the backend
is idle or runs an SQL query this causes the backend to shutdown, if
logical replication is in progress all existing WAL records are
processed followed by a shutdown.  Otherwise this causes the walsender
to switch to the "stopping" state. In this state, the walsender will
reject any further replication commands. The checkpointer begins the
shutdown checkpoint once all walsenders are confirmed as
stopping. When the shutdown checkpoint finishes, the postmaster sends
us SIGUSR2. 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.

Author: Andres Freund, based on an earlier patch by Michael Paquier
Reported-By: Fujii Masao, Andres Freund
Reviewed-By: Michael Paquier
Discussion: https://postgr.es/m/20170602002912.tqlwn4gymzlxpvs2@alap3.anarazel.de
Backpatch: 9.4, where logical decoding was introduced
2017-06-05 19:18:15 -07:00
..
brin Assorted translatable string fixes 2017-06-04 11:41:16 -04:00
common Post-PG 10 beta1 pgindent run 2017-05-17 16:31:56 -04:00
gin Fix wording in amvalidate error messages 2017-05-30 15:45:42 -04:00
gist Fix wording in amvalidate error messages 2017-05-30 15:45:42 -04:00
hash Fix wording in amvalidate error messages 2017-05-30 15:45:42 -04:00
heap Post-PG 10 beta1 pgindent run 2017-05-17 16:31:56 -04:00
index Improve error reporting for tuple-routing failures. 2017-03-03 09:09:52 +05:30
nbtree Fix wording in amvalidate error messages 2017-05-30 15:45:42 -04:00
rmgrdesc Post-PG 10 beta1 pgindent run 2017-05-17 16:31:56 -04:00
spgist Fix wording in amvalidate error messages 2017-05-30 15:45:42 -04:00
tablesample Update copyright via script for 2017 2017-01-03 13:48:53 -05:00
transam Prevent possibility of panics during shutdown checkpoint. 2017-06-05 19:18:15 -07:00
Makefile TABLESAMPLE, SQL Standard and extensible 2015-05-15 14:37:10 -04:00