From 8deaf0a748525a733a4861e6123bf616e451bb26 Mon Sep 17 00:00:00 2001
From: Nik Samokhvalov <nik@postgres.ai>
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-FS18Q@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)

