PostgreSQL Source Code git master
pg_walinspect.c File Reference
#include "postgres.h"
#include "access/htup_details.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"
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/pg_lsn.h"
Include dependency graph for pg_walinspect.c:

Go to the source code of this file.

Macros

#define PG_GET_WAL_BLOCK_INFO_COLS   20
 
#define PG_GET_WAL_RECORD_INFO_COLS   11
 
#define PG_GET_WAL_RECORDS_INFO_COLS   11
 
#define PG_GET_WAL_STATS_COLS   9
 

Functions

 PG_MODULE_MAGIC_EXT (.name="pg_walinspect",.version=PG_VERSION)
 
 PG_FUNCTION_INFO_V1 (pg_get_wal_block_info)
 
 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)
 
static void ValidateInputLSNs (XLogRecPtr start_lsn, XLogRecPtr *end_lsn)
 
static XLogRecPtr GetCurrentLSN (void)
 
static XLogReaderStateInitXLogReaderState (XLogRecPtr lsn)
 
static XLogRecordReadNextXLogRecord (XLogReaderState *xlogreader)
 
static void GetWALRecordInfo (XLogReaderState *record, Datum *values, bool *nulls, uint32 ncols)
 
static void GetWALRecordsInfo (FunctionCallInfo fcinfo, XLogRecPtr start_lsn, XLogRecPtr end_lsn)
 
static void GetXLogSummaryStats (XLogStats *stats, ReturnSetInfo *rsinfo, 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)
 
static void GetWalStats (FunctionCallInfo fcinfo, XLogRecPtr start_lsn, XLogRecPtr end_lsn, bool stats_per_record)
 
static void GetWALBlockInfo (FunctionCallInfo fcinfo, XLogReaderState *record, bool show_data)
 
Datum pg_get_wal_block_info (PG_FUNCTION_ARGS)
 
Datum pg_get_wal_record_info (PG_FUNCTION_ARGS)
 
Datum pg_get_wal_records_info (PG_FUNCTION_ARGS)
 
Datum pg_get_wal_stats (PG_FUNCTION_ARGS)
 
Datum pg_get_wal_records_info_till_end_of_wal (PG_FUNCTION_ARGS)
 
Datum pg_get_wal_stats_till_end_of_wal (PG_FUNCTION_ARGS)
 

Macro Definition Documentation

◆ PG_GET_WAL_BLOCK_INFO_COLS

#define PG_GET_WAL_BLOCK_INFO_COLS   20

◆ PG_GET_WAL_RECORD_INFO_COLS

#define PG_GET_WAL_RECORD_INFO_COLS   11

◆ PG_GET_WAL_RECORDS_INFO_COLS

#define PG_GET_WAL_RECORDS_INFO_COLS   11

◆ PG_GET_WAL_STATS_COLS

#define PG_GET_WAL_STATS_COLS   9

Function Documentation

◆ FillXLogStatsRow()

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 
)
static

Definition at line 608 of file pg_walinspect.c.

614{
615 double n_pct,
616 rec_len_pct,
617 fpi_len_pct,
618 tot_len_pct;
619 int i = 0;
620
621 n_pct = 0;
622 if (total_count != 0)
623 n_pct = 100 * (double) n / total_count;
624
625 rec_len_pct = 0;
626 if (total_rec_len != 0)
627 rec_len_pct = 100 * (double) rec_len / total_rec_len;
628
629 fpi_len_pct = 0;
630 if (total_fpi_len != 0)
631 fpi_len_pct = 100 * (double) fpi_len / total_fpi_len;
632
633 tot_len_pct = 0;
634 if (total_len != 0)
635 tot_len_pct = 100 * (double) tot_len / total_len;
636
638 values[i++] = Int64GetDatum(n);
639 values[i++] = Float8GetDatum(n_pct);
640 values[i++] = Int64GetDatum(rec_len);
641 values[i++] = Float8GetDatum(rec_len_pct);
642 values[i++] = Int64GetDatum(fpi_len);
643 values[i++] = Float8GetDatum(fpi_len_pct);
644 values[i++] = Int64GetDatum(tot_len);
645 values[i++] = Float8GetDatum(tot_len_pct);
646
647 Assert(i == ncols);
648}
static Datum values[MAXATTR]
Definition: bootstrap.c:153
#define CStringGetTextDatum(s)
Definition: builtins.h:97
Assert(PointerIsAligned(start, uint64))
int i
Definition: isn.c:77
static Datum Int64GetDatum(int64 X)
Definition: postgres.h:403
static Datum Float8GetDatum(float8 X)
Definition: postgres.h:492
const char * name

References Assert(), CStringGetTextDatum, Float8GetDatum(), i, Int64GetDatum(), name, and values.

Referenced by GetXLogSummaryStats().

◆ GetCurrentLSN()

static XLogRecPtr GetCurrentLSN ( void  )
static

Definition at line 73 of file pg_walinspect.c.

74{
75 XLogRecPtr curr_lsn;
76
77 /*
78 * We determine the current LSN of the server similar to how page_read
79 * callback read_local_xlog_page_no_wait does.
80 */
81 if (!RecoveryInProgress())
82 curr_lsn = GetFlushRecPtr(NULL);
83 else
84 curr_lsn = GetXLogReplayRecPtr(NULL);
85
86 Assert(XLogRecPtrIsValid(curr_lsn));
87
88 return curr_lsn;
89}
bool RecoveryInProgress(void)
Definition: xlog.c:6406
XLogRecPtr GetFlushRecPtr(TimeLineID *insertTLI)
Definition: xlog.c:6571
#define XLogRecPtrIsValid(r)
Definition: xlogdefs.h:29
uint64 XLogRecPtr
Definition: xlogdefs.h:21
XLogRecPtr GetXLogReplayRecPtr(TimeLineID *replayTLI)

References Assert(), GetFlushRecPtr(), GetXLogReplayRecPtr(), RecoveryInProgress(), and XLogRecPtrIsValid.

Referenced by pg_get_wal_record_info(), pg_get_wal_records_info_till_end_of_wal(), pg_get_wal_stats_till_end_of_wal(), and ValidateInputLSNs().

◆ GetWALBlockInfo()

static void GetWALBlockInfo ( FunctionCallInfo  fcinfo,
XLogReaderState record,
bool  show_data 
)
static

Definition at line 253 of file pg_walinspect.c.

255{
256#define PG_GET_WAL_BLOCK_INFO_COLS 20
257 int block_id;
258 ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
259 RmgrData desc;
260 const char *record_type;
261 StringInfoData rec_desc;
262
264
265 desc = GetRmgr(XLogRecGetRmid(record));
266 record_type = desc.rm_identify(XLogRecGetInfo(record));
267
268 if (record_type == NULL)
269 record_type = psprintf("UNKNOWN (%x)",
270 XLogRecGetInfo(record) & ~XLR_INFO_MASK);
271
272 initStringInfo(&rec_desc);
273 desc.rm_desc(&rec_desc, record);
274
275 for (block_id = 0; block_id <= XLogRecMaxBlockId(record); block_id++)
276 {
277 DecodedBkpBlock *blk;
278 BlockNumber blkno;
279 RelFileLocator rnode;
280 ForkNumber forknum;
282 bool nulls[PG_GET_WAL_BLOCK_INFO_COLS] = {0};
283 uint32 block_data_len = 0,
284 block_fpi_len = 0;
285 ArrayType *block_fpi_info = NULL;
286 int i = 0;
287
288 if (!XLogRecHasBlockRef(record, block_id))
289 continue;
290
291 blk = XLogRecGetBlock(record, block_id);
292
293 (void) XLogRecGetBlockTagExtended(record, block_id,
294 &rnode, &forknum, &blkno, NULL);
295
296 /* Save block_data_len */
297 if (blk->has_data)
298 block_data_len = blk->data_len;
299
300 if (blk->has_image)
301 {
302 /* Block reference has an FPI, so prepare relevant output */
303 int bitcnt;
304 int cnt = 0;
305 Datum *flags;
306
307 /* Save block_fpi_len */
308 block_fpi_len = blk->bimg_len;
309
310 /* Construct and save block_fpi_info */
311 bitcnt = pg_popcount((const char *) &blk->bimg_info,
312 sizeof(uint8));
313 flags = (Datum *) palloc0(sizeof(Datum) * bitcnt);
314 if ((blk->bimg_info & BKPIMAGE_HAS_HOLE) != 0)
315 flags[cnt++] = CStringGetTextDatum("HAS_HOLE");
316 if (blk->apply_image)
317 flags[cnt++] = CStringGetTextDatum("APPLY");
318 if ((blk->bimg_info & BKPIMAGE_COMPRESS_PGLZ) != 0)
319 flags[cnt++] = CStringGetTextDatum("COMPRESS_PGLZ");
320 if ((blk->bimg_info & BKPIMAGE_COMPRESS_LZ4) != 0)
321 flags[cnt++] = CStringGetTextDatum("COMPRESS_LZ4");
322 if ((blk->bimg_info & BKPIMAGE_COMPRESS_ZSTD) != 0)
323 flags[cnt++] = CStringGetTextDatum("COMPRESS_ZSTD");
324
325 Assert(cnt <= bitcnt);
326 block_fpi_info = construct_array_builtin(flags, cnt, TEXTOID);
327 }
328
329 /* start_lsn, end_lsn, prev_lsn, and blockid outputs */
330 values[i++] = LSNGetDatum(record->ReadRecPtr);
331 values[i++] = LSNGetDatum(record->EndRecPtr);
332 values[i++] = LSNGetDatum(XLogRecGetPrev(record));
333 values[i++] = Int16GetDatum(block_id);
334
335 /* relfile and block related outputs */
339 values[i++] = Int16GetDatum(forknum);
340 values[i++] = Int64GetDatum((int64) blkno);
341
342 /* xid, resource_manager, and record_type outputs */
344 values[i++] = CStringGetTextDatum(desc.rm_name);
345 values[i++] = CStringGetTextDatum(record_type);
346
347 /*
348 * record_length, main_data_length, block_data_len, and
349 * block_fpi_length outputs
350 */
353 values[i++] = UInt32GetDatum(block_data_len);
354 values[i++] = UInt32GetDatum(block_fpi_len);
355
356 /* block_fpi_info (text array) output */
357 if (block_fpi_info)
358 values[i++] = PointerGetDatum(block_fpi_info);
359 else
360 nulls[i++] = true;
361
362 /* description output (describes WAL record) */
363 if (rec_desc.len > 0)
364 values[i++] = CStringGetTextDatum(rec_desc.data);
365 else
366 nulls[i++] = true;
367
368 /* block_data output */
369 if (blk->has_data && show_data)
370 {
371 bytea *block_data;
372
373 block_data = (bytea *) palloc(block_data_len + VARHDRSZ);
374 SET_VARSIZE(block_data, block_data_len + VARHDRSZ);
375 memcpy(VARDATA(block_data), blk->data, block_data_len);
376 values[i++] = PointerGetDatum(block_data);
377 }
378 else
379 nulls[i++] = true;
380
381 /* block_fpi_data output */
382 if (blk->has_image && show_data)
383 {
385 Page page;
386 bytea *block_fpi_data;
387
388 page = (Page) buf.data;
389 if (!RestoreBlockImage(record, block_id, page))
391 (errcode(ERRCODE_INTERNAL_ERROR),
392 errmsg_internal("%s", record->errormsg_buf)));
393
394 block_fpi_data = (bytea *) palloc(BLCKSZ + VARHDRSZ);
395 SET_VARSIZE(block_fpi_data, BLCKSZ + VARHDRSZ);
396 memcpy(VARDATA(block_fpi_data), page, BLCKSZ);
397 values[i++] = PointerGetDatum(block_fpi_data);
398 }
399 else
400 nulls[i++] = true;
401
403
404 /* Store a tuple for this block reference */
405 tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc,
406 values, nulls);
407 }
408
409#undef PG_GET_WAL_BLOCK_INFO_COLS
410}
ArrayType * construct_array_builtin(Datum *elems, int nelems, Oid elmtype)
Definition: arrayfuncs.c:3382
uint32 BlockNumber
Definition: block.h:31
PageData * Page
Definition: bufpage.h:81
uint8_t uint8
Definition: c.h:539
#define VARHDRSZ
Definition: c.h:700
int64_t int64
Definition: c.h:538
uint32_t uint32
Definition: c.h:541
int errmsg_internal(const char *fmt,...)
Definition: elog.c:1170
int errcode(int sqlerrcode)
Definition: elog.c:863
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:150
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:81
void * palloc0(Size size)
Definition: mcxt.c:1395
void * palloc(Size size)
Definition: mcxt.c:1365
static uint64 pg_popcount(const char *buf, int bytes)
Definition: pg_bitutils.h:363
static Datum LSNGetDatum(XLogRecPtr X)
Definition: pg_lsn.h:31
static char * buf
Definition: pg_test_fsync.c:72
#define PG_GET_WAL_BLOCK_INFO_COLS
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:332
static Datum TransactionIdGetDatum(TransactionId X)
Definition: postgres.h:282
static Datum Int16GetDatum(int16 X)
Definition: postgres.h:182
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:262
uint64_t Datum
Definition: postgres.h:70
static Datum UInt32GetDatum(uint32 X)
Definition: postgres.h:242
char * psprintf(const char *fmt,...)
Definition: psprintf.c:43
ForkNumber
Definition: relpath.h:56
void initStringInfo(StringInfo str)
Definition: stringinfo.c:97
RelFileLocator rlocator
Definition: xlogreader.h:125
RelFileNumber relNumber
TupleDesc setDesc
Definition: execnodes.h:364
Tuplestorestate * setResult
Definition: execnodes.h:363
char * errormsg_buf
Definition: xlogreader.h:310
XLogRecPtr EndRecPtr
Definition: xlogreader.h:206
XLogRecPtr ReadRecPtr
Definition: xlogreader.h:205
Definition: c.h:695
void tuplestore_putvalues(Tuplestorestate *state, TupleDesc tdesc, const Datum *values, const bool *isnull)
Definition: tuplestore.c:784
static char * VARDATA(const void *PTR)
Definition: varatt.h:305
static void SET_VARSIZE(void *PTR, Size len)
Definition: varatt.h:432
static RmgrData GetRmgr(RmgrId rmid)
bool XLogRecGetBlockTagExtended(XLogReaderState *record, uint8 block_id, RelFileLocator *rlocator, ForkNumber *forknum, BlockNumber *blknum, Buffer *prefetch_buffer)
Definition: xlogreader.c:2017
bool RestoreBlockImage(XLogReaderState *record, uint8 block_id, char *page)
Definition: xlogreader.c:2076
#define XLogRecGetDataLen(decoder)
Definition: xlogreader.h:415
#define XLogRecGetInfo(decoder)
Definition: xlogreader.h:409
#define XLogRecGetRmid(decoder)
Definition: xlogreader.h:410
#define XLogRecGetTotalLen(decoder)
Definition: xlogreader.h:407
#define XLogRecGetXid(decoder)
Definition: xlogreader.h:411
#define XLogRecGetBlock(decoder, i)
Definition: xlogreader.h:418
#define XLogRecMaxBlockId(decoder)
Definition: xlogreader.h:417
#define XLogRecHasBlockRef(decoder, block_id)
Definition: xlogreader.h:419
#define XLogRecGetPrev(decoder)
Definition: xlogreader.h:408
#define XLogRecHasAnyBlockRefs(decoder)
Definition: xlogreader.h:416
#define BKPIMAGE_COMPRESS_ZSTD
Definition: xlogrecord.h:162
#define BKPIMAGE_HAS_HOLE
Definition: xlogrecord.h:157
#define BKPIMAGE_COMPRESS_LZ4
Definition: xlogrecord.h:161
#define XLR_INFO_MASK
Definition: xlogrecord.h:62
#define BKPIMAGE_COMPRESS_PGLZ
Definition: xlogrecord.h:160

References DecodedBkpBlock::apply_image, Assert(), DecodedBkpBlock::bimg_info, DecodedBkpBlock::bimg_len, BKPIMAGE_COMPRESS_LZ4, BKPIMAGE_COMPRESS_PGLZ, BKPIMAGE_COMPRESS_ZSTD, BKPIMAGE_HAS_HOLE, buf, construct_array_builtin(), CStringGetTextDatum, DecodedBkpBlock::data, DecodedBkpBlock::data_len, RelFileLocator::dbOid, XLogReaderState::EndRecPtr, ereport, errcode(), errmsg_internal(), ERROR, XLogReaderState::errormsg_buf, GetRmgr(), DecodedBkpBlock::has_data, DecodedBkpBlock::has_image, i, if(), initStringInfo(), Int16GetDatum(), Int64GetDatum(), LSNGetDatum(), ObjectIdGetDatum(), palloc(), palloc0(), PG_GET_WAL_BLOCK_INFO_COLS, pg_popcount(), PointerGetDatum(), psprintf(), XLogReaderState::ReadRecPtr, RelFileLocator::relNumber, RestoreBlockImage(), FunctionCallInfoBaseData::resultinfo, DecodedBkpBlock::rlocator, SET_VARSIZE(), ReturnSetInfo::setDesc, ReturnSetInfo::setResult, RelFileLocator::spcOid, TransactionIdGetDatum(), tuplestore_putvalues(), UInt32GetDatum(), values, VARDATA(), VARHDRSZ, XLogRecGetBlock, XLogRecGetBlockTagExtended(), XLogRecGetDataLen, XLogRecGetInfo, XLogRecGetPrev, XLogRecGetRmid, XLogRecGetTotalLen, XLogRecGetXid, XLogRecHasAnyBlockRefs, XLogRecHasBlockRef, XLogRecMaxBlockId, and XLR_INFO_MASK.

Referenced by pg_get_wal_block_info().

◆ GetWALRecordInfo()

static void GetWALRecordInfo ( XLogReaderState record,
Datum values,
bool *  nulls,
uint32  ncols 
)
static

Definition at line 193 of file pg_walinspect.c.

195{
196 const char *record_type;
197 RmgrData desc;
198 uint32 fpi_len = 0;
199 StringInfoData rec_desc;
200 StringInfoData rec_blk_ref;
201 int i = 0;
202
203 desc = GetRmgr(XLogRecGetRmid(record));
204 record_type = desc.rm_identify(XLogRecGetInfo(record));
205
206 if (record_type == NULL)
207 record_type = psprintf("UNKNOWN (%x)", XLogRecGetInfo(record) & ~XLR_INFO_MASK);
208
209 initStringInfo(&rec_desc);
210 desc.rm_desc(&rec_desc, record);
211
212 if (XLogRecHasAnyBlockRefs(record))
213 {
214 initStringInfo(&rec_blk_ref);
215 XLogRecGetBlockRefInfo(record, false, true, &rec_blk_ref, &fpi_len);
216 }
217
218 values[i++] = LSNGetDatum(record->ReadRecPtr);
219 values[i++] = LSNGetDatum(record->EndRecPtr);
220 values[i++] = LSNGetDatum(XLogRecGetPrev(record));
223 values[i++] = CStringGetTextDatum(record_type);
226 values[i++] = UInt32GetDatum(fpi_len);
227
228 if (rec_desc.len > 0)
229 values[i++] = CStringGetTextDatum(rec_desc.data);
230 else
231 nulls[i++] = true;
232
233 if (XLogRecHasAnyBlockRefs(record))
234 values[i++] = CStringGetTextDatum(rec_blk_ref.data);
235 else
236 nulls[i++] = true;
237
238 Assert(i == ncols);
239}
const char *(* rm_identify)(uint8 info)
const char * rm_name
void(* rm_desc)(StringInfo buf, XLogReaderState *record)
void XLogRecGetBlockRefInfo(XLogReaderState *record, bool pretty, bool detailed_format, StringInfo buf, uint32 *fpi_len)
Definition: xlogdesc.c:231

References Assert(), CStringGetTextDatum, StringInfoData::data, XLogReaderState::EndRecPtr, GetRmgr(), i, initStringInfo(), StringInfoData::len, LSNGetDatum(), psprintf(), XLogReaderState::ReadRecPtr, RmgrData::rm_desc, RmgrData::rm_identify, RmgrData::rm_name, TransactionIdGetDatum(), UInt32GetDatum(), values, XLogRecGetBlockRefInfo(), XLogRecGetDataLen, XLogRecGetInfo, XLogRecGetPrev, XLogRecGetRmid, XLogRecGetTotalLen, XLogRecGetXid, XLogRecHasAnyBlockRefs, and XLR_INFO_MASK.

Referenced by GetWALRecordsInfo(), and pg_get_wal_record_info().

◆ GetWALRecordsInfo()

static void GetWALRecordsInfo ( FunctionCallInfo  fcinfo,
XLogRecPtr  start_lsn,
XLogRecPtr  end_lsn 
)
static

Definition at line 541 of file pg_walinspect.c.

543{
544#define PG_GET_WAL_RECORDS_INFO_COLS 11
546 ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
547 MemoryContext old_cxt;
548 MemoryContext tmp_cxt;
549
550 Assert(start_lsn <= end_lsn);
551
552 InitMaterializedSRF(fcinfo, 0);
553
554 xlogreader = InitXLogReaderState(start_lsn);
555
557 "GetWALRecordsInfo temporary cxt",
559
561 xlogreader->EndRecPtr <= end_lsn)
562 {
564 bool nulls[PG_GET_WAL_RECORDS_INFO_COLS] = {0};
565
566 /* Use the tmp context so we can clean up after each tuple is done */
567 old_cxt = MemoryContextSwitchTo(tmp_cxt);
568
571
572 tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc,
573 values, nulls);
574
575 /* clean up and switch back */
576 MemoryContextSwitchTo(old_cxt);
577 MemoryContextReset(tmp_cxt);
578
580 }
581
582 MemoryContextDelete(tmp_cxt);
585
586#undef PG_GET_WAL_RECORDS_INFO_COLS
587}
void InitMaterializedSRF(FunctionCallInfo fcinfo, bits32 flags)
Definition: funcapi.c:76
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:400
void pfree(void *pointer)
Definition: mcxt.c:1594
MemoryContext CurrentMemoryContext
Definition: mcxt.c:160
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:469
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:160
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:123
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:124
static void GetWALRecordInfo(XLogReaderState *record, Datum *values, bool *nulls, uint32 ncols)
#define PG_GET_WAL_RECORDS_INFO_COLS
static XLogRecord * ReadNextXLogRecord(XLogReaderState *xlogreader)
static XLogReaderState * InitXLogReaderState(XLogRecPtr lsn)
Definition: pg_walinspect.c:95
void * private_data
Definition: xlogreader.h:195
void XLogReaderFree(XLogReaderState *state)
Definition: xlogreader.c:162
static XLogReaderState * xlogreader
Definition: xlogrecovery.c:191

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, Assert(), CHECK_FOR_INTERRUPTS, CurrentMemoryContext, XLogReaderState::EndRecPtr, GetWALRecordInfo(), InitMaterializedSRF(), InitXLogReaderState(), MemoryContextDelete(), MemoryContextReset(), MemoryContextSwitchTo(), pfree(), PG_GET_WAL_RECORDS_INFO_COLS, XLogReaderState::private_data, ReadNextXLogRecord(), FunctionCallInfoBaseData::resultinfo, ReturnSetInfo::setDesc, ReturnSetInfo::setResult, tuplestore_putvalues(), values, xlogreader, and XLogReaderFree().

Referenced by pg_get_wal_records_info(), and pg_get_wal_records_info_till_end_of_wal().

◆ GetWalStats()

static void GetWalStats ( FunctionCallInfo  fcinfo,
XLogRecPtr  start_lsn,
XLogRecPtr  end_lsn,
bool  stats_per_record 
)
static

Definition at line 767 of file pg_walinspect.c.

769{
770#define PG_GET_WAL_STATS_COLS 9
772 XLogStats stats = {0};
773 ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
775 bool nulls[PG_GET_WAL_STATS_COLS] = {0};
776
777 Assert(start_lsn <= end_lsn);
778
779 InitMaterializedSRF(fcinfo, 0);
780
781 xlogreader = InitXLogReaderState(start_lsn);
782
784 xlogreader->EndRecPtr <= end_lsn)
785 {
787
789 }
790
793
794 GetXLogSummaryStats(&stats, rsinfo, values, nulls,
796 stats_per_record);
797
798#undef PG_GET_WAL_STATS_COLS
799}
#define PG_GET_WAL_STATS_COLS
static void GetXLogSummaryStats(XLogStats *stats, ReturnSetInfo *rsinfo, Datum *values, bool *nulls, uint32 ncols, bool stats_per_record)
void XLogRecStoreStats(XLogStats *stats, XLogReaderState *record)
Definition: xlogstats.c:54

References PG_GET_WAL_STATS_COLS, FunctionCallInfoBaseData::resultinfo, values, and xlogreader.

Referenced by pg_get_wal_stats(), and pg_get_wal_stats_till_end_of_wal().

◆ GetXLogSummaryStats()

static void GetXLogSummaryStats ( XLogStats stats,
ReturnSetInfo rsinfo,
Datum values,
bool *  nulls,
uint32  ncols,
bool  stats_per_record 
)
static

Definition at line 654 of file pg_walinspect.c.

657{
658 MemoryContext old_cxt;
659 MemoryContext tmp_cxt;
660 uint64 total_count = 0;
661 uint64 total_rec_len = 0;
662 uint64 total_fpi_len = 0;
663 uint64 total_len = 0;
664 int ri;
665
666 /*
667 * Each row shows its percentages of the total, so make a first pass to
668 * calculate column totals.
669 */
670 for (ri = 0; ri <= RM_MAX_ID; ri++)
671 {
672 if (!RmgrIdIsValid(ri))
673 continue;
674
675 total_count += stats->rmgr_stats[ri].count;
676 total_rec_len += stats->rmgr_stats[ri].rec_len;
677 total_fpi_len += stats->rmgr_stats[ri].fpi_len;
678 }
679 total_len = total_rec_len + total_fpi_len;
680
682 "GetXLogSummaryStats temporary cxt",
684
685 for (ri = 0; ri <= RM_MAX_ID; ri++)
686 {
687 uint64 count;
688 uint64 rec_len;
689 uint64 fpi_len;
690 uint64 tot_len;
691 RmgrData desc;
692
693 if (!RmgrIdIsValid(ri))
694 continue;
695
696 if (!RmgrIdExists(ri))
697 continue;
698
699 desc = GetRmgr(ri);
700
701 if (stats_per_record)
702 {
703 int rj;
704
705 for (rj = 0; rj < MAX_XLINFO_TYPES; rj++)
706 {
707 const char *id;
708
709 count = stats->record_stats[ri][rj].count;
710 rec_len = stats->record_stats[ri][rj].rec_len;
711 fpi_len = stats->record_stats[ri][rj].fpi_len;
712 tot_len = rec_len + fpi_len;
713
714 /* Skip undefined combinations and ones that didn't occur */
715 if (count == 0)
716 continue;
717
718 old_cxt = MemoryContextSwitchTo(tmp_cxt);
719
720 /* the upper four bits in xl_info are the rmgr's */
721 id = desc.rm_identify(rj << 4);
722 if (id == NULL)
723 id = psprintf("UNKNOWN (%x)", rj << 4);
724
725 FillXLogStatsRow(psprintf("%s/%s", desc.rm_name, id), count,
726 total_count, rec_len, total_rec_len, fpi_len,
727 total_fpi_len, tot_len, total_len,
728 values, nulls, ncols);
729
730 tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc,
731 values, nulls);
732
733 /* clean up and switch back */
734 MemoryContextSwitchTo(old_cxt);
735 MemoryContextReset(tmp_cxt);
736 }
737 }
738 else
739 {
740 count = stats->rmgr_stats[ri].count;
741 rec_len = stats->rmgr_stats[ri].rec_len;
742 fpi_len = stats->rmgr_stats[ri].fpi_len;
743 tot_len = rec_len + fpi_len;
744
745 old_cxt = MemoryContextSwitchTo(tmp_cxt);
746
747 FillXLogStatsRow(desc.rm_name, count, total_count, rec_len,
748 total_rec_len, fpi_len, total_fpi_len, tot_len,
749 total_len, values, nulls, ncols);
750
751 tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc,
752 values, nulls);
753
754 /* clean up and switch back */
755 MemoryContextSwitchTo(old_cxt);
756 MemoryContextReset(tmp_cxt);
757 }
758 }
759
760 MemoryContextDelete(tmp_cxt);
761}
uint64_t uint64
Definition: c.h:542
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)
#define RmgrIdIsValid(rmid)
Definition: rmgr.h:53
#define RM_MAX_ID
Definition: rmgr.h:33
uint64 count
Definition: xlogstats.h:23
uint64 fpi_len
Definition: xlogstats.h:25
uint64 rec_len
Definition: xlogstats.h:24
XLogRecStats record_stats[RM_MAX_ID+1][MAX_XLINFO_TYPES]
Definition: xlogstats.h:36
XLogRecStats rmgr_stats[RM_MAX_ID+1]
Definition: xlogstats.h:35
static bool RmgrIdExists(RmgrId rmid)
#define MAX_XLINFO_TYPES
Definition: xlogstats.h:19

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, XLogRecStats::count, CurrentMemoryContext, FillXLogStatsRow(), XLogRecStats::fpi_len, GetRmgr(), MAX_XLINFO_TYPES, MemoryContextDelete(), MemoryContextReset(), MemoryContextSwitchTo(), psprintf(), XLogRecStats::rec_len, XLogStats::record_stats, RmgrData::rm_identify, RM_MAX_ID, RmgrData::rm_name, XLogStats::rmgr_stats, RmgrIdExists(), RmgrIdIsValid, ReturnSetInfo::setDesc, ReturnSetInfo::setResult, tuplestore_putvalues(), and values.

◆ InitXLogReaderState()

static XLogReaderState * InitXLogReaderState ( XLogRecPtr  lsn)
static

Definition at line 95 of file pg_walinspect.c.

96{
99 XLogRecPtr first_valid_record;
100
101 /*
102 * Reading WAL below the first page of the first segments isn't allowed.
103 * This is a bootstrap WAL page and the page_read callback fails to read
104 * it.
105 */
106 if (lsn < XLOG_BLCKSZ)
108 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
109 errmsg("could not read WAL at LSN %X/%08X",
110 LSN_FORMAT_ARGS(lsn))));
111
112 private_data = (ReadLocalXLogPageNoWaitPrivate *)
114
117 .segment_open = &wal_segment_open,
118 .segment_close = &wal_segment_close),
119 private_data);
120
121 if (xlogreader == NULL)
123 (errcode(ERRCODE_OUT_OF_MEMORY),
124 errmsg("out of memory"),
125 errdetail("Failed while allocating a WAL reading processor.")));
126
127 /* first find a valid recptr to start from */
128 first_valid_record = XLogFindNextRecord(xlogreader, lsn);
129
130 if (!XLogRecPtrIsValid(first_valid_record))
132 errmsg("could not find a valid record after %X/%08X",
133 LSN_FORMAT_ARGS(lsn)));
134
135 return xlogreader;
136}
int errdetail(const char *fmt,...)
Definition: elog.c:1216
int errmsg(const char *fmt,...)
Definition: elog.c:1080
int wal_segment_size
Definition: xlog.c:145
#define LSN_FORMAT_ARGS(lsn)
Definition: xlogdefs.h:47
XLogReaderState * XLogReaderAllocate(int wal_segment_size, const char *waldir, XLogReaderRoutine *routine, void *private_data)
Definition: xlogreader.c:107
XLogRecPtr XLogFindNextRecord(XLogReaderState *state, XLogRecPtr RecPtr)
Definition: xlogreader.c:1394
#define XL_ROUTINE(...)
Definition: xlogreader.h:117
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
int read_local_xlog_page_no_wait(XLogReaderState *state, XLogRecPtr targetPagePtr, int reqLen, XLogRecPtr targetRecPtr, char *cur_page)
Definition: xlogutils.c:857

References ereport, errcode(), errdetail(), errmsg(), ERROR, LSN_FORMAT_ARGS, palloc0(), read_local_xlog_page_no_wait(), wal_segment_close(), wal_segment_open(), wal_segment_size, XL_ROUTINE, XLogFindNextRecord(), xlogreader, XLogReaderAllocate(), and XLogRecPtrIsValid.

Referenced by GetWALRecordsInfo(), pg_get_wal_block_info(), and pg_get_wal_record_info().

◆ PG_FUNCTION_INFO_V1() [1/6]

PG_FUNCTION_INFO_V1 ( pg_get_wal_block_info  )

◆ PG_FUNCTION_INFO_V1() [2/6]

PG_FUNCTION_INFO_V1 ( pg_get_wal_record_info  )

◆ PG_FUNCTION_INFO_V1() [3/6]

PG_FUNCTION_INFO_V1 ( pg_get_wal_records_info  )

◆ PG_FUNCTION_INFO_V1() [4/6]

PG_FUNCTION_INFO_V1 ( pg_get_wal_records_info_till_end_of_wal  )

◆ PG_FUNCTION_INFO_V1() [5/6]

PG_FUNCTION_INFO_V1 ( pg_get_wal_stats  )

◆ PG_FUNCTION_INFO_V1() [6/6]

PG_FUNCTION_INFO_V1 ( pg_get_wal_stats_till_end_of_wal  )

◆ pg_get_wal_block_info()

Datum pg_get_wal_block_info ( PG_FUNCTION_ARGS  )

Definition at line 416 of file pg_walinspect.c.

417{
418 XLogRecPtr start_lsn = PG_GETARG_LSN(0);
419 XLogRecPtr end_lsn = PG_GETARG_LSN(1);
420 bool show_data = PG_GETARG_BOOL(2);
422 MemoryContext old_cxt;
423 MemoryContext tmp_cxt;
424
425 ValidateInputLSNs(start_lsn, &end_lsn);
426
427 InitMaterializedSRF(fcinfo, 0);
428
429 xlogreader = InitXLogReaderState(start_lsn);
430
432 "pg_get_wal_block_info temporary cxt",
434
436 xlogreader->EndRecPtr <= end_lsn)
437 {
439
441 continue;
442
443 /* Use the tmp context so we can clean up after each tuple is done */
444 old_cxt = MemoryContextSwitchTo(tmp_cxt);
445
446 GetWALBlockInfo(fcinfo, xlogreader, show_data);
447
448 /* clean up and switch back */
449 MemoryContextSwitchTo(old_cxt);
450 MemoryContextReset(tmp_cxt);
451 }
452
453 MemoryContextDelete(tmp_cxt);
456
458}
#define PG_RETURN_VOID()
Definition: fmgr.h:349
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:274
#define PG_GETARG_LSN(n)
Definition: pg_lsn.h:36
static void GetWALBlockInfo(FunctionCallInfo fcinfo, XLogReaderState *record, bool show_data)
static void ValidateInputLSNs(XLogRecPtr start_lsn, XLogRecPtr *end_lsn)

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, CHECK_FOR_INTERRUPTS, CurrentMemoryContext, XLogReaderState::EndRecPtr, GetWALBlockInfo(), InitMaterializedSRF(), InitXLogReaderState(), MemoryContextDelete(), MemoryContextReset(), MemoryContextSwitchTo(), pfree(), PG_GETARG_BOOL, PG_GETARG_LSN, PG_RETURN_VOID, XLogReaderState::private_data, ReadNextXLogRecord(), ValidateInputLSNs(), xlogreader, XLogReaderFree(), and XLogRecHasAnyBlockRefs.

◆ pg_get_wal_record_info()

Datum pg_get_wal_record_info ( PG_FUNCTION_ARGS  )

Definition at line 464 of file pg_walinspect.c.

465{
466#define PG_GET_WAL_RECORD_INFO_COLS 11
467 Datum result;
469 bool nulls[PG_GET_WAL_RECORD_INFO_COLS] = {0};
470 XLogRecPtr lsn;
471 XLogRecPtr curr_lsn;
473 TupleDesc tupdesc;
474 HeapTuple tuple;
475
476 lsn = PG_GETARG_LSN(0);
477 curr_lsn = GetCurrentLSN();
478
479 if (lsn > curr_lsn)
481 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
482 errmsg("WAL input LSN must be less than current LSN"),
483 errdetail("Current WAL LSN on the database system is at %X/%08X.",
484 LSN_FORMAT_ARGS(curr_lsn))));
485
486 /* Build a tuple descriptor for our result type. */
487 if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
488 elog(ERROR, "return type must be a row type");
489
491
494 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
495 errmsg("could not read WAL at %X/%08X",
497
499
502
503 tuple = heap_form_tuple(tupdesc, values, nulls);
504 result = HeapTupleGetDatum(tuple);
505
506 PG_RETURN_DATUM(result);
507#undef PG_GET_WAL_RECORD_INFO_COLS
508}
#define elog(elevel,...)
Definition: elog.h:226
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:353
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:276
@ TYPEFUNC_COMPOSITE
Definition: funcapi.h:149
static Datum HeapTupleGetDatum(const HeapTupleData *tuple)
Definition: funcapi.h:230
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, const Datum *values, const bool *isnull)
Definition: heaptuple.c:1117
#define PG_GET_WAL_RECORD_INFO_COLS
static XLogRecPtr GetCurrentLSN(void)
Definition: pg_walinspect.c:73

References elog, XLogReaderState::EndRecPtr, ereport, errcode(), errdetail(), errmsg(), ERROR, get_call_result_type(), GetCurrentLSN(), GetWALRecordInfo(), heap_form_tuple(), HeapTupleGetDatum(), InitXLogReaderState(), LSN_FORMAT_ARGS, pfree(), PG_GET_WAL_RECORD_INFO_COLS, PG_GETARG_LSN, PG_RETURN_DATUM, XLogReaderState::private_data, ReadNextXLogRecord(), TYPEFUNC_COMPOSITE, values, xlogreader, and XLogReaderFree().

◆ pg_get_wal_records_info()

Datum pg_get_wal_records_info ( PG_FUNCTION_ARGS  )

Definition at line 593 of file pg_walinspect.c.

594{
595 XLogRecPtr start_lsn = PG_GETARG_LSN(0);
596 XLogRecPtr end_lsn = PG_GETARG_LSN(1);
597
598 ValidateInputLSNs(start_lsn, &end_lsn);
599 GetWALRecordsInfo(fcinfo, start_lsn, end_lsn);
600
602}
static void GetWALRecordsInfo(FunctionCallInfo fcinfo, XLogRecPtr start_lsn, XLogRecPtr end_lsn)

References GetWALRecordsInfo(), PG_GETARG_LSN, PG_RETURN_VOID, and ValidateInputLSNs().

◆ pg_get_wal_records_info_till_end_of_wal()

Datum pg_get_wal_records_info_till_end_of_wal ( PG_FUNCTION_ARGS  )

Definition at line 822 of file pg_walinspect.c.

823{
824 XLogRecPtr start_lsn = PG_GETARG_LSN(0);
825 XLogRecPtr end_lsn = GetCurrentLSN();
826
827 if (start_lsn > end_lsn)
829 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
830 errmsg("WAL start LSN must be less than current LSN"),
831 errdetail("Current WAL LSN on the database system is at %X/%08X.",
832 LSN_FORMAT_ARGS(end_lsn))));
833
834 GetWALRecordsInfo(fcinfo, start_lsn, end_lsn);
835
837}

References ereport, errcode(), errdetail(), errmsg(), ERROR, GetCurrentLSN(), GetWALRecordsInfo(), LSN_FORMAT_ARGS, PG_GETARG_LSN, and PG_RETURN_VOID.

◆ pg_get_wal_stats()

Datum pg_get_wal_stats ( PG_FUNCTION_ARGS  )

Definition at line 805 of file pg_walinspect.c.

806{
807 XLogRecPtr start_lsn = PG_GETARG_LSN(0);
808 XLogRecPtr end_lsn = PG_GETARG_LSN(1);
809 bool stats_per_record = PG_GETARG_BOOL(2);
810
811 ValidateInputLSNs(start_lsn, &end_lsn);
812 GetWalStats(fcinfo, start_lsn, end_lsn, stats_per_record);
813
815}
static void GetWalStats(FunctionCallInfo fcinfo, XLogRecPtr start_lsn, XLogRecPtr end_lsn, bool stats_per_record)

References GetWalStats(), PG_GETARG_BOOL, PG_GETARG_LSN, PG_RETURN_VOID, and ValidateInputLSNs().

◆ pg_get_wal_stats_till_end_of_wal()

Datum pg_get_wal_stats_till_end_of_wal ( PG_FUNCTION_ARGS  )

Definition at line 840 of file pg_walinspect.c.

841{
842 XLogRecPtr start_lsn = PG_GETARG_LSN(0);
843 XLogRecPtr end_lsn = GetCurrentLSN();
844 bool stats_per_record = PG_GETARG_BOOL(1);
845
846 if (start_lsn > end_lsn)
848 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
849 errmsg("WAL start LSN must be less than current LSN"),
850 errdetail("Current WAL LSN on the database system is at %X/%08X.",
851 LSN_FORMAT_ARGS(end_lsn))));
852
853 GetWalStats(fcinfo, start_lsn, end_lsn, stats_per_record);
854
856}

References ereport, errcode(), errdetail(), errmsg(), ERROR, GetCurrentLSN(), GetWalStats(), LSN_FORMAT_ARGS, PG_GETARG_BOOL, PG_GETARG_LSN, and PG_RETURN_VOID.

◆ PG_MODULE_MAGIC_EXT()

PG_MODULE_MAGIC_EXT ( name = "pg_walinspect",
version = PG_VERSION 
)

◆ ReadNextXLogRecord()

static XLogRecord * ReadNextXLogRecord ( XLogReaderState xlogreader)
static

Definition at line 151 of file pg_walinspect.c.

152{
153 XLogRecord *record;
154 char *errormsg;
155
156 record = XLogReadRecord(xlogreader, &errormsg);
157
158 if (record == NULL)
159 {
160 ReadLocalXLogPageNoWaitPrivate *private_data;
161
162 /* return NULL, if end of WAL is reached */
163 private_data = (ReadLocalXLogPageNoWaitPrivate *)
165
166 if (private_data->end_of_wal)
167 return NULL;
168
169 if (errormsg)
172 errmsg("could not read WAL at %X/%08X: %s",
173 LSN_FORMAT_ARGS(xlogreader->EndRecPtr), errormsg)));
174 else
177 errmsg("could not read WAL at %X/%08X",
179 }
180
181 return record;
182}
int errcode_for_file_access(void)
Definition: elog.c:886
XLogRecord * XLogReadRecord(XLogReaderState *state, char **errormsg)
Definition: xlogreader.c:390

References ReadLocalXLogPageNoWaitPrivate::end_of_wal, XLogReaderState::EndRecPtr, ereport, errcode_for_file_access(), errmsg(), ERROR, if(), LSN_FORMAT_ARGS, XLogReaderState::private_data, xlogreader, and XLogReadRecord().

Referenced by GetWALRecordsInfo(), pg_get_wal_block_info(), and pg_get_wal_record_info().

◆ ValidateInputLSNs()

static void ValidateInputLSNs ( XLogRecPtr  start_lsn,
XLogRecPtr end_lsn 
)
static

Definition at line 517 of file pg_walinspect.c.

518{
519 XLogRecPtr curr_lsn = GetCurrentLSN();
520
521 if (start_lsn > curr_lsn)
523 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
524 errmsg("WAL start LSN must be less than current LSN"),
525 errdetail("Current WAL LSN on the database system is at %X/%08X.",
526 LSN_FORMAT_ARGS(curr_lsn))));
527
528 if (start_lsn > *end_lsn)
530 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
531 errmsg("WAL start LSN must be less than end LSN")));
532
533 if (*end_lsn > curr_lsn)
534 *end_lsn = curr_lsn;
535}

References ereport, errcode(), errdetail(), errmsg(), ERROR, GetCurrentLSN(), and LSN_FORMAT_ARGS.

Referenced by pg_get_wal_block_info(), pg_get_wal_records_info(), and pg_get_wal_stats().