Skip to content

Commit 2a78020

Browse files
committed
checkdb bugfixes: query to select indexes not from "pg_global" tablespace was incorrect, also index namespace used in "--progress" and WARNING messages was incorrect
1 parent b12b89f commit 2a78020

File tree

1 file changed

+37
-21
lines changed

1 file changed

+37
-21
lines changed

src/checkdb.c

+37-21
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ typedef struct pg_indexEntry
7979
{
8080
Oid indexrelid;
8181
char *name;
82+
char *namespace;
8283
bool heapallindexed_is_supported;
8384
/* schema where amcheck extention is located */
8485
char *amcheck_nspname;
@@ -98,6 +99,8 @@ pg_indexEntry_free(void *index)
9899

99100
if (index_ptr->name)
100101
free(index_ptr->name);
102+
if (index_ptr->name)
103+
free(index_ptr->namespace);
101104
if (index_ptr->amcheck_nspname)
102105
free(index_ptr->amcheck_nspname);
103106

@@ -324,7 +327,7 @@ check_indexes(void *arg)
324327
if (progress)
325328
elog(INFO, "Thread [%d]. Progress: (%d/%d). Amchecking index '%s.%s'",
326329
arguments->thread_num, i + 1, n_indexes,
327-
ind->amcheck_nspname, ind->name);
330+
ind->namespace, ind->name);
328331

329332
if (arguments->conn_arg.conn == NULL)
330333
{
@@ -362,7 +365,7 @@ get_index_list(const char *dbname, bool first_db_with_amcheck,
362365
PGconn *db_conn)
363366
{
364367
PGresult *res;
365-
char *nspname = NULL;
368+
char *amcheck_nspname = NULL;
366369
int i;
367370
bool heapallindexed_is_supported = false;
368371
parray *index_list = NULL;
@@ -391,8 +394,8 @@ get_index_list(const char *dbname, bool first_db_with_amcheck,
391394
return NULL;
392395
}
393396

394-
nspname = pgut_malloc(strlen(PQgetvalue(res, 0, 1)) + 1);
395-
strcpy(nspname, PQgetvalue(res, 0, 1));
397+
amcheck_nspname = pgut_malloc(strlen(PQgetvalue(res, 0, 1)) + 1);
398+
strcpy(amcheck_nspname, PQgetvalue(res, 0, 1));
396399

397400
/* heapallindexed_is_supported is database specific */
398401
if (strcmp(PQgetvalue(res, 0, 2), "1.0") != 0 &&
@@ -419,24 +422,28 @@ get_index_list(const char *dbname, bool first_db_with_amcheck,
419422
if (first_db_with_amcheck)
420423
{
421424

422-
res = pgut_execute(db_conn, "SELECT cls.oid, cls.relname "
423-
"FROM pg_index idx "
424-
"JOIN pg_class cls ON idx.indexrelid=cls.oid "
425-
"JOIN pg_am am ON cls.relam=am.oid "
426-
"WHERE am.amname='btree' AND cls.relpersistence != 't'",
425+
res = pgut_execute(db_conn, "SELECT cls.oid, cls.relname, nmspc.nspname "
426+
"FROM pg_catalog.pg_index idx "
427+
"LEFT JOIN pg_catalog.pg_class cls ON idx.indexrelid=cls.oid "
428+
"LEFT JOIN pg_catalog.pg_namespace nmspc ON cls.relnamespace=nmspc.oid "
429+
"LEFT JOIN pg_catalog.pg_am am ON cls.relam=am.oid "
430+
"WHERE am.amname='btree' AND cls.relpersistence != 't' "
431+
"ORDER BY nmspc.nspname DESC",
427432
0, NULL);
428433
}
429434
else
430435
{
431436

432-
res = pgut_execute(db_conn, "SELECT cls.oid, cls.relname "
433-
"FROM pg_index idx "
434-
"JOIN pg_class cls ON idx.indexrelid=cls.oid "
435-
"JOIN pg_am am ON cls.relam=am.oid "
436-
"LEFT JOIN pg_tablespace tbl "
437-
"ON cls.reltablespace=tbl.oid "
438-
"AND tbl.spcname <> 'pg_global' "
439-
"WHERE am.amname='btree' AND cls.relpersistence != 't'",
437+
res = pgut_execute(db_conn, "SELECT cls.oid, cls.relname, nmspc.nspname "
438+
"FROM pg_catalog.pg_index idx "
439+
"LEFT JOIN pg_catalog.pg_class cls ON idx.indexrelid=cls.oid "
440+
"LEFT JOIN pg_catalog.pg_namespace nmspc ON cls.relnamespace=nmspc.oid "
441+
"LEFT JOIN pg_catalog.pg_am am ON cls.relam=am.oid "
442+
"WHERE am.amname='btree' AND cls.relpersistence != 't' AND "
443+
"(cls.reltablespace IN "
444+
"(SELECT oid from pg_catalog.pg_tablespace where spcname <> 'pg_global') "
445+
"OR cls.reltablespace = 0) "
446+
"ORDER BY nmspc.nspname DESC",
440447
0, NULL);
441448
}
442449

@@ -445,15 +452,24 @@ get_index_list(const char *dbname, bool first_db_with_amcheck,
445452
{
446453
pg_indexEntry *ind = (pg_indexEntry *) pgut_malloc(sizeof(pg_indexEntry));
447454
char *name = NULL;
455+
char *namespace = NULL;
448456

457+
/* index oid */
449458
ind->indexrelid = atoi(PQgetvalue(res, i, 0));
459+
460+
/* index relname */
450461
name = PQgetvalue(res, i, 1);
451462
ind->name = pgut_malloc(strlen(name) + 1);
452463
strcpy(ind->name, name); /* enough buffer size guaranteed */
453464

465+
/* index namespace */
466+
namespace = PQgetvalue(res, i, 2);
467+
ind->namespace = pgut_malloc(strlen(namespace) + 1);
468+
strcpy(ind->namespace, namespace); /* enough buffer size guaranteed */
469+
454470
ind->heapallindexed_is_supported = heapallindexed_is_supported;
455-
ind->amcheck_nspname = pgut_malloc(strlen(nspname) + 1);
456-
strcpy(ind->amcheck_nspname, nspname);
471+
ind->amcheck_nspname = pgut_malloc(strlen(amcheck_nspname) + 1);
472+
strcpy(ind->amcheck_nspname, amcheck_nspname);
457473
pg_atomic_clear_flag(&ind->lock);
458474

459475
if (index_list == NULL)
@@ -509,7 +525,7 @@ amcheck_one_index(check_indexes_arg *arguments,
509525
{
510526
elog(WARNING, "Thread [%d]. Amcheck failed in database '%s' for index: '%s.%s': %s",
511527
arguments->thread_num, arguments->conn_opt.pgdatabase,
512-
ind->amcheck_nspname, ind->name, PQresultErrorMessage(res));
528+
ind->namespace, ind->name, PQresultErrorMessage(res));
513529

514530
pfree(params[0]);
515531
pfree(query);
@@ -519,7 +535,7 @@ amcheck_one_index(check_indexes_arg *arguments,
519535
else
520536
elog(LOG, "Thread [%d]. Amcheck succeeded in database '%s' for index: '%s.%s'",
521537
arguments->thread_num,
522-
arguments->conn_opt.pgdatabase, ind->amcheck_nspname, ind->name);
538+
arguments->conn_opt.pgdatabase, ind->namespace, ind->name);
523539

524540
pfree(params[0]);
525541
pfree(query);

0 commit comments

Comments
 (0)