PostgreSQL Source Code  git master
pg_walsummary.c File Reference
#include "postgres_fe.h"
#include <fcntl.h>
#include <limits.h>
#include "common/blkreftable.h"
#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "getopt_long.h"
#include "lib/stringinfo.h"
Include dependency graph for pg_walsummary.c:

Go to the source code of this file.

Data Structures

struct  ws_options
 
struct  ws_file_info
 

Typedefs

typedef struct ws_options ws_options
 
typedef struct ws_file_info ws_file_info
 

Functions

static void dump_one_relation (ws_options *opt, RelFileLocator *rlocator, ForkNumber forknum, BlockNumber limit_block, BlockRefTableReader *reader)
 
static void help (const char *progname)
 
static int compare_block_numbers (const void *a, const void *b)
 
static int walsummary_read_callback (void *callback_arg, void *data, int length)
 
static void walsummary_error_callback (void *callback_arg, char *fmt,...) pg_attribute_printf(2
 
static void int main (int argc, char *argv[])
 

Variables

static BlockNumberblock_buffer = NULL
 
static unsigned block_buffer_size = 512
 

Typedef Documentation

◆ ws_file_info

typedef struct ws_file_info ws_file_info

◆ ws_options

typedef struct ws_options ws_options

Function Documentation

◆ compare_block_numbers()

static int compare_block_numbers ( const void *  a,
const void *  b 
)
static

Definition at line 219 of file pg_walsummary.c.

220 {
221  BlockNumber aa = *(BlockNumber *) a;
222  BlockNumber bb = *(BlockNumber *) b;
223 
224  return pg_cmp_u32(aa, bb);
225 }
uint32 BlockNumber
Definition: block.h:31
static int pg_cmp_u32(uint32 a, uint32 b)
Definition: int.h:604
int b
Definition: isn.c:70
int a
Definition: isn.c:69

References a, b, and pg_cmp_u32().

Referenced by dump_one_relation().

◆ dump_one_relation()

static void dump_one_relation ( ws_options opt,
RelFileLocator rlocator,
ForkNumber  forknum,
BlockNumber  limit_block,
BlockRefTableReader reader 
)
static

Definition at line 129 of file pg_walsummary.c.

132 {
133  unsigned i = 0;
134  unsigned nblocks;
135  BlockNumber startblock = InvalidBlockNumber;
136  BlockNumber endblock = InvalidBlockNumber;
137 
138  /* Dump limit block, if any. */
139  if (limit_block != InvalidBlockNumber)
140  printf("TS %u, DB %u, REL %u, FORK %s: limit %u\n",
141  rlocator->spcOid, rlocator->dbOid, rlocator->relNumber,
142  forkNames[forknum], limit_block);
143 
144  /* If we haven't allocated a block buffer yet, do that now. */
145  if (block_buffer == NULL)
147 
148  /* Try to fill the block buffer. */
149  nblocks = BlockRefTableReaderGetBlocks(reader,
150  block_buffer,
152 
153  /* If we filled the block buffer completely, we must enlarge it. */
154  while (nblocks >= block_buffer_size)
155  {
156  unsigned new_size;
157 
158  /* Double the size, being careful about overflow. */
159  new_size = block_buffer_size * 2;
160  if (new_size < block_buffer_size)
161  new_size = PG_UINT32_MAX;
163 
164  /* Try to fill the newly-allocated space. */
165  nblocks +=
168  new_size - block_buffer_size);
169 
170  /* Save the new size for later calls. */
171  block_buffer_size = new_size;
172  }
173 
174  /* If we don't need to produce any output, skip the rest of this. */
175  if (opt->quiet)
176  return;
177 
178  /*
179  * Sort the returned block numbers. If the block reference table was using
180  * the bitmap representation for a given chunk, the block numbers in that
181  * chunk will already be sorted, but when the array-of-offsets
182  * representation is used, we can receive block numbers here out of order.
183  */
185 
186  /* Dump block references. */
187  while (i < nblocks)
188  {
189  /*
190  * Find the next range of blocks to print, but if --individual was
191  * specified, then consider each block a separate range.
192  */
193  startblock = endblock = block_buffer[i++];
194  if (!opt->individual)
195  {
196  while (i < nblocks && block_buffer[i] == endblock + 1)
197  {
198  endblock++;
199  i++;
200  }
201  }
202 
203  /* Format this range of block numbers as a string. */
204  if (startblock == endblock)
205  printf("TS %u, DB %u, REL %u, FORK %s: block %u\n",
206  rlocator->spcOid, rlocator->dbOid, rlocator->relNumber,
207  forkNames[forknum], startblock);
208  else
209  printf("TS %u, DB %u, REL %u, FORK %s: blocks %u..%u\n",
210  rlocator->spcOid, rlocator->dbOid, rlocator->relNumber,
211  forkNames[forknum], startblock, endblock);
212  }
213 }
unsigned BlockRefTableReaderGetBlocks(BlockRefTableReader *reader, BlockNumber *blocks, int nblocks)
Definition: blkreftable.c:689
#define InvalidBlockNumber
Definition: block.h:33
#define PG_UINT32_MAX
Definition: c.h:593
#define repalloc_array(pointer, type, count)
Definition: fe_memutils.h:66
#define palloc_array(type, count)
Definition: fe_memutils.h:64
int i
Definition: isn.c:73
static int compare_block_numbers(const void *a, const void *b)
static BlockNumber * block_buffer
Definition: pg_walsummary.c:37
static unsigned block_buffer_size
Definition: pg_walsummary.c:38
#define qsort(a, b, c, d)
Definition: port.h:447
#define printf(...)
Definition: port.h:244
const char *const forkNames[]
Definition: relpath.c:33
RelFileNumber relNumber
bool individual
Definition: pg_walsummary.c:27

References block_buffer, block_buffer_size, BlockRefTableReaderGetBlocks(), compare_block_numbers(), RelFileLocator::dbOid, forkNames, i, ws_options::individual, InvalidBlockNumber, palloc_array, PG_UINT32_MAX, printf, qsort, ws_options::quiet, RelFileLocator::relNumber, repalloc_array, and RelFileLocator::spcOid.

Referenced by main().

◆ help()

static void help ( const char *  progname)
static

Definition at line 265 of file pg_walsummary.c.

266 {
267  printf(_("%s prints the contents of a WAL summary file.\n\n"), progname);
268  printf(_("Usage:\n"));
269  printf(_(" %s [OPTION]... FILE...\n"), progname);
270  printf(_("\nOptions:\n"));
271  printf(_(" -i, --individual list block numbers individually, not as ranges\n"));
272  printf(_(" -q, --quiet don't print anything, just parse the files\n"));
273  printf(_(" -V, --version output version information, then exit\n"));
274  printf(_(" -?, --help show this help, then exit\n"));
275 
276  printf(_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
277  printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
278 }
#define _(x)
Definition: elog.c:90
const char * progname
Definition: main.c:44

References _, printf, and progname.

Referenced by main().

◆ main()

static void int main ( int  argc,
char *  argv[] 
)

Definition at line 53 of file pg_walsummary.c.

54 {
55  static struct option long_options[] = {
56  {"individual", no_argument, NULL, 'i'},
57  {"quiet", no_argument, NULL, 'q'},
58  {NULL, 0, NULL, 0}
59  };
60 
61  const char *progname;
62  int optindex;
63  int c;
64  ws_options opt;
65 
66  memset(&opt, 0, sizeof(ws_options));
67 
68  pg_logging_init(argv[0]);
69  progname = get_progname(argv[0]);
70  set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_walsummary"));
72 
73  /* process command-line options */
74  while ((c = getopt_long(argc, argv, "iq",
75  long_options, &optindex)) != -1)
76  {
77  switch (c)
78  {
79  case 'i':
80  opt.individual = true;
81  break;
82  case 'q':
83  opt.quiet = true;
84  break;
85  default:
86  /* getopt_long already emitted a complaint */
87  pg_log_error_hint("Try \"%s --help\" for more information.", progname);
88  exit(1);
89  }
90  }
91 
92  if (optind >= argc)
93  {
94  pg_log_error("no input files specified");
95  pg_log_error_hint("Try \"%s --help\" for more information.", progname);
96  exit(1);
97  }
98 
99  while (optind < argc)
100  {
101  ws_file_info ws;
102  BlockRefTableReader *reader;
103  RelFileLocator rlocator;
104  ForkNumber forknum;
105  BlockNumber limit_block;
106 
107  ws.filename = argv[optind++];
108  if ((ws.fd = open(ws.filename, O_RDONLY | PG_BINARY, 0)) < 0)
109  pg_fatal("could not open file \"%s\": %m", ws.filename);
110 
112  ws.filename,
114  while (BlockRefTableReaderNextRelation(reader, &rlocator, &forknum,
115  &limit_block))
116  dump_one_relation(&opt, &rlocator, forknum, limit_block, reader);
117 
119  close(ws.fd);
120  }
121 
122  exit(0);
123 }
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
void DestroyBlockRefTableReader(BlockRefTableReader *reader)
Definition: blkreftable.c:773
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1217
#define PG_BINARY
Definition: c.h:1276
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition: exec.c:429
int getopt_long(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex)
Definition: getopt_long.c:60
#define no_argument
Definition: getopt_long.h:24
#define close(a)
Definition: win32.h:12
exit(1)
void pg_logging_init(const char *argv0)
Definition: logging.c:83
#define pg_log_error(...)
Definition: logging.h:106
#define pg_log_error_hint(...)
Definition: logging.h:112
void handle_help_version_opts(int argc, char *argv[], const char *fixed_progname, help_handler hlp)
Definition: option_utils.c:24
#define pg_fatal(...)
PGDLLIMPORT int optind
Definition: getopt.c:50
static void walsummary_error_callback(void *callback_arg, char *fmt,...) pg_attribute_printf(2
static void help(const char *progname)
static void dump_one_relation(ws_options *opt, RelFileLocator *rlocator, ForkNumber forknum, BlockNumber limit_block, BlockRefTableReader *reader)
static int walsummary_read_callback(void *callback_arg, void *data, int length)
const char * get_progname(const char *argv0)
Definition: path.c:575
char * c
ForkNumber
Definition: relpath.h:56
char * filename
Definition: pg_walsummary.c:34

References BlockRefTableReaderNextRelation(), close, CreateBlockRefTableReader(), DestroyBlockRefTableReader(), dump_one_relation(), exit(), ws_file_info::fd, ws_file_info::filename, get_progname(), getopt_long(), handle_help_version_opts(), help(), ws_options::individual, no_argument, optind, PG_BINARY, pg_fatal, pg_log_error, pg_log_error_hint, pg_logging_init(), PG_TEXTDOMAIN, progname, ws_options::quiet, set_pglocale_pgservice(), walsummary_error_callback(), and walsummary_read_callback().

◆ walsummary_error_callback()

void walsummary_error_callback ( void *  callback_arg,
char *  fmt,
  ... 
)
static

Definition at line 231 of file pg_walsummary.c.

232 {
233  va_list ap;
234 
235  va_start(ap, fmt);
237  va_end(ap);
238 
239  exit(1);
240 }
static void const char * fmt
va_end(args)
va_start(args, fmt)
void pg_log_generic_v(enum pg_log_level level, enum pg_log_part part, const char *pg_restrict fmt, va_list ap)
Definition: logging.c:218
@ PG_LOG_PRIMARY
Definition: logging.h:67
@ PG_LOG_ERROR
Definition: logging.h:43

References exit(), fmt, PG_LOG_ERROR, pg_log_generic_v(), PG_LOG_PRIMARY, va_end(), and va_start().

Referenced by main().

◆ walsummary_read_callback()

int walsummary_read_callback ( void *  callback_arg,
void *  data,
int  length 
)
static

Definition at line 246 of file pg_walsummary.c.

247 {
248  ws_file_info *ws = callback_arg;
249  int rc;
250 
251  if ((rc = read(ws->fd, data, length)) < 0)
252  pg_fatal("could not read file \"%s\": %m", ws->filename);
253 
254  return rc;
255 }
#define read(a, b, c)
Definition: win32.h:13
const void * data

References data, ws_file_info::fd, ws_file_info::filename, pg_fatal, and read.

Referenced by main().

Variable Documentation

◆ block_buffer

BlockNumber* block_buffer = NULL
static

Definition at line 37 of file pg_walsummary.c.

Referenced by dump_one_relation().

◆ block_buffer_size

unsigned block_buffer_size = 512
static

Definition at line 38 of file pg_walsummary.c.

Referenced by dump_one_relation().