Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fcf8fc5

Browse files
committed
PGPRO-2065: add support for heapallindexed in newer versions of amcheck
1 parent 8cf98da commit fcf8fc5

File tree

4 files changed

+107
-19
lines changed

4 files changed

+107
-19
lines changed

src/backup.c

+46-19
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ static int is_ptrack_enable = false;
7474
bool is_ptrack_support = false;
7575
bool is_checksum_enabled = false;
7676
bool exclusive_backup = false;
77+
bool heapallindexed_is_supported = false;
7778

7879
/* Backup connections */
7980
static PGconn *backup_conn = NULL;
@@ -1044,6 +1045,7 @@ do_amcheck(void)
10441045
int n_databases = 0;
10451046
bool first_db_with_amcheck = true;
10461047
PGconn *db_conn = NULL;
1048+
bool db_skipped = false;
10471049

10481050
pgBackupGetPath(&current, database_path, lengthof(database_path),
10491051
DATABASE_DIR);
@@ -1076,6 +1078,7 @@ do_amcheck(void)
10761078
{
10771079
if (db_conn)
10781080
pgut_disconnect(db_conn);
1081+
db_skipped = true;
10791082
continue;
10801083
}
10811084

@@ -1125,17 +1128,14 @@ do_amcheck(void)
11251128
}
11261129

11271130
/* TODO write better info message */
1128-
if (check_isok)
1131+
if (check_isok && !db_skipped)
11291132
elog(INFO, "Indexes are valid");
1130-
else
1133+
1134+
if (!check_isok)
11311135
elog(ERROR, "Some indexes are corrupted");
11321136

1133-
//if (backup_files_list)
1134-
//{
1135-
// parray_walk(backup_files_list, pgFileFree);
1136-
// parray_free(backup_files_list);
1137-
// backup_files_list = NULL;
1138-
//}
1137+
if (db_skipped)
1138+
elog(ERROR, "Some databases were not checked");
11391139
}
11401140

11411141
/* Entry point of pg_probackup CHECKDB subcommand. */
@@ -1156,6 +1156,7 @@ do_checkdb(bool need_amcheck)
11561156
if (need_amcheck)
11571157
do_amcheck();
11581158

1159+
/* need to exit with 1 if some corruption is found */
11591160
return 0;
11601161
}
11611162

@@ -3487,12 +3488,16 @@ get_index_list(PGresult *res_db, int db_number,
34873488
dbname,
34883489
instance_config.pguser);
34893490

3490-
res = pgut_execute(db_conn, "SELECT extname, nspname, extversion "
3491+
res = pgut_execute(db_conn, "SELECT "
3492+
"extname, nspname, extversion, "
3493+
"extversion::numeric in (1.1, 1) as old_version "
34913494
"FROM pg_namespace n "
34923495
"JOIN pg_extension e "
34933496
"ON n.oid=e.extnamespace "
3494-
"WHERE e.extname='amcheck'",
3495-
0, NULL);
3497+
"WHERE e.extname IN ('amcheck', 'amcheck_next') "
3498+
"ORDER BY extversion DESC "
3499+
"LIMIT 1",
3500+
0, NULL);
34963501

34973502
if (PQresultStatus(res) != PGRES_TUPLES_OK)
34983503
{
@@ -3503,13 +3508,21 @@ get_index_list(PGresult *res_db, int db_number,
35033508

35043509
if (PQntuples(res) < 1)
35053510
{
3506-
elog(WARNING, "Extension amcheck is not installed in database %s", dbname);
3511+
elog(WARNING, "Extension 'amcheck' or 'amcheck_next' are not installed in database %s", dbname);
35073512
return NULL;
35083513
}
35093514

35103515
nspname = pgut_malloc(strlen(PQgetvalue(res, 0, 1)) + 1);
35113516
strcpy(nspname, PQgetvalue(res, 0, 1));
35123517

3518+
/* heapallindexed_is_supported is database specific */
3519+
if (strcmp(PQgetvalue(res, 0, 2), "1.0") != 0 &&
3520+
strcmp(PQgetvalue(res, 0, 2), "1") != 0)
3521+
heapallindexed_is_supported = true;
3522+
3523+
elog(INFO, "Checking database %s using module '%s' version %s from schema '%s'",
3524+
dbname, PQgetvalue(res, 0, 0), PQgetvalue(res, 0, 2), PQgetvalue(res, 0, 1));
3525+
35133526
/*
35143527
* In order to avoid duplicates, select global indexes
35153528
* (tablespace pg_global with oid 1664) only once
@@ -3566,8 +3579,6 @@ get_index_list(PGresult *res_db, int db_number,
35663579

35673580
PQclear(res);
35683581

3569-
elog(INFO, "Amcheck database '%s'", dbname);
3570-
35713582
return index_list;
35723583
}
35733584

@@ -3577,18 +3588,34 @@ amcheck_one_index(backup_files_arg *arguments,
35773588
pg_indexEntry *ind)
35783589
{
35793590
PGresult *res;
3580-
char *params[1];
3581-
char *query;
3591+
char *params[2];
3592+
char *query = NULL;
3593+
35823594
params[0] = palloc(64);
35833595

3596+
/* first argument is index oid */
35843597
sprintf(params[0], "%i", ind->indexrelid);
3598+
/* second argument is heapallindexed */
3599+
params[1] = heapallindexed ? "true" : "false";
35853600

3586-
query = palloc(strlen(ind->amcheck_nspname)+strlen("SELECT .bt_index_check($1)")+1);
3587-
sprintf(query, "SELECT %s.bt_index_check($1)", ind->amcheck_nspname);
3601+
if (heapallindexed_is_supported)
3602+
{
3603+
query = palloc(strlen(ind->amcheck_nspname)+strlen("SELECT .bt_index_check($1, $2)")+1);
3604+
sprintf(query, "SELECT %s.bt_index_check($1, $2)", ind->amcheck_nspname);
35883605

3589-
res = pgut_execute_parallel(arguments->backup_conn,
3606+
res = pgut_execute_parallel(arguments->backup_conn,
3607+
arguments->cancel_conn,
3608+
query, 2, (const char **)params, true, true);
3609+
}
3610+
else
3611+
{
3612+
query = palloc(strlen(ind->amcheck_nspname)+strlen("SELECT .bt_index_check($1)")+1);
3613+
sprintf(query, "SELECT %s.bt_index_check($1)", ind->amcheck_nspname);
3614+
3615+
res = pgut_execute_parallel(arguments->backup_conn,
35903616
arguments->cancel_conn,
35913617
query, 1, (const char **)params, true, true);
3618+
}
35923619

35933620
if (PQresultStatus(res) != PGRES_TUPLES_OK)
35943621
{

src/help.c

+56
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,11 @@ help_pg_probackup(void)
140140
printf(_(" [--timeline=timeline]\n"));
141141
printf(_(" [--skip-block-validation]\n"));
142142

143+
printf(_("\n %s checkdb [-B backup-path] [--instance=instance_name]\n"), PROGRAM_NAME);
144+
printf(_(" [-D pgdata-path] [--progress] [-j num-threads]\n"));
145+
printf(_(" [--amcheck] [--skip-block-validation]\n"));
146+
printf(_(" [--heapallindexed] [--work-mem]\n"));
147+
143148
printf(_("\n %s show -B backup-path\n"), PROGRAM_NAME);
144149
printf(_(" [--instance=instance_name [-i backup-id]]\n"));
145150
printf(_(" [--format=format]\n"));
@@ -396,6 +401,57 @@ help_validate(void)
396401
printf(_(" available units: 'ms', 's', 'min', 'h', 'd' (default: min)\n"));
397402
}
398403

404+
static void
405+
help_checkdb(void)
406+
{
407+
printf(_("%s checkdb [-B backup-path] [--instance=instance_name]\n"), PROGRAM_NAME);
408+
printf(_(" [-D pgdata-path] [-j num-threads] [--progress]\n"));
409+
printf(_(" [--amcheck] [--skip-block-validation]\n"));
410+
printf(_(" [--heapallindexed] [--work-mem=work_mem_size]\n"));
411+
412+
printf(_(" -B, --backup-path=backup-path location of the backup storage area\n"));
413+
printf(_(" --instance=instance_name name of the instance\n"));
414+
printf(_(" -D, --pgdata=pgdata-path location of the database storage area\n"));
415+
416+
printf(_(" --progress show progress\n"));
417+
printf(_(" -j, --threads=NUM number of parallel threads\n"));
418+
printf(_(" --skip-block-validation skip file-level block checking\n"));
419+
printf(_(" can be used only with --amcheck option\n"));
420+
printf(_(" --amcheck in addition to file-level block checking\n"));
421+
printf(_(" check btree indexes using 'amcheck' or 'amcheck_next' extension"));
422+
printf(_(" --heapallindexed also check that heap is indexed\n"));
423+
printf(_(" can be used only with --amcheck option\n"));
424+
425+
printf(_("\n Logging options:\n"));
426+
printf(_(" --log-level-console=log-level-console\n"));
427+
printf(_(" level for console logging (default: info)\n"));
428+
printf(_(" available options: 'off', 'error', 'warning', 'info', 'log', 'verbose'\n"));
429+
printf(_(" --log-level-file=log-level-file\n"));
430+
printf(_(" level for file logging (default: off)\n"));
431+
printf(_(" available options: 'off', 'error', 'warning', 'info', 'log', 'verbose'\n"));
432+
printf(_(" --log-filename=log-filename\n"));
433+
printf(_(" filename for file logging (default: 'pg_probackup.log')\n"));
434+
printf(_(" support strftime format (example: pg_probackup-%%Y-%%m-%%d_%%H%%M%%S.log\n"));
435+
printf(_(" --error-log-filename=error-log-filename\n"));
436+
printf(_(" filename for error logging (default: none)\n"));
437+
printf(_(" --log-directory=log-directory\n"));
438+
printf(_(" directory for file logging (default: BACKUP_PATH/log)\n"));
439+
printf(_(" --log-rotation-size=log-rotation-size\n"));
440+
printf(_(" rotate logfile if its size exceeds this value; 0 disables; (default: 0)\n"));
441+
printf(_(" available units: 'kB', 'MB', 'GB', 'TB' (default: kB)\n"));
442+
printf(_(" --log-rotation-age=log-rotation-age\n"));
443+
printf(_(" rotate logfile if its age exceeds this value; 0 disables; (default: 0)\n"));
444+
printf(_(" available units: 'ms', 's', 'min', 'h', 'd' (default: min)\n"));
445+
446+
printf(_("\n Connection options:\n"));
447+
printf(_(" -U, --username=USERNAME user name to connect as (default: current local user)\n"));
448+
printf(_(" -d, --dbname=DBNAME database to connect (default: username)\n"));
449+
printf(_(" -h, --host=HOSTNAME database server host or socket directory(default: 'local socket')\n"));
450+
printf(_(" -p, --port=PORT database server port (default: 5432)\n"));
451+
printf(_(" -w, --no-password never prompt for password\n"));
452+
printf(_(" -W, --password force password prompt\n"));
453+
}
454+
399455
static void
400456
help_show(void)
401457
{

src/pg_probackup.c

+2
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ bool skip_external_dirs = false;
9292

9393
/* checkdb options */
9494
bool do_amcheck = false;
95+
bool heapallindexed = false;
9596

9697
/* delete options */
9798
bool delete_wal = false;
@@ -163,6 +164,7 @@ static ConfigOption cmd_options[] =
163164
{ 'b', 156, "skip-external-dirs", &skip_external_dirs, SOURCE_CMD_STRICT },
164165
/* checkdb options */
165166
{ 'b', 157, "amcheck", &do_amcheck, SOURCE_CMD_STRICT },
167+
{ 'b', 158, "heapallindexed", &heapallindexed, SOURCE_CMD_STRICT },
166168
/* delete options */
167169
{ 'b', 145, "wal", &delete_wal, SOURCE_CMD_STRICT },
168170
{ 'b', 146, "expired", &delete_expired, SOURCE_CMD_STRICT },

src/pg_probackup.h

+3
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,9 @@ extern char *instance_name;
408408
/* show options */
409409
extern ShowFormat show_format;
410410

411+
/* checkdb options */
412+
extern bool heapallindexed;
413+
411414
/* current settings */
412415
extern pgBackup current;
413416

0 commit comments

Comments
 (0)