Re: Can you please tell us how set this prefetch attribute infollowing lines. - Mailing list pgsql-performance

From Justin Pryzby
Subject Re: Can you please tell us how set this prefetch attribute infollowing lines.
Date
Msg-id 20191018161502.GF3599@telsasoft.com
Whole thread Raw
In response to Can you please tell us how set this prefetch attribute in followinglines.  (M Tarkeshwar Rao <m.tarkeshwar.rao@ericsson.com>)
List pgsql-performance
On Fri, Oct 18, 2019 at 03:47:49AM +0000, M Tarkeshwar Rao wrote:
> How to fetch certain number of tuples from a postgres table.
> 
> Same I am doing in oracle using following lines by setting prefetch attribute.
> 
> For oracle
> // Prepare query
>     if( OCIStmtPrepare( myOciStatement, myOciError, (text *)aSqlStatement,
> // Get statement type
>  OCIAttrGet( (void *)myOciStatement, OCI_HTYPE_STMT, &statement_type, 0, OCI_ATTR_STMT_TYPE, myOciError );
> // Set prefetch count
>   OCIAttrSet( myOciStatement, OCI_HTYPE_STMT, &prefetch, 0, OCI_ATTR_PREFETCH_ROWS, myOciError );
> // Execute query
> status = OCIStmtExecute( myOciServerCtx, myOciStatement, myOciError, iters, 0, NULL, NULL, OCI_DEFAULT );
> 
> For Postgres
> Can you please tell us how set this prefetch attribute in following lines. Is PQexec returns all the rows from the
table?

Yes, PQexec reads everything at once into a buffer on the library.
https://www.postgresql.org/docs/current/libpq-exec.html

I think you want this:
https://www.postgresql.org/docs/current/libpq-async.html
|Another frequently-desired feature that can be obtained with PQsendQuery and PQgetResult is retrieving large query
resultsa row at a time. This is discussed in Section 33.5.
 
https://www.postgresql.org/docs/current/libpq-single-row-mode.html

Note this does not naively send "get one row" requests to the server on each
call.  Rather, I believe it behaves at a protocol layer exactly the same as
PQexec(), but each library call returns only a single row.  When it runs out of
rows, it requests from the server another packet full of rows, which are saved
for future iterations.

The effect is constant memory use for arbitrarily large result set with same
number of network roundtrips as PQexec().  You'd do something like:

PQsendQuery(conn)
PQsetSingleRowMode(conn)
while(res = PQgetResult(conn)) {
    ...
    PQclear(res)
}

Justin



pgsql-performance by date:

Previous
From: M Tarkeshwar Rao
Date:
Subject: Can you please tell us how set this prefetch attribute in followinglines.
Next
From: David Conlin
Date:
Subject: Re: Change in CTE treatment in query plans?