PostgreSQL Source Code  git master
walsummaryfuncs.c File Reference
#include "postgres.h"
#include "backup/walsummary.h"
#include "common/blkreftable.h"
#include "funcapi.h"
#include "miscadmin.h"
#include "postmaster/walsummarizer.h"
#include "utils/fmgrprotos.h"
#include "utils/pg_lsn.h"
Include dependency graph for walsummaryfuncs.c:

Go to the source code of this file.

Macros

#define NUM_WS_ATTS   3
 
#define NUM_SUMMARY_ATTS   6
 
#define NUM_STATE_ATTS   4
 
#define MAX_BLOCKS_PER_CALL   256
 

Functions

Datum pg_available_wal_summaries (PG_FUNCTION_ARGS)
 
Datum pg_wal_summary_contents (PG_FUNCTION_ARGS)
 
Datum pg_get_wal_summarizer_state (PG_FUNCTION_ARGS)
 

Macro Definition Documentation

◆ MAX_BLOCKS_PER_CALL

#define MAX_BLOCKS_PER_CALL   256

Definition at line 26 of file walsummaryfuncs.c.

◆ NUM_STATE_ATTS

#define NUM_STATE_ATTS   4

Definition at line 25 of file walsummaryfuncs.c.

◆ NUM_SUMMARY_ATTS

#define NUM_SUMMARY_ATTS   6

Definition at line 24 of file walsummaryfuncs.c.

◆ NUM_WS_ATTS

#define NUM_WS_ATTS   3

Definition at line 23 of file walsummaryfuncs.c.

Function Documentation

◆ pg_available_wal_summaries()

Datum pg_available_wal_summaries ( PG_FUNCTION_ARGS  )

Definition at line 32 of file walsummaryfuncs.c.

33 {
34  ReturnSetInfo *rsi;
35  List *wslist;
36  ListCell *lc;
38  bool nulls[NUM_WS_ATTS];
39 
40  InitMaterializedSRF(fcinfo, 0);
41  rsi = (ReturnSetInfo *) fcinfo->resultinfo;
42 
43  memset(nulls, 0, sizeof(nulls));
44 
46  foreach(lc, wslist)
47  {
48  WalSummaryFile *ws = (WalSummaryFile *) lfirst(lc);
49  HeapTuple tuple;
50 
52 
53  values[0] = Int64GetDatum((int64) ws->tli);
54  values[1] = LSNGetDatum(ws->start_lsn);
55  values[2] = LSNGetDatum(ws->end_lsn);
56 
57  tuple = heap_form_tuple(rsi->setDesc, values, nulls);
58  tuplestore_puttuple(rsi->setResult, tuple);
59  }
60 
61  return (Datum) 0;
62 }
static Datum values[MAXATTR]
Definition: bootstrap.c:152
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:1807
void InitMaterializedSRF(FunctionCallInfo fcinfo, bits32 flags)
Definition: funcapi.c:76
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, const Datum *values, const bool *isnull)
Definition: heaptuple.c:1116
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:122
#define lfirst(lc)
Definition: pg_list.h:172
static Datum LSNGetDatum(XLogRecPtr X)
Definition: pg_lsn.h:28
uintptr_t Datum
Definition: postgres.h:64
Definition: pg_list.h:54
TupleDesc setDesc
Definition: execnodes.h:340
Tuplestorestate * setResult
Definition: execnodes.h:339
XLogRecPtr end_lsn
Definition: walsummary.h:30
TimeLineID tli
Definition: walsummary.h:31
XLogRecPtr start_lsn
Definition: walsummary.h:29
void tuplestore_puttuple(Tuplestorestate *state, HeapTuple tuple)
Definition: tuplestore.c:730
List * GetWalSummaries(TimeLineID tli, XLogRecPtr start_lsn, XLogRecPtr end_lsn)
Definition: walsummary.c:43
#define NUM_WS_ATTS
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28

References CHECK_FOR_INTERRUPTS, WalSummaryFile::end_lsn, GetWalSummaries(), heap_form_tuple(), InitMaterializedSRF(), Int64GetDatum(), InvalidXLogRecPtr, lfirst, LSNGetDatum(), NUM_WS_ATTS, ReturnSetInfo::setDesc, ReturnSetInfo::setResult, WalSummaryFile::start_lsn, WalSummaryFile::tli, tuplestore_puttuple(), and values.

◆ pg_get_wal_summarizer_state()

Datum pg_get_wal_summarizer_state ( PG_FUNCTION_ARGS  )

Definition at line 177 of file walsummaryfuncs.c.

178 {
180  bool nulls[NUM_STATE_ATTS];
181  TimeLineID summarized_tli;
182  XLogRecPtr summarized_lsn;
183  XLogRecPtr pending_lsn;
184  int summarizer_pid;
185  TupleDesc tupdesc;
186  HeapTuple htup;
187 
188  GetWalSummarizerState(&summarized_tli, &summarized_lsn, &pending_lsn,
189  &summarizer_pid);
190 
191  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
192  elog(ERROR, "return type must be a row type");
193 
194  memset(nulls, 0, sizeof(nulls));
195 
196  values[0] = Int64GetDatum((int64) summarized_tli);
197  values[1] = LSNGetDatum(summarized_lsn);
198  values[2] = LSNGetDatum(pending_lsn);
199 
200  if (summarizer_pid < 0)
201  nulls[3] = true;
202  else
203  values[3] = Int32GetDatum(summarizer_pid);
204 
205  htup = heap_form_tuple(tupdesc, values, nulls);
206 
208 }
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:224
#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
static Datum Int32GetDatum(int32 X)
Definition: postgres.h:212
void GetWalSummarizerState(TimeLineID *summarized_tli, XLogRecPtr *summarized_lsn, XLogRecPtr *pending_lsn, int *summarizer_pid)
#define NUM_STATE_ATTS
uint64 XLogRecPtr
Definition: xlogdefs.h:21
uint32 TimeLineID
Definition: xlogdefs.h:59

References elog, ERROR, get_call_result_type(), GetWalSummarizerState(), heap_form_tuple(), HeapTupleGetDatum(), Int32GetDatum(), Int64GetDatum(), LSNGetDatum(), NUM_STATE_ATTS, PG_RETURN_DATUM, TYPEFUNC_COMPOSITE, and values.

◆ pg_wal_summary_contents()

Datum pg_wal_summary_contents ( PG_FUNCTION_ARGS  )

Definition at line 69 of file walsummaryfuncs.c.

70 {
71  ReturnSetInfo *rsi;
73  bool nulls[NUM_SUMMARY_ATTS];
74  WalSummaryFile ws;
75  WalSummaryIO io;
76  BlockRefTableReader *reader;
77  int64 raw_tli;
78  RelFileLocator rlocator;
79  ForkNumber forknum;
80  BlockNumber limit_block;
81 
82  InitMaterializedSRF(fcinfo, 0);
83  rsi = (ReturnSetInfo *) fcinfo->resultinfo;
84  memset(nulls, 0, sizeof(nulls));
85 
86  /*
87  * Since the timeline could at least in theory be more than 2^31, and
88  * since we don't have unsigned types at the SQL level, it is passed as a
89  * 64-bit integer. Test whether it's out of range.
90  */
91  raw_tli = PG_GETARG_INT64(0);
92  if (raw_tli < 1 || raw_tli > PG_INT32_MAX)
93  ereport(ERROR,
94  errcode(ERRCODE_INVALID_PARAMETER_VALUE),
95  errmsg("invalid timeline %lld", (long long) raw_tli));
96 
97  /* Prepare to read the specified WAL summary file. */
98  ws.tli = (TimeLineID) raw_tli;
99  ws.start_lsn = PG_GETARG_LSN(1);
100  ws.end_lsn = PG_GETARG_LSN(2);
101  io.filepos = 0;
102  io.file = OpenWalSummaryFile(&ws, false);
104  FilePathName(io.file),
105  ReportWalSummaryError, NULL);
106 
107  /* Loop over relation forks. */
108  while (BlockRefTableReaderNextRelation(reader, &rlocator, &forknum,
109  &limit_block))
110  {
112  HeapTuple tuple;
113 
115 
116  values[0] = ObjectIdGetDatum(rlocator.relNumber);
117  values[1] = ObjectIdGetDatum(rlocator.spcOid);
118  values[2] = ObjectIdGetDatum(rlocator.dbOid);
119  values[3] = Int16GetDatum((int16) forknum);
120 
121  /* Loop over blocks within the current relation fork. */
122  while (1)
123  {
124  unsigned nblocks;
125  unsigned i;
126 
128 
129  nblocks = BlockRefTableReaderGetBlocks(reader, blocks,
131  if (nblocks == 0)
132  break;
133 
134  /*
135  * For each block that we specifically know to have been modified,
136  * emit a row with that block number and limit_block = false.
137  */
138  values[5] = BoolGetDatum(false);
139  for (i = 0; i < nblocks; ++i)
140  {
141  values[4] = Int64GetDatum((int64) blocks[i]);
142 
143  tuple = heap_form_tuple(rsi->setDesc, values, nulls);
144  tuplestore_puttuple(rsi->setResult, tuple);
145  }
146 
147  /*
148  * If the limit block is not InvalidBlockNumber, emit an extra row
149  * with that block number and limit_block = true.
150  *
151  * There is no point in doing this when the limit_block is
152  * InvalidBlockNumber, because no block with that number or any
153  * higher number can ever exist.
154  */
155  if (BlockNumberIsValid(limit_block))
156  {
157  values[4] = Int64GetDatum((int64) limit_block);
158  values[5] = BoolGetDatum(true);
159 
160  tuple = heap_form_tuple(rsi->setDesc, values, nulls);
161  tuplestore_puttuple(rsi->setResult, tuple);
162  }
163  }
164  }
165 
166  /* Cleanup */
168  FileClose(io.file);
169 
170  return (Datum) 0;
171 }
BlockRefTableReader * CreateBlockRefTableReader(io_callback_fn read_callback, void *read_callback_arg, char *error_filename, report_error_fn error_callback, void *error_callback_arg)
Definition: blkreftable.c:577
bool BlockRefTableReaderNextRelation(BlockRefTableReader *reader, RelFileLocator *rlocator, ForkNumber *forknum, BlockNumber *limit_block)
Definition: blkreftable.c:613
unsigned BlockRefTableReaderGetBlocks(BlockRefTableReader *reader, BlockNumber *blocks, int nblocks)
Definition: blkreftable.c:689
void DestroyBlockRefTableReader(BlockRefTableReader *reader)
Definition: blkreftable.c:773
uint32 BlockNumber
Definition: block.h:31
static bool BlockNumberIsValid(BlockNumber blockNumber)
Definition: block.h:71
#define PG_INT32_MAX
Definition: c.h:589
signed short int16
Definition: c.h:493
int errcode(int sqlerrcode)
Definition: elog.c:859
int errmsg(const char *fmt,...)
Definition: elog.c:1072
#define ereport(elevel,...)
Definition: elog.h:149
void FileClose(File file)
Definition: fd.c:1978
char * FilePathName(File file)
Definition: fd.c:2461
#define PG_GETARG_INT64(n)
Definition: fmgr.h:283
int i
Definition: isn.c:73
#define PG_GETARG_LSN(n)
Definition: pg_lsn.h:33
static Datum Int16GetDatum(int16 X)
Definition: postgres.h:172
static Datum BoolGetDatum(bool X)
Definition: postgres.h:102
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:252
ForkNumber
Definition: relpath.h:48
RelFileNumber relNumber
off_t filepos
Definition: walsummary.h:24
File OpenWalSummaryFile(WalSummaryFile *ws, bool missing_ok)
Definition: walsummary.c:205
int ReadWalSummary(void *wal_summary_io, void *data, int length)
Definition: walsummary.c:273
void ReportWalSummaryError(void *callback_arg, char *fmt,...)
Definition: walsummary.c:322
#define NUM_SUMMARY_ATTS
#define MAX_BLOCKS_PER_CALL

References BlockNumberIsValid(), BlockRefTableReaderGetBlocks(), BlockRefTableReaderNextRelation(), BoolGetDatum(), CHECK_FOR_INTERRUPTS, CreateBlockRefTableReader(), RelFileLocator::dbOid, DestroyBlockRefTableReader(), WalSummaryFile::end_lsn, ereport, errcode(), errmsg(), ERROR, WalSummaryIO::file, FileClose(), FilePathName(), WalSummaryIO::filepos, heap_form_tuple(), i, InitMaterializedSRF(), Int16GetDatum(), Int64GetDatum(), MAX_BLOCKS_PER_CALL, NUM_SUMMARY_ATTS, ObjectIdGetDatum(), OpenWalSummaryFile(), PG_GETARG_INT64, PG_GETARG_LSN, PG_INT32_MAX, ReadWalSummary(), RelFileLocator::relNumber, ReportWalSummaryError(), ReturnSetInfo::setDesc, ReturnSetInfo::setResult, RelFileLocator::spcOid, WalSummaryFile::start_lsn, WalSummaryFile::tli, tuplestore_puttuple(), and values.