Movatterモバイル変換


[0]ホーム

URL:



Facebook
Postgres Pro
Facebook
Downloads
33.6. Retrieving Query Results Row-by-Row
Prev UpChapter 33. libpq — C LibraryHome Next

33.6. Retrieving Query Results Row-by-Row#

Ordinarily,libpq collects an SQL command's entire result and returns it to the application as a singlePGresult. This can be unworkable for commands that return a large number of rows. For such cases, applications can usePQsendQuery andPQgetResult insingle-row mode. In this mode, the result row(s) are returned to the application one at a time, as they are received from the server.

To enter single-row mode, callPQsetSingleRowMode immediately after a successful call ofPQsendQuery (or a sibling function). This mode selection is effective only for the currently executing query. Then callPQgetResult repeatedly, until it returns null, as documented inSection 33.4. If the query returns any rows, they are returned as individualPGresult objects, which look like normal query results except for having status codePGRES_SINGLE_TUPLE instead ofPGRES_TUPLES_OK. After the last row, or immediately if the query returns zero rows, a zero-row object with statusPGRES_TUPLES_OK is returned; this is the signal that no more rows will arrive. (But note that it is still necessary to continue callingPQgetResult until it returns null.) All of thesePGresult objects will contain the same row description data (column names, types, etc.) that an ordinaryPGresult object for the query would have. Each object should be freed withPQclear as usual.

When using pipeline mode, single-row mode needs to be activated for each query in the pipeline before retrieving results for that query withPQgetResult. SeeSection 33.5 for more information.

PQsetSingleRowMode#

Select single-row mode for the currently-executing query.

int PQsetSingleRowMode(PGconn *conn);

This function can only be called immediately afterPQsendQuery or one of its sibling functions, before any other operation on the connection such asPQconsumeInput orPQgetResult. If called at the correct time, the function activates single-row mode for the current query and returns 1. Otherwise the mode stays unchanged and the function returns 0. In any case, the mode reverts to normal after completion of the current query.

Caution

While processing a query, the server may return some rows and then encounter an error, causing the query to be aborted. Ordinarily,libpq discards any such rows and reports only the error. But in single-row mode, those rows will have already been returned to the application. Hence, the application will see somePGRES_SINGLE_TUPLEPGresult objects followed by aPGRES_FATAL_ERROR object. For proper transactional behavior, the application must be designed to discard or undo whatever has been done with the previously-processed rows, if the query ultimately fails.


Prev Up Next
33.5. Pipeline Mode Home 33.7. Canceling Queries in Progress
pdfepub
Go to Postgres Pro Standard 16
By continuing to browse this website, you agree to the use of cookies. Go toPrivacy Policy.

[8]ページ先頭

©2009-2025 Movatter.jp