@@ -1620,6 +1620,8 @@ repack_table_indexes(PGresult *index_details)
1620
1620
int i , num , num_repacked = 0 ;
1621
1621
bool * repacked_indexes ;
1622
1622
1623
+ initStringInfo (& sql );
1624
+
1623
1625
num = PQntuples (index_details );
1624
1626
table = getoid (index_details , 0 , 3 );
1625
1627
params [1 ] = utoa (table , buffer [1 ]);
@@ -1648,8 +1650,36 @@ repack_table_indexes(PGresult *index_details)
1648
1650
if (isvalid [0 ] == 't' )
1649
1651
{
1650
1652
index = getoid (index_details , i , 1 );
1651
- params [0 ] = utoa (index , buffer [0 ]);
1652
1653
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 ]);
1653
1683
res = execute ("SELECT repack.repack_indexdef($1, $2, $3, true)" , 3 ,
1654
1684
params );
1655
1685
@@ -1669,14 +1699,9 @@ repack_table_indexes(PGresult *index_details)
1669
1699
{
1670
1700
ereport (WARNING ,
1671
1701
(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
+ ) ));
1680
1705
}
1681
1706
else
1682
1707
{
@@ -1707,7 +1732,7 @@ repack_table_indexes(PGresult *index_details)
1707
1732
}
1708
1733
1709
1734
/* take an exclusive lock on table before calling repack_index_swap() */
1710
- initStringInfo (& sql );
1735
+ resetStringInfo (& sql );
1711
1736
appendStringInfo (& sql , "LOCK TABLE %s IN ACCESS EXCLUSIVE MODE" ,
1712
1737
table_name );
1713
1738
if (!(lock_exclusive (connection , params [1 ], sql .data , TRUE)))
@@ -1734,7 +1759,7 @@ repack_table_indexes(PGresult *index_details)
1734
1759
1735
1760
drop_idx :
1736
1761
CLEARPGRES (res );
1737
- initStringInfo (& sql );
1762
+ resetStringInfo (& sql );
1738
1763
initStringInfo (& sql_drop );
1739
1764
#if PG_VERSION_NUM < 90200
1740
1765
appendStringInfoString (& sql , "DROP INDEX " );
@@ -1790,7 +1815,7 @@ repack_all_indexes(char *errbuf, size_t errsize)
1790
1815
1791
1816
cell = r_index .head ;
1792
1817
}
1793
- else if (table_list .head )
1818
+ else if (table_list .head )
1794
1819
{
1795
1820
appendStringInfoString (& sql ,
1796
1821
"SELECT i.relname, idx.indexrelid, idx.indisvalid, idx.indrelid, $1::text, n.nspname"
0 commit comments