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 1lXaVa-00037U-VP for pgsql-bugs@arkaria.postgresql.org; Sat, 17 Apr 2021 02:16:31 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lXaVY-0000XX-TI for pgsql-bugs@arkaria.postgresql.org; Sat, 17 Apr 2021 02:16:28 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lXaVX-0000XQ-D0 for pgsql-bugs@lists.postgresql.org; Sat, 17 Apr 2021 02:16:28 +0000 Received: from [66.111.4.25] (helo=out1-smtp.messagingengine.com) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lXaVO-0001D5-CM for pgsql-bugs@lists.postgresql.org; Sat, 17 Apr 2021 02:16:27 +0000 Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailout.nyi.internal (Postfix) with ESMTP id 004465C0213; Fri, 16 Apr 2021 22:16:05 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute1.internal (MEProxy); Fri, 16 Apr 2021 22:16:06 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=anarazel.de; h= date:from:to:cc:subject:message-id:references:mime-version :content-type:in-reply-to; s=fm1; bh=O+/kBH8YVo+qnwACj9IXySggZ/P j+0U0FVZuFftkkVw=; b=B9Ho2lhP4o+RZBdA1Br6jTiXo3wSMqgABv26NQ/XffE pzAnPv5oZZJ7/9QEPZ2PLaiDoUS5ruwkwDY0hlgMhXlDB7tXiksl0jCU+RJpV8TR twCQx+TOkluEZuVIacA7R2uA7VR/d8aK9UD/WdMEOZTjFXOyYP5/82cXEud1/Qja ZAMpmcKTDkKuRGM7Mdm4/Zdvl6OtAJm8kie/FPxkEh0xqKlTcZk+Esx/iRaEydj1 zzTw6b9Ae76LJETvhUF7Gf5y6wu08zdp2YOMK6LzrPpDYYCot65jwqhQzPMQ9E6H uGAuYF69ySFBl5n7ksE7BZS0WqelaNHsCxXlhFzcCTQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm2; bh=O+/kBH 8YVo+qnwACj9IXySggZ/Pj+0U0FVZuFftkkVw=; b=Q2dHJB2Kmekf1hXSFWckw8 aEHbEL54TGRNXS25OiHCCanM2rTjIh5tkzO5Db2dANm+USxmpA4ejhokheyZDccj D0vH1RF0hlpDdNaGgY2CtfpRHnsIu04tiKETBB0fTf/KQuSAr+3I2yu2qagWbWxW zlZdOwWReL+qsj8RZOHOKil9P13OQcPn6NtyMa24rpiGY14BDpNChYMkMO95K7HT UkQdip3YjNoE/9T+x51DChael/KYi2mT59zXwHPN4LbujGtkaHz/BzHIcqsA03x4 u56WyqJHuEK9K8syq2c2Q/ypFXCxbjxLA9GGrgTyEy8J77/29H7SpSYPFqQcatew == X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduledrudeliedgheeiucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvffukfhfgggtuggjsehmtderredttddvnecuhfhrohhmpeetnhgurhgv shcuhfhrvghunhguuceorghnughrvghssegrnhgrrhgriigvlhdruggvqeenucggtffrrg htthgvrhhnpefgueehveehhedtuefgvdejuedujeeukeelteekkeevjeduheegvddtfeev heelkeenucfkphepuddtjedrudegvddrfeefrddutdefnecuvehluhhsthgvrhfuihiivg eptdenucfrrghrrghmpehmrghilhhfrhhomheprghnughrvghssegrnhgrrhgriigvlhdr uggv X-ME-Proxy: Received: from intern.anarazel.de (107-142-33-103.lightspeed.sntcca.sbcglobal.net [107.142.33.103]) by mail.messagingengine.com (Postfix) with ESMTPA id 2EB48108005B; Fri, 16 Apr 2021 22:16:04 -0400 (EDT) Date: Fri, 16 Apr 2021 19:16:02 -0700 From: Andres Freund To: Jaime Casanova Cc: Kurt Roeckx , Tom Lane , pgsql-bugs@lists.postgresql.org, Justin Pryzby Subject: Re: BUG #16707: Memory leak Message-ID: <20210417021602.7dilihkdc7oblrf7@alap3.anarazel.de> References: <20201109220329.GC391173@roeckx.be> <2002012.1604960437@sss.pgh.pa.us> <20201110043127.sfkyvvjqy7x3er5k@alap3.anarazel.de> <20201110081120.GF391173@roeckx.be> <20201110193517.t5hbqkya67gb23rq@alap3.anarazel.de> <20201110195039.GG391173@roeckx.be> <20201110224516.GA568918@roeckx.be> <20201111013808.4cev55uxq32bd6dd@alap3.anarazel.de> <20210407162835.GB2857@ahch-to> <7acc8678-df5f-4923-9cf6-e843131ae89d@www.fastmail.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="um6u6gsnflasiacx" Content-Disposition: inline In-Reply-To: <7acc8678-df5f-4923-9cf6-e843131ae89d@www.fastmail.com> X-Host-Lookup-Failed: Reverse DNS lookup failed for 66.111.4.25 (deferred) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --um6u6gsnflasiacx Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Hi, On 2021-04-07 09:31:54 -0700, Andres Freund wrote: > On Wed, Apr 7, 2021, at 09:28, Jaime Casanova wrote: > > I was bit by this too while testing something. I thought it could have been > > a problem caused by the support of llvm 12? but given that this report > > is for pg12, it seems this is older than that. > > I hope to fix it soon - I have two different mostly working > fixes. I'll get back to it once the dust around the freeze settles. Quick recap: The issue is that inlining needs to re-read the modules with the code that's being inlined, because LLVMs IR linker is destructive. Copying the modules alone is not sufficient, because the IR linker modifies the types, and IR types are cross-module entities (just the names are modified, but that's enough to cause problems). Whenever a module is read, it does *not* reuse existing struct types, but instead creates them from scratch (type uniquing would be expensive, and names are not sufficient identification). I have evaluated two approaches to fixing the issue: 1) Write a new IR linker module for LLVM that is not destructive. The pro arguments for that is that that improves inlining performance substantially. But it's a fair bit of new code, which I think makes it unsuitable for fixing the bug in the back branches. 2) Occasionally dispose of the current LLVMContext and create a new one. The LLVMContext is where modules and types live, so disposing the context releases all the accumulated types. Unfortunately we can't just create a separate LLVMContext for every query, as types etc cannot be reused across contexts, and reloading all the information needed for JITing would be too expensive. A second disadvantage of this approach is that it's somewhat invasive, as a fair bit of code needs to be changed not to reference the "global context" we were using so far. But that's fairly mechanical, and not too hard to audit. While I hope to continue to look into 1), it's clearly v15+ material. See the attached patches for a draft of how 2) would look like. The last of the attached patches is the main logic change, whereas the first contains the changes to make all context references explicit. The last patch also contains a number of TODOs in the commit message. Greetings, Andres Freund --um6u6gsnflasiacx Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="v1-0001-wip-llvmjit-Use-explicit-LLVMContextRef.patch"