@@ -257,7 +257,7 @@ main(int argc, char *argv[])
257
257
errmsg ("cannot specify --index (-i) and --table (-t)" )));
258
258
else if (r_index && only_indexes )
259
259
ereport (ERROR , (errcode (EINVAL ),
260
- errmsg ("cannot specify --index (-i) and --indexes_only (-x)" )));
260
+ errmsg ("cannot specify --index (-i) and --only-index (-x)" )));
261
261
else if (only_indexes && !table_list .head )
262
262
ereport (ERROR , (errcode (EINVAL ),
263
263
errmsg ("cannot repack all indexes of database, specify the table with -t option" )));
@@ -969,7 +969,7 @@ repack_one_table(const repack_table *table, const char *orderby)
969
969
970
970
/* Keep track of whether we have gotten through setup to install
971
971
* the z_repack_trigger, log table, etc. ourselves. We don't want to
972
- * go through repack_cleanup() if we didnt' actually set up the
972
+ * go through repack_cleanup() if we didn't actually set up the
973
973
* trigger ourselves, lest we be cleaning up another pg_repack's mess,
974
974
* or worse, interfering with a still-running pg_repack.
975
975
*/
@@ -1012,7 +1012,7 @@ repack_one_table(const repack_table *table, const char *orderby)
1012
1012
}
1013
1013
1014
1014
/*
1015
- * Check z_repack_trigger is the trigger executed at last so that
1015
+ * Check z_repack_trigger is the trigger executed last so that
1016
1016
* other before triggers cannot modify triggered tuples.
1017
1017
*/
1018
1018
params [0 ] = utoa (table -> target_oid , buffer );
@@ -1577,11 +1577,12 @@ repack_cleanup(bool fatal, const repack_table *table)
1577
1577
/*
1578
1578
* repack one index
1579
1579
*/
1580
- static bool
1581
- repack_one_index (Oid table , const char * table_name , Oid index , const char * schema_name ){
1580
+ static bool
1581
+ repack_one_index (Oid table , const char * table_name , Oid index , const char * schema_name )
1582
+ {
1582
1583
bool ret = false;
1583
1584
PGresult * res = NULL ;
1584
- StringInfoData sql , temp_index ;
1585
+ StringInfoData sql , temp_index ;
1585
1586
char buffer [2 ][12 ];
1586
1587
char * create_idx ;
1587
1588
const char * params [3 ];
@@ -1593,13 +1594,13 @@ repack_one_index(Oid table, const char *table_name, Oid index, const char *schem
1593
1594
if (PQntuples (res ) < 1 )
1594
1595
{
1595
1596
ereport (ERROR , (errcode (EINVAL ),
1596
- errmsg ("unable to generate SQL to CREATE new index" )));
1597
+ errmsg ("unable to generate SQL to CREATE new index" )));
1597
1598
goto cleanup ;
1598
1599
}
1599
1600
create_idx = getstr (res , 0 , 0 );
1600
1601
CLEARPGRES (res );
1601
1602
res = execute_elevel (create_idx , 0 , NULL , DEBUG2 );
1602
-
1603
+
1603
1604
initStringInfo (& temp_index );
1604
1605
if (schema_name )
1605
1606
appendStringInfo (& temp_index , "%s.index_%u" , schema_name , index );
@@ -1611,25 +1612,27 @@ repack_one_index(Oid table, const char *table_name, Oid index, const char *schem
1611
1612
ereport (ERROR ,
1612
1613
(errcode (E_PG_COMMAND ),
1613
1614
errmsg ("%s" , PQerrorMessage (connection )),
1614
- errdetail ("The temporary index may be left behind "
1615
- " by a pg_repack command on the table which"
1616
- " was interrupted and failed to clean up"
1617
- " the temporary objects. Please use the \"DROP INDEX %s\""
1618
- " to remove the temporary index." , temp_index .data )));
1615
+ errdetail ("An invalid index may have been left behind "
1616
+ " by a pg_repack command on the table which"
1617
+ " was interrupted and failed to clean up"
1618
+ " the temporary objects. Please use \"DROP INDEX %s\""
1619
+ " to remove this index." , temp_index .data )));
1619
1620
goto cleanup ;
1620
1621
}
1621
1622
CLEARPGRES (res );
1622
1623
1623
1624
/* take exclusive lock on table before calling repack_index_swap() */
1624
1625
initStringInfo (& sql );
1625
1626
if (schema_name )
1626
- appendStringInfo (& sql , "LOCK TABLE %s.%s IN ACCESS EXCLUSIVE MODE" , schema_name , table_name );
1627
+ appendStringInfo (& sql , "LOCK TABLE %s.%s IN ACCESS EXCLUSIVE MODE" ,
1628
+ schema_name , table_name );
1627
1629
else
1628
- appendStringInfo (& sql , "LOCK TABLE %s IN ACCESS EXCLUSIVE MODE" , table_name );
1630
+ appendStringInfo (& sql , "LOCK TABLE %s IN ACCESS EXCLUSIVE MODE" ,
1631
+ table_name );
1629
1632
if (!(lock_exclusive (connection , params [1 ], sql .data , TRUE)))
1630
1633
{
1631
1634
elog (WARNING , "lock_exclusive() failed in connection for %s" ,
1632
- table_name );
1635
+ table_name );
1633
1636
goto drop_idx ;
1634
1637
}
1635
1638
pgut_command (connection , "SELECT repack.repack_index_swap($1)" , 1 , params );
@@ -1645,6 +1648,7 @@ repack_one_index(Oid table, const char *table_name, Oid index, const char *schem
1645
1648
appendStringInfo (& sql , "%s" , temp_index .data );
1646
1649
command (sql .data , 0 , NULL );
1647
1650
ret = true;
1651
+
1648
1652
cleanup :
1649
1653
CLEARPGRES (res );
1650
1654
termStringInfo (& sql );
@@ -1654,25 +1658,27 @@ repack_one_index(Oid table, const char *table_name, Oid index, const char *schem
1654
1658
/*
1655
1659
* Call repack_one_index for each of the indexes
1656
1660
*/
1657
- static bool
1661
+ static bool
1658
1662
repack_all_indexes (char * errbuf , size_t errsize ){
1659
1663
bool ret = false;
1660
- PGresult * res = NULL , * res2 = NULL ;
1661
- int i ;
1662
- int num ;
1663
- StringInfoData sql ;
1664
+ PGresult * res = NULL , * res2 = NULL ;
1665
+ int i ;
1666
+ int num ;
1667
+ StringInfoData sql ;
1664
1668
const char * params [1 ];
1665
1669
const char * table_name = NULL ;
1666
1670
const char * schema_name = NULL ;
1667
- char * pos ;
1671
+ char * pos ;
1668
1672
1669
1673
initStringInfo (& sql );
1670
1674
reconnect (ERROR );
1671
1675
1672
1676
if (!preliminary_checks (errbuf , errsize ))
1673
- goto cleanup ;
1677
+ goto cleanup ;
1674
1678
1675
- /* If only one index is specified, append the appropriate data to the sql and check if the index exists */
1679
+ /* If only one index is specified, append the appropriate data to the sql
1680
+ * and check if the index exists
1681
+ */
1676
1682
if (r_index )
1677
1683
{
1678
1684
appendStringInfoString (& sql , "SELECT i.relname, idx.indexrelid, idx.indisvalid, tbl.oid, tbl.relname"
@@ -1699,7 +1705,7 @@ repack_all_indexes(char *errbuf, size_t errsize){
1699
1705
goto cleanup ;
1700
1706
}
1701
1707
}
1702
-
1708
+
1703
1709
// seperate schema name and index name
1704
1710
pos = strchr (params [0 ], '.' );
1705
1711
if (pos )
@@ -1709,11 +1715,11 @@ repack_all_indexes(char *errbuf, size_t errsize){
1709
1715
r_index = pos + 1 ;
1710
1716
}
1711
1717
table_name = getstr (res , 0 , 4 );
1712
- }
1718
+ }
1713
1719
/* To repack all indexes, append appropriate data to the sql and run the query */
1714
1720
else {
1715
1721
params [0 ] = table_list .head -> val ;
1716
-
1722
+
1717
1723
appendStringInfoString (& sql , "SELECT i.relname, idx.indexrelid, idx.indisvalid, idx.indrelid"
1718
1724
" FROM pg_index idx JOIN pg_class i ON i.oid = idx.indexrelid"
1719
1725
" WHERE idx.indrelid = $1::regclass" );
@@ -1725,12 +1731,12 @@ repack_all_indexes(char *errbuf, size_t errsize){
1725
1731
snprintf (errbuf , errsize , "%s" , PQerrorMessage (connection ));
1726
1732
goto cleanup ;
1727
1733
}
1728
- else
1734
+ else
1729
1735
{
1730
1736
num = PQntuples (res );
1731
1737
if (num == 0 )
1732
1738
{
1733
- elog (WARNING , "\"%s\" doesnot have any indexes" , table_list .head -> val );
1739
+ elog (WARNING , "\"%s\" does not have any indexes" , table_list .head -> val );
1734
1740
ret = true;
1735
1741
goto cleanup ;
1736
1742
}
@@ -1748,6 +1754,14 @@ repack_all_indexes(char *errbuf, size_t errsize){
1748
1754
table_name = params [0 ];
1749
1755
}
1750
1756
1757
+ /* XXX: Make sure that repack_one_table() also obtains an advisory
1758
+ * lock on the table, so that we can't have a table-wide repack running
1759
+ * along with an indexes-only repack. Also, since advisory locks are
1760
+ * 8 bytes wide and OIDs are only 4 bytes, consider using our own prefix
1761
+ * rather than just the table OID, to avoid inadvertent conflict with
1762
+ * other applications using advisory locks.
1763
+ */
1764
+
1751
1765
/* Check if any concurrent pg_repack command is being run on the same table */
1752
1766
initStringInfo (& sql );
1753
1767
appendStringInfo (& sql , "SELECT pg_try_advisory_lock(%u)" , getoid (res , 0 , 3 ));
@@ -1767,7 +1781,7 @@ repack_all_indexes(char *errbuf, size_t errsize){
1767
1781
for (i = 0 ; i < num ; i ++ )
1768
1782
{
1769
1783
char * isvalid = getstr (res , i , 2 );
1770
- if (isvalid [0 ] == 't' )
1784
+ if (isvalid [0 ] == 't' )
1771
1785
{
1772
1786
if (schema_name )
1773
1787
elog (INFO , "repacking index \"%s.%s\"" , schema_name , getstr (res , i , 0 ));
@@ -1779,9 +1793,9 @@ repack_all_indexes(char *errbuf, size_t errsize){
1779
1793
}
1780
1794
else
1781
1795
if (schema_name )
1782
- elog (WARNING , "skipping invalid index: %s.%s" , schema_name , getstr (res , i , 0 ));
1796
+ elog (WARNING , "skipping invalid index: %s.%s" , schema_name , getstr (res , i , 0 ));
1783
1797
else
1784
- elog (WARNING , "skipping invalid index: %s" , getstr (res , i , 0 ));
1798
+ elog (WARNING , "skipping invalid index: %s" , getstr (res , i , 0 ));
1785
1799
}
1786
1800
ret = true;
1787
1801
cleanup :
0 commit comments