Skip to content

Commit 467ef18

Browse files
committed
call apply_log() in between lock attempts
1 parent 1b3f891 commit 467ef18

1 file changed

Lines changed: 15 additions & 8 deletions

File tree

‎bin/pg_repack.c‎

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ static bool rebuild_indexes(const repack_table *table);
225225
static char *getstr(PGresult *res, int row, int col);
226226
static Oid getoid(PGresult *res, int row, int col);
227227
static bool advisory_lock(PGconn *conn, const char *relid);
228-
static bool lock_exclusive(PGconn *conn, const char *relid, const char *lock_query, bool start_xact);
228+
static bool lock_exclusive(PGconn *conn, const char *relid, const char *lock_query, bool start_xact, const repack_table *apply_log_table);
229229
static bool kill_ddl(PGconn *conn, Oid relid, bool terminate);
230230
static bool lock_access_share(PGconn *conn, Oid relid, const char *target_name);
231231

@@ -1302,7 +1302,7 @@ repack_one_table(repack_table *table, const char *orderby)
13021302
if (!advisory_lock(connection, buffer))
13031303
goto cleanup;
13041304

1305-
if (!(lock_exclusive(connection, buffer, table->lock_table, true)))
1305+
if (!(lock_exclusive(connection, buffer, table->lock_table, true, NULL)))
13061306
{
13071307
if (no_kill_backend)
13081308
elog(INFO, "Skipping repack %s due to timeout", table->target_name);
@@ -1615,7 +1615,7 @@ repack_one_table(repack_table *table, const char *orderby)
16151615
/* Bump our existing AccessShare lock to AccessExclusive */
16161616

16171617
if (!(lock_exclusive(conn2, utoa(table->target_oid, buffer),
1618-
table->lock_table, false)))
1618+
table->lock_table, false, table)))
16191619
{
16201620
elog(WARNING, "lock_exclusive() failed in conn2 for %s",
16211621
table->target_name);
@@ -1634,7 +1634,7 @@ repack_one_table(repack_table *table, const char *orderby)
16341634

16351635
command("BEGIN ISOLATION LEVEL READ COMMITTED", 0, NULL);
16361636
if (!(lock_exclusive(connection, utoa(table->target_oid, buffer),
1637-
table->lock_table, false)))
1637+
table->lock_table, false, NULL)))
16381638
{
16391639
elog(WARNING, "lock_exclusive() failed in connection for %s",
16401640
table->target_name);
@@ -1885,9 +1885,14 @@ static bool advisory_lock(PGconn *conn, const char *relid)
18851885
* start_xact: whether we will issue a BEGIN ourselves. If not, we will
18861886
* use a SAVEPOINT and ROLLBACK TO SAVEPOINT if our query
18871887
* times out, to avoid leaving the transaction in error state.
1888+
* apply_log_table: if non-NULL, call apply_log() on this table between
1889+
* locking attempts. This prevents lots of log from
1890+
* building up if getting the exclusive lock takes many
1891+
* retries over a long period.
18881892
*/
18891893
static bool
1890-
lock_exclusive(PGconn *conn, const char *relid, const char *lock_query, bool start_xact)
1894+
lock_exclusive(PGconn *conn, const char *relid, const char *lock_query,
1895+
bool start_xact, const repack_table *apply_log_table)
18911896
{
18921897
time_t start = time(NULL);
18931898
int i;
@@ -1968,6 +1973,8 @@ lock_exclusive(PGconn *conn, const char *relid, const char *lock_query, bool sta
19681973
pgut_command(conn, "ROLLBACK TO SAVEPOINT repack_sp1", 0, NULL);
19691974
if (timeout_msec < wait_msec)
19701975
usleep(1000 * (wait_msec - timeout_msec));
1976+
if (apply_log_table)
1977+
apply_log(conn, apply_log_table, 0);
19711978
continue;
19721979
}
19731980
else
@@ -2008,7 +2015,7 @@ repack_cleanup_callback(bool fatal, void *userdata)
20082015
reconnect(ERROR);
20092016

20102017
command("BEGIN ISOLATION LEVEL READ COMMITTED", 0, NULL);
2011-
if (!(lock_exclusive(connection, params[0], table->lock_table, false)))
2018+
if (!(lock_exclusive(connection, params[0], table->lock_table, false, NULL)))
20122019
{
20132020
pgut_rollback(connection);
20142021
elog(ERROR, "lock_exclusive() failed in connection for %s during cleanup callback",
@@ -2048,7 +2055,7 @@ repack_cleanup(bool fatal, const repack_table *table)
20482055
params[1] = utoa(temp_obj_num, num_buff);
20492056

20502057
command("BEGIN ISOLATION LEVEL READ COMMITTED", 0, NULL);
2051-
if (!(lock_exclusive(connection, params[0], table->lock_table, false)))
2058+
if (!(lock_exclusive(connection, params[0], table->lock_table, false, NULL)))
20522059
{
20532060
pgut_rollback(connection);
20542061
elog(ERROR, "lock_exclusive() failed in connection for %s during cleanup",
@@ -2202,7 +2209,7 @@ repack_table_indexes(PGresult *index_details)
22022209
resetStringInfo(&sql);
22032210
appendStringInfo(&sql, "LOCK TABLE %s IN ACCESS EXCLUSIVE MODE",
22042211
table_name);
2205-
if (!(lock_exclusive(connection, params[1], sql.data, true)))
2212+
if (!(lock_exclusive(connection, params[1], sql.data, true, NULL)))
22062213
{
22072214
elog(WARNING, "lock_exclusive() failed in connection for %s",
22082215
table_name);

0 commit comments

Comments
 (0)