70 printf(
_(
"%s enables, disables, or verifies data checksums in a PostgreSQL database cluster.\n\n"),
progname);
74 printf(
_(
" [-D, --pgdata=]DATADIR data directory\n"));
75 printf(
_(
" -c, --check check data checksums (default)\n"));
76 printf(
_(
" -d, --disable disable data checksums\n"));
77 printf(
_(
" -e, --enable enable data checksums\n"));
78 printf(
_(
" -f, --filenode=FILENODE check only relation with specified filenode\n"));
79 printf(
_(
" -N, --no-sync do not wait for changes to be written safely to disk\n"));
80 printf(
_(
" -P, --progress show progress information\n"));
81 printf(
_(
" --sync-method=METHOD set method for syncing files to disk\n"));
82 printf(
_(
" -v, --verbose output verbose messages\n"));
83 printf(
_(
" -V, --version output version information, then exit\n"));
84 printf(
_(
" -?, --help show this help, then exit\n"));
85 printf(
_(
"\nIf no data directory (DATADIR) is specified, "
86 "the environment variable PGDATA\nis used.\n\n"));
87 printf(
_(
"Report bugs to <%s>.\n"), PACKAGE_BUGREPORT);
88 printf(
_(
"%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
109 {
"pg_control",
false},
110 {
"pg_filenode.map",
false},
111 {
"pg_internal.init",
true},
112 {
"PG_VERSION",
false},
114 {
"config_exec_params",
true},
145 fprintf(stderr,
_(
"%lld/%lld MB (%d%%) computed"),
154 fputc((!finished && isatty(fileno(stderr))) ?
'\r' :
'\n', stderr);
162 for (excludeIdx = 0;
skip[excludeIdx].
name != NULL; excludeIdx++)
164 int cmplen = strlen(
skip[excludeIdx].
name);
168 if (strncmp(
skip[excludeIdx].
name,
fn, cmplen) == 0)
183 int64 blocks_written_in_file = 0;
192 pg_fatal(
"could not open file \"%s\": %m",
fn);
196 for (blockno = 0;; blockno++)
199 int r =
read(f,
buf.data, BLCKSZ);
206 pg_fatal(
"could not read block %u in file \"%s\": %m",
209 pg_fatal(
"could not read block %u in file \"%s\": read %d of %d",
210 blockno,
fn, r, BLCKSZ);
232 pg_log_error(
"checksum verification failed in file \"%s\", block %u: calculated checksum %X but block contains %X",
248 blocks_written_in_file++;
254 if (lseek(f, -BLCKSZ, SEEK_CUR) < 0)
255 pg_fatal(
"seek failed for block %u in file \"%s\": %m", blockno,
fn);
262 pg_fatal(
"could not write block %u in file \"%s\": %m",
265 pg_fatal(
"could not write block %u in file \"%s\": wrote %d of %d",
266 blockno,
fn, w, BLCKSZ);
283 if (blocks_written_in_file > 0)
310 pg_fatal(
"could not open directory \"%s\": %m", path);
311 while ((de =
readdir(dir)) != NULL)
316 if (strcmp(de->
d_name,
".") == 0 ||
317 strcmp(de->
d_name,
"..") == 0)
334 pg_fatal(
"could not stat file \"%s\": %m",
fn);
352 segmentpath = strchr(fnonly,
'.');
353 if (segmentpath != NULL)
355 *segmentpath++ =
'\0';
356 segmentno = atoi(segmentpath);
358 pg_fatal(
"invalid segment number %d in file name \"%s\"",
362 forkpath = strchr(fnonly,
'_');
363 if (forkpath != NULL)
387 if (strncmp(
"pg_tblspc", subdir, strlen(
"pg_tblspc")) == 0)
390 struct stat tblspc_st;
399 snprintf(tblspc_path,
sizeof(tblspc_path),
"%s/%s/%s",
402 if (
lstat(tblspc_path, &tblspc_st) < 0)
403 pg_fatal(
"could not stat file \"%s\": %m",
410 snprintf(tblspc_path,
sizeof(tblspc_path),
"%s/%s",
431 static struct option long_options[] = {
455 if (strcmp(argv[1],
"--help") == 0 || strcmp(argv[1],
"-?") == 0)
460 if (strcmp(argv[1],
"--version") == 0 || strcmp(argv[1],
"-V") == 0)
462 puts(
"pg_checksums (PostgreSQL) " PG_VERSION);
467 while ((
c =
getopt_long(argc, argv,
"cdD:ef:NPv", long_options, &option_index)) != -1)
529 pg_log_error(
"too many command-line arguments (first is \"%s\")",
538 pg_log_error(
"option -f/--filenode can only be used with --check");
546 pg_fatal(
"pg_control CRC value is incorrect");
549 pg_fatal(
"cluster is not compatible with this version of pg_checksums");
554 pg_log_error_detail(
"The database cluster was initialized with block size %u, but pg_checksums was compiled with block size %u.",
566 pg_fatal(
"cluster must be shut down");
570 pg_fatal(
"data checksums are not enabled in cluster");
574 pg_fatal(
"data checksums are already disabled in cluster");
578 pg_fatal(
"data checksums are already enabled in cluster");
602 printf(
_(
"Checksum operation completed\n"));
642 printf(
_(
"Checksums enabled in cluster\n"));
644 printf(
_(
"Checksums disabled in cluster\n"));
Datum now(PG_FUNCTION_ARGS)
PageHeaderData * PageHeader
static bool PageIsNew(Page page)
#define PG_DATA_CHECKSUM_VERSION
#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
Assert(fmt[strlen(fmt) - 1] !='\n')
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 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)