From: Justin Pryzby <pryzby@telsasoft.com>
To: M Tarkeshwar Rao <m.tarkeshwar.rao@ericsson.com>
Cc: pgsql-performance@postgresql.org <pgsql-performance@postgresql.org>
Subject: Re: Can you please tell us how set this prefetch attribute in following lines.
Date: Fri, 18 Oct 2019 11:15:02 -0500
Message-ID: <20191018161502.GF3599@telsasoft.com> (raw)
In-Reply-To: <VI1PR0701MB2718E031C9729F3047E5C339AE6C0@VI1PR0701MB2718.eurprd07.prod.outlook.com>
References: <VI1PR0701MB271869EC2BEDD69E1D0CEC86AE6D0@VI1PR0701MB2718.eurprd07.prod.outlook.com>
<VI1PR0701MB2718E54F1394D67F445FDC46AE6C0@VI1PR0701MB2718.eurprd07.prod.outlook.com>
<VI1PR0701MB2718A333E891D0E95208C043AE6C0@VI1PR0701MB2718.eurprd07.prod.outlook.com>
<VI1PR0701MB2718E031C9729F3047E5C339AE6C0@VI1PR0701MB2718.eurprd07.prod.outlook.com>
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-performance@postgresql.org
Cc: pryzby@telsasoft.com, m.tarkeshwar.rao@ericsson.com
Subject: Re: Can you please tell us how set this prefetch attribute in following lines.
In-Reply-To: <20191018161502.GF3599@telsasoft.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox