Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pfu6G-00055O-Ec for pgsql-hackers@arkaria.postgresql.org; Sat, 25 Mar 2023 02:57:48 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pfu6F-0005LC-89 for pgsql-hackers@arkaria.postgresql.org; Sat, 25 Mar 2023 02:57:47 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pfu6E-0005L3-5a for pgsql-hackers@lists.postgresql.org; Sat, 25 Mar 2023 02:57:46 +0000 Received: from out3-smtp.messagingengine.com ([66.111.4.27]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pfu6B-0002bJ-GK for pgsql-hackers@postgresql.org; Sat, 25 Mar 2023 02:57:45 +0000 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id A4D175C0120; Fri, 24 Mar 2023 22:57:42 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute4.internal (MEProxy); Fri, 24 Mar 2023 22:57:42 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=anarazel.de; h= cc:content-type:content-type:date:date:from:from:in-reply-to :message-id:mime-version:reply-to:sender:subject:subject:to:to; s=fm2; t=1679713062; x=1679799462; bh=0KElgU7mG+Ppkt8lZ0lXjx+Tz 2ZVTiMFoz+pfFFgC3M=; b=IFYIjSX6m6ARtd7eSVGbAVQr0zUK6wN3PNy3ijJ4D T5u1TJ6oSh8e//mNJbf7uu9Xp3qk1zT3A0o+4qaICY+HjLpqN2/GOwDQo4rZTG2Z uxLXRVUCQtm9GAmjr3wWZlq2aAucvlyJ/jMyeItfqKZoRT1mqN4nxjZJqmEVr9Ey MBvWuLNixMTy0NU6P6S4t04agZfUjjmNxRS7A+42p97WpkOrKQeGPQDJ4p51bHim gWRSyUu57ghCZCF06IOSa0ynKe+aITvgBXZ2QBtX+xdC2lCPmGgnP40ep4vY+sRI Pjn357d3susqtVqkOC/5XGuN7mpx9TwESUyafVf0SGK5g== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:message-id :mime-version:reply-to:sender:subject:subject:to:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm2; t= 1679713062; x=1679799462; bh=0KElgU7mG+Ppkt8lZ0lXjx+Tz2ZVTiMFoz+ pfFFgC3M=; b=Zal6+QAT25bQgpcWg7adf6IymGtroAP/tRmo5Vh8v/P0E07/9sc kfjyxCx0TwwRytVZUkpX2OxxxMzvECzDP6b8XQudF3ruFH9DpMcMqf7zXxlausll PFPf0LVZQNPJ1lrOvx2NxopPdfJtSlFMybB1wihpAP0Hi3jJlQua49bPfk+bXkMU 8v3R6DKOqZ+S9ZRhFQygOChuro6qVTTpyw7Wq7lWv9W5jRCty+q+ZRWq4LJ6bAhW B/5yYzyB8oA5tGw0FLV04jCBKEmhUVl89VkmPBfJqTWxqNhtaykr4Mnx+t8MSqgE bg+iEuQ7mlCeDIrxr511AxGnfN2RDTIPktA== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvhedrvdegjedgheefucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvffukfggtggusehttdertddttddvnecuhfhrohhmpeetnhgurhgvshcu hfhrvghunhguuceorghnughrvghssegrnhgrrhgriigvlhdruggvqeenucggtffrrghtth gvrhhnpeeivdeileegueeutdefudfhfeethefhvedvleejtefguefgtdeltddutedvheek ffenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegrnh gurhgvshesrghnrghrrgiivghlrdguvg X-ME-Proxy: Feedback-ID: id4a34324:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 24 Mar 2023 22:57:41 -0400 (EDT) Date: Fri, 24 Mar 2023 19:57:40 -0700 From: Andres Freund To: pgsql-hackers@postgresql.org, Tomas Vondra , Anastasia Lubennikova , Pavan Deolasee , Jeff Janes Subject: hio.c does visibilitymap_pin()/IO while holding buffer lock Message-ID: <20230325025740.wzvchp2kromw4zqz@awork3.anarazel.de> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi, Starting with commit 7db0cd2145f2bce84cac92402e205e4d2b045bf2 Author: Tomas Vondra Date: 2021-01-17 22:11:39 +0100 Set PD_ALL_VISIBLE and visibility map bits in COPY FREEZE RelationGetBufferForTuple does /* * The page is empty, pin vmbuffer to set all_frozen bit. */ if (options & HEAP_INSERT_FROZEN) { Assert(PageGetMaxOffsetNumber(BufferGetPage(buffer)) == 0); visibilitymap_pin(relation, BufferGetBlockNumber(buffer), vmbuffer); } while holding a buffer lock. visibilitymap_pin() reads pages, if vmbuffer doesn't already point to the right block. The lock ordering rules are to lock VM pages *before* locking heap pages. I think the reason this hasn't yet bitten us badly, is that INSERT_FROZEN effectively requires that the relation is access exclusive locked. There shouldn't be other backends locking multiple buffers in the relation (bgwriter / checkpointer can lock a single buffer at a time, but that's it). I see roughly two ways forward: 1) We add a comment explaining why it's safe to violate lock ordering rules in this one situation 2) Change relevant code so that we only return a valid vmbuffer if we could do so without blocking / IO and, obviously, skip updating the VM if we couldn't get the buffer. Greetings, Andres Freund