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 1wEESa-003nxJ-0l for pgsql-hackers@arkaria.postgresql.org; Sat, 18 Apr 2026 22:48:20 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wEERY-00Dths-2k for pgsql-hackers@arkaria.postgresql.org; Sat, 18 Apr 2026 22:47:16 +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 1wEERY-00Dthk-1K for pgsql-hackers@lists.postgresql.org; Sat, 18 Apr 2026 22:47:16 +0000 Received: from mail-lf1-x135.google.com ([2a00:1450:4864:20::135]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1wEERV-00000001gv6-3p3B for pgsql-hackers@lists.postgresql.org; Sat, 18 Apr 2026 22:47:15 +0000 Received: by mail-lf1-x135.google.com with SMTP id 2adb3069b0e04-5a283c44478so3050929e87.3 for ; Sat, 18 Apr 2026 15:47:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1776552433; cv=none; d=google.com; s=arc-20240605; b=Xa2GrEADBl9wew6h34kQiOSx5qVuDTXDAP0hIsvrGA2iYmiwi+6K6o9T49+7MJ+L/X 00iB8FK+IlorU4C+HKa54s2RvVTzRvryE6yTv1F/eIOdS9irIvw5iCNDk+g1mqXxL9Mc n1c/HOhr6X/ViwfFEna2ODC7YCQ8pb3HJ1FD50l5UG5y/NsBi2/Y/WXkvWA+KhCPf5ni k5lqYsYbO8h/ccLtbhrVyGqGQkc60q71buBuj9xvrFqBIKo7W8OWnj88JGEet+wiorkC DOGlsNhvCmDTGA+6JpPGsBW4oWDroA5YLNjiFowteVHr3viUB59MQ8nlr3R0j8d67qkD o8Hg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:dkim-signature; bh=cjNN445FvkJmjLsVsLtMALjfQvMTp6Oa9dBUoPKSuO4=; fh=FLFHXee1M6K5Zqk2rkTTnk6wSN2SODtuDe7icqUE6j0=; b=FrNCxYT8JXtgQAvx3ATylQQUVN2GXhj2ZEa+3s4BD10ZLbfhAff9KvxWZ2fl5oqhgf jNIsdwPqkCpNErF2vv+SNapqTpt542fTQIS43dffBzQW58x7JqG1nMlBPTZ41UjAfn6e BzZEfQvbLX/dVwEdbGdSOVyTsqmin4KDa6VWfr6NI1gIagFO1aNZG8+zEGFp1EVA+vii FBgi3z0dc/TbeFY+iT22CbE11xfrx+SoA+0k7OYpRiwFk+nvwJp3k4ynIK89rUG9gLIv ZJiKl7sBz5FZguzRRlS3WuN8LNs3pozCupydtxVN9hdawu8M395wS9MVa5jUrIcevpCJ 4o+Q==; 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=1776552433; x=1777157233; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=cjNN445FvkJmjLsVsLtMALjfQvMTp6Oa9dBUoPKSuO4=; b=XLbksaMDbjHmT3Z3bIc896YAGmlIHaa5M7RRPttU7cG56Xt8cxAzhWWDBj7oh8rV8v 5A/Qynwa6PN+/q57uL4xsFOZ/nQhu3gob4A2yTi6O7BXfi2+RLozjxn0140KZBAZy+P/ P+1DccRjuh+1TMSTigCuXP7jtbGlsgW9y9C3FEU8zLHARlY+EKFt+HrJQa8Y+b2ATv89 L9TV52237Hp/nxOEJJR6X2nhkRHI9PxTAYjhMXuWf9W2iO6i6NG/sagmE6kbI52SkbsY erIBPEzkG+JoOTkEwTf08pLFSOPAznB2hOM1sakgqXD7TvumtoUUMHwqbWmozZkzJ0YH i02g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776552433; x=1777157233; h=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=cjNN445FvkJmjLsVsLtMALjfQvMTp6Oa9dBUoPKSuO4=; b=VbNSXxaxDZETAqwmg6SMX9r/y+vogvHYE7RQu6E9vx4Kq+IVT28158W7+z0TqDQS/x 4367eOD01P56Y73Yt0mNXRM2Jnf2lfbhBB0sAciFWpwF/MCz8jD3kLl2SMCZrerHAo3t y2POf36u5xjTOR5Aenbepq6ljmsJdgF5Yg4dtKHi4ch5eZPvGRrKDCpk/XnTPYe7TBek WZyxEIvlKR7iRZVgC0ZfKn/S3VSKwEUI4RU1YweWgnAsEasXnsij8fkny9OrDWQYVU2L BW905fikQ9FDYEkLayRrkgfFybEinx8kdN8C3cVa1C9+NYdj3EqJae0YmwZ9vq0NWBgn JcTQ== X-Forwarded-Encrypted: i=1; AFNElJ9WMhncX/7Vpsy3Db4IVbOOiywZvTJlxifLQCGh4vnE+qp5/eDVPFsC6ZSSD/1INZifvXFKr3uNeNdKyQOO@lists.postgresql.org X-Gm-Message-State: AOJu0Yy0qmz89Vn9GwGXhxAUom275qGa+ELcurRhaIhhS++W6cALlp2s 34EFM3juBgNUAt4dIvibJqCFQLszTrascJsdZRB6BszfF2aT/bq0ulfynr72Ebhi75DyMhcZ2cf 6jWRLGCwHQmTDvEpzvXCUNSN0x1lfcRa4BvXp X-Gm-Gg: AeBDieuiT4dr6hXiBo5jRJRvcdT8RETlu17ARrckrYuEvAPocUd/Ox9QLymAJY07BnX 3ZWSlNFuzW/dqOdW4YuA/AGqsZBOWry050+qM11gQJDPn8K6rTEqNbjMT4bzA/cHhdxfpnTnkMy Q4dGq6eyv1xamqZGgRHbgPUswWcgAStDZUokirOeoHkarVoRZeVitwJahyS17fDg0ApAon+8smm vU+pTEWuZ3G3Tdq6IQyOsVC+SJl6EnjC1CZ8wjr+eQlKGUaDRrigyzkXBF8OIsAsZMgeZVN6Med ylSRyz3oaAf7tF6LAsvfxAaJyMeLb5xzMcY8P0fwi0VqEKb3KbKZicWPz4zPd+CvcdrmoiNXfdo PIXmmXp5v68E7XDEJKtCfcTFVWF963wX7s8CimpF2IHFPdsdV X-Received: by 2002:a05:6512:1589:b0:5a2:86a3:709f with SMTP id 2adb3069b0e04-5a4172c8cd0mr2469152e87.17.1776552432408; Sat, 18 Apr 2026 15:47:12 -0700 (PDT) MIME-Version: 1.0 References: <9539.1775724194@localhost> <112208.1776173876@localhost> <25514.1776264611@localhost> <38385.1776277704@localhost> <44458.1776540188@localhost> In-Reply-To: <44458.1776540188@localhost> From: Mihail Nikalayeu Date: Sun, 19 Apr 2026 00:46:00 +0200 X-Gm-Features: AQROBzBhwKwdxmdx18l2qR2UVEKVgd2oyHRKdOY87CWUloLqwluYRJqdpXgFrxc Message-ID: Subject: Re: Adding REPACK [concurrently] To: Antonin Houska Cc: Andres Freund , Amit Kapila , Alvaro Herrera , Srinath Reddy Sadipiralla , Matthias van de Meent , Pg Hackers , Robert Treat Content-Type: text/plain; charset="UTF-8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hello, Antonin! I've briefly looked at your patch. As I understand it, it cancels the other process only when REPACK actually tries to upgrade the lock. In that sense, its approach is close to my variant at [0]. AFAIU, Andres's concern is that the "victim" should be cancelled sooner, rather than waiting until REPACK actually attempts the upgrade. I was trying to solve it by [1]. ------------------------------- There are some comments on [1]: Some details related to POC from previous letter (once I re-read that in the morning I relized it is not so easy to understand, sorry). So, goal of patch to: * make sure REPACK will survive deadlock which may caused by lock upgrade * reject some other backend with error early than deadlock really occur - to avoid pointless waiting for other commands * implement it at deadlock detector level to correctly handle different 2+ backend-involved scenarious To achive it the first idea is to add some kind of "future lock" (FutureWaitLock). It may declare an intention to acquire a lock of a certain level as high-priority in the future. Once deadlock detector starts to walking graph it treat that intention like an actual "waiting". That way deadlock detector looks for one more step in the future - moment of actual acquiring the "future" lock - and if it ends with cycle - reject waiting backend ("future deadlock detected"). Looks like best place to put that FutureWaitLock is PGPROC itself. Few moments to consider: * it is not allowed to declare a future lock if backend already holds some kind of lock for the same tag (this may cause a race condition). * so, REPACK first declares future Access Excluvie and only after it Share Update Exclusive * declared future lock should be >= SUE So far everything looks good. In case of that scenario: S1: BEGIN; SELECT * FROM t; S2: REPACK (CONCURRENTLY) t; S1: LOCK TABLE t in ACCESS EXCLUSIVE MODE <--- "future deadlock detected" Deadlock detector sees: S1 ----> S2 (waiting for AE conflicting with SUE) S2 ----> S1 (future wait for AE conflict with current Access Share) But there is a tricky case related to SUE: S1: BEGIN; SELECT * FROM t; S2: REPACK (CONCURRENTLY) t; S1: LOCK TABLE t in SHARE UPDATE EXCLUSIVE MODE; In that case we have almost the same deadlock scenario - but that is not visible to deadlock detector. It happens because SUE does not force all locks taken on 't' to be transfered using FastPathTransferRelationLocks into the main table (SUE is does not ConflictsWithRelationFastPath). Because of it S2 -> S1 edge is not visible by deadlock detector (Access Share is held using fast-path). To deal with it we may force any relation with FutureWaitLock to through slow-path locking - but I don't think it is acceptable. Instead next approach is proposed: * deadlock detector checks if any "future" locks are present in the system (counter in shared memory) * if so - it iterates over all PROCs to collect relations which are "future locked" * for each such relations - FastPathTransferRelationLocks called and slow-path is forced (FastPathStrongRelationLocks->count) * deadlock detector start looking for cycles * once ready - FastPathStrongRelationLocks->count is decremented to allow fast-path That way performance degradation happens only during deadlock detector processing and only if some future locks present. Due tue LW ordering we need to use some tricks to avoid LW-level deadlocks (using some kind of retry logic, but that is more explained in the patch). [0]: https://www.postgresql.org/message-id/flat/CADzfLwURKVNQ%2B%2BDpi7bjoGfj-8pchDQEVex3eWBx0NCYn6TbDQ%40mail.gmail.com#bceb18354aa20c130a94b1deedd76fb7. [1}] postgresql.org/message-id/flat/CADzfLwU8Qw6LXFHO7Tbjc-O7o%2BtM26jdnOJBWqYLu61rf7bO%2Bg%40mail.gmail.com#1e96f8882363afb2fc53c2f08346f527