The same 2PC data maybe recovered twice
suyu.cmj <mengjuan.cmj@alibaba-inc.com>
From: 蔡梦娟(玊于) <mengjuan.cmj@alibaba-inc.com>
To: "pgsql-hackers" <pgsql-hackers@postgresql.org>
Cc: "pgsql-bugs" <pgsql-bugs@postgresql.org>
Date: 2023-07-06T02:02:15Z
Lists: pgsql-bugs, pgsql-hackers
Hi, all. I want to report a bug about recovery of 2pc data, in current implementation of crash recovery, there are two ways to recover 2pc data: 1、before redo, func restoreTwoPhaseData() will restore 2pc data those xid < ShmemVariableCache->nextXid, which is initialized from checkPoint.nextXid; 2、during redo, func xact_redo() will add 2pc from wal; The following scenario may cause the same 2pc to be added repeatedly: 1、start creating checkpoint_1, checkpoint_1.redo is set as curInsert; 2、before set checkPoint_1.nextXid, a new 2pc is prepared, suppose the xid of this 2pc is 100, and then ShmemVariableCache->nextXid will be advanced as 101; 3、checkPoint_1.nextXid is set as 101; 4、in CheckPointTwoPhase() of checkpoint_1, 2pc_100 won't be copied to disk because its prepare_end_lsn > checkpoint_1.redo; 5、checkPoint_1 is finished, after checkpoint_timeout, start creating checkpoint_2; 6、during checkpoint_2, data of 2pc_100 will be copied to disk; 7、before UpdateControlFile() of checkpoint_2, crash happened; 8、during crash recovery, redo will start from checkpoint_1, and 2pc_100 will be restored first by restoreTwoPhaseData() because xid_100 < checkPoint_1.nextXid, which is 101; 9、because prepare_start_lsn of 2pc_100 > checkpoint_1.redo, 2pc_100 will be added again by xact_redo() during wal replay, resulting in the same 2pc data being added twice; 10、In RecoverPreparedTransactions() -> lock_twophase_recover(), lock the same 2pc will cause panic. Is the above scenario reasonable, and do you have any good ideas for fixing this bug? Thanks & Best Regard
Commits
Same data as JSON:
GET /api/v1/messages/:b64id/commits
the thread's linked commits as JSON, with link sources.
API reference →
-
Fix recovery of 2PC transaction during crash recovery
- bc0581f8fb8b 11.21 landed
- d2ee542a2bbe 12.16 landed
- db59108a2b67 13.12 landed
- 442749100d34 14.9 landed
- a878eff6b4b9 15.4 landed
- f88bc9f38839 16.0 landed
- cb0cca188072 17.0 landed