Re: REPACK (CONCURRENTLY): do not block the table while waiting for the final lock

From: Nikolay Samokhvalov <nik(at)postgres(dot)ai>
To: Alvaro Herrera <alvherre(at)kurilemu(dot)de>
Cc: Antonin Houska <ah(at)cybertec(dot)at>, shihao zhong <zhong950419(at)gmail(dot)com>, Kirk Wolak <wolakk(at)gmail(dot)com>, pgsql-hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: REPACK (CONCURRENTLY): do not block the table while waiting for the final lock
Date: 2026-12-01 23:57:37
Message-ID: CAM527d_Uc0yRTBHQWCz5vGg+pOeGxW7Bt5hkcirLmSCntCD6Qg@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Tue, Oct 6, 2026 at 1:08 PM Alvaro Herrera <alvherre(at)kurilemu(dot)de> wrote:
> I definitely dont't want to change this behavior in 19. At the most,
> lets document any shortcomings that the current behavior may have.

Hi Alvaro, Antonin, hackers,

Kirk Wolak (CC'd) and I ran into several operational edge cases while
testing REPACK (CONCURRENTLY) on PG 19 using DBLab Forge [1], which led
to the attached doc patch for repack.sgml [2]. Line numbers below are
for REL_19_STABLE at fb4e094ec45e.

1. Description (lines 68-73) and CONCURRENTLY parameter (lines 110-116)

Description says an ACCESS EXCLUSIVE lock is acquired "unless the
CONCURRENTLY option is given", and the CONCURRENTLY parameter says
"Allow other transactions to use the table while it is being repacked."
Users familiar with CIC/RC may read both as saying REPACK (CONCURRENTLY)
never takes ACCESS EXCLUSIVE on the table. Claried in both places that
ACCESS EXCLUSIVE is still required during the final swap phase.

2. Final ACCESS EXCLUSIVE wait and no retry (lines 342-357)

Notes on Concurrent Operation say the final lock "is typically held for
a short time", without mentioning what happens while waiting for it:

- while REPACK (CONCURRENTLY) waits for ACCESS EXCLUSIVE behind a
long-running transaction holding any lock on the table (even ACCESS
SHARE from a plain SELECT), new conflicting lock requests (including
new reads and writes) queue behind it;
- the final lock request does not retry, so if it fails due to
lock_timeout, a deadlock, or cancellation, the entire command rolls
back and discards all repacking work done so far. On a large table,
an operator who sets lock_timeout to protect production traffic from
a lock-queue stall can lose hours of copying work at the final step.
This is especially surprising for users coming from pg_repack, which
retries the final lock acquisition iterating with lock_timeout=100..1000ms.

Also mention autovacuum at the start of the operation (initial SHARE
UPDATE EXCLUSIVE lock): regular autovacuum is normally canceled after
deadlock_timeout (unless a shorter lock_timeout fires first), while a
wraparound-prevention autovacuum worker is not canceled and causes
REPACK to wait without blocking ordinary reads and writes; a
lock_timeout failure at that initial stage discards no copying work.

3. Notes on Resources (lines 282-308)

- Line 305 recommends monitoring pg_stat_activity.backend_xid. Because
REPACK takes a snapshot before an XID is assigned, backend_xmin is
normally the older value; mention both backend_xmin and backend_xid.
- Lines 282-286: add a brief cross-reference to the separate on-disk
storage for concurrent changes under CONCURRENTLY (lines 365-367).

4. Typo (line 94; "specific" -> "specified")

Separately, beyond the polling and bounded-wait approaches discussed
earlier in this thread for PG 20, was a pg_repack-style retry loop
(retrying on lock_timeout rather than aborting the whole REPACK)
discussed anywhere else?

The attached docs patch is based on master (dc168a83874f) and applies
cleanly to REL_19_STABLE (fb4e094ec45e). I prefixed it with nocfbot- so
cfbot keeps testing the PG 20 patches in this thread.

[1] https://console.postgres.ai/shared/brief/01a22e1d-4f42-7e46-996e-6664e89d0fc2
[2] https://github.com/postgres/postgres/blob/fb4e094ec45e/doc/src/sgml/ref/repack.sgml

Nik

Attachment Content-Type Size
nocfbot-v1-0001-doc-Clarify-REPACK-CONCURRENTLY-locking-and-resou.patch text/x-patch 6.5 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Previous Message Tomas Vondra 2026-12-01 23:51:56 A couple old bugs in BRIN [de]summarization