62 #define PG_TEMP_FILES_DIR "pgsql_tmp"
63 #define PG_TEMP_FILE_PREFIX "pgsql_tmp"
79 printf(
_(
"%s enables, disables, or verifies data checksums in a PostgreSQL database cluster.\n\n"),
progname);
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);
117 {
"pg_control",
false},
118 {
"pg_filenode.map",
false},
119 {
"pg_internal.init",
true},
120 {
"PG_VERSION",
false},
122 {
"config_exec_params",
true},
153 fprintf(stderr,
_(
"%lld/%lld MB (%d%%) computed"),
162 fputc((!finished && isatty(fileno(stderr))) ?
'\r' :
'\n', stderr);
170 for (excludeIdx = 0;
skip[excludeIdx].
name != NULL; excludeIdx++)
172 int cmplen = strlen(
skip[excludeIdx].
name);
176 if (strncmp(
skip[excludeIdx].
name,
fn, cmplen) == 0)
191 int64 blocks_written_in_file = 0;
200 pg_fatal(
"could not open file \"%s\": %m",
fn);
204 for (blockno = 0;; blockno++)
207 int r =
read(f,
buf.data, BLCKSZ);
214 pg_fatal(
"could not read block %u in file \"%s\": %m",
217 pg_fatal(
"could not read block %u in file \"%s\": read %d of %d",
218 blockno,
fn, r, BLCKSZ);
237 if (csum !=
header->pd_checksum)
240 pg_log_error(
"checksum verification failed in file \"%s\", block %u: calculated checksum %X but block contains %X",
241 fn, blockno, csum,
header->pd_checksum);
253 if (
header->pd_checksum == csum)
256 blocks_written_in_file++;
259 header->pd_checksum = csum;
262 if (lseek(f, -BLCKSZ, SEEK_CUR) < 0)
263 pg_fatal(
"seek failed for block %u in file \"%s\": %m", blockno,
fn);
270 pg_fatal(
"could not write block %u in file \"%s\": %m",
273 pg_fatal(
"could not write block %u in file \"%s\": wrote %d of %d",
274 blockno,
fn, w, BLCKSZ);
291 if (blocks_written_in_file > 0)
318 pg_fatal(
"could not open directory \"%s\": %m", path);
319 while ((de =
readdir(dir)) != NULL)
324 if (strcmp(de->
d_name,
".") == 0 ||
325 strcmp(de->
d_name,
"..") == 0)
342 pg_fatal(
"could not stat file \"%s\": %m",
fn);
360 segmentpath = strchr(fnonly,
'.');
361 if (segmentpath != NULL)
363 *segmentpath++ =
'\0';
364 segmentno = atoi(segmentpath);
366 pg_fatal(
"invalid segment number %d in file name \"%s\"",
370 forkpath = strchr(fnonly,
'_');
371 if (forkpath != NULL)
395 if (strncmp(
"pg_tblspc", subdir, strlen(
"pg_tblspc")) == 0)
398 struct stat tblspc_st;
407 snprintf(tblspc_path,
sizeof(tblspc_path),
"%s/%s/%s",
410 if (
lstat(tblspc_path, &tblspc_st) < 0)
411 pg_fatal(
"could not stat file \"%s\": %m",
418 snprintf(tblspc_path,
sizeof(tblspc_path),
"%s/%s",
439 static struct option long_options[] = {
462 if (strcmp(argv[1],
"--help") == 0 || strcmp(argv[1],
"-?") == 0)
467 if (strcmp(argv[1],
"--version") == 0 || strcmp(argv[1],
"-V") == 0)
469 puts(
"pg_checksums (PostgreSQL) " PG_VERSION);
474 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"));
636 fsync_pgdata(
DataDir, PG_VERSION_NUM);
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 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 *)
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)
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[]
#define PG_TEMP_FILES_DIR
#define PG_TEMP_FILE_PREFIX
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 int64 blocks_written
static const char * progname
static int64 files_written
#define PG_CONTROL_VERSION
@ DB_SHUTDOWNED_IN_RECOVERY
PGDLLIMPORT char * optarg
static void header(const char *fmt,...) pg_attribute_printf(1
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)