Re: Can you please tell us how set this prefetch attribute in following lines.

From: Justin Pryzby <pryzby(at)telsasoft(dot)com>
To: M Tarkeshwar Rao <m(dot)tarkeshwar(dot)rao(at)ericsson(dot)com>
Cc: "pgsql-performance(at)postgresql(dot)org" <pgsql-performance(at)postgresql(dot)org>
Subject: Re: Can you please tell us how set this prefetch attribute in following lines.
Date: 2019-10-18 16:15:02
Message-ID: 20191018161502.GF3599@telsasoft.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-hackers 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 results a 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

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Ariadne Conill 2019-10-18 17:37:24 jsonb_set() strictness considered harmful to data
Previous Message Patrick FICHE 2019-10-18 16:14:17 RE: Execute a function through fdw

Browse pgsql-hackers by date

  From Date Subject
Next Message Konstantin Knizhnik 2019-10-18 16:53:23 Re: Columns correlation and adaptive query optimization
Previous Message Hans Buschmann 2019-10-18 15:40:34 Missing constant propagation in planner on hash quals causes join slowdown

Browse pgsql-performance by date

  From Date Subject
Next Message David Conlin 2019-10-21 13:22:01 Re: Change in CTE treatment in query plans?
Previous Message M Tarkeshwar Rao 2019-10-18 03:47:49 Can you please tell us how set this prefetch attribute in following lines.