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 1wAkz0-000ILa-0D for pgsql-hackers@arkaria.postgresql.org; Thu, 09 Apr 2026 08:43:26 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wAkyy-004bNF-0C for pgsql-hackers@arkaria.postgresql.org; Thu, 09 Apr 2026 08:43:24 +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 1wAkyx-004bN7-2J for pgsql-hackers@lists.postgresql.org; Thu, 09 Apr 2026 08:43:24 +0000 Received: from mail-wr1-x42f.google.com ([2a00:1450:4864:20::42f]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1wAkyw-000000009Hv-1ZZL for pgsql-hackers@lists.postgresql.org; Thu, 09 Apr 2026 08:43:24 +0000 Received: by mail-wr1-x42f.google.com with SMTP id ffacd0b85a97d-43d572f7437so417576f8f.1 for ; Thu, 09 Apr 2026 01:43:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec.at; s=google; t=1775724201; x=1776329001; darn=lists.postgresql.org; h=message-id:date:content-transfer-encoding:mime-version:comments :references:in-reply-to:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=bBNF+uQGJF1E19g8OpDfPFDS5GxEK0L7qPUq8o3TZhE=; b=GHR3Px/FauVlnu+A9znTMFa7xRn9AAtoOqAkr/lAAeT0ZNjyF4TwOKkNnrSBta6SyA vxieqTFOXHqySsAyieUQQv6f1RtT5vMixJ9rWd/DGzXPz5YfCODuU290x9Cejttg97dY p0N59J3KvgHQies9u8b8vNNl+aAiCRFxU2MAle5gF28RyuH/B7c2P+/Ss/V29F2Urya+ MTEmPHC2a7r+8DKna5dy9SeudKAExCSPLnryW2LtRzMfTF/nCBlnlP/ipvv0Z2v+EjO0 pv9U6n9k3gi2pa1Jma7Wse6d2wS5JCfd2aIm+hWZhmrzlv0CcNVIq8a1LQCM7OVVgPMV gRtg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1775724201; x=1776329001; h=message-id:date:content-transfer-encoding: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; bh=bBNF+uQGJF1E19g8OpDfPFDS5GxEK0L7qPUq8o3TZhE=; b=Ed3kMYZTxjozNBNGq4dTSHlQ3SYo7hAE3advu2O4sUJb0GecLWUB+bh8UGfarUSU+i ApQj1A7YLxZadu1iv4xixavDvmK5JlahcnfxlyDDgMGhQym0AnsCDqFJAoohdIgFi6kL fRjx5Xf6RyI2AIC38z+2zP1SmzprCKqQnVDj2PusFlGRneqLcKkuAD4D8OjYjTsrbPnk RuMGnsR/icgr36zUwjnaK++lxzN2ET/5ZQw7T7Han3AmBbE1LnLel0dDczQ5fo9uKsAL /41kqxBZSX+MPuAem6yfPlIXl5ouZunuSbLlPEW6RiQiHqlVimxN48JKDlyIxckVx9Dx Q9Ig== X-Forwarded-Encrypted: i=1; AJvYcCVfVOPsWpZusm0zs5t8zPEWYLWxTk+ofIlIYFf49KzSww4b9Vr0zRQWTLmFd5LNyzVDkRntAVgJJyTHOrg4@lists.postgresql.org X-Gm-Message-State: AOJu0Yzqf5JHE44E9O0KaCLV5l4OMnLEWsF7PTqkfm1fR6/LgVCE23X2 qTwdNvzp0Qgx/wZ7RaNQRdT3mRiQwC3cSf6z1X/cMrklNNBJcnXsz2ZZK7VndXHGyMQ= X-Gm-Gg: AeBDiesr+et/ZGJKUfQ2Z7vKLH9LZUwpHqvUxbSrtpkLOLOaOOWgstIhnvX+V+WsrXZ 9gywqs2HW/d8t/GoRYMmeJiW5dbzkVMDC15jJrpGIo6i8C/cJnD6ZbvoAFN1JLOLW6LSOfCcxNp vsiT30gE6Jrw/7g0YD1rlgKsCM8Wgngajwb6EPEvDkDErAFXRg6Gz6tUEPWTKmBYINO+QCQvtzW mVDiBPmeplxRYM0AGgInvZ33NXtH2t/ejTlO6KMe21G7IQA0tYB5+7+kU049j5dhYyjhmJq7ofX 7jOp6J4DIpkUGN30B3NgK+4KwpSE3wP33ADh9OurhJQt1nMbMUczqJUkTP3gnxP0hiVyK/nJqqG p7blggHZltPZC0hoD0KufYhY0hcgOESEQ0U5qPq0bAgN0QqL0vz4qOR+C5SAqSXK/679v+/ybmn diW+gDoFLl2Fk2xbeEwRDWpjVcQZKIS6bk5ESO X-Received: by 2002:a05:600c:1d19:b0:488:cc72:5497 with SMTP id 5b1f17b1804b1-488cd0330e1mr39172175e9.31.1775724200716; Thu, 09 Apr 2026 01:43:20 -0700 (PDT) Received: from localhost (109-81-168-142.rct.o2.cz. [109.81.168.142]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-488cd2181casm59232575e9.11.2026.04.09.01.43.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Apr 2026 01:43:20 -0700 (PDT) From: Antonin Houska To: Amit Kapila cc: Mihail Nikalayeu , Andres Freund , Alvaro Herrera , Srinath Reddy Sadipiralla , Matthias van de Meent , Pg Hackers , Robert Treat Subject: Re: Adding REPACK [concurrently] In-reply-to: References: <202604062213.cgo352cdsgsm@alvherre.pgsql> <4n4q3preb3lgyhpzstebhux7b2aojhsw7gik4ivaznyggiezrs@lrznutssxlh2> Comments: In-reply-to Amit Kapila message dated "Thu, 09 Apr 2026 10:24:05 +0530." 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, 09 Apr 2026 10:43:14 +0200 Message-ID: <9539.1775724194@localhost> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Amit Kapila wrote: > 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= lock > > > acquisitions on the relation to be able to swap it, not just conflict= ing 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. > > >=20 > Yes, this is exactly what I had in mind. >=20 > > 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. > > >=20 > 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. >=20 > > > 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? > > >=20 > 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. >=20 > > > I don't think CheckTableNotInUse() would work anyway - don't we alrea= dy 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. > > >=20 > 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. >=20 > Session-1: > REPACK (CONCURRENTLY) foo; >=20 > -- now say after the above command has acquired > ShareUpdateExclusiveLock and is doing the work of copying the table, > Session-2 did following actions. >=20 > Session-2: > Select * from foo; -- this is allowed > ALTER TABLE foo ADD COLUMN c2; -- this will blocked as Session-1 > already has acquired ShareUpdateExclusiveLock >=20 > Session-1: > -- continues and tries to upgrade the lock to AEL. This leads to > deadlock ERROR in the current session doing REPACK (CONCURRENTLY). >=20 > 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. >=20 > > > 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." > > >=20 > True, this is the core of the idea. This approach LGTM when it comes to concurrent DDLs. However, consider REPA= CK holding ShareUpdateExclusiveLock (SUEL) and VACUUM (w/o VACOPT_SKIP_LOCKED) waiting for the same lock. Once REPACK releases its SUEL, VACUUM gets it and processes the table, then REPACK finally gets AccessExclusiveLock (AEL) and finishes too. Nothing went wrong from the data consistency POV, however the VACUUM proably wasted a lot of resources, because REPACK does "more than VACUUM". Furthermore, while REPACK was waiting for the AEL (and the VACUUM = was running), a *lot of* DMLs could have been executed on the table, and REPACK will have to replay those while holding AEL. The point is that REPACK should hold AEL for as short time as possible. What Andres proposed (AFAIU) should help to avoid this problem because REPACK's request for AEL would get in front of the VACUUM's request for SUEL in the queue. Of course, VACUUM would eventually run too, but - if REPACK succeeded - it'd be much cheaper because it would (supposedly) find very little work to do. Anti-wraparound (failsafe) VACUUM is a bit different case [1] (i.e. it shou= ld possibly have higher priority than REPACK), but I think this prioritization should be implemented in other way than just letting it get in the way of REPACK (at the time REPACK is nearly finished). [1] https://www.postgresql.org/message-id/CABV9wwMrrP4S54jPGn5D-a8AbJm%2Be5= %2BWORs6ykUBgXdc-%2B%2BNtQ%40mail.gmail.com --=20 Antonin Houska Web: https://www.cybertec-postgresql.com