@@ -3588,6 +3588,20 @@ ExecStatusType PQresultStatus(const PGresult *res);
3588
3588
</listitem>
3589
3589
</varlistentry>
3590
3590
3591
+ <varlistentry id="libpq-pgres-tuples-chunk">
3592
+ <term><literal>PGRES_TUPLES_CHUNK</literal></term>
3593
+ <listitem>
3594
+ <para>
3595
+ The <structname>PGresult</structname> contains several result tuples
3596
+ from the current command. This status occurs only when
3597
+ chunked mode has been selected for the query
3598
+ (see <xref linkend="libpq-single-row-mode"/>).
3599
+ The number of tuples will not exceed the limit passed to
3600
+ <xref linkend="libpq-PQsetChunkedRowsMode"/>.
3601
+ </para>
3602
+ </listitem>
3603
+ </varlistentry>
3604
+
3591
3605
<varlistentry id="libpq-pgres-pipeline-sync">
3592
3606
<term><literal>PGRES_PIPELINE_SYNC</literal></term>
3593
3607
<listitem>
@@ -3617,8 +3631,9 @@ ExecStatusType PQresultStatus(const PGresult *res);
3617
3631
3618
3632
</variablelist>
3619
3633
3620
- If the result status is <literal>PGRES_TUPLES_OK</literal> or
3621
- <literal>PGRES_SINGLE_TUPLE</literal>, then
3634
+ If the result status is <literal>PGRES_TUPLES_OK</literal>,
3635
+ <literal>PGRES_SINGLE_TUPLE</literal>, or
3636
+ <literal>PGRES_TUPLES_CHUNK</literal>, then
3622
3637
the functions described below can be used to retrieve the rows
3623
3638
returned by the query. Note that a <command>SELECT</command>
3624
3639
command that happens to retrieve zero rows still shows
@@ -4030,7 +4045,9 @@ void PQclear(PGresult *res);
4030
4045
These functions are used to extract information from a
4031
4046
<structname>PGresult</structname> object that represents a successful
4032
4047
query result (that is, one that has status
4033
- <literal>PGRES_TUPLES_OK</literal> or <literal>PGRES_SINGLE_TUPLE</literal>).
4048
+ <literal>PGRES_TUPLES_OK</literal>,
4049
+ <literal>PGRES_SINGLE_TUPLE</literal>, or
4050
+ <literal>PGRES_TUPLES_CHUNK</literal>).
4034
4051
They can also be used to extract
4035
4052
information from a successful Describe operation: a Describe's result
4036
4053
has all the same column information that actual execution of the query
@@ -5235,7 +5252,8 @@ PGresult *PQgetResult(PGconn *conn);
5235
5252
<para>
5236
5253
Another frequently-desired feature that can be obtained with
5237
5254
<xref linkend="libpq-PQsendQuery"/> and <xref linkend="libpq-PQgetResult"/>
5238
- is retrieving large query results a row at a time. This is discussed
5255
+ is retrieving large query results a limited number of rows at a time.
5256
+ This is discussed
5239
5257
in <xref linkend="libpq-single-row-mode"/>.
5240
5258
</para>
5241
5259
@@ -5599,15 +5617,6 @@ int PQflush(PGconn *conn);
5599
5617
queries in the pipeline; see <xref linkend="libpq-pipeline-interleave"/>.
5600
5618
</para>
5601
5619
5602
- <para>
5603
- To enter single-row mode, call <function>PQsetSingleRowMode</function>
5604
- before retrieving results with <function>PQgetResult</function>.
5605
- This mode selection is effective only for the query currently
5606
- being processed. For more information on the use of
5607
- <function>PQsetSingleRowMode</function>,
5608
- refer to <xref linkend="libpq-single-row-mode"/>.
5609
- </para>
5610
-
5611
5620
<para>
5612
5621
<function>PQgetResult</function> behaves the same as for normal
5613
5622
asynchronous processing except that it may contain the new
@@ -5972,36 +5981,49 @@ UPDATE mytable SET x = x + 1 WHERE id = 42;
5972
5981
</sect2>
5973
5982
</sect1>
5974
5983
5984
+ <!-- keep this not-too-apropos sect1 ID for stability of doc URLs -->
5975
5985
<sect1 id="libpq-single-row-mode">
5976
- <title>Retrieving Query Results Row-by-Row </title>
5986
+ <title>Retrieving Query Results in Chunks </title>
5977
5987
5978
5988
<indexterm zone="libpq-single-row-mode">
5979
5989
<primary>libpq</primary>
5980
5990
<secondary>single-row mode</secondary>
5981
5991
</indexterm>
5982
5992
5993
+ <indexterm zone="libpq-single-row-mode">
5994
+ <primary>libpq</primary>
5995
+ <secondary>chunked mode</secondary>
5996
+ </indexterm>
5997
+
5983
5998
<para>
5984
5999
Ordinarily, <application>libpq</application> collects an SQL command's
5985
6000
entire result and returns it to the application as a single
5986
6001
<structname>PGresult</structname>. This can be unworkable for commands
5987
6002
that return a large number of rows. For such cases, applications can use
5988
6003
<xref linkend="libpq-PQsendQuery"/> and <xref linkend="libpq-PQgetResult"/> in
5989
- <firstterm>single-row mode</firstterm>. In this mode, the result row(s) are
5990
- returned to the application one at a time, as they are received from the
5991
- server.
6004
+ <firstterm>single-row mode</firstterm> or <firstterm>chunked
6005
+ mode</firstterm>. In these modes, result row(s) are returned to the
6006
+ application as they are received from the server, one at a time for
6007
+ single-row mode or in groups for chunked mode.
5992
6008
</para>
5993
6009
5994
6010
<para>
5995
- To enter single-row mode, call <xref linkend="libpq-PQsetSingleRowMode"/>
6011
+ To enter one of these modes, call <xref linkend="libpq-PQsetSingleRowMode"/>
6012
+ or <xref linkend="libpq-PQsetChunkedRowsMode"/>
5996
6013
immediately after a successful call of <xref linkend="libpq-PQsendQuery"/>
5997
6014
(or a sibling function). This mode selection is effective only for the
5998
6015
currently executing query. Then call <xref linkend="libpq-PQgetResult"/>
5999
6016
repeatedly, until it returns null, as documented in <xref
6000
6017
linkend="libpq-async"/>. If the query returns any rows, they are returned
6001
- as individual <structname>PGresult</structname> objects, which look like
6018
+ as one or more <structname>PGresult</structname> objects, which look like
6002
6019
normal query results except for having status code
6003
- <literal>PGRES_SINGLE_TUPLE</literal> instead of
6004
- <literal>PGRES_TUPLES_OK</literal>. After the last row, or immediately if
6020
+ <literal>PGRES_SINGLE_TUPLE</literal> for single-row mode or
6021
+ <literal>PGRES_TUPLES_CHUNK</literal> for chunked mode, instead of
6022
+ <literal>PGRES_TUPLES_OK</literal>. There is exactly one result row in
6023
+ each <literal>PGRES_SINGLE_TUPLE</literal> object, while
6024
+ a <literal>PGRES_TUPLES_CHUNK</literal> object contains at least one
6025
+ row but not more than the specified number of rows per chunk.
6026
+ After the last row, or immediately if
6005
6027
the query returns zero rows, a zero-row object with status
6006
6028
<literal>PGRES_TUPLES_OK</literal> is returned; this is the signal that no
6007
6029
more rows will arrive. (But note that it is still necessary to continue
@@ -6013,9 +6035,9 @@ UPDATE mytable SET x = x + 1 WHERE id = 42;
6013
6035
</para>
6014
6036
6015
6037
<para>
6016
- When using pipeline mode, single-row mode needs to be activated for each
6017
- query in the pipeline before retrieving results for that query
6018
- with <function>PQgetResult</function>.
6038
+ When using pipeline mode, single-row or chunked mode needs to be
6039
+ activated for each query in the pipeline before retrieving results for
6040
+ that query with <function>PQgetResult</function>.
6019
6041
See <xref linkend="libpq-pipeline-mode"/> for more information.
6020
6042
</para>
6021
6043
@@ -6046,6 +6068,36 @@ int PQsetSingleRowMode(PGconn *conn);
6046
6068
</para>
6047
6069
</listitem>
6048
6070
</varlistentry>
6071
+
6072
+ <varlistentry id="libpq-PQsetChunkedRowsMode">
6073
+ <term><function>PQsetChunkedRowsMode</function><indexterm><primary>PQsetChunkedRowsMode</primary></indexterm></term>
6074
+
6075
+ <listitem>
6076
+ <para>
6077
+ Select chunked mode for the currently-executing query.
6078
+
6079
+ <synopsis>
6080
+ int PQsetChunkedRowsMode(PGconn *conn, int chunkSize);
6081
+ </synopsis>
6082
+ </para>
6083
+
6084
+ <para>
6085
+ This function is similar to
6086
+ <xref linkend="libpq-PQsetSingleRowMode"/>, except that it
6087
+ specifies retrieval of up to <replaceable>chunkSize</replaceable> rows
6088
+ per <structname>PGresult</structname>, not necessarily just one row.
6089
+ This function can only be called immediately after
6090
+ <xref linkend="libpq-PQsendQuery"/> or one of its sibling functions,
6091
+ before any other operation on the connection such as
6092
+ <xref linkend="libpq-PQconsumeInput"/> or
6093
+ <xref linkend="libpq-PQgetResult"/>. If called at the correct time,
6094
+ the function activates chunked mode for the current query and
6095
+ returns 1. Otherwise the mode stays unchanged and the function
6096
+ returns 0. In any case, the mode reverts to normal after
6097
+ completion of the current query.
6098
+ </para>
6099
+ </listitem>
6100
+ </varlistentry>
6049
6101
</variablelist>
6050
6102
</para>
6051
6103
@@ -6054,9 +6106,10 @@ int PQsetSingleRowMode(PGconn *conn);
6054
6106
While processing a query, the server may return some rows and then
6055
6107
encounter an error, causing the query to be aborted. Ordinarily,
6056
6108
<application>libpq</application> discards any such rows and reports only the
6057
- error. But in single-row mode, those rows will have already been
6058
- returned to the application. Hence, the application will see some
6059
- <literal>PGRES_SINGLE_TUPLE</literal> <structname>PGresult</structname>
6109
+ error. But in single-row or chunked mode, some rows may have already
6110
+ been returned to the application. Hence, the application will see some
6111
+ <literal>PGRES_SINGLE_TUPLE</literal> or <literal>PGRES_TUPLES_CHUNK</literal>
6112
+ <structname>PGresult</structname>
6060
6113
objects followed by a <literal>PGRES_FATAL_ERROR</literal> object. For
6061
6114
proper transactional behavior, the application must be designed to
6062
6115
discard or undo whatever has been done with the previously-processed
0 commit comments