PostgreSQL Source Code git master
Loading...
Searching...
No Matches
walsummarizer.c File Reference
#include "postgres.h"
#include "access/timeline.h"
#include "access/visibilitymap.h"
#include "access/xlog.h"
#include "access/xlog_internal.h"
#include "access/xlogrecovery.h"
#include "access/xlogutils.h"
#include "backup/walsummary.h"
#include "catalog/storage_xlog.h"
#include "commands/dbcommands_xlog.h"
#include "common/blkreftable.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "postmaster/auxprocess.h"
#include "postmaster/interrupt.h"
#include "postmaster/walsummarizer.h"
#include "replication/walreceiver.h"
#include "storage/aio_subsys.h"
#include "storage/fd.h"
#include "storage/ipc.h"
#include "storage/latch.h"
#include "storage/lwlock.h"
#include "storage/proc.h"
#include "storage/procsignal.h"
#include "storage/shmem.h"
#include "utils/guc.h"
#include "utils/memutils.h"
#include "utils/wait_event.h"
Include dependency graph for walsummarizer.c:

Go to the source code of this file.

Data Structures

struct  WalSummarizerData
 
struct  SummarizerReadLocalXLogPrivate
 

Macros

#define MAX_SLEEP_QUANTA   150
 
#define MS_PER_SLEEP_QUANTUM   200
 

Functions

static void WalSummarizerShutdown (int code, Datum arg)
 
static XLogRecPtr GetLatestLSN (TimeLineID *tli)
 
static void ProcessWalSummarizerInterrupts (void)
 
static XLogRecPtr SummarizeWAL (TimeLineID tli, XLogRecPtr start_lsn, bool exact, XLogRecPtr switch_lsn, XLogRecPtr maximum_lsn)
 
static void SummarizeDbaseRecord (XLogReaderState *xlogreader, BlockRefTable *brtab)
 
static void SummarizeSmgrRecord (XLogReaderState *xlogreader, BlockRefTable *brtab)
 
static void SummarizeXactRecord (XLogReaderState *xlogreader, BlockRefTable *brtab)
 
static bool SummarizeXlogRecord (XLogReaderState *xlogreader, bool *new_fast_forward)
 
static int summarizer_read_local_xlog_page (XLogReaderState *state, XLogRecPtr targetPagePtr, int reqLen, XLogRecPtr targetRecPtr, char *cur_page)
 
static void summarizer_wait_for_wal (void)
 
static void MaybeRemoveOldWalSummaries (void)
 
Size WalSummarizerShmemSize (void)
 
void WalSummarizerShmemInit (void)
 
void WalSummarizerMain (const void *startup_data, size_t startup_data_len)
 
void GetWalSummarizerState (TimeLineID *summarized_tli, XLogRecPtr *summarized_lsn, XLogRecPtr *pending_lsn, int *summarizer_pid)
 
XLogRecPtr GetOldestUnsummarizedLSN (TimeLineID *tli, bool *lsn_is_exact)
 
void WakeupWalSummarizer (void)
 
void WaitForWalSummarization (XLogRecPtr lsn)
 

Variables

static WalSummarizerDataWalSummarizerCtl
 
static long sleep_quanta = 1
 
static long pages_read_since_last_sleep = 0
 
static XLogRecPtr redo_pointer_at_last_summary_removal = InvalidXLogRecPtr
 
bool summarize_wal = false
 
int wal_summary_keep_time = 10 * HOURS_PER_DAY * MINS_PER_HOUR
 

Macro Definition Documentation

◆ MAX_SLEEP_QUANTA

#define MAX_SLEEP_QUANTA   150

Definition at line 128 of file walsummarizer.c.

◆ MS_PER_SLEEP_QUANTUM

#define MS_PER_SLEEP_QUANTUM   200

Definition at line 129 of file walsummarizer.c.

Function Documentation

◆ GetLatestLSN()

static XLogRecPtr GetLatestLSN ( TimeLineID tli)
static

Definition at line 800 of file walsummarizer.c.

801{
802 if (!RecoveryInProgress())
803 {
804 /* Don't summarize WAL before it's flushed. */
805 return GetFlushRecPtr(tli);
806 }
807 else
808 {
814
815 /*
816 * After the insert TLI has been set and before the control file has
817 * been updated to show the DB in production, RecoveryInProgress()
818 * will return true, because it's not yet safe for all backends to
819 * begin writing WAL. However, replay has already ceased, so from our
820 * point of view, recovery is already over. We should summarize up to
821 * where replay stopped and then prepare to resume at the start of the
822 * insert timeline.
823 */
825 {
826 *tli = insert_tli;
828 }
829
830 /*
831 * What we really want to know is how much WAL has been flushed to
832 * disk, but the only flush position available is the one provided by
833 * the walreceiver, which may not be running, because this could be
834 * crash recovery or recovery via restore_command. So use either the
835 * WAL receiver's flush position or the replay position, whichever is
836 * further ahead, on the theory that if the WAL has been replayed then
837 * it must also have been flushed to disk.
838 */
841 if (flush_lsn > replay_lsn)
842 {
843 *tli = flush_tli;
844 return flush_lsn;
845 }
846 else
847 {
848 *tli = replay_tli;
849 return replay_lsn;
850 }
851 }
852}
static int fb(int x)
XLogRecPtr GetWalRcvFlushRecPtr(XLogRecPtr *latestChunkStart, TimeLineID *receiveTLI)
bool RecoveryInProgress(void)
Definition xlog.c:6444
XLogRecPtr GetFlushRecPtr(TimeLineID *insertTLI)
Definition xlog.c:6609
TimeLineID GetWALInsertionTimeLineIfSet(void)
Definition xlog.c:6646
uint64 XLogRecPtr
Definition xlogdefs.h:21
uint32 TimeLineID
Definition xlogdefs.h:63
XLogRecPtr GetXLogReplayRecPtr(TimeLineID *replayTLI)

References fb(), GetFlushRecPtr(), GetWALInsertionTimeLineIfSet(), GetWalRcvFlushRecPtr(), GetXLogReplayRecPtr(), and RecoveryInProgress().

Referenced by GetOldestUnsummarizedLSN(), summarizer_read_local_xlog_page(), and WalSummarizerMain().

◆ GetOldestUnsummarizedLSN()

XLogRecPtr GetOldestUnsummarizedLSN ( TimeLineID tli,
bool lsn_is_exact 
)

Definition at line 505 of file walsummarizer.c.

506{
508 int n;
509 List *tles;
512 bool should_make_exact = false;
514 ListCell *lc;
516
517 /* If not summarizing WAL, do nothing. */
518 if (!summarize_wal)
519 return InvalidXLogRecPtr;
520
521 /*
522 * If we are not the WAL summarizer process, then we normally just want to
523 * read the values from shared memory. However, as an exception, if shared
524 * memory hasn't been initialized yet, then we need to do that so that we
525 * can read legal values and not remove any WAL too early.
526 */
528 {
530
532 {
534 if (tli != NULL)
536 if (lsn_is_exact != NULL)
537 *lsn_is_exact = WalSummarizerCtl->lsn_is_exact;
539 return unsummarized_lsn;
540 }
541
543 }
544
545 /*
546 * Find the oldest timeline on which WAL still exists, and the earliest
547 * segment for which it exists.
548 *
549 * Note that we do this every time the WAL summarizer process restarts or
550 * recovers from an error, in case the contents of pg_wal have changed
551 * under us e.g. if some files were removed, either manually - which
552 * shouldn't really happen, but might - or by postgres itself, if
553 * summarize_wal was turned off and then back on again.
554 */
557 for (n = list_length(tles) - 1; n >= 0; --n)
558 {
561
563 if (oldest_segno != 0)
564 {
565 /* Compute oldest LSN that still exists on disk. */
568
569 unsummarized_tli = tle->tli;
570 break;
571 }
572 }
573
574 /*
575 * Don't try to summarize anything older than the end LSN of the newest
576 * summary file that exists for this timeline.
577 */
581 foreach(lc, existing_summaries)
582 {
584
585 if (ws->end_lsn > unsummarized_lsn)
586 {
587 unsummarized_lsn = ws->end_lsn;
588 should_make_exact = true;
589 }
590 }
591
592 /* It really should not be possible for us to find no WAL. */
593 if (unsummarized_tli == 0)
596 errmsg_internal("no WAL found on timeline %u", latest_tli));
597
598 /*
599 * If we're the WAL summarizer, we always want to store the values we just
600 * computed into shared memory, because those are the values we're going
601 * to use to drive our operation, and so they are the authoritative
602 * values. Otherwise, we only store values into shared memory if shared
603 * memory is uninitialized. Our values are not canonical in such a case,
604 * but it's better to have something than nothing, to guide WAL retention.
605 */
608 {
614 }
615 else
617
618 /* Also return the to the caller as required. */
619 if (tli != NULL)
621 if (lsn_is_exact != NULL)
622 *lsn_is_exact = WalSummarizerCtl->lsn_is_exact;
624
625 return unsummarized_lsn;
626}
List * readTimeLineHistory(TimeLineID targetTLI)
Definition timeline.c:77
int errcode(int sqlerrcode)
Definition elog.c:874
int int errmsg_internal(const char *fmt,...) pg_attribute_printf(1
#define ERROR
Definition elog.h:39
#define ereport(elevel,...)
Definition elog.h:150
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition lwlock.c:1153
void LWLockRelease(LWLock *lock)
Definition lwlock.c:1770
@ LW_SHARED
Definition lwlock.h:105
@ LW_EXCLUSIVE
Definition lwlock.h:104
#define AmWalSummarizerProcess()
Definition miscadmin.h:392
#define lfirst(lc)
Definition pg_list.h:172
static int list_length(const List *l)
Definition pg_list.h:152
static void * list_nth(const List *list, int n)
Definition pg_list.h:331
Definition pg_list.h:54
XLogRecPtr summarized_lsn
TimeLineID summarized_tli
XLogRecPtr pending_lsn
static XLogRecPtr GetLatestLSN(TimeLineID *tli)
static WalSummarizerData * WalSummarizerCtl
bool summarize_wal
List * GetWalSummaries(TimeLineID tli, XLogRecPtr start_lsn, XLogRecPtr end_lsn)
Definition walsummary.c:43
int wal_segment_size
Definition xlog.c:147
XLogSegNo XLogGetOldestSegno(TimeLineID tli)
Definition xlog.c:3795
#define XLogSegNoOffsetToRecPtr(segno, offset, wal_segsz_bytes, dest)
#define InvalidXLogRecPtr
Definition xlogdefs.h:28
uint64 XLogSegNo
Definition xlogdefs.h:52

References AmWalSummarizerProcess, ereport, errcode(), errmsg_internal(), ERROR, fb(), GetLatestLSN(), GetWalSummaries(), WalSummarizerData::initialized, InvalidXLogRecPtr, lfirst, list_length(), list_nth(), WalSummarizerData::lsn_is_exact, LW_EXCLUSIVE, LW_SHARED, LWLockAcquire(), LWLockRelease(), WalSummarizerData::pending_lsn, readTimeLineHistory(), summarize_wal, WalSummarizerData::summarized_lsn, WalSummarizerData::summarized_tli, wal_segment_size, WalSummarizerCtl, XLogGetOldestSegno(), and XLogSegNoOffsetToRecPtr.

Referenced by KeepLogSeg(), and WalSummarizerMain().

◆ GetWalSummarizerState()

void GetWalSummarizerState ( TimeLineID summarized_tli,
XLogRecPtr summarized_lsn,
XLogRecPtr pending_lsn,
int summarizer_pid 
)

Definition at line 447 of file walsummarizer.c.

449{
452 {
453 /*
454 * If initialized is false, the rest of the structure contents are
455 * undefined.
456 */
457 *summarized_tli = 0;
458 *summarized_lsn = InvalidXLogRecPtr;
459 *pending_lsn = InvalidXLogRecPtr;
460 *summarizer_pid = -1;
461 }
462 else
463 {
464 int summarizer_pgprocno = WalSummarizerCtl->summarizer_pgprocno;
465
466 *summarized_tli = WalSummarizerCtl->summarized_tli;
467 *summarized_lsn = WalSummarizerCtl->summarized_lsn;
468 if (summarizer_pgprocno == INVALID_PROC_NUMBER)
469 {
470 /*
471 * If the summarizer has exited, the fact that it had processed
472 * beyond summarized_lsn is irrelevant now.
473 */
474 *pending_lsn = WalSummarizerCtl->summarized_lsn;
475 *summarizer_pid = -1;
476 }
477 else
478 {
479 *pending_lsn = WalSummarizerCtl->pending_lsn;
480
481 /*
482 * We're not fussed about inexact answers here, since they could
483 * become stale instantly, so we don't bother taking the lock, but
484 * make sure that invalid PID values are normalized to -1.
485 */
486 *summarizer_pid = GetPGProcByNumber(summarizer_pgprocno)->pid;
487 if (*summarizer_pid <= 0)
488 *summarizer_pid = -1;
489 }
490 }
492}
#define GetPGProcByNumber(n)
Definition proc.h:503
#define INVALID_PROC_NUMBER
Definition procnumber.h:26
ProcNumber summarizer_pgprocno

References fb(), GetPGProcByNumber, WalSummarizerData::initialized, INVALID_PROC_NUMBER, InvalidXLogRecPtr, LW_SHARED, LWLockAcquire(), LWLockRelease(), WalSummarizerData::pending_lsn, WalSummarizerData::summarized_lsn, WalSummarizerData::summarized_tli, WalSummarizerData::summarizer_pgprocno, and WalSummarizerCtl.

Referenced by pg_get_wal_summarizer_state().

◆ MaybeRemoveOldWalSummaries()

static void MaybeRemoveOldWalSummaries ( void  )
static

Definition at line 1664 of file walsummarizer.c.

1665{
1667 List *wslist;
1669
1670 /* If WAL summary removal is disabled, don't do anything. */
1671 if (wal_summary_keep_time == 0)
1672 return;
1673
1674 /*
1675 * If the redo pointer has not advanced, don't do anything.
1676 *
1677 * This has the effect that we only try to remove old WAL summary files
1678 * once per checkpoint cycle.
1679 */
1681 return;
1683
1684 /*
1685 * Files should only be removed if the last modification time precedes the
1686 * cutoff time we compute here.
1687 */
1689
1690 /* Get all the summaries that currently exist. */
1692
1693 /* Loop until all summaries have been considered for removal. */
1694 while (wslist != NIL)
1695 {
1696 ListCell *lc;
1700
1702
1703 /*
1704 * Pick a timeline for which some summary files still exist on disk,
1705 * and find the oldest LSN that still exists on disk for that
1706 * timeline.
1707 */
1710 if (oldest_segno != 0)
1712 oldest_lsn);
1713
1714
1715 /* Consider each WAL file on the selected timeline in turn. */
1716 foreach(lc, wslist)
1717 {
1719
1721
1722 /* If it's not on this timeline, it's not time to consider it. */
1723 if (selected_tli != ws->tli)
1724 continue;
1725
1726 /*
1727 * If the WAL doesn't exist any more, we can remove it if the file
1728 * modification time is old enough.
1729 */
1730 if (!XLogRecPtrIsValid(oldest_lsn) || ws->end_lsn <= oldest_lsn)
1732
1733 /*
1734 * Whether we removed the file or not, we need not consider it
1735 * again.
1736 */
1738 pfree(ws);
1739 }
1740 }
1741}
#define SECS_PER_MINUTE
Definition timestamp.h:128
void pfree(void *pointer)
Definition mcxt.c:1616
#define NIL
Definition pg_list.h:68
#define foreach_delete_current(lst, var_or_cell)
Definition pg_list.h:423
#define linitial(l)
Definition pg_list.h:178
static XLogRecPtr redo_pointer_at_last_summary_removal
static void ProcessWalSummarizerInterrupts(void)
int wal_summary_keep_time
void RemoveWalSummaryIfOlderThan(WalSummaryFile *ws, time_t cutoff_time)
Definition walsummary.c:230
XLogRecPtr GetRedoRecPtr(void)
Definition xlog.c:6547
#define XLogRecPtrIsValid(r)
Definition xlogdefs.h:29

References fb(), foreach_delete_current, GetRedoRecPtr(), GetWalSummaries(), InvalidXLogRecPtr, lfirst, linitial, NIL, pfree(), ProcessWalSummarizerInterrupts(), redo_pointer_at_last_summary_removal, RemoveWalSummaryIfOlderThan(), SECS_PER_MINUTE, wal_segment_size, wal_summary_keep_time, XLogGetOldestSegno(), XLogRecPtrIsValid, and XLogSegNoOffsetToRecPtr.

Referenced by WalSummarizerMain().

◆ ProcessWalSummarizerInterrupts()

static void ProcessWalSummarizerInterrupts ( void  )
static

Definition at line 858 of file walsummarizer.c.

859{
862
864 {
865 ConfigReloadPending = false;
867 }
868
870 {
872 errmsg_internal("WAL summarizer shutting down"));
873 proc_exit(0);
874 }
875
876 /* Perform logging of memory contexts of this process */
879}
#define DEBUG1
Definition elog.h:30
volatile sig_atomic_t LogMemoryContextPending
Definition globals.c:41
volatile sig_atomic_t ProcSignalBarrierPending
Definition globals.c:40
void ProcessConfigFile(GucContext context)
Definition guc-file.l:120
@ PGC_SIGHUP
Definition guc.h:75
volatile sig_atomic_t ShutdownRequestPending
Definition interrupt.c:28
volatile sig_atomic_t ConfigReloadPending
Definition interrupt.c:27
void proc_exit(int code)
Definition ipc.c:105
void ProcessLogMemoryContextInterrupt(void)
Definition mcxt.c:1340
void ProcessProcSignalBarrier(void)
Definition procsignal.c:502

References ConfigReloadPending, DEBUG1, ereport, errmsg_internal(), LogMemoryContextPending, PGC_SIGHUP, proc_exit(), ProcessConfigFile(), ProcessLogMemoryContextInterrupt(), ProcessProcSignalBarrier(), ProcSignalBarrierPending, ShutdownRequestPending, and summarize_wal.

Referenced by MaybeRemoveOldWalSummaries(), summarizer_read_local_xlog_page(), SummarizeWAL(), and WalSummarizerMain().

◆ SummarizeDbaseRecord()

static void SummarizeDbaseRecord ( XLogReaderState xlogreader,
BlockRefTable brtab 
)
static

Definition at line 1252 of file walsummarizer.c.

1253{
1255
1256 /*
1257 * We use relfilenode zero for a given database OID and tablespace OID to
1258 * indicate that all relations with that pair of IDs have been recreated
1259 * if they exist at all. Effectively, we're setting a limit block of 0 for
1260 * all such relfilenodes.
1261 *
1262 * Technically, this special handling is only needed in the case of
1263 * XLOG_DBASE_CREATE_FILE_COPY, because that can create a whole bunch of
1264 * relation files in a directory without logging anything specific to each
1265 * one. If we didn't mark the whole DB OID/TS OID combination in some way,
1266 * then a tablespace that was dropped after the reference backup and
1267 * recreated using the FILE_COPY method prior to the incremental backup
1268 * would look just like one that was never touched at all, which would be
1269 * catastrophic.
1270 *
1271 * But it seems best to adopt this treatment for all records that drop or
1272 * create a DB OID/TS OID combination. That's similar to how we treat the
1273 * limit block for individual relations, and it's an extra layer of safety
1274 * here. We can never lose data by marking more stuff as needing to be
1275 * backed up in full.
1276 */
1277 if (info == XLOG_DBASE_CREATE_FILE_COPY)
1278 {
1280 RelFileLocator rlocator;
1281
1282 xlrec =
1284 rlocator.spcOid = xlrec->tablespace_id;
1285 rlocator.dbOid = xlrec->db_id;
1286 rlocator.relNumber = 0;
1287 BlockRefTableSetLimitBlock(brtab, &rlocator, MAIN_FORKNUM, 0);
1288 }
1289 else if (info == XLOG_DBASE_CREATE_WAL_LOG)
1290 {
1292 RelFileLocator rlocator;
1293
1295 rlocator.spcOid = xlrec->tablespace_id;
1296 rlocator.dbOid = xlrec->db_id;
1297 rlocator.relNumber = 0;
1298 BlockRefTableSetLimitBlock(brtab, &rlocator, MAIN_FORKNUM, 0);
1299 }
1300 else if (info == XLOG_DBASE_DROP)
1301 {
1303 RelFileLocator rlocator;
1304 int i;
1305
1307 rlocator.dbOid = xlrec->db_id;
1308 rlocator.relNumber = 0;
1309 for (i = 0; i < xlrec->ntablespaces; ++i)
1310 {
1311 rlocator.spcOid = xlrec->tablespace_ids[i];
1312 BlockRefTableSetLimitBlock(brtab, &rlocator, MAIN_FORKNUM, 0);
1313 }
1314 }
1315}
void BlockRefTableSetLimitBlock(BlockRefTable *brtab, const RelFileLocator *rlocator, ForkNumber forknum, BlockNumber limit_block)
uint8_t uint8
Definition c.h:616
#define XLOG_DBASE_CREATE_WAL_LOG
#define XLOG_DBASE_DROP
#define XLOG_DBASE_CREATE_FILE_COPY
int i
Definition isn.c:77
@ MAIN_FORKNUM
Definition relpath.h:58
RelFileNumber relNumber
#define XLogRecGetInfo(decoder)
Definition xlogreader.h:410
#define XLogRecGetData(decoder)
Definition xlogreader.h:415
static XLogReaderState * xlogreader

References BlockRefTableSetLimitBlock(), RelFileLocator::dbOid, fb(), i, MAIN_FORKNUM, RelFileLocator::relNumber, RelFileLocator::spcOid, XLOG_DBASE_CREATE_FILE_COPY, XLOG_DBASE_CREATE_WAL_LOG, XLOG_DBASE_DROP, xlogreader, XLogRecGetData, and XLogRecGetInfo.

Referenced by SummarizeWAL().

◆ summarizer_read_local_xlog_page()

static int summarizer_read_local_xlog_page ( XLogReaderState state,
XLogRecPtr  targetPagePtr,
int  reqLen,
XLogRecPtr  targetRecPtr,
char cur_page 
)
static

Definition at line 1504 of file walsummarizer.c.

1507{
1508 int count;
1510 SummarizerReadLocalXLogPrivate *private_data;
1511
1513
1514 private_data = (SummarizerReadLocalXLogPrivate *)
1515 state->private_data;
1516
1517 while (1)
1518 {
1520 {
1521 /*
1522 * more than one block available; read only that block, have
1523 * caller come back if they need more.
1524 */
1525 count = XLOG_BLCKSZ;
1526 break;
1527 }
1528 else if (targetPagePtr + reqLen > private_data->read_upto)
1529 {
1530 /* We don't seem to have enough data. */
1531 if (private_data->historic)
1532 {
1533 /*
1534 * This is a historic timeline, so there will never be any
1535 * more data than we have currently.
1536 */
1537 private_data->end_of_wal = true;
1538 return -1;
1539 }
1540 else
1541 {
1544
1545 /*
1546 * This is - or at least was up until very recently - the
1547 * current timeline, so more data might show up. Delay here
1548 * so we don't tight-loop.
1549 */
1552
1553 /* Recheck end-of-WAL. */
1555 if (private_data->tli == latest_tli)
1556 {
1557 /* Still the current timeline, update max LSN. */
1558 Assert(latest_lsn >= private_data->read_upto);
1559 private_data->read_upto = latest_lsn;
1560 }
1561 else
1562 {
1565
1566 /*
1567 * The timeline we're scanning is no longer the latest
1568 * one. Figure out when it ended.
1569 */
1570 private_data->historic = true;
1571 switchpoint = tliSwitchPoint(private_data->tli, tles,
1572 NULL);
1573
1574 /*
1575 * Allow reads up to exactly the switch point.
1576 *
1577 * It's possible that this will cause read_upto to move
1578 * backwards, because we might have been promoted before
1579 * reaching the end of the previous timeline. In that
1580 * case, the next loop iteration will likely conclude that
1581 * we've reached end of WAL.
1582 */
1583 private_data->read_upto = switchpoint;
1584
1585 /* Debugging output. */
1587 errmsg_internal("timeline %u became historic, can read up to %X/%08X",
1588 private_data->tli, LSN_FORMAT_ARGS(private_data->read_upto)));
1589 }
1590
1591 /* Go around and try again. */
1592 }
1593 }
1594 else
1595 {
1596 /* enough bytes available to satisfy the request */
1597 count = private_data->read_upto - targetPagePtr;
1598 break;
1599 }
1600 }
1601
1602 if (!WALRead(state, cur_page, targetPagePtr, count,
1603 private_data->tli, &errinfo))
1605
1606 /* Track that we read a page, for sleep time calculation. */
1608
1609 /* number of valid bytes in the buffer */
1610 return count;
1611}
XLogRecPtr tliSwitchPoint(TimeLineID tli, List *history, TimeLineID *nextTLI)
Definition timeline.c:573
#define Assert(condition)
Definition c.h:945
static long pages_read_since_last_sleep
static void summarizer_wait_for_wal(void)
#define LSN_FORMAT_ARGS(lsn)
Definition xlogdefs.h:47
bool WALRead(XLogReaderState *state, char *buf, XLogRecPtr startptr, Size count, TimeLineID tli, WALReadError *errinfo)
void WALReadRaiseError(WALReadError *errinfo)
Definition xlogutils.c:1011

References Assert, DEBUG1, SummarizerReadLocalXLogPrivate::end_of_wal, ereport, errmsg_internal(), fb(), GetLatestLSN(), SummarizerReadLocalXLogPrivate::historic, LSN_FORMAT_ARGS, pages_read_since_last_sleep, ProcessWalSummarizerInterrupts(), SummarizerReadLocalXLogPrivate::read_upto, readTimeLineHistory(), summarizer_wait_for_wal(), SummarizerReadLocalXLogPrivate::tli, tliSwitchPoint(), WALRead(), and WALReadRaiseError().

Referenced by SummarizeWAL().

◆ summarizer_wait_for_wal()

static void summarizer_wait_for_wal ( void  )
static

Definition at line 1618 of file walsummarizer.c.

1619{
1621 {
1622 /*
1623 * No pages were read since the last sleep, so double the sleep time,
1624 * but not beyond the maximum allowable value.
1625 */
1627 }
1628 else if (pages_read_since_last_sleep > 1)
1629 {
1630 /*
1631 * Multiple pages were read since the last sleep, so reduce the sleep
1632 * time.
1633 *
1634 * A large burst of activity should be able to quickly reduce the
1635 * sleep time to the minimum, but we don't want a handful of extra WAL
1636 * records to provoke a strong reaction. We choose to reduce the sleep
1637 * time by 1 quantum for each page read beyond the first, which is a
1638 * fairly arbitrary way of trying to be reactive without overreacting.
1639 */
1641 sleep_quanta = 1;
1642 else
1644 }
1645
1646 /* Report pending statistics to the cumulative stats system. */
1647 pgstat_report_wal(false);
1648
1649 /* OK, now sleep. */
1655
1656 /* Reset count of pages read. */
1658}
#define Min(x, y)
Definition c.h:1093
struct Latch * MyLatch
Definition globals.c:63
void ResetLatch(Latch *latch)
Definition latch.c:374
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition latch.c:172
void pgstat_report_wal(bool force)
Definition pgstat_wal.c:46
#define WL_TIMEOUT
#define WL_EXIT_ON_PM_DEATH
#define WL_LATCH_SET
#define MAX_SLEEP_QUANTA
#define MS_PER_SLEEP_QUANTUM
static long sleep_quanta

References fb(), MAX_SLEEP_QUANTA, Min, MS_PER_SLEEP_QUANTUM, MyLatch, pages_read_since_last_sleep, pgstat_report_wal(), ResetLatch(), sleep_quanta, WaitLatch(), WL_EXIT_ON_PM_DEATH, WL_LATCH_SET, and WL_TIMEOUT.

Referenced by summarizer_read_local_xlog_page().

◆ SummarizeSmgrRecord()

static void SummarizeSmgrRecord ( XLogReaderState xlogreader,
BlockRefTable brtab 
)
static

Definition at line 1321 of file walsummarizer.c.

1322{
1324
1325 if (info == XLOG_SMGR_CREATE)
1326 {
1328
1329 /*
1330 * If a new relation fork is created on disk, there is no point
1331 * tracking anything about which blocks have been modified, because
1332 * the whole thing will be new. Hence, set the limit block for this
1333 * fork to 0.
1334 *
1335 * Ignore the FSM fork, which is not fully WAL-logged.
1336 */
1338
1339 if (xlrec->forkNum != FSM_FORKNUM)
1340 BlockRefTableSetLimitBlock(brtab, &xlrec->rlocator,
1341 xlrec->forkNum, 0);
1342 }
1343 else if (info == XLOG_SMGR_TRUNCATE)
1344 {
1346
1348
1349 /*
1350 * If a relation fork is truncated on disk, there is no point in
1351 * tracking anything about block modifications beyond the truncation
1352 * point.
1353 *
1354 * We ignore SMGR_TRUNCATE_FSM here because the FSM isn't fully
1355 * WAL-logged and thus we can't track modified blocks for it anyway.
1356 */
1357 if ((xlrec->flags & SMGR_TRUNCATE_HEAP) != 0)
1358 BlockRefTableSetLimitBlock(brtab, &xlrec->rlocator,
1359 MAIN_FORKNUM, xlrec->blkno);
1360 if ((xlrec->flags & SMGR_TRUNCATE_VM) != 0)
1361 BlockRefTableSetLimitBlock(brtab, &xlrec->rlocator,
1364 }
1365}
@ FSM_FORKNUM
Definition relpath.h:59
@ VISIBILITYMAP_FORKNUM
Definition relpath.h:60
#define SMGR_TRUNCATE_VM
#define XLOG_SMGR_CREATE
#define XLOG_SMGR_TRUNCATE
#define SMGR_TRUNCATE_HEAP
BlockNumber visibilitymap_truncation_length(BlockNumber nheapblocks)

References BlockRefTableSetLimitBlock(), fb(), FSM_FORKNUM, MAIN_FORKNUM, SMGR_TRUNCATE_HEAP, SMGR_TRUNCATE_VM, VISIBILITYMAP_FORKNUM, visibilitymap_truncation_length(), XLOG_SMGR_CREATE, XLOG_SMGR_TRUNCATE, xlogreader, XLogRecGetData, and XLogRecGetInfo.

Referenced by SummarizeWAL().

◆ SummarizeWAL()

static XLogRecPtr SummarizeWAL ( TimeLineID  tli,
XLogRecPtr  start_lsn,
bool  exact,
XLogRecPtr  switch_lsn,
XLogRecPtr  maximum_lsn 
)
static

Definition at line 906 of file walsummarizer.c.

908{
909 SummarizerReadLocalXLogPrivate *private_data;
913 char temp_path[MAXPGPATH];
914 char final_path[MAXPGPATH];
915 WalSummaryIO io;
917 bool fast_forward = true;
918 char *errormsg;
919
920 /* Initialize private data for xlogreader. */
922 private_data->tli = tli;
923 private_data->historic = XLogRecPtrIsValid(switch_lsn);
924 private_data->read_upto = maximum_lsn;
925
926 /* Create xlogreader. */
929 .segment_open = &wal_segment_open,
930 .segment_close = &wal_segment_close),
931 private_data);
932 if (xlogreader == NULL)
935 errmsg("out of memory"),
936 errdetail("Failed while allocating a WAL reading processor.")));
937
938 /*
939 * When exact = false, we're starting from an arbitrary point in the WAL
940 * and must search forward for the start of the next record.
941 *
942 * When exact = true, start_lsn should be either the LSN where a record
943 * begins, or the LSN of a page where the page header is immediately
944 * followed by the start of a new record. XLogBeginRead should tolerate
945 * either case.
946 *
947 * We need to allow for both cases because the behavior of xlogreader
948 * varies. When a record spans two or more xlog pages, the ending LSN
949 * reported by xlogreader will be the starting LSN of the following
950 * record, but when an xlog page boundary falls between two records, the
951 * end LSN for the first will be reported as the first byte of the
952 * following page. We can't know until we read that page how large the
953 * header will be, but we'll have to skip over it to find the next record.
954 */
955 if (exact)
956 {
957 /*
958 * Even if start_lsn is the beginning of a page rather than the
959 * beginning of the first record on that page, we should still use it
960 * as the start LSN for the summary file. That's because we detect
961 * missing summary files by looking for cases where the end LSN of one
962 * file is less than the start LSN of the next file. When only a page
963 * header is skipped, nothing has been missed.
964 */
965 XLogBeginRead(xlogreader, start_lsn);
966 summary_start_lsn = start_lsn;
967 }
968 else
969 {
970 summary_start_lsn = XLogFindNextRecord(xlogreader, start_lsn, &errormsg);
972 {
973 /*
974 * If we hit end-of-WAL while trying to find the next valid
975 * record, we must be on a historic timeline that has no valid
976 * records that begin after start_lsn and before end of WAL.
977 */
978 if (private_data->end_of_wal)
979 {
981 errmsg_internal("could not read WAL from timeline %u at %X/%08X: end of WAL at %X/%08X",
982 tli,
983 LSN_FORMAT_ARGS(start_lsn),
984 LSN_FORMAT_ARGS(private_data->read_upto)));
985
986 /*
987 * The timeline ends at or after start_lsn, without containing
988 * any records. Thus, we must make sure the main loop does not
989 * iterate. If start_lsn is the end of the timeline, then we
990 * won't actually emit an empty summary file, but otherwise,
991 * we must, to capture the fact that the LSN range in question
992 * contains no interesting WAL records.
993 */
994 summary_start_lsn = start_lsn;
995 summary_end_lsn = private_data->read_upto;
997 }
998 else
999 {
1000 if (errormsg)
1001 ereport(ERROR,
1002 errmsg("could not find a valid record after %X/%08X: %s",
1003 LSN_FORMAT_ARGS(start_lsn), errormsg));
1004 else
1005 ereport(ERROR,
1006 errmsg("could not find a valid record after %X/%08X",
1007 LSN_FORMAT_ARGS(start_lsn)));
1008 }
1009 }
1010
1011 /* We shouldn't go backward. */
1012 Assert(summary_start_lsn >= start_lsn);
1013 }
1014
1015 /*
1016 * Main loop: read xlog records one by one.
1017 */
1018 while (1)
1019 {
1020 int block_id;
1021 XLogRecord *record;
1022 uint8 rmid;
1023
1025
1026 /* We shouldn't go backward. */
1028
1029 /* Now read the next record. */
1030 record = XLogReadRecord(xlogreader, &errormsg);
1031 if (record == NULL)
1032 {
1033 if (private_data->end_of_wal)
1034 {
1035 /*
1036 * This timeline must be historic and must end before we were
1037 * able to read a complete record.
1038 */
1040 errmsg_internal("could not read WAL from timeline %u at %X/%08X: end of WAL at %X/%08X",
1041 tli,
1043 LSN_FORMAT_ARGS(private_data->read_upto)));
1044 /* Summary ends at end of WAL. */
1045 summary_end_lsn = private_data->read_upto;
1046 break;
1047 }
1048 if (errormsg)
1049 ereport(ERROR,
1051 errmsg("could not read WAL from timeline %u at %X/%08X: %s",
1053 errormsg)));
1054 else
1055 ereport(ERROR,
1057 errmsg("could not read WAL from timeline %u at %X/%08X",
1059 }
1060
1061 /* We shouldn't go backward. */
1063
1066 {
1067 /*
1068 * Whoops! We've read a record that *starts* after the switch LSN,
1069 * contrary to our goal of reading only until we hit the first
1070 * record that ends at or after the switch LSN. Pretend we didn't
1071 * read it after all by bailing out of this loop right here,
1072 * before we do anything with this record.
1073 *
1074 * This can happen because the last record before the switch LSN
1075 * might be continued across multiple pages, and then we might
1076 * come to a page with XLP_FIRST_IS_OVERWRITE_CONTRECORD set. In
1077 * that case, the record that was continued across multiple pages
1078 * is incomplete and will be disregarded, and the read will
1079 * restart from the beginning of the page that is flagged
1080 * XLP_FIRST_IS_OVERWRITE_CONTRECORD.
1081 *
1082 * If this case occurs, we can fairly say that the current summary
1083 * file ends at the switch LSN exactly. The first record on the
1084 * page marked XLP_FIRST_IS_OVERWRITE_CONTRECORD will be
1085 * discovered when generating the next summary file.
1086 */
1088 break;
1089 }
1090
1091 /*
1092 * Certain types of records require special handling. Redo points and
1093 * shutdown checkpoints trigger creation of new summary files and can
1094 * also cause us to enter or exit "fast forward" mode. Other types of
1095 * records can require special updates to the block reference table.
1096 */
1097 rmid = XLogRecGetRmid(xlogreader);
1098 if (rmid == RM_XLOG_ID)
1099 {
1100 bool new_fast_forward;
1101
1102 /*
1103 * If we've already processed some WAL records when we hit a redo
1104 * point or shutdown checkpoint, then we stop summarization before
1105 * including this record in the current file, so that it will be
1106 * the first record in the next file.
1107 *
1108 * When we hit one of those record types as the first record in a
1109 * file, we adjust our notion of whether we're fast-forwarding.
1110 * Any WAL generated with wal_level=minimal must be skipped
1111 * without actually generating any summary file, because an
1112 * incremental backup that crosses such WAL would be unsafe.
1113 */
1115 {
1117 {
1119 break;
1120 }
1121 else
1122 fast_forward = new_fast_forward;
1123 }
1124 }
1125 else if (!fast_forward)
1126 {
1127 /*
1128 * This switch handles record types that require extra updates to
1129 * the contents of the block reference table.
1130 */
1131 switch (rmid)
1132 {
1133 case RM_DBASE_ID:
1135 break;
1136 case RM_SMGR_ID:
1138 break;
1139 case RM_XACT_ID:
1141 break;
1142 }
1143 }
1144
1145 /*
1146 * If we're in fast-forward mode, we don't really need to do anything.
1147 * Otherwise, feed block references from xlog record to block
1148 * reference table.
1149 */
1150 if (!fast_forward)
1151 {
1153 block_id++)
1154 {
1155 RelFileLocator rlocator;
1156 ForkNumber forknum;
1157 BlockNumber blocknum;
1158
1160 &forknum, &blocknum, NULL))
1161 continue;
1162
1163 /*
1164 * As we do elsewhere, ignore the FSM fork, because it's not
1165 * fully WAL-logged.
1166 */
1167 if (forknum != FSM_FORKNUM)
1168 BlockRefTableMarkBlockModified(brtab, &rlocator, forknum,
1169 blocknum);
1170 }
1171 }
1172
1173 /* Update our notion of where this summary file ends. */
1175
1176 /* Also update shared memory. */
1181
1182 /*
1183 * If we have a switch LSN and have reached it, stop before reading
1184 * the next record.
1185 */
1188 break;
1189 }
1190
1191 /* Destroy xlogreader. */
1194
1195 /*
1196 * If a timeline switch occurs, we may fail to make any progress at all
1197 * before exiting the loop above. If that happens, we don't write a WAL
1198 * summary file at all. We can also skip writing a file if we're in
1199 * fast-forward mode.
1200 */
1201 if (summary_end_lsn > summary_start_lsn && !fast_forward)
1202 {
1203 /* Generate temporary and final path name. */
1205 XLOGDIR "/summaries/temp.summary");
1207 XLOGDIR "/summaries/%08X%08X%08X%08X%08X.summary",
1208 tli,
1211
1212 /* Open the temporary file for writing. */
1213 io.filepos = 0;
1215 if (io.file < 0)
1216 ereport(ERROR,
1218 errmsg("could not create file \"%s\": %m", temp_path)));
1219
1220 /* Write the data. */
1222
1223 /* Close temporary file and shut down xlogreader. */
1224 FileClose(io.file);
1225
1226 /* Tell the user what we did. */
1228 errmsg_internal("summarized WAL on TLI %u from %X/%08X to %X/%08X",
1229 tli,
1232
1233 /* Durably rename the new summary into place. */
1235 }
1236
1237 /* If we skipped a non-zero amount of WAL, log a debug message. */
1238 if (summary_end_lsn > summary_start_lsn && fast_forward)
1240 errmsg_internal("skipped summarizing WAL on TLI %u from %X/%08X to %X/%08X",
1241 tli,
1244
1245 return summary_end_lsn;
1246}
void BlockRefTableMarkBlockModified(BlockRefTable *brtab, const RelFileLocator *rlocator, ForkNumber forknum, BlockNumber blknum)
void WriteBlockRefTable(BlockRefTable *brtab, io_callback_fn write_callback, void *write_callback_arg)
void(*) BlockRefTable CreateEmptyBlockRefTable)(void)
uint32 BlockNumber
Definition block.h:31
int errcode_for_file_access(void)
Definition elog.c:897
int errdetail(const char *fmt,...) pg_attribute_printf(1
int durable_rename(const char *oldfile, const char *newfile, int elevel)
Definition fd.c:783
void FileClose(File file)
Definition fd.c:1966
File PathNameOpenFile(const char *fileName, int fileFlags)
Definition fd.c:1563
#define palloc0_object(type)
Definition fe_memutils.h:75
static char * errmsg
#define MAXPGPATH
#define snprintf
Definition port.h:260
ForkNumber
Definition relpath.h:56
off_t filepos
Definition walsummary.h:24
XLogRecPtr EndRecPtr
Definition xlogreader.h:206
XLogRecPtr ReadRecPtr
Definition xlogreader.h:205
void * private_data
Definition xlogreader.h:195
static bool SummarizeXlogRecord(XLogReaderState *xlogreader, bool *new_fast_forward)
static void SummarizeXactRecord(XLogReaderState *xlogreader, BlockRefTable *brtab)
static void SummarizeDbaseRecord(XLogReaderState *xlogreader, BlockRefTable *brtab)
static int summarizer_read_local_xlog_page(XLogReaderState *state, XLogRecPtr targetPagePtr, int reqLen, XLogRecPtr targetRecPtr, char *cur_page)
static void SummarizeSmgrRecord(XLogReaderState *xlogreader, BlockRefTable *brtab)
int WriteWalSummary(void *wal_summary_io, void *data, int length)
Definition walsummary.c:294
#define XLOGDIR
bool XLogRecGetBlockTagExtended(XLogReaderState *record, uint8 block_id, RelFileLocator *rlocator, ForkNumber *forknum, BlockNumber *blknum, Buffer *prefetch_buffer)
XLogReaderState * XLogReaderAllocate(int wal_segment_size, const char *waldir, XLogReaderRoutine *routine, void *private_data)
Definition xlogreader.c:108
XLogRecord * XLogReadRecord(XLogReaderState *state, char **errormsg)
Definition xlogreader.c:391
void XLogReaderFree(XLogReaderState *state)
Definition xlogreader.c:163
XLogRecPtr XLogFindNextRecord(XLogReaderState *state, XLogRecPtr RecPtr, char **errormsg)
void XLogBeginRead(XLogReaderState *state, XLogRecPtr RecPtr)
Definition xlogreader.c:233
#define XLogRecGetRmid(decoder)
Definition xlogreader.h:411
#define XL_ROUTINE(...)
Definition xlogreader.h:117
#define XLogRecMaxBlockId(decoder)
Definition xlogreader.h:418
void wal_segment_close(XLogReaderState *state)
Definition xlogutils.c:831
void wal_segment_open(XLogReaderState *state, XLogSegNo nextSegNo, TimeLineID *tli_p)
Definition xlogutils.c:806

References Assert, BlockRefTableMarkBlockModified(), DEBUG1, durable_rename(), SummarizerReadLocalXLogPrivate::end_of_wal, XLogReaderState::EndRecPtr, ereport, errcode(), errcode_for_file_access(), errdetail(), errmsg, errmsg_internal(), ERROR, fb(), WalSummaryIO::file, FileClose(), WalSummaryIO::filepos, FSM_FORKNUM, SummarizerReadLocalXLogPrivate::historic, LSN_FORMAT_ARGS, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), MAXPGPATH, palloc0_object, PathNameOpenFile(), WalSummarizerData::pending_lsn, pfree(), XLogReaderState::private_data, ProcessWalSummarizerInterrupts(), SummarizerReadLocalXLogPrivate::read_upto, XLogReaderState::ReadRecPtr, snprintf, WalSummarizerData::summarized_lsn, SummarizeDbaseRecord(), summarizer_read_local_xlog_page(), SummarizeSmgrRecord(), SummarizeXactRecord(), SummarizeXlogRecord(), SummarizerReadLocalXLogPrivate::tli, wal_segment_close(), wal_segment_open(), wal_segment_size, WalSummarizerCtl, WriteBlockRefTable(), WriteWalSummary(), XL_ROUTINE, XLogBeginRead(), XLOGDIR, XLogFindNextRecord(), xlogreader, XLogReaderAllocate(), XLogReaderFree(), XLogReadRecord(), XLogRecGetBlockTagExtended(), XLogRecGetRmid, XLogRecMaxBlockId, and XLogRecPtrIsValid.

Referenced by WalSummarizerMain().

◆ SummarizeXactRecord()

static void SummarizeXactRecord ( XLogReaderState xlogreader,
BlockRefTable brtab 
)
static

Definition at line 1371 of file walsummarizer.c.

1372{
1375
1376 if (xact_info == XLOG_XACT_COMMIT ||
1378 {
1381 int i;
1382
1383 /*
1384 * Don't track modified blocks for any relations that were removed on
1385 * commit.
1386 */
1388 for (i = 0; i < parsed.nrels; ++i)
1389 {
1390 ForkNumber forknum;
1391
1392 for (forknum = 0; forknum <= MAX_FORKNUM; ++forknum)
1393 if (forknum != FSM_FORKNUM)
1394 BlockRefTableSetLimitBlock(brtab, &parsed.xlocators[i],
1395 forknum, 0);
1396 }
1397 }
1398 else if (xact_info == XLOG_XACT_ABORT ||
1400 {
1403 int i;
1404
1405 /*
1406 * Don't track modified blocks for any relations that were removed on
1407 * abort.
1408 */
1410 for (i = 0; i < parsed.nrels; ++i)
1411 {
1412 ForkNumber forknum;
1413
1414 for (forknum = 0; forknum <= MAX_FORKNUM; ++forknum)
1415 if (forknum != FSM_FORKNUM)
1416 BlockRefTableSetLimitBlock(brtab, &parsed.xlocators[i],
1417 forknum, 0);
1418 }
1419 }
1420}
#define MAX_FORKNUM
Definition relpath.h:70
#define XLOG_XACT_COMMIT_PREPARED
Definition xact.h:173
#define XLOG_XACT_COMMIT
Definition xact.h:170
#define XLOG_XACT_OPMASK
Definition xact.h:180
#define XLOG_XACT_ABORT
Definition xact.h:172
#define XLOG_XACT_ABORT_PREPARED
Definition xact.h:174
void ParseCommitRecord(uint8 info, xl_xact_commit *xlrec, xl_xact_parsed_commit *parsed)
Definition xactdesc.c:35
void ParseAbortRecord(uint8 info, xl_xact_abort *xlrec, xl_xact_parsed_abort *parsed)
Definition xactdesc.c:141

References BlockRefTableSetLimitBlock(), fb(), FSM_FORKNUM, i, MAX_FORKNUM, ParseAbortRecord(), ParseCommitRecord(), XLOG_XACT_ABORT, XLOG_XACT_ABORT_PREPARED, XLOG_XACT_COMMIT, XLOG_XACT_COMMIT_PREPARED, XLOG_XACT_OPMASK, xlogreader, XLogRecGetData, and XLogRecGetInfo.

Referenced by SummarizeWAL().

◆ SummarizeXlogRecord()

static bool SummarizeXlogRecord ( XLogReaderState xlogreader,
bool new_fast_forward 
)
static

Definition at line 1431 of file walsummarizer.c.

1432{
1434 int record_wal_level;
1435
1436 if (info == XLOG_CHECKPOINT_REDO)
1437 {
1438 /* Payload is wal_level at the time record was written. */
1440 }
1441 else if (info == XLOG_CHECKPOINT_SHUTDOWN)
1442 {
1444
1445 /* Extract wal_level at time record was written from payload. */
1447 record_wal_level = rec_ckpt.wal_level;
1448 }
1449 else if (info == XLOG_PARAMETER_CHANGE)
1450 {
1452
1453 /* Extract wal_level at time record was written from payload. */
1455 sizeof(xl_parameter_change));
1456 record_wal_level = xlrec.wal_level;
1457 }
1458 else if (info == XLOG_END_OF_RECOVERY)
1459 {
1461
1462 /* Extract wal_level at time record was written from payload. */
1464 record_wal_level = xlrec.wal_level;
1465 }
1466 else
1467 {
1468 /* No special handling required. Return false. */
1469 return false;
1470 }
1471
1472 /*
1473 * Redo can only begin at an XLOG_CHECKPOINT_REDO or
1474 * XLOG_CHECKPOINT_SHUTDOWN record, so we want WAL summarization to begin
1475 * at those points. Hence, when those records are encountered, return
1476 * true, so that we stop just before summarizing either of those records.
1477 *
1478 * We also reach here if we just saw XLOG_END_OF_RECOVERY or
1479 * XLOG_PARAMETER_CHANGE. These are not places where recovery can start,
1480 * but they're still relevant here. A new timeline can begin with
1481 * XLOG_END_OF_RECOVERY, so we need to confirm the WAL level at that
1482 * point; and a restart can provoke XLOG_PARAMETER_CHANGE after an
1483 * intervening change to postgresql.conf, which might force us to stop
1484 * summarizing.
1485 */
1487 return true;
1488}
#define XLOG_CHECKPOINT_REDO
Definition pg_control.h:83
#define XLOG_CHECKPOINT_SHUTDOWN
Definition pg_control.h:69
#define XLOG_PARAMETER_CHANGE
Definition pg_control.h:75
#define XLOG_END_OF_RECOVERY
Definition pg_control.h:78
@ WAL_LEVEL_MINIMAL
Definition xlog.h:75

References fb(), WAL_LEVEL_MINIMAL, XLOG_CHECKPOINT_REDO, XLOG_CHECKPOINT_SHUTDOWN, XLOG_END_OF_RECOVERY, XLOG_PARAMETER_CHANGE, xlogreader, XLogRecGetData, and XLogRecGetInfo.

Referenced by SummarizeWAL().

◆ WaitForWalSummarization()

void WaitForWalSummarization ( XLogRecPtr  lsn)

Definition at line 660 of file walsummarizer.c.

661{
666 int deadcycles = 0;
667
669
670 while (1)
671 {
672 long timeout_in_ms = 10000;
673 XLogRecPtr summarized_lsn;
674 XLogRecPtr pending_lsn;
675
677
678 /* If WAL summarization is disabled while we're waiting, give up. */
679 if (!summarize_wal)
680 return;
681
682 /*
683 * If the LSN summarized on disk has reached the target value, stop.
684 */
686 summarized_lsn = WalSummarizerCtl->summarized_lsn;
687 pending_lsn = WalSummarizerCtl->pending_lsn;
689
690 /* If WAL summarization has progressed sufficiently, stop waiting. */
691 if (summarized_lsn >= lsn)
692 break;
693
694 /* Recheck current time. */
696
697 /* Have we finished the current cycle of waiting? */
700 {
701 long elapsed_seconds;
702
703 /* Begin new wait cycle. */
706
707 /*
708 * Keep track of the number of cycles during which there has been
709 * no progression of pending_lsn. If pending_lsn is not advancing,
710 * that means that not only are no new files appearing on disk,
711 * but we're not even incorporating new records into the in-memory
712 * state.
713 */
714 if (pending_lsn > prior_pending_lsn)
715 {
716 prior_pending_lsn = pending_lsn;
717 deadcycles = 0;
718 }
719 else
720 ++deadcycles;
721
722 /*
723 * If we've managed to wait for an entire minute without the WAL
724 * summarizer absorbing a single WAL record, error out; probably
725 * something is wrong.
726 *
727 * We could consider also erroring out if the summarizer is taking
728 * too long to catch up, but it's not clear what rate of progress
729 * would be acceptable and what would be too slow. So instead, we
730 * just try to error out in the case where there's no progress at
731 * all. That seems likely to catch a reasonable number of the
732 * things that can go wrong in practice (e.g. the summarizer
733 * process is completely hung, say because somebody hooked up a
734 * debugger to it or something) without giving up too quickly when
735 * the system is just slow.
736 */
737 if (deadcycles >= 6)
740 errmsg("WAL summarization is not progressing"),
741 errdetail("Summarization is needed through %X/%08X, but is stuck at %X/%08X on disk and %X/%08X in memory.",
742 LSN_FORMAT_ARGS(lsn),
743 LSN_FORMAT_ARGS(summarized_lsn),
744 LSN_FORMAT_ARGS(pending_lsn))));
745
746
747 /*
748 * Otherwise, just let the user know what's happening.
749 */
752 current_time) / 1000;
755 errmsg_plural("still waiting for WAL summarization through %X/%08X after %ld second",
756 "still waiting for WAL summarization through %X/%08X after %ld seconds",
758 LSN_FORMAT_ARGS(lsn),
760 errdetail("Summarization has reached %X/%08X on disk and %X/%08X in memory.",
761 LSN_FORMAT_ARGS(summarized_lsn),
762 LSN_FORMAT_ARGS(pending_lsn))));
763 }
764
765 /*
766 * Align the wait time to prevent drift. This doesn't really matter,
767 * but we'd like the warnings about how long we've been waiting to say
768 * 10 seconds, 20 seconds, 30 seconds, 40 seconds ... without ever
769 * drifting to something that is not a multiple of ten.
770 */
773
774 /* Wait and see. */
778 }
779
781}
long TimestampDifferenceMilliseconds(TimestampTz start_time, TimestampTz stop_time)
Definition timestamp.c:1751
TimestampTz GetCurrentTimestamp(void)
Definition timestamp.c:1639
bool ConditionVariableCancelSleep(void)
bool ConditionVariableTimedSleep(ConditionVariable *cv, long timeout, uint32 wait_event_info)
int64 TimestampTz
Definition timestamp.h:39
#define WARNING
Definition elog.h:36
int int int errmsg_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...) pg_attribute_printf(1
#define CHECK_FOR_INTERRUPTS()
Definition miscadmin.h:123
ConditionVariable summary_file_cv
#define TimestampTzPlusMilliseconds(tz, ms)
Definition timestamp.h:85

References CHECK_FOR_INTERRUPTS, ConditionVariableCancelSleep(), ConditionVariableTimedSleep(), ereport, errcode(), errdetail(), errmsg, errmsg_plural(), ERROR, fb(), GetCurrentTimestamp(), InvalidXLogRecPtr, LSN_FORMAT_ARGS, LW_SHARED, LWLockAcquire(), LWLockRelease(), WalSummarizerData::pending_lsn, summarize_wal, WalSummarizerData::summarized_lsn, WalSummarizerData::summary_file_cv, TimestampDifferenceMilliseconds(), TimestampTzPlusMilliseconds, WalSummarizerCtl, and WARNING.

Referenced by CleanupAfterArchiveRecovery(), and PrepareForIncrementalBackup().

◆ WakeupWalSummarizer()

void WakeupWalSummarizer ( void  )

Definition at line 637 of file walsummarizer.c.

638{
639 ProcNumber pgprocno;
640
641 if (WalSummarizerCtl == NULL)
642 return;
643
647
648 if (pgprocno != INVALID_PROC_NUMBER)
649 SetLatch(&GetPGProcByNumber(pgprocno)->procLatch);
650}
void SetLatch(Latch *latch)
Definition latch.c:290
int ProcNumber
Definition procnumber.h:24

References fb(), GetPGProcByNumber, INVALID_PROC_NUMBER, LW_SHARED, LWLockAcquire(), LWLockRelease(), SetLatch(), WalSummarizerData::summarizer_pgprocno, and WalSummarizerCtl.

Referenced by CreateCheckPoint().

◆ WalSummarizerMain()

void WalSummarizerMain ( const void startup_data,
size_t  startup_data_len 
)

Definition at line 214 of file walsummarizer.c.

215{
217 MemoryContext context;
218
219 /*
220 * Within this function, 'current_lsn' and 'current_tli' refer to the
221 * point from which the next WAL summary file should start. 'exact' is
222 * true if 'current_lsn' is known to be the start of a WAL record or WAL
223 * segment, and false if it might be in the middle of a record someplace.
224 *
225 * 'switch_lsn' and 'switch_tli', if set, are the LSN at which we need to
226 * switch to a new timeline and the timeline to which we need to switch.
227 * If not set, we either haven't figured out the answers yet or we're
228 * already on the latest timeline.
229 */
232 bool exact;
235
237
239
241 (errmsg_internal("WAL summarizer started")));
242
243 /*
244 * Properly accept or ignore signals the postmaster might send us
245 */
247 pqsignal(SIGINT, SIG_IGN); /* no query to cancel */
249 /* SIGQUIT handler was already set up by InitPostmasterChild */
253 pqsignal(SIGUSR2, SIG_IGN); /* not used */
254
255 /* Advertise ourselves. */
260
261 /* Create and switch to a memory context that we can reset on error. */
263 "Wal Summarizer",
265 MemoryContextSwitchTo(context);
266
267 /*
268 * Reset some signals that are accepted by postmaster but not here
269 */
271
272 /*
273 * If an exception is encountered, processing resumes here.
274 */
275 if (sigsetjmp(local_sigjmp_buf, 1) != 0)
276 {
277 /* Since not using PG_TRY, must reset error stack by hand */
279
280 /* Prevent interrupts while cleaning up */
282
283 /* Report the error to the server log */
285
286 /* Release resources we might have acquired. */
292 AtEOXact_Files(false);
293 AtEOXact_HashTables(false);
294
295 /*
296 * Now return to normal top-level context and clear ErrorContext for
297 * next time.
298 */
299 MemoryContextSwitchTo(context);
301
302 /* Flush any leaked data in the top-level context */
303 MemoryContextReset(context);
304
305 /* Now we can allow interrupts again */
307
308 /*
309 * Sleep for 10 seconds before attempting to resume operations in
310 * order to avoid excessive logging.
311 *
312 * Many of the likely error conditions are things that will repeat
313 * every time. For example, if the WAL can't be read or the summary
314 * can't be written, only administrator action will cure the problem.
315 * So a really fast retry time doesn't seem to be especially
316 * beneficial, and it will clutter the logs.
317 */
320 10000,
322 }
323
324 /* We can now handle ereport(ERROR) */
326
327 /*
328 * Unblock signals (they were blocked when the postmaster forked us)
329 */
331
332 /*
333 * Fetch information about previous progress from shared memory, and ask
334 * GetOldestUnsummarizedLSN to reset pending_lsn to summarized_lsn. We
335 * might be recovering from an error, and if so, pending_lsn might have
336 * advanced past summarized_lsn, but any WAL we read previously has been
337 * lost and will need to be reread.
338 *
339 * If we discover that WAL summarization is not enabled, just exit.
340 */
343 proc_exit(0);
344
345 /*
346 * Loop forever
347 */
348 for (;;)
349 {
353
354 /* Flush any leaked data in the top-level context */
355 MemoryContextReset(context);
356
357 /* Process any signals received recently. */
359
360 /* If it's time to remove any old WAL summaries, do that now. */
362
363 /* Find the LSN and TLI up to which we can safely summarize. */
365
366 /*
367 * If we're summarizing a historic timeline and we haven't yet
368 * computed the point at which to switch to the next timeline, do that
369 * now.
370 *
371 * Note that if this is a standby, what was previously the current
372 * timeline could become historic at any time.
373 *
374 * We could try to make this more efficient by caching the results of
375 * readTimeLineHistory when latest_tli has not changed, but since we
376 * only have to do this once per timeline switch, we probably wouldn't
377 * save any significant amount of work in practice.
378 */
380 {
382
385 errmsg_internal("switch point from TLI %u to TLI %u is at %X/%08X",
387 }
388
389 /*
390 * If we've reached the switch LSN, we can't summarize anything else
391 * on this timeline. Switch to the next timeline and go around again,
392 * backing up to the exact switch point if we passed it.
393 */
395 {
396 /* Restart summarization from switch point. */
399
400 /* Next timeline and switch point, if any, not yet known. */
402 switch_tli = 0;
403
404 /* Update (really, rewind, if needed) state in shared memory. */
411
412 continue;
413 }
414
415 /* Summarize WAL. */
421
422 /*
423 * Update state for next loop iteration.
424 *
425 * Next summary file should start from exactly where this one ended.
426 */
428 exact = true;
429
430 /* Update state in shared memory. */
437
438 /* Wake up anyone waiting for more summary files to be written. */
440 }
441}
void pgaio_error_cleanup(void)
Definition aio.c:1165
void AuxiliaryProcessMainCommon(void)
Definition auxprocess.c:40
sigset_t UnBlockSig
Definition pqsignal.c:22
void ConditionVariableBroadcast(ConditionVariable *cv)
void AtEOXact_HashTables(bool isCommit)
Definition dynahash.c:1931
void EmitErrorReport(void)
Definition elog.c:1882
ErrorContextCallback * error_context_stack
Definition elog.c:99
void FlushErrorState(void)
Definition elog.c:2062
sigjmp_buf * PG_exception_stack
Definition elog.c:101
void AtEOXact_Files(bool isCommit)
Definition fd.c:3214
ProcNumber MyProcNumber
Definition globals.c:90
void SignalHandlerForShutdownRequest(SIGNAL_ARGS)
Definition interrupt.c:104
void SignalHandlerForConfigReload(SIGNAL_ARGS)
Definition interrupt.c:61
void on_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition ipc.c:372
void LWLockReleaseAll(void)
Definition lwlock.c:1869
void MemoryContextReset(MemoryContext context)
Definition mcxt.c:403
MemoryContext TopMemoryContext
Definition mcxt.c:166
#define AllocSetContextCreate
Definition memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition memutils.h:160
#define RESUME_INTERRUPTS()
Definition miscadmin.h:136
#define HOLD_INTERRUPTS()
Definition miscadmin.h:134
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition palloc.h:124
#define pqsignal
Definition port.h:547
uint64_t Datum
Definition postgres.h:70
void procsignal_sigusr1_handler(SIGNAL_ARGS)
Definition procsignal.c:680
void ReleaseAuxProcessResources(bool isCommit)
Definition resowner.c:1016
static void pgstat_report_wait_end(void)
Definition wait_event.h:85
static XLogRecPtr SummarizeWAL(TimeLineID tli, XLogRecPtr start_lsn, bool exact, XLogRecPtr switch_lsn, XLogRecPtr maximum_lsn)
static void WalSummarizerShutdown(int code, Datum arg)
static void MaybeRemoveOldWalSummaries(void)
XLogRecPtr GetOldestUnsummarizedLSN(TimeLineID *tli, bool *lsn_is_exact)
#define SIGCHLD
Definition win32_port.h:168
#define SIGHUP
Definition win32_port.h:158
#define SIGPIPE
Definition win32_port.h:163
#define SIGUSR1
Definition win32_port.h:170
#define SIGALRM
Definition win32_port.h:164
#define SIGUSR2
Definition win32_port.h:171

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, Assert, AtEOXact_Files(), AtEOXact_HashTables(), AuxiliaryProcessMainCommon(), ConditionVariableBroadcast(), ConditionVariableCancelSleep(), DEBUG1, EmitErrorReport(), ereport, errmsg_internal(), error_context_stack, fb(), FlushErrorState(), GetLatestLSN(), GetOldestUnsummarizedLSN(), HOLD_INTERRUPTS, InvalidXLogRecPtr, LSN_FORMAT_ARGS, WalSummarizerData::lsn_is_exact, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), LWLockReleaseAll(), MaybeRemoveOldWalSummaries(), MemoryContextReset(), MemoryContextSwitchTo(), MyProcNumber, on_shmem_exit(), WalSummarizerData::pending_lsn, PG_exception_stack, pgaio_error_cleanup(), pgstat_report_wait_end(), pqsignal, proc_exit(), ProcessWalSummarizerInterrupts(), procsignal_sigusr1_handler(), readTimeLineHistory(), ReleaseAuxProcessResources(), RESUME_INTERRUPTS, SIGALRM, SIGCHLD, SIGHUP, SignalHandlerForConfigReload(), SignalHandlerForShutdownRequest(), SIGPIPE, SIGUSR1, SIGUSR2, WalSummarizerData::summarized_lsn, WalSummarizerData::summarized_tli, WalSummarizerData::summarizer_pgprocno, SummarizeWAL(), WalSummarizerData::summary_file_cv, tliSwitchPoint(), TopMemoryContext, UnBlockSig, WaitLatch(), WalSummarizerCtl, WalSummarizerShutdown(), WL_EXIT_ON_PM_DEATH, WL_TIMEOUT, and XLogRecPtrIsValid.

◆ WalSummarizerShmemInit()

void WalSummarizerShmemInit ( void  )

Definition at line 183 of file walsummarizer.c.

184{
185 bool found;
186
188 ShmemInitStruct("Wal Summarizer Ctl", WalSummarizerShmemSize(),
189 &found);
190
191 if (!found)
192 {
193 /*
194 * First time through, so initialize.
195 *
196 * We're just filling in dummy values here -- the real initialization
197 * will happen when GetOldestUnsummarizedLSN() is called for the first
198 * time.
199 */
207 }
208}
void ConditionVariableInit(ConditionVariable *cv)
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition shmem.c:380
Size WalSummarizerShmemSize(void)

References ConditionVariableInit(), WalSummarizerData::initialized, INVALID_PROC_NUMBER, InvalidXLogRecPtr, WalSummarizerData::lsn_is_exact, WalSummarizerData::pending_lsn, ShmemInitStruct(), WalSummarizerData::summarized_lsn, WalSummarizerData::summarized_tli, WalSummarizerData::summarizer_pgprocno, WalSummarizerData::summary_file_cv, WalSummarizerCtl, and WalSummarizerShmemSize().

Referenced by CreateOrAttachShmemStructs().

◆ WalSummarizerShmemSize()

Size WalSummarizerShmemSize ( void  )

Definition at line 174 of file walsummarizer.c.

175{
176 return sizeof(WalSummarizerData);
177}

Referenced by CalculateShmemSize(), and WalSummarizerShmemInit().

◆ WalSummarizerShutdown()

Variable Documentation

◆ pages_read_since_last_sleep

long pages_read_since_last_sleep = 0
static

Definition at line 135 of file walsummarizer.c.

Referenced by summarizer_read_local_xlog_page(), and summarizer_wait_for_wal().

◆ redo_pointer_at_last_summary_removal

XLogRecPtr redo_pointer_at_last_summary_removal = InvalidXLogRecPtr
static

Definition at line 140 of file walsummarizer.c.

Referenced by MaybeRemoveOldWalSummaries().

◆ sleep_quanta

long sleep_quanta = 1
static

Definition at line 118 of file walsummarizer.c.

Referenced by summarizer_wait_for_wal().

◆ summarize_wal

◆ wal_summary_keep_time

int wal_summary_keep_time = 10 * HOURS_PER_DAY * MINS_PER_HOUR

Definition at line 146 of file walsummarizer.c.

Referenced by MaybeRemoveOldWalSummaries().

◆ WalSummarizerCtl