pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Lucas Fairchild-Madar <lucas.madar@gmail.com>
Cc: pgsql-bugs@postgresql.org
Subject: Re: json(b)_array_elements use causes very large memory usage when also referencing entire json document
Date: Fri, 06 Oct 2017 15:37:03 -0400
Message-ID: <4514.1507318623@sss.pgh.pa.us> (raw)
In-Reply-To: <CAJmoq7MKcfk2+X8A5OeQqedg9rypGoesOOg2pJQ3SzfNBE22TA@mail.gmail.com>
References: <CAJmoq7OJ=ntoLRE0R8h+_Dt8KoNifDhQQua_w2y_d1e7T6nL-w@mail.gmail.com>
	<1546.1507314635@sss.pgh.pa.us>
	<CAJmoq7OmXJ_G87D_He5j9kRSraANsuV7Oatxg3bhMsuawvOJ3Q@mail.gmail.com>
	<2376.1507315734@sss.pgh.pa.us>
	<CAJmoq7MKcfk2+X8A5OeQqedg9rypGoesOOg2pJQ3SzfNBE22TA@mail.gmail.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>

Lucas Fairchild-Madar <lucas.madar@gmail.com> 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



view thread (23+ messages)  latest in thread

Message-ID: <4514.1507318623@sss.pgh.pa.us>
Permalink:  ../4514.1507318623@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/4514.1507318623@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: tgl@sss.pgh.pa.us, lucas.madar@gmail.com
  Subject: Re: json(b)_array_elements use causes very large memory usage when also referencing entire json document
  In-Reply-To: <4514.1507318623@sss.pgh.pa.us>
  References: <CAJmoq7OJ=ntoLRE0R8h+_Dt8KoNifDhQQua_w2y_d1e7T6nL-w@mail.gmail.com> <1546.1507314635@sss.pgh.pa.us> <CAJmoq7OmXJ_G87D_He5j9kRSraANsuV7Oatxg3bhMsuawvOJ3Q@mail.gmail.com> <2376.1507315734@sss.pgh.pa.us> <CAJmoq7MKcfk2+X8A5OeQqedg9rypGoesOOg2pJQ3SzfNBE22TA@mail.gmail.com> <4514.1507318623@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox