Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eLenI-0000XB-Hq for pgsql-general@arkaria.postgresql.org; Mon, 04 Dec 2017 00:39:36 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eLenH-0005fm-SO for pgsql-general@arkaria.postgresql.org; Mon, 04 Dec 2017 00:39:35 +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 1eLenH-0005fd-Hy for pgsql-general@lists.postgresql.org; Mon, 04 Dec 2017 00:39:35 +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.89) (envelope-from ) id 1eLenF-0004fv-4m for pgsql-general@lists.postgresql.org; Mon, 04 Dec 2017 00:39:34 +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 vB40dTff015700; Sun, 3 Dec 2017 19:39:29 -0500 From: Tom Lane To: Yuri Budilov cc: rob stone , "pgsql-general@lists.postgresql.org" Subject: Re: JSON out of memory error on PostgreSQL 9.6.x In-reply-to: References: <81363279.2185591.1511207314550.ref@mail.yahoo.com> <81363279.2185591.1511207314550@mail.yahoo.com> <6207.1511235609@sss.pgh.pa.us> , ,<1512345677.5030.1.camel@gmail.com>, Comments: In-reply-to Yuri Budilov message dated "Mon, 04 Dec 2017 00:23:18 +0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <15698.1512347969.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Sun, 03 Dec 2017 19:39:29 -0500 Message-ID: <15699.1512347969@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Yuri Budilov writes: > The out-of-memory error happens if I also retrieve another JSON Column l= ike so: > CREATE TABLE csnbi_stg.junk4 > AS > SELECT > json_rmq->>'totalSize' as totalSize, -- this plus array below cau= ses 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 order= s 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