Skip to content

Commit d3111cb

Browse files
committed
Fix correctness issue with computation of FPI size in WAL stats
XLogRecordAssemble() may be called multiple times before inserting a record in XLogInsertRecord(), and the amount of FPIs generated inside a record whose insertion is attempted multiple times may vary. The logic added in f9a09aa touched directly pgWalUsage in XLogRecordAssemble(), meaning that it could be possible for pgWalUsage to be incremented multiple times for a single record. This commit changes the code to use the same logic as the number of FPIs added to a record, where XLogRecordAssemble() returns this information and feeds it to XLogInsertRecord(), updating pgWalUsage only when a record is inserted. Reported-by: Shinya Kato <[email protected]> Discussion: https://postgr.es/m/CAOzEurSiSr+rusd0GzVy8Bt30QwLTK=ugVMnF6=5WhsSrukvvw@mail.gmail.com
1 parent b3ce55f commit d3111cb

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

src/backend/access/transam/xlog.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,7 @@ XLogInsertRecord(XLogRecData *rdata,
749749
XLogRecPtr fpw_lsn,
750750
uint8 flags,
751751
int num_fpi,
752+
uint64 fpi_bytes,
752753
bool topxid_included)
753754
{
754755
XLogCtlInsert *Insert = &XLogCtl->Insert;
@@ -1081,6 +1082,7 @@ XLogInsertRecord(XLogRecData *rdata,
10811082
pgWalUsage.wal_bytes += rechdr->xl_tot_len;
10821083
pgWalUsage.wal_records++;
10831084
pgWalUsage.wal_fpi += num_fpi;
1085+
pgWalUsage.wal_fpi_bytes += fpi_bytes;
10841086

10851087
/* Required for the flush of pending stats WAL data */
10861088
pgstat_report_fixed = true;

src/backend/access/transam/xloginsert.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ static MemoryContext xloginsert_cxt;
139139
static XLogRecData *XLogRecordAssemble(RmgrId rmid, uint8 info,
140140
XLogRecPtr RedoRecPtr, bool doPageWrites,
141141
XLogRecPtr *fpw_lsn, int *num_fpi,
142+
uint64 *fpi_bytes,
142143
bool *topxid_included);
143144
static bool XLogCompressBackupBlock(const PageData *page, uint16 hole_offset,
144145
uint16 hole_length, void *dest, uint16 *dlen);
@@ -512,6 +513,7 @@ XLogInsert(RmgrId rmid, uint8 info)
512513
XLogRecPtr fpw_lsn;
513514
XLogRecData *rdt;
514515
int num_fpi = 0;
516+
uint64 fpi_bytes = 0;
515517

516518
/*
517519
* Get values needed to decide whether to do full-page writes. Since
@@ -521,10 +523,11 @@ XLogInsert(RmgrId rmid, uint8 info)
521523
GetFullPageWriteInfo(&RedoRecPtr, &doPageWrites);
522524

523525
rdt = XLogRecordAssemble(rmid, info, RedoRecPtr, doPageWrites,
524-
&fpw_lsn, &num_fpi, &topxid_included);
526+
&fpw_lsn, &num_fpi, &fpi_bytes,
527+
&topxid_included);
525528

526529
EndPos = XLogInsertRecord(rdt, fpw_lsn, curinsert_flags, num_fpi,
527-
topxid_included);
530+
fpi_bytes, topxid_included);
528531
} while (EndPos == InvalidXLogRecPtr);
529532

530533
XLogResetInsertion();
@@ -562,7 +565,8 @@ XLogSimpleInsertInt64(RmgrId rmid, uint8 info, int64 value)
562565
static XLogRecData *
563566
XLogRecordAssemble(RmgrId rmid, uint8 info,
564567
XLogRecPtr RedoRecPtr, bool doPageWrites,
565-
XLogRecPtr *fpw_lsn, int *num_fpi, bool *topxid_included)
568+
XLogRecPtr *fpw_lsn, int *num_fpi, uint64 *fpi_bytes,
569+
bool *topxid_included)
566570
{
567571
XLogRecData *rdt;
568572
uint64 total_len = 0;
@@ -800,8 +804,7 @@ XLogRecordAssemble(RmgrId rmid, uint8 info,
800804
total_len += bimg.length;
801805

802806
/* Track the WAL full page images in bytes */
803-
pgWalUsage.wal_fpi_bytes += bimg.length;
804-
pgstat_report_fixed = true;
807+
*fpi_bytes += bimg.length;
805808
}
806809

807810
if (needs_data)

src/include/access/xlog.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ extern XLogRecPtr XLogInsertRecord(struct XLogRecData *rdata,
202202
XLogRecPtr fpw_lsn,
203203
uint8 flags,
204204
int num_fpi,
205+
uint64 fpi_bytes,
205206
bool topxid_included);
206207
extern void XLogFlush(XLogRecPtr record);
207208
extern bool XLogBackgroundFlush(void);

0 commit comments

Comments
 (0)