Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1e0YRW-0003JB-1D for pgsql-bugs@arkaria.postgresql.org; Fri, 06 Oct 2017 19:37:54 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1e0YRV-0004SC-In for pgsql-bugs@arkaria.postgresql.org; Fri, 06 Oct 2017 19:37:53 +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 1e0YQl-0000la-JN for pgsql-bugs@postgresql.org; Fri, 06 Oct 2017 19:37:07 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1e0YQj-0005Om-32 for pgsql-bugs@postgresql.org; Fri, 06 Oct 2017 19:37:06 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id v96Jb3p7004515; Fri, 6 Oct 2017 15:37:03 -0400 From: Tom Lane To: Lucas Fairchild-Madar cc: pgsql-bugs@postgresql.org Subject: Re: json(b)_array_elements use causes very large memory usage when also referencing entire json document In-reply-to: References: <1546.1507314635@sss.pgh.pa.us> <2376.1507315734@sss.pgh.pa.us> Comments: In-reply-to Lucas Fairchild-Madar message dated "Fri, 06 Oct 2017 12:11:40 -0700" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <4509.1507318623.1@sss.pgh.pa.us> Date: Fri, 06 Oct 2017 15:37:03 -0400 Message-ID: <4514.1507318623@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 Lucas Fairchild-Madar writes: > Sure, here's a bash script. Assumes you have PGDATABASE, etc set. > Verified this also blows up on pg 10. I haven't verified the new patch. Oh, I see the problem. This is a different animal, because it's actually an intra-row leak, as it were. What you've got is select data->'id', jsonb_array_elements(data->'items') from kaboom; where the SRF jsonb_array_elements() emits a lot of values. For each of those values, data->'id' gets evaluated over again, and we can't reclaim memory in the per-tuple context until we've finished the whole cycle for the current row of "kaboom". So a leak would occur in any case ... but it's particularly awful in this case, because data->'id' involves detoasting the rather wide value of "data", which is then promptly leaked. So the total memory consumption is more or less proportional to O(N^2) in the length of "data". This has been like this since forever, and it's probably impractical to do anything about it pre-v10, given the unstructured way that targetlist SRFs are handled. You could dodge the problem by moving the SRF to a lateral FROM item: select data->'id', ja from kaboom, lateral jsonb_array_elements(data->'items') as ja; (The LATERAL keyword is optional here, but I like it because it makes it clearer what's happening.) As of v10, it might be possible to fix this for the tlist case as well, by doing something like using a separate short-lived context for the non-SRF tlist items. regards, tom lane -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs