postgresql/src
Andres Freund 79abd23db1 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
..
backend Fix race condition leading to hanging logical slot creation. 2017-05-13 14:21:00 -07:00
bin psql: Add missing translation markers 2017-05-10 10:16:07 -04:00
common Unlink static libraries before rebuilding them. 2015-03-01 13:06:33 -05:00
include Fix race condition leading to hanging logical slot creation. 2017-05-13 14:21:00 -07:00
interfaces Stamp 9.4.12. 2017-05-08 17:19:04 -04:00
makefiles Consolidate makefile code for setting top_srcdir, srcdir and VPATH. 2015-07-30 20:49:22 -04:00
pl Translation updates 2017-05-08 10:15:23 -04:00
port Stamp 9.4.12. 2017-05-08 17:19:04 -04:00
template AIX: Test the -qlonglong option before use. 2015-07-17 03:01:35 -04:00
test Match pg_user_mappings limits to information_schema.user_mapping_options. 2017-05-08 07:24:27 -07:00
timezone Guard against null t->tm_zone in strftime.c. 2017-05-07 12:33:27 -04:00
tools Honor PROVE_FLAGS environment setting 2017-05-12 11:27:56 -04:00
tutorial pgindent run for 9.4 2014-05-06 12:12:18 -04:00
.gitignore Convert cvsignore to gitignore, and add .gitignore for build targets. 2010-09-22 12:57:04 +02:00
bcc32.mak Autoconfiscate selection of 64-bit int type for 64-bit large object API. 2012-10-07 21:52:43 -04:00
DEVELOPERS Replace a couple of references to files that no longer exist in the source 2009-05-04 08:08:47 +00:00
Makefile Install TAP test infrastructure so it's available for extension testing. 2016-09-23 15:50:00 -04:00
Makefile.global.in Honor PROVE_FLAGS environment setting 2017-05-12 11:27:56 -04:00
Makefile.shlib Fix typos in comments. 2017-02-06 11:34:24 +02:00
nls-global.mk nls-global.mk: search build dir for source files, too 2016-06-07 18:55:18 -04:00
win32.mak Autoconfiscate selection of 64-bit int type for 64-bit large object API. 2012-10-07 21:52:43 -04:00