Skip to content

Commit b99097a

Browse files
committed
pgpro-2065. fix cycle in do_amcheck, remove unused variables and debug messages. TODO: code cleanup, fix exit statuses for threads
1 parent ac2236c commit b99097a

File tree

1 file changed

+23
-36
lines changed

1 file changed

+23
-36
lines changed

src/backup.c

+23-36
Original file line numberDiff line numberDiff line change
@@ -981,10 +981,9 @@ do_amcheck(void)
981981
pthread_t *threads;
982982
backup_files_arg *threads_args;
983983
bool backup_isok = true;
984-
const char *dbname;
985984
PGresult *res_db;
986985
int n_databases = 0;
987-
bool first_db_with_amcheck = false;
986+
bool first_db_with_amcheck = true;
988987
PGconn *db_conn = NULL;
989988

990989
pgBackupGetPath(&current, database_path, lengthof(database_path),
@@ -993,7 +992,6 @@ do_amcheck(void)
993992
res_db = pgut_execute(backup_conn, "SELECT datname, oid, dattablespace FROM pg_database",
994993
0, NULL);
995994
n_databases = PQntuples(res_db);
996-
elog(WARNING, "number of databases found: %d", n_databases);
997995

998996
/* TODO Warn user that one connection is used for snaphot */
999997
if (num_threads > 1)
@@ -1002,10 +1000,14 @@ do_amcheck(void)
10021000
/* For each database check indexes. In parallel. */
10031001
for(i = 0; i < n_databases; i++)
10041002
{
1003+
int j;
1004+
10051005
if (index_list != NULL)
1006+
{
10061007
free(index_list);
1008+
index_list = NULL;
1009+
}
10071010

1008-
elog(WARNING, "get index list for db %d of %d", i, n_databases);
10091011
index_list = get_index_list(res_db, i,
10101012
&first_db_with_amcheck, db_conn);
10111013
if (index_list == NULL)
@@ -1019,9 +1021,9 @@ do_amcheck(void)
10191021
threads = (pthread_t *) palloc(sizeof(pthread_t) * num_threads);
10201022
threads_args = (backup_files_arg *) palloc(sizeof(backup_files_arg)*num_threads);
10211023

1022-
for (i = 0; i < num_threads; i++)
1024+
for (j = 0; j < num_threads; j++)
10231025
{
1024-
backup_files_arg *arg = &(threads_args[i]);
1026+
backup_files_arg *arg = &(threads_args[j]);
10251027

10261028
arg->from_root = instance_config.pgdata;
10271029
arg->to_root = database_path;
@@ -1037,32 +1039,31 @@ do_amcheck(void)
10371039

10381040
pgut_atexit_push(threads_conn_disconnect, NULL);
10391041

1040-
/* TODO write better info message */
1041-
elog(INFO, "Start checking data files");
1042+
elog(INFO, "Start checking indexes with amcheck");
10421043

10431044
/* Run threads */
1044-
for (i = 0; i < num_threads; i++)
1045+
for (j = 0; j < num_threads; j++)
10451046
{
1046-
backup_files_arg *arg = &(threads_args[i]);
1047+
backup_files_arg *arg = &(threads_args[j]);
10471048

1048-
elog(VERBOSE, "Start thread num: %i", i);
1049+
elog(VERBOSE, "Start thread num: %i", j);
10491050

1050-
pthread_create(&threads[i], NULL, check_indexes, arg);
1051+
pthread_create(&threads[j], NULL, check_indexes, arg);
10511052
}
10521053

10531054
/* Wait threads */
1054-
for (i = 0; i < num_threads; i++)
1055+
for (j = 0; j < num_threads; j++)
10551056
{
1056-
pthread_join(threads[i], NULL);
1057-
if (threads_args[i].ret == 1)
1057+
pthread_join(threads[j], NULL);
1058+
if (threads_args[j].ret == 1)
10581059
backup_isok = false;
10591060
}
10601061
pgut_disconnect(db_conn);
10611062
}
10621063

10631064
/* TODO write better info message */
10641065
if (backup_isok)
1065-
elog(INFO, "Indexes are checked");
1066+
elog(INFO, "Indexes are checked");
10661067
else
10671068
elog(ERROR, "Indexs checking failed");
10681069

@@ -2476,7 +2477,6 @@ check_files(void *arg)
24762477
int i;
24772478
backup_files_arg *arguments = (backup_files_arg *) arg;
24782479
int n_backup_files_list = 0;
2479-
char dbname[NAMEDATALEN];
24802480

24812481
if (arguments->files_list)
24822482
n_backup_files_list = parray_num(arguments->files_list);
@@ -2536,7 +2536,8 @@ check_files(void *arg)
25362536
join_path_components(to_path, arguments->to_root,
25372537
file->path + strlen(arguments->from_root) + 1);
25382538

2539-
check_data_file(arguments, file);
2539+
if (!check_data_file(arguments, file))
2540+
arguments->ret = 1;
25402541
}
25412542
}
25422543
else
@@ -2565,7 +2566,6 @@ check_indexes(void *arg)
25652566
if (arguments->index_list)
25662567
n_indexes = parray_num(arguments->index_list);
25672568

2568-
elog(WARNING, "n_indexes %d", n_indexes);
25692569
for (i = 0; i < n_indexes; i++)
25702570
{
25712571
pg_indexEntry *ind = (pg_indexEntry *) parray_get(arguments->index_list, i);
@@ -2606,14 +2606,15 @@ check_indexes(void *arg)
26062606
free(query);
26072607
}
26082608

2609-
amcheck_one_index(arguments, ind);
2609+
/* remember that we have a failed check */
2610+
if (!amcheck_one_index(arguments, ind))
2611+
arguments->ret = 1;
26102612
}
26112613

26122614
/* Close connection */
26132615
if (arguments->backup_conn)
26142616
pgut_disconnect(arguments->backup_conn);
26152617

2616-
/* Data files transferring is successful */
26172618
/* TODO where should we set arguments->ret to 1? */
26182619
arguments->ret = 0;
26192620

@@ -3324,19 +3325,12 @@ get_index_list(PGresult *res_db, int db_number,
33243325
PGresult *res;
33253326
char *nspname = NULL;
33263327
char *snapshot = NULL;
3327-
Oid dbOid, tblspcOid;
3328-
Oid indexrelid;
33293328
int i;
33303329

33313330
dbname = PQgetvalue(res_db, db_number, 0);
33323331
if (strcmp(dbname, "template0") == 0)
33333332
return NULL;
33343333

3335-
elog(WARNING, "get_index_list db_number %d dbname %s", db_number, dbname);
3336-
3337-
dbOid = atoi(PQgetvalue(res_db, db_number, 1));
3338-
tblspcOid = atoi(PQgetvalue(res_db, db_number, 2));
3339-
33403334
db_conn = pgut_connect(instance_config.pghost, instance_config.pgport,
33413335
dbname,
33423336
instance_config.pguser);
@@ -3360,15 +3354,13 @@ get_index_list(PGresult *res_db, int db_number,
33603354

33613355
nspname = pgut_malloc(strlen(PQgetvalue(res, 0, 1)) + 1);
33623356
strcpy(nspname, PQgetvalue(res, 0, 1));
3363-
elog(WARNING, "index_list for db %s nspname %s", dbname, nspname);
33643357

33653358
/*
33663359
* In order to avoid duplicates, select global indexes
33673360
* (tablespace pg_global with oid 1664) only once
33683361
*/
33693362
if (*first_db_with_amcheck)
33703363
{
3371-
elog(WARNING, "FIRST AMCHECK for db %s nspname %s", dbname, nspname);
33723364
res = pgut_execute(db_conn, "BEGIN TRANSACTION ISOLATION LEVEL REPEATABLE READ;", 0, NULL);
33733365
PQclear(res);
33743366

@@ -3380,7 +3372,6 @@ get_index_list(PGresult *res_db, int db_number,
33803372
snapshot = pgut_malloc(strlen(PQgetvalue(res, 0, 0)) + 1);
33813373
strcpy(snapshot, PQgetvalue(res, 0, 0));
33823374

3383-
elog(WARNING, "exported snapshot '%s'", snapshot);
33843375
PQclear(res);
33853376

33863377
res = pgut_execute(db_conn, "SELECT cls.oid, cls.relname"
@@ -3393,8 +3384,6 @@ get_index_list(PGresult *res_db, int db_number,
33933384
}
33943385
else
33953386
{
3396-
elog(WARNING, "NOT FIRST AMCHECK for db %s nspname %s", dbname, nspname);
3397-
33983387
res = pgut_execute(db_conn, "BEGIN TRANSACTION ISOLATION LEVEL REPEATABLE READ;", 0, NULL);
33993388
PQclear(res);
34003389

@@ -3408,7 +3397,6 @@ get_index_list(PGresult *res_db, int db_number,
34083397
snapshot = pgut_malloc(strlen(PQgetvalue(res, 0, 0)) + 1);
34093398
strcpy(snapshot, PQgetvalue(res, 0, 0));
34103399

3411-
elog(WARNING, "exported snapshot '%s'", snapshot);
34123400
PQclear(res);
34133401

34143402
res = pgut_execute(db_conn, "SELECT cls.oid, cls.relname"
@@ -3459,13 +3447,12 @@ amcheck_one_index(backup_files_arg *arguments,
34593447
{
34603448
PGresult *res;
34613449
char *params[1];
3462-
char *result;
34633450
char *query;
34643451
params[0] = palloc(64);
34653452

34663453
sprintf(params[0], "%i", ind->indexrelid);
34673454

3468-
// elog(WARNING, "amcheck_one_index %s", ind->name);
3455+
elog(VERBOSE, "amcheck index: '%s'", ind->name);
34693456

34703457
query = palloc(strlen(ind->amcheck_nspname)+strlen("SELECT .bt_index_check($1)")+1);
34713458
sprintf(query, "SELECT %s.bt_index_check($1)", ind->amcheck_nspname);

0 commit comments

Comments
 (0)