Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fiRct-000358-Jj for pgsql-bugs@arkaria.postgresql.org; Wed, 25 Jul 2018 21:47:20 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fiRco-0004ig-Sr for pgsql-bugs@arkaria.postgresql.org; Wed, 25 Jul 2018 21:47:14 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fiRcn-0004iV-6c; Wed, 25 Jul 2018 21:47:14 +0000 Received: from wout2-smtp.messagingengine.com ([64.147.123.25]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fiRci-0006L4-86; Wed, 25 Jul 2018 21:47:12 +0000 Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailout.west.internal (Postfix) with ESMTP id AB042228; Wed, 25 Jul 2018 17:47:04 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute5.internal (MEProxy); Wed, 25 Jul 2018 17:47:04 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=anarazel.de; h= cc:content-type:date:from:in-reply-to:message-id:mime-version :references:subject:to:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; bh=hpBcwzb26d1rqyzm11cFtuukJKgpHEF0+coA9BOKvAI=; b=Dxm9PPTO wyc5yNCgJL6MIOaNL0FJ/qgCIpMRNNHJFNUFuJTWEg1En8K0S6+WVbMJ2YOVf9gc rUlGXfsZXmorBvCNLx8b/c6D8Ft4QU3S8BjL4n+NDnMAgVqDVfMWLk7nhqoIrsgU 2l1nsxrgDzCcr2yAXLnSozPc9PKWJFYGoiEo9r9UzF2CTtRiyongO0WH+lV4fzfm ShW1Gvzcngw3XwsKdQZvYJWpmCAxLUmvhixU7vZs6sOzkxVRCjj+ZUvpn37k7mvt +yB/6FxivfYj+NIrUDt0U/h1oFLd7QoPwYTeGLakRD1iY7w6GVdStvtv/p7qTgRe wv0XpgFmVoQkoA== 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-sender :x-me-sender:x-sasl-enc; s=fm3; bh=hpBcwzb26d1rqyzm11cFtuukJKgpH EF0+coA9BOKvAI=; b=R/K6pt4J+F0cK7Pv3Pwsx/BAOm4UruQOHuSYoCi4iHs6R HrvHiq0mbWb/vqyiZGDErCQkL9WXjCYfy7lxn7k5QZWiaEQvS9W4G1xij8aWnntt dv4J+aQuySozkEK2eRNBoLBAwn5TAJnB/ImwjhB57clHHVOsSFurNcE6A3F2hSSg sAga0m9L7zmSCtxcSJsdjiODtF7OAbbBLHDnVEAJJz8/PNXh1L3XzXrIzsTVsgFi JdO0oLd6wYh9mF45+Kp1ugCxGnTRm+40Cx8rE5zAYV9+M4k1NxCKfh3M/wK0sBlk j/thOazjyanXNScOhq5aCCRqbH0d+D//Bwu5Hlh4Q== X-ME-Proxy: X-ME-Sender: Received: from intern.anarazel.de (c-98-210-140-171.hsd1.ca.comcast.net [98.210.140.171]) by mail.messagingengine.com (Postfix) with ESMTPA id 50189E4445; Wed, 25 Jul 2018 17:47:03 -0400 (EDT) Date: Wed, 25 Jul 2018 14:47:02 -0700 From: Andres Freund To: Dmitry Dolgov <9erthalion6@gmail.com> Cc: Michael Paquier , PostgreSQL-development , PostgreSQL mailing lists Subject: Re: LLVM jit and matview Message-ID: <20180725214702.6v7jxi2zkcv3exar@alap3.anarazel.de> References: <20180710001958.GD1661@paquier.xyz> <20180725024956.6gxqscgk5fmuqbb7@alap3.anarazel.de> <20180725054857.GB6660@paquier.xyz> <20180725154129.lvaabpypucdajliz@alap3.anarazel.de> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20180725154129.lvaabpypucdajliz@alap3.anarazel.de> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Hi, On 2018-07-25 08:41:29 -0700, Andres Freund wrote: > Oh, interesting. It only crashes when compiling LLVM without LLVM's > asserts enabled, even when using exactly the same LLVM checkout for both > builds. No idea what that's about, yet. Oh, well, this took me longer than it should have. The problem, to my possibly demented mind, is actually kinda funny: The problematic expression in the query invokes ExecEvalRowNull() (it's EEOP_WHOLEROW followed by EEOP_NULLTEST_ROWISNOTNULL). With inlining enabled, that ends up inlining ExecEvalRowNull(), ExecEvalRowNullInt(), get_cached_rowtype(), ShutdownTupleDescRef() (largely because these are static functions that'd otherwise prevent ExecEvalRowNull from being inlined). get_cached_rowtype() does /* Need to register shutdown callback to release tupdesc */ RegisterExprContextCallback(econtext, ShutdownTupleDescRef, PointerGetDatum(cache_field)); which, in the inlined version, means that ShutdownTupleDescRef is the inlined copy. So, the query execution sets up a shutdown callback, that is a JITed function. But note standard_ExecutorEnd(): /* release JIT context, if allocated */ if (estate->es_jit) { jit_release_context(estate->es_jit); estate->es_jit = NULL; } /* * Must switch out of context before destroying it */ MemoryContextSwitchTo(oldcontext); /* * Release EState and per-query memory context. This should release * everything the executor has allocated. */ FreeExecutorState(estate); FreeExecutorState(), which calls the shutdown callbacks, is executed *after* the JIT context is destroyed! Thereby causing the segfault, as the shutdown callback now invokes unmapped memory. The fix is easy, releasing the JIT context should just happen in FreeExecutorState(). Only thing is that that function has the following comment in the header: * Note: this is not responsible for releasing non-memory resources, * such as open relations or buffer pins. But it will shut down any * still-active ExprContexts within the EState. That is sufficient * cleanup for situations where the EState has only been used for expression * evaluation, and not to run a complete Plan. I don't really think throwing away functions is a violation of that, but I think it's possible to argue the other way? Greetings, Andres Freund