pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Lazier alternative to row_to_json ?
2+ messages / 2 participants
[nested] [flat]

* Lazier alternative to row_to_json ?
@ 2021-11-01 11:18 Florents Tselai <florents.tselai@gmail.com>
  2021-11-01 14:05 ` Re: Lazier alternative to row_to_json ? Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 2+ messages in thread

From: Florents Tselai @ 2021-11-01 11:18 UTC (permalink / raw)
  To: pgsql-general <pgsql-general@lists.postgresql.org>

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.

Any ideas how I could bypass this? 






^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: Lazier alternative to row_to_json ?
  2021-11-01 11:18 Lazier alternative to row_to_json ? Florents Tselai <florents.tselai@gmail.com>
@ 2021-11-01 14:05 ` Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 2+ messages in thread

From: Tom Lane @ 2021-11-01 14:05 UTC (permalink / raw)
  To: Florents Tselai <florents.tselai@gmail.com>; +Cc: pgsql-general <pgsql-general@lists.postgresql.org>

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





^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2021-11-01 14:05 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2021-11-01 11:18 Lazier alternative to row_to_json ? Florents Tselai <florents.tselai@gmail.com>
2021-11-01 14:05 ` Tom Lane <tgl@sss.pgh.pa.us>

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