postgresql/src/include
Andres Freund 955a684e04 Fix race condition leading to hanging logical slot creation.
The snapshot assembly during the creation of logical slots relied
waiting for transactions in xl_running_xacts to end, by checking for
their commit/abort records.  Unfortunately, despite locking, it is
possible to see an xl_running_xact record listing transactions as
ready, that have already WAL-logged an commit/abort record, as the
locking just prevents the ProcArray to be adjusted, and the commit
record has to be logged first.

That lead to either delayed or hanging snapshot creation, because
snapbuild.c would wait "forever" to see commit/abort records for some
transactions.  That hang resolved only if a xl_running_xacts record
without any running transactions happened to be logged, far from
certain on a busy server.

It's impractical to prevent that via more heavyweight locking, the
likelihood of deadlocks and significantly increased contention would
be too big.

Instead change the initial snapshot creation to be solely based on
tracking the oldest running transaction via
xl_running_xacts->oldestRunningXid - that actually ends up
significantly simplifying the code.  That has two disadvantages:
1) Because we cannot fully "trust" the contents of xl_running_xacts,
   we cannot use it to build the initial snapshot.  Instead we have to
   wait twice for all running transactions to finish.
2) Previously a slot, unless the race occurred, could be created when
   the all transaction perceived as running based on commit/abort
   records, now we have to wait for the next xl_running_xacts record.
To address that, trigger logging new xl_running_xacts record from
within snapbuild.c exactly when necessary.

Unfortunately snabuild.c's SnapBuild is stored on disk, one of the
stupider ideas of a certain Mr Freund, so we can't change it in a
minor release.  As this is going to be backpatched, we have to hack
around a bit to keep on-disk compatibility.  A later commit will
rejigger that on master.

Author: Andres Freund, based on a quite different patch from Petr Jelinek
Analyzed-By: Petr Jelinek
Reviewed-By: Petr Jelinek
Discussion: https://postgr.es/m/f37e975c-908f-858e-707f-058d3b1eb214@2ndquadrant.com
Backpatch: 9.4-, where logical decoding has been introduced
2017-05-13 14:21:00 -07:00
..
access Standardize "WAL location" terminology 2017-05-12 13:51:27 -04:00
bootstrap Update copyright via script for 2017 2017-01-03 13:48:53 -05:00
catalog Teach \d+ to show partitioning constraints. 2017-05-13 12:04:53 -04:00
commands Identity columns 2017-04-06 08:41:37 -04:00
common Make SCRAM salts and nonces longer. 2017-05-05 10:02:13 +03:00
datatype Consistently declare timestamp variables as TimestampTz. 2017-02-23 15:57:08 -05:00
executor Provide an error cursor for "can't call an SRF here" errors. 2017-04-18 13:21:08 -04:00
fe_utils Allow psql variable substitution to occur in backtick command strings. 2017-04-01 21:44:54 -04:00
foreign Allow custom and foreign scans to have shutdown callbacks. 2017-02-26 13:41:12 +05:30
lib Revert "Permit dump/reload of not-too-large >1GB tuples" 2017-05-10 18:41:27 -03:00
libpq Remove support for password_encryption='off' / 'plain'. 2017-05-08 11:26:07 +03:00
mb ICU support 2017-03-23 15:28:48 -04:00
nodes Fix multi-column range partitioning constraints. 2017-05-13 11:36:41 -04:00
optimizer Reduce semijoins with unique inner relations to plain inner joins. 2017-05-01 14:53:42 -04:00
parser Rework the options syntax for logical replication commands 2017-05-12 08:57:49 -04:00
port Remove symbol WIN32_ONLY_COMPILER 2017-04-11 15:22:21 +02:00
portability Update copyright via script for 2017 2017-01-03 13:48:53 -05:00
postmaster Add an Assert() to max_parallel_workers enforcement. 2017-04-11 13:03:44 -04:00
regex Document intentional violations of header inclusion policy. 2017-03-08 17:01:13 -05:00
replication Fix race condition leading to hanging logical slot creation. 2017-05-13 14:21:00 -07:00
rewrite Remove deprecated COMMENT ON RULE syntax 2017-02-23 08:19:52 -05:00
snowball Document intentional violations of header inclusion policy. 2017-03-08 17:01:13 -05:00
statistics Collect and use multi-column dependency stats 2017-04-05 18:00:42 -04:00
storage Preserve required !catalog tuples while computing initial decoding snapshot. 2017-04-27 13:13:36 -07:00
tcop Remove dead code and fix comments in fast-path function handling. 2017-04-06 09:09:39 +03:00
tsearch Full Text Search support for json and jsonb 2017-03-31 14:26:03 -04:00
utils Redesign get_attstatsslot()/free_attstatsslot() for more safety and speed. 2017-05-13 15:14:39 -04:00
.gitignore
c.h Recommend wrappers of PG_DETOAST_DATUM_PACKED(). 2017-03-12 19:35:33 -04:00
fmgr.h Avoid passing function pointers across process boundaries. 2017-04-14 23:50:16 -04:00
funcapi.h Update copyright via script for 2017 2017-01-03 13:48:53 -05:00
getaddrinfo.h Remove symbol WIN32_ONLY_COMPILER 2017-04-11 15:22:21 +02:00
getopt_long.h Update copyright via script for 2017 2017-01-03 13:48:53 -05:00
Makefile Generate fmgr prototypes automatically 2017-01-17 14:06:07 -05:00
miscadmin.h Update copyright via script for 2017 2017-01-03 13:48:53 -05:00
pg_config_ext.h.in
pg_config_ext.h.win32
pg_config_manual.h Fix default minimum value for descending sequences 2017-01-23 14:00:58 -05:00
pg_config.h.in Revert "Use pselect(2) not select(2), if available, to wait in postmaster's loop." 2017-04-24 18:29:03 -04:00
pg_config.h.win32 Revert "Use pselect(2) not select(2), if available, to wait in postmaster's loop." 2017-04-24 18:29:03 -04:00
pg_getopt.h Update copyright via script for 2017 2017-01-03 13:48:53 -05:00
pg_trace.h Update copyright via script for 2017 2017-01-03 13:48:53 -05:00
pgstat.h Add ProcArrayGroupUpdate wait event. 2017-04-07 13:41:47 -04:00
pgtar.h Update copyright via script for 2017 2017-01-03 13:48:53 -05:00
pgtime.h Fix incorrect comment: pgtime's tm_mon is 1-based, not 0-based. 2017-01-24 09:36:17 -05:00
port.h Run the postmaster's signal handlers without SA_RESTART. 2017-04-24 13:00:30 -04:00
postgres_ext.h Move atooid() definition to a central place 2017-03-01 11:55:28 -05:00
postgres_fe.h Update copyright via script for 2017 2017-01-03 13:48:53 -05:00
postgres.h Recommend wrappers of PG_DETOAST_DATUM_PACKED(). 2017-03-12 19:35:33 -04:00
rusagestub.h Update copyright via script for 2017 2017-01-03 13:48:53 -05:00
windowapi.h Update copyright via script for 2017 2017-01-03 13:48:53 -05:00