Home Explore Blog CI



postgresql

86th chunk of `doc/src/sgml/libpq.sgml`
7460e12d639e4654569b324a44a5ec2a8a8694aca87d292f0000000100000ff5
 operation. In such cases, the client
    would have to introduce a synchronization point and wait for a full client/server
    round-trip to get the results it needs. However, it's often possible to
    adjust the client design to exchange the required information server-side.
    Read-modify-write cycles are especially good candidates; for example:
<programlisting>
BEGIN;
SELECT x FROM mytable WHERE id = 42 FOR UPDATE;
-- result: x=2
-- client adds 1 to x:
UPDATE mytable SET x = 3 WHERE id = 42;
COMMIT;
</programlisting>
    could be much more efficiently done with:
<programlisting>
UPDATE mytable SET x = x + 1 WHERE id = 42;
</programlisting>
   </para>

   <para>
    Pipelining is less useful, and more complex, when a single pipeline contains
    multiple transactions (see <xref linkend="libpq-pipeline-errors"/>).
   </para>
  </sect2>
 </sect1>

 <!-- keep this not-too-apropos sect1 ID for stability of doc URLs -->
 <sect1 id="libpq-single-row-mode">
  <title>Retrieving Query Results in Chunks</title>

  <indexterm zone="libpq-single-row-mode">
   <primary>libpq</primary>
   <secondary>single-row mode</secondary>
  </indexterm>

  <indexterm zone="libpq-single-row-mode">
   <primary>libpq</primary>
   <secondary>chunked mode</secondary>
  </indexterm>

  <para>
   Ordinarily, <application>libpq</application> collects an SQL command's
   entire result and returns it to the application as a single
   <structname>PGresult</structname>.  This can be unworkable for commands
   that return a large number of rows.  For such cases, applications can use
   <xref linkend="libpq-PQsendQuery"/> and <xref linkend="libpq-PQgetResult"/> in
   <firstterm>single-row mode</firstterm> or <firstterm>chunked
   mode</firstterm>.  In these modes, result row(s) are returned to the
   application as they are received from the server, one at a time for
   single-row mode or in groups for chunked mode.
  </para>

  <para>
   To enter one of these modes, call <xref linkend="libpq-PQsetSingleRowMode"/>
    or <xref linkend="libpq-PQsetChunkedRowsMode"/>
   immediately after a successful call of <xref linkend="libpq-PQsendQuery"/>
   (or a sibling function).  This mode selection is effective only for the
   currently executing query.  Then call <xref linkend="libpq-PQgetResult"/>
   repeatedly, until it returns null, as documented in <xref
   linkend="libpq-async"/>.  If the query returns any rows, they are returned
   as one or more <structname>PGresult</structname> objects, which look like
   normal query results except for having status code
   <literal>PGRES_SINGLE_TUPLE</literal> for single-row mode or
   <literal>PGRES_TUPLES_CHUNK</literal> for chunked mode, instead of
   <literal>PGRES_TUPLES_OK</literal>.  There is exactly one result row in
   each <literal>PGRES_SINGLE_TUPLE</literal> object, while
   a <literal>PGRES_TUPLES_CHUNK</literal> object contains at least one
   row but not more than the specified number of rows per chunk.
   After the last row, or immediately if
   the query returns zero rows, a zero-row object with status
   <literal>PGRES_TUPLES_OK</literal> is returned; this is the signal that no
   more rows will arrive.  (But note that it is still necessary to continue
   calling <xref linkend="libpq-PQgetResult"/> until it returns null.)  All of
   these <structname>PGresult</structname> objects will contain the same row
   description data (column names, types, etc.) that an ordinary
   <structname>PGresult</structname> object for the query would have.
   Each object should be freed with <xref linkend="libpq-PQclear"/> as usual.
  </para>

  <para>
   When using pipeline mode, single-row or chunked mode needs to be
   activated for each query in the pipeline before retrieving results for
   that query with <function>PQgetResult</function>.
   See <xref linkend="libpq-pipeline-mode"/> for more information.
  </para>

  <para>
   <variablelist>
    <varlistentry id="libpq-PQsetSingleRowMode">
     <term><function>PQsetSingleRowMode</function><indexterm><primary>PQsetSingleRowMode</primary></indexterm></term>

Title: Retrieving Query Results in Chunks with libpq's Single-Row Mode
Summary
This section discusses libpq's single-row and chunked modes, which allow applications to retrieve large query results in smaller, more manageable chunks instead of a single PGresult. It explains how to enable these modes using `PQsetSingleRowMode` or `PQsetChunkedRowsMode` after sending a query and how to retrieve results using `PQgetResult`. The results are returned as PGresult objects with specific status codes, such as PGRES_SINGLE_TUPLE or PGRES_TUPLES_CHUNK, signaling the presence of rows. A zero-row object with status PGRES_TUPLES_OK indicates the end of the result set. The section also notes that when using pipeline mode, you must activate single-row or chunked mode for each query.