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
#!/bin/bash
# Noah Misch's 2012 reproducer (pgsql-hackers, "Disabled features on Hot
# Standby", <[email protected]>), run with each
# table-rewriting command: a hot standby without hot_standby_feedback, a
# REPEATABLE READ snapshot taken there before a DELETE on the primary, then
# a rewrite on the primary. Prints one line per case: the command, the rows
# deleted, how the primary read the old heap (from the command's VERBOSE
# output), and the row count the standby snapshot got or the error that
# ended its session.
# Usage: standby.sh <bindir> <workdir> <port>
set -euo pipefail
BIN=$1; W=$2; P=$3; S=$((P + 1))
export PATH=$BIN:$PATH PGHOST=/tmp PGDATABASE=postgres
rm -rf "$W"; mkdir -p "$W"
initdb -A trust -D "$W/primary" >/dev/null
pg_ctl -s -D "$W/primary" -l "$W/primary.log" \
    -o "-p $P -k /tmp -c listen_addresses='' -c autovacuum=off" start
pg_basebackup -p $P -D "$W/standby" -R
pg_ctl -s -D "$W/standby" -l "$W/standby.log" \
    -o "-p $S -k /tmp -c listen_addresses='' -c hot_standby_feedback=off" start
trap 'pg_ctl -s -D "$W/standby" -m immediate stop; pg_ctl -s -D "$W/primary" -m immediate stop' EXIT

# Run SQL on the primary, then wait until the standby has replayed it.
cat > "$W/on_primary" <<EOF
#!/bin/bash
set -e
psql -XAtq -p $P -v ON_ERROR_STOP=1 -c "\$1"
lsn=\$(psql -XAt -p $P -c 'SELECT pg_current_wal_lsn()')
until [ "\$(psql -XAt -p $S -c "SELECT pg_last_wal_replay_lsn() >= '\$lsn'")" = t ]; do sleep 0.1; done
EOF
chmod +x "$W/on_primary"

postgres --version
echo "standby: hot_standby_feedback = $(psql -XAt -p $S -c 'SHOW hot_standby_feedback')"
echo
printf '%-35s %-10s %-27s %s\n' "rewrite on the primary" "deleted" "old heap read by" "standby count(*)"

# Each table has a primary key on n. t1 to t4 hold 1000 rows in key order,
# 226 to a page, so rows 1 to 10 are on a full page and rows 991 to 1000 on
# the last page, which is less than half full. t5 holds 100000 wider rows
# in random order, for which CLUSTER chooses a sequential scan and sort.
while IFS='|' read -r t rows cols order del cmd; do
    "$W/on_primary" "CREATE TABLE $t AS SELECT $cols FROM generate_series(1, $rows) g(n) ORDER BY $order; ALTER TABLE $t ADD PRIMARY KEY (n)"
    # Clean up dead catalog rows first: their removal would otherwise
    # cancel the standby session with a recovery conflict.
    "$W/on_primary" "VACUUM (ANALYZE)"
    if out=$(psql -XAtq -p $S -v ON_ERROR_STOP=1 2>"$W/err" <<SQL
BEGIN ISOLATION LEVEL REPEATABLE READ;
SELECT 0 \g /dev/null
\! "$W/on_primary" 'DELETE FROM $t WHERE $del'
\! "$W/on_primary" '$cmd'
SELECT count(*) FROM $t;
COMMIT;
SQL
    ); then
        res="$out of $rows"
        [ "$out" = "$rows" ] || res="$res  <-- wrong result, no error"
    else
        res=$(grep -m1 -E '^(ERROR|FATAL)' "$W/err" || head -n1 "$W/err")
    fi
    how=$(grep -m1 -oE '(using index scan|using sequential scan and sort|in physical order)' "$W/err" || echo '?')
    printf '%-35s %-10s %-27s %s\n' "$cmd" "$del" "${how#using }" "$res"
done <<'CASES'
t1|1000|n|n|n <= 10|VACUUM (FULL, VERBOSE) t1
t2|1000|n|n|n <= 10|REPACK (VERBOSE) t2
t3|1000|n|n|n <= 10|CLUSTER (VERBOSE) t3 USING t3_pkey
t4|1000|n|n|n > 990|CLUSTER (VERBOSE) t4 USING t4_pkey
t5|100000|n, repeat($$x$$, 200) AS pad|random()|n <= 10|CLUSTER (VERBOSE) t5 USING t5_pkey
CASES

Reply via email to