Will PQsetSingleRowMode get me results faster?

2025-01-06 Thread Stijn Sanders
I've been using LibPQ to get data from PostgreSQL instances with great
success.
I'm using PQsendQuery and PQgetResult, but noticed there's
also PQsetSingleRowMode.
The documentation is clearly stating it only benefits a limited set of
scenario's, but I'm saddened that it can't help to get the first resulte of
a (longer running) query faster.

There's a different database solution I won't name here that has a thing
they call 'firehose mode' that in fact does this: their equivalent
of PQntuples actually returns -1 in this mode, and you're expected to use
their equivalent of PQgetResult to get record per record ** as it is
rolling in from the server while the query is still running **.

>From what I notice using LibPQ, it appears a query needs to complete before
resulting data is being transferred to the client. Please correct me if I'm
wrong.

Please point me in the correct direction if I'm missing something and I
need to look elsewhere. (I just now notice there's a PQsetChunkedRowsMode
now —nice! — but I suspect the above still holds.)

Should I attempt to use LIMIT and OFFSET to limit the running time of
queries to get results faster? This will still interrupt the query and add
overhead of starting and stopping each query, even using PQexecPrepared, I
guess...

Greetings
Stijn


Re: Will PQsetSingleRowMode get me results faster?

2025-04-07 Thread Stijn Sanders
On Mon, Jan 6, 2025 at 9:06 PM Tom Lane  wrote:
> So in principle, you might get best results by defining your query
> with DECLARE CURSOR and then using PQsetSingleRowMode on the FETCH.
> But it'd really depend on the particular query whether this gives
> any benefit.

That's a really nice suggestion, and it took me some time to set up a
suitable test environment to see if it would work, but using separate
PQsendquery/PQexec calls for:

start transaction read only
declare cr1 no scroll cursor for select (and the rest of my query)
fetch next in cr1

it seems like the fetch instruction still takes about as much time as
the full 'normal' select would, I tried a few different queries, but
I'm still suspecting PostgreSQL's internals is waiting for the data to
all 'be ready' before it can send any data over, even if these
PQgetResult's for each fetch will have a PQntuples of 1.
(I even tried with "fetch 8", and PQntuples neatly serves 8 at a time,
but still after about the same time PQsendquery(,'select... would
take)
Or could there still be something that I'm doing that prevents 'firehosing'?