Skip to content

Commit

Permalink
Add options for lock statement timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurprs committed Sep 10, 2018
1 parent f97c41e commit dbb4a69
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions bin/pg_repack.c
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ static unsigned int temp_obj_num = 0; /* temporary objects counter */
static bool no_kill_backend = false; /* abandon when timed-out */
static bool no_superuser_check = false;
static SimpleStringList exclude_extension_list = {NULL, NULL}; /* don't repack tables of these extensions */
static int lock_wait_max = 1; /* Max lock statement timeout, in seconds */

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

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

if (wait_timeout < lock_wait_max)
{
ereport(ERROR, (errcode(EINVAL),
errmsg("wait-timeout needs to be >= lock-wait-max")));
}

if (r_index.head || only_indexes)
{
if (r_index.head && table_list.head)
Expand Down Expand Up @@ -1673,7 +1681,7 @@ lock_access_share(PGconn *conn, Oid relid, const char *target_name)
break;

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

Expand Down Expand Up @@ -1814,7 +1822,7 @@ lock_exclusive(PGconn *conn, const char *relid, const char *lock_query, bool sta
}

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

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

0 comments on commit dbb4a69

Please sign in to comment.