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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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





[Postgresql General]     [Postgresql PHP]     [PHP Users]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Yosemite]

  Powered by Linux