postgresql/src/backend
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
..
access Fix recovery of 2PC transaction during crash recovery 2023-07-18 13:43:44 +09:00
archive Redesign archive modules 2023-02-17 14:26:42 +09:00
backup Message wording improvements 2023-07-10 10:47:24 +02:00
bootstrap Fix CREATE INDEX progress reporting for multi-level partitioning. 2023-03-25 15:34:03 -04:00
catalog Remove ancient special case code for dropping oid columns 2023-07-12 16:13:50 +02:00
commands Fix privilege check for SET SESSION AUTHORIZATION. 2023-07-13 21:13:45 -07:00
executor Allow the use of a hash index on the subscriber during replication. 2023-07-14 08:21:54 +05:30
foreign Expand some more uses of "deleg" to "delegation" or "delegated". 2023-05-21 10:55:18 -04:00
jit Remove duplicated assignment of LLVMJitHandle->lljit 2023-07-13 16:44:17 +09:00
lib Fix ILIST_DEBUG build 2023-01-18 10:26:15 -08:00
libpq Remove db_user_namespace. 2023-07-17 11:44:59 -07:00
main Remove obsolete defense against strxfrm() bugs. 2023-04-20 13:20:14 +12:00
nodes Don't include CaseTestExpr in JsonValueExpr.formatted_expr 2023-07-13 12:13:58 +09:00
optimizer Allow plan nodes with initPlans to be considered parallel-safe. 2023-07-14 11:41:20 -04:00
parser Add missing initializations of p_perminfo 2023-07-14 14:56:35 +09:00
partitioning Pre-beta mechanical code beautification. 2023-05-19 17:24:48 -04:00
po Translation updates 2023-06-26 12:02:02 +02:00
port Add GUC parameter "huge_pages_status" 2023-07-06 14:42:36 +09:00
postmaster Remove db_user_namespace. 2023-07-17 11:44:59 -07:00
regex All supported systems have locale_t. 2023-07-09 11:55:18 +12:00
replication Allow the use of a hash index on the subscriber during replication. 2023-07-14 08:21:54 +05:30
rewrite A minor simplification for List manipulation 2023-07-03 11:39:03 +02:00
snowball Fix the install rule for snowball_create.sql. 2023-05-23 11:15:57 -04:00
statistics Take pg_attribute out of VacAttrStats 2023-07-03 07:18:57 +02:00
storage Remove wal_sync_method=fsync_writethrough on Windows. 2023-07-14 12:30:13 +12:00
tcop Be more rigorous about local variables in PostgresMain(). 2023-07-10 12:14:34 -04:00
tsearch Take pg_attribute out of VacAttrStats 2023-07-03 07:18:57 +02:00
utils Remove db_user_namespace. 2023-07-17 11:44:59 -07:00
.gitignore Add .gitignore entries for AIX-specific intermediate build artifacts. 2015-07-08 20:44:22 -04:00
common.mk Blind attempt to fix LLVM dependency in the backend 2022-09-15 10:53:48 +07:00
Makefile Generate automatically code and documentation related to wait events 2023-07-05 10:53:11 +09:00
meson.build Add win32ver data to meson-built postgres.exe. 2023-06-12 07:40:38 -07:00
nls.mk Add missing gettext triggers 2023-05-10 13:51:51 +02:00