Home Explore Blog CI



postgresql

60th chunk of `doc/src/sgml/libpq.sgml`
1604d1407281e813518958c3d334cd07edf2e9c1f952488a0000000100000fab
    and constraint name are supplied only for a limited number of error
         types; see <xref linkend="errcodes-appendix"/>.  Do not assume that
         the presence of any of these fields guarantees the presence of
         another field.  Core error sources observe the interrelationships
         noted above, but user-defined functions may use these fields in other
         ways.  In the same vein, do not assume that these fields denote
         contemporary objects in the current database.
        </para>
       </note>

       <para>
        The client is responsible for formatting displayed information to meet
        its needs; in particular it should break long lines as needed.
        Newline characters appearing in the error message fields should be
        treated as paragraph breaks, not line breaks.
       </para>

       <para>
        Errors generated internally by <application>libpq</application> will
        have severity and primary message, but typically no other fields.
       </para>

       <para>
        Note that error fields are only available from
        <structname>PGresult</structname> objects, not
        <structname>PGconn</structname> objects; there is no
        <function>PQerrorField</function> function.
       </para>
      </listitem>
     </varlistentry>

     <varlistentry id="libpq-PQclear">
      <term><function>PQclear</function><indexterm><primary>PQclear</primary></indexterm></term>
      <listitem>
       <para>
        Frees  the  storage  associated with a
        <structname>PGresult</structname>.  Every command result should be
        freed via <xref linkend="libpq-PQclear"/> when it  is  no  longer
        needed.

<synopsis>
void PQclear(PGresult *res);
</synopsis>

        If the argument is a <symbol>NULL</symbol> pointer, no operation is
        performed.
       </para>

       <para>
        You can keep a <structname>PGresult</structname> object around for
        as long as you need it; it does not go away when you issue a new
        command, nor even if you close the connection.  To get rid of it,
        you must call <xref linkend="libpq-PQclear"/>.  Failure to do this
        will result in memory leaks in your application.
       </para>
      </listitem>
     </varlistentry>
    </variablelist>
   </para>
  </sect2>

  <sect2 id="libpq-exec-select-info">
   <title>Retrieving Query Result Information</title>

   <para>
    These functions are used to extract information from a
    <structname>PGresult</structname> object that represents a successful
    query result (that is, one that has status
    <literal>PGRES_TUPLES_OK</literal>,
    <literal>PGRES_SINGLE_TUPLE</literal>, or
    <literal>PGRES_TUPLES_CHUNK</literal>).
    They can also be used to extract
    information from a successful Describe operation: a Describe's result
    has all the same column information that actual execution of the query
    would provide, but it has zero rows.  For objects with other status values,
    these functions will act as though the result has zero rows and zero columns.
   </para>

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

     <listitem>
      <para>
       Returns the number of rows (tuples) in the query result.
       (Note that <structname>PGresult</structname> objects are limited to no more
       than <literal>INT_MAX</literal> rows, so an <type>int</type> result is
       sufficient.)

<synopsis>
int PQntuples(const PGresult *res);
</synopsis>

      </para>
     </listitem>
    </varlistentry>

    <varlistentry id="libpq-PQnfields">
     <term><function>PQnfields</function><indexterm><primary>PQnfields</primary></indexterm></term>

     <listitem>
      <para>
       Returns the number of columns (fields) in each row of the query
       result.

<synopsis>
int PQnfields(const PGresult *res);
</synopsis>
      </para>
     </listitem>
    </varlistentry>

Title: Error Handling and Result Management in libpq: PQclear and Query Result Information Retrieval
Summary
This section details error handling and result management in libpq, specifically focusing on `PQclear` for freeing `PGresult` objects and functions for retrieving query result information. It emphasizes the importance of freeing `PGresult` objects to prevent memory leaks and outlines functions like `PQntuples` and `PQnfields` for extracting row and column counts from successful query results or Describe operations.