Home Explore Blog CI



postgresql

6th chunk of `doc/src/sgml/libpq.sgml`
fe09b731cdc40a3801c1da04ac7747d8f25392f1cb46bf0c0000000100000fa0
       or <xref linkend="libpq-PQconnectStartParams"/> succeeds, the next stage
       is to poll <application>libpq</application> so that it can proceed with
       the connection sequence.
       Use <function>PQsocket(conn)</function> to obtain the descriptor of the
       socket underlying the database connection.
       (Caution: do not assume that the socket remains the same
       across <function>PQconnectPoll</function> calls.)
       Loop thus: If <function>PQconnectPoll(conn)</function> last returned
       <symbol>PGRES_POLLING_READING</symbol>, wait until the socket is ready to
       read (as indicated by <function>select()</function>, <function>poll()</function>, or
       similar system function).  Note that <function>PQsocketPoll</function>
       can help reduce boilerplate by abstracting the setup of
       <function>select(2)</function> or <function>poll(2)</function> if it is
       available on your system.
       Then call <function>PQconnectPoll(conn)</function> again.
       Conversely, if <function>PQconnectPoll(conn)</function> last returned
       <symbol>PGRES_POLLING_WRITING</symbol>, wait until the socket is ready
       to write, then call <function>PQconnectPoll(conn)</function> again.
       On the first iteration, i.e., if you have yet to call
       <function>PQconnectPoll</function>, behave as if it last returned
       <symbol>PGRES_POLLING_WRITING</symbol>.  Continue this loop until
       <function>PQconnectPoll(conn)</function> returns
       <symbol>PGRES_POLLING_FAILED</symbol>, indicating the connection procedure
       has failed, or <symbol>PGRES_POLLING_OK</symbol>, indicating the connection
       has been successfully made.
      </para>

      <para>
       At any time during connection, the status of the connection can be
       checked by calling <xref linkend="libpq-PQstatus"/>. If this call returns <symbol>CONNECTION_BAD</symbol>, then the
       connection procedure has failed; if the call returns <symbol>CONNECTION_OK</symbol>, then the
       connection is ready.  Both of these states are equally detectable
       from the return value of <function>PQconnectPoll</function>, described above. Other states might also occur
       during (and only during) an asynchronous connection procedure. These
       indicate the current stage of the connection procedure and might be useful
       to provide feedback to the user for example. These statuses are:

       <variablelist>
        <varlistentry id="libpq-connection-started">
         <term><symbol>CONNECTION_STARTED</symbol></term>
         <listitem>
          <para>
           Waiting for connection to be made.
          </para>
         </listitem>
        </varlistentry>

        <varlistentry id="libpq-connection-made">
         <term><symbol>CONNECTION_MADE</symbol></term>
         <listitem>
          <para>
           Connection OK; waiting to send.
          </para>
         </listitem>
        </varlistentry>

        <varlistentry id="libpq-connection-awaiting-response">
         <term><symbol>CONNECTION_AWAITING_RESPONSE</symbol></term>
         <listitem>
          <para>
           Waiting for a response from the server.
          </para>
         </listitem>
        </varlistentry>

        <varlistentry id="libpq-connection-auth-ok">
         <term><symbol>CONNECTION_AUTH_OK</symbol></term>
         <listitem>
          <para>
           Received authentication; waiting for backend start-up to finish.
          </para>
         </listitem>
        </varlistentry>

        <varlistentry id="libpq-connection-ssl-startup">
         <term><symbol>CONNECTION_SSL_STARTUP</symbol></term>
         <listitem>
          <para>
           Negotiating SSL encryption.
          </para>
         </listitem>
        </varlistentry>

        <varlistentry id="libpq-connection-gss-startup">
         <term><symbol>CONNECTION_GSS_STARTUP</symbol></term>
         <listitem>
          <para>
           Negotiating GSS encryption.

Title: PQconnectPoll and Connection Status Monitoring in Non-blocking Connections
Summary
After initiating a non-blocking connection with PQconnectStart or PQconnectStartParams, this section details how to use PQconnectPoll in a loop to advance the connection sequence. It explains how to monitor the socket's read/write readiness using select() or poll() based on PQconnectPoll's return values (PGRES_POLLING_READING or PGRES_POLLING_WRITING), continuing until the connection succeeds (PGRES_POLLING_OK) or fails (PGRES_POLLING_FAILED). Additionally, it describes how PQstatus can be used to check the connection status and lists various connection states (CONNECTION_STARTED, CONNECTION_MADE, etc.) that may occur during the asynchronous connection process, which can be used for user feedback.