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 1w5iof-003YbI-1V for pgsql-hackers@arkaria.postgresql.org; Thu, 26 Mar 2026 11:23:57 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1w5ioc-002J17-1w for pgsql-hackers@arkaria.postgresql.org; Thu, 26 Mar 2026 11:23:55 +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.96) (envelope-from ) id 1w5ioc-002J0z-0K for pgsql-hackers@lists.postgresql.org; Thu, 26 Mar 2026 11:23:54 +0000 Received: from mail-wm1-x32a.google.com ([2a00:1450:4864:20::32a]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1w5ioa-0000000188y-0DxY for pgsql-hackers@lists.postgresql.org; Thu, 26 Mar 2026 11:23:53 +0000 Received: by mail-wm1-x32a.google.com with SMTP id 5b1f17b1804b1-486ff3a0fc1so8107285e9.2 for ; Thu, 26 Mar 2026 04:23:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec.at; s=google; t=1774524230; x=1775129030; darn=lists.postgresql.org; h=message-id:date:mime-version:comments:references:in-reply-to :subject:cc:to:from:from:to:cc:subject:date:message-id:reply-to; bh=NF7JoMauzvLllRO4wMvX4s0lLwsVTSHZnFBNws5sLqA=; b=GkwVwqv6N8zfyW/Xia38xa+lL8qxHAb2hx0zo5dHK72TQaX8SezYz+LyRhFhGjBWWj 7uWmZJN5sdCtgKzRsmNK9Qyo2L07LIlqja2SCXrd/NjUtct+dt/xOYtxelrQPLVoiErt y7EKBUc7LRsBMu6nvcdt+W9p/gviNTJxFYt309lUOxo8er1aWRBsaXZ/FN+KKaKQUis3 Sb1/3+3b0Hz1KRYt071UbLr8NpaxW6+QkwgQXeVpXDPfpLlxtAahrVb0iaKSA4KZo+f1 4kaRNiLsFrFrVlbpQMz3yK0CdckvW+mOAt2uEvhmmYEAI7XcZ5Tgbw4lwIWtwLURUDGj reLA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1774524230; x=1775129030; h=message-id:date: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=NF7JoMauzvLllRO4wMvX4s0lLwsVTSHZnFBNws5sLqA=; b=SskK3LDgpjYZ5kQ6VoerFc4ZWfdjXc8G/nTCAC7EHL9KpbumwwN0ZUwjTwkh1XDPCD 1MmlA5vMAYylh/k4U2EhPj/tADIdUwGoQ2/rDIhDsnhD+hrZjOuCKzN3H9H0ojT9otE3 FCdFTsO2FP9hy9Tt39f/ah3H9GAjBDggPAaUdO6NBM2i2h71WSpJ05jE/vDIq1WfD7jD hIBG+xApSeS5qlEZfjQ/s8sCtEdZjCfJc4HbfAVrpGmyujZ3GPG6X4j9onM8efwheBxO is53ypUhRRYkRQFlOTr7GzjYd0vMkhHKPc49mY8HRncPnCcG0tHgMcApacQKwG1lMre8 rlFQ== X-Forwarded-Encrypted: i=1; AJvYcCWeNYkT6PUwhTunVlnS7XE6BPei0nNyDF+ehtkHTkKUPi+r3v6AGpXwzs/5OgURw921wVtnTtuoHzEfDAEh@lists.postgresql.org X-Gm-Message-State: AOJu0Yxf0l5LBrGYXKWc1aZN11ropzqv4XoM1zOqLxaPcUInhbMz7qar wyktBjoLd8Y8HZUV7B9t0KQNoeIGf+WK5Bjf6yXq1/cvg9+fTLemYyoljipfkFgPGf4= X-Gm-Gg: ATEYQzzUaks0hfaEsR3rXeeWoRECTzrk08EFVk53k5ueaW5GffJRXBkaIFpmOZN68hN jwijfvS5r3qG3VznfDsRWj9Q6xaDn5KwKxELWninBx1WOnruFyvlfb7qgx0GQ/vQRe6y6DWnetX eFRIp4KACt8jw2cP07LE0yYwxJOlTTTQuImIE9F3jJj81DddloR8emibag7BTpivx34qdPBd/wF uSDflzKApYcJvBJ/2d86LThuDstgeNcNWY1HYtj1aQNoSYxh2zkIyB42jBn1kojVXFRLSWznR0V 6NvTZlHEZR31Fn1o9cDa/SKQ9nfQoeM1pjl8uZAfFrFfLbxYa2gMJJK78APVpgXYUGBcsr7eSUz uuDb9kaCidEVCOec0GYk2wQdyAC5/tZThzSud5RNsufly8rKnGi0dyhxc5EhQQ1bHziHPtWHIOU JMmtqGF2iCefZ1lsiuEiuVFdaRl+tzzq3zQLFy X-Received: by 2002:a05:600c:4447:b0:485:445a:87d1 with SMTP id 5b1f17b1804b1-48715fd4baamr100353245e9.8.1774524230398; Thu, 26 Mar 2026 04:23:50 -0700 (PDT) Received: from localhost (109-81-168-142.rct.o2.cz. [109.81.168.142]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-48722c845b8sm24825705e9.4.2026.03.26.04.23.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 26 Mar 2026 04:23:49 -0700 (PDT) From: Antonin Houska To: Alvaro Herrera Cc: Srinath Reddy Sadipiralla , Mihail Nikalayeu , Matthias van de Meent , Pg Hackers , Robert Treat Subject: Re: Adding REPACK [concurrently] In-reply-to: <23138.1774518710@localhost> References: <202603252005.quy5h4oipoxd@alvherre.pgsql> <23138.1774518710@localhost> Comments: In-reply-to Antonin Houska message dated "Thu, 26 Mar 2026 10:51:50 +0100." X-Mailer: MH-E 8.6+git; nmh 1.8; GNU Emacs 28.3 MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Date: Thu, 26 Mar 2026 12:23:49 +0100 Message-ID: <29614.1774524229@localhost> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --=-=-= Content-Type: text/plain Antonin Houska wrote: > Alvaro Herrera wrote: > > As for lock upgrade, I wonder if the best way to handle this isn't to > > hack the deadlock detector so that it causes any *other* process to die, > > if they detect that they would block on REPACK. Arguably there's > > nothing that you can do to a table while its undergoing REPACK > > CONCURRENTLY; any alterations would have to wait until the repacking is > > compelted. We can implement that idea simply enough, as shown in this > > crude prototype. (I omitted the last three patches in the series, and > > squashed my proposed changes into 0003, as announced in my previous > > posting.) If we take this approach, some comments on deadlock need to be adjusted - see my proposals in nocfbot_comments_deadlock.diff. Besides that, nocfbot_comment_cluster_rel.diff suggests one more comment change that does not depend on the deadlock detection - I forgot to change it when implementing the lock upgrade. Also the commit message of 0003 needs to be adjusted. (Does it need to mention the problem at all?) -- Antonin Houska Web: https://www.cybertec-postgresql.com --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=nocfbot_comments_deadlock.diff diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c index d5b1dfbff69..a9788ac6209 100644 --- a/src/backend/commands/cluster.c +++ b/src/backend/commands/cluster.c @@ -618,10 +615,12 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid, /* * Make sure we're not in a transaction block. * - * The reason is that repack_setup_logical_decoding() could deadlock - * if there's an XID already assigned. It would be possible to run in - * a transaction block if we had no XID, but this restriction is - * simpler for users to understand and we don't lose anything. + * The reason is that repack_setup_logical_decoding() could wait + * indefinitely for our XID to complete. (The deadlock detector would + * not recognize it because we'd be waiting for ourselves, i.e. no + * real lock conflict.) It would be possible to run in a transaction + * block if we had no XID, but this restriction is simpler for users + * to understand and we don't lose anything. */ PreventInTransactionBlock(isTopLevel, "REPACK (CONCURRENTLY)"); @@ -1104,10 +1103,8 @@ 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 (e.g. it runs CREATE INDEX), it should encounter ERROR in the + * deadlock checking code. */ start_repack_decoding_worker(tableOid); @@ -3766,9 +3763,11 @@ start_repack_decoding_worker(Oid relid) /* * The decoding setup must be done before the caller can have XID assigned - * for any reason, otherwise the worker might end up in a deadlock, - * waiting for the caller's transaction to end. Therefore wait here until - * the worker indicates that it has the logical decoding initialized. + * for any reason, otherwise the worker might end up waiting for the + * caller's transaction to end. (Deadlock detector does not consider this + * a conflict because the worker is in the same locking group as the + * backend that launched it.) Therefore wait here until the worker + * indicates that it has the logical decoding initialized. */ ConditionVariablePrepareToSleep(&shared->cv); for (;;) --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=nocfbot_comment_cluster_rel.diff diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c index d5b1dfbff69..a9788ac6209 100644 --- a/src/backend/commands/cluster.c +++ b/src/backend/commands/cluster.c @@ -582,11 +582,8 @@ RepackLockLevel(bool concurrent) * If indexOid is InvalidOid, the table will be rewritten in physical order * instead of index order. * - * Note that, in the concurrent case, the function releases the lock at some - * point, in order to get AccessExclusiveLock for the final steps (i.e. to - * swap the relation files). To make things simpler, the caller should expect - * OldHeap to be closed on return, regardless CLUOPT_CONCURRENT. (The - * AccessExclusiveLock is kept till the end of the transaction.) + * On return, OldHeap is closed but locked with AccessExclusiveLock - the lock + * will be released at end of the transaction. * * 'cmd' indicates which command is being executed, to be used for error * messages. --=-=-=--