VACUUM FULL / REPACK on the primary make hot standby queries miss rows (2012 report, still unfixed)

From: Dave Sharpe <davesharpe13(at)gmail(dot)com>
To: pgsql-hackers(at)lists(dot)postgresql(dot)org
Cc: "noah(at)leadboat(dot)com" <noah(at)leadboat(dot)com>, robertmhaas(at)gmail(dot)com
Subject: VACUUM FULL / REPACK on the primary make hot standby queries miss rows (2012 report, still unfixed)
Date: 2026-11-27 13:41:04
Message-ID: CAKFmFJ9niQECQoJ8Jviitjq_KZRqzYKMY3=NfrVm3fUao4ia-g@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

On January 2012, in thread "Disabled features on Hot Standby", Noah
Misch showed that VACUUM FULL on the primary removes rows that a hot
standby snapshot can still see, without a recovery conflict, when
hot_standby_feedback is off:

https://www.postgresql.org/message-id/20120114111719.GA1081%40tornado.leadboat.com

M: CREATE TABLE t AS SELECT * FROM generate_series(1,1000) t(n);
S: BEGIN ISOLATION LEVEL REPEATABLE READ; SELECT 0;
M: DELETE FROM t WHERE n <= 10;
M: VACUUM FULL t;
S: SELECT count(*) FROM t; -- 990, should be 1000

The agreed fix was to emit a cleanup-info record at the end of
CLUSTER, so that such snapshots conflict:

https://www.postgresql.org/message-id/CA%2BU5nM%2BAajL%3DMYEYq6LZJT9-wnfxZA71CeCKiS_PP1PU2ZgwXA%40mail.gmail.com

I don't think it was ever done: rewriteheap.c and repack.c
(which now runs VACUUM FULL, CLUSTER and REPACK) log no conflict
horizon, and the replayed AccessExclusiveLock only conflicts with
standby queries that already hold a lock on the table. PostgreSQL 19
adds a new spelling of the same rewrite, REPACK without CONCURRENTLY.

The attached script runs that sequence for each rewriting command (a
primary and one streaming standby, hot_standby_feedback = off,
autovacuum off, all other settings default). The results are the same
on 19.0 and on master (9ebc9a458c3); "old heap read by" is from the
command's VERBOSE output:

command deleted old heap read by standby count(*)
VACUUM FULL n <= 10 physical order 990 of 1000
REPACK n <= 10 physical order 990 of 1000
CLUSTER n <= 10 index scan cancelled (conflict)
CLUSTER n > 990 index scan 990 of 1000
CLUSTER n <= 10 seq scan + sort 99990 of 100000

(REPACK is without CONCURRENTLY. The last table has 100000 wider rows
in random order, so that CLUSTER chooses the sort.)

The one conflict does not come from the rewrite. An index scan of the
old heap prunes on access, and that PRUNE_ON_ACCESS record
(snapshotConflictHorizon = the DELETE's xid, per pg_waldump) is what
cancels the standby query. heap_page_prune_opt() only prunes a page
that is nearly full, so deleting rows from the last, partly filled
page instead (n > 990) gives the wrong count again, as does a CLUSTER
that sorts. Those rewrites wrote no conflict horizon for the table.

The workaround hot_standby_feedback = on avoids it (one feedback
standby holds back the primary's horizon for all of them), but
high-availability.sgml does not list table rewrites among the causes of
wrong results or conflicts, so a user has no reason to know feedback is
needed for correctness rather than only for fewer cancellations.

Two possible directions:

1. Do what was proposed in 2012: after the rewrite, log the cutoff it
used (cutoffs.OldestXmin in repack.c, copy_table_data) as a
snapshot conflict horizon for the table, so replay cancels standby
snapshots older than it. That turns the wrong answer into a
conflict, consistent with how VACUUM's pruning is handled.

2. At least document it in "Handling Query Conflicts": VACUUM FULL,
CLUSTER and REPACK (without CONCURRENTLY) on the primary can make
standby queries miss rows unless hot_standby_feedback is on.

I am happy to write a patch for either, and would like to know which
the list prefers. (REPACK CONCURRENTLY has a separate, documented
caveat on the primary; I have sent doc patches for that in
https://www.postgresql.org/message-id/CAKFmFJ9ZHmOEN%2BwkPwUiP8nB6gppQAm9sGYMMfLw%2B21FvZ-%2Bpw%40mail.gmail.com.)

I used an AI assistant (Claude) to help write the reproducer and this
message; I ran the script on both versions and checked the statements
above against the sources.

Regards,
Dave Sharpe, datastone.ca

Attachment Content-Type Size
standby.sh text/x-sh 3.2 KB

Browse pgsql-hackers by date

  From Date Subject
Previous Message Andrey Borodin 2026-11-27 13:25:29 Re: Set 1s WaitLatch timeout if standby limit has expired in ResolveRecoveryConflictWithBufferPin