8000 Refactor some repetitive SLRU code · postgres/postgres@c616785 · GitHub
[go: up one dir, main page]

Skip to content

Commit c616785

Browse files
author
Álvaro Herrera
committed
Refactor some repetitive SLRU code
Functions to bootstrap and zero pages in various SLRU callers were fairly duplicative. We can slash almost two hundred lines with a couple of simple helpers: - SimpleLruZeroAndWritePage: Does the equivalent of SimpleLruZeroPage followed by flushing the page to disk - XLogSimpleInsertInt64: Does a XLogBeginInsert followed by XLogInsert of a trivial record whose data is just an int64. Author: Evgeny Voropaev <evgeny.voropaev@tantorlabs.com> Reviewed by: Álvaro Herrera <alvherre@kurilemu.de> Reviewed by: Andrey Borodin <x4mmm@yandex-team.ru> Reviewed by: Aleksander Alekseev <aleksander@timescale.com> Discussion: https://www.postgresql.org/message-id/flat/97820ce8-a1cd-407f-a02b-47368fadb14b%40tantorlabs.com
1 parent 2633dae commit c616785

File tree

8 files changed

+71
-255
lines changed

8 files changed

+71
-255
lines changed

src/backend/access/transam/clog.c

Lines changed: 6 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,7 @@ static SlruCtlData XactCtlData;
110110
#define XactCtl (&XactCtlData)
111111

112112

113-
static int ZeroCLOGPage(int64 pageno, bool writeXlog);
114113
static bool CLOGPagePrecedes(int64 page1, int64 page2);
115-
static void WriteZeroPageXlogRec(int64 pageno);
116114
static void WriteTruncateXlogRec(int64 pageno, TransactionId oldestXact,
117115
Oid oldestXactDb);
118116
static void TransactionIdSetPageStatus(TransactionId xid, int nsubxids,
@@ -832,41 +830,8 @@ check_transaction_buffers(int *newval, void **extra, GucSource source)
832830
void
833831
BootStrapCLOG(void)
834832
{
835-
int slotno;
836-
LWLock *lock = SimpleLruGetBankLock(XactCtl, 0);
837-
838-
LWLockAcquire(lock, LW_EXCLUSIVE);
839-
840-
/* Create and zero the first page of the commit log */
841-
slotno = ZeroCLOGPage(0, false);
842-
843-
/* Make sure it's written out */
844-
SimpleLruWritePage(XactCtl, slotno);
845-
Assert(!XactCtl->shared->page_dirty[slotno]);
846-
847-
LWLockRelease(lock);
848-
}
849-
850-
/*
851-
* Initialize (or reinitialize) a page of CLOG to zeroes.
852-
* If writeXlog is true, also emit an XLOG record saying we did this.
853-
*
854-
* The page is not actually written, just set up in shared memory.
855-
* The slot number of the new page is returned.
856-
*
857-
* Control lock must be held at entry, and will be held at exit.
858-
*/
859-
static int
860-
ZeroCLOGPage(int64 pageno, bool writeXlog)
861-
{
862-
int slotno;
863-
864-
slotno = SimpleLruZeroPage(XactCtl, pageno);
865-
866-
if (writeXlog)
867-
WriteZeroPageXlogRec(pageno);
868-
869-
return slotno;
833+
/* Zero the initial page and flush it to disk */
834+
SimpleLruZeroAndWritePage(XactCtl, 0);
870835
}
871836

872837
/*
@@ -974,8 +939,9 @@ ExtendCLOG(TransactionId newestXact)
974939

975940
LWLockAcquire(lock, LW_EXCLUSIVE);
976941

977-
/* Zero the page and make an XLOG entry about it */
978-
ZeroCLOGPage(pageno, true);
942+
/* Zero the page and make a WAL entry about it */
943+
SimpleLruZeroPage(XactCtl, pageno);
944+
XLogSimpleInsertInt64(RM_CLOG_ID, CLOG_ZEROPAGE, pageno);
979945

980946
LWLockRelease(lock);
981947
}
@@ -1067,17 +1033,6 @@ CLOGPagePrecedes(int64 page1, int64 page2)
10671033
}
10681034

10691035

1070-
/*
1071-
* Write a ZEROPAGE xlog record
1072-
*/
1073-
static void
1074-
WriteZeroPageXlogRec(int64 pageno)
1075-
{
1076-
XLogBeginInsert();
1077-
XLogRegisterData(&pageno, sizeof(pageno));
1078-
(void) XLogInsert(RM_CLOG_ID, CLOG_ZEROPAGE);
1079-
}
1080-
10811036
/*
10821037
* Write a TRUNCATE xlog record
10831038
*
@@ -1114,19 +1069,9 @@ clog_redo(XLogReaderState *record)
11141069
if (info == CLOG_ZEROPAGE)
11151070
{
11161071
int64 pageno;
1117-
int slotno;
1118-
LWLock *lock;
11191072

11201073
memcpy(&pageno, XLogRecGetData(record), sizeof(pageno));
1121-
1122-
lock = SimpleLruGetBankLock(XactCtl, pageno);
1123-
LWLockAcquire(lock, LW_EXCLUSIVE);
1124-
1125-
slotno = ZeroCLOGPage(pageno, false);
1126-
SimpleLruWritePage(XactCtl, slotno);
1127-
Assert(!XactCtl->shared->page_dirty[slotno]);
1128-
1129-
LWLockRelease(lock);
1074+
SimpleLruZeroAndWritePage(XactCtl, pageno);
11301075
}
11311076
else if (info == CLOG_TRUNCATE)
11321077
{

src/backend/access/transam/commit_ts.c

Lines changed: 8 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,9 @@ static void SetXidCommitTsInPage(TransactionId xid, int nsubxids,
114114
static void TransactionIdSetCommitTs(TransactionId xid, TimestampTz ts,
115115
RepOriginId nodeid, int slotno);
116116
static void error_commit_ts_disabled(void);
117-
static int ZeroCommitTsPage(int64 pageno, bool writeXlog);
118117
static bool CommitTsPagePrecedes(int64 page1, int64 page2);
119118
static void ActivateCommitTs(void);
120119
static void DeactivateCommitTs(void);
121-
static void WriteZeroPageXlogRec(int64 pageno);
122120
static void WriteTruncateXlogRec(int64 pageno, TransactionId oldestXid);
123121

124122
/*
@@ -602,28 +600,6 @@ BootStrapCommitTs(void)
602600
*/
603601
}
604602

605-
/*
606-
* Initialize (or reinitialize) a page of CommitTs to zeroes.
607-
* If writeXlog is true, also emit an XLOG record saying we did this.
608-
*
609-
* The page is not actually written, just set up in shared memory.
610-
* The slot number of the new page is returned.
611-
*
612-
* Control lock must be held at entry, and will be held at exit.
613-
*/
614-
static int
615-
ZeroCommitTsPage(int64 pageno, bool writeXlog)
616-
{
617-
int slotno;
618-
619-
slotno = SimpleLruZeroPage(CommitTsCtl, pageno);
620-
621-
if (writeXlog)
622-
WriteZeroPageXlogRec(pageno);
623-
624-
return slotno;
625-
}
626-
627603
/*
628604
* This must be called ONCE during postmaster or standalone-backend startup,
629605
* after StartupXLOG has initialized TransamVariables->nextXid.
@@ -754,16 +730,7 @@ ActivateCommitTs(void)
754730

755731
/* Create the current segment file, if necessary */
756732
if (!SimpleLruDoesPhysicalPageExist(CommitTsCtl, pageno))
757-
{
758-
LWLock *lock = SimpleLruGetBankLock(CommitTsCtl, pageno);
759-
int slotno;
760-
761-
LWLockAcquire(lock, LW_EXCLUSIVE);
762-
slotno = ZeroCommitTsPage(pageno, false);
763-
SimpleLruWritePage(CommitTsCtl, slotno);
764-
Assert(!CommitTsCtl->shared->page_dirty[slotno]);
765-
LWLockRelease(lock);
766-
}
733+
SimpleLruZeroAndWritePage(CommitTsCtl, pageno);
767734

768735
/* Change the activation status in shared memory. */
769736
LWLockAcquire(CommitTsLock, LW_EXCLUSIVE);
@@ -874,8 +841,12 @@ ExtendCommitTs(TransactionId newestXact)
874841

875842
LWLockAcquire(lock, LW_EXCLUSIVE);
876843

877-
/* Zero the page and make an XLOG entry about it */
878-
ZeroCommitTsPage(pageno, !InRecovery);
844+
/* Zero the page ... */
845+
SimpleLruZeroPage(CommitTsCtl, pageno);
846+
847+
/* and make a WAL entry about that, unless we're in REDO */
848+
if (!InRecovery)
849+
XLogSimpleInsertInt64(RM_COMMIT_TS_ID, COMMIT_TS_ZEROPAGE, pageno);
879850

880851
LWLockRelease(lock);
881852
}
@@ -989,17 +960,6 @@ CommitTsPagePrecedes(int64 page1, int64 page2)
989960
}
990961

991962

992-
/*
993-
* Write a ZEROPAGE xlog record
994-
*/
995-
static void
996-
WriteZeroPageXlogRec(int64 pageno)
997-
{
998-
XLogBeginInsert();
999-
XLogRegisterData(&pageno, sizeof(pageno));
1000-
(void) XLogInsert(RM_COMMIT_TS_ID, COMMIT_TS_ZEROPAGE);
1001-
}
1002-
1003963
/*
1004964
* Write a TRUNCATE xlog record
1005965
*/
@@ -1030,19 +990,9 @@ commit_ts_redo(XLogReaderState *record)
1030990
if (info == COMMIT_TS_ZEROPAGE)
1031991
{
1032992
int64 pageno;
1033-
int slotno;
1034-
LWLock *lock;
1035993

1036994
memcpy(&pageno, XLogRecGetData(record), sizeof(pageno));
1037-
1038-
lock = SimpleLruGetBankLock(CommitTsCtl, pageno);
1039-
LWLockAcquire(lock, LW_EXCLUSIVE);
1040-
1041-
slotno = ZeroCommitTsPage(pageno, false);
1042-
SimpleLruWritePage(CommitTsCtl, slotno);
1043-
Assert(!CommitTsCtl->shared->page_dirty[slotno]);
1044-
1045-
LWLockRelease(lock);
995+
SimpleLruZeroAndWritePage(CommitTsCtl, pageno);
1046996
}
1047997
else if (info == COMMIT_TS_TRUNCATE)
1048998
{

0 commit comments

Comments
 (0)
0