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.94.2) (envelope-from ) id 1r5lIU-00EuAb-Hh for pgsql-hackers@arkaria.postgresql.org; Wed, 22 Nov 2023 11:21:34 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1r5lIR-007sdo-Vx for pgsql-hackers@arkaria.postgresql.org; Wed, 22 Nov 2023 11:21:31 +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.94.2) (envelope-from ) id 1r5lIR-007scB-KI for pgsql-hackers@lists.postgresql.org; Wed, 22 Nov 2023 11:21:31 +0000 Received: from mail-wm1-x329.google.com ([2a00:1450:4864:20::329]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1r5lIN-006nSn-L9 for pgsql-hackers@lists.postgresql.org; Wed, 22 Nov 2023 11:21:29 +0000 Received: by mail-wm1-x329.google.com with SMTP id 5b1f17b1804b1-40b2c8e91afso8504995e9.3 for ; Wed, 22 Nov 2023 03:21:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb.com; s=google; t=1700652086; x=1701256886; darn=lists.postgresql.org; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=rK4UmvBe8n6L20WDhnnANfDCaIqLS586az1k+Ur8VzA=; b=AfWliCx9wtrE1DHC2rqSL7RC+VnVumWkCOGefxF9L5VFypuCpWQKAbwMBy7/9y/Ky+ AzoCsz6XGIgs6mBcGiVI1D9Nfwg4Hm3Vd8f5fwggXCBv3grHPjyBiA2/pHoRO2c4EZ9H +b7+Hk6vESXF/9IoZf68zad71RP1TIEqMwjnVfIZXn24QmYlDSqLPO0PB3e0LHd1kWLW xT6L1GPn5+zfJPB/Qcq78zSOhMIlWW2s8O/MMF0sN7TqUw4ZU5iVi5eY/CCt3CqfkSN7 rRhZMguN/fU90udaTg2ZLotfwQx3D7eDtSdTSgyX3NuOU7oGDdE7vkHtZ4BrBgNCWH3t QP0A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700652086; x=1701256886; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=rK4UmvBe8n6L20WDhnnANfDCaIqLS586az1k+Ur8VzA=; b=g44AdTagWuvPyTlKTbeXjehrBp44svfVOJNcRC/OgO1ro3fcAu8/hXF7pzHo1EWwQk PjRNuMsWLFS1rKHFk9ww/2A4rxCZ4KCDSO5ld1cqmvMUooDSl3iv6jQHsreWmOY8xyoX GV15lFLPPJeywc618cZRj5R3MX8QvDpR2jAXN3+2vhitozw8KaWif3B/NOnz6Ko348jK 0yemtPcKTUlAeQenvvUkDxmyur1Y1YPGa0WCooY1E9ULhCGXVcDERMePEf2hH2Iqx5z9 wOiW0+ykTW3GoL+atVSquX1MrmO20x1Xx7f6RZ8UYdg2ofZPpTRCgEWq0dtNcQ6R2Uqp 35Gw== X-Gm-Message-State: AOJu0Yy6pEIazh/GNax5/W3GK4r5hcp1Tzoskrv/yO85w9oH3ZuqMeUP D8pn14QheZjoj47DJB8zGB7wmg== X-Google-Smtp-Source: AGHT+IHbuL/x0fZZECjEKc/Qg+FVwBEDBdQ+1PEPKrfRc8EYU0ya8lvzOfLAZtMZdfuwj5/8oFSEDg== X-Received: by 2002:a05:600c:19c6:b0:402:ea96:c09a with SMTP id u6-20020a05600c19c600b00402ea96c09amr1271085wmq.16.1700652086029; Wed, 22 Nov 2023 03:21:26 -0800 (PST) Received: from [10.137.0.17] (srv1.mobissw.com. [89.235.0.226]) by smtp.gmail.com with ESMTPSA id d1-20020adfe841000000b00332cfd83b8dsm3171190wrn.96.2023.11.22.03.21.25 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 22 Nov 2023 03:21:25 -0800 (PST) Message-ID: <69e9a1af-a315-8b05-0674-2cc53bf27682@enterprisedb.com> Date: Wed, 22 Nov 2023 12:21:25 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.15.1 Subject: Re: undetected deadlock in ALTER SUBSCRIPTION ... REFRESH PUBLICATION Content-Language: en-US To: Amit Kapila Cc: PostgreSQL Hackers References: <8948e4a2-fec7-8a61-9ace-2537cee3ef6f@enterprisedb.com> From: Tomas Vondra In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 11/22/23 11:38, Amit Kapila wrote: > On Tue, Nov 21, 2023 at 6:56 PM Tomas Vondra > wrote: >> >> On 11/21/23 14:16, Amit Kapila wrote: >>> On Tue, Nov 21, 2023 at 5:17 PM Tomas Vondra >>> wrote: >>>> >>> >>> It seems there is some inconsistency in what you have written for >>> client backends/tablesync worker vs. apply worker. The above text >>> seems to be saying that the client backend and table sync worker are >>> waiting on a "subscription row in pg_subscription" and the apply >>> worker is operating on "pg_subscription_rel". So, if that is true then >>> they shouldn't get stuck. >>> >>> I think here client backend and tablesync worker seems to be blocked >>> for a lock on pg_subscription_rel. >>> >> >> Not really, they are all locking the subscription. All the locks are on >> classid=6100, which is pg_subscription: >> >> test=# select 6100::regclass; >> regclass >> ----------------- >> pg_subscription >> (1 row) >> >> The thing is, the tablesync workers call UpdateSubscriptionRelState, >> which locks the pg_subscription catalog at the very beginning: >> >> LockSharedObject(SubscriptionRelationId, ...); >> >> So that's the issue. I haven't explored why it's done this way, and >> there's no comment explaining locking the subscriptions is needed ... >> > > I think it prevents concurrent drop of rel during the REFRESH operation. > Yes. Or maybe some other concurrent DDL on the relations included in the subscription. >>>> The tablesync workers can't proceed because their lock request is stuck >>>> behind the AccessExclusiveLock request. >>>> >>>> And the apply worker can't proceed, because it's waiting for status >>>> update from the tablesync workers. >>>> >>> >>> This part is not clear to me because >>> wait_for_relation_state_change()->GetSubscriptionRelState() seems to >>> be releasing the lock while closing the relation. Am, I missing >>> something? >>> >> >> I think you're missing the fact that GetSubscriptionRelState() acquires >> and releases the lock on pg_subscription_rel, but that's not the lock >> causing the issue. The problem is the lock on the pg_subscription row. >> > > Okay. IIUC, what's going on here is that the apply worker acquires > AccessShareLock on pg_subscription to update rel state for one of the > tables say tbl-1, and then for another table say tbl-2, it started > waiting for a state change via wait_for_relation_state_change(). I > think here the fix is to commit the transaction before we go for a > wait. I guess we need something along the lines of what is proposed in > [1] though we have solved the problem in that thread in some other > way.. > Possibly. I haven't checked if the commit might have some unexpected consequences, but I can confirm I can no longer reproduce the deadlock with the patch applied. regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company