@@ -1617,7 +1617,7 @@ repack_table_indexes(PGresult *index_details)
1617
1617
char buffer [2 ][12 ];
1618
1618
const char * create_idx , * schema_name , * table_name , * params [3 ];
1619
1619
Oid table , index ;
1620
- int i , num ;
1620
+ int i , num , num_repacked = 0 ;
1621
1621
bool * repacked_indexes ;
1622
1622
1623
1623
num = PQntuples (index_details );
@@ -1679,7 +1679,10 @@ repack_table_indexes(PGresult *index_details)
1679
1679
schema_name , index )));
1680
1680
}
1681
1681
else
1682
+ {
1682
1683
repacked_indexes [i ] = true;
1684
+ num_repacked ++ ;
1685
+ }
1683
1686
1684
1687
CLEARPGRES (res );
1685
1688
CLEARPGRES (res2 );
@@ -1689,7 +1692,21 @@ repack_table_indexes(PGresult *index_details)
1689
1692
getstr (index_details , i , 0 ));
1690
1693
}
1691
1694
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() */
1693
1710
initStringInfo (& sql );
1694
1711
appendStringInfo (& sql , "LOCK TABLE %s IN ACCESS EXCLUSIVE MODE" ,
1695
1712
table_name );
0 commit comments