postgresql/src
Michael Paquier cb0cca1880 Fix recovery of 2PC transaction during crash recovery
A crash in the middle of a checkpoint with some two-phase state data
already flushed to disk by this checkpoint could cause a follow-up crash
recovery to recover twice the same transaction, once from what has been
found in pg_twophase/ at the beginning of recovery and a second time
when replaying its corresponding record.

This would lead to FATAL failures in the startup process during
recovery, where the same transaction would have a state recovered twice
instead of once:
LOG:  recovering prepared transaction 731 from shared memory
LOG:  recovering prepared transaction 731 from shared memory
FATAL:  lock ExclusiveLock on object 731/0/0 is already held

This issue is fixed by skipping the addition of any 2PC state coming
from a record whose equivalent 2PC state file has already been loaded in
TwoPhaseState at the beginning of recovery by restoreTwoPhaseData(),
which is OK as long as the system has not reached a consistent state.

The timing to get a messed up recovery processing is very racy, and
would very unlikely happen.  The thread that has reported the issue has
demonstrated the bug using injection points to force a PANIC in the
middle of a checkpoint.

Issue introduced in 728bd99, so backpatch all the way down.

Reported-by: "suyu.cmj" <mengjuan.cmj@alibaba-inc.com>
Author: "suyu.cmj" <mengjuan.cmj@alibaba-inc.com>
Author: Michael Paquier
Discussion: https://postgr.es/m/109e6994-b971-48cb-84f6-829646f18b4c.mengjuan.cmj@alibaba-inc.com
Backpatch-through: 11
2023-07-18 13:43:44 +09:00
..
backend Fix recovery of 2PC transaction during crash recovery 2023-07-18 13:43:44 +09:00
bin Simplify option handling in pg_ctl. 2023-07-14 12:35:54 -07:00
common Generate automatically code and documentation related to wait events 2023-07-05 10:53:11 +09:00
fe_utils Handle \v as a whitespace character in parsers 2023-07-06 08:16:24 +09:00
include Remove db_user_namespace. 2023-07-17 11:44:59 -07:00
interfaces Include <limits.h> in fe-auth.c, to get CHAR_BIT reliably. 2023-07-17 16:54:54 -04:00
makefiles Remove --disable-thread-safety and related code. 2023-07-12 08:20:43 +12:00
pl Translation updates 2023-06-26 12:02:02 +02:00
port Teach in-tree getopt_long() to move non-options to the end of argv. 2023-07-12 20:34:39 -07:00
template Use unnamed POSIX semaphores on Cygwin. 2023-01-06 10:33:28 +13:00
test Allow plan nodes with initPlans to be considered parallel-safe. 2023-07-14 11:41:20 -04:00
timezone Pre-beta mechanical code beautification. 2023-05-19 17:24:48 -04:00
tools pg_bsd_indent: Collect test diffs in test.diffs instead of tests.diff 2023-07-12 16:26:02 -07:00
tutorial Pre-beta mechanical code beautification. 2023-05-19 17:24:48 -04:00
.gitignore
DEVELOPERS
Makefile Integrate pg_bsd_indent into our build/test infrastructure. 2023-02-12 12:22:21 -05:00
Makefile.global.in Remove --disable-thread-safety and related code. 2023-07-12 08:20:43 +12:00
Makefile.shlib autoconf: Rely on ar supporting index creation 2022-10-07 11:53:39 -07:00
meson.build Integrate pg_bsd_indent into our build/test infrastructure. 2023-02-12 12:22:21 -05:00
nls-global.mk Fix for make unportability 2022-07-13 09:15:01 +02:00