Skip to content

Commit cd0b4eb

Browse files
committed
Improved error handling for repack_table_indexes()
If we do not successfully build any indexes on the target table, bail out early instead of acquiring an ACCESS EXCLUSIVE lock on the table.
1 parent fa9af9d commit cd0b4eb

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

bin/pg_repack.c

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1617,7 +1617,7 @@ repack_table_indexes(PGresult *index_details)
16171617
char buffer[2][12];
16181618
const char *create_idx, *schema_name, *table_name, *params[3];
16191619
Oid table, index;
1620-
int i, num;
1620+
int i, num, num_repacked = 0;
16211621
bool *repacked_indexes;
16221622

16231623
num = PQntuples(index_details);
@@ -1679,7 +1679,10 @@ repack_table_indexes(PGresult *index_details)
16791679
schema_name, index)));
16801680
}
16811681
else
1682+
{
16821683
repacked_indexes[i] = true;
1684+
num_repacked++;
1685+
}
16831686

16841687
CLEARPGRES(res);
16851688
CLEARPGRES(res2);
@@ -1689,7 +1692,21 @@ repack_table_indexes(PGresult *index_details)
16891692
getstr(index_details, i, 0));
16901693
}
16911694

1692-
/* take exclusive lock on table before calling repack_index_swap() */
1695+
/* If we did not successfully repack any indexes, e.g. because of some
1696+
* error affecting every CREATE INDEX attempt, don't waste time with
1697+
* the ACCESS EXCLUSIVE lock on the table, and return false.
1698+
* N.B. none of the DROP INDEXes should be performed since
1699+
* repacked_indexes[] flags should all be false.
1700+
*/
1701+
if (!num_repacked)
1702+
{
1703+
elog(WARNING,
1704+
"Skipping index swapping for \"%s\", since no new indexes built",
1705+
table_name);
1706+
goto drop_idx;
1707+
}
1708+
1709+
/* take an exclusive lock on table before calling repack_index_swap() */
16931710
initStringInfo(&sql);
16941711
appendStringInfo(&sql, "LOCK TABLE %s IN ACCESS EXCLUSIVE MODE",
16951712
table_name);

0 commit comments

Comments
 (0)