postgresql/src/backend
Michael Paquier 3c3f118d50 Fix snapshot builds during promotion of hot standby node with 2PC
Some specific logic is done at the end of recovery when involving 2PC
transactions:
1) Call RecoverPreparedTransactions(), to recover the state of 2PC
transactions into memory (re-acquire locks, etc.).
2) ShutdownRecoveryTransactionEnvironment(), to move back to normal
operations, mainly cleaning up recovery locks and KnownAssignedXids
(including any 2PC transaction tracked previously).
3) Switch XLogCtl->SharedRecoveryState to RECOVERY_STATE_DONE, which is
the tipping point for any process calling RecoveryInProgress() to check
if the cluster is still in recovery or not.

Any snapshot taken between steps 2) and 3) would be empty, causing any
transaction relying on a snapshot at this point to potentially corrupt
data as there could still be some 2PC transactions to track, with
RecentXmin moving backwards on successive calls to GetSnapshotData() in
the same transaction.

As SharedRecoveryState is the point to take into account to know if it
is safe to discard KnownAssignedXids, this commit moves step 2) after
step 3), so as we can never finish with empty snapshots.

This exists since the introduction of hot standby, so backpatch all the
way down.  The window with incorrect snapshots is extremely small, but I
have seen it when running 023_pitr_prepared_xact.pl, as did buildfarm
member fairywren.  Thomas Munro also found it independently.  Special
thanks to Andres Freund for taking the time to analyze this issue.

Reported-by: Thomas Munro, Michael Paquier
Analyzed-by: Andres Freund
Discussion: https://postgr.es/m/20210422203603.fdnh3fu2mmfp2iov@alap3.anarazel.de
Backpatch-through: 9.6
2021-10-04 14:05:55 +09:00
..
access Fix snapshot builds during promotion of hot standby node with 2PC 2021-10-04 14:05:55 +09:00
bootstrap Revert "Skip WAL for new relfilenodes, under wal_level=minimal." 2020-03-22 09:24:13 -07:00
catalog Fix lookup error in extended stats ownership check 2021-08-31 18:40:09 +02:00
commands Fix misevaluation of STABLE parameters in CALL within plpgsql. 2021-09-21 19:06:33 -04:00
executor Fix usage of "tableoid" in GENERATED expressions. 2021-05-21 15:02:07 -04:00
foreign Update copyright for 2019 2019-01-02 12:44:25 -05:00
jit jit: Do not try to shut down LLVM state in case of LLVM triggered errors. 2021-09-13 18:26:18 -07:00
lib Fix typo in comment 2021-04-20 14:36:44 +02:00
libpq Set type identifier on BIO 2021-08-17 14:31:08 +02:00
main Replace @postgresql.org with @lists.postgresql.org for mailinglists 2019-01-19 19:06:35 +01:00
nodes Ensure that expandTableLikeClause() re-examines the same table. 2020-12-01 14:02:28 -05:00
optimizer Fix pull_varnos to cope with translated PlaceHolderVars. 2021-09-17 15:41:16 -04:00
parser Don't elide casting to typmod -1. 2021-09-20 11:48:52 -04:00
partitioning Avoid using ambiguous word "non-negative" in error messages. 2021-07-28 01:24:24 +09:00
po Translation updates 2021-08-09 12:36:42 +02:00
port Make EXEC_BACKEND more convenient on macOS. 2021-08-13 11:11:06 +12:00
postmaster Revert "Avoid creating archive status ".ready" files too early" 2021-09-04 12:14:30 -04:00
regex Make pg_regexec() robust against out-of-range search_start. 2021-09-11 15:19:54 -04:00
replication Fix issue with WAL archiving in standby. 2021-09-09 23:58:54 +09:00
rewrite Fix rewriter to set hasModifyingCTE correctly on rewritten queries. 2021-09-08 12:05:43 -04:00
snowball Update copyright for 2019 2019-01-02 12:44:25 -05:00
statistics Release memory allocated by dependency_degree 2021-09-23 18:43:05 +02:00
storage Fix variable shadowing in procarray.c. 2021-09-16 13:08:00 +09:00
tcop Fix Portal snapshot tracking to handle subtransactions properly. 2021-10-01 11:10:12 -04:00
tsearch Don't leak compiled regex(es) when an ispell cache entry is dropped. 2021-03-18 21:44:43 -04:00
utils Avoid believing incomplete MCV-only stats in get_variable_range(). 2021-10-01 14:59:35 -04:00
.gitignore Add .gitignore entries for AIX-specific intermediate build artifacts. 2015-07-08 20:44:22 -04:00
common.mk Remove PARTIAL_LINKING build mode. 2018-03-30 17:33:04 -07:00
Makefile Get rid of jsonpath_gram.h and jsonpath_scanner.h 2019-03-20 11:13:34 +03:00
nls.mk Translation updates 2020-08-10 15:21:18 +02:00