From d7a3ddfa5153013208c643103054174786b0f3a6 Mon Sep 17 00:00:00 2001 From: Mikhail Nikalayeu Date: Sun, 6 Dec 2026 15:41:58 +0100 Subject: [PATCH] Add reproducer for lost TOAST values in REPACK (CONCURRENTLY). At a block range boundary the decoding worker builds the next snapshot and advances the xmin of its replication slot before the backend has replayed the concurrent changes decoded for the previous snapshot. The backend does not advertise an xmin of its own, so during the replay nothing holds back the xmin horizon for the snapshot that is still active. The replay reads the old heap in one case: if UPDATE is converted to INSERT (the new tuple version is in the part of the table already copied, the old one is not), unchanged TOAST values are fetched from the old heap. If the row was deleted in the meantime, on-access pruning by another session can remove the chunks first, and REPACK fails with ERROR: missing chunk number 2 for toast value ... in pg_toast_... The test needs a new injection point, repack-concurrently-before-apply, to stop the backend between receiving the changes and replaying them. This is not meant for commit as is; the test fails until the problem is fixed. --- src/backend/commands/repack.c | 6 + src/test/modules/test_misc/meson.build | 1 + .../test_misc/t/016_repack_toast_horizon.pl | 154 ++++++++++++++++++ 3 files changed, 161 insertions(+) create mode 100644 src/test/modules/test_misc/t/016_repack_toast_horizon.pl diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c index 8fa6fe2bd27..1eebaa1f9db 100644 --- a/src/backend/commands/repack.c +++ b/src/backend/commands/repack.c @@ -5470,6 +5470,12 @@ repack_process_concurrent_changes(ChangeContext *chgcxt, SpinLockRelease(&shared->mutex); #endif + /* + * During testing, wait here: the decoding worker has already built the + * next snapshot and advanced the xmin of the replication slot. + */ + INJECTION_POINT("repack-concurrently-before-apply", NULL); + /* Apply the changes to the new table. */ apply_concurrent_changes(chgcxt, range_start, range_end, nblocks); diff --git a/src/test/modules/test_misc/meson.build b/src/test/modules/test_misc/meson.build index 5d81f5b13be..308301845f6 100644 --- a/src/test/modules/test_misc/meson.build +++ b/src/test/modules/test_misc/meson.build @@ -24,6 +24,7 @@ tests += { 't/013_temp_obj_multisession.pl', 't/014_log_statement_max_length.pl', 't/015_temp_schema_exit_deferrable.pl', + 't/016_repack_toast_horizon.pl', ], # The injection points are cluster-wide, so disable installcheck 'runningcheck': false, diff --git a/src/test/modules/test_misc/t/016_repack_toast_horizon.pl b/src/test/modules/test_misc/t/016_repack_toast_horizon.pl new file mode 100644 index 00000000000..c2c1383d085 --- /dev/null +++ b/src/test/modules/test_misc/t/016_repack_toast_horizon.pl @@ -0,0 +1,154 @@ +# Copyright (c) 2026, PostgreSQL Global Development Group + +# REPACK (CONCURRENTLY): TOAST values of the old heap must stay available +# until the concurrent changes of the finished block range are replayed. +# +# At a range boundary the decoding worker builds the next snapshot and +# advances the xmin of the replication slot before the backend replays the +# changes decoded for the previous snapshot. If one of those changes is an +# UPDATE converted to INSERT (new tuple version in the part of the table +# already copied, old version in the part not copied yet), the replay fetches +# the TOAST value from the old heap. If the row was deleted afterwards, +# nothing prevents the TOAST chunks from being pruned in between. +# +# The sequence at the boundary is: +# +# 1. the backend asks the worker for the changes and the next snapshot +# 2. the worker decodes the changes, builds the snapshot and advances the +# slot's xmin to the xmin of that snapshot +# 3. the backend replays the changes, still with the previous snapshot +# 4. the backend switches to the new snapshot +# +# Between 2 and 3 neither the slot nor the backend (its xmin is not set) +# holds back the xmin horizon for the previous snapshot. The test stops +# REPACK there with an injection point and lets another session prune the +# TOAST page. Without a fix, REPACK then fails with +# +# ERROR: missing chunk number 2 for toast value ... in pg_toast_... +# +# VACUUM cannot be used to remove the chunks because it conflicts with the +# lock held by REPACK, but on-access pruning by any session that reads a +# neighbouring value is enough. Note that a session only prunes if it has +# recomputed its xmin horizon after step 2; a new connection always does. + +use strict; +use warnings FATAL => 'all'; + +use PostgreSQL::Test::Cluster; +use PostgreSQL::Test::Utils; +use Test::More; + +plan skip_all => 'Injection points not supported by this build' + unless $ENV{enable_injection_points} eq 'yes'; + +my $node = PostgreSQL::Test::Cluster->new('node'); +$node->init(); +$node->append_conf( + 'postgresql.conf', q[ +wal_level = replica +autovacuum = off +]); +$node->start(); + +plan skip_all => 'Extension injection_points not installed' + unless $node->check_extension('injection_points'); + +$node->safe_psql('postgres', 'CREATE EXTENSION injection_points;'); + +# Three rows per heap page, three full pages. Each value of "t" takes three +# TOAST chunks and four chunks fill a TOAST page, so the values of adjacent +# rows share TOAST pages. Then make room for exactly one row in block 0. +$node->safe_psql( + 'postgres', q[ +CREATE TABLE repack_test(i int PRIMARY KEY, pad text, t text); +ALTER TABLE repack_test ALTER COLUMN pad SET STORAGE PLAIN; +ALTER TABLE repack_test ALTER COLUMN t SET STORAGE EXTERNAL; +INSERT INTO repack_test + SELECT g, repeat('p', 2500), repeat(chr(64 + g), 5988) + FROM generate_series(1, 9) g; +DELETE FROM repack_test WHERE i = 1; +]); +$node->safe_psql('postgres', 'VACUUM repack_test;'); + +is( $node->safe_psql( + 'postgres', 'SELECT tid_block(ctid) FROM repack_test WHERE i = 7'), + '2', + 'row 7 starts in block 2'); + +my $expected = $node->safe_psql('postgres', + 'SELECT i, md5(t) FROM repack_test WHERE i <> 7 ORDER BY i'); + +$node->safe_psql( + 'postgres', q[ +SELECT injection_points_attach('repack-concurrently-new-range', 'wait'); +SELECT injection_points_attach('repack-concurrently-before-apply', 'wait'); +]); + +# Use one snapshot per block. +my $repack = $node->background_psql('postgres', on_error_stop => 0); +$repack->query_until( + qr/starting_repack/, q[ +\echo starting_repack +SET repack_snapshot_after = 1; +REPACK (CONCURRENTLY) repack_test; +]); + +# Let REPACK finish the first range (block 0) completely. The two injection +# points alternate, so each wait below is for a different wait event than the +# one we have just woken up. +$node->wait_for_event('client backend', 'repack-concurrently-new-range'); +$node->safe_psql('postgres', + "SELECT injection_points_wakeup('repack-concurrently-new-range')"); +$node->wait_for_event('client backend', 'repack-concurrently-before-apply'); +$node->safe_psql('postgres', + "SELECT injection_points_wakeup('repack-concurrently-before-apply')"); + +# REPACK has now copied block 1 and is about to finish the second range. +$node->wait_for_event('client backend', 'repack-concurrently-new-range'); + +# Row 7 is in block 2, which was not copied yet. The new version goes to +# block 0, which was, and it keeps the TOAST pointer. +$node->safe_psql('postgres', + "UPDATE repack_test SET pad = repeat('q', 2500) WHERE i = 7"); +is( $node->safe_psql( + 'postgres', 'SELECT tid_block(ctid) FROM repack_test WHERE i = 7'), + '0', + 'new version of row 7 is in block 0'); + +# Delete the row and make sure that the next snapshot built by the decoding +# worker has xmin beyond the deleting transaction. +$node->safe_psql( + 'postgres', q[ +DELETE FROM repack_test WHERE i = 7; +SELECT pg_log_standby_snapshot(); +CREATE TABLE flush_wal(); +]); + +# Let REPACK receive the changes and the next snapshot, and stop it before +# the changes are replayed. +$node->safe_psql('postgres', + "SELECT injection_points_wakeup('repack-concurrently-new-range')"); +$node->wait_for_event('client backend', 'repack-concurrently-before-apply'); + +# The last chunk of row 7 shares a TOAST page with row 8, so reading row 8 +# prunes it if the xmin horizon allows. +$node->safe_psql('postgres', 'SELECT md5(t) FROM repack_test WHERE i = 8'); + +# Let REPACK run to completion. +$node->safe_psql( + 'postgres', q[ +SELECT injection_points_detach('repack-concurrently-new-range'); +SELECT injection_points_detach('repack-concurrently-before-apply'); +SELECT injection_points_wakeup('repack-concurrently-before-apply'); +]); +$repack->query_until(qr/repack_done/, "\\echo repack_done\n"); +my $stderr = $repack->{stderr}; +$repack->quit; + +is($stderr, '', 'REPACK (CONCURRENTLY) completed without error'); +is( $node->safe_psql( + 'postgres', 'SELECT i, md5(t) FROM repack_test ORDER BY i'), + $expected, + 'table contents after REPACK'); + +done_testing(); -- 2.43.0