Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1camqz-000864-CP for pgsql-hackers@arkaria.postgresql.org; Mon, 06 Feb 2017 17:13:25 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1camqy-0005dI-Pl for pgsql-hackers@arkaria.postgresql.org; Mon, 06 Feb 2017 17:13:24 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1camqy-0005d7-4B for pgsql-hackers@postgresql.org; Mon, 06 Feb 2017 17:13:24 +0000 Received: from mail-wm0-x229.google.com ([2a00:1450:400c:c09::229]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1camqq-0002my-QR for pgsql-hackers@postgresql.org; Mon, 06 Feb 2017 17:13:22 +0000 Received: by mail-wm0-x229.google.com with SMTP id c85so129445488wmi.1 for ; Mon, 06 Feb 2017 09:13:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=subject:to:references:cc:from:message-id:date:user-agent :mime-version:in-reply-to:content-transfer-encoding; bh=dpIkbCqHJsIDC2y3Wfqf6HnOC3zS6PWWDX0rV9hFrIM=; b=n1YZxAGRcvTQO0h6oAkKWWqQGFYp6gY6nSNTnaEDD80LPN7KExQqNmRsovAlkO0qvV Gt37VJbtItCq+Qc2PSx2ytkUp7A7xpguJea33MMRLOYXKaicgog/348ZtWQkykWP7dJ+ ksHCv9sfFuUGA5hd1tvTaOZg6EoLs9e4VCDKQAEU2OU4EAoBdEeNoKwfLvhdsaIq9lbP iJItxIz70tl04wGHC7GR9IUVmxd/Q+JlR5xAbQhMCM91BDF3SgRndjJT5okPoH8mYJBD oRcCdtmJ4itNQQU0hGmMsMuGzQbAIBC/hbIxbpr9GVlS8kdRMYYfvLZpCVi2FBc7WVef NEHg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:cc:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding; bh=dpIkbCqHJsIDC2y3Wfqf6HnOC3zS6PWWDX0rV9hFrIM=; b=EKiKqChz+swpnZ5QamXeG4zMnO9NHFLsyEpTszZBqo6XCeImLnqd97DhsHKlb7WRl4 1e5mGkajdA1WZJcV30/dOWwRM/Zn2PbKFVtQFbo/+4fG9SK12Ir2TLTenazVIlcAydUA R3xKCsgTmizWrWw0LCmKqaOrTnVBi9mk8Qb7cnejdcg8o8I/r89YSRp0rBkRhyKGwel7 YRIKRyfS9IHqNkoaGPpoZeb3+xMqo6RAWpYjp60kwFRT3cTU14MpaQOs6bBCzjQ0k1iy p7XswlNxkowSoBmVOt7TekWRVkKmPfkon+vnvLwwKsrUkdaCgyijC2GMbAu/at7fTSZy 1y5g== X-Gm-Message-State: AMke39nuZkfv3s7sWv/UAKboSZVghUveycOF/XZu3nhNHaeHn9PDnW0/+e36CdP6zXG/xlzKaHu2hAn408JBijktlf/9fo3PD+1v18kwLn0S6vX6Og9bwVs4b6RXUzUn/rv38+lJmrdeLVH8m+VIK4CS/CH7TQrRem5CQXK91iGVWQ8S5k194MywpfenVNyd1ULIunmYNsbT X-Received: by 10.28.230.91 with SMTP id d88mr8993231wmh.129.1486401195201; Mon, 06 Feb 2017 09:13:15 -0800 (PST) Received: from [192.168.43.221] (ip-37-188-238-11.eurotel.cz. [37.188.238.11]) by smtp.gmail.com with ESMTPSA id x25sm2551659wrx.27.2017.02.06.09.13.13 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 06 Feb 2017 09:13:14 -0800 (PST) Subject: Re: Cannot shutdown subscriber after DROP SUBSCRIPTION To: Fujii Masao References: <1289.1486012392@sss.pgh.pa.us> <20170203.105635.240968059.horiguchi.kyotaro@lab.ntt.co.jp> Cc: Kyotaro HORIGUCHI , Michael Paquier , Tom Lane , PostgreSQL-development From: Petr Jelinek Message-ID: Date: Mon, 6 Feb 2017 18:13:12 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.5.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -2.6 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-hackers Precedence: bulk Sender: pgsql-hackers-owner@postgresql.org On 06/02/17 17:33, Fujii Masao wrote: > On Sun, Feb 5, 2017 at 5:11 AM, Petr Jelinek > wrote: >> On 03/02/17 19:38, Fujii Masao wrote: >>> On Sat, Feb 4, 2017 at 12:49 AM, Fujii Masao wrote: >>>> On Fri, Feb 3, 2017 at 10:56 AM, Kyotaro HORIGUCHI >>>> wrote: >>>>> At Fri, 3 Feb 2017 01:02:47 +0900, Fujii Masao wrote in >>>>>> On Thu, Feb 2, 2017 at 2:36 PM, Michael Paquier >>>>>> wrote: >>>>>>> On Thu, Feb 2, 2017 at 2:13 PM, Tom Lane wrote: >>>>>>>> Kyotaro HORIGUCHI writes: >>>>>>>>> Then, the reason for the TRY-CATCH cluase is that I found that >>>>>>>>> some functions called from there can throw exceptions. >>>>>>>> >>>>>>>> Yes, but all LWLocks should be released by normal error recovery. >>>>>>>> It should not be necessary for this code to clean that up by hand. >>>>>>>> If it were necessary, there would be TRY-CATCH around every single >>>>>>>> LWLockAcquire in the backend, and we'd have an unreadable and >>>>>>>> unmaintainable system. Please don't add a TRY-CATCH unless it's >>>>>>>> *necessary* -- and you haven't explained why this one is. >>>>>> >>>>>> Yes. >>>>> >>>>> Thank you for the suggestion. I minunderstood that. >>>>> >>>>>>> Putting hands into the code and at the problem, I can see that >>>>>>> dropping a subscription on a node makes it unresponsive in case of a >>>>>>> stop. And that's just because calls to LWLockRelease are missing as in >>>>>>> the patch attached. A try/catch problem should not be necessary. >>>>>> >>>>>> Thanks for the patch! >>>>>> >>>>>> With the patch, LogicalRepLauncherLock is released at the end of >>>>>> DropSubscription(). But ISTM that the lock should be released just after >>>>>> logicalrep_worker_stop() and there is no need to protect the removal of >>>>>> replication slot with the lock. >>>>> >>>>> That's true. logicalrep_worker_stop returns after confirmig that >>>>> worker->proc is cleard, so no false relaunch cannot be caused. >>>>> After all, logicalrep_worker_stop is surrounded by >>>>> LWLockAcquire/Relase pair. So it can be moved into the funciton >>>>> and make the lock secrion to be more narrower. >>> >>> If we do this, Assert(LWLockHeldByMe(LogicalRepLauncherLock)) should be >>> removed and the comment for logicalrep_worker_stop() should be updated. >>> >>> Your approach may cause the deadlock. The launcher takes LogicalRepWorkerLock >>> while holding LogicalRepLauncherLock. OTOH, with your approach, >>> logicalrep_worker_stop() takes LogicalRepLauncherLock while holding >>> LogicalRepWorkerLock. >>> >>> Therefore I pushed the simple patch which adds LWLockRelease() just after >>> logicalrep_worker_stop(). >>> >>> Another problem that I found while reading the code is that the launcher can >>> start up the worker with the subscription that DROP SUBSCRIPTION just removed. >>> That is, DROP SUBSCRIPTION removes the target entry from pg_subscription, >>> but the launcher can see it and start new worker until the transaction for >>> DROP has been committed. >>> >> >> That was the reason why DropSubscription didn't release the lock in the >> first place. It was supposed to be released at the end of the >> transaction though. > > OK, I understood why you used the lock in that way. But using LWLock > for that purpose is not valid. > Yeah, I just tried to avoid what we are doing now really hard :) >>> To fix this issue, I think that DROP SUBSCRIPTION should take >>> AccessExclusiveLock on pg_subscription, instead of RowExclusiveLock, >>> so that the launcher cannot see the entry to be being removed. >>> >> >> The whole point of having LogicalRepLauncherLock is to avoid having to >> do this, so if we do this we could probably get rid of it. > > Yes, let's remove LogicalRepLauncherLock and lock pg_subscription > with AccessExclusive mode at the beginning of DROP SUBSCRIPTION. > Attached patch does this. > Okay, looks reasonable to me. -- Petr Jelinek http://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Training & Services -- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers