Hi Antonin, Thanks for the patch. I like that it is much simpler and does not touch the lock manager.
> Waiting for a limited time would make more sense, but why exactly > deadlock_timeout should control that? REPACK has to wait at least deadlock_timeout so its own deadlock check runs, because some cycles only REPACK can find. With a three session cycle and deadlock_timeout = 10s, master reports a deadlock after 10 seconds but your patch still hangs after 75 seconds. > Note that it deliberately does not teach REPACK to give up. Also, with lock_timeout = 5s REPACK did not fail and finished after 16 seconds. Should the docs say so, or could lock_timeout still apply? Thanks, Shihao
From 514c247cf32d2fbf6f3f3604c8fecd3dcaecdeb9 Mon Sep 17 00:00:00 2001 From: Shihao <[email protected]> Date: Wed, 2 Dec 2026 20:47:29 -0500 Subject: [PATCH] Don't block the table while REPACK (CONCURRENTLY) waits for its lock While REPACK queues for AccessExclusiveLock, all new lock requests on the table queue behind it. Poll for the lock instead, and apply the changes made meanwhile between attempts. lock_timeout limits how long we keep polling. --- doc/src/sgml/ref/repack.sgml | 13 +++-- src/backend/commands/repack.c | 47 ++++++++++++++++++- .../utils/activity/wait_event_names.txt | 1 + 3 files changed, 57 insertions(+), 4 deletions(-) diff --git a/doc/src/sgml/ref/repack.sgml b/doc/src/sgml/ref/repack.sgml index 0649e033aa1..cc469dd7f9e 100644 --- a/doc/src/sgml/ref/repack.sgml +++ b/doc/src/sgml/ref/repack.sgml @@ -363,9 +363,16 @@ REPACK [ ( <replaceable class="parameter">option</replaceable> [, ...] ) ] USING the <literal>ACCESS EXCLUSIVE</literal> lock is requested. Once that lock is obtained, a final pass over any remaining captured concurrent data changes is done and the files are swapped. - Thus the lock is typically held for a short time, depending - on the amount of changes accumulated while the lock was being - waited for. + Thus the lock is typically held for a short time. + </para> + + <para> + <command>REPACK</command> does not queue for the <literal>ACCESS + EXCLUSIVE</literal> lock, because other transactions that want to use + the table would have to wait behind it. Instead, it checks whether the + lock is available, and while it is not, it applies the data changes that + take place meanwhile and checks again. <xref linkend="guc-lock-timeout"/>, + if set, limits how long it keeps trying. </para> <para> diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c index 5f50abf522c..74f1c9704d0 100644 --- a/src/backend/commands/repack.c +++ b/src/backend/commands/repack.c @@ -83,6 +83,7 @@ #include "utils/relmapper.h" #include "utils/snapmgr.h" #include "utils/syscache.h" +#include "utils/timestamp.h" #include "utils/wait_event_types.h" /* @@ -102,6 +103,9 @@ typedef struct */ #define WORKER_FILE_SNAPSHOT 0 +/* How often to check for the final AccessExclusiveLock, in milliseconds. */ +#define REPACK_LOCK_WAIT_INTERVAL 50 + /* * Information needed to apply concurrent data changes. */ @@ -3442,6 +3446,7 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap, XLogRecPtr end_of_wal; List *indexrels; ChangeContext chgcxt; + TimestampTz lock_wait_start; Assert(CheckRelationLockedByMe(OldHeap, ShareUpdateExclusiveLock, false)); Assert(CheckRelationLockedByMe(NewHeap, AccessExclusiveLock, false)); @@ -3512,8 +3517,48 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap, /* * Acquire AccessExclusiveLock on the table, its TOAST relation (if there * is one), all its indexes, so that we can swap the files. + * + * Do not queue for the table lock. While we are queued, every new lock + * request on the table queues behind us, so waiting behind one + * long-running transaction would block all access to the table for that + * long. Instead, keep checking whether the lock is available, and apply + * the changes that arrive in the meantime, so that the work left for the + * time we hold the lock stays small. lock_timeout limits how long we + * keep trying. */ - LockRelationOid(old_table_oid, AccessExclusiveLock); + lock_wait_start = GetCurrentTimestamp(); + while (!ConditionalLockRelationOid(old_table_oid, AccessExclusiveLock)) + { + CHECK_FOR_INTERRUPTS(); + + if (LockTimeout > 0 && + TimestampDifferenceExceeds(lock_wait_start, GetCurrentTimestamp(), + LockTimeout)) + ereport(ERROR, + errcode(ERRCODE_LOCK_NOT_AVAILABLE), + errmsg("canceling statement due to lock timeout")); + + XLogFlush(GetXLogInsertEndRecPtr()); + end_of_wal = GetFlushRecPtr(NULL); + process_concurrent_changes(end_of_wal, &chgcxt, false); + + /* + * If someone is already waiting behind the lock we hold, they may + * hold a lock on the table themselves, which they will never release + * while waiting for us. Wait for the lock the normal way then, so + * that we either get it ahead of them or the deadlock detector + * resolves the situation. + */ + if (LockHasWaitersRelation(OldHeap, ShareUpdateExclusiveLock)) + { + LockRelationOid(old_table_oid, AccessExclusiveLock); + break; + } + + (void) WaitLatch(MyLatch, WL_LATCH_SET | WL_TIMEOUT | WL_EXIT_ON_PM_DEATH, + REPACK_LOCK_WAIT_INTERVAL, WAIT_EVENT_REPACK_LOCK_WAIT); + ResetLatch(MyLatch); + } /* * Lock all indexes now, not only the clustering one: all indexes need to diff --git a/src/backend/utils/activity/wait_event_names.txt b/src/backend/utils/activity/wait_event_names.txt index 112873dc46b..c2d694919a5 100644 --- a/src/backend/utils/activity/wait_event_names.txt +++ b/src/backend/utils/activity/wait_event_names.txt @@ -154,6 +154,7 @@ RECOVERY_CONFLICT_SNAPSHOT "Waiting for recovery conflict resolution for a vacuu RECOVERY_CONFLICT_TABLESPACE "Waiting for recovery conflict resolution for dropping a tablespace." RECOVERY_END_COMMAND "Waiting for <xref linkend="guc-recovery-end-command"/> to complete." RECOVERY_PAUSE "Waiting for recovery to be resumed." +REPACK_LOCK_WAIT "Waiting to acquire an exclusive lock on a table repacked concurrently, to swap its files." REPACK_WORKER_EXPORT "Waiting for decoding worker to export a new output file." REPLICATION_ORIGIN_DROP "Waiting for a replication origin to become inactive so it can be dropped." REPLICATION_SLOT_DROP "Waiting for a replication slot to become inactive so it can be dropped." -- 2.37.1 (Apple Git-137.1)
