PostgreSQL

PostgreSQL Elephant Logo

33.6. Canceling Queries in Progress

A client application can request cancellation of a command that is still being processed by the server, using the functions described in this section.

[.term]#PQgetCancel

Creates a data structure containing the information needed to cancel a command issued through a particular database connection. +

PGcancel *PQgetCancel(PGconn *conn);
  +
  link:libpq-cancel.html#LIBPQ-PQGETCANCEL[`+PQgetCancel+`] creates a `+PGcancel+` object given a `+PGconn+` connection object. It will return `+NULL+` if the given _`+conn+`_ is `+NULL+` or an invalid connection. The `+PGcancel+` object is an opaque structure that is not meant to be accessed directly by the application; it can only be passed to link:libpq-cancel.html#LIBPQ-PQCANCEL[`+PQcancel+`] or link:libpq-cancel.html#LIBPQ-PQFREECANCEL[`+PQfreeCancel+`].
[.term]#`+PQfreeCancel+`::
  Frees a data structure created by link:libpq-cancel.html#LIBPQ-PQGETCANCEL[`+PQgetCancel+`].
  +
void PQfreeCancel(PGcancel *cancel);
  +
  link:libpq-cancel.html#LIBPQ-PQFREECANCEL[`+PQfreeCancel+`] frees a data object previously created by link:libpq-cancel.html#LIBPQ-PQGETCANCEL[`+PQgetCancel+`].
[.term]#`+PQcancel+`::
  Requests that the server abandon processing of the current command.
  +
int PQcancel(PGcancel *cancel, char *errbuf, int errbufsize);
+
The return value is 1 if the cancel request was successfully dispatched and 0 if not. If not, _`+errbuf+`_ is filled with an explanatory error message. _`+errbuf+`_ must be a char array of size _`+errbufsize+`_ (the recommended size is 256 bytes).
+
Successful dispatch is no guarantee that the request will have any effect, however. If the cancellation is effective, the current command 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.
+
link:libpq-cancel.html#LIBPQ-PQCANCEL[`+PQcancel+`] can safely be invoked from a signal handler, if the _`+errbuf+`_ is a local variable in the signal handler. The `+PGcancel+` object is read-only as far as link:libpq-cancel.html#LIBPQ-PQCANCEL[`+PQcancel+`] is concerned, so it can also be invoked from a thread that is separate from the one manipulating the `+PGconn+` object.
[.term]#PQrequestCancel

PQrequestCancel is a deprecated variant of PQcancel. +

int PQrequestCancel(PGconn *conn);
+
Requests that the server abandon processing of the current command. It operates directly on the `+PGconn+` object, and in case of failure stores the error message in the `+PGconn+` object (whence it can be retrieved by link:libpq-status.html#LIBPQ-PQERRORMESSAGE[`+PQerrorMessage+`]). Although the functionality is the same, this approach creates hazards for multiple-thread programs and signal handlers, since it is possible that overwriting the `+PGconn+`'s error message will mess up the operation currently in progress on the connection.

Prev Up Next

33.5. Retrieving Query Results Row-by-Row

Home

33.7. The Fast-Path Interface

Submit correction

If you see anything in the documentation that is not correct, does not match your experience with the particular feature or requires further clarification, please use this form to report a documentation issue.

Copyright © 1996-2023 The PostgreSQL Global Development Group