postgresql/src/include
Alvaro Herrera f49a80c481 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:48:10 -04:00
..
access Move _bt_upgrademetapage() into critical section. 2018-05-30 19:45:39 +03:00
bootstrap Rationalize handling of array type names in bootstrap data. 2018-04-17 18:29:11 -04:00
catalog Mark binary_upgrade_set_missing_value as parallel_unsafe 2018-06-23 08:43:05 -04:00
commands Post-feature-freeze pgindent run. 2018-04-26 14:47:16 -04:00
common Fix incorrect ordering of operations in pg_resetwal and pg_rewind. 2018-05-23 10:59:55 -04:00
datatype Update copyright for 2018 2018-01-02 23:30:12 -05:00
executor Fix up run-time partition pruning's use of relcache's partition data. 2018-06-13 12:03:26 -04:00
fe_utils Post-feature-freeze pgindent run. 2018-04-26 14:47:16 -04:00
foreign Allow insert and update tuple routing and COPY for foreign tables. 2018-04-06 19:22:03 -04:00
jit Make PGJIT_* macros safer. 2018-04-23 04:48:08 -04:00
lib Fix a boatload of typos in C comments. 2018-04-01 15:01:28 -04:00
libpq Post-feature-freeze pgindent run. 2018-04-26 14:47:16 -04:00
mb Fix a boatload of typos in C comments. 2018-04-01 15:01:28 -04:00
nodes Allow direct lookups of AppendRelInfo by child relid 2018-06-26 10:35:26 -04:00
optimizer Allow direct lookups of AppendRelInfo by child relid 2018-06-26 10:35:26 -04:00
parser Post-feature-freeze pgindent run. 2018-04-26 14:47:16 -04:00
partitioning Consistently use the term 'partitioned rel' in partprune comments 2018-06-20 11:43:01 -04:00
port Remove overzeleous assertions in pg_atomic_flag code. 2018-04-07 18:30:15 -07:00
portability Update copyright for 2018 2018-01-02 23:30:12 -05:00
postmaster Add comment explaining BGWORKER_BYPASS_ALLOWCONN 2018-04-23 10:31:22 +02:00
regex Update copyright for 2018 2018-01-02 23:30:12 -05:00
replication Fix "base" snapshot handling in logical decoding 2018-06-26 16:48:10 -04:00
rewrite Revert MERGE patch 2018-04-12 11:22:56 +01:00
snowball Update copyright for 2018 2018-01-02 23:30:12 -05:00
statistics Update copyright for 2018 2018-01-02 23:30:12 -05:00
storage Remove AELs from subxids correctly on standby 2018-06-16 14:03:29 +01:00
tcop Post-feature-freeze pgindent run. 2018-04-26 14:47:16 -04:00
tsearch Post-feature-freeze pgindent run. 2018-04-26 14:47:16 -04:00
utils Teach SHOW ALL to honor pg_read_all_settings membership 2018-06-08 16:19:05 -04:00
.gitignore Autoconfiscate selection of 64-bit int type for 64-bit large object API. 2012-10-07 21:52:43 -04:00
c.h Support platforms where strtoll/strtoull are spelled __strtoll/__strtoull. 2018-05-19 14:22:18 -04:00
fmgr.h Add expression compilation support to LLVM JIT provider. 2018-03-22 14:45:59 -07:00
funcapi.h Support INOUT arguments in procedures 2018-03-14 12:07:28 -04:00
getaddrinfo.h Update copyright for 2018 2018-01-02 23:30:12 -05:00
getopt_long.h Update copyright for 2018 2018-01-02 23:30:12 -05:00
Makefile Rearrange makefile rules for running Gen_fmgrtab.pl. 2018-05-03 17:54:18 -04:00
miscadmin.h Allow group access on PGDATA 2018-04-07 17:45:39 -04:00
pg_config.h.in Remove configure's check for nonstandard "long long" printf modifiers. 2018-05-23 14:19:04 -04:00
pg_config.h.win32 Stamp 11beta2. 2018-06-25 11:09:49 -04:00
pg_config_ext.h.in Autoconfiscate selection of 64-bit int type for 64-bit large object API. 2012-10-07 21:52:43 -04:00
pg_config_ext.h.win32 Autoconfiscate selection of 64-bit int type for 64-bit large object API. 2012-10-07 21:52:43 -04:00
pg_config_manual.h Update copyright for 2018 2018-01-02 23:30:12 -05:00
pg_getopt.h Update copyright for 2018 2018-01-02 23:30:12 -05:00
pg_trace.h Update copyright for 2018 2018-01-02 23:30:12 -05:00
pgstat.h Revert "Allow on-line enabling and disabling of data checksums" 2018-04-09 19:03:42 +02:00
pgtar.h Update copyright for 2018 2018-01-02 23:30:12 -05:00
pgtime.h Update copyright for 2018 2018-01-02 23:30:12 -05:00
port.h Fix a boatload of typos in C comments. 2018-04-01 15:01:28 -04:00
postgres.h Detoast plpgsql variables if they might live across a transaction boundary. 2018-05-16 14:56:52 -04:00
postgres_ext.h Phase 2 of pgindent updates. 2017-06-21 15:19:25 -04:00
postgres_fe.h Update copyright for 2018 2018-01-02 23:30:12 -05:00
rusagestub.h Update copyright for 2018 2018-01-02 23:30:12 -05:00
windowapi.h Update copyright for 2018 2018-01-02 23:30:12 -05:00