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 1x9f1K-000000029Ut-1GAo for pgsql-hackers@arkaria.postgresql.org; Thu, 24 Sep 2026 08:41:34 +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 1x9f1I-0000000APLq-2LM9 for pgsql-hackers@arkaria.postgresql.org; Thu, 24 Sep 2026 08:41:32 +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 1x9f1I-0000000APLi-0vAF for pgsql-hackers@lists.postgresql.org; Thu, 24 Sep 2026 08:41:32 +0000 Received: from mail-wr2-x0f.google.com ([2a00:1450:4864:30::f]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1x9f1F-00000000zsT-1KiY for pgsql-hackers@lists.postgresql.org; Thu, 24 Sep 2026 08:41:31 +0000 Received: by mail-wr2-x0f.google.com with SMTP id ffacd0b85a97d-4858bc96fabso1534486f8f.3 for ; Thu, 24 Sep 2026 01:41:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec.at; s=google; t=1790239288; x=1790844088; darn=lists.postgresql.org; h=message-id:date:content-transfer-encoding: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=S0ddso6ao811H1+zUNP7MTj2TLrA0Vk8lpupEM/cad0=; b=cqq/lMq6FI2fYI92mrudp7GcqIwN/2edP2YRot93kgpYlo/xTThRVRYjYsRtMT6p73 Kpyx8osg6GVfzILv+L8AJPMJDanDtrCOEoh/f657hmEPZgqCq4zdHuJLpYGWrKbcPhRZ oUMIw2iZiXg9biVqv8gFmIjOqOwVzltzOnCTsZBE+DS9/SFL7PlB7pSIthd8xqKJ34MN sNpdvY9uoAiFT6bLuPMsJzpnbozYHVxj288+pAtvM0IrKd0y6NQz8E1JoK6bqQm4aEWw Iupzrh/Td2R/h68iw5uRngolmhJp/1kUZ3iNfZv0JbDqphznFX3JN5o+VMBhD+XCkpXW AI0w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1790239288; x=1790844088; h=message-id:date:content-transfer-encoding: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=S0ddso6ao811H1+zUNP7MTj2TLrA0Vk8lpupEM/cad0=; b=KgcgOeafIZW8WRjAT7kU/tFnvFbISlUuLktm7bKe1b/AkQ0Lxl/WB1zWPkQvUfd0Lu MIgoambaJB+Zi4zgJvwfb/P5L26QHAqbaKZH36IE5jjJDkzZD0hOpSzSfpSJHQEMTpy/ eSOf4ngZlc6GmocN1zfKnJ6MAzD9oGUpFrAP51yfNlVEqd7+Y/ZBVzZ/d9SjRivYVHIs 0C3wZ8OsC7NWID1rjSIaoEEvewBthy9a0xGS84FYX8QB5QT4MXHSj3u0DYhjbFICq3NX BeCkysynXaM6OtNVAed5UCZCGG+s0SpjXWWRPYoWoXpZAMaYU5n3n9zAFLC32/F5E7NM DaQw== X-Forwarded-Encrypted: i=1; AKwUvByT6nVlDZlIGjYlZ+C77kZ+U+CnLIBjlvJtGhpDM4RsIC7FC8LRH1cuO5XNbD3F/jIkMm6MROrKG+rnUzGz@lists.postgresql.org X-Gm-Message-State: AFuF++kZI2bTWnY/+pWXFb/EMZX8NCzsnYNxZUBKpoZmgsgbMGjrYpHs JwHOpvvAy1N6bwBagqjNpXHaifCtFLkkXiQmu24FtKBAcvA1+EG8U+tWUZ71WKWyQA4= X-Gm-Gg: AYBFou0pfwwmzdVYFzXM/9vuqaxZcOwQwNT4NgVDIAUwIo0NhLf66cvFhq4YA3vmfRt CQce7+nm4l0eE3zI6T6dzT1EiAqR0X6DFuDTz98hpdKj7v62OfGmdo4yyMYX01SzfWCAsBIpYOk K7uKoYxFoky0WKSHOSVNfmFiP7XaBa3U2Zz9jv67ABUV0T+5WmuDg26M84EWT9gneHRWG/bNESR uKz0e48rbn1X8II4jgGktcVBi+3oy4v4KGQ6mZjRHOrtq2Tmnogx9X2PKHL1mcVtN2tBUIi6hf0 HWTKw28DheGvj7ss8N0A2zoZpNHPeQ7LHHKLhw0lqjuhVkKrCQwtGs9HNjD5nWOWnlOGHwm+Efc dEZQ06ThJ4RqCS0uJIB/OAvQRU46J9Dl/gYelKT06ruXDjfTy9Dlwz/l7ZQjhypqNDOURwUxwPk GYQ50USOCVQZo/ObJIXAZvJyzO9X5ZXOGKRgxvgUS1sbm3zuNLFXLPGzWLfCFUmfCdxQiKLzhH3 uoHKw== X-Received: by 2002:a5d:6f04:0:b0:487:1084:10fb with SMTP id ffacd0b85a97d-48872abc6d7mr2226014f8f.36.1790239288146; Thu, 24 Sep 2026 01:41:28 -0700 (PDT) Received: from localhost (109-81-170-16.rct.o2.cz. [109.81.170.16]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4886876c64dsm13331638f8f.21.2026.09.24.01.41.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Sep 2026 01:41:27 -0700 (PDT) From: Antonin Houska To: Robert Treat cc: Masahiko Sawada , shihao zhong , Manu , Thom Brown , 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 Robert Treat message dated "Wed, 23 Sep 2026 23:51:20 -0400." X-Mailer: MH-E 8.6+git; nmh 1.8; GNU Emacs 28.3 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Thu, 24 Sep 2026 10:41:27 +0200 Message-ID: <10459.1790239287@localhost> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Robert Treat wrote: > On Wed, Sep 23, 2026 at 2:28=E2=80=AFPM Masahiko Sawada wrote: > > > > On Wed, Sep 23, 2026 at 9:23=E2=80=AFAM Antonin Houska = wrote: > > > > > > shihao zhong wrote: > > > > > > > > or whether the relfilenode should be re-checked after the snapsho= t is built > > > > > > > > Holding the toast lock from the start deadlocks. A session that ask= s for > > > > AccessExclusiveLock gets an XID before it waits, and the decoding w= orker > > > > waits for all XIDs while it sets up. > > > > > > The same (supposedly low) deadlock risk already exists for the main t= able, 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 o= n our > > > * table (e.g. it runs CREATE INDEX), we can end up in a deadloc= k. > > > * Not sure this risk is worth unlocking/locking the table (and i= ts > > > * 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 sit= uation > > > worse. > > > > Agreed. > > > > So I think the simplest fix would be to acquire a lock on the TOAST > > table before starting the repack worker. It would make the case in > > question fail with a deadlock, instead of silently losing updates. > > > > The proposed patch also fixes the problem, but I'm concerned that it > > repeatedly starts and stops the repack worker without any limit. I > > think we could error out if we detect a concurrent rewrite, so that > > users can re-run REPACK CONCURRENTLY. This check could also be done on > > the repack worker side: after getting the relfilelocator of the TOAST > > table and initializing the logical decoding, the repack worker > > rechecks the relfilelocator. If they don't match, it raises an error. > > >=20 > It feels a little off to me that if I am trying to REPACKCC, and > someone (maybe even myself, but certainly not Postgres) comes along > and runs a command the conflicts with my existing REPACKCC, that my > REPACKCC is canceled rather than having the other command either wait > or error out. pg_squeeze gives up as soon as it notices a "disrupting" catalog change. Although I haven't heard complaints about this behavior (it's proba= bly not common to run conflicting DDL commands during maintenance window), I ad= mit it's not the ideal approach. For REPACK (CONCURRENTLY), we decided to not give up voluntarily. Even if REPACK ends up in a deadlock, it still has some chance to win. The direction we took here is to adjust the deadlock detector (in future versions) so that REPACK always wins. Raising ERROR on REPACK's side in case of specific conflict would be against that strategy. (What I said does not mean that I'm in favor of restarting the decoding wor= ker either. I still prefer locking the TOAST relation early, as I noted elsewhe= re in the thread.) --=20 Antonin Houska Web: https://www.cybertec-postgresql.com