pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Florents Tselai <florents.tselai@gmail.com>
Cc: pgsql-general <pgsql-general@lists.postgresql.org>
Subject: Re: Lazier alternative to row_to_json ?
Date: Mon, 01 Nov 2021 10:05:38 -0400
Message-ID: <1131853.1635775538@sss.pgh.pa.us> (raw)
In-Reply-To: <43A856C7-353A-49CA-A99F-C1E81E8D5935@gmail.com>
References: <43A856C7-353A-49CA-A99F-C1E81E8D5935@gmail.com>

Florents Tselai <florents.tselai@gmail.com> 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’s 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





view thread (2+ messages)

Message-ID: <1131853.1635775538@sss.pgh.pa.us>
Permalink:  ../1131853.1635775538@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/1131853.1635775538@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, florents.tselai@gmail.com
  Subject: Re: Lazier alternative to row_to_json ?
  In-Reply-To: <1131853.1635775538@sss.pgh.pa.us>
  References: <43A856C7-353A-49CA-A99F-C1E81E8D5935@gmail.com> <1131853.1635775538@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