Search code examples
postgresqllibpq

Is PQntuples guaranteed to be 1 with PQsetSingleRowMode?


I've started using PQsetSingleRowMode to be able to have more control over the data as it is streaming after PQsendQueryParams. You need to call PQgetResult a lot more, but is it as a matter of fact guaranteed that you'll get a single record each time, and thus PQntuples returns 1?

I'm asking because I'm not sure if it's 100% safe to drop the code that checks for PQntuples and loops that many times. Also I would not mind if it were greater than 1. Or if there were a separate “fire hose mode” where you get any records that have been fully transferred over the network since the previous PQgetresult...


Solution

  • In single row mode PQgetResult will always produce a result with at most one row. Be aware that the last PGresult * returned will be empty.

    If you want the result set in chunks of more than a single row, you'd have to use cursors and FETCH.