Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xA7MO-00000002Sh7-0FgL for pgsql-hackers@arkaria.postgresql.org; Fri, 25 Sep 2026 14:57:12 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xA7MN-00000001IUC-1Yqq for pgsql-hackers@arkaria.postgresql.org; Fri, 25 Sep 2026 14:57:11 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xA7MM-00000001IU1-016i for pgsql-hackers@lists.postgresql.org; Fri, 25 Sep 2026 14:57:11 +0000 Received: from fout-a6-smtp.messagingengine.com ([103.168.172.149]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xA7MH-00000001DIk-2oc5 for pgsql-hackers@lists.postgresql.org; Fri, 25 Sep 2026 14:57:09 +0000 Received: from phl-compute-05.internal (phl-compute-05.internal [10.202.2.45]) by mailfout.phl.internal (Postfix) with ESMTP id 8E959EC0190; Fri, 25 Sep 2026 10:57:04 -0400 (EDT) Received: from phl-frontend-04 ([10.202.2.163]) by phl-compute-05.internal (MEProxy); Fri, 25 Sep 2026 10:57:04 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kurilemu.de; h= cc:cc:content-transfer-encoding:content-type:content-type:date :date:from:from:in-reply-to:in-reply-to:message-id:mime-version :reply-to:subject:subject:to:to; s=fm3; t=1790348224; x= 1790434624; bh=J4/EWM4MHZl9B4BdaOS3bbwuywSz2rdr4FGahj98qQ0=; b=v g+pJ7Tiw2M2GRph3vnZIYbyOVY/hTQe3XYEJNI0pIPUxCgqqt0H5Csy4xCCU5Ca8 UqWaCFRsnxHulACx8ACARQRk+Xt8eex8mcUJso52GN8Xdc5MokidZuRIboA0fmuK rlYdKzNjYZ0YhGsvXUXSOxS/6mm0EgL6Tm8zyt0KHILM6mB3fBiVzY1ORnhV21g9 R2zVDotN4Uy9GRkidwoCDIr5dg3zQ2dQq4VIBWHoTOfoEe3tvRroeXC7dYJPaAHd qf1KIJ6+HYM1nYzztP1dLu+RzsCXI0dwxSVMaUf0isHoA2ijCQKC/opMbsAE+WMA xqSTY9X1oEIV0+Cr9Nw8A== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:feedback-id:feedback-id :from:from:in-reply-to:in-reply-to:message-id:mime-version :reply-to:subject:subject:to:to:x-me-proxy:x-me-sender :x-me-sender:x-sasl-enc; s=fm1; t=1790348224; x=1790434624; bh=J 4/EWM4MHZl9B4BdaOS3bbwuywSz2rdr4FGahj98qQ0=; b=tU5ug30PArRoOJzWk lHh/uZOgGDIfY/V/KKVhnfVL+0e9MKUfuu1A4AA5vyqsVnU3rdI0QnqUDq98lcx2 9Ggzzlih/sv/WtZHd1HNJalJRBW7VoGKNhuH2POBSpCc6AeFMGkhN6EPaCIKThzh uN4Xr5/qQFEzmKdV35NBptzv714C119qRcET2o4vBCWmeqRp9Z9N6ibXU5eUXjGQ ikco+jj7Ep8X3LWXfoJHYBRzOOntfZSHXoAv0Kxib6FG3F8sC6oxcobSfsDpQVRY I1oFcgD0kUFBoo8dM2MOF7K5xXoBizvsMhZVFoWRjxFtCmRDhZPCoQSnfUDUOJ8Y eb2KQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: dmFkZTGjRfp+RyQ3YHP8NDIB/Ir2PmVU9jEJ2UIlSPoQ7MbOqfr7xOo2BqgM7M4EdyDj8u wp6LfHQ40pcXR7k41FqAQ8evstgKiushTK9a7QuhXlLrZcuHY9UZJStOLr34lI4t17OjJN AcRGy2vMPsZNrhW1+fjgJVIRZ/jFKSEn9klRUhz7OCED1XbH+2b5afhsZSW9LxGeF1GlGJ MCQ9jKo4iWN74ed0YNebIitQ4TG74VwuiuQ1XucsRvGmx39JNonhft3acuyeOC1gwOQTfF UwjR76zq3NwYhqt2uGXTcC8dgv87EgCch7m+o4VJJx2ckYS35ppGNMMj6fWnI3M794iAm6 sirwUJ0pogk7cP20OmmdwB9hnccWLfT3Sz3tXF1rmtWYJPy1arZOmH7CS3K884VB+z810k 8uLixaFfFNlMOdEIct7yPqWJk1uzBqeX/qbuxXW/fUiKFXmM+3THdL+eRsc1wK3P1lfx+X UzNLtL6EBKneBvlDHBqznMMm5LDlPxMYXw1GbE81iPiJ9y05mdJeKj0leMQYbA1HBYbmcr CLLX5c8OWkMzNU3fbSGJ5tEJsYaUwDLxZrbArvwHO/gAtPEyk+XS8cmRXAFmNFqE7H7x86 x+sQU4huL12nNDD2E7Ewao79DQzDeBXgxcAV46EzMfUjL53nn/QsxU3xtdbA X-ME-Proxy: Feedback-ID: ie3de48e3:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 25 Sep 2026 10:57:03 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kurilemu.de; s=schmee; t=1790348218; bh=GlaMDKtXExwFZX5DfPio6Jh2Mn/Jkv3iDjCa//j37Xc=; h=Date:From:To:Cc:Subject:In-Reply-To:From; b=Ns4w9Qvs+rc69x/Ss9PHLkP9io7sgQ0X5H7xtl6I7mzWE73a9ZqgDaNkrJdqWbEHE lm/Ij4rWcwLd7Vte304y8Xnz09enJzSMoq7yp5G9saESBrZ8sUSlr56qCAKZNjXfGY +azvMpqkwfGEp6HrnpJ1QGwltKbLe3trf3Iq1LSufhpsL9s478onrU2MDocoMp4wgq oHLX44eg+5YTPsf/0oHJH2gDJHbNTrbx2W36Wi+lPO1ypvsIVrHVEiz2y0xE7K560o a8p+G+MZTlV3CPuRrZV2wyoLg71bcFzoIOyKNdJQvWHP6rr1LI9VuYakAC7cm1nRLI ALUPSaTwMMs4w== Received: by ida.kurilemu.internal (Postfix, from userid 1000) id C812AB00096; Fri, 25 Sep 2026 16:56:58 +0200 (CEST) Date: Fri, 25 Sep 2026 16:56:58 +0200 From: =?utf-8?Q?=C3=81lvaro?= Herrera To: Thom Brown Cc: Antonin Houska , shihao zhong , Robert Treat , Masahiko Sawada , Manu , pgsql-hackers@lists.postgresql.org Subject: Re: REPACK (CONCURRENTLY) can silently lose updates when the toast table is rewritten Message-ID: MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="nyjzce72b5mmvfal" Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --nyjzce72b5mmvfal Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit On 2026-Sep-25, Thom Brown wrote: > Moving it into cluster_rel() makes sense, but does it need to be the > full ShareUpdateExclusiveLock there? Yeah, it's better to acquire the lock you want upfront, because otherwise you introduce more risk of deadlock caused by lock upgrades (admittedly the user would have to be doing something really stupid in order for this to be a real problem, but still.) It's only AEL that we don't want to hold for long. Maybe the patch could be somewhat like this, then? I didn't review the test carefully other than running without the code fix to verify that it fails, and then passes with the fix; and I didn't read the commit messages either, which I think are LLM-written and not really correct. (Also, I would push both things as a single commit.) I think changing the lock as obtained by copy_table_data is not very nice, because that one is unconditional, and here we only want it in concurrent mode. BTW I noticed that the comment for copy_table_data mentions decoding_ctx as an argument, which doesn't exist. -- Álvaro Herrera 48°01'N 7°57'E — https://www.EnterpriseDB.com/ "Nunca se desea ardientemente lo que solo se desea por razón" (F. Alexandre) --nyjzce72b5mmvfal Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename=v4-0001-Test-TOAST-rewrite-during-REPACK-CONCURRENTLY-sta.patch From e0ec78b896ddae6be382371e2132dc1bb9cfe08c Mon Sep 17 00:00:00 2001 From: Shihao Date: Thu, 24 Sep 2026 23:46:58 -0400 Subject: [PATCH v4 1/2] Test TOAST rewrite during REPACK (CONCURRENTLY) startup Add a permutation to repack_toast.spec that tries to rewrite the TOAST relation while the decoding worker waits for a running transaction. The rewrite has to fail on lock_timeout, and REPACK has to keep all the concurrent changes. Without the fix, the rewrite succeeds and the concurrent updates of TOASTed columns are lost. Discussion: https://postgr.es/m/CAA-aLv5MF6BLL+BWvix2Yw+CBardtH43AofPReQunhDZPNBtuA@mail.gmail.com --- .../expected/repack_toast.out | 151 +++++++++++++++++- .../injection_points/specs/repack_toast.spec | 51 ++++++ 2 files changed, 201 insertions(+), 1 deletion(-) diff --git a/src/test/modules/injection_points/expected/repack_toast.out b/src/test/modules/injection_points/expected/repack_toast.out index 95e7b19893e..756e7e8187f 100644 --- a/src/test/modules/injection_points/expected/repack_toast.out +++ b/src/test/modules/injection_points/expected/repack_toast.out @@ -1,4 +1,4 @@ -Parsed test spec with 2 sessions +Parsed test spec with 3 sessions starting permutation: s1_wait_before_lock s2_updates s2_check s2_wakeup_before_lock s1_check injection_points_attach @@ -124,3 +124,152 @@ injection_points_detach (1 row) + +starting permutation: s2_begin s1_wait_before_lock s3_rewrite_toast s3_noop s2_commit s2_updates s2_check s2_wakeup_before_lock s1_check +injection_points_attach +----------------------- + +(1 row) + +step s2_begin: + BEGIN; + SELECT pg_current_xact_id() IS NOT NULL AS has_xid; + +has_xid +------- +t +(1 row) + +step s1_wait_before_lock: + REPACK (CONCURRENTLY) repack_toast; + +step s3_rewrite_toast: + DO $$ + BEGIN + EXECUTE format('REPACK %s', + (SELECT reltoastrelid::regclass FROM pg_class + WHERE relname = 'repack_toast')); + END; + $$; + +step s3_rewrite_toast: <... completed> +ERROR: canceling statement due to lock timeout +step s3_noop: +step s2_commit: + COMMIT; + +step s2_updates: + DELETE FROM repack_toast WHERE i=1; + INSERT INTO repack_toast(i, j, k) VALUES (1, gen_external(), gen_compressible(1)); + + -- existing toast data unchanged. (This covers the case where we + -- adjust the toast pointer.) + UPDATE repack_toast SET i=i+300 where i % 10 = 2 RETURNING OLD.i, NEW.i; + + -- "j" is here an external indirect, written to the file separately. + UPDATE repack_toast SET j=gen_external() where i % 10 = 3 RETURNING OLD.i, NEW.i; + + -- the updated value of "j" is compressed. + UPDATE repack_toast SET j=gen_compressible(1), k=k||'' where i % 10 = 4 RETURNING i; + + -- the updated value of "j" is compressed externally. + UPDATE repack_toast SET j=gen_compressible_external(2) where i % 10 = 5 RETURNING i; + + -- the updated value of "j" stays inline. + UPDATE repack_toast SET j=gen_inline(), k=repeat(k,5) where i % 10 = 6 RETURNING i; + + -- updated value of "j" is a short varlena; "k" is written separately. + UPDATE repack_toast SET j=gen_short(), k=gen_external() where i % 10 = 7 RETURNING i; + + i| i +--+--- + 2|302 +12|312 +(2 rows) + + i| i +--+-- + 3| 3 +13|13 +(2 rows) + + i +-- + 4 +14 +(2 rows) + + i +-- + 5 +15 +(2 rows) + + i +-- + 6 +16 +(2 rows) + + i +-- + 7 +17 +(2 rows) + +step s2_check: + INSERT INTO relfilenodes(node) + SELECT c2.relfilenode + FROM pg_class c1 JOIN pg_class c2 ON c2.oid = c1.oid OR c2.oid = c1.reltoastrelid + WHERE c1.relname='repack_toast'; + + INSERT INTO data_s2(i, j, j_toast, k, k_toast) + SELECT i, j, COALESCE(pg_column_toast_chunk_id(j), 0) AS j_toast, + k, COALESCE(pg_column_toast_chunk_id(k), 0) AS k_toast + FROM repack_toast; + +step s2_wakeup_before_lock: + SELECT injection_points_wakeup('repack-concurrently-before-lock'); + +injection_points_wakeup +----------------------- + +(1 row) + +step s1_wait_before_lock: <... completed> +step s1_check: + INSERT INTO relfilenodes(node) + SELECT c2.relfilenode + FROM pg_class c1 JOIN pg_class c2 ON c2.oid = c1.oid OR c2.oid = c1.reltoastrelid + WHERE c1.relname='repack_toast'; + + SELECT count(DISTINCT node) FROM relfilenodes; + + INSERT INTO data_s1(i, j, j_toast, k, k_toast) + SELECT i, + j, COALESCE(pg_column_toast_chunk_id(j), 0) AS j_toast, + k, COALESCE(pg_column_toast_chunk_id(k), 0) AS k_toast + FROM repack_toast; + + -- this should be empty + SELECT d1.i, substring(d1.j FOR 12) AS d1_j, substring(d1.k FOR 12) AS d1_k, + d2.i, substring(d2.j FOR 12) AS d2_j, substring(d2.k FOR 12) AS d2_k, + d1.j_toast as d1_j_tst, d2.j_toast as d2_j_tst, + d1.k_toast as d1_k_tst, d2.k_toast AS d2_k_tst + FROM data_s1 d1 FULL JOIN data_s2 d2 USING (i, j, k) + WHERE d1.i ISNULL OR d2.i ISNULL; + +count +----- + 4 +(1 row) + +i|d1_j|d1_k|i|d2_j|d2_k|d1_j_tst|d2_j_tst|d1_k_tst|d2_k_tst +-+----+----+-+----+----+--------+--------+--------+-------- +(0 rows) + +injection_points_detach +----------------------- + +(1 row) + diff --git a/src/test/modules/injection_points/specs/repack_toast.spec b/src/test/modules/injection_points/specs/repack_toast.spec index cc8f034d016..a105a44848e 100644 --- a/src/test/modules/injection_points/specs/repack_toast.spec +++ b/src/test/modules/injection_points/specs/repack_toast.spec @@ -125,6 +125,18 @@ teardown session s2 +# Keep a transaction with XID open, so that the decoding worker has to wait +# before it can build the initial snapshot. +step s2_begin +{ + BEGIN; + SELECT pg_current_xact_id() IS NOT NULL AS has_xid; +} +step s2_commit +{ + COMMIT; +} + # Test different kinds of toast data changes. step s2_updates { @@ -170,6 +182,32 @@ step s2_wakeup_before_lock SELECT injection_points_wakeup('repack-concurrently-before-lock'); } +# Try to rewrite the TOAST relation. The decoding worker only decodes the +# changes of the TOAST relation stored under the relfilenumber it saw when +# starting, so REPACK must not let the TOAST relation be rewritten after that. +# Otherwise the TOAST chunks of the concurrent changes are not decoded, and the +# changes are lost. +# +# The name of the TOAST relation is only known at run time, hence the DO +# block, and REPACK rather than VACUUM FULL, which cannot run in one. +# +# Don't wait for the lock. The rewrite gets an XID before it waits, and once +# s2 commits, the decoding worker would wait for that XID, which is a deadlock. +session s3 +setup { SET lock_timeout = 10; } +step s3_rewrite_toast +{ + DO $$ + BEGIN + EXECUTE format('REPACK %s', + (SELECT reltoastrelid::regclass FROM pg_class + WHERE relname = 'repack_toast')); + END; + $$; +} +# Empty step, so that s2 cannot go on until s3_rewrite_toast is done. +step s3_noop { } + # Test if data changes introduced while one session is performing REPACK # CONCURRENTLY find their way into the table. permutation @@ -178,3 +216,16 @@ permutation s2_check s2_wakeup_before_lock s1_check + +# Same, but try to rewrite the TOAST relation while the decoding worker waits +# for s2 to commit. +permutation + s2_begin + s1_wait_before_lock + s3_rewrite_toast(*) + s3_noop + s2_commit + s2_updates + s2_check + s2_wakeup_before_lock + s1_check -- 2.47.3 --nyjzce72b5mmvfal Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename=v4-0002-Fix-REPACK-CONCURRENTLY-losing-updates-after-a-TO.patch From 85b2bcff30c776d8487fe686e83594a034af4da6 Mon Sep 17 00:00:00 2001 From: Shihao Date: Thu, 24 Sep 2026 23:46:58 -0400 Subject: [PATCH v4 2/2] Fix REPACK (CONCURRENTLY) losing updates after a TOAST rewrite The decoding worker of REPACK (CONCURRENTLY) remembers the relfilenumber of the TOAST relation when it starts, and only decodes the TOAST changes stored under it. The backend did not lock the TOAST relation until it started to copy the data. In between, the worker waits for running transactions to finish, so the gap can be long. If the TOAST relation was rewritten in that gap, for example by VACUUM FULL run on it directly, the TOAST chunks of concurrent updates were filtered out. An updated value then reached the apply phase as a plain on-disk TOAST pointer, which the apply code takes as a sign that the column did not change. So it kept the old value, and the committed update was lost with no error. Fix by locking the TOAST relation before the worker starts, like the table itself. A rewrite that comes during the startup now waits for REPACK. The rewrite has an XID by then, so if the worker still waits for running transactions, the two can deadlock, and the deadlock detector cancels one of them. DDL on the table itself has the same risk already. Backpatch to v19, where REPACK (CONCURRENTLY) was introduced. Reported-by: Thom Brown Discussion: https://postgr.es/m/CAA-aLv5MF6BLL+BWvix2Yw+CBardtH43AofPReQunhDZPNBtuA@mail.gmail.com --- src/backend/commands/repack.c | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c index b748426930a..386797915be 100644 --- a/src/backend/commands/repack.c +++ b/src/backend/commands/repack.c @@ -536,6 +536,15 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid, if (concurrent) check_concurrent_repack_requirements(OldHeap, &ident_idx); + /* + * In concurrent mode, also lock the toast table. Otherwise it would be + * possible for the toast relfilenode to change (e.g. because VACUUM FULL + * is run on it), and then logical decoding would fail to detect any + * concurrent changes there. + */ + if (concurrent && OidIsValid(OldHeap->rd_rel->reltoastrelid)) + LockRelationOid(OldHeap->rd_rel->reltoastrelid, lmode); + /* * Also check the state of indexes; this can abort the command for REPACK. * Historically this hasn't affected CLUSTER or VACUUM FULL, so don't do @@ -1136,6 +1145,11 @@ rebuild_relation(Relation OldHeap, Relation index, bool verbose, */ BecomeLockGroupLeader(); + /* If there is a toast table, it must have been locked already */ + Assert(!OidIsValid(OldHeap->rd_rel->reltoastrelid) || + CheckRelationOidLockedByMe(OldHeap->rd_rel->reltoastrelid, + lmode, false)); + /* * Start the worker that decodes data changes applied while we're * copying the table contents. @@ -1143,10 +1157,10 @@ rebuild_relation(Relation OldHeap, Relation index, bool verbose, * Note that the worker has to wait for all transactions with XID * already assigned to finish. If some of those transactions is * waiting for a lock conflicting with ShareUpdateExclusiveLock on our - * table (e.g. it runs CREATE INDEX), we can end up in a deadlock. - * Not sure this risk is worth unlocking/locking the table (and its - * clustering index) and checking again if it's still eligible for - * REPACK CONCURRENTLY. + * table or its TOAST relation (e.g. it runs CREATE INDEX), we can + * end up in a deadlock. Not sure this risk is worth unlocking/locking + * the table (and its clustering index) and checking again if it's + * still eligible for REPACK CONCURRENTLY. */ start_repack_decoding_worker(tableOid); -- 2.47.3 --nyjzce72b5mmvfal--