Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mhXwX-0007eB-3M for pgsql-general@arkaria.postgresql.org; Mon, 01 Nov 2021 14:05:45 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mhXwU-000541-QT for pgsql-general@arkaria.postgresql.org; Mon, 01 Nov 2021 14:05:42 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mhXwU-00053Y-Ga for pgsql-general@lists.postgresql.org; Mon, 01 Nov 2021 14:05:42 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mhXwS-0003kK-Co for pgsql-general@lists.postgresql.org; Mon, 01 Nov 2021 14:05:41 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 1A1E5cOL1131854; Mon, 1 Nov 2021 10:05:38 -0400 From: Tom Lane To: Florents Tselai cc: pgsql-general Subject: Re: Lazier alternative to row_to_json ? In-reply-to: <43A856C7-353A-49CA-A99F-C1E81E8D5935@gmail.com> References: <43A856C7-353A-49CA-A99F-C1E81E8D5935@gmail.com> Comments: In-reply-to Florents Tselai message dated "Mon, 01 Nov 2021 13:18:46 +0200" MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-ID: <1131852.1635775538.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Mon, 01 Nov 2021 10:05:38 -0400 Message-ID: <1131853.1635775538@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Florents Tselai writes: > I have the following simple query > select row_to_json(d) from documents d > The output of this goes to script that expects new-line-delimited stream= of JSON objects. > But as-is, ti looks like the server=E2=80=99s memory fills-up before ti = starts emitting results. Usually, when we hear about query result size problems, they're on the client side not the server side ... so are you sure about your diagnosis above? The server flushes its results to the client after each row, but common client libraries (particularly libpq) try to accumulate the whole query result before returning it to the application. If the problem is indeed inside libpq, you could alleviate it by using single-row mode [1], assuming you're using reasonably late-model libpq. Another solution could be to use COPY, which again provides more stream-based access to the output; though you'd have to provide a layer of dequoting and line-separation logic. regards, tom lane [1] https://www.postgresql.org/docs/current/libpq-single-row-mode.html