On Tue, Oct 6, 2026 at 1:08 PM Alvaro Herrera <[email protected]> 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
From 8deaf0a748525a733a4861e6123bf616e451bb26 Mon Sep 17 00:00:00 2001
From: Nik Samokhvalov <[email protected]>
Date: Tue, 1 Dec 2026 15:48:04 -0800
Subject: [PATCH v1] doc: Clarify REPACK (CONCURRENTLY) locking and resource
 notes

Document operational behavior of REPACK (CONCURRENTLY) in repack.sgml:

- Clarify in Description and Parameters that CONCURRENTLY still acquires
  ACCESS EXCLUSIVE during the final swap phase.
- Add a list of locking caveats in Notes on Concurrent Operation covering
  final ACCESS EXCLUSIVE lock-queue blocking, lack of retry on
  lock_timeout/deadlock/cancellation (discarding all rebuild work), and
  initial SHARE UPDATE EXCLUSIVE interaction with regular and
  wraparound-prevention autovacuum.
- Note pg_stat_activity.backend_xmin alongside backend_xid and
  cross-reference on-disk storage of concurrent changes in Notes on
  Resources.
- Fix a typo in Parameters ("specific" -> "specified").

Discussion: https://postgr.es/m/CAGRkXqQTRvmJJBjLmLa0boO1S78nz46s=b3htymftksw-fs...@mail.gmail.com
Backpatch-through: 19
---
 doc/src/sgml/ref/repack.sgml | 66 ++++++++++++++++++++++++++++++++----
 1 file changed, 59 insertions(+), 7 deletions(-)

diff --git a/doc/src/sgml/ref/repack.sgml b/doc/src/sgml/ref/repack.sgml
index 0649e033aa1..3cb37c1254f 100644
--- a/doc/src/sgml/ref/repack.sgml
+++ b/doc/src/sgml/ref/repack.sgml
@@ -67,9 +67,11 @@ REPACK [ ( <replaceable class="parameter">option</replaceable> [, ...] ) ] USING
 
   <para>
    When a table is being repacked, an <literal>ACCESS EXCLUSIVE</literal> lock
-   is acquired on it, unless the <literal>CONCURRENTLY</literal> option is given.
-   See <xref linkend="sql-repack-notes-on-concurrently"/> for a discussion
-   on the effects of this option.
+   is held on it for the entire operation unless the
+   <literal>CONCURRENTLY</literal> option is given, in which case an
+   <literal>ACCESS EXCLUSIVE</literal> lock is still acquired during the final
+   swap phase.  See <xref linkend="sql-repack-notes-on-concurrently"/> for a
+   discussion on the effects of this option.
   </para>
  </refsect1>
 
@@ -91,7 +93,7 @@ REPACK [ ( <replaceable class="parameter">option</replaceable> [, ...] ) ] USING
     <listitem>
      <para>
       The name of a specific column to analyze. Defaults to all columns.
-      If a column list is specific, <literal>ANALYZE</literal> must also
+      If a column list is specified, <literal>ANALYZE</literal> must also
       be specified.
      </para>
     </listitem>
@@ -110,7 +112,9 @@ REPACK [ ( <replaceable class="parameter">option</replaceable> [, ...] ) ] USING
     <term><literal>CONCURRENTLY</literal></term>
     <listitem>
      <para>
-      Allow other transactions to use the table while it is being repacked.
+      Allow other transactions to read and write the table during the bulk of
+      the repacking operation; an <literal>ACCESS EXCLUSIVE</literal> lock is
+      still required during the final swap phase.
       See <xref linkend="sql-repack-notes-on-concurrently" /> for more details.
      </para>
 
@@ -292,7 +296,10 @@ REPACK [ ( <replaceable class="parameter">option</replaceable> [, ...] ) ] USING
    <para>
     <command>REPACK</command> creates a temporary copy of the table and all
     of its indexes, so you need free disk space at least equal to the sum
-    of the table size and the index sizes.
+    of the table size and the index sizes.  With
+    <literal>CONCURRENTLY</literal>, additional disk space is needed to store
+    the concurrent data changes
+    (see <xref linkend="sql-repack-notes-on-concurrently"/>).
    </para>
 
    <para>
@@ -314,7 +321,9 @@ REPACK [ ( <replaceable class="parameter">option</replaceable> [, ...] ) ] USING
     <command>REPACK</command> is considered a single long-running
     transaction, which prevents <command>VACUUM</command> from cleaning
     up dead rows from other tables.
-    It is advisable to monitor <structname>pg_stat_activity.backend_xid</structname>
+    It is advisable to monitor
+    <structname>pg_stat_activity.backend_xmin</structname> and
+    <structname>pg_stat_activity.backend_xid</structname>
     for the process running <command>REPACK</command> when repacking
     very large tables, to avoid causing excessive bloat in other tables.
    </para>
@@ -379,6 +388,49 @@ REPACK [ ( <replaceable class="parameter">option</replaceable> [, ...] ) ] USING
     by <command>UPDATE</command>) are stored on disk separately.
    </para>
 
+   <para>
+    When running <command>REPACK (CONCURRENTLY)</command>, keep the following
+    locking caveats in mind:
+    <itemizedlist>
+     <listitem>
+      <para>
+       While <command>REPACK</command> waits for the final
+       <literal>ACCESS EXCLUSIVE</literal> lock behind a long-running
+       transaction holding any lock on the table, new conflicting lock requests
+       on the table (including plain reads and writes) queue behind
+       <command>REPACK</command>'s request.
+      </para>
+     </listitem>
+
+     <listitem>
+      <para>
+       The final <literal>ACCESS EXCLUSIVE</literal> lock request does not
+       retry.  If it fails due to <xref linkend="guc-lock-timeout"/>, a
+       deadlock, or cancellation, the entire command rolls back and discards
+       all repacking work done so far (which on a large table can mean hours
+       of copying).  Setting <varname>lock_timeout</varname> to protect
+       concurrent traffic therefore carries the risk of aborting at the final
+       step; avoid having long-running transactions open on the table when
+       <command>REPACK (CONCURRENTLY)</command> reaches its final phase.
+      </para>
+     </listitem>
+
+     <listitem>
+      <para>
+       At the start of the operation, the initial
+       <literal>SHARE UPDATE EXCLUSIVE</literal> lock conflicts with
+       autovacuum: regular autovacuum is normally canceled after
+       <xref linkend="guc-deadlock-timeout"/> (unless a shorter
+       <varname>lock_timeout</varname> fires first), while a
+       wraparound-prevention autovacuum worker is not canceled and causes
+       <command>REPACK</command> to wait (without blocking ordinary reads and
+       writes).  A <varname>lock_timeout</varname> failure at that initial
+       stage discards no copying work.
+      </para>
+     </listitem>
+    </itemizedlist>
+   </para>
+
    <para>
     <command>REPACK (CONCURRENTLY)</command> might fail to complete if DDL
     commands are executed on the table by other transactions during the

base-commit: dc168a83874fbf5cee4afb76a623e1349a539724
-- 
2.50.1 (Apple Git-155)

Reply via email to