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 1x9eL9-0000000299s-1ZUS for pgsql-hackers@arkaria.postgresql.org; Thu, 24 Sep 2026 07:57:59 +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 1x9eL8-0000000A3hn-2fkJ for pgsql-hackers@arkaria.postgresql.org; Thu, 24 Sep 2026 07:57:58 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1x9eL8-0000000A3he-1Cng for pgsql-hackers@lists.postgresql.org; Thu, 24 Sep 2026 07:57:58 +0000 Received: from mail-wm2-x10.google.com ([2a00:1450:4864:31::10]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1x9eL6-000000011TB-00y1 for pgsql-hackers@lists.postgresql.org; Thu, 24 Sep 2026 07:57:57 +0000 Received: by mail-wm2-x10.google.com with SMTP id 5b1f17b1804b1-49cd5462b69so9740745e9.1 for ; Thu, 24 Sep 2026 00:57:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec.at; s=google; t=1790236673; x=1790841473; darn=lists.postgresql.org; h=message-id:date:content-transfer-encoding:content-id:content-type :mime-version:comments:references:in-reply-to:subject:cc:to:from :from:to:cc:subject:date:message-id:reply-to:content-type; bh=wciNmcHoHrjqdwZ7ZtM7Q8afAHOVu+jSZFSh4iBuKDc=; b=Ntc7L4p3uM40/qSIk/ZmzazEldSCQujJFb7VDZpMnvtkvXIbyrs13SBgBl7WG+5y6m tMOcTdYB7ankHQSXg5pMoqimCrfXskRqqNXRY6Uawio4vDoNWyoHQ/bS/I3UmGfrRqCi RrUXsLE2DoAN5OdXj5LnVL4Vp70SPGe1n/QgIE355ao/UFYaVjzYGmzKpSUdI9Gicdn8 JNUAPIzUfrO1Ec/q8537xWWNDh3qe7d1MCHkdpDGO+9uojybGgxzI1jNod3T5DLcSPAW WR3SCVvpR5mH+mEyYuDz7LfEFb1oiDTFGnQ2hcw6L4yOBdXmv0W3t4NB4eghrysM9WRT pBUw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1790236673; x=1790841473; h=message-id:date:content-transfer-encoding:content-id:content-type :mime-version:comments:references:in-reply-to:subject:cc:to:from :x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to:content-type; bh=wciNmcHoHrjqdwZ7ZtM7Q8afAHOVu+jSZFSh4iBuKDc=; b=miOoAl5V3AO8wAkTsAILWVJbD1wRmvNqKzfKr/au803oHWiD/qBwErPcHlbCv5ucFP lsc802e+NfBzjVyP1LV9DsQTCDBnhnDxxe7Ym8/wgYcSFVA6Ld6OMPyNco04t6Bi3qAa fhxPb809DWOCwDdKB/YpdatnbgUuluQZNfO1nhDuvPdBRP7QuE5eCn8nmH0sZP2jtu/0 y/vz/mlcMWwSKDDr6k5LJuyiP3sbo0NH98x32aWSHqDjNdeuAHp0NO/JbXj8JzbFFBrL 6HPi//eo/yhFw4I22NE0se4VNNl25S9suUo76D0+0iBGRCUNHkLj3ohcTejP8v0VwGbB pV3g== X-Forwarded-Encrypted: i=1; AKwUvByRAv9MRvAqNTyszYlkAzGDyHC7jy/eUoWPtxTcEPbOzeuEPZl9hraYZn2ALLIWNHpkVR8KnYUbY6u2Kh3e@lists.postgresql.org X-Gm-Message-State: AFuF++mmPrO9IaM+RnFMFkI7qBCQxSnvPDIlDPF8EG/HYO3V7NImXCVZ FotI0iP8IsVOlzf1Ofe55lRXR9fRPodGJVdWmVnNElwSjM1EIB1GnxeFxrPA3n38VtM= X-Gm-Gg: AYBFou1UEFOcRBp+k87S4EUSGpeRSMKwiB1bkue56OcmT/smdDhn9rXc6AV2FqWVSkH CeR5xslzdHTDvX4vy0kibpSpw93mhbAx2hfT6TFj74cmRqmwzz0LqK62mlMLAf9v9Eh5MdTYS2g lzNGaOPjK6tN5saPdJ3EORCNw0mpWbmYiDhJ8DOjP1Yb5o5tQdHz4TNYcrCSjlZM2yVOMNm4eOL Kp82+DjMHT4+2OHgH2C4MCSPLcTJBmwrTyuuJFte47bCHJ/1+6mkDrcSl/xACZTgySU3t/b3YBo 75FcQcV2u7sqVSsiwGLtO3zXwTJ6vF4rh/D0GdZ9/1PQXCuXeUjpZJifObAod7L/2Q0Ri4Vknk/ jYQ/C5v+DiUJLxDqqhVOrxM3Uj8jfkW/97Eo4jgxdnU3bBVeenZ70R+GtBg2xxPWyQGf420QlTa ChDsx5e7rwQ7k+pEuEA24fVs+YXc8/WBQiJKQ4gHL1wjlVN+p5hdfsBjHjRC9hGIHR598UmHaOY BcoZN0DgA== X-Received: by 2002:a05:600c:4e14:b0:49f:ce78:3563 with SMTP id 5b1f17b1804b1-49fe66f1a7bmr24002635e9.20.1790236673137; Thu, 24 Sep 2026 00:57:53 -0700 (PDT) Received: from localhost (109-81-170-16.rct.o2.cz. [109.81.170.16]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-49fe5cd42f2sm46018755e9.15.2026.09.24.00.57.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Sep 2026 00:57:52 -0700 (PDT) From: Antonin Houska To: Thom Brown cc: shihao zhong , Manu , pgsql-hackers@lists.postgresql.org Subject: Re: REPACK (CONCURRENTLY) can silently lose updates when the toast table is rewritten In-reply-to: References: <179012413951.1850281.5077495683381671561@gmail.com> <47479.1790180571@localhost> Comments: In-reply-to Thom Brown message dated "Wed, 23 Sep 2026 18:18:20 +0100." X-Mailer: MH-E 8.6+git; nmh 1.8; GNU Emacs 28.3 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <8576.1790236672.1@localhost> Content-Transfer-Encoding: quoted-printable Date: Thu, 24 Sep 2026 09:57:52 +0200 Message-ID: <8577.1790236672@localhost> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Thom Brown wrote: > On Wed, 23 Sept 2026 at 17:22, Antonin Houska wrote: > > > > shihao zhong wrote: > > > > > > or whether the relfilenode should be re-checked after the snapshot= is built > > > > > > Holding the toast lock from the start deadlocks. A session that asks= for > > > AccessExclusiveLock gets an XID before it waits, and the decoding wo= rker > > > waits for all XIDs while it sets up. > > > > The same (supposedly low) deadlock risk already exists for the main ta= ble, see > > this comment in rebuild_relation(): > > > > /* > > * Start the worker that decodes data changes applied while we're > > * copying the table contents. > > * > > * 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 it= s > > * clustering index) and checking again if it's still eligible for > > * REPACK CONCURRENTLY. > > */ > > start_repack_decoding_worker(tableOid); > > > > I'm not sure if locking the TOAST relation earlier would make the situ= ation > > worse. > > > > The reason TOAST relation is not locked until copy_table_data() does s= o is > > that CLUSTER / VACUUM FULL in v18 did it this way (not sure what the r= eason > > for such design was). I haven't changed that for REPACK exactly becaus= e I > > failed to envision this stale relfilenode issue. > = > I gave that a try, and it does. It just swaps the lost update for a dead= lock. > = > If you lock the toast up front and something rewrites it at the same > time (which is the thing that triggers this in the first place, e.g. a > REPACK of the toast table), REPACK falls over: > = > Session 1: > BEGIN; > INSERT INTO test VALUES (999999, 'x'); > = > Session 2: > REPACK (CONCURRENTLY) test; > = > Session 1: > CREATE INDEX ON test (big); > = > ERROR: deadlock detected > DETAIL: Process 214534 waits for ShareLock on transaction 1774005; > blocked by process 214579. > Process 214579 waits for AccessExclusiveLock on relation 3672470 of > database 5; blocked by process 214534. > CONTEXT: REPACK decoding worker > = > The rewrite already has an XID by the time it waits, and the worker > waits for that XID whilst it sets up, so the two just sit on each > other. It doesn't matter which lock we take either because anything > that would stop the rewrite conflicts with it. IMO this example does not exactly demonstrate the problem described in the comment above: if REPACK (CONCURRENTLY) waits for AccessExclusiveLock, it'= s going to perform the relation swap, so the worker should already be gone. On the other hand, the message "Process ... waits for ShareLock on transaction ..." is what the deadlock detector would report for the decoding worker. Howeve= r, where would the request for AccessExclusiveLock come from in that case? CR= EATE INDEX only uses it to lock the new index relation, however that cannot be locked by other backends until the transaction has committed (because it's= not visible before commit). What exactly have you changed in the code? -- = Antonin Houska Web: https://www.cybertec-postgresql.com