From 6f57c70f2ebb90947cf61a748640c0e71569ccf3 Mon Sep 17 00:00:00 2001
From: Dave Sharpe <dave.sharpe@datastone.ca>
Date: Tue, 24 Nov 2026 12:36:51 +0000
Subject: [PATCH v1 2/3] doc: pg_dump can dump a table as empty after a
 non-MVCC-safe command

pg_dump takes its snapshot and then locks the tables it dumps.  If a
command that is not MVCC-safe commits on a table in between, pg_dump
reads that table as empty: the dump has no rows for it, pg_dump exits
with status 0, and the archive restores without error.  Nothing reports
the loss.

For TRUNCATE and a rewriting ALTER TABLE the window is the short time
between pg_dump's snapshot and its lock, because those commands hold
their lock for their whole duration.  REPACK (CONCURRENTLY), new in 19,
takes its exclusive lock only for the final swap, so a dump started at
any time while it runs is exposed.

The pg_dump reference page does not mention this.  Add a note that
points to the MVCC caveats.

Reproducer (REL_19_0); the exported snapshot only fixes the timing:

    CREATE TABLE orders (id int PRIMARY KEY);
    INSERT INTO orders SELECT generate_series(1, 1000);

    s1: BEGIN ISOLATION LEVEL REPEATABLE READ;
    s1: SELECT pg_export_snapshot();          -- e.g. 00000003-0000001B-1
    s2: REPACK (CONCURRENTLY) orders;
    $ pg_dump --data-only --table=orders \
          --snapshot=00000003-0000001B-1 > orders.dump; echo $?
    0                                         -- and no row in orders.dump

Only the REPACK (CONCURRENTLY) case above was run; the TRUNCATE and ALTER
TABLE cases follow from the same section of mvcc.sgml.

Backpatch-through: 19

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---
 doc/src/sgml/ref/pg_dump.sgml | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/doc/src/sgml/ref/pg_dump.sgml b/doc/src/sgml/ref/pg_dump.sgml
index 951de54e77f..01eb12b44a9 100644
--- a/doc/src/sgml/ref/pg_dump.sgml
+++ b/doc/src/sgml/ref/pg_dump.sgml
@@ -1665,6 +1665,21 @@ CREATE DATABASE foo WITH TEMPLATE template0;
    not automatically restored.
   </para>
 
+  <para>
+   <application>pg_dump</application> takes its snapshot before it locks
+   the tables it dumps.  If a command that is not MVCC-safe (see
+   <xref linkend="mvcc-caveats"/>) commits on a table in between, that
+   table is dumped without its rows, and
+   <application>pg_dump</application> reports no error.  The commands
+   concerned are <command>TRUNCATE</command>, the table-rewriting forms of
+   <command>ALTER TABLE</command>, and <command>REPACK</command> with
+   the <literal>CONCURRENTLY</literal> option.
+   <command>REPACK</command> with <literal>CONCURRENTLY</literal> does not
+   lock out other sessions while it copies the table, so a dump that
+   starts at any time while it runs is affected; do not start a dump
+   while one is running.
+  </para>
+
   <para>
    When a dump without schema is chosen and the option <option>--disable-triggers</option>
    is used, <application>pg_dump</application> emits commands
-- 
2.43.0

