69 printf(
_(
"%s enables, disables, or verifies data checksums in a PostgreSQL database cluster.\n\n"),
progname);
73 printf(
_(
" [-D, --pgdata=]DATADIR data directory\n"));
74 printf(
_(
" -c, --check check data checksums (default)\n"));
75 printf(
_(
" -d, --disable disable data checksums\n"));
76 printf(
_(
" -e, --enable enable data checksums\n"));
77 printf(
_(
" -f, --filenode=FILENODE check only relation with specified filenode\n"));
78 printf(
_(
" -N, --no-sync do not wait for changes to be written safely to disk\n"));
79 printf(
_(
" -P, --progress show progress information\n"));
80 printf(
_(
" --sync-method=METHOD set method for syncing files to disk\n"));
81 printf(
_(
" -v, --verbose output verbose messages\n"));
82 printf(
_(
" -V, --version output version information, then exit\n"));
83 printf(
_(
" -?, --help show this help, then exit\n"));
84 printf(
_(
"\nIf no data directory (DATADIR) is specified, "
85 "the environment variable PGDATA\nis used.\n\n"));
86 printf(
_(
"Report bugs to <%s>.\n"), PACKAGE_BUGREPORT);
87 printf(
_(
"%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
108 {
"pg_control",
false},
109 {
"pg_filenode.map",
false},
110 {
"pg_internal.init",
true},
111 {
"PG_VERSION",
false},
113 {
"config_exec_params",
true},
144 fprintf(stderr,
_(
"%lld/%lld MB (%d%%) computed"),
153 fputc((!finished && isatty(fileno(stderr))) ?
'\r' :
'\n', stderr);
161 for (excludeIdx = 0;
skip[excludeIdx].
name != NULL; excludeIdx++)
163 int cmplen = strlen(
skip[excludeIdx].
name);
167 if (strncmp(
skip[excludeIdx].
name,
fn, cmplen) == 0)
182 int64 blocks_written_in_file = 0;
191 pg_fatal(
"could not open file \"%s\": %m",
fn);
195 for (blockno = 0;; blockno++)
198 int r =
read(f,
buf.data, BLCKSZ);
205 pg_fatal(
"could not read block %u in file \"%s\": %m",
208 pg_fatal(
"could not read block %u in file \"%s\": read %d of %d",
209 blockno,
fn, r, BLCKSZ);
231 pg_log_error(
"checksum verification failed in file \"%s\", block %u: calculated checksum %X but block contains %X",
247 blocks_written_in_file++;
253 if (lseek(f, -BLCKSZ, SEEK_CUR) < 0)
254 pg_fatal(
"seek failed for block %u in file \"%s\": %m", blockno,
fn);
261 pg_fatal(
"could not write block %u in file \"%s\": %m",
264 pg_fatal(
"could not write block %u in file \"%s\": wrote %d of %d",
265 blockno,
fn, w, BLCKSZ);
282 if (blocks_written_in_file > 0)
309 pg_fatal(
"could not open directory \"%s\": %m", path);
310 while ((de =
readdir(dir)) != NULL)
315 if (strcmp(de->
d_name,
".") == 0 ||
316 strcmp(de->
d_name,
"..") == 0)
332 if (strcmp(de->
d_name,
".DS_Store") == 0)
337 pg_fatal(
"could not stat file \"%s\": %m",
fn);
355 segmentpath = strchr(fnonly,
'.');
356 if (segmentpath != NULL)
358 *segmentpath++ =
'\0';
359 segmentno = atoi(segmentpath);
361 pg_fatal(
"invalid segment number %d in file name \"%s\"",
365 forkpath = strchr(fnonly,
'_');
366 if (forkpath != NULL)
393 struct stat tblspc_st;
402 snprintf(tblspc_path,
sizeof(tblspc_path),
"%s/%s/%s",
405 if (
lstat(tblspc_path, &tblspc_st) < 0)
406 pg_fatal(
"could not stat file \"%s\": %m",
413 snprintf(tblspc_path,
sizeof(tblspc_path),
"%s/%s",
434 static struct option long_options[] = {
458 if (strcmp(argv[1],
"--help") == 0 || strcmp(argv[1],
"-?") == 0)
463 if (strcmp(argv[1],
"--version") == 0 || strcmp(argv[1],
"-V") == 0)
465 puts(
"pg_checksums (PostgreSQL) " PG_VERSION);
470 while ((
c =
getopt_long(argc, argv,
"cdD:ef:NPv", long_options, &option_index)) != -1)
532 pg_log_error(
"too many command-line arguments (first is \"%s\")",
541 pg_log_error(
"option -f/--filenode can only be used with --check");
549 pg_fatal(
"pg_control CRC value is incorrect");
552 pg_fatal(
"cluster is not compatible with this version of pg_checksums");
557 pg_log_error_detail(
"The database cluster was initialized with block size %u, but pg_checksums was compiled with block size %u.",
569 pg_fatal(
"cluster must be shut down");
573 pg_fatal(
"data checksums are not enabled in cluster");
577 pg_fatal(
"data checksums are already disabled in cluster");
581 pg_fatal(
"data checksums are already enabled in cluster");
605 printf(
_(
"Checksum operation completed\n"));
645 printf(
_(
"Checksums enabled in cluster\n"));
647 printf(
_(
"Checksums disabled in cluster\n"));
Datum now(PG_FUNCTION_ARGS)
PageHeaderData * PageHeader
static bool PageIsNew(Page page)
#define PG_DATA_CHECKSUM_VERSION
#define Assert(condition)
#define PG_TEXTDOMAIN(domain)
uint16 pg_checksum_page(char *page, BlockNumber blkno)
void set_pglocale_pgservice(const char *argv0, const char *app)
void update_controlfile(const char *DataDir, ControlFileData *ControlFile, bool do_sync)
ControlFileData * get_controlfile(const char *DataDir, bool *crc_ok_p)
struct dirent * readdir(DIR *)
DIR * opendir(const char *)
#define PG_TEMP_FILES_DIR
#define PG_TEMP_FILE_PREFIX
@ DATA_DIR_SYNC_METHOD_FSYNC
int getopt_long(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex)
#define required_argument
void pg_logging_init(const char *argv0)
#define pg_log_error(...)
#define pg_log_error_hint(...)
#define pg_log_error_detail(...)
char * pstrdup(const char *in)
bool option_parse_int(const char *optarg, const char *optname, int min_range, int max_range, int *result)
bool parse_sync_method(const char *optarg, DataDirSyncMethod *sync_method)
int main(int argc, char *argv[])
static void scan_file(const char *fn, int segmentno)
static PgChecksumMode mode
static char * only_filenode
static int64 blocks_scanned
static int64 current_size
static void progress_report(bool finished)
static pg_time_t last_progress_report
static const struct exclude_list_item skip[]
static int64 scan_directory(const char *basedir, const char *subdir, bool sizeonly)
static int64 files_scanned
static bool skipfile(const char *fn)
static ControlFileData * ControlFile
static DataDirSyncMethod sync_method
static int64 blocks_written
static const char * progname
static int64 files_written
#define PG_CONTROL_VERSION
@ DB_SHUTDOWNED_IN_RECOVERY
PGDLLIMPORT char * optarg
const char * get_progname(const char *argv0)
size_t strlcpy(char *dst, const char *src, size_t siz)
#define TABLESPACE_VERSION_DIRECTORY
uint32 pg_control_version
uint32 data_checksum_version
static void * fn(void *arg)