Skip to content

Commit 3f7a051

Browse files
committed
Improved error message distinction for repack_table_indexes().
Previously, any error creating the temp index was blamed on a previous temporary index existing. However, other errors could occur, e.g. if --tablespace=pg_global was specified. Perform an explicit check for whether an existing index_xxxxx already exists first, so that we can give that error message only in the appropriate case.
1 parent cd0b4eb commit 3f7a051

File tree

1 file changed

+37
-12
lines changed

1 file changed

+37
-12
lines changed

bin/pg_repack.c

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1620,6 +1620,8 @@ repack_table_indexes(PGresult *index_details)
16201620
int i, num, num_repacked = 0;
16211621
bool *repacked_indexes;
16221622

1623+
initStringInfo(&sql);
1624+
16231625
num = PQntuples(index_details);
16241626
table = getoid(index_details, 0, 3);
16251627
params[1] = utoa(table, buffer[1]);
@@ -1648,8 +1650,36 @@ repack_table_indexes(PGresult *index_details)
16481650
if (isvalid[0] == 't')
16491651
{
16501652
index = getoid(index_details, i, 1);
1651-
params[0] = utoa(index, buffer[0]);
16521653

1654+
resetStringInfo(&sql);
1655+
appendStringInfo(&sql, "SELECT pgc.relname, nsp.nspname "
1656+
"FROM pg_class pgc INNER JOIN pg_namespace nsp "
1657+
"ON nsp.oid = pgc.relnamespace "
1658+
"WHERE pgc.relname = 'index_%u' "
1659+
"AND nsp.nspname = $1", index);
1660+
params[0] = schema_name;
1661+
res = execute(sql.data, 1, params);
1662+
if (PQresultStatus(res) != PGRES_TUPLES_OK)
1663+
{
1664+
elog(WARNING, "%s", PQerrorMessage(connection));
1665+
continue;
1666+
}
1667+
if (PQntuples(res) > 0)
1668+
{
1669+
ereport(WARNING,
1670+
(errcode(E_PG_COMMAND),
1671+
errmsg("Cannot create index \"%s\".\"index_%u\", "
1672+
"already exists", schema_name, index),
1673+
errdetail("An invalid index may have been left behind"
1674+
" by a previous pg_repack on the table"
1675+
" which was interrupted. Please use DROP "
1676+
"INDEX \"%s\".\"index_%u\""
1677+
" to remove this index and try again.",
1678+
schema_name, index)));
1679+
continue;
1680+
}
1681+
1682+
params[0] = utoa(index, buffer[0]);
16531683
res = execute("SELECT repack.repack_indexdef($1, $2, $3, true)", 3,
16541684
params);
16551685

@@ -1669,14 +1699,9 @@ repack_table_indexes(PGresult *index_details)
16691699
{
16701700
ereport(WARNING,
16711701
(errcode(E_PG_COMMAND),
1672-
errmsg("Error creating index: %s",
1673-
PQerrorMessage(connection)),
1674-
errdetail("An invalid index may have been left behind"
1675-
" by a previous pg_repack on the table"
1676-
" which was interrupted. Please use DROP "
1677-
"INDEX \"%s\".\"index_%u\""
1678-
" to remove this index and try again.",
1679-
schema_name, index)));
1702+
errmsg("Error creating index \"%s\".\"index_%u\": %s",
1703+
schema_name, index, PQerrorMessage(connection)
1704+
) ));
16801705
}
16811706
else
16821707
{
@@ -1707,7 +1732,7 @@ repack_table_indexes(PGresult *index_details)
17071732
}
17081733

17091734
/* take an exclusive lock on table before calling repack_index_swap() */
1710-
initStringInfo(&sql);
1735+
resetStringInfo(&sql);
17111736
appendStringInfo(&sql, "LOCK TABLE %s IN ACCESS EXCLUSIVE MODE",
17121737
table_name);
17131738
if (!(lock_exclusive(connection, params[1], sql.data, TRUE)))
@@ -1734,7 +1759,7 @@ repack_table_indexes(PGresult *index_details)
17341759

17351760
drop_idx:
17361761
CLEARPGRES(res);
1737-
initStringInfo(&sql);
1762+
resetStringInfo(&sql);
17381763
initStringInfo(&sql_drop);
17391764
#if PG_VERSION_NUM < 90200
17401765
appendStringInfoString(&sql, "DROP INDEX ");
@@ -1790,7 +1815,7 @@ repack_all_indexes(char *errbuf, size_t errsize)
17901815

17911816
cell = r_index.head;
17921817
}
1793-
else if(table_list.head)
1818+
else if (table_list.head)
17941819
{
17951820
appendStringInfoString(&sql,
17961821
"SELECT i.relname, idx.indexrelid, idx.indisvalid, idx.indrelid, $1::text, n.nspname"

0 commit comments

Comments
 (0)