Skip to content

Commit c5501cf

Browse files
committed
Add options for lock statement timeout
1 parent f97c41e commit c5501cf

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

bin/pg_repack.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ static unsigned int temp_obj_num = 0; /* temporary objects counter */
255255
static bool no_kill_backend = false; /* abandon when timed-out */
256256
static bool no_superuser_check = false;
257257
static SimpleStringList exclude_extension_list = {NULL, NULL}; /* don't repack tables of these extensions */
258+
static int lock_wait_max = 1; /* Max lock statement timeout, in seconds */
258259

259260
/* buffer should have at least 11 bytes */
260261
static char *
@@ -284,6 +285,7 @@ static pgut_option options[] =
284285
{ 'b', 'D', "no-kill-backend", &no_kill_backend },
285286
{ 'b', 'k', "no-superuser-check", &no_superuser_check },
286287
{ 'l', 'C', "exclude-extension", &exclude_extension_list },
288+
{ 'i', 'L', "lock-wait-max", &lock_wait_max },
287289
{ 0 },
288290
};
289291

@@ -307,6 +309,12 @@ main(int argc, char *argv[])
307309
if (dryrun)
308310
elog(INFO, "Dry run enabled, not executing repack");
309311

312+
if (wait_timeout < lock_wait_max)
313+
{
314+
ereport(ERROR, (errcode(EINVAL),
315+
errmsg("wait-timeout needs to be >= lock-wait-max")));
316+
}
317+
310318
if (r_index.head || only_indexes)
311319
{
312320
if (r_index.head && table_list.head)
@@ -1673,7 +1681,7 @@ lock_access_share(PGconn *conn, Oid relid, const char *target_name)
16731681
break;
16741682

16751683
/* wait for a while to lock the table. */
1676-
wait_msec = Min(1000, i * 100);
1684+
wait_msec = Min(lock_wait_max, Max(lock_wait_max / 10 * i, 100));
16771685
printfStringInfo(&sql, "SET LOCAL statement_timeout = %d", wait_msec);
16781686
pgut_command(conn, sql.data, 0, NULL);
16791687

@@ -1814,7 +1822,7 @@ lock_exclusive(PGconn *conn, const char *relid, const char *lock_query, bool sta
18141822
}
18151823

18161824
/* wait for a while to lock the table. */
1817-
wait_msec = Min(1000, i * 100);
1825+
wait_msec = Min(lock_wait_max, Max(lock_wait_max / 10 * i, 100));
18181826
snprintf(sql, lengthof(sql), "SET LOCAL statement_timeout = %d", wait_msec);
18191827
pgut_command(conn, sql, 0, NULL);
18201828

@@ -2240,4 +2248,5 @@ pgut_help(bool details)
22402248
printf(" -Z, --no-analyze don't analyze at end\n");
22412249
printf(" -k, --no-superuser-check skip superuser checks in client\n");
22422250
printf(" -C, --exclude-extension don't repack tables which belong to specific extension\n");
2251+
printf(" -L, --lock-wait-max=SECS lock statement max wait time\n");
22432252
}

0 commit comments

Comments
 (0)