postgresql/src/include
Tom Lane a1a789eb5a In walreceiver, don't try to do ereport() in a signal handler.
This is quite unsafe, even for the case of ereport(FATAL) where we won't
return control to the interrupted code, and despite this code's use of
a flag to restrict the areas where we'd try to do it.  It's possible
for example that we interrupt malloc or free while that's holding a lock
that's meant to protect against cross-thread interference.  Then, any
attempt to do malloc or free within ereport() will result in a deadlock,
preventing the walreceiver process from exiting in response to SIGTERM.
We hypothesize that this explains some hard-to-reproduce failures seen
in the buildfarm.

Hence, get rid of the immediate-exit code in WalRcvShutdownHandler,
as well as the logic associated with WalRcvImmediateInterruptOK.
Instead, we need to take care that potentially-blocking operations
in the walreceiver's data transmission logic (libpqwalreceiver.c)
will respond reasonably promptly to the process's latch becoming
set and then call ProcessWalRcvInterrupts.  Much of the needed code
for that was already present in libpqwalreceiver.c.  I refactored
things a bit so that all the uses of PQgetResult use latch-aware
waiting, but didn't need to do much more.

These changes should be enough to ensure that libpqwalreceiver.c
will respond promptly to SIGTERM whenever it's waiting to receive
data.  In principle, it could block for a long time while waiting
to send data too, and this patch does nothing to guard against that.
I think that that hazard is mostly theoretical though: such blocking
should occur only if we fill the kernel's data transmission buffers,
and we don't generally send enough data to make that happen without
waiting for input.  If we find out that the hazard isn't just
theoretical, we could fix it by using PQsetnonblocking, but that
would require more ticklish changes than I care to make now.

This is a bug fix, but it seems like too big a change to push into
the back branches without much more testing than there's time for
right now.  Perhaps we'll back-patch once we have more confidence
in the change.

Patch by me; thanks to Thomas Munro for review.

Discussion: https://postgr.es/m/20190416070119.GK2673@paquier.xyz
2019-04-29 12:26:07 -04:00
..
access Allow pg_class xid & multixid horizons to not be set. 2019-04-23 21:42:12 -07:00
bootstrap Update copyright for 2019 2019-01-02 12:44:25 -05:00
catalog Do pre-release housekeeping on catalog data, and fix jsonpath send/recv. 2019-04-28 17:16:50 -04:00
commands Fix tablespace inheritance for partitioned rels 2019-04-25 10:31:32 -04:00
common Fix detection of passwords hashed with MD5 or SCRAM-SHA-256 2019-04-23 15:43:21 +09:00
datatype Update copyright for 2019 2019-01-02 12:44:25 -05:00
executor Fix two memory leaks around force-storing tuples in slots. 2019-04-19 11:39:56 -07:00
fe_utils Unified logging system for command-line programs 2019-04-01 20:01:35 +02:00
foreign Store tuples for EvalPlanQual in slots, rather than as HeapTuples. 2019-03-01 10:37:57 -08:00
jit Renaming for new subscripting mechanism 2019-02-01 12:50:32 -03:00
lib Add IntegerSet, to hold large sets of 64-bit ints efficiently. 2019-03-22 13:21:45 +02:00
libpq Fix detection of passwords hashed with MD5 or SCRAM-SHA-256 2019-04-23 15:43:21 +09:00
mb Update copyright for 2019 2019-01-02 12:44:25 -05:00
nodes Fix tablespace inheritance for partitioned rels 2019-04-25 10:31:32 -04:00
optimizer Use Append rather than MergeAppend for scanning ordered partitions. 2019-04-05 19:20:43 -04:00
parser Avoid order-of-execution problems with ALTER TABLE ADD PRIMARY KEY. 2019-04-23 12:25:27 -04:00
partitioning Use Append rather than MergeAppend for scanning ordered partitions. 2019-04-05 19:20:43 -04:00
port Fix previous MinGW fix. 2019-02-16 15:23:02 +00:00
portability Update copyright for 2019 2019-01-02 12:44:25 -05:00
postmaster Refactor the fsync queue for wider use. 2019-04-04 23:38:38 +13:00
regex Partial implementation of SQL/JSON path language 2019-03-16 12:16:48 +03:00
replication In walreceiver, don't try to do ereport() in a signal handler. 2019-04-29 12:26:07 -04:00
rewrite Update copyright for 2019 2019-01-02 12:44:25 -05:00
snowball Update copyright for 2019 2019-01-02 12:44:25 -05:00
statistics Fix mvdistinct and dependencies size calculations 2019-04-21 20:23:34 +02:00
storage Consistently test for in-use shared memory. 2019-04-12 22:36:38 -07:00
tcop Update copyright for 2019 2019-01-02 12:44:25 -05:00
tsearch Update copyright for 2019 2019-01-02 12:44:25 -05:00
utils Prevent memory leaks associated with relcache rd_partcheck structures. 2019-04-13 13:22:26 -04:00
.gitignore Refactor dlopen() support 2018-09-06 11:33:04 +02:00
Makefile Get rid of jsonpath_gram.h and jsonpath_scanner.h 2019-03-20 11:13:34 +03:00
c.h Add macro to cast away volatile without allowing changes to underlying type 2019-03-25 09:37:03 +01:00
fmgr.h Change function call information to be variable length. 2019-01-26 14:17:52 -08:00
funcapi.h Update copyright for 2019 2019-01-02 12:44:25 -05:00
getaddrinfo.h Update copyright for 2019 2019-01-02 12:44:25 -05:00
getopt_long.h Update copyright for 2019 2019-01-02 12:44:25 -05:00
miscadmin.h Convert [autovacuum_]vacuum_cost_delay into floating-point GUCs. 2019-03-10 15:01:39 -04:00
pg_config.h.in Make use of compiler builtins and/or assembly for CLZ, CTZ, POPCNT. 2019-02-15 23:22:33 -05:00
pg_config.h.win32 Make use of compiler builtins and/or assembly for CLZ, CTZ, POPCNT. 2019-02-15 23:22:33 -05:00
pg_config_ext.h.in
pg_config_ext.h.win32
pg_config_manual.h Update copyright for 2019 2019-01-02 12:44:25 -05:00
pg_getopt.h Use our own getopt() on OpenBSD. 2019-01-18 15:06:26 -05:00
pg_trace.h Update copyright for 2019 2019-01-02 12:44:25 -05:00
pgstat.h Show shared object statistics in pg_stat_database 2019-04-12 14:04:50 +02:00
pgtar.h Update copyright for 2019 2019-01-02 12:44:25 -05:00
pgtime.h Update copyright for 2019 2019-01-02 12:44:25 -05:00
port.h Cygwin and Mingw floating-point fixes. 2019-02-16 01:50:16 +00:00
postgres.h Change function call information to be variable length. 2019-01-26 14:17:52 -08:00
postgres_ext.h
postgres_fe.h Update copyright for 2019 2019-01-02 12:44:25 -05:00
rusagestub.h Update copyright for 2019 2019-01-02 12:44:25 -05:00
windowapi.h Update copyright for 2019 2019-01-02 12:44:25 -05:00