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 1rUQAS-00AmfC-G2 for pgsql-bugs@arkaria.postgresql.org; Mon, 29 Jan 2024 11:51:13 +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 1rUQAR-00DfbR-LE for pgsql-bugs@arkaria.postgresql.org; Mon, 29 Jan 2024 11:51:11 +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 1rUQ8F-00DdLh-0o for pgsql-bugs@lists.postgresql.org; Mon, 29 Jan 2024 11:48:55 +0000 Received: from meesny.iki.fi ([195.140.195.201]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rUQ8A-003xfY-Ed for pgsql-bugs@lists.postgresql.org; Mon, 29 Jan 2024 11:48:53 +0000 Received: from [192.168.1.115] (dsl-hkibng22-54f8db-125.dhcp.inet.fi [84.248.219.125]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: hlinnaka) by meesny.iki.fi (Postfix) with ESMTPSA id 4TNmlx5BmjzySy; Mon, 29 Jan 2024 13:48:45 +0200 (EET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=iki.fi; s=meesny; t=1706528926; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=R3GohPtA5ZlWN5JwE+uiNSAWk4iNHjd9q3pDcDIQpf0=; b=WBNbpHEmYFG+t00m+YK+GTtp4QXE20b9x1WvDGOiPiwNdLjceoSjKcckzWMzjpTPoQxtQw G0GoOZ6zgvpRDvlhEOh2uyJyujvXrsQcGdKpzEGSMA3Uuk/z2PUW6jfUZPV+3wC+hk3/Rq 8l+FyFIgDTYKRmlw4CZIR+d+udCjEv0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=iki.fi; s=meesny; t=1706528926; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=R3GohPtA5ZlWN5JwE+uiNSAWk4iNHjd9q3pDcDIQpf0=; b=FZ4Wn0hVhGd+Et+o+LdKIxU9qRfb+n6DqOoFTnvEXEKDJZyHZrX8DDW5aVSvFwY3qiR7it qxVv+crVzlCtT/y35m3yXzU1ynGUXEecve0MMDnFXrJDqzMdHv3Z3fdPQ7p7HXpDvXiyv/ 27N0Gj69YF0w28kWRUquf90OQrKmU3k= ARC-Authentication-Results: i=1; ORIGINATING; auth=pass smtp.auth=hlinnaka smtp.mailfrom=hlinnaka@iki.fi ARC-Seal: i=1; s=meesny; d=iki.fi; t=1706528926; a=rsa-sha256; cv=none; b=bFtWXoaGin/nWhW7/Ezpg4Fs6S87UlF0WtqN31GpEL9eOsa0Vcl3a1lj5kZw728mruoLEL dqlcV3gJyTMwHfjRjRLZ5Ql4w/ooeeqJ1mbVOVEAvT1WHwhqc6BjdqBNM2Ebgc9BtrtZPl /diIEugd3L2FMgkj80ZbQzdC0YdktGY= Message-ID: <72ce1cf6-d718-446e-bdcf-021a24c57c84@iki.fi> Date: Mon, 29 Jan 2024 13:48:44 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: MarkBufferDirty Assert held LW_EXCLUSIVE lock fail when ginFinishSplit To: feichanghong Cc: Michael Paquier , pgsql-bugs References: <053bc845-3cb5-445e-8ea4-00f6c678ee25@iki.fi> Content-Language: en-US From: Heikki Linnakangas In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 23/01/2024 05:39, feichanghong wrote: > >> On Jan 23, 2024, at 04:47, Heikki Linnakangas wrote: >> >> On 22/01/2024 18:21, feichanghong wrote: >>>> From a performance point of view, this doesn't matter. Incomplete >>>> split are extremely rare. For convenience, though, I added a new >>>> function specifically for handling these "leftover" incomplete >>>> splits as opposed to finishing a split that you just made, which >>>> performs the lock-upgrade. See attached. I think that helps with >>>> readability, and makes it less likely that we'll forget the >>>> lock-upgrade in the future if the insertion code is refactored. >>> I think that the lock-upgrade in the ginFinishOldSplit function is unsafe >>> because it violates the requirement of the ginStepRight function that >>> "The next page is locked first, before releasing the current page.” >> >> Good point. >> >> I started to work on a more invasive patch that would move the >> ginFinishOldSplit() calls to ginTraverseLock() and ginStepRight(), >> doing the interlocking correctly. That makes life easier for the >> callers; they don't need to deal with incomplete-splits anymore. >> >> But then I read the Page deletion section in the README and understood >> that my earlier patch is safe, after all. The lock-coupling in >> ginStepRight() is only needed for searches, not for inserts. There is >> another mechanism that prevents concurrent page deletions during an >> insert: VACUUM holds a cleanup-lock on the root page. >> >> Does that make sense, or am I missing something? Here's the same patch >> as before, with some extra comments to explain why it's safe. > > From my understanding, you are right. The README includes the following > explanation: > ginScanToDelete() traverses the whole tree in depth-first manner. It starts > from the full cleanup lock on the tree root.  This lock prevents all the > concurrent insertions into this tree while we're deleting pages. However, > there are still might be some in-progress readers, who traversed root before > we locked it. > > For insert, ginFindLeafPage maintains a pin on the relevant pages along the > path, naturally retaining the pin on the root page as well. Before calling > ginScanToDelete to delete a page, it secures an exclusive lock on the > root page > and no other backend holds a pin on the root page through > LockBufferForCleanup. I have pushed this fix. Thanks for the report and the review! -- Heikki Linnakangas Neon (https://neon.tech)