Home Explore Blog CI



postgresql

89th chunk of `doc/src/sgml/libpq.sgml`
ef7e7d13d12971345605e145503f5ae1fc727553c30dee9f0000000100000fa4
 same requirements. Any connection options
       that are only used during authentication or after authentication of the
       client are ignored though, because cancellation requests do not require
       authentication and the connection is closed right after the cancellation
       request is submitted.
      </para>

      <para>
       Note that when <function>PQcancelCreate</function> returns a non-null
       pointer, you must call <xref linkend="libpq-PQcancelFinish"/> when you
       are finished with it, in order to dispose of the structure and any
       associated memory blocks. This must be done even if the cancel request
       failed or was abandoned.
      </para>
     </listitem>
    </varlistentry>

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

     <listitem>
      <para>
       Requests that the server abandons processing of the current command
       in a blocking manner.
<synopsis>
int PQcancelBlocking(PGcancelConn *cancelConn);
</synopsis>
      </para>

      <para>
       The request is made over the given <structname>PGcancelConn</structname>,
       which needs to be created with <xref linkend="libpq-PQcancelCreate"/>.
       The return value of <xref linkend="libpq-PQcancelBlocking"/>
       is 1 if the cancel request was successfully
       dispatched and 0 if not. If it was unsuccessful, the error message can be
       retrieved using <xref linkend="libpq-PQcancelErrorMessage"/>.
      </para>

      <para>
       Successful dispatch of the cancellation is no guarantee that the request
       will have any effect, however. If the cancellation is effective, the
       command being canceled will terminate early and return an error result.
       If the cancellation fails (say, because the server was already done
       processing the command), then there will be no visible result at all.
      </para>

     </listitem>
    </varlistentry>

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

     <listitem>
      <para>
       Requests that the server abandons processing of the current command
       in a non-blocking manner.
<synopsis>
int PQcancelStart(PGcancelConn *cancelConn);

PostgresPollingStatusType PQcancelPoll(PGcancelConn *cancelConn);
</synopsis>
      </para>

      <para>
       The request is made over the given <structname>PGcancelConn</structname>,
       which needs to be created with <xref linkend="libpq-PQcancelCreate"/>.
       The return value of <xref linkend="libpq-PQcancelStart"/>
       is 1 if the cancellation request could be started and 0 if not.
       If it was unsuccessful, the error message can be
       retrieved using <xref linkend="libpq-PQcancelErrorMessage"/>.
      </para>

      <para>
       If <function>PQcancelStart</function> succeeds, the next stage
       is to poll <application>libpq</application> so that it can proceed with
       the cancel connection sequence.
       Use <xref linkend="libpq-PQcancelSocket"/> to obtain the descriptor of the
       socket underlying the database connection.
       (Caution: do not assume that the socket remains the same
       across <function>PQcancelPoll</function> calls.)
       Loop thus: If <function>PQcancelPoll(cancelConn)</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).
       Then call <function>PQcancelPoll(cancelConn)</function> again.
       Conversely, if <function>PQcancelPoll(cancelConn)</function> last returned
       <symbol>PGRES_POLLING_WRITING</symbol>, wait until the socket is ready
       to write, then

Title: PQcancelBlocking and PQcancelStart/PQcancelPoll for Query Cancellation
Summary
This section details the `PQcancelBlocking` function for synchronously requesting server-side query cancellation. It emphasizes that successful dispatch doesn't guarantee cancellation and that errors are reported through `PQcancelErrorMessage`. It then describes `PQcancelStart` and `PQcancelPoll` for asynchronous cancellation, outlining the polling process to ensure the cancellation request is sent effectively, involving monitoring the socket for read/write readiness.