Skip to content

Commit 8a7047c

Browse files
committed
Add options for lock statement timeout
1 parent 7eaa988 commit 8a7047c

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

bin/pg_repack.c

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

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

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

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

17911799
/* wait for a while to lock the table. */
1792-
wait_msec = Min(1000, i * 100);
1800+
wait_msec = Min(lock_wait_max * 1000, Max(lock_wait_max * 1000 * i / 10, 100));
17931801
printfStringInfo(&sql, "SET LOCAL statement_timeout = %d", wait_msec);
17941802
pgut_command(conn, sql.data, 0, NULL);
17951803

@@ -1930,7 +1938,7 @@ lock_exclusive(PGconn *conn, const char *relid, const char *lock_query, bool sta
19301938
}
19311939

19321940
/* wait for a while to lock the table. */
1933-
wait_msec = Min(1000, i * 100);
1941+
wait_msec = Min(lock_wait_max * 1000, Max(lock_wait_max * 1000 * i / 10, 100));
19341942
snprintf(sql, lengthof(sql), "SET LOCAL statement_timeout = %d", wait_msec);
19351943
pgut_command(conn, sql, 0, NULL);
19361944

@@ -2351,6 +2359,7 @@ pgut_help(bool details)
23512359
printf(" -i, --index=INDEX move only the specified index\n");
23522360
printf(" -x, --only-indexes move only indexes of the specified table\n");
23532361
printf(" -T, --wait-timeout=SECS timeout to cancel other backends on conflict\n");
2362+
printf(" -L, --lock-wait-max=SECS lock statement max wait time\n");
23542363
printf(" -D, --no-kill-backend don't kill other backends when timed out\n");
23552364
printf(" -Z, --no-analyze don't analyze at end\n");
23562365
printf(" -k, --no-superuser-check skip superuser checks in client\n");

doc/pg_repack.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ Options:
119119
-i, --index=INDEX move only the specified index
120120
-x, --only-indexes move only indexes of the specified table
121121
-T, --wait-timeout=SECS timeout to cancel other backends on conflict
122+
-L, --lock-wait-max=SECS lock statement max wait time
122123
-D, --no-kill-backend don't kill other backends when timed out
123124
-Z, --no-analyze don't analyze at end
124125
-k, --no-superuser-check skip superuser checks in client
@@ -207,6 +208,13 @@ Reorg Options
207208
backends after twice this timeout has passed.
208209
The default is 60 seconds.
209210

211+
``-L SECS``, ``--lock-wait-max=SECS``
212+
When pg_repack needs to take table locks (shared or exclusive) it'll wait
213+
up to this many seconds before canceling the lock request and trying again.
214+
This is done to allow concurrent queries to make progress and not get
215+
queued up behind the pg_repack lock request. Consider increasing this
216+
timeout if pg_repack fails to acquire table locks. The default is 1 second.
217+
210218
``-D``, ``--no-kill-backend``
211219
Skip to repack table if the lock cannot be taken for duration specified
212220
``--wait-timeout``, instead of cancelling conflicting queries. The default

doc/pg_repack_jp.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ pg_repackもしくはpg_reorgの古いバージョンからのアップグレー
207207
-i, --index=INDEX move only the specified index
208208
-x, --only-indexes move only indexes of the specified table
209209
-T, --wait-timeout=SECS timeout to cancel other backends on conflict
210+
-L, --lock-wait-max=SECS lock statement max wait time
210211
-D, --no-kill-backend don't kill other backends when timed out
211212
-Z, --no-analyze don't analyze at end
212213
-k, --no-superuser-check skip superuser checks in client

0 commit comments

Comments
 (0)