Hi,

This message has a delivery problem with DKIM-signed list headers, so I
grabbed it from the moderation queue to ensure we don't lose it.

Dave: this is me resending your message back to you but also to the
pgsql-hackers mailing list, to ensure it isn't lost.

Regards

-- 
Álvaro Herrera
--- Begin Message ---
Hi,

While building an extension that guards against the documented
REPACK (CONCURRENTLY) caveat, I found three places where the
documentation of commands that are not MVCC-safe is incomplete or
inconsistent. The attached patches fix the docs only; no behaviour
changes.

0001 doc: REPACK (CONCURRENTLY) affects snapshots taken before it commits

  The <warning> on the REPACK reference page says the table appears
  empty to transactions "using a snapshot taken before repacking
  started". The MVCC "Caveats" section says, correctly, before the
  repacking transaction committed. The difference matters because
  REPACK (CONCURRENTLY) can run for a long time: a transaction that
  takes its snapshot while it runs, and reads the table after the
  commit, also sees it as empty. The patch aligns the warning with
  mvcc.sgml and notes that max_repack_replication_slots = 0 disables
  the CONCURRENTLY option for a whole server.

0002 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. If a command
  that is not MVCC-safe commits on a table in between, the dump has no
  rows for that table, pg_dump exits with 0, and the restore succeeds.
  For TRUNCATE and rewriting ALTER TABLE the window is short. For
  REPACK (CONCURRENTLY), which takes its exclusive lock only for the
  final swap, any dump started while the repack runs is exposed. The
  pg_dump page does not mention this; the patch adds a paragraph that
  points to the MVCC caveats.

0003 doc: REFRESH MATERIALIZED VIEW without CONCURRENTLY is not MVCC-safe

  The refresh writes the new contents with TABLE_INSERT_FROZEN
  (matview.c, transientrel_startup), so a transaction whose snapshot
  predates the refresh, and which had not yet read the view, sees the
  new contents after the commit: data newer than its snapshot. The
  caveats section does not list this command, and its "appears empty"
  description does not fit it. The patch documents it in mvcc.sgml and
  on the command's reference page. The behaviour is long-standing, so
  the patch documents it as it is.

Each commit message has a two-session reproducer; all three give the
wrong result on 19.0 and on master at 9ebc9a458c3 (cassert build). 0001
and 0002 describe behaviour new in 19 and apply cleanly to
REL_19_STABLE; I would suggest back-patching them to 19. 0003 applies
to REL_19_STABLE too; the behaviour it documents is older than 19.

The patches apply to master at 9ebc9a458c3 and REL_19_STABLE at
20d6f4a22e5, and the documentation builds without warnings on both
(make -C doc/src/sgml check html).

For context, the extension is pg_repack_safe
(https://github.com/datastone-inc/pg_repack_safe): it turns the
"appears empty" read into a serialization failure. Its doc/problem.md
has longer reproducers, including the pg_dump one.

I used an AI assistant (Claude) to help write the patches and
reproducers; I have checked every statement against the 19 sources
and run the reproducers myself.

Regards,
Dave Sharpe
From 265e9f5549297b8436c84f25cba6dc62b4372561 Mon Sep 17 00:00:00 2001
From: Dave Sharpe <[email protected]>
Date: Tue, 24 Nov 2026 12:36:51 +0000
Subject: [PATCH v1 1/3] doc: REPACK (CONCURRENTLY) affects snapshots taken
 before it commits

The warning in the REPACK reference page says the table appears empty to
transactions "using a snapshot taken before repacking started".  The
affected snapshots are those taken before the repacking transaction
committed, which is what the "Caveats" section of the MVCC chapter says
for the same command.  A snapshot taken while REPACK (CONCURRENTLY) runs
reads the table as empty too: for example, a pg_dump started during the
repack, which takes its snapshot before it locks the table.

Also say how to disable the option for a whole server:
max_repack_replication_slots = 0 makes REPACK (CONCURRENTLY) fail with
"REPACK can only be used if "max_repack_replication_slots" > 0", while
REPACK without CONCURRENTLY, which needs no slot, still runs.

Reproducer (REL_19_0), two sessions:

    CREATE TABLE t (id int PRIMARY KEY);
    INSERT INTO t SELECT generate_series(1, 5);
    CREATE TABLE other (id int);

    s1: REPACK (CONCURRENTLY) t;       -- still running, e.g. waiting for
                                       -- a transaction that locked t
    s2: BEGIN ISOLATION LEVEL REPEATABLE READ;
    s2: SELECT count(*) FROM other;    -- snapshot taken after REPACK started
    s1: (REPACK commits)
    s2: SELECT count(*) FROM t;        -- 0, expected 5

Backpatch-through: 19

Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 doc/src/sgml/config.sgml     |  2 ++
 doc/src/sgml/ref/repack.sgml | 10 +++++++++-
 2 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/doc/src/sgml/config.sgml b/doc/src/sgml/config.sgml
index 28440932a73..b611057e78c 100644
--- a/doc/src/sgml/config.sgml
+++ b/doc/src/sgml/config.sgml
@@ -4832,6 +4832,8 @@ restore_command = 'copy "C:\\server\\archivedir\\%f" "%p"'  # Windows
         <para>
          Specifies the maximum number of replication slots for use of
          the <command>REPACK</command> command.  The default is 5.
+         With a value of zero, <command>REPACK</command> cannot be used
+         with the <literal>CONCURRENTLY</literal> option.
          This parameter can only be set at server start.
         </para>
        </listitem>
diff --git a/doc/src/sgml/ref/repack.sgml b/doc/src/sgml/ref/repack.sgml
index 0649e033aa1..194d65efc88 100644
--- a/doc/src/sgml/ref/repack.sgml
+++ b/doc/src/sgml/ref/repack.sgml
@@ -334,9 +334,17 @@ REPACK [ ( <replaceable class="parameter">option</replaceable> [, ...] ) ] USING
      <command>REPACK</command> with the <literal>CONCURRENTLY</literal>
      option is not MVCC-safe.  After the repacking transaction commits,
      the table will appear empty to concurrent transactions, if they are
-     using a snapshot taken before repacking started.
+     using a snapshot taken before the repacking transaction committed.
+     That includes a snapshot taken while <command>REPACK</command> was
+     running.
      See <xref linkend="mvcc-caveats"/> for more details.
     </para>
+    <para>
+     To prevent the use of the <literal>CONCURRENTLY</literal> option on a
+     server, set <xref linkend="guc-max-repack-replication-slots"/> to
+     zero.  <command>REPACK</command> without
+     <literal>CONCURRENTLY</literal> remains available.
+    </para>
    </warning>
 
    <para>

base-commit: 9ebc9a458c38465cd9609814cfc9ca3a2f46fef5
-- 
2.43.0

From 6f57c70f2ebb90947cf61a748640c0e71569ccf3 Mon Sep 17 00:00:00 2001
From: Dave Sharpe <[email protected]>
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 <[email protected]>
---
 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

From 7110d0d05ae327ff8bd77fd10357cb8a75d4092d Mon Sep 17 00:00:00 2001
From: Dave Sharpe <[email protected]>
Date: Tue, 24 Nov 2026 12:36:51 +0000
Subject: [PATCH v1 3/3] doc: REFRESH MATERIALIZED VIEW without CONCURRENTLY is
 not MVCC-safe

REFRESH MATERIALIZED VIEW without CONCURRENTLY writes the new contents
with TABLE_INSERT_FROZEN (matview.c, transientrel_startup) and swaps them
in.  Frozen rows are visible to every snapshot, so a transaction whose
snapshot was taken before the refresh committed, and which had not yet
accessed the materialized view, reads the new contents: data from after
its snapshot.

The "Caveats" section of the MVCC chapter lists the commands that are not
MVCC-safe and does not include this one, and its description ("the table
will appear empty") does not fit it.  Document it there and on the
command's reference page.  REFRESH MATERIALIZED VIEW CONCURRENTLY changes
the view with ordinary row operations and is not affected.

Reproducer (REL_19_0), two sessions:

    CREATE TABLE t (id int PRIMARY KEY);
    INSERT INTO t SELECT generate_series(1, 3);
    CREATE MATERIALIZED VIEW mv AS SELECT id FROM t;
    INSERT INTO t VALUES (4);
    CREATE TABLE other (id int);

    s1: BEGIN ISOLATION LEVEL REPEATABLE READ;
    s1: SELECT count(*) FROM other;      -- takes the snapshot
    s2: REFRESH MATERIALIZED VIEW mv;
    s1: SELECT count(*) FROM mv;         -- 4, expected 3

Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 doc/src/sgml/mvcc.sgml                          | 13 +++++++++++++
 doc/src/sgml/ref/refresh_materialized_view.sgml |  8 ++++++++
 2 files changed, 21 insertions(+)

diff --git a/doc/src/sgml/mvcc.sgml b/doc/src/sgml/mvcc.sgml
index ff01e97ea3d..808f97feb25 100644
--- a/doc/src/sgml/mvcc.sgml
+++ b/doc/src/sgml/mvcc.sgml
@@ -1904,6 +1904,19 @@ SELECT pg_advisory_lock(q.id) FROM
     table and other tables in the database.
    </para>
 
+   <para>
+    <link linkend="sql-refreshmaterializedview"><command>REFRESH
+    MATERIALIZED VIEW</command></link> without
+    the <literal>CONCURRENTLY</literal> option is not MVCC-safe either, in
+    a different way.  After the refresh commits, the materialized view
+    shows its new contents to concurrent transactions that are using a
+    snapshot taken before the refresh committed, provided they did not
+    access the materialized view before the refresh started.  Such a
+    transaction sees data in the materialized view that is newer than its
+    snapshot.  With the <literal>CONCURRENTLY</literal> option the command
+    is MVCC-safe.
+   </para>
+
    <para>
     Support for the Serializable transaction isolation level has not yet
     been added to hot standby replication targets (described in
diff --git a/doc/src/sgml/ref/refresh_materialized_view.sgml b/doc/src/sgml/ref/refresh_materialized_view.sgml
index 8ed43ade803..2dcf13cbbcd 100644
--- a/doc/src/sgml/ref/refresh_materialized_view.sgml
+++ b/doc/src/sgml/ref/refresh_materialized_view.sgml
@@ -100,6 +100,14 @@ REFRESH MATERIALIZED VIEW [ CONCURRENTLY ] <replaceable class="parameter">name</
    VIEW</command> does not guarantee to preserve that ordering.
   </para>
 
+  <para>
+   <command>REFRESH MATERIALIZED VIEW</command> without
+   the <literal>CONCURRENTLY</literal> option is not MVCC-safe.  After it
+   commits, a concurrent transaction that is using a snapshot taken before
+   that commit, and that had not yet accessed the materialized view, sees
+   the new contents.  See <xref linkend="mvcc-caveats"/> for more details.
+  </para>
+
   <para>
    While <command>REFRESH MATERIALIZED VIEW</command> is running, the <xref
    linkend="guc-search-path"/> is temporarily changed to <literal>pg_catalog,
-- 
2.43.0


--- End Message ---

Reply via email to