Skip to content

Commit 9e5fe4d

Browse files
Fix timing of Startup CLOG and MultiXact during Hot Standby
Patch by me, bug report by Chris Redekop, analysis by Florian Pflug
1 parent 5e4dd5f commit 9e5fe4d

File tree

4 files changed

+33
-6
lines changed

4 files changed

+33
-6
lines changed

src/backend/access/transam/clog.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,25 @@ StartupCLOG(void)
490490
*/
491491
ClogCtl->shared->latest_page_number = pageno;
492492

493+
LWLockRelease(CLogControlLock);
494+
}
495+
496+
/*
497+
* This must be called ONCE at the end of startup/recovery.
498+
*/
499+
void
500+
TrimCLOG(void)
501+
{
502+
TransactionId xid = ShmemVariableCache->nextXid;
503+
int pageno = TransactionIdToPage(xid);
504+
505+
LWLockAcquire(CLogControlLock, LW_EXCLUSIVE);
506+
507+
/*
508+
* Re-Initialize our idea of the latest page number.
509+
*/
510+
ClogCtl->shared->latest_page_number = pageno;
511+
493512
/*
494513
* Zero out the remainder of the current clog page. Under normal
495514
* circumstances it should be zeroes already, but it seems at least

src/backend/access/transam/multixact.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1568,7 +1568,7 @@ StartupMultiXact(void)
15681568

15691569
/*
15701570
* Zero out the remainder of the current members page. See notes in
1571-
* StartupCLOG() for motivation.
1571+
* TrimCLOG() for motivation.
15721572
*/
15731573
entryno = MXOffsetToMemberEntry(offset);
15741574
if (entryno != 0)

src/backend/access/transam/xlog.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6384,10 +6384,12 @@ StartupXLOG(void)
63846384
oldestActiveXID = checkPoint.oldestActiveXid;
63856385
Assert(TransactionIdIsValid(oldestActiveXID));
63866386

6387-
/* Startup commit log and related stuff */
6387+
/*
6388+
* Startup commit log and subtrans only. Other SLRUs are not
6389+
* maintained during recovery and need not be started yet.
6390+
*/
63886391
StartupCLOG();
63896392
StartupSUBTRANS(oldestActiveXID);
6390-
StartupMultiXact();
63916393

63926394
/*
63936395
* If we're beginning at a shutdown checkpoint, we know that
@@ -6876,16 +6878,21 @@ StartupXLOG(void)
68766878
TransactionIdRetreat(ShmemVariableCache->latestCompletedXid);
68776879

68786880
/*
6879-
* Start up the commit log and related stuff, too. In hot standby mode we
6880-
* did this already before WAL replay.
6881+
* Start up the commit log and subtrans, if not already done for
6882+
* hot standby.
68816883
*/
68826884
if (standbyState == STANDBY_DISABLED)
68836885
{
68846886
StartupCLOG();
68856887
StartupSUBTRANS(oldestActiveXID);
6886-
StartupMultiXact();
68876888
}
68886889

6890+
/*
6891+
* Perform end of recovery actions for any SLRUs that need it.
6892+
*/
6893+
StartupMultiXact();
6894+
TrimCLOG();
6895+
68896896
/* Reload shared-memory state for prepared transactions */
68906897
RecoverPreparedTransactions();
68916898

src/include/access/clog.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ extern Size CLOGShmemSize(void);
4040
extern void CLOGShmemInit(void);
4141
extern void BootStrapCLOG(void);
4242
extern void StartupCLOG(void);
43+
extern void TrimCLOG(void);
4344
extern void ShutdownCLOG(void);
4445
extern void CheckPointCLOG(void);
4546
extern void ExtendCLOG(TransactionId newestXact);

0 commit comments

Comments
 (0)