Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1e0epo-0008Hj-Rg for pgsql-bugs@arkaria.postgresql.org; Sat, 07 Oct 2017 02:27: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 1e0epo-0000J1-9w for pgsql-bugs@arkaria.postgresql.org; Sat, 07 Oct 2017 02:27: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 1e0ep5-0007Sk-RJ for pgsql-bugs@postgresql.org; Sat, 07 Oct 2017 02:26:40 +0000 Received: from out1-smtp.messagingengine.com ([66.111.4.25]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1e0ep2-0005sw-SU for pgsql-bugs@postgresql.org; Sat, 07 Oct 2017 02:26:38 +0000 Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailout.nyi.internal (Postfix) with ESMTP id 0C78920B6F; Fri, 6 Oct 2017 22:26:35 -0400 (EDT) Received: from frontend1 ([10.202.2.160]) by compute5.internal (MEProxy); Fri, 06 Oct 2017 22:26:35 -0400 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:x-sasl-enc; s=fm1; bh=I0zz+c4+am84mDK33z VKivZLOHjuTuZ/an+QAZnP5E0=; b=Yg6pYY80ToKNsPss2f0Ii+wbA7IhwnXsht qgfzftBa2vlukwJNkX4UZvVQv+oMgq9P9JJNEhl3d12V10UeGdRDQl5vsYcufyI/ U4pYp+Aa0EK+qjkXSsyuXUmWoEn4Sqda5Czy9Ax8PurA5J2EprI+TTSSVO/6Mnrp G+Q2dnRHFIDmuA06+zhcYIlgaPmxe5cjDqTRn6AU94PNXIWUJ1TztX1srHhwl4Ci NuiEkURFq+7ZkCt0HPD7JtwGnVoEyngjf+n90+1o7b0DSsJCNmZ18mVALgs6+yKq LLwmNsMZMjj2ibmnJgHMdaeqwqqPiAFxYNjRMQxDs4EA0f9hu3Wg== X-ME-Sender: X-Sasl-enc: e77aWpeUi20oWjcI5eVJruYdgJU3cp0t2RM8D1R5w8yO 1507343194 Received: from intern.anarazel.de (unknown [136.24.76.72]) by mail.messagingengine.com (Postfix) with ESMTPA id AF8107E3E0; Fri, 6 Oct 2017 22:26:34 -0400 (EDT) Date: Fri, 6 Oct 2017 19:26:33 -0700 From: Andres Freund To: Tom Lane Cc: Lucas Fairchild-Madar , pgsql-bugs@postgresql.org Subject: Re: json(b)_array_elements use causes very large memory usage when also referencing entire json document Message-ID: <20171007022633.kckfl2g7mnmtap2c@alap3.anarazel.de> References: <1546.1507314635@sss.pgh.pa.us> <2376.1507315734@sss.pgh.pa.us> <4514.1507318623@sss.pgh.pa.us> <20171007002116.2fqr6z26byhsjwoe@alap3.anarazel.de> <2219.1507336101@sss.pgh.pa.us> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <2219.1507336101@sss.pgh.pa.us> List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org On 2017-10-06 20:28:21 -0400, Tom Lane wrote: > Andres Freund writes: > > Yea, that should be quite doable, slightly annoying to need more than > > one expr context, but that seems unavoidable. Should even be doable for > > SFRM_ValuePerCall? SFRM_Materialize isn't a "central" problem, given it > > properly manages memory for the tuplestore and filling the tuplestore is > > an internal matter. > > I'm not really convinced whether there's an issue for ValuePerCall SRFs. > We've not had complaints about them, and I would think we would if that > feature alone would create an issue. But I've not thought about it hard, > nor tested. I've just played around with this. ValuePerCall SRFs are fine with called in a short-lived context (they're required to be able to, as documented in xfunc.sgml), so is SFRM_Materialize. The only thing to be careful about is the *arguments* to the function, those need to live long enough in the ValuePerCall case. I'm not entirely sure what the best way to code the memory allocation is. I've just prototyped this by switching to econtext->ecxt_per_query_memory around ExecEvalFuncArgs, and resetting econtext->ecxt_per_tuple_memory before every ExecProjectSRF call. But obviously we don't want to leak the arguments this way. Easiest seems to be to just pass in a separate econtext for the arguments, and reset that explicitly. Unless you've a better suggestion, I'll clean up my hack and propose it. Greetings, Andres Freund -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs