Re: libpq: How are result sets fetched behind the scene?

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Christian Barthel <bch(at)online(dot)de>
Cc: Sebastien FLAESCH <sf(at)4js(dot)com>, pgsql-sql(at)lists(dot)postgresql(dot)org
Subject: Re: libpq: How are result sets fetched behind the scene?
Date: 2019-09-08 19:35:38
Message-ID: 24795.1567971338@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Christian Barthel <bch(at)online(dot)de> writes:
> Sebastien FLAESCH <sf(at)4js(dot)com> writes:
>> Is the whole result set fetched to the client app, not matter what row
>> number is provided to the first PQgetvalue() call (or similar API call
>> on result set data or meta-data)?

> I have tested this as well and came to the same result as you.
> The entire result set seems to be fetched at once.

This must be so, and is documented as being so, because the abstraction
that libpq provides is that a query either succeeds or fails. It cannot
fetch a few rows and then decide that the query has succeeded; it has to
collect the whole input before it knows there will be no late failure.

Recent releases of libpq have an API to let you examine the rows as
they come in, but then it's on your head to deal with the situation
where an error occurs after you've already processed some rows.
See

https://www.postgresql.org/docs/current/libpq-single-row-mode.html

You can alternatively use a cursor and FETCH a few rows at a time,
as you mentioned. An error later than the first FETCH is still
possible that way, but it's quantized in some sense --- any one
FETCH either succeeds or fails.

regards, tom lane

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Rob Sargent 2019-09-09 02:30:01 Re: How do I enabled Windows 10 to be able to run PSQL etc
Previous Message jj08 2019-09-08 16:12:59 RE:Re: RE:Re: RE:Re: A complex SQL query