pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Chris Mair <chris@1006.org>
Cc: Enrico Bianchi <enrico.bianchi@ymail.com>
Cc: pgsql-general@postgresql.org
Subject: Re: Out of Memory
Date: Fri, 16 Jan 2015 11:23:42 -0500
Message-ID: <27898.1421425422@sss.pgh.pa.us> (raw)
In-Reply-To: <c3637fa69e84f4b4c779822ea59a7fc6@smtp.hushmail.com>
References: <54B84A83.9020003@ymail.com>
	<3681.1421371129@sss.pgh.pa.us>
	<54B8D2A8.4090904@ymail.com>
	<54B8E668.6070302@ymail.com>
	<54B8FCAA.1020500@ymail.com>
	<c3637fa69e84f4b4c779822ea59a7fc6@smtp.hushmail.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-general>

Chris Mair <chris@1006.org> writes:
> ...
> Interestingly, if you combine these, it quickly blows up! The following query with a limit 1000 already
> has a RES of well over 1GB. With larger limits it quickly thrashes my machine.
> enrico=# explain analyze
> SELECT substring((field_id ->'comment')::text,1,1),
>        jsonb_array_elements(field_id ->'comment')->>'id'
> FROM stage.fbcomment limit 1000;

Yeah.  The key point here is that jsonb_array_elements() returns a set,
that is one tuple per array element.  We've semi-deprecated use of
set-returning functions in SELECT output lists, and one of the reasons
is that memory management in the presence of set-returning functions
is a mess: we can't clear the SELECT's expression-evaluation context
until the SRF is done returning rows.  Some of the rows in this dataset
contain 'comment' arrays with over 1000 elements; that means that the
"substring()" expression gets executed over 1000 times without any
opportunity to reclaim memory.  And that expression will leak a pretty
fair amount of memory when dealing with a large field_id value.

Multiply that by the fact that the original query does this half a
dozen times, and you have a fairly large peak memory consumption.
The space does get reclaimed after each original table row, but that's
no comfort if you ran out before that.

It would probably be possible to go through the JSONB code and reduce
(though not eliminate) its appetite for memory in this sort of situation
--- for instance, freeing detoasted input arguments would help a lot.
But that's not likely to happen overnight.  In any case, you're right
that this is a very inefficiently expressed query; refactoring things
so that the jsonb_array_elements() call is done just once in a subquery
would probably help a great deal.

			regards, tom lane


-- 
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general



view thread (58+ messages)  latest in thread

Message-ID: <27898.1421425422@sss.pgh.pa.us>
Permalink:  ../27898.1421425422@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/27898.1421425422@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-general@postgresql.org
  Cc: tgl@sss.pgh.pa.us, chris@1006.org, enrico.bianchi@ymail.com
  Subject: Re: Out of Memory
  In-Reply-To: <27898.1421425422@sss.pgh.pa.us>
  References: <54B84A83.9020003@ymail.com> <3681.1421371129@sss.pgh.pa.us> <54B8D2A8.4090904@ymail.com> <54B8E668.6070302@ymail.com> <54B8FCAA.1020500@ymail.com> <c3637fa69e84f4b4c779822ea59a7fc6@smtp.hushmail.com> <27898.1421425422@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