@@ -227,7 +227,7 @@ static bool rebuild_indexes(const repack_table *table);
227
227
static char * getstr (PGresult * res , int row , int col );
228
228
static Oid getoid (PGresult * res , int row , int col );
229
229
static bool advisory_lock (PGconn * conn , const char * relid );
230
- static bool lock_exclusive (PGconn * conn , const char * relid , const char * lock_query , bool start_xact );
230
+ static bool lock_exclusive (PGconn * conn , const char * relid , const char * lock_query , bool start_xact , const repack_table * apply_log_table );
231
231
static bool kill_ddl (PGconn * conn , Oid relid , bool terminate );
232
232
static bool lock_access_share (PGconn * conn , Oid relid , const char * target_name );
233
233
@@ -1298,7 +1298,7 @@ repack_one_table(repack_table *table, const char *orderby)
1298
1298
if (!advisory_lock (connection , buffer ))
1299
1299
goto cleanup ;
1300
1300
1301
- if (!(lock_exclusive (connection , buffer , table -> lock_table , true)))
1301
+ if (!(lock_exclusive (connection , buffer , table -> lock_table , true, NULL )))
1302
1302
{
1303
1303
if (no_kill_backend )
1304
1304
elog (INFO , "Skipping repack %s due to timeout" , table -> target_name );
@@ -1621,7 +1621,7 @@ repack_one_table(repack_table *table, const char *orderby)
1621
1621
/* Bump our existing AccessShare lock to AccessExclusive */
1622
1622
1623
1623
if (!(lock_exclusive (conn2 , utoa (table -> target_oid , buffer ),
1624
- table -> lock_table , false)))
1624
+ table -> lock_table , false, table )))
1625
1625
{
1626
1626
elog (WARNING , "lock_exclusive() failed in conn2 for %s" ,
1627
1627
table -> target_name );
@@ -1654,7 +1654,7 @@ repack_one_table(repack_table *table, const char *orderby)
1654
1654
1655
1655
command ("BEGIN ISOLATION LEVEL READ COMMITTED" , 0 , NULL );
1656
1656
if (!(lock_exclusive (connection , utoa (table -> target_oid , buffer ),
1657
- table -> lock_table , false)))
1657
+ table -> lock_table , false, NULL )))
1658
1658
{
1659
1659
elog (WARNING , "lock_exclusive() failed in connection for %s" ,
1660
1660
table -> target_name );
@@ -1908,9 +1908,14 @@ static bool advisory_lock(PGconn *conn, const char *relid)
1908
1908
* start_xact: whether we will issue a BEGIN ourselves. If not, we will
1909
1909
* use a SAVEPOINT and ROLLBACK TO SAVEPOINT if our query
1910
1910
* times out, to avoid leaving the transaction in error state.
1911
+ * apply_log_table: if non-NULL, call apply_log() on this table between
1912
+ * locking attempts. This prevents lots of log from
1913
+ * building up if getting the exclusive lock takes many
1914
+ * retries over a long period.
1911
1915
*/
1912
1916
static bool
1913
- lock_exclusive (PGconn * conn , const char * relid , const char * lock_query , bool start_xact )
1917
+ lock_exclusive (PGconn * conn , const char * relid , const char * lock_query ,
1918
+ bool start_xact , const repack_table * apply_log_table )
1914
1919
{
1915
1920
time_t start = time (NULL );
1916
1921
int i ;
@@ -1991,6 +1996,8 @@ lock_exclusive(PGconn *conn, const char *relid, const char *lock_query, bool sta
1991
1996
pgut_command (conn , "ROLLBACK TO SAVEPOINT repack_sp1" , 0 , NULL );
1992
1997
if (timeout_msec < wait_msec )
1993
1998
usleep (1000 * (wait_msec - timeout_msec ));
1999
+ if (apply_log_table )
2000
+ apply_log (conn , apply_log_table , 0 );
1994
2001
continue ;
1995
2002
}
1996
2003
else
@@ -2031,7 +2038,7 @@ repack_cleanup_callback(bool fatal, void *userdata)
2031
2038
reconnect (ERROR );
2032
2039
2033
2040
command ("BEGIN ISOLATION LEVEL READ COMMITTED" , 0 , NULL );
2034
- if (!(lock_exclusive (connection , params [0 ], table -> lock_table , false)))
2041
+ if (!(lock_exclusive (connection , params [0 ], table -> lock_table , false, NULL )))
2035
2042
{
2036
2043
pgut_rollback (connection );
2037
2044
elog (ERROR , "lock_exclusive() failed in connection for %s during cleanup callback" ,
@@ -2071,7 +2078,7 @@ repack_cleanup(bool fatal, const repack_table *table)
2071
2078
params [1 ] = utoa (temp_obj_num , num_buff );
2072
2079
2073
2080
command ("BEGIN ISOLATION LEVEL READ COMMITTED" , 0 , NULL );
2074
- if (!(lock_exclusive (connection , params [0 ], table -> lock_table , false)))
2081
+ if (!(lock_exclusive (connection , params [0 ], table -> lock_table , false, NULL )))
2075
2082
{
2076
2083
pgut_rollback (connection );
2077
2084
elog (ERROR , "lock_exclusive() failed in connection for %s during cleanup" ,
@@ -2225,7 +2232,7 @@ repack_table_indexes(PGresult *index_details)
2225
2232
resetStringInfo (& sql );
2226
2233
appendStringInfo (& sql , "LOCK TABLE %s IN ACCESS EXCLUSIVE MODE" ,
2227
2234
table_name );
2228
- if (!(lock_exclusive (connection , params [1 ], sql .data , true)))
2235
+ if (!(lock_exclusive (connection , params [1 ], sql .data , true, NULL )))
2229
2236
{
2230
2237
elog (WARNING , "lock_exclusive() failed in connection for %s" ,
2231
2238
table_name );
0 commit comments