Skip to content

Commit 5721e54

Browse files
committed
Revert "Add redo LSN to pgstats files"
This reverts commit b860848, that was added as a prerequisite for the support of pgstats data flush across checkpoints, linking a pgstats file to a specific checkpoint redo LSN. As reported, this is proving to be currently problematic when going through a pg_upgrade, that does direct manipulations of the control file in the new cluster. The LSN stored in the pgstats file is not able to cope with any changes done in the control file by pg_upgrade yet, causing the pgstats file to be discarded when starting the new cluster after overriding its redo LSN (one is a `pg_resetwal -l` where the new cluster's start LSN is bumped by a hardcoded value of 8 segments, see copy_xact_xlog_xid). The least painful path going forward is likely going to be a refactor of the pgstats code so as it is possible to read and write some of its data with some routines in src/common/, so as pg_upgrade or pg_resetwal are able to update its data. The main point is that we are going to need a LSN in the stats file should we make it written at checkpoint time and not only as part of a shutdown sequence. It is too late to dive into these details for v18, so let's revert the change, and let's try to figure out all the details in the next release cycle. The pgstats file is currently only written as part of a shutdown sequence, and its contents are still lost on crash, same as older releases. Bump PGSTAT_FILE_FORMAT_ID. Reported-by: Tom Lane <tgl@sss.pgh.pa.us> Discussion: https://postgr.es/m/2563883.1741826489@sss.pgh.pa.us
1 parent cd3c451 commit 5721e54

File tree

3 files changed

+12
-36
lines changed

3 files changed

+12
-36
lines changed

src/backend/access/transam/xlog.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5728,7 +5728,7 @@ StartupXLOG(void)
57285728
if (didCrash)
57295729
pgstat_discard_stats();
57305730
else
5731-
pgstat_restore_stats(checkPoint.redo);
5731+
pgstat_restore_stats();
57325732

57335733
lastFullPageWrites = checkPoint.fullPageWrites;
57345734

src/backend/utils/activity/pgstat.c

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@
104104
#include <unistd.h>
105105

106106
#include "access/xact.h"
107-
#include "access/xlog.h"
108107
#include "lib/dshash.h"
109108
#include "pgstat.h"
110109
#include "storage/fd.h"
@@ -181,8 +180,8 @@ typedef struct PgStat_SnapshotEntry
181180
* ----------
182181
*/
183182

184-
static void pgstat_write_statsfile(XLogRecPtr redo);
185-
static void pgstat_read_statsfile(XLogRecPtr redo);
183+
static void pgstat_write_statsfile(void);
184+
static void pgstat_read_statsfile(void);
186185

187186
static void pgstat_init_snapshot_fixed(void);
188187

@@ -503,9 +502,9 @@ static const PgStat_KindInfo **pgstat_kind_custom_infos = NULL;
503502
* Should only be called by the startup process or in single user mode.
504503
*/
505504
void
506-
pgstat_restore_stats(XLogRecPtr redo)
505+
pgstat_restore_stats(void)
507506
{
508-
pgstat_read_statsfile(redo);
507+
pgstat_read_statsfile();
509508
}
510509

511510
/*
@@ -581,7 +580,7 @@ pgstat_before_server_shutdown(int code, Datum arg)
581580
if (code == 0)
582581
{
583582
pgStatLocal.shmem->is_shutdown = true;
584-
pgstat_write_statsfile(GetRedoRecPtr());
583+
pgstat_write_statsfile();
585584
}
586585
}
587586

@@ -1585,7 +1584,7 @@ write_chunk(FILE *fpout, void *ptr, size_t len)
15851584
* stats so locking is not required.
15861585
*/
15871586
static void
1588-
pgstat_write_statsfile(XLogRecPtr redo)
1587+
pgstat_write_statsfile(void)
15891588
{
15901589
FILE *fpout;
15911590
int32 format_id;
@@ -1602,8 +1601,7 @@ pgstat_write_statsfile(XLogRecPtr redo)
16021601
/* we're shutting down, so it's ok to just override this */
16031602
pgstat_fetch_consistency = PGSTAT_FETCH_CONSISTENCY_NONE;
16041603

1605-
elog(DEBUG2, "writing stats file \"%s\" with redo %X/%X", statfile,
1606-
LSN_FORMAT_ARGS(redo));
1604+
elog(DEBUG2, "writing stats file \"%s\"", statfile);
16071605

16081606
/*
16091607
* Open the statistics temp file to write out the current values.
@@ -1624,9 +1622,6 @@ pgstat_write_statsfile(XLogRecPtr redo)
16241622
format_id = PGSTAT_FILE_FORMAT_ID;
16251623
write_chunk_s(fpout, &format_id);
16261624

1627-
/* Write the redo LSN, used to cross check the file read */
1628-
write_chunk_s(fpout, &redo);
1629-
16301625
/* Write various stats structs for fixed number of objects */
16311626
for (PgStat_Kind kind = PGSTAT_KIND_MIN; kind <= PGSTAT_KIND_MAX; kind++)
16321627
{
@@ -1773,20 +1768,18 @@ read_chunk(FILE *fpin, void *ptr, size_t len)
17731768
* stats so locking is not required.
17741769
*/
17751770
static void
1776-
pgstat_read_statsfile(XLogRecPtr redo)
1771+
pgstat_read_statsfile(void)
17771772
{
17781773
FILE *fpin;
17791774
int32 format_id;
17801775
bool found;
17811776
const char *statfile = PGSTAT_STAT_PERMANENT_FILENAME;
17821777
PgStat_ShmemControl *shmem = pgStatLocal.shmem;
1783-
XLogRecPtr file_redo;
17841778

17851779
/* shouldn't be called from postmaster */
17861780
Assert(IsUnderPostmaster || !IsPostmasterEnvironment);
17871781

1788-
elog(DEBUG2, "reading stats file \"%s\" with redo %X/%X", statfile,
1789-
LSN_FORMAT_ARGS(redo));
1782+
elog(DEBUG2, "reading stats file \"%s\"", statfile);
17901783

17911784
/*
17921785
* Try to open the stats file. If it doesn't exist, the backends simply
@@ -1824,22 +1817,6 @@ pgstat_read_statsfile(XLogRecPtr redo)
18241817
goto error;
18251818
}
18261819

1827-
/*
1828-
* Read the redo LSN stored in the file.
1829-
*/
1830-
if (!read_chunk_s(fpin, &file_redo))
1831-
{
1832-
elog(WARNING, "could not read redo LSN");
1833-
goto error;
1834-
}
1835-
1836-
if (file_redo != redo)
1837-
{
1838-
elog(WARNING, "found incorrect redo LSN %X/%X (expected %X/%X)",
1839-
LSN_FORMAT_ARGS(file_redo), LSN_FORMAT_ARGS(redo));
1840-
goto error;
1841-
}
1842-
18431820
/*
18441821
* We found an existing statistics file. Read it and put all the stats
18451822
* data into place.

src/include/pgstat.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#ifndef PGSTAT_H
1212
#define PGSTAT_H
1313

14-
#include "access/xlogdefs.h"
1514
#include "datatype/timestamp.h"
1615
#include "portability/instr_time.h"
1716
#include "postmaster/pgarch.h" /* for MAX_XFN_CHARS */
@@ -212,7 +211,7 @@ typedef struct PgStat_TableXactStatus
212211
* ------------------------------------------------------------
213212
*/
214213

215-
#define PGSTAT_FILE_FORMAT_ID 0x01A5BCB5
214+
#define PGSTAT_FILE_FORMAT_ID 0x01A5BCB6
216215

217216
typedef struct PgStat_ArchiverStats
218217
{
@@ -514,7 +513,7 @@ extern Size StatsShmemSize(void);
514513
extern void StatsShmemInit(void);
515514

516515
/* Functions called during server startup / shutdown */
517-
extern void pgstat_restore_stats(XLogRecPtr redo);
516+
extern void pgstat_restore_stats(void);
518517
extern void pgstat_discard_stats(void);
519518
extern void pgstat_before_server_shutdown(int code, Datum arg);
520519

0 commit comments

Comments
 (0)