postgresql/src
Alvaro Herrera da10d6a8a9 Fix "base" snapshot handling in logical decoding
Two closely related bugs are fixed.  First, xmin of logical slots was
advanced too early.  During xl_running_xacts processing, xmin of the
slot was set to the oldest running xid in the record, but that's wrong:
actually, snapshots which will be used for not-yet-replayed transactions
might consider older txns as running too, so we need to keep xmin back
for them.  The problem wasn't noticed earlier because DDL which allows
to delete tuple (set xmax) while some another not-yet-committed
transaction looks at it is pretty rare, if not unique: e.g. all forms of
ALTER TABLE which change schema acquire ACCESS EXCLUSIVE lock
conflicting with any inserts. The included test case (test_decoding's
oldest_xmin) uses ALTER of a composite type, which doesn't have such
interlocking.

To deal with this, we must be able to quickly retrieve oldest xmin
(oldest running xid among all assigned snapshots) from ReorderBuffer. To
fix, add another list of ReorderBufferTXNs to the reorderbuffer, where
transactions are sorted by base-snapshot-LSN.  This is slightly
different from the existing (sorted by first-LSN) list, because a
transaction can have an earlier LSN but a later Xmin, if its first
record does not obtain an xmin (eg. xl_xact_assignment).  Note this new
list doesn't fully replace the existing txn list: we still need that one
to prevent WAL recycling.

The second issue concerns SnapBuilder snapshots and subtransactions.
SnapBuildDistributeNewCatalogSnapshot never assigned a snapshot to a
transaction that is known to be a subtxn, which is good in the common
case that the top-level transaction already has one (no point in doing
so), but a bug otherwise.  To fix, arrange to transfer the snapshot from
the subtxn to its top-level txn as soon as the kinship gets known.
test_decoding's snapshot_transfer verifies this.

Also, fix a minor memory leak: refcount of toplevel's old base snapshot
was not decremented when the snapshot is transferred from child.

Liberally sprinkle code comments, and rewrite a few existing ones.  This
part is my (Álvaro's) contribution to this commit, as I had to write all
those comments in order to understand the existing code and Arseny's
patch.

Reported-by: Arseny Sher <a.sher@postgrespro.ru>
Diagnosed-by: Arseny Sher <a.sher@postgrespro.ru>
Co-authored-by: Arseny Sher <a.sher@postgrespro.ru>
Co-authored-by: Álvaro Herrera <alvherre@alvh.no-ip.org>
Reviewed-by: Antonin Houska <ah@cybertec.at>
Discussion: https://postgr.es/m/87lgdyz1wj.fsf@ars-thinkpad
2018-06-26 16:38:34 -04:00
..
backend Fix "base" snapshot handling in logical decoding 2018-06-26 16:38:34 -04:00
bin Address set of issues with errno handling 2018-06-25 11:20:50 +09:00
common Fix error message on short read of pg_control 2018-05-18 17:53:15 +02:00
fe_utils Empty search_path in Autovacuum and non-psql/pgbench clients. 2018-02-26 07:39:48 -08:00
include Fix "base" snapshot handling in logical decoding 2018-06-26 16:38:34 -04:00
interfaces Add PGTYPESchar_free() to avoid cross-module problems on Windows. 2018-06-26 20:54:09 +12:00
makefiles Always use -fPIC, not -fpic, when building shared libraries with gcc. 2017-06-01 13:32:56 -04:00
pl Fix misidentification of SQL statement type in plpgsql's exec_stmt_execsql. 2018-05-25 14:31:07 -04:00
port Fix simple_prompt() to disable echo on Windows when stdin != terminal. 2018-05-23 19:04:34 -04:00
template Remove some remains from Alpha support removal 2015-10-29 16:40:14 -04:00
test Properly schema-qualify additional object types in getObjectDescription(). 2018-05-24 12:07:41 -04:00
timezone Update time zone data files to tzdata release 2018e. 2018-05-09 13:55:48 -04:00
tools Clear severity 5 perlcritic warnings from vcregress.pl 2018-05-06 07:39:37 -04:00
tutorial Update copyright for 2016 2016-01-02 13:33:40 -05: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 PL/Perl portability fix: absorb relevant -D switches from Perl. 2017-07-31 12:38:35 -04:00
Makefile.shlib Fix make rules that generate multiple output files. 2018-03-23 13:45:38 -04: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