PostgreSQL Source Code  git master
pg_checksums.c File Reference
#include "postgres_fe.h"
#include <dirent.h>
#include <limits.h>
#include <time.h>
#include <sys/stat.h>
#include <unistd.h>
#include "access/xlog_internal.h"
#include "common/controldata_utils.h"
#include "common/file_perm.h"
#include "common/file_utils.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "getopt_long.h"
#include "pg_getopt.h"
#include "storage/bufpage.h"
#include "storage/checksum.h"
#include "storage/checksum_impl.h"
Include dependency graph for pg_checksums.c:

Go to the source code of this file.

Data Structures

struct  exclude_list_item
 

Macros

#define PG_TEMP_FILES_DIR   "pgsql_tmp"
 
#define PG_TEMP_FILE_PREFIX   "pgsql_tmp"
 

Enumerations

enum  PgChecksumMode { PG_MODE_CHECK, PG_MODE_DISABLE, PG_MODE_ENABLE }
 

Functions

static void usage (void)
 
static void progress_report (bool finished)
 
static bool skipfile (const char *fn)
 
static void scan_file (const char *fn, BlockNumber segmentno)
 
static int64 scan_directory (const char *basedir, const char *subdir, bool sizeonly)
 
int main (int argc, char *argv[])
 

Variables

static int64 files_scanned = 0
 
static int64 files_written = 0
 
static int64 blocks_scanned = 0
 
static int64 blocks_written = 0
 
static int64 badblocks = 0
 
static ControlFileDataControlFile
 
static char * only_filenode = NULL
 
static bool do_sync = true
 
static bool verbose = false
 
static bool showprogress = false
 
static PgChecksumMode mode = PG_MODE_CHECK
 
static const char * progname
 
int64 total_size = 0
 
int64 current_size = 0
 
static pg_time_t last_progress_report = 0
 
static const struct exclude_list_item skip []
 

Macro Definition Documentation

◆ PG_TEMP_FILE_PREFIX

#define PG_TEMP_FILE_PREFIX   "pgsql_tmp"

◆ PG_TEMP_FILES_DIR

#define PG_TEMP_FILES_DIR   "pgsql_tmp"

Enumeration Type Documentation

◆ PgChecksumMode

Enumerator
PG_MODE_CHECK 
PG_MODE_DISABLE 
PG_MODE_ENABLE 

Definition at line 48 of file pg_checksums.c.

Function Documentation

◆ main()

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

Definition at line 472 of file pg_checksums.c.

References _, badblocks, ControlFileData::blcksz, blocks_scanned, blocks_written, ControlFileData::data_checksum_version, DataDir, DB_SHUTDOWNED, DB_SHUTDOWNED_IN_RECOVERY, do_sync, files_scanned, files_written, fprintf, get_controlfile(), get_progname(), getopt_long(), INT64_FORMAT, mode, no_argument, only_filenode, optarg, optind, option_parse_int(), PG_CONTROL_VERSION, ControlFileData::pg_control_version, PG_DATA_CHECKSUM_VERSION, pg_log_error, pg_log_info, pg_logging_init(), PG_MODE_CHECK, PG_MODE_DISABLE, PG_MODE_ENABLE, PG_TEXTDOMAIN, printf, progname, progress_report(), psprintf(), pstrdup(), required_argument, scan_directory(), set_pglocale_pgservice(), showprogress, ControlFileData::state, total_size, update_controlfile(), usage(), and verbose.

473 {
474  static struct option long_options[] = {
475  {"check", no_argument, NULL, 'c'},
476  {"pgdata", required_argument, NULL, 'D'},
477  {"disable", no_argument, NULL, 'd'},
478  {"enable", no_argument, NULL, 'e'},
479  {"filenode", required_argument, NULL, 'f'},
480  {"no-sync", no_argument, NULL, 'N'},
481  {"progress", no_argument, NULL, 'P'},
482  {"verbose", no_argument, NULL, 'v'},
483  {NULL, 0, NULL, 0}
484  };
485 
486  char *DataDir = NULL;
487  int c;
488  int option_index;
489  bool crc_ok;
490 
491  pg_logging_init(argv[0]);
492  set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_checksums"));
493  progname = get_progname(argv[0]);
494 
495  if (argc > 1)
496  {
497  if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
498  {
499  usage();
500  exit(0);
501  }
502  if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
503  {
504  puts("pg_checksums (PostgreSQL) " PG_VERSION);
505  exit(0);
506  }
507  }
508 
509  while ((c = getopt_long(argc, argv, "cD:deNPf:v", long_options, &option_index)) != -1)
510  {
511  switch (c)
512  {
513  case 'c':
515  break;
516  case 'd':
518  break;
519  case 'e':
521  break;
522  case 'f':
523  if (!option_parse_int(optarg, "-f/--filenode", 0,
524  INT_MAX,
525  NULL))
526  exit(1);
528  break;
529  case 'N':
530  do_sync = false;
531  break;
532  case 'v':
533  verbose = true;
534  break;
535  case 'D':
536  DataDir = optarg;
537  break;
538  case 'P':
539  showprogress = true;
540  break;
541  default:
542  fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
543  exit(1);
544  }
545  }
546 
547  if (DataDir == NULL)
548  {
549  if (optind < argc)
550  DataDir = argv[optind++];
551  else
552  DataDir = getenv("PGDATA");
553 
554  /* If no DataDir was specified, and none could be found, error out */
555  if (DataDir == NULL)
556  {
557  pg_log_error("no data directory specified");
558  fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
559  exit(1);
560  }
561  }
562 
563  /* Complain if any arguments remain */
564  if (optind < argc)
565  {
566  pg_log_error("too many command-line arguments (first is \"%s\")",
567  argv[optind]);
568  fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
569  progname);
570  exit(1);
571  }
572 
573  /* filenode checking only works in --check mode */
574  if (mode != PG_MODE_CHECK && only_filenode)
575  {
576  pg_log_error("option -f/--filenode can only be used with --check");
577  fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
578  progname);
579  exit(1);
580  }
581 
582  /* Read the control file and check compatibility */
583  ControlFile = get_controlfile(DataDir, &crc_ok);
584  if (!crc_ok)
585  {
586  pg_log_error("pg_control CRC value is incorrect");
587  exit(1);
588  }
589 
591  {
592  pg_log_error("cluster is not compatible with this version of pg_checksums");
593  exit(1);
594  }
595 
596  if (ControlFile->blcksz != BLCKSZ)
597  {
598  pg_log_error("database cluster is not compatible");
599  fprintf(stderr, _("The database cluster was initialized with block size %u, but pg_checksums was compiled with block size %u.\n"),
600  ControlFile->blcksz, BLCKSZ);
601  exit(1);
602  }
603 
604  /*
605  * Check if cluster is running. A clean shutdown is required to avoid
606  * random checksum failures caused by torn pages. Note that this doesn't
607  * guard against someone starting the cluster concurrently.
608  */
609  if (ControlFile->state != DB_SHUTDOWNED &&
611  {
612  pg_log_error("cluster must be shut down");
613  exit(1);
614  }
615 
617  mode == PG_MODE_CHECK)
618  {
619  pg_log_error("data checksums are not enabled in cluster");
620  exit(1);
621  }
622 
625  {
626  pg_log_error("data checksums are already disabled in cluster");
627  exit(1);
628  }
629 
631  mode == PG_MODE_ENABLE)
632  {
633  pg_log_error("data checksums are already enabled in cluster");
634  exit(1);
635  }
636 
637  /* Operate on all files if checking or enabling checksums */
638  if (mode == PG_MODE_CHECK || mode == PG_MODE_ENABLE)
639  {
640  /*
641  * If progress status information is requested, we need to scan the
642  * directory tree twice: once to know how much total data needs to be
643  * processed and once to do the real work.
644  */
645  if (showprogress)
646  {
647  total_size = scan_directory(DataDir, "global", true);
648  total_size += scan_directory(DataDir, "base", true);
649  total_size += scan_directory(DataDir, "pg_tblspc", true);
650  }
651 
652  (void) scan_directory(DataDir, "global", false);
653  (void) scan_directory(DataDir, "base", false);
654  (void) scan_directory(DataDir, "pg_tblspc", false);
655 
656  if (showprogress)
657  progress_report(true);
658 
659  printf(_("Checksum operation completed\n"));
660  printf(_("Files scanned: %s\n"), psprintf(INT64_FORMAT, files_scanned));
661  printf(_("Blocks scanned: %s\n"), psprintf(INT64_FORMAT, blocks_scanned));
662  if (mode == PG_MODE_CHECK)
663  {
664  printf(_("Bad checksums: %s\n"), psprintf(INT64_FORMAT, badblocks));
665  printf(_("Data checksum version: %u\n"), ControlFile->data_checksum_version);
666 
667  if (badblocks > 0)
668  exit(1);
669  }
670  else if (mode == PG_MODE_ENABLE)
671  {
672  printf(_("Files written: %s\n"), psprintf(INT64_FORMAT, files_written));
673  printf(_("Blocks written: %s\n"), psprintf(INT64_FORMAT, blocks_written));
674  }
675  }
676 
677  /*
678  * Finally make the data durable on disk if enabling or disabling
679  * checksums. Flush first the data directory for safety, and then update
680  * the control file to keep the switch consistent.
681  */
683  {
686 
687  if (do_sync)
688  {
689  pg_log_info("syncing data directory");
690  fsync_pgdata(DataDir, PG_VERSION_NUM);
691  }
692 
693  pg_log_info("updating control file");
695 
696  if (verbose)
697  printf(_("Data checksum version: %u\n"), ControlFile->data_checksum_version);
698  if (mode == PG_MODE_ENABLE)
699  printf(_("Checksums enabled in cluster\n"));
700  else
701  printf(_("Checksums disabled in cluster\n"));
702  }
703 
704  return 0;
705 }
static PgChecksumMode mode
Definition: pg_checksums.c:65
static const char * progname
Definition: pg_checksums.c:67
bool option_parse_int(const char *optarg, const char *optname, int min_range, int max_range, int *result)
Definition: option_utils.c:50
const char * get_progname(const char *argv0)
Definition: path.c:453
#define pg_log_error(...)
Definition: logging.h:80
int getopt_long(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex)
Definition: getopt_long.c:57
static void usage(void)
Definition: pg_checksums.c:77
char * pstrdup(const char *in)
Definition: mcxt.c:1299
void pg_logging_init(const char *argv0)
Definition: logging.c:81
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
static ControlFileData * ControlFile
Definition: pg_checksums.c:41
static int64 blocks_scanned
Definition: pg_checksums.c:38
#define PG_CONTROL_VERSION
Definition: pg_control.h:25
uint32 pg_control_version
Definition: pg_control.h:123
#define printf(...)
Definition: port.h:223
static bool showprogress
Definition: pg_checksums.c:46
#define fprintf
Definition: port.h:221
void update_controlfile(const char *DataDir, ControlFileData *ControlFile, bool do_sync)
#define required_argument
Definition: getopt_long.h:25
int optind
Definition: getopt.c:50
uint32 data_checksum_version
Definition: pg_control.h:222
char * c
static int64 files_written
Definition: pg_checksums.c:37
static bool do_sync
Definition: pg_checksums.c:44
static int64 scan_directory(const char *basedir, const char *subdir, bool sizeonly)
Definition: pg_checksums.c:327
static bool verbose
Definition: pg_checksums.c:45
static int64 files_scanned
Definition: pg_checksums.c:36
#define no_argument
Definition: getopt_long.h:24
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1215
int64 total_size
Definition: pg_checksums.c:72
ControlFileData * get_controlfile(const char *DataDir, bool *crc_ok_p)
#define INT64_FORMAT
Definition: c.h:483
static int64 blocks_written
Definition: pg_checksums.c:39
static void progress_report(bool finished)
Definition: pg_checksums.c:132
static char * only_filenode
Definition: pg_checksums.c:43
static int64 badblocks
Definition: pg_checksums.c:40
#define PG_DATA_CHECKSUM_VERSION
Definition: bufpage.h:200
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition: exec.c:433
char * optarg
Definition: getopt.c:52
char * DataDir
Definition: globals.c:65
#define _(x)
Definition: elog.c:89
#define pg_log_info(...)
Definition: logging.h:88

◆ progress_report()

static void progress_report ( bool  finished)
static

Definition at line 132 of file pg_checksums.c.

References _, Assert, current_size, fprintf, INT64_FORMAT, last_progress_report, now(), showprogress, snprintf, and total_size.

Referenced by main(), and scan_file().

133 {
134  int percent;
135  char total_size_str[32];
136  char current_size_str[32];
137  pg_time_t now;
138 
140 
141  now = time(NULL);
142  if (now == last_progress_report && !finished)
143  return; /* Max once per second */
144 
145  /* Save current time */
147 
148  /* Adjust total size if current_size is larger */
149  if (current_size > total_size)
151 
152  /* Calculate current percentage of size done */
153  percent = total_size ? (int) ((current_size) * 100 / total_size) : 0;
154 
155  /*
156  * Separate step to keep platform-dependent format code out of
157  * translatable strings. And we only test for INT64_FORMAT availability
158  * in snprintf, not fprintf.
159  */
160  snprintf(total_size_str, sizeof(total_size_str), INT64_FORMAT,
161  total_size / (1024 * 1024));
162  snprintf(current_size_str, sizeof(current_size_str), INT64_FORMAT,
163  current_size / (1024 * 1024));
164 
165  fprintf(stderr, _("%*s/%s MB (%d%%) computed"),
166  (int) strlen(current_size_str), current_size_str, total_size_str,
167  percent);
168 
169  /*
170  * Stay on the same line if reporting to a terminal and we're not done
171  * yet.
172  */
173  fputc((!finished && isatty(fileno(stderr))) ? '\r' : '\n', stderr);
174 }
int64 pg_time_t
Definition: pgtime.h:23
static bool showprogress
Definition: pg_checksums.c:46
#define fprintf
Definition: port.h:221
int64 total_size
Definition: pg_checksums.c:72
#define Assert(condition)
Definition: c.h:804
static pg_time_t last_progress_report
Definition: pg_checksums.c:74
#define INT64_FORMAT
Definition: c.h:483
int64 current_size
Definition: pg_checksums.c:73
#define snprintf
Definition: port.h:217
#define _(x)
Definition: elog.c:89
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1544

◆ scan_directory()

static int64 scan_directory ( const char *  basedir,
const char *  subdir,
bool  sizeonly 
)
static

Definition at line 327 of file pg_checksums.c.

References closedir(), dirent::d_name, fn(), lstat, MAXPGPATH, only_filenode, opendir(), pg_log_error, PG_TEMP_FILE_PREFIX, PG_TEMP_FILES_DIR, pgwin32_is_junction(), readdir(), S_ISDIR, S_ISREG, scan_file(), skipfile(), snprintf, stat::st_mode, stat::st_size, strlcpy(), and TABLESPACE_VERSION_DIRECTORY.

Referenced by main().

328 {
329  int64 dirsize = 0;
330  char path[MAXPGPATH];
331  DIR *dir;
332  struct dirent *de;
333 
334  snprintf(path, sizeof(path), "%s/%s", basedir, subdir);
335  dir = opendir(path);
336  if (!dir)
337  {
338  pg_log_error("could not open directory \"%s\": %m", path);
339  exit(1);
340  }
341  while ((de = readdir(dir)) != NULL)
342  {
343  char fn[MAXPGPATH];
344  struct stat st;
345 
346  if (strcmp(de->d_name, ".") == 0 ||
347  strcmp(de->d_name, "..") == 0)
348  continue;
349 
350  /* Skip temporary files */
351  if (strncmp(de->d_name,
353  strlen(PG_TEMP_FILE_PREFIX)) == 0)
354  continue;
355 
356  /* Skip temporary folders */
357  if (strncmp(de->d_name,
359  strlen(PG_TEMP_FILES_DIR)) == 0)
360  continue;
361 
362  snprintf(fn, sizeof(fn), "%s/%s", path, de->d_name);
363  if (lstat(fn, &st) < 0)
364  {
365  pg_log_error("could not stat file \"%s\": %m", fn);
366  exit(1);
367  }
368  if (S_ISREG(st.st_mode))
369  {
370  char fnonly[MAXPGPATH];
371  char *forkpath,
372  *segmentpath;
373  BlockNumber segmentno = 0;
374 
375  if (skipfile(de->d_name))
376  continue;
377 
378  /*
379  * Cut off at the segment boundary (".") to get the segment number
380  * in order to mix it into the checksum. Then also cut off at the
381  * fork boundary, to get the filenode the file belongs to for
382  * filtering.
383  */
384  strlcpy(fnonly, de->d_name, sizeof(fnonly));
385  segmentpath = strchr(fnonly, '.');
386  if (segmentpath != NULL)
387  {
388  *segmentpath++ = '\0';
389  segmentno = atoi(segmentpath);
390  if (segmentno == 0)
391  {
392  pg_log_error("invalid segment number %d in file name \"%s\"",
393  segmentno, fn);
394  exit(1);
395  }
396  }
397 
398  forkpath = strchr(fnonly, '_');
399  if (forkpath != NULL)
400  *forkpath++ = '\0';
401 
402  if (only_filenode && strcmp(only_filenode, fnonly) != 0)
403  /* filenode not to be included */
404  continue;
405 
406  dirsize += st.st_size;
407 
408  /*
409  * No need to work on the file when calculating only the size of
410  * the items in the data folder.
411  */
412  if (!sizeonly)
413  scan_file(fn, segmentno);
414  }
415 #ifndef WIN32
416  else if (S_ISDIR(st.st_mode) || S_ISLNK(st.st_mode))
417 #else
418  else if (S_ISDIR(st.st_mode) || pgwin32_is_junction(fn))
419 #endif
420  {
421  /*
422  * If going through the entries of pg_tblspc, we assume to operate
423  * on tablespace locations where only TABLESPACE_VERSION_DIRECTORY
424  * is valid, resolving the linked locations and dive into them
425  * directly.
426  */
427  if (strncmp("pg_tblspc", subdir, strlen("pg_tblspc")) == 0)
428  {
429  char tblspc_path[MAXPGPATH];
430  struct stat tblspc_st;
431 
432  /*
433  * Resolve tablespace location path and check whether
434  * TABLESPACE_VERSION_DIRECTORY exists. Not finding a valid
435  * location is unexpected, since there should be no orphaned
436  * links and no links pointing to something else than a
437  * directory.
438  */
439  snprintf(tblspc_path, sizeof(tblspc_path), "%s/%s/%s",
441 
442  if (lstat(tblspc_path, &tblspc_st) < 0)
443  {
444  pg_log_error("could not stat file \"%s\": %m",
445  tblspc_path);
446  exit(1);
447  }
448 
449  /*
450  * Move backwards once as the scan needs to happen for the
451  * contents of TABLESPACE_VERSION_DIRECTORY.
452  */
453  snprintf(tblspc_path, sizeof(tblspc_path), "%s/%s",
454  path, de->d_name);
455 
456  /* Looks like a valid tablespace location */
457  dirsize += scan_directory(tblspc_path,
459  sizeonly);
460  }
461  else
462  {
463  dirsize += scan_directory(path, de->d_name, sizeonly);
464  }
465  }
466  }
467  closedir(dir);
468  return dirsize;
469 }
#define pg_log_error(...)
Definition: logging.h:80
int closedir(DIR *)
Definition: dirent.c:123
#define PG_TEMP_FILES_DIR
Definition: pg_checksums.c:62
uint32 BlockNumber
Definition: block.h:31
Definition: dirent.h:9
static char * basedir
Definition: dirent.c:25
#define PG_TEMP_FILE_PREFIX
Definition: pg_checksums.c:63
#define MAXPGPATH
DIR * opendir(const char *)
Definition: dirent.c:33
#define TABLESPACE_VERSION_DIRECTORY
Definition: relpath.h:26
static int64 scan_directory(const char *basedir, const char *subdir, bool sizeonly)
Definition: pg_checksums.c:327
#define S_ISREG(m)
Definition: win32_port.h:327
static void * fn(void *arg)
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
static void scan_file(const char *fn, BlockNumber segmentno)
Definition: pg_checksums.c:195
struct dirent * readdir(DIR *)
Definition: dirent.c:78
#define S_ISDIR(m)
Definition: win32_port.h:324
#define lstat(path, sb)
Definition: win32_port.h:284
static char * only_filenode
Definition: pg_checksums.c:43
char d_name[MAX_PATH]
Definition: dirent.h:15
#define snprintf
Definition: port.h:217
static bool skipfile(const char *fn)
Definition: pg_checksums.c:177
bool pgwin32_is_junction(const char *path)

◆ scan_file()

static void scan_file ( const char *  fn,
BlockNumber  segmentno 
)
static

Definition at line 195 of file pg_checksums.c.

References Assert, badblocks, blocks_scanned, blocks_written, buf, close, current_size, PGAlignedBlock::data, ControlFileData::data_checksum_version, files_scanned, files_written, header(), mode, PageIsNew, PageHeaderData::pd_checksum, PG_BINARY, pg_checksum_page(), PG_DATA_CHECKSUM_VERSION, pg_log_error, pg_log_info, PG_MODE_CHECK, PG_MODE_ENABLE, progress_report(), read, showprogress, verbose, and write.

Referenced by scan_directory().

196 {
199  int f;
200  BlockNumber blockno;
201  int flags;
202  int64 blocks_written_in_file = 0;
203 
205  mode == PG_MODE_CHECK);
206 
207  flags = (mode == PG_MODE_ENABLE) ? O_RDWR : O_RDONLY;
208  f = open(fn, PG_BINARY | flags, 0);
209 
210  if (f < 0)
211  {
212  pg_log_error("could not open file \"%s\": %m", fn);
213  exit(1);
214  }
215 
216  files_scanned++;
217 
218  for (blockno = 0;; blockno++)
219  {
220  uint16 csum;
221  int r = read(f, buf.data, BLCKSZ);
222 
223  if (r == 0)
224  break;
225  if (r != BLCKSZ)
226  {
227  if (r < 0)
228  pg_log_error("could not read block %u in file \"%s\": %m",
229  blockno, fn);
230  else
231  pg_log_error("could not read block %u in file \"%s\": read %d of %d",
232  blockno, fn, r, BLCKSZ);
233  exit(1);
234  }
235  blocks_scanned++;
236 
237  /*
238  * Since the file size is counted as total_size for progress status
239  * information, the sizes of all pages including new ones in the file
240  * should be counted as current_size. Otherwise the progress reporting
241  * calculated using those counters may not reach 100%.
242  */
243  current_size += r;
244 
245  /* New pages have no checksum yet */
246  if (PageIsNew(header))
247  continue;
248 
249  csum = pg_checksum_page(buf.data, blockno + segmentno * RELSEG_SIZE);
250  if (mode == PG_MODE_CHECK)
251  {
252  if (csum != header->pd_checksum)
253  {
255  pg_log_error("checksum verification failed in file \"%s\", block %u: calculated checksum %X but block contains %X",
256  fn, blockno, csum, header->pd_checksum);
257  badblocks++;
258  }
259  }
260  else if (mode == PG_MODE_ENABLE)
261  {
262  int w;
263 
264  /*
265  * Do not rewrite if the checksum is already set to the expected
266  * value.
267  */
268  if (header->pd_checksum == csum)
269  continue;
270 
271  blocks_written_in_file++;
272 
273  /* Set checksum in page header */
274  header->pd_checksum = csum;
275 
276  /* Seek back to beginning of block */
277  if (lseek(f, -BLCKSZ, SEEK_CUR) < 0)
278  {
279  pg_log_error("seek failed for block %u in file \"%s\": %m", blockno, fn);
280  exit(1);
281  }
282 
283  /* Write block with checksum */
284  w = write(f, buf.data, BLCKSZ);
285  if (w != BLCKSZ)
286  {
287  if (w < 0)
288  pg_log_error("could not write block %u in file \"%s\": %m",
289  blockno, fn);
290  else
291  pg_log_error("could not write block %u in file \"%s\": wrote %d of %d",
292  blockno, fn, w, BLCKSZ);
293  exit(1);
294  }
295  }
296 
297  if (showprogress)
298  progress_report(false);
299  }
300 
301  if (verbose)
302  {
303  if (mode == PG_MODE_CHECK)
304  pg_log_info("checksums verified in file \"%s\"", fn);
305  if (mode == PG_MODE_ENABLE)
306  pg_log_info("checksums enabled in file \"%s\"", fn);
307  }
308 
309  /* Update write counters if any write activity has happened */
310  if (blocks_written_in_file > 0)
311  {
312  files_written++;
313  blocks_written += blocks_written_in_file;
314  }
315 
316  close(f);
317 }
static PgChecksumMode mode
Definition: pg_checksums.c:65
#define write(a, b, c)
Definition: win32.h:14
#define pg_log_error(...)
Definition: logging.h:80
static ControlFileData * ControlFile
Definition: pg_checksums.c:41
static int64 blocks_scanned
Definition: pg_checksums.c:38
uint32 BlockNumber
Definition: block.h:31
static bool showprogress
Definition: pg_checksums.c:46
#define PG_BINARY
Definition: c.h:1271
uint16 pd_checksum
Definition: bufpage.h:156
char data[BLCKSZ]
Definition: c.h:1141
unsigned short uint16
Definition: c.h:440
uint32 data_checksum_version
Definition: pg_control.h:222
static char * buf
Definition: pg_test_fsync.c:68
static int64 files_written
Definition: pg_checksums.c:37
static bool verbose
Definition: pg_checksums.c:45
static int64 files_scanned
Definition: pg_checksums.c:36
static void * fn(void *arg)
PageHeaderData * PageHeader
Definition: bufpage.h:166
#define Assert(condition)
Definition: c.h:804
static void header(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:212
static int64 blocks_written
Definition: pg_checksums.c:39
static void progress_report(bool finished)
Definition: pg_checksums.c:132
static int64 badblocks
Definition: pg_checksums.c:40
#define PG_DATA_CHECKSUM_VERSION
Definition: bufpage.h:200
#define PageIsNew(page)
Definition: bufpage.h:229
int64 current_size
Definition: pg_checksums.c:73
#define close(a)
Definition: win32.h:12
uint16 pg_checksum_page(char *page, BlockNumber blkno)
#define read(a, b, c)
Definition: win32.h:13
#define pg_log_info(...)
Definition: logging.h:88

◆ skipfile()

static bool skipfile ( const char *  fn)
static

Definition at line 177 of file pg_checksums.c.

References exclude_list_item::match_prefix, and exclude_list_item::name.

Referenced by scan_directory().

178 {
179  int excludeIdx;
180 
181  for (excludeIdx = 0; skip[excludeIdx].name != NULL; excludeIdx++)
182  {
183  int cmplen = strlen(skip[excludeIdx].name);
184 
185  if (!skip[excludeIdx].match_prefix)
186  cmplen++;
187  if (strncmp(skip[excludeIdx].name, fn, cmplen) == 0)
188  return true;
189  }
190 
191  return false;
192 }
static const struct exclude_list_item skip[]
Definition: pg_checksums.c:116
static void * fn(void *arg)
const char * name
Definition: encode.c:561
const char * name
Definition: basebackup.c:141

◆ usage()

static void usage ( void  )
static

Definition at line 77 of file pg_checksums.c.

References _, exclude_list_item::match_prefix, exclude_list_item::name, printf, and progname.

Referenced by main().

78 {
79  printf(_("%s enables, disables, or verifies data checksums in a PostgreSQL database cluster.\n\n"), progname);
80  printf(_("Usage:\n"));
81  printf(_(" %s [OPTION]... [DATADIR]\n"), progname);
82  printf(_("\nOptions:\n"));
83  printf(_(" [-D, --pgdata=]DATADIR data directory\n"));
84  printf(_(" -c, --check check data checksums (default)\n"));
85  printf(_(" -d, --disable disable data checksums\n"));
86  printf(_(" -e, --enable enable data checksums\n"));
87  printf(_(" -f, --filenode=FILENODE check only relation with specified filenode\n"));
88  printf(_(" -N, --no-sync do not wait for changes to be written safely to disk\n"));
89  printf(_(" -P, --progress show progress information\n"));
90  printf(_(" -v, --verbose output verbose messages\n"));
91  printf(_(" -V, --version output version information, then exit\n"));
92  printf(_(" -?, --help show this help, then exit\n"));
93  printf(_("\nIf no data directory (DATADIR) is specified, "
94  "the environment variable PGDATA\nis used.\n\n"));
95  printf(_("Report bugs to <%s>.\n"), PACKAGE_BUGREPORT);
96  printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
97 }
static const char * progname
Definition: pg_checksums.c:67
#define printf(...)
Definition: port.h:223
#define _(x)
Definition: elog.c:89

Variable Documentation

◆ badblocks

int64 badblocks = 0
static

Definition at line 40 of file pg_checksums.c.

Referenced by main(), and scan_file().

◆ blocks_scanned

int64 blocks_scanned = 0
static

Definition at line 38 of file pg_checksums.c.

Referenced by main(), and scan_file().

◆ blocks_written

int64 blocks_written = 0
static

Definition at line 39 of file pg_checksums.c.

Referenced by main(), and scan_file().

◆ ControlFile

ControlFileData* ControlFile
static

Definition at line 41 of file pg_checksums.c.

◆ current_size

int64 current_size = 0

Definition at line 73 of file pg_checksums.c.

Referenced by progress_report(), scan_file(), and unicode_normalize().

◆ do_sync

bool do_sync = true
static

Definition at line 44 of file pg_checksums.c.

Referenced by main().

◆ files_scanned

int64 files_scanned = 0
static

Definition at line 36 of file pg_checksums.c.

Referenced by main(), and scan_file().

◆ files_written

int64 files_written = 0
static

Definition at line 37 of file pg_checksums.c.

Referenced by main(), and scan_file().

◆ last_progress_report

pg_time_t last_progress_report = 0
static

Definition at line 74 of file pg_checksums.c.

Referenced by progress_report().

◆ mode

Definition at line 65 of file pg_checksums.c.

Referenced by _allocAH(), _copyFunctionParameter(), _copyLockStmt(), _equalFunctionParameter(), _equalLockStmt(), be_lo_open(), bf_load(), from_char_set_mode(), get_mxact_status_for_lock(), GetLockmodeName(), GetMultiXactIdHintBits(), has_any_column_privilege_id(), has_any_column_privilege_id_id(), has_any_column_privilege_id_name(), has_any_column_privilege_name(), has_any_column_privilege_name_id(), has_any_column_privilege_name_name(), has_column_privilege_id_attnum(), has_column_privilege_id_id_attnum(), has_column_privilege_id_id_name(), has_column_privilege_id_name(), has_column_privilege_id_name_attnum(), has_column_privilege_id_name_name(), has_column_privilege_name_attnum(), has_column_privilege_name_id_attnum(), has_column_privilege_name_id_name(), has_column_privilege_name_name(), has_column_privilege_name_name_attnum(), has_column_privilege_name_name_name(), has_database_privilege_id(), has_database_privilege_id_id(), has_database_privilege_id_name(), has_database_privilege_name(), has_database_privilege_name_id(), has_database_privilege_name_name(), has_foreign_data_wrapper_privilege_id(), has_foreign_data_wrapper_privilege_id_id(), has_foreign_data_wrapper_privilege_id_name(), has_foreign_data_wrapper_privilege_name(), has_foreign_data_wrapper_privilege_name_id(), has_foreign_data_wrapper_privilege_name_name(), has_function_privilege_id(), has_function_privilege_id_id(), has_function_privilege_id_name(), has_function_privilege_name(), has_function_privilege_name_id(), has_function_privilege_name_name(), has_language_privilege_id(), has_language_privilege_id_id(), has_language_privilege_id_name(), has_language_privilege_name(), has_language_privilege_name_id(), has_language_privilege_name_name(), has_schema_privilege_id(), has_schema_privilege_id_id(), has_schema_privilege_id_name(), has_schema_privilege_name(), has_schema_privilege_name_id(), has_schema_privilege_name_name(), has_sequence_privilege_id(), has_sequence_privilege_id_id(), has_sequence_privilege_id_name(), has_sequence_privilege_name(), has_sequence_privilege_name_id(), has_sequence_privilege_name_name(), has_server_privilege_id(), has_server_privilege_id_id(), has_server_privilege_id_name(), has_server_privilege_name(), has_server_privilege_name_id(), has_server_privilege_name_name(), has_table_privilege_id(), has_table_privilege_id_id(), has_table_privilege_id_name(), has_table_privilege_name(), has_table_privilege_name_id(), has_table_privilege_name_name(), has_tablespace_privilege_id(), has_tablespace_privilege_id_id(), has_tablespace_privilege_id_name(), has_tablespace_privilege_name(), has_tablespace_privilege_name_id(), has_tablespace_privilege_name_name(), has_type_privilege_id(), has_type_privilege_id_id(), has_type_privilege_id_name(), has_type_privilege_name(), has_type_privilege_name_id(), has_type_privilege_name_name(), lo_creat(), lo_open(), main(), open_target_file(), pg_has_role_id(), pg_has_role_id_id(), pg_has_role_id_name(), pg_has_role_name(), pg_has_role_name_id(), pg_has_role_name_name(), pg_lock_status(), pgp_s2k_fill(), pgp_set_s2k_mode(), pgp_set_text_mode(), pgtls_verify_peer_name_matches_certificate_guts(), raw_parser(), rj_load(), scan_file(), SyncRepQueueInsert(), SyncRepUpdateSyncStandbysDefined(), SyncRepWaitForLSN(), SyncRepWakeQueue(), and tarOpen().

◆ only_filenode

char* only_filenode = NULL
static

Definition at line 43 of file pg_checksums.c.

Referenced by main(), and scan_directory().

◆ progname

const char* progname
static

Definition at line 67 of file pg_checksums.c.

Referenced by main(), and usage().

◆ showprogress

bool showprogress = false
static

Definition at line 46 of file pg_checksums.c.

Referenced by main(), progress_report(), and scan_file().

◆ skip

const struct exclude_list_item skip[]
static
Initial value:
= {
{"pg_control", false},
{"pg_filenode.map", false},
{"pg_internal.init", true},
{"PG_VERSION", false},
{NULL, false}
}

Definition at line 116 of file pg_checksums.c.

Referenced by array_replace_internal(), DefineVirtualRelation(), dependencies_clauselist_selectivity(), heap_hot_search_buffer(), initTrie(), and verify_heapam().

◆ total_size

int64 total_size = 0

Definition at line 72 of file pg_checksums.c.

Referenced by main(), make_new_segment(), and progress_report().

◆ verbose

bool verbose = false
static

Definition at line 45 of file pg_checksums.c.

Referenced by main(), and scan_file().