2022-04-08 09:02:10 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* pg_walinspect.c
|
|
|
|
* Functions to inspect contents of PostgreSQL Write-Ahead Log
|
|
|
|
*
|
2023-01-02 21:00:37 +01:00
|
|
|
* Copyright (c) 2022-2023, PostgreSQL Global Development Group
|
2022-04-08 09:02:10 +02:00
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
|
|
|
* contrib/pg_walinspect/pg_walinspect.c
|
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
|
|
|
|
|
|
|
#include "access/xlog.h"
|
|
|
|
#include "access/xlog_internal.h"
|
|
|
|
#include "access/xlogreader.h"
|
|
|
|
#include "access/xlogrecovery.h"
|
|
|
|
#include "access/xlogstats.h"
|
|
|
|
#include "access/xlogutils.h"
|
|
|
|
#include "funcapi.h"
|
|
|
|
#include "miscadmin.h"
|
pg_walinspect: pg_get_wal_fpi_info() -> pg_get_wal_block_info()
This commit reworks pg_get_wal_fpi_info() to become aware of all the
block information that can be attached to a record rather than just its
full-page writes:
- Addition of the block id as assigned by XLogRegisterBuffer(),
XLogRegisterBlock() or XLogRegisterBufData().
- Addition of the block data, as bytea, or NULL if none. The length of
the block data can be guessed with length(), so there is no need to
store its length in a separate field.
- Addition of the full-page image length, as counted without a hole or
even compressed.
- Modification of the handling of the full-page image data. This is
still a bytea, but it could become NULL if none is assigned to a block.
- Addition of the full-page image flags, tracking if a page is stored
with a hole, if it needs to be applied and the type of compression
applied to it, as of all the BKPIMAGE_* values in xlogrecord.h.
The information of each block is returned as one single record, with the
record's ReadRecPtr included to be able to join the block information
with the existing pg_get_wal_records_info(). Note that it is perfectly
possible for a block to hold both data and full-page image.
Thanks also to Kyotaro Horiguchi and Matthias van de Meent for the
discussion.
This commit uses some of the work proposed by Melanie, though it has
been largely redesigned and rewritten by me. Bharath has helped in
refining a bit the whole.
Reported-by: Melanie Plageman
Author: Michael Paquier, Melanie Plageman, Bharath Rupireddy
Discussion: https://postgr.es/m/CAAKRu_bORebdZmcV8V4cZBzU8M_C6tDDdbiPhCZ6i-iuSXW9TA@mail.gmail.com
2023-03-10 02:09:07 +01:00
|
|
|
#include "utils/array.h"
|
2022-04-08 09:02:10 +02:00
|
|
|
#include "utils/builtins.h"
|
|
|
|
#include "utils/pg_lsn.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* NOTE: For any code change or issue fix here, it is highly recommended to
|
|
|
|
* give a thought about doing the same in pg_waldump tool as well.
|
|
|
|
*/
|
|
|
|
|
|
|
|
PG_MODULE_MAGIC;
|
|
|
|
|
pg_walinspect: pg_get_wal_fpi_info() -> pg_get_wal_block_info()
This commit reworks pg_get_wal_fpi_info() to become aware of all the
block information that can be attached to a record rather than just its
full-page writes:
- Addition of the block id as assigned by XLogRegisterBuffer(),
XLogRegisterBlock() or XLogRegisterBufData().
- Addition of the block data, as bytea, or NULL if none. The length of
the block data can be guessed with length(), so there is no need to
store its length in a separate field.
- Addition of the full-page image length, as counted without a hole or
even compressed.
- Modification of the handling of the full-page image data. This is
still a bytea, but it could become NULL if none is assigned to a block.
- Addition of the full-page image flags, tracking if a page is stored
with a hole, if it needs to be applied and the type of compression
applied to it, as of all the BKPIMAGE_* values in xlogrecord.h.
The information of each block is returned as one single record, with the
record's ReadRecPtr included to be able to join the block information
with the existing pg_get_wal_records_info(). Note that it is perfectly
possible for a block to hold both data and full-page image.
Thanks also to Kyotaro Horiguchi and Matthias van de Meent for the
discussion.
This commit uses some of the work proposed by Melanie, though it has
been largely redesigned and rewritten by me. Bharath has helped in
refining a bit the whole.
Reported-by: Melanie Plageman
Author: Michael Paquier, Melanie Plageman, Bharath Rupireddy
Discussion: https://postgr.es/m/CAAKRu_bORebdZmcV8V4cZBzU8M_C6tDDdbiPhCZ6i-iuSXW9TA@mail.gmail.com
2023-03-10 02:09:07 +01:00
|
|
|
PG_FUNCTION_INFO_V1(pg_get_wal_block_info);
|
2022-04-08 09:02:10 +02:00
|
|
|
PG_FUNCTION_INFO_V1(pg_get_wal_record_info);
|
|
|
|
PG_FUNCTION_INFO_V1(pg_get_wal_records_info);
|
|
|
|
PG_FUNCTION_INFO_V1(pg_get_wal_records_info_till_end_of_wal);
|
|
|
|
PG_FUNCTION_INFO_V1(pg_get_wal_stats);
|
|
|
|
PG_FUNCTION_INFO_V1(pg_get_wal_stats_till_end_of_wal);
|
|
|
|
|
2023-03-14 12:13:02 +01:00
|
|
|
static void ValidateInputLSNs(XLogRecPtr start_lsn, XLogRecPtr *end_lsn);
|
|
|
|
static XLogRecPtr GetCurrentLSN(void);
|
2022-08-18 23:23:30 +02:00
|
|
|
static XLogReaderState *InitXLogReaderState(XLogRecPtr lsn);
|
|
|
|
static XLogRecord *ReadNextXLogRecord(XLogReaderState *xlogreader);
|
|
|
|
static void GetWALRecordInfo(XLogReaderState *record, Datum *values,
|
|
|
|
bool *nulls, uint32 ncols);
|
2023-03-14 12:13:02 +01:00
|
|
|
static void GetWALRecordsInfo(FunctionCallInfo fcinfo,
|
|
|
|
XLogRecPtr start_lsn,
|
2022-04-08 09:02:10 +02:00
|
|
|
XLogRecPtr end_lsn);
|
2022-05-12 21:17:30 +02:00
|
|
|
static void GetXLogSummaryStats(XLogStats *stats, ReturnSetInfo *rsinfo,
|
2022-04-08 09:02:10 +02:00
|
|
|
Datum *values, bool *nulls, uint32 ncols,
|
|
|
|
bool stats_per_record);
|
|
|
|
static void FillXLogStatsRow(const char *name, uint64 n, uint64 total_count,
|
|
|
|
uint64 rec_len, uint64 total_rec_len,
|
|
|
|
uint64 fpi_len, uint64 total_fpi_len,
|
|
|
|
uint64 tot_len, uint64 total_len,
|
|
|
|
Datum *values, bool *nulls, uint32 ncols);
|
2023-03-14 12:13:02 +01:00
|
|
|
static void GetWalStats(FunctionCallInfo fcinfo,
|
|
|
|
XLogRecPtr start_lsn,
|
|
|
|
XLogRecPtr end_lsn,
|
|
|
|
bool stats_per_record);
|
2023-03-31 23:02:52 +02:00
|
|
|
static void GetWALBlockInfo(FunctionCallInfo fcinfo, XLogReaderState *record,
|
|
|
|
bool show_data);
|
2022-04-08 09:02:10 +02:00
|
|
|
|
|
|
|
/*
|
2023-03-14 12:13:02 +01:00
|
|
|
* Return the LSN up to which the server has WAL.
|
2022-04-08 09:02:10 +02:00
|
|
|
*/
|
2023-03-14 12:13:02 +01:00
|
|
|
static XLogRecPtr
|
|
|
|
GetCurrentLSN(void)
|
2022-04-08 09:02:10 +02:00
|
|
|
{
|
2023-03-14 12:13:02 +01:00
|
|
|
XLogRecPtr curr_lsn;
|
|
|
|
|
2022-04-08 09:02:10 +02:00
|
|
|
/*
|
|
|
|
* We determine the current LSN of the server similar to how page_read
|
|
|
|
* callback read_local_xlog_page_no_wait does.
|
|
|
|
*/
|
|
|
|
if (!RecoveryInProgress())
|
2023-03-14 12:13:02 +01:00
|
|
|
curr_lsn = GetFlushRecPtr(NULL);
|
2022-04-08 09:02:10 +02:00
|
|
|
else
|
2023-03-14 12:13:02 +01:00
|
|
|
curr_lsn = GetXLogReplayRecPtr(NULL);
|
2022-04-08 09:02:10 +02:00
|
|
|
|
2023-03-14 12:13:02 +01:00
|
|
|
Assert(!XLogRecPtrIsInvalid(curr_lsn));
|
2022-04-08 09:02:10 +02:00
|
|
|
|
2023-03-14 12:13:02 +01:00
|
|
|
return curr_lsn;
|
2022-04-08 09:02:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2023-04-19 03:50:33 +02:00
|
|
|
* Initialize WAL reader and identify first valid LSN.
|
2022-04-08 09:02:10 +02:00
|
|
|
*/
|
|
|
|
static XLogReaderState *
|
2022-08-18 23:23:30 +02:00
|
|
|
InitXLogReaderState(XLogRecPtr lsn)
|
2022-04-08 09:02:10 +02:00
|
|
|
{
|
|
|
|
XLogReaderState *xlogreader;
|
2022-04-30 17:28:33 +02:00
|
|
|
ReadLocalXLogPageNoWaitPrivate *private_data;
|
2022-08-18 23:23:30 +02:00
|
|
|
XLogRecPtr first_valid_record;
|
2022-04-08 09:02:10 +02:00
|
|
|
|
|
|
|
/*
|
2022-04-26 07:24:13 +02:00
|
|
|
* Reading WAL below the first page of the first segments isn't allowed.
|
2022-04-08 09:02:10 +02:00
|
|
|
* This is a bootstrap WAL page and the page_read callback fails to read
|
|
|
|
* it.
|
|
|
|
*/
|
|
|
|
if (lsn < XLOG_BLCKSZ)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errmsg("could not read WAL at LSN %X/%X",
|
|
|
|
LSN_FORMAT_ARGS(lsn))));
|
|
|
|
|
2022-04-30 17:28:33 +02:00
|
|
|
private_data = (ReadLocalXLogPageNoWaitPrivate *)
|
2022-05-12 21:17:30 +02:00
|
|
|
palloc0(sizeof(ReadLocalXLogPageNoWaitPrivate));
|
2022-04-30 17:28:33 +02:00
|
|
|
|
2022-04-08 09:02:10 +02:00
|
|
|
xlogreader = XLogReaderAllocate(wal_segment_size, NULL,
|
|
|
|
XL_ROUTINE(.page_read = &read_local_xlog_page_no_wait,
|
|
|
|
.segment_open = &wal_segment_open,
|
|
|
|
.segment_close = &wal_segment_close),
|
2022-04-30 17:28:33 +02:00
|
|
|
private_data);
|
2022-04-08 09:02:10 +02:00
|
|
|
|
|
|
|
if (xlogreader == NULL)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_OUT_OF_MEMORY),
|
|
|
|
errmsg("out of memory"),
|
|
|
|
errdetail("Failed while allocating a WAL reading processor.")));
|
|
|
|
|
|
|
|
/* first find a valid recptr to start from */
|
2022-08-18 23:23:30 +02:00
|
|
|
first_valid_record = XLogFindNextRecord(xlogreader, lsn);
|
2022-04-08 09:02:10 +02:00
|
|
|
|
2022-08-18 23:23:30 +02:00
|
|
|
if (XLogRecPtrIsInvalid(first_valid_record))
|
2022-04-08 09:02:10 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errmsg("could not find a valid record after %X/%X",
|
|
|
|
LSN_FORMAT_ARGS(lsn))));
|
|
|
|
|
|
|
|
return xlogreader;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read next WAL record.
|
|
|
|
*
|
|
|
|
* By design, to be less intrusive in a running system, no slot is allocated
|
|
|
|
* to reserve the WAL we're about to read. Therefore this function can
|
|
|
|
* encounter read errors for historical WAL.
|
|
|
|
*
|
|
|
|
* We guard against ordinary errors trying to read WAL that hasn't been
|
|
|
|
* written yet by limiting end_lsn to the flushed WAL, but that can also
|
2022-04-30 17:28:33 +02:00
|
|
|
* encounter errors if the flush pointer falls in the middle of a record. In
|
|
|
|
* that case we'll return NULL.
|
2022-04-08 09:02:10 +02:00
|
|
|
*/
|
|
|
|
static XLogRecord *
|
2022-08-18 23:23:30 +02:00
|
|
|
ReadNextXLogRecord(XLogReaderState *xlogreader)
|
2022-04-08 09:02:10 +02:00
|
|
|
{
|
|
|
|
XLogRecord *record;
|
2022-05-12 21:17:30 +02:00
|
|
|
char *errormsg;
|
2022-04-08 09:02:10 +02:00
|
|
|
|
|
|
|
record = XLogReadRecord(xlogreader, &errormsg);
|
|
|
|
|
|
|
|
if (record == NULL)
|
|
|
|
{
|
2022-04-30 17:28:33 +02:00
|
|
|
ReadLocalXLogPageNoWaitPrivate *private_data;
|
|
|
|
|
|
|
|
/* return NULL, if end of WAL is reached */
|
|
|
|
private_data = (ReadLocalXLogPageNoWaitPrivate *)
|
2022-05-12 21:17:30 +02:00
|
|
|
xlogreader->private_data;
|
2022-04-30 17:28:33 +02:00
|
|
|
|
|
|
|
if (private_data->end_of_wal)
|
|
|
|
return NULL;
|
|
|
|
|
2022-04-08 09:02:10 +02:00
|
|
|
if (errormsg)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode_for_file_access(),
|
|
|
|
errmsg("could not read WAL at %X/%X: %s",
|
2022-08-18 23:23:30 +02:00
|
|
|
LSN_FORMAT_ARGS(xlogreader->EndRecPtr), errormsg)));
|
2022-04-08 09:02:10 +02:00
|
|
|
else
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode_for_file_access(),
|
|
|
|
errmsg("could not read WAL at %X/%X",
|
2022-08-18 23:23:30 +02:00
|
|
|
LSN_FORMAT_ARGS(xlogreader->EndRecPtr))));
|
2022-04-08 09:02:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return record;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2023-03-30 21:26:12 +02:00
|
|
|
* Output values that make up a row describing caller's WAL record.
|
|
|
|
*
|
|
|
|
* This function leaks memory. Caller may need to use its own custom memory
|
|
|
|
* context.
|
|
|
|
*
|
|
|
|
* Keep this in sync with GetWALBlockInfo.
|
2022-04-08 09:02:10 +02:00
|
|
|
*/
|
|
|
|
static void
|
2022-08-18 23:23:30 +02:00
|
|
|
GetWALRecordInfo(XLogReaderState *record, Datum *values,
|
|
|
|
bool *nulls, uint32 ncols)
|
2022-04-08 09:02:10 +02:00
|
|
|
{
|
2023-03-30 21:26:12 +02:00
|
|
|
const char *record_type;
|
2022-05-12 21:17:30 +02:00
|
|
|
RmgrData desc;
|
|
|
|
uint32 fpi_len = 0;
|
2022-04-08 09:02:10 +02:00
|
|
|
StringInfoData rec_desc;
|
2023-03-28 19:53:48 +02:00
|
|
|
StringInfoData rec_blk_ref;
|
2022-05-12 21:17:30 +02:00
|
|
|
int i = 0;
|
2022-04-08 09:02:10 +02:00
|
|
|
|
|
|
|
desc = GetRmgr(XLogRecGetRmid(record));
|
2023-03-30 21:26:12 +02:00
|
|
|
record_type = desc.rm_identify(XLogRecGetInfo(record));
|
2022-04-08 09:02:10 +02:00
|
|
|
|
2023-03-30 21:26:12 +02:00
|
|
|
if (record_type == NULL)
|
|
|
|
record_type = psprintf("UNKNOWN (%x)", XLogRecGetInfo(record) & ~XLR_INFO_MASK);
|
2022-04-08 09:02:10 +02:00
|
|
|
|
|
|
|
initStringInfo(&rec_desc);
|
|
|
|
desc.rm_desc(&rec_desc, record);
|
|
|
|
|
2023-03-28 19:53:48 +02:00
|
|
|
if (XLogRecHasAnyBlockRefs(record))
|
|
|
|
{
|
|
|
|
initStringInfo(&rec_blk_ref);
|
|
|
|
XLogRecGetBlockRefInfo(record, false, true, &rec_blk_ref, &fpi_len);
|
|
|
|
}
|
|
|
|
|
2022-08-18 23:23:30 +02:00
|
|
|
values[i++] = LSNGetDatum(record->ReadRecPtr);
|
2022-04-08 09:02:10 +02:00
|
|
|
values[i++] = LSNGetDatum(record->EndRecPtr);
|
|
|
|
values[i++] = LSNGetDatum(XLogRecGetPrev(record));
|
|
|
|
values[i++] = TransactionIdGetDatum(XLogRecGetXid(record));
|
|
|
|
values[i++] = CStringGetTextDatum(desc.rm_name);
|
2023-03-30 21:26:12 +02:00
|
|
|
values[i++] = CStringGetTextDatum(record_type);
|
2022-04-08 09:02:10 +02:00
|
|
|
values[i++] = UInt32GetDatum(XLogRecGetTotalLen(record));
|
2023-03-27 06:15:04 +02:00
|
|
|
values[i++] = UInt32GetDatum(XLogRecGetDataLen(record));
|
2022-04-08 09:02:10 +02:00
|
|
|
values[i++] = UInt32GetDatum(fpi_len);
|
2023-03-27 06:15:04 +02:00
|
|
|
|
|
|
|
if (rec_desc.len > 0)
|
|
|
|
values[i++] = CStringGetTextDatum(rec_desc.data);
|
|
|
|
else
|
|
|
|
nulls[i++] = true;
|
|
|
|
|
|
|
|
if (XLogRecHasAnyBlockRefs(record))
|
|
|
|
values[i++] = CStringGetTextDatum(rec_blk_ref.data);
|
|
|
|
else
|
|
|
|
nulls[i++] = true;
|
2022-04-08 09:02:10 +02:00
|
|
|
|
|
|
|
Assert(i == ncols);
|
|
|
|
}
|
|
|
|
|
2023-01-23 05:55:18 +01:00
|
|
|
|
|
|
|
/*
|
2023-03-30 21:26:12 +02:00
|
|
|
* Output one or more rows in rsinfo tuple store, each describing a single
|
|
|
|
* block reference from caller's WAL record. (Should only be called with
|
|
|
|
* records that have block references.)
|
|
|
|
*
|
|
|
|
* This function leaks memory. Caller may need to use its own custom memory
|
|
|
|
* context.
|
|
|
|
*
|
|
|
|
* Keep this in sync with GetWALRecordInfo.
|
2023-01-23 05:55:18 +01:00
|
|
|
*/
|
|
|
|
static void
|
2023-03-31 23:02:52 +02:00
|
|
|
GetWALBlockInfo(FunctionCallInfo fcinfo, XLogReaderState *record,
|
|
|
|
bool show_data)
|
2023-01-23 05:55:18 +01:00
|
|
|
{
|
2023-03-30 21:26:12 +02:00
|
|
|
#define PG_GET_WAL_BLOCK_INFO_COLS 20
|
2023-01-23 05:55:18 +01:00
|
|
|
int block_id;
|
|
|
|
ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
|
2023-03-30 21:26:12 +02:00
|
|
|
RmgrData desc;
|
|
|
|
const char *record_type;
|
|
|
|
StringInfoData rec_desc;
|
|
|
|
|
|
|
|
Assert(XLogRecHasAnyBlockRefs(record));
|
|
|
|
|
|
|
|
desc = GetRmgr(XLogRecGetRmid(record));
|
|
|
|
record_type = desc.rm_identify(XLogRecGetInfo(record));
|
|
|
|
|
|
|
|
if (record_type == NULL)
|
|
|
|
record_type = psprintf("UNKNOWN (%x)",
|
|
|
|
XLogRecGetInfo(record) & ~XLR_INFO_MASK);
|
|
|
|
|
|
|
|
initStringInfo(&rec_desc);
|
|
|
|
desc.rm_desc(&rec_desc, record);
|
2023-01-23 05:55:18 +01:00
|
|
|
|
|
|
|
for (block_id = 0; block_id <= XLogRecMaxBlockId(record); block_id++)
|
|
|
|
{
|
pg_walinspect: pg_get_wal_fpi_info() -> pg_get_wal_block_info()
This commit reworks pg_get_wal_fpi_info() to become aware of all the
block information that can be attached to a record rather than just its
full-page writes:
- Addition of the block id as assigned by XLogRegisterBuffer(),
XLogRegisterBlock() or XLogRegisterBufData().
- Addition of the block data, as bytea, or NULL if none. The length of
the block data can be guessed with length(), so there is no need to
store its length in a separate field.
- Addition of the full-page image length, as counted without a hole or
even compressed.
- Modification of the handling of the full-page image data. This is
still a bytea, but it could become NULL if none is assigned to a block.
- Addition of the full-page image flags, tracking if a page is stored
with a hole, if it needs to be applied and the type of compression
applied to it, as of all the BKPIMAGE_* values in xlogrecord.h.
The information of each block is returned as one single record, with the
record's ReadRecPtr included to be able to join the block information
with the existing pg_get_wal_records_info(). Note that it is perfectly
possible for a block to hold both data and full-page image.
Thanks also to Kyotaro Horiguchi and Matthias van de Meent for the
discussion.
This commit uses some of the work proposed by Melanie, though it has
been largely redesigned and rewritten by me. Bharath has helped in
refining a bit the whole.
Reported-by: Melanie Plageman
Author: Michael Paquier, Melanie Plageman, Bharath Rupireddy
Discussion: https://postgr.es/m/CAAKRu_bORebdZmcV8V4cZBzU8M_C6tDDdbiPhCZ6i-iuSXW9TA@mail.gmail.com
2023-03-10 02:09:07 +01:00
|
|
|
DecodedBkpBlock *blk;
|
|
|
|
BlockNumber blkno;
|
2023-01-23 05:55:18 +01:00
|
|
|
RelFileLocator rnode;
|
2023-03-30 21:26:12 +02:00
|
|
|
ForkNumber forknum;
|
pg_walinspect: pg_get_wal_fpi_info() -> pg_get_wal_block_info()
This commit reworks pg_get_wal_fpi_info() to become aware of all the
block information that can be attached to a record rather than just its
full-page writes:
- Addition of the block id as assigned by XLogRegisterBuffer(),
XLogRegisterBlock() or XLogRegisterBufData().
- Addition of the block data, as bytea, or NULL if none. The length of
the block data can be guessed with length(), so there is no need to
store its length in a separate field.
- Addition of the full-page image length, as counted without a hole or
even compressed.
- Modification of the handling of the full-page image data. This is
still a bytea, but it could become NULL if none is assigned to a block.
- Addition of the full-page image flags, tracking if a page is stored
with a hole, if it needs to be applied and the type of compression
applied to it, as of all the BKPIMAGE_* values in xlogrecord.h.
The information of each block is returned as one single record, with the
record's ReadRecPtr included to be able to join the block information
with the existing pg_get_wal_records_info(). Note that it is perfectly
possible for a block to hold both data and full-page image.
Thanks also to Kyotaro Horiguchi and Matthias van de Meent for the
discussion.
This commit uses some of the work proposed by Melanie, though it has
been largely redesigned and rewritten by me. Bharath has helped in
refining a bit the whole.
Reported-by: Melanie Plageman
Author: Michael Paquier, Melanie Plageman, Bharath Rupireddy
Discussion: https://postgr.es/m/CAAKRu_bORebdZmcV8V4cZBzU8M_C6tDDdbiPhCZ6i-iuSXW9TA@mail.gmail.com
2023-03-10 02:09:07 +01:00
|
|
|
Datum values[PG_GET_WAL_BLOCK_INFO_COLS] = {0};
|
|
|
|
bool nulls[PG_GET_WAL_BLOCK_INFO_COLS] = {0};
|
2023-03-30 21:26:12 +02:00
|
|
|
uint32 block_data_len = 0,
|
|
|
|
block_fpi_len = 0;
|
|
|
|
ArrayType *block_fpi_info = NULL;
|
2023-01-23 05:55:18 +01:00
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
if (!XLogRecHasBlockRef(record, block_id))
|
|
|
|
continue;
|
|
|
|
|
pg_walinspect: pg_get_wal_fpi_info() -> pg_get_wal_block_info()
This commit reworks pg_get_wal_fpi_info() to become aware of all the
block information that can be attached to a record rather than just its
full-page writes:
- Addition of the block id as assigned by XLogRegisterBuffer(),
XLogRegisterBlock() or XLogRegisterBufData().
- Addition of the block data, as bytea, or NULL if none. The length of
the block data can be guessed with length(), so there is no need to
store its length in a separate field.
- Addition of the full-page image length, as counted without a hole or
even compressed.
- Modification of the handling of the full-page image data. This is
still a bytea, but it could become NULL if none is assigned to a block.
- Addition of the full-page image flags, tracking if a page is stored
with a hole, if it needs to be applied and the type of compression
applied to it, as of all the BKPIMAGE_* values in xlogrecord.h.
The information of each block is returned as one single record, with the
record's ReadRecPtr included to be able to join the block information
with the existing pg_get_wal_records_info(). Note that it is perfectly
possible for a block to hold both data and full-page image.
Thanks also to Kyotaro Horiguchi and Matthias van de Meent for the
discussion.
This commit uses some of the work proposed by Melanie, though it has
been largely redesigned and rewritten by me. Bharath has helped in
refining a bit the whole.
Reported-by: Melanie Plageman
Author: Michael Paquier, Melanie Plageman, Bharath Rupireddy
Discussion: https://postgr.es/m/CAAKRu_bORebdZmcV8V4cZBzU8M_C6tDDdbiPhCZ6i-iuSXW9TA@mail.gmail.com
2023-03-10 02:09:07 +01:00
|
|
|
blk = XLogRecGetBlock(record, block_id);
|
2023-01-23 05:55:18 +01:00
|
|
|
|
|
|
|
(void) XLogRecGetBlockTagExtended(record, block_id,
|
2023-03-30 21:26:12 +02:00
|
|
|
&rnode, &forknum, &blkno, NULL);
|
2023-01-23 05:55:18 +01:00
|
|
|
|
2023-03-30 21:26:12 +02:00
|
|
|
/* Save block_data_len */
|
|
|
|
if (blk->has_data)
|
|
|
|
block_data_len = blk->data_len;
|
|
|
|
|
|
|
|
if (blk->has_image)
|
|
|
|
{
|
|
|
|
/* Block reference has an FPI, so prepare relevant output */
|
|
|
|
int bitcnt;
|
|
|
|
int cnt = 0;
|
|
|
|
Datum *flags;
|
|
|
|
|
|
|
|
/* Save block_fpi_len */
|
|
|
|
block_fpi_len = blk->bimg_len;
|
|
|
|
|
|
|
|
/* Construct and save block_fpi_info */
|
|
|
|
bitcnt = pg_popcount((const char *) &blk->bimg_info,
|
|
|
|
sizeof(uint8));
|
|
|
|
flags = (Datum *) palloc0(sizeof(Datum) * bitcnt);
|
|
|
|
if ((blk->bimg_info & BKPIMAGE_HAS_HOLE) != 0)
|
|
|
|
flags[cnt++] = CStringGetTextDatum("HAS_HOLE");
|
|
|
|
if (blk->apply_image)
|
|
|
|
flags[cnt++] = CStringGetTextDatum("APPLY");
|
|
|
|
if ((blk->bimg_info & BKPIMAGE_COMPRESS_PGLZ) != 0)
|
|
|
|
flags[cnt++] = CStringGetTextDatum("COMPRESS_PGLZ");
|
|
|
|
if ((blk->bimg_info & BKPIMAGE_COMPRESS_LZ4) != 0)
|
|
|
|
flags[cnt++] = CStringGetTextDatum("COMPRESS_LZ4");
|
|
|
|
if ((blk->bimg_info & BKPIMAGE_COMPRESS_ZSTD) != 0)
|
|
|
|
flags[cnt++] = CStringGetTextDatum("COMPRESS_ZSTD");
|
|
|
|
|
|
|
|
Assert(cnt <= bitcnt);
|
|
|
|
block_fpi_info = construct_array_builtin(flags, cnt, TEXTOID);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* start_lsn, end_lsn, prev_lsn, and blockid outputs */
|
2023-01-23 05:55:18 +01:00
|
|
|
values[i++] = LSNGetDatum(record->ReadRecPtr);
|
2023-03-30 21:26:12 +02:00
|
|
|
values[i++] = LSNGetDatum(record->EndRecPtr);
|
|
|
|
values[i++] = LSNGetDatum(XLogRecGetPrev(record));
|
pg_walinspect: pg_get_wal_fpi_info() -> pg_get_wal_block_info()
This commit reworks pg_get_wal_fpi_info() to become aware of all the
block information that can be attached to a record rather than just its
full-page writes:
- Addition of the block id as assigned by XLogRegisterBuffer(),
XLogRegisterBlock() or XLogRegisterBufData().
- Addition of the block data, as bytea, or NULL if none. The length of
the block data can be guessed with length(), so there is no need to
store its length in a separate field.
- Addition of the full-page image length, as counted without a hole or
even compressed.
- Modification of the handling of the full-page image data. This is
still a bytea, but it could become NULL if none is assigned to a block.
- Addition of the full-page image flags, tracking if a page is stored
with a hole, if it needs to be applied and the type of compression
applied to it, as of all the BKPIMAGE_* values in xlogrecord.h.
The information of each block is returned as one single record, with the
record's ReadRecPtr included to be able to join the block information
with the existing pg_get_wal_records_info(). Note that it is perfectly
possible for a block to hold both data and full-page image.
Thanks also to Kyotaro Horiguchi and Matthias van de Meent for the
discussion.
This commit uses some of the work proposed by Melanie, though it has
been largely redesigned and rewritten by me. Bharath has helped in
refining a bit the whole.
Reported-by: Melanie Plageman
Author: Michael Paquier, Melanie Plageman, Bharath Rupireddy
Discussion: https://postgr.es/m/CAAKRu_bORebdZmcV8V4cZBzU8M_C6tDDdbiPhCZ6i-iuSXW9TA@mail.gmail.com
2023-03-10 02:09:07 +01:00
|
|
|
values[i++] = Int16GetDatum(block_id);
|
2023-03-30 21:26:12 +02:00
|
|
|
|
|
|
|
/* relfile and block related outputs */
|
pg_walinspect: pg_get_wal_fpi_info() -> pg_get_wal_block_info()
This commit reworks pg_get_wal_fpi_info() to become aware of all the
block information that can be attached to a record rather than just its
full-page writes:
- Addition of the block id as assigned by XLogRegisterBuffer(),
XLogRegisterBlock() or XLogRegisterBufData().
- Addition of the block data, as bytea, or NULL if none. The length of
the block data can be guessed with length(), so there is no need to
store its length in a separate field.
- Addition of the full-page image length, as counted without a hole or
even compressed.
- Modification of the handling of the full-page image data. This is
still a bytea, but it could become NULL if none is assigned to a block.
- Addition of the full-page image flags, tracking if a page is stored
with a hole, if it needs to be applied and the type of compression
applied to it, as of all the BKPIMAGE_* values in xlogrecord.h.
The information of each block is returned as one single record, with the
record's ReadRecPtr included to be able to join the block information
with the existing pg_get_wal_records_info(). Note that it is perfectly
possible for a block to hold both data and full-page image.
Thanks also to Kyotaro Horiguchi and Matthias van de Meent for the
discussion.
This commit uses some of the work proposed by Melanie, though it has
been largely redesigned and rewritten by me. Bharath has helped in
refining a bit the whole.
Reported-by: Melanie Plageman
Author: Michael Paquier, Melanie Plageman, Bharath Rupireddy
Discussion: https://postgr.es/m/CAAKRu_bORebdZmcV8V4cZBzU8M_C6tDDdbiPhCZ6i-iuSXW9TA@mail.gmail.com
2023-03-10 02:09:07 +01:00
|
|
|
values[i++] = ObjectIdGetDatum(blk->rlocator.spcOid);
|
|
|
|
values[i++] = ObjectIdGetDatum(blk->rlocator.dbOid);
|
|
|
|
values[i++] = ObjectIdGetDatum(blk->rlocator.relNumber);
|
2023-03-30 21:26:12 +02:00
|
|
|
values[i++] = Int16GetDatum(forknum);
|
pg_walinspect: pg_get_wal_fpi_info() -> pg_get_wal_block_info()
This commit reworks pg_get_wal_fpi_info() to become aware of all the
block information that can be attached to a record rather than just its
full-page writes:
- Addition of the block id as assigned by XLogRegisterBuffer(),
XLogRegisterBlock() or XLogRegisterBufData().
- Addition of the block data, as bytea, or NULL if none. The length of
the block data can be guessed with length(), so there is no need to
store its length in a separate field.
- Addition of the full-page image length, as counted without a hole or
even compressed.
- Modification of the handling of the full-page image data. This is
still a bytea, but it could become NULL if none is assigned to a block.
- Addition of the full-page image flags, tracking if a page is stored
with a hole, if it needs to be applied and the type of compression
applied to it, as of all the BKPIMAGE_* values in xlogrecord.h.
The information of each block is returned as one single record, with the
record's ReadRecPtr included to be able to join the block information
with the existing pg_get_wal_records_info(). Note that it is perfectly
possible for a block to hold both data and full-page image.
Thanks also to Kyotaro Horiguchi and Matthias van de Meent for the
discussion.
This commit uses some of the work proposed by Melanie, though it has
been largely redesigned and rewritten by me. Bharath has helped in
refining a bit the whole.
Reported-by: Melanie Plageman
Author: Michael Paquier, Melanie Plageman, Bharath Rupireddy
Discussion: https://postgr.es/m/CAAKRu_bORebdZmcV8V4cZBzU8M_C6tDDdbiPhCZ6i-iuSXW9TA@mail.gmail.com
2023-03-10 02:09:07 +01:00
|
|
|
values[i++] = Int64GetDatum((int64) blkno);
|
2023-01-23 05:55:18 +01:00
|
|
|
|
2023-03-30 21:26:12 +02:00
|
|
|
/* xid, resource_manager, and record_type outputs */
|
|
|
|
values[i++] = TransactionIdGetDatum(XLogRecGetXid(record));
|
|
|
|
values[i++] = CStringGetTextDatum(desc.rm_name);
|
|
|
|
values[i++] = CStringGetTextDatum(record_type);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* record_length, main_data_length, block_data_len, and
|
|
|
|
* block_fpi_length outputs
|
|
|
|
*/
|
|
|
|
values[i++] = UInt32GetDatum(XLogRecGetTotalLen(record));
|
|
|
|
values[i++] = UInt32GetDatum(XLogRecGetDataLen(record));
|
|
|
|
values[i++] = UInt32GetDatum(block_data_len);
|
|
|
|
values[i++] = UInt32GetDatum(block_fpi_len);
|
|
|
|
|
|
|
|
/* block_fpi_info (text array) output */
|
|
|
|
if (block_fpi_info)
|
|
|
|
values[i++] = PointerGetDatum(block_fpi_info);
|
2023-01-23 05:55:18 +01:00
|
|
|
else
|
2023-03-30 21:26:12 +02:00
|
|
|
nulls[i++] = true;
|
2023-01-23 05:55:18 +01:00
|
|
|
|
2023-03-30 21:26:12 +02:00
|
|
|
/* description output (describes WAL record) */
|
|
|
|
if (rec_desc.len > 0)
|
|
|
|
values[i++] = CStringGetTextDatum(rec_desc.data);
|
|
|
|
else
|
|
|
|
nulls[i++] = true;
|
|
|
|
|
|
|
|
/* block_data output */
|
2023-03-31 23:02:52 +02:00
|
|
|
if (blk->has_data && show_data)
|
pg_walinspect: pg_get_wal_fpi_info() -> pg_get_wal_block_info()
This commit reworks pg_get_wal_fpi_info() to become aware of all the
block information that can be attached to a record rather than just its
full-page writes:
- Addition of the block id as assigned by XLogRegisterBuffer(),
XLogRegisterBlock() or XLogRegisterBufData().
- Addition of the block data, as bytea, or NULL if none. The length of
the block data can be guessed with length(), so there is no need to
store its length in a separate field.
- Addition of the full-page image length, as counted without a hole or
even compressed.
- Modification of the handling of the full-page image data. This is
still a bytea, but it could become NULL if none is assigned to a block.
- Addition of the full-page image flags, tracking if a page is stored
with a hole, if it needs to be applied and the type of compression
applied to it, as of all the BKPIMAGE_* values in xlogrecord.h.
The information of each block is returned as one single record, with the
record's ReadRecPtr included to be able to join the block information
with the existing pg_get_wal_records_info(). Note that it is perfectly
possible for a block to hold both data and full-page image.
Thanks also to Kyotaro Horiguchi and Matthias van de Meent for the
discussion.
This commit uses some of the work proposed by Melanie, though it has
been largely redesigned and rewritten by me. Bharath has helped in
refining a bit the whole.
Reported-by: Melanie Plageman
Author: Michael Paquier, Melanie Plageman, Bharath Rupireddy
Discussion: https://postgr.es/m/CAAKRu_bORebdZmcV8V4cZBzU8M_C6tDDdbiPhCZ6i-iuSXW9TA@mail.gmail.com
2023-03-10 02:09:07 +01:00
|
|
|
{
|
2023-03-30 21:26:12 +02:00
|
|
|
bytea *block_data;
|
2023-01-23 05:55:18 +01:00
|
|
|
|
2023-03-30 21:26:12 +02:00
|
|
|
block_data = (bytea *) palloc(block_data_len + VARHDRSZ);
|
|
|
|
SET_VARSIZE(block_data, block_data_len + VARHDRSZ);
|
|
|
|
memcpy(VARDATA(block_data), blk->data, block_data_len);
|
|
|
|
values[i++] = PointerGetDatum(block_data);
|
pg_walinspect: pg_get_wal_fpi_info() -> pg_get_wal_block_info()
This commit reworks pg_get_wal_fpi_info() to become aware of all the
block information that can be attached to a record rather than just its
full-page writes:
- Addition of the block id as assigned by XLogRegisterBuffer(),
XLogRegisterBlock() or XLogRegisterBufData().
- Addition of the block data, as bytea, or NULL if none. The length of
the block data can be guessed with length(), so there is no need to
store its length in a separate field.
- Addition of the full-page image length, as counted without a hole or
even compressed.
- Modification of the handling of the full-page image data. This is
still a bytea, but it could become NULL if none is assigned to a block.
- Addition of the full-page image flags, tracking if a page is stored
with a hole, if it needs to be applied and the type of compression
applied to it, as of all the BKPIMAGE_* values in xlogrecord.h.
The information of each block is returned as one single record, with the
record's ReadRecPtr included to be able to join the block information
with the existing pg_get_wal_records_info(). Note that it is perfectly
possible for a block to hold both data and full-page image.
Thanks also to Kyotaro Horiguchi and Matthias van de Meent for the
discussion.
This commit uses some of the work proposed by Melanie, though it has
been largely redesigned and rewritten by me. Bharath has helped in
refining a bit the whole.
Reported-by: Melanie Plageman
Author: Michael Paquier, Melanie Plageman, Bharath Rupireddy
Discussion: https://postgr.es/m/CAAKRu_bORebdZmcV8V4cZBzU8M_C6tDDdbiPhCZ6i-iuSXW9TA@mail.gmail.com
2023-03-10 02:09:07 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
nulls[i++] = true;
|
|
|
|
|
2023-03-30 21:26:12 +02:00
|
|
|
/* block_fpi_data output */
|
2023-03-31 23:02:52 +02:00
|
|
|
if (blk->has_image && show_data)
|
pg_walinspect: pg_get_wal_fpi_info() -> pg_get_wal_block_info()
This commit reworks pg_get_wal_fpi_info() to become aware of all the
block information that can be attached to a record rather than just its
full-page writes:
- Addition of the block id as assigned by XLogRegisterBuffer(),
XLogRegisterBlock() or XLogRegisterBufData().
- Addition of the block data, as bytea, or NULL if none. The length of
the block data can be guessed with length(), so there is no need to
store its length in a separate field.
- Addition of the full-page image length, as counted without a hole or
even compressed.
- Modification of the handling of the full-page image data. This is
still a bytea, but it could become NULL if none is assigned to a block.
- Addition of the full-page image flags, tracking if a page is stored
with a hole, if it needs to be applied and the type of compression
applied to it, as of all the BKPIMAGE_* values in xlogrecord.h.
The information of each block is returned as one single record, with the
record's ReadRecPtr included to be able to join the block information
with the existing pg_get_wal_records_info(). Note that it is perfectly
possible for a block to hold both data and full-page image.
Thanks also to Kyotaro Horiguchi and Matthias van de Meent for the
discussion.
This commit uses some of the work proposed by Melanie, though it has
been largely redesigned and rewritten by me. Bharath has helped in
refining a bit the whole.
Reported-by: Melanie Plageman
Author: Michael Paquier, Melanie Plageman, Bharath Rupireddy
Discussion: https://postgr.es/m/CAAKRu_bORebdZmcV8V4cZBzU8M_C6tDDdbiPhCZ6i-iuSXW9TA@mail.gmail.com
2023-03-10 02:09:07 +01:00
|
|
|
{
|
|
|
|
PGAlignedBlock buf;
|
|
|
|
Page page;
|
2023-03-30 21:26:12 +02:00
|
|
|
bytea *block_fpi_data;
|
pg_walinspect: pg_get_wal_fpi_info() -> pg_get_wal_block_info()
This commit reworks pg_get_wal_fpi_info() to become aware of all the
block information that can be attached to a record rather than just its
full-page writes:
- Addition of the block id as assigned by XLogRegisterBuffer(),
XLogRegisterBlock() or XLogRegisterBufData().
- Addition of the block data, as bytea, or NULL if none. The length of
the block data can be guessed with length(), so there is no need to
store its length in a separate field.
- Addition of the full-page image length, as counted without a hole or
even compressed.
- Modification of the handling of the full-page image data. This is
still a bytea, but it could become NULL if none is assigned to a block.
- Addition of the full-page image flags, tracking if a page is stored
with a hole, if it needs to be applied and the type of compression
applied to it, as of all the BKPIMAGE_* values in xlogrecord.h.
The information of each block is returned as one single record, with the
record's ReadRecPtr included to be able to join the block information
with the existing pg_get_wal_records_info(). Note that it is perfectly
possible for a block to hold both data and full-page image.
Thanks also to Kyotaro Horiguchi and Matthias van de Meent for the
discussion.
This commit uses some of the work proposed by Melanie, though it has
been largely redesigned and rewritten by me. Bharath has helped in
refining a bit the whole.
Reported-by: Melanie Plageman
Author: Michael Paquier, Melanie Plageman, Bharath Rupireddy
Discussion: https://postgr.es/m/CAAKRu_bORebdZmcV8V4cZBzU8M_C6tDDdbiPhCZ6i-iuSXW9TA@mail.gmail.com
2023-03-10 02:09:07 +01:00
|
|
|
|
|
|
|
page = (Page) buf.data;
|
|
|
|
if (!RestoreBlockImage(record, block_id, page))
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INTERNAL_ERROR),
|
|
|
|
errmsg_internal("%s", record->errormsg_buf)));
|
|
|
|
|
2023-03-30 21:26:12 +02:00
|
|
|
block_fpi_data = (bytea *) palloc(BLCKSZ + VARHDRSZ);
|
|
|
|
SET_VARSIZE(block_fpi_data, BLCKSZ + VARHDRSZ);
|
|
|
|
memcpy(VARDATA(block_fpi_data), page, BLCKSZ);
|
|
|
|
values[i++] = PointerGetDatum(block_fpi_data);
|
pg_walinspect: pg_get_wal_fpi_info() -> pg_get_wal_block_info()
This commit reworks pg_get_wal_fpi_info() to become aware of all the
block information that can be attached to a record rather than just its
full-page writes:
- Addition of the block id as assigned by XLogRegisterBuffer(),
XLogRegisterBlock() or XLogRegisterBufData().
- Addition of the block data, as bytea, or NULL if none. The length of
the block data can be guessed with length(), so there is no need to
store its length in a separate field.
- Addition of the full-page image length, as counted without a hole or
even compressed.
- Modification of the handling of the full-page image data. This is
still a bytea, but it could become NULL if none is assigned to a block.
- Addition of the full-page image flags, tracking if a page is stored
with a hole, if it needs to be applied and the type of compression
applied to it, as of all the BKPIMAGE_* values in xlogrecord.h.
The information of each block is returned as one single record, with the
record's ReadRecPtr included to be able to join the block information
with the existing pg_get_wal_records_info(). Note that it is perfectly
possible for a block to hold both data and full-page image.
Thanks also to Kyotaro Horiguchi and Matthias van de Meent for the
discussion.
This commit uses some of the work proposed by Melanie, though it has
been largely redesigned and rewritten by me. Bharath has helped in
refining a bit the whole.
Reported-by: Melanie Plageman
Author: Michael Paquier, Melanie Plageman, Bharath Rupireddy
Discussion: https://postgr.es/m/CAAKRu_bORebdZmcV8V4cZBzU8M_C6tDDdbiPhCZ6i-iuSXW9TA@mail.gmail.com
2023-03-10 02:09:07 +01:00
|
|
|
}
|
|
|
|
else
|
2023-03-30 21:26:12 +02:00
|
|
|
nulls[i++] = true;
|
2023-01-23 05:55:18 +01:00
|
|
|
|
pg_walinspect: pg_get_wal_fpi_info() -> pg_get_wal_block_info()
This commit reworks pg_get_wal_fpi_info() to become aware of all the
block information that can be attached to a record rather than just its
full-page writes:
- Addition of the block id as assigned by XLogRegisterBuffer(),
XLogRegisterBlock() or XLogRegisterBufData().
- Addition of the block data, as bytea, or NULL if none. The length of
the block data can be guessed with length(), so there is no need to
store its length in a separate field.
- Addition of the full-page image length, as counted without a hole or
even compressed.
- Modification of the handling of the full-page image data. This is
still a bytea, but it could become NULL if none is assigned to a block.
- Addition of the full-page image flags, tracking if a page is stored
with a hole, if it needs to be applied and the type of compression
applied to it, as of all the BKPIMAGE_* values in xlogrecord.h.
The information of each block is returned as one single record, with the
record's ReadRecPtr included to be able to join the block information
with the existing pg_get_wal_records_info(). Note that it is perfectly
possible for a block to hold both data and full-page image.
Thanks also to Kyotaro Horiguchi and Matthias van de Meent for the
discussion.
This commit uses some of the work proposed by Melanie, though it has
been largely redesigned and rewritten by me. Bharath has helped in
refining a bit the whole.
Reported-by: Melanie Plageman
Author: Michael Paquier, Melanie Plageman, Bharath Rupireddy
Discussion: https://postgr.es/m/CAAKRu_bORebdZmcV8V4cZBzU8M_C6tDDdbiPhCZ6i-iuSXW9TA@mail.gmail.com
2023-03-10 02:09:07 +01:00
|
|
|
Assert(i == PG_GET_WAL_BLOCK_INFO_COLS);
|
2023-01-23 05:55:18 +01:00
|
|
|
|
2023-03-30 21:26:12 +02:00
|
|
|
/* Store a tuple for this block reference */
|
2023-01-23 05:55:18 +01:00
|
|
|
tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc,
|
|
|
|
values, nulls);
|
|
|
|
}
|
|
|
|
|
2023-04-18 03:23:23 +02:00
|
|
|
#undef PG_GET_WAL_BLOCK_INFO_COLS
|
2023-01-23 05:55:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2023-03-30 21:26:12 +02:00
|
|
|
* Get WAL record info, unnested by block reference
|
2023-01-23 05:55:18 +01:00
|
|
|
*/
|
|
|
|
Datum
|
pg_walinspect: pg_get_wal_fpi_info() -> pg_get_wal_block_info()
This commit reworks pg_get_wal_fpi_info() to become aware of all the
block information that can be attached to a record rather than just its
full-page writes:
- Addition of the block id as assigned by XLogRegisterBuffer(),
XLogRegisterBlock() or XLogRegisterBufData().
- Addition of the block data, as bytea, or NULL if none. The length of
the block data can be guessed with length(), so there is no need to
store its length in a separate field.
- Addition of the full-page image length, as counted without a hole or
even compressed.
- Modification of the handling of the full-page image data. This is
still a bytea, but it could become NULL if none is assigned to a block.
- Addition of the full-page image flags, tracking if a page is stored
with a hole, if it needs to be applied and the type of compression
applied to it, as of all the BKPIMAGE_* values in xlogrecord.h.
The information of each block is returned as one single record, with the
record's ReadRecPtr included to be able to join the block information
with the existing pg_get_wal_records_info(). Note that it is perfectly
possible for a block to hold both data and full-page image.
Thanks also to Kyotaro Horiguchi and Matthias van de Meent for the
discussion.
This commit uses some of the work proposed by Melanie, though it has
been largely redesigned and rewritten by me. Bharath has helped in
refining a bit the whole.
Reported-by: Melanie Plageman
Author: Michael Paquier, Melanie Plageman, Bharath Rupireddy
Discussion: https://postgr.es/m/CAAKRu_bORebdZmcV8V4cZBzU8M_C6tDDdbiPhCZ6i-iuSXW9TA@mail.gmail.com
2023-03-10 02:09:07 +01:00
|
|
|
pg_get_wal_block_info(PG_FUNCTION_ARGS)
|
2023-01-23 05:55:18 +01:00
|
|
|
{
|
2023-03-14 12:13:02 +01:00
|
|
|
XLogRecPtr start_lsn = PG_GETARG_LSN(0);
|
|
|
|
XLogRecPtr end_lsn = PG_GETARG_LSN(1);
|
2023-03-31 23:02:52 +02:00
|
|
|
bool show_data = PG_GETARG_BOOL(2);
|
2023-01-23 05:55:18 +01:00
|
|
|
XLogReaderState *xlogreader;
|
2023-02-20 19:29:53 +01:00
|
|
|
MemoryContext old_cxt;
|
|
|
|
MemoryContext tmp_cxt;
|
2023-01-23 05:55:18 +01:00
|
|
|
|
2023-03-14 12:13:02 +01:00
|
|
|
ValidateInputLSNs(start_lsn, &end_lsn);
|
2023-01-23 05:55:18 +01:00
|
|
|
|
|
|
|
InitMaterializedSRF(fcinfo, 0);
|
|
|
|
|
|
|
|
xlogreader = InitXLogReaderState(start_lsn);
|
|
|
|
|
2023-02-20 19:29:53 +01:00
|
|
|
tmp_cxt = AllocSetContextCreate(CurrentMemoryContext,
|
2023-03-28 01:14:32 +02:00
|
|
|
"pg_get_wal_block_info temporary cxt",
|
2023-02-20 19:29:53 +01:00
|
|
|
ALLOCSET_DEFAULT_SIZES);
|
|
|
|
|
2023-01-23 05:55:18 +01:00
|
|
|
while (ReadNextXLogRecord(xlogreader) &&
|
|
|
|
xlogreader->EndRecPtr <= end_lsn)
|
|
|
|
{
|
2023-03-27 06:15:04 +02:00
|
|
|
CHECK_FOR_INTERRUPTS();
|
|
|
|
|
|
|
|
if (!XLogRecHasAnyBlockRefs(xlogreader))
|
|
|
|
continue;
|
|
|
|
|
2023-02-20 19:29:53 +01:00
|
|
|
/* Use the tmp context so we can clean up after each tuple is done */
|
|
|
|
old_cxt = MemoryContextSwitchTo(tmp_cxt);
|
|
|
|
|
2023-03-31 23:02:52 +02:00
|
|
|
GetWALBlockInfo(fcinfo, xlogreader, show_data);
|
2023-01-23 05:55:18 +01:00
|
|
|
|
2023-02-20 19:29:53 +01:00
|
|
|
/* clean up and switch back */
|
|
|
|
MemoryContextSwitchTo(old_cxt);
|
|
|
|
MemoryContextReset(tmp_cxt);
|
2023-01-23 05:55:18 +01:00
|
|
|
}
|
|
|
|
|
2023-02-20 19:29:53 +01:00
|
|
|
MemoryContextDelete(tmp_cxt);
|
2023-01-23 05:55:18 +01:00
|
|
|
pfree(xlogreader->private_data);
|
|
|
|
XLogReaderFree(xlogreader);
|
|
|
|
|
|
|
|
PG_RETURN_VOID();
|
|
|
|
}
|
|
|
|
|
2022-04-08 09:02:10 +02:00
|
|
|
/*
|
|
|
|
* Get WAL record info.
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
pg_get_wal_record_info(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
#define PG_GET_WAL_RECORD_INFO_COLS 11
|
2022-05-12 21:17:30 +02:00
|
|
|
Datum result;
|
2022-07-16 08:42:15 +02:00
|
|
|
Datum values[PG_GET_WAL_RECORD_INFO_COLS] = {0};
|
|
|
|
bool nulls[PG_GET_WAL_RECORD_INFO_COLS] = {0};
|
2022-04-08 09:02:10 +02:00
|
|
|
XLogRecPtr lsn;
|
|
|
|
XLogRecPtr curr_lsn;
|
|
|
|
XLogReaderState *xlogreader;
|
|
|
|
TupleDesc tupdesc;
|
|
|
|
HeapTuple tuple;
|
|
|
|
|
|
|
|
lsn = PG_GETARG_LSN(0);
|
2023-03-14 12:13:02 +01:00
|
|
|
curr_lsn = GetCurrentLSN();
|
2022-04-08 09:02:10 +02:00
|
|
|
|
2023-03-14 12:13:02 +01:00
|
|
|
if (lsn > curr_lsn)
|
2022-04-08 09:02:10 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
2023-03-14 12:13:02 +01:00
|
|
|
errmsg("WAL input LSN must be less than current LSN"),
|
|
|
|
errdetail("Current WAL LSN on the database system is at %X/%X.",
|
2022-04-08 09:02:10 +02:00
|
|
|
LSN_FORMAT_ARGS(curr_lsn))));
|
|
|
|
|
|
|
|
/* Build a tuple descriptor for our result type. */
|
|
|
|
if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
|
|
|
|
elog(ERROR, "return type must be a row type");
|
|
|
|
|
2022-08-18 23:23:30 +02:00
|
|
|
xlogreader = InitXLogReaderState(lsn);
|
2022-04-08 09:02:10 +02:00
|
|
|
|
2022-08-18 23:23:30 +02:00
|
|
|
if (!ReadNextXLogRecord(xlogreader))
|
2022-04-30 17:28:33 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
|
|
errmsg("could not read WAL at %X/%X",
|
2022-08-18 23:23:30 +02:00
|
|
|
LSN_FORMAT_ARGS(xlogreader->EndRecPtr))));
|
2022-04-08 09:02:10 +02:00
|
|
|
|
2022-08-18 23:23:30 +02:00
|
|
|
GetWALRecordInfo(xlogreader, values, nulls, PG_GET_WAL_RECORD_INFO_COLS);
|
2022-04-08 09:02:10 +02:00
|
|
|
|
2022-04-30 17:28:33 +02:00
|
|
|
pfree(xlogreader->private_data);
|
2022-04-08 09:02:10 +02:00
|
|
|
XLogReaderFree(xlogreader);
|
|
|
|
|
|
|
|
tuple = heap_form_tuple(tupdesc, values, nulls);
|
|
|
|
result = HeapTupleGetDatum(tuple);
|
|
|
|
|
|
|
|
PG_RETURN_DATUM(result);
|
|
|
|
#undef PG_GET_WAL_RECORD_INFO_COLS
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2023-03-14 12:13:02 +01:00
|
|
|
* Validate start and end LSNs coming from the function inputs.
|
|
|
|
*
|
|
|
|
* If end_lsn is found to be higher than the current LSN reported by the
|
|
|
|
* cluster, use the current LSN as the upper bound.
|
2022-04-08 09:02:10 +02:00
|
|
|
*/
|
2023-03-14 12:13:02 +01:00
|
|
|
static void
|
|
|
|
ValidateInputLSNs(XLogRecPtr start_lsn, XLogRecPtr *end_lsn)
|
2022-04-08 09:02:10 +02:00
|
|
|
{
|
2023-03-14 12:13:02 +01:00
|
|
|
XLogRecPtr curr_lsn = GetCurrentLSN();
|
2022-04-08 09:02:10 +02:00
|
|
|
|
2023-03-14 12:13:02 +01:00
|
|
|
if (start_lsn > curr_lsn)
|
2022-04-08 09:02:10 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
2023-03-14 12:13:02 +01:00
|
|
|
errmsg("WAL start LSN must be less than current LSN"),
|
|
|
|
errdetail("Current WAL LSN on the database system is at %X/%X.",
|
2022-04-08 09:02:10 +02:00
|
|
|
LSN_FORMAT_ARGS(curr_lsn))));
|
|
|
|
|
2023-03-14 12:13:02 +01:00
|
|
|
if (start_lsn > *end_lsn)
|
2022-04-08 09:02:10 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
|
|
errmsg("WAL start LSN must be less than end LSN")));
|
|
|
|
|
2023-03-14 12:13:02 +01:00
|
|
|
if (*end_lsn > curr_lsn)
|
|
|
|
*end_lsn = curr_lsn;
|
2022-04-08 09:02:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2023-03-30 21:26:12 +02:00
|
|
|
* Get info of all WAL records between start LSN and end LSN.
|
2022-04-08 09:02:10 +02:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
GetWALRecordsInfo(FunctionCallInfo fcinfo, XLogRecPtr start_lsn,
|
|
|
|
XLogRecPtr end_lsn)
|
|
|
|
{
|
|
|
|
#define PG_GET_WAL_RECORDS_INFO_COLS 11
|
|
|
|
XLogReaderState *xlogreader;
|
|
|
|
ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
|
2023-02-20 19:29:53 +01:00
|
|
|
MemoryContext old_cxt;
|
|
|
|
MemoryContext tmp_cxt;
|
2022-04-08 09:02:10 +02:00
|
|
|
|
2023-03-14 12:13:02 +01:00
|
|
|
Assert(start_lsn <= end_lsn);
|
|
|
|
|
2022-10-18 03:22:35 +02:00
|
|
|
InitMaterializedSRF(fcinfo, 0);
|
2022-04-08 09:02:10 +02:00
|
|
|
|
2022-08-18 23:23:30 +02:00
|
|
|
xlogreader = InitXLogReaderState(start_lsn);
|
2022-04-08 09:02:10 +02:00
|
|
|
|
2023-02-20 19:29:53 +01:00
|
|
|
tmp_cxt = AllocSetContextCreate(CurrentMemoryContext,
|
|
|
|
"GetWALRecordsInfo temporary cxt",
|
|
|
|
ALLOCSET_DEFAULT_SIZES);
|
|
|
|
|
2022-08-18 23:23:30 +02:00
|
|
|
while (ReadNextXLogRecord(xlogreader) &&
|
2022-04-30 17:28:33 +02:00
|
|
|
xlogreader->EndRecPtr <= end_lsn)
|
2022-04-08 09:02:10 +02:00
|
|
|
{
|
2023-03-27 06:15:04 +02:00
|
|
|
Datum values[PG_GET_WAL_RECORDS_INFO_COLS] = {0};
|
|
|
|
bool nulls[PG_GET_WAL_RECORDS_INFO_COLS] = {0};
|
|
|
|
|
2023-02-20 19:29:53 +01:00
|
|
|
/* Use the tmp context so we can clean up after each tuple is done */
|
|
|
|
old_cxt = MemoryContextSwitchTo(tmp_cxt);
|
|
|
|
|
2022-08-18 23:23:30 +02:00
|
|
|
GetWALRecordInfo(xlogreader, values, nulls,
|
2022-04-30 17:28:33 +02:00
|
|
|
PG_GET_WAL_RECORDS_INFO_COLS);
|
2022-04-08 09:02:10 +02:00
|
|
|
|
2022-04-30 17:28:33 +02:00
|
|
|
tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc,
|
|
|
|
values, nulls);
|
2022-04-08 09:02:10 +02:00
|
|
|
|
2023-02-20 19:29:53 +01:00
|
|
|
/* clean up and switch back */
|
|
|
|
MemoryContextSwitchTo(old_cxt);
|
|
|
|
MemoryContextReset(tmp_cxt);
|
|
|
|
|
2022-04-08 09:02:10 +02:00
|
|
|
CHECK_FOR_INTERRUPTS();
|
|
|
|
}
|
|
|
|
|
2023-02-20 19:29:53 +01:00
|
|
|
MemoryContextDelete(tmp_cxt);
|
2022-04-30 17:28:33 +02:00
|
|
|
pfree(xlogreader->private_data);
|
2022-04-08 09:02:10 +02:00
|
|
|
XLogReaderFree(xlogreader);
|
|
|
|
|
|
|
|
#undef PG_GET_WAL_RECORDS_INFO_COLS
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2023-03-30 21:26:12 +02:00
|
|
|
* Get info of all WAL records between start LSN and end LSN.
|
2022-04-08 09:02:10 +02:00
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
pg_get_wal_records_info(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2023-03-14 12:13:02 +01:00
|
|
|
XLogRecPtr start_lsn = PG_GETARG_LSN(0);
|
|
|
|
XLogRecPtr end_lsn = PG_GETARG_LSN(1);
|
2022-04-08 09:02:10 +02:00
|
|
|
|
2023-03-14 12:13:02 +01:00
|
|
|
ValidateInputLSNs(start_lsn, &end_lsn);
|
2022-04-08 09:02:10 +02:00
|
|
|
GetWALRecordsInfo(fcinfo, start_lsn, end_lsn);
|
|
|
|
|
|
|
|
PG_RETURN_VOID();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Fill single row of record counts and sizes for an rmgr or record.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
FillXLogStatsRow(const char *name,
|
|
|
|
uint64 n, uint64 total_count,
|
|
|
|
uint64 rec_len, uint64 total_rec_len,
|
|
|
|
uint64 fpi_len, uint64 total_fpi_len,
|
|
|
|
uint64 tot_len, uint64 total_len,
|
|
|
|
Datum *values, bool *nulls, uint32 ncols)
|
|
|
|
{
|
2022-05-12 21:17:30 +02:00
|
|
|
double n_pct,
|
|
|
|
rec_len_pct,
|
|
|
|
fpi_len_pct,
|
|
|
|
tot_len_pct;
|
|
|
|
int i = 0;
|
2022-04-08 09:02:10 +02:00
|
|
|
|
|
|
|
n_pct = 0;
|
|
|
|
if (total_count != 0)
|
|
|
|
n_pct = 100 * (double) n / total_count;
|
|
|
|
|
|
|
|
rec_len_pct = 0;
|
|
|
|
if (total_rec_len != 0)
|
|
|
|
rec_len_pct = 100 * (double) rec_len / total_rec_len;
|
|
|
|
|
|
|
|
fpi_len_pct = 0;
|
|
|
|
if (total_fpi_len != 0)
|
|
|
|
fpi_len_pct = 100 * (double) fpi_len / total_fpi_len;
|
|
|
|
|
|
|
|
tot_len_pct = 0;
|
|
|
|
if (total_len != 0)
|
|
|
|
tot_len_pct = 100 * (double) tot_len / total_len;
|
|
|
|
|
|
|
|
values[i++] = CStringGetTextDatum(name);
|
|
|
|
values[i++] = Int64GetDatum(n);
|
2022-09-12 09:38:07 +02:00
|
|
|
values[i++] = Float8GetDatum(n_pct);
|
2022-04-08 09:02:10 +02:00
|
|
|
values[i++] = Int64GetDatum(rec_len);
|
2022-09-12 09:38:07 +02:00
|
|
|
values[i++] = Float8GetDatum(rec_len_pct);
|
2022-04-08 09:02:10 +02:00
|
|
|
values[i++] = Int64GetDatum(fpi_len);
|
2022-09-12 09:38:07 +02:00
|
|
|
values[i++] = Float8GetDatum(fpi_len_pct);
|
2022-04-08 09:02:10 +02:00
|
|
|
values[i++] = Int64GetDatum(tot_len);
|
2022-09-12 09:38:07 +02:00
|
|
|
values[i++] = Float8GetDatum(tot_len_pct);
|
2022-04-08 09:02:10 +02:00
|
|
|
|
|
|
|
Assert(i == ncols);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get summary statistics about the records seen so far.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
GetXLogSummaryStats(XLogStats *stats, ReturnSetInfo *rsinfo,
|
|
|
|
Datum *values, bool *nulls, uint32 ncols,
|
|
|
|
bool stats_per_record)
|
|
|
|
{
|
2023-03-14 12:13:02 +01:00
|
|
|
MemoryContext old_cxt;
|
|
|
|
MemoryContext tmp_cxt;
|
|
|
|
uint64 total_count = 0;
|
|
|
|
uint64 total_rec_len = 0;
|
|
|
|
uint64 total_fpi_len = 0;
|
|
|
|
uint64 total_len = 0;
|
|
|
|
int ri;
|
2022-04-08 09:02:10 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Each row shows its percentages of the total, so make a first pass to
|
|
|
|
* calculate column totals.
|
|
|
|
*/
|
|
|
|
for (ri = 0; ri <= RM_MAX_ID; ri++)
|
|
|
|
{
|
|
|
|
if (!RmgrIdIsValid(ri))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
total_count += stats->rmgr_stats[ri].count;
|
|
|
|
total_rec_len += stats->rmgr_stats[ri].rec_len;
|
|
|
|
total_fpi_len += stats->rmgr_stats[ri].fpi_len;
|
|
|
|
}
|
|
|
|
total_len = total_rec_len + total_fpi_len;
|
|
|
|
|
2023-02-20 19:29:53 +01:00
|
|
|
tmp_cxt = AllocSetContextCreate(CurrentMemoryContext,
|
|
|
|
"GetXLogSummaryStats temporary cxt",
|
|
|
|
ALLOCSET_DEFAULT_SIZES);
|
|
|
|
|
2022-04-08 09:02:10 +02:00
|
|
|
for (ri = 0; ri <= RM_MAX_ID; ri++)
|
|
|
|
{
|
|
|
|
uint64 count;
|
|
|
|
uint64 rec_len;
|
|
|
|
uint64 fpi_len;
|
|
|
|
uint64 tot_len;
|
2022-05-12 21:17:30 +02:00
|
|
|
RmgrData desc;
|
2022-04-08 09:02:10 +02:00
|
|
|
|
|
|
|
if (!RmgrIdIsValid(ri))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!RmgrIdExists(ri))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
desc = GetRmgr(ri);
|
|
|
|
|
|
|
|
if (stats_per_record)
|
|
|
|
{
|
2022-05-12 21:17:30 +02:00
|
|
|
int rj;
|
2022-04-08 09:02:10 +02:00
|
|
|
|
|
|
|
for (rj = 0; rj < MAX_XLINFO_TYPES; rj++)
|
|
|
|
{
|
|
|
|
const char *id;
|
|
|
|
|
|
|
|
count = stats->record_stats[ri][rj].count;
|
|
|
|
rec_len = stats->record_stats[ri][rj].rec_len;
|
|
|
|
fpi_len = stats->record_stats[ri][rj].fpi_len;
|
|
|
|
tot_len = rec_len + fpi_len;
|
|
|
|
|
|
|
|
/* Skip undefined combinations and ones that didn't occur */
|
|
|
|
if (count == 0)
|
|
|
|
continue;
|
|
|
|
|
2023-02-20 19:29:53 +01:00
|
|
|
old_cxt = MemoryContextSwitchTo(tmp_cxt);
|
|
|
|
|
2022-04-08 09:02:10 +02:00
|
|
|
/* the upper four bits in xl_info are the rmgr's */
|
|
|
|
id = desc.rm_identify(rj << 4);
|
|
|
|
if (id == NULL)
|
|
|
|
id = psprintf("UNKNOWN (%x)", rj << 4);
|
|
|
|
|
|
|
|
FillXLogStatsRow(psprintf("%s/%s", desc.rm_name, id), count,
|
|
|
|
total_count, rec_len, total_rec_len, fpi_len,
|
|
|
|
total_fpi_len, tot_len, total_len,
|
|
|
|
values, nulls, ncols);
|
|
|
|
|
|
|
|
tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc,
|
|
|
|
values, nulls);
|
2023-02-20 19:29:53 +01:00
|
|
|
|
|
|
|
/* clean up and switch back */
|
|
|
|
MemoryContextSwitchTo(old_cxt);
|
|
|
|
MemoryContextReset(tmp_cxt);
|
2022-04-08 09:02:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
count = stats->rmgr_stats[ri].count;
|
|
|
|
rec_len = stats->rmgr_stats[ri].rec_len;
|
|
|
|
fpi_len = stats->rmgr_stats[ri].fpi_len;
|
|
|
|
tot_len = rec_len + fpi_len;
|
|
|
|
|
2023-02-20 19:29:53 +01:00
|
|
|
old_cxt = MemoryContextSwitchTo(tmp_cxt);
|
|
|
|
|
2022-04-08 09:02:10 +02:00
|
|
|
FillXLogStatsRow(desc.rm_name, count, total_count, rec_len,
|
|
|
|
total_rec_len, fpi_len, total_fpi_len, tot_len,
|
|
|
|
total_len, values, nulls, ncols);
|
|
|
|
|
|
|
|
tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc,
|
|
|
|
values, nulls);
|
2023-02-20 19:29:53 +01:00
|
|
|
|
|
|
|
/* clean up and switch back */
|
|
|
|
MemoryContextSwitchTo(old_cxt);
|
|
|
|
MemoryContextReset(tmp_cxt);
|
2022-04-08 09:02:10 +02:00
|
|
|
}
|
|
|
|
}
|
2023-02-20 19:29:53 +01:00
|
|
|
|
|
|
|
MemoryContextDelete(tmp_cxt);
|
2022-04-08 09:02:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get WAL stats between start LSN and end LSN.
|
|
|
|
*/
|
|
|
|
static void
|
2023-03-14 12:13:02 +01:00
|
|
|
GetWalStats(FunctionCallInfo fcinfo, XLogRecPtr start_lsn, XLogRecPtr end_lsn,
|
|
|
|
bool stats_per_record)
|
2022-04-08 09:02:10 +02:00
|
|
|
{
|
|
|
|
#define PG_GET_WAL_STATS_COLS 9
|
|
|
|
XLogReaderState *xlogreader;
|
2022-07-16 08:42:15 +02:00
|
|
|
XLogStats stats = {0};
|
2022-04-08 09:02:10 +02:00
|
|
|
ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
|
2022-07-16 08:42:15 +02:00
|
|
|
Datum values[PG_GET_WAL_STATS_COLS] = {0};
|
|
|
|
bool nulls[PG_GET_WAL_STATS_COLS] = {0};
|
2022-04-08 09:02:10 +02:00
|
|
|
|
2023-03-14 12:13:02 +01:00
|
|
|
Assert(start_lsn <= end_lsn);
|
|
|
|
|
2022-10-18 03:22:35 +02:00
|
|
|
InitMaterializedSRF(fcinfo, 0);
|
2022-04-08 09:02:10 +02:00
|
|
|
|
2022-08-18 23:23:30 +02:00
|
|
|
xlogreader = InitXLogReaderState(start_lsn);
|
2022-04-08 09:02:10 +02:00
|
|
|
|
2022-08-18 23:23:30 +02:00
|
|
|
while (ReadNextXLogRecord(xlogreader) &&
|
2022-04-30 17:28:33 +02:00
|
|
|
xlogreader->EndRecPtr <= end_lsn)
|
2022-04-08 09:02:10 +02:00
|
|
|
{
|
2022-04-30 17:28:33 +02:00
|
|
|
XLogRecStoreStats(&stats, xlogreader);
|
2022-04-08 09:02:10 +02:00
|
|
|
|
|
|
|
CHECK_FOR_INTERRUPTS();
|
|
|
|
}
|
|
|
|
|
2022-04-30 17:28:33 +02:00
|
|
|
pfree(xlogreader->private_data);
|
2022-04-08 09:02:10 +02:00
|
|
|
XLogReaderFree(xlogreader);
|
|
|
|
|
|
|
|
GetXLogSummaryStats(&stats, rsinfo, values, nulls,
|
|
|
|
PG_GET_WAL_STATS_COLS,
|
|
|
|
stats_per_record);
|
|
|
|
|
|
|
|
#undef PG_GET_WAL_STATS_COLS
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get stats of all WAL records between start LSN and end LSN.
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
pg_get_wal_stats(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2023-03-14 12:13:02 +01:00
|
|
|
XLogRecPtr start_lsn = PG_GETARG_LSN(0);
|
|
|
|
XLogRecPtr end_lsn = PG_GETARG_LSN(1);
|
|
|
|
bool stats_per_record = PG_GETARG_BOOL(2);
|
2022-04-08 09:02:10 +02:00
|
|
|
|
2023-03-14 12:13:02 +01:00
|
|
|
ValidateInputLSNs(start_lsn, &end_lsn);
|
2022-04-08 09:02:10 +02:00
|
|
|
GetWalStats(fcinfo, start_lsn, end_lsn, stats_per_record);
|
|
|
|
|
|
|
|
PG_RETURN_VOID();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2023-03-14 12:13:02 +01:00
|
|
|
* The following functions have been removed in newer versions in 1.1, but
|
|
|
|
* they are kept around for compatibility.
|
2022-04-08 09:02:10 +02:00
|
|
|
*/
|
|
|
|
Datum
|
2023-03-14 12:13:02 +01:00
|
|
|
pg_get_wal_records_info_till_end_of_wal(PG_FUNCTION_ARGS)
|
2022-04-08 09:02:10 +02:00
|
|
|
{
|
2023-03-14 12:13:02 +01:00
|
|
|
XLogRecPtr start_lsn = PG_GETARG_LSN(0);
|
|
|
|
XLogRecPtr end_lsn = GetCurrentLSN();
|
|
|
|
|
|
|
|
if (start_lsn > end_lsn)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
|
|
errmsg("WAL start LSN must be less than current LSN"),
|
|
|
|
errdetail("Current WAL LSN on the database system is at %X/%X.",
|
|
|
|
LSN_FORMAT_ARGS(end_lsn))));
|
2022-04-08 09:02:10 +02:00
|
|
|
|
2023-03-14 12:13:02 +01:00
|
|
|
GetWALRecordsInfo(fcinfo, start_lsn, end_lsn);
|
2022-04-08 09:02:10 +02:00
|
|
|
|
2023-03-14 12:13:02 +01:00
|
|
|
PG_RETURN_VOID();
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
pg_get_wal_stats_till_end_of_wal(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
XLogRecPtr start_lsn = PG_GETARG_LSN(0);
|
|
|
|
XLogRecPtr end_lsn = GetCurrentLSN();
|
|
|
|
bool stats_per_record = PG_GETARG_BOOL(1);
|
|
|
|
|
|
|
|
if (start_lsn > end_lsn)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
|
|
errmsg("WAL start LSN must be less than current LSN"),
|
|
|
|
errdetail("Current WAL LSN on the database system is at %X/%X.",
|
|
|
|
LSN_FORMAT_ARGS(end_lsn))));
|
2022-04-08 09:02:10 +02:00
|
|
|
|
|
|
|
GetWalStats(fcinfo, start_lsn, end_lsn, stats_per_record);
|
|
|
|
|
|
|
|
PG_RETURN_VOID();
|
|
|
|
}
|