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.96) (envelope-from ) id 1wAhPP-000En0-1N for pgsql-hackers@arkaria.postgresql.org; Thu, 09 Apr 2026 04:54:27 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wAhPM-003iww-1u for pgsql-hackers@arkaria.postgresql.org; Thu, 09 Apr 2026 04:54:25 +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.96) (envelope-from ) id 1wAhPM-003iwo-0q for pgsql-hackers@lists.postgresql.org; Thu, 09 Apr 2026 04:54:25 +0000 Received: from mail-lj1-x236.google.com ([2a00:1450:4864:20::236]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1wAhPL-000000007ft-0VKz for pgsql-hackers@lists.postgresql.org; Thu, 09 Apr 2026 04:54:24 +0000 Received: by mail-lj1-x236.google.com with SMTP id 38308e7fff4ca-38e0ef48ac0so4221861fa.3 for ; Wed, 08 Apr 2026 21:54:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1775710459; cv=none; d=google.com; s=arc-20240605; b=RZ6LuQgXZ8rWufn21MKd1GocnrA3ra5EXOccwlIBfuNK12+fuF8sC4AvxQdXyOF3Sr 2wYhlOqiUzpL7AsriJTjKAEw+sbxCLNDIB/yrugHco9j3WUUUmbyXqxfJXx3LcH0Mpd0 ty6GnJgAVAITGV8wFZGiVNWLu1ZvgoYQR1qLbsRRlPpneGG2THTIn2sND4cH+Q2oTQZq 4cCm4h8egOCUplzzODyFDICkM/ss1g5Fh3/UKtk8J51QQf1wIKbRnhtZoP6Bgz2fwrVA 2Xf4X9mv2y+L9Hmo1HhSdV/ak9vjtJb/G04z0ruLzAoqTgqIpP27R31/Ygn1J1pg6Rgp eULw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=xqJZt/AlaYxqLJZWCTguAzsTlD4gfP6T0rzJECviYqc=; fh=wKuNmjLdEGDq/ztFY3v1OH7eMx7X9OdTZQ3Z80UDWgQ=; b=S98MJeNxqmeFJ6w5jZXbZFd17gjVEcUQmzgzadmeFMx7QxMxxxNwmgxrc/0ESTtYay qMWBIz8bHtg4VMuYcI7z3sssQF9ryoSboC7p7MFdU692uHaZroL8onftNCbUBo4zoCkq d4Qp2ZLbDJjzCKtrdveYExqnDj9dTE9LbqQostCSrE2UgniPIQfI1bJknOsIEc/QiGTz DjYa3iP/lwo/ygAHZhKRzY+36vURQS/blpsS8Mzx/uzFPb23Av6GWrPyMnWKoUuEJymm qdeTCIv7PaCzkuwssSZkSCl52fomqT6rhxAEQHPRcN1Qdj2G3Pr33iqqR1YE97sFLkU3 3OIQ==; darn=lists.postgresql.org ARC-Authentication-Results: i=1; mx.google.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1775710459; x=1776315259; darn=lists.postgresql.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=xqJZt/AlaYxqLJZWCTguAzsTlD4gfP6T0rzJECviYqc=; b=YgIm6X79kwUmj1sBtwlt8Ksc6keKXHg51H2G8nD34nqto2GA4uS3XPTmdtX0py6OgW nVtsHrwE0Oo8hGuRMTLNWneEwThtNoI2WaVRqRaGYq7frRb1PGynUQHQVK2v7DVIhzEj HERkZGGGWtVJaN1HnF9QYE759HUYJoeOKyUDM7m+j5/WcDAw6zimWVGBO1o5KNfDKlP6 rf8NCaMdL6IrWC+qYWXbsq2jON49+NF3ozVLsuFLHaG4DqoD3iqVD+ZhRsai2hHefVPw 5RmtuFkgF/FhSd+bWdK1Y+7YI81c3/yBc5oN4JCxAyJJCrppNIUw8Jr5fFdnty0CuiuQ V52g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1775710459; x=1776315259; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=xqJZt/AlaYxqLJZWCTguAzsTlD4gfP6T0rzJECviYqc=; b=oBHzhjw4DBQ1kNJiYcL41axDn3wtrgcjWHv3hgtlRg6qDozEd9et81EejGhaPxWyl7 OwtawrjRneP3wd9bn+0fi96W4MIY/0aAFmn+A+cQgz+NAFTSjkrGPVho0Oxz7fXqi5F4 mu5Gy/reeTJ5z8H/V928h9/v/GOHav66m6m+owt6UYNWpRYEz4+RNDCDm16558RgDS0W KZOk1FkfbmeiHhaCNyc/8JJi1dpTN8+s/iBUylvHr7strVfxiYJ/m+Gh2uxDxsZGsJuv m2g0rV4gXq+LXWMiWTM0I1PZYULKsHFcJefYK+5Dt+2ftPRlKMCCvV57SaGyiAGAOtsH wqFg== X-Forwarded-Encrypted: i=1; AJvYcCXoS36HNio0OAaTpw3I6AVZZ4Qdph3R+oMrGVQ9TJF7Fzcu5y7qbqbGIY2wzIgJpcSLPrLXCH0QOJ/iYGD9@lists.postgresql.org X-Gm-Message-State: AOJu0Yx73c3W2RCot3d7bbP3ZvcQkaynAIBNkKLb0NAiQU/SqX1oot0q LyWHENjT/kWCGlFsrRv18WWRgYQZbLBMjybI4ew7lf0BDwCStz0AR2xLzQbMc48v0NMnw0d3zQ7 ATSQQDRpEdhRJCXwc3DDzctdd4HNaBlI= X-Gm-Gg: AeBDieuxz7x68QDIWzba3BVYwEps4i5WuQiVuxJL5CkvkxvNHZ1vJISRTNckZo7d1sV c/SLyWdLHA/n0UWP5Xzu2ZhOcU2G0Pkv3C6CALMAk5ipedADE0XNC7JTXkE5YIg59lv/87Juc1a lWWiU7vGf8Z8pIxa+wUbv2B30CwGnzmWd9hE3TzUECFRFC3SZ9ime2Yl+WOUHG/b+uZQB2w7ttX 9qyMWmiPrJTyDgicDTwcA/EfWBxBoJ3dH9lQI8yI8zjR9/heO9GNg3JV2Pjdndu4P3q/jZpFUZX IJdbBGGK/QLgEWnQdMpHMkRtAnZDj2SfikbG3Ice3g== X-Received: by 2002:a05:651c:4016:b0:38e:13eb:f0a2 with SMTP id 38308e7fff4ca-38e335b1852mr4527481fa.8.1775710459210; Wed, 08 Apr 2026 21:54:19 -0700 (PDT) MIME-Version: 1.0 References: <202604062213.cgo352cdsgsm@alvherre.pgsql> <4n4q3preb3lgyhpzstebhux7b2aojhsw7gik4ivaznyggiezrs@lrznutssxlh2> In-Reply-To: From: Amit Kapila Date: Thu, 9 Apr 2026 10:24:05 +0530 X-Gm-Features: AQROBzCbkfJBislviN4klE5XK_444JBfZuzWoncSmbR6dUbnbOvDDL6jcMWXeu4 Message-ID: Subject: Re: Adding REPACK [concurrently] To: Mihail Nikalayeu Cc: Andres Freund , Alvaro Herrera , Antonin Houska , Srinath Reddy Sadipiralla , Matthias van de Meent , Pg Hackers , Robert Treat Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Thu, Apr 9, 2026 at 5:08=E2=80=AFAM Mihail Nikalayeu wrote: > > On Wed, Apr 8, 2026 at 7:22=E2=80=AFPM Andres Freund = wrote: > > I don't think this is a viable path. You need to prevent any further l= ock > > acquisitions on the relation to be able to swap it, not just conflictin= g DDL. > > AFAIU, Amit's main idea is that we currently upgrade the lock instead > of **releasing and re-acquiring** it because we fear DDL between those > actions. > DML actions are ok for us; REPACK will wait for them while getting > AEL. Later changes will be applied by REPACK backend while holding > AEL. > Yes, this is exactly what I had in mind. > One more thing we may prevent from sneaking into that hole is a > VACUUM. It will not break anything, but will be huge waste of time and > resources. > We can prevent other commands (if required) by checking the rel_in_use_by_repack flag but I thought for the initial version it is better to do what is minimally required. > > And you need to wait for all pre-existing locks to have been released. = That > > doesn't really get easier by what you propose. > > Do you mean locks from other sessions accessing the table? Is it done > automatically while waiting for AEL? > Right and this is already the case with the code where locks not-conflicting with ShareUpdateExclusiveLock could be present before we try to upgrade the lock. The point was we will not let DDL execute on the table after the new flag (rel_in_use_by_repack) is set and we released the ShareUpdateExclusiveLock. > > I don't think CheckTableNotInUse() would work anyway - don't we already= hold > > locks by the point we call it? > > Yes, the DDL session already holds locks by the time > CheckTableNotInUse is called - but is that really the problem? They > will be released on error. > Yes, that is the key to solve this problem. Let me take an example to explain this a bit more. Right now, the problem can happen in the following kind of sequence. Session-1: REPACK (CONCURRENTLY) foo; -- now say after the above command has acquired ShareUpdateExclusiveLock and is doing the work of copying the table, Session-2 did following actions. Session-2: Select * from foo; -- this is allowed ALTER TABLE foo ADD COLUMN c2; -- this will blocked as Session-1 already has acquired ShareUpdateExclusiveLock Session-1: -- continues and tries to upgrade the lock to AEL. This leads to deadlock ERROR in the current session doing REPACK (CONCURRENTLY). Now, with the solution I proposed, because we will release ShareUpdateExclusiveLock after setting a flag like rel_in_use_by_repack, the ALTER TABLE in session-2 will succeed but will error_out by CheckTableNotInUse(or a similar function that checks rel_in_use_by_repack). Both with and without this solution, acquiring AEL by REPACK (CONCURRENTLY) needs to wait concurrent locks like the one for SELECT in above example that could have been acquired during the time REPACKing had ShareUpdateExclusiveLock. > > And even if that were not the case, there are > > several paths to locking relations that don't ever go anywhere near > > CheckTableNotInUse(). > > But those aren't DDL, so they shouldn't be the problem (CREATE TRIGGER > might be - it seems to ignore CheckTableNotInUse, but perhaps it's > fine). > > So, in my undeerstanding Amit's idea has two parts: "set flag and > release/re-acquire" + "use CheckTableNotInUse (or some place like > that) to check the flag and fail for DDL commands." > True, this is the core of the idea. --=20 With Regards, Amit Kapila.