Skip to content

Commit 2f55c53

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 7f797d2 commit 2f55c53

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
@@ -6067,10 +6067,12 @@ StartupXLOG(void)
60676067
oldestActiveXID = checkPoint.oldestActiveXid;
60686068
Assert(TransactionIdIsValid(oldestActiveXID));
60696069

6070-
/* Startup commit log and related stuff */
6070+
/*
6071+
* Startup commit log and subtrans only. Other SLRUs are not
6072+
* maintained during recovery and need not be started yet.
6073+
*/
60716074
StartupCLOG();
60726075
StartupSUBTRANS(oldestActiveXID);
6073-
StartupMultiXact();
60746076

60756077
/*
60766078
* If we're beginning at a shutdown checkpoint, we know that
@@ -6530,16 +6532,21 @@ StartupXLOG(void)
65306532
TransactionIdRetreat(ShmemVariableCache->latestCompletedXid);
65316533

65326534
/*
6533-
* Start up the commit log and related stuff, too. In hot standby mode we
6534-
* did this already before WAL replay.
6535+
* Start up the commit log and subtrans, if not already done for
6536+
* hot standby.
65356537
*/
65366538
if (standbyState == STANDBY_DISABLED)
65376539
{
65386540
StartupCLOG();
65396541
StartupSUBTRANS(oldestActiveXID);
6540-
StartupMultiXact();
65416542
}
65426543

6544+
/*
6545+
* Perform end of recovery actions for any SLRUs that need it.
6546+
*/
6547+
StartupMultiXact();
6548+
TrimCLOG();
6549+
65436550
/* Reload shared-memory state for prepared transactions */
65446551
RecoverPreparedTransactions();
65456552

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)