> For the complete documentation index, see [llms.txt](https://docs.postgresql.tw/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.postgresql.tw/15/client-interfaces/libpq-c-library/33.6.-canceling-queries-in-progress.md).

# 34.7. Canceling Queries in Progress

## 34.7. 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.

`PQgetCancel`

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

```

PGcancel *PQgetCancel(PGconn *conn);
```

[`PQgetCancel`](#LIBPQ-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 [`PQcancel`](#LIBPQ-PQCANCEL) or [`PQfreeCancel`](#LIBPQ-PQFREECANCEL).

`PQfreeCancel`

Frees a data structure created by [`PQgetCancel`](#LIBPQ-PQGETCANCEL).

```

void PQfreeCancel(PGcancel *cancel);
```

[`PQfreeCancel`](#LIBPQ-PQFREECANCEL) frees a data object previously created by [`PQgetCancel`](#LIBPQ-PQGETCANCEL).

`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.

[`PQcancel`](#LIBPQ-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 [`PQcancel`](#LIBPQ-PQCANCEL) is concerned, so it can also be invoked from a thread that is separate from the one manipulating the `PGconn` object.

`PQrequestCancel`

[`PQrequestCancel`](#LIBPQ-PQREQUESTCANCEL) is a deprecated variant of [`PQcancel`](#LIBPQ-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 [`PQerrorMessage`](/15/client-interfaces/libpq-c-library/connection-status-functions.md#LIBPQ-PQERRORMESSAGE)). Although the functionality is the same, this approach is not safe within multiple-thread programs or signal handlers, since it is possible that overwriting the `PGconn`'s error message will mess up the operation currently in progress on the connection.

***

原文：[PostgreSQL 15.19 Documentation](/15/client-interfaces/libpq-c-library/33.6.-canceling-queries-in-progress.md)（英文原文，待翻譯）


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.postgresql.tw/15/client-interfaces/libpq-c-library/33.6.-canceling-queries-in-progress.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
