pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Yuri Budilov <yuri.budilov@hotmail.com>
Cc: rob stone <floriparob@gmail.com>
Cc: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
Subject: Re: JSON out of memory error on PostgreSQL 9.6.x
Date: Sun, 03 Dec 2017 19:39:29 -0500
Message-ID: <15699.1512347969@sss.pgh.pa.us> (raw)
In-Reply-To: <DM5PR13MB1755A0FC1BF6C3EDB9CC0DE3993C0@DM5PR13MB1755.namprd13.prod.outlook.com>
References: <81363279.2185591.1511207314550.ref@mail.yahoo.com>
	<81363279.2185591.1511207314550@mail.yahoo.com>
	<CACqosVSBOQxtCKEipCX=uaf9s6LbDYiCQ__2Het7pzU6pq+smg@mail.gmail.com>
	<CAO32GkCcVOn6kums8mo1hGFHmsKkqoWMxTfD6yk3EmD5e48eZA@mail.gmail.com>
	<CANvgUk4=-Y6bDyc-oMU_pwbxip1iLH1OLVFC-gKR+UjAZMxiGA@mail.gmail.com>
	<6207.1511235609@sss.pgh.pa.us>
	<df175ae4-7522-0ee3-67c0-98b822c5c3e7@hogranch.com>
	<DM5PR13MB17557D9BE4B9846D16D05405993F0@DM5PR13MB1755.namprd13.prod.outlook.com>
	<1512345677.5030.1.camel@gmail.com>
	<DM5PR13MB1755F0DD97887D07AFAFFF1F993C0@DM5PR13MB1755.namprd13.prod.outlook.com>
	<DM5PR13MB1755A0FC1BF6C3EDB9CC0DE3993C0@DM5PR13MB1755.namprd13.prod.outlook.com>

Yuri Budilov <yuri.budilov@hotmail.com> writes:
> The out-of-memory error happens if I also retrieve another JSON Column like so:

> CREATE TABLE csnbi_stg.junk4
> AS
> SELECT
>        json_rmq->>'totalSize' as totalSize, -- this plus array below causes out of memory error
>        json_array_elements(json_rmq -> 'orders'::text) AS orders
>   FROM csnbi_stg.stg_rmq_prod_test_json_raw_jmcn stg_rmq_json_raw

Ah.  The problem here is that "json_rmq->>'totalSize'" leaks some memory
on each execution, and it's executed again for each row produced by the
json_array_elements() SRF, and the memory can't be reclaimed until we've
finished the full output cycle for the SRF.  So the leakage (which is
more or less of the size of the JSON value, I think) accumulates across
150K executions in this example.

This is fixed as of v10.  It seems impractical to do anything about it
in previous release branches, although you could reformulate your query to
avoid it by not having any other expression evaluations occurring in the
same tlist as the SRF.  Something like this should work:

SELECT totalSize, json_array_elements(json_rmq -> 'orders'::text) AS orders
FROM
 (SELECT
       json_rmq->>'totalSize' as totalSize, json_rmq
  FROM csnbi_stg.stg_rmq_prod_test_json_raw_jmcn stg_rmq_json_raw
  WHERE ...
  OFFSET 0) ss;

			regards, tom lane




view thread (10+ messages)  latest in thread

Message-ID: <15699.1512347969@sss.pgh.pa.us>
Permalink:  ../15699.1512347969@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/15699.1512347969@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, yuri.budilov@hotmail.com, floriparob@gmail.com
  Subject: Re: JSON out of memory error on PostgreSQL 9.6.x
  In-Reply-To: <15699.1512347969@sss.pgh.pa.us>
  References: <81363279.2185591.1511207314550.ref@mail.yahoo.com> <81363279.2185591.1511207314550@mail.yahoo.com> <CACqosVSBOQxtCKEipCX=uaf9s6LbDYiCQ__2Het7pzU6pq+smg@mail.gmail.com> <CAO32GkCcVOn6kums8mo1hGFHmsKkqoWMxTfD6yk3EmD5e48eZA@mail.gmail.com> <CANvgUk4=-Y6bDyc-oMU_pwbxip1iLH1OLVFC-gKR+UjAZMxiGA@mail.gmail.com> <6207.1511235609@sss.pgh.pa.us> <df175ae4-7522-0ee3-67c0-98b822c5c3e7@hogranch.com> <DM5PR13MB17557D9BE4B9846D16D05405993F0@DM5PR13MB1755.namprd13.prod.outlook.com> <1512345677.5030.1.camel@gmail.com> <DM5PR13MB1755F0DD97887D07AFAFFF1F993C0@DM5PR13MB1755.namprd13.prod.outlook.com> <DM5PR13MB1755A0FC1BF6C3EDB9CC0DE3993C0@DM5PR13MB1755.namprd13.prod.outlook.com> <15699.1512347969@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