---
title: "PostgreSQL® - Detect and terminate long-running queries"
description: "Learn how to detect and terminate specific sessions via the OVHcloud Control Panel and Command Line Interface (CLI)."
url: https://docs.us.ovhcloud.com/en/public-cloud/databases/postgresql-detect-terminate-long-running-queries
lang: en
---
> For AI agents: the complete documentation index is available at https://docs.us.ovhcloud.com/en/llms.txt, the full documentation bundle is available at https://docs.us.ovhcloud.com/en/llms-full.txt.

# PostgreSQL® - Detect and terminate long-running queries

**Learn how to detect and terminate specific sessions via the OVHcloud Control Panel and Command Line Interface (CLI).**

Queries on a PostgreSQL[®](#go-further) instance can run indefinitely, and OVHcloud will not stop them automatically. Such queries can lock resources inappropriately and cause wrong behaviors in your system.

 

## Requirements

- A [Public Cloud project](https://us.ovhcloud.com/public-cloud/) in your OVHcloud account
- A PostgreSQL® database running on your OVHcloud Managed Databases (also called _Cloud Databases_); [this guide](https://docs.us.ovhcloud.com/en/public-cloud/databases/getting-started.md) can help you to meet this requirement
- Access to the [OVHcloud Control Panel](https://manager.us.ovhcloud.com/)

 

## Instructions

### Terminate long-running queries with OVHcloud Control Panel

From your OVHcloud Control Panel, browse your Cloud Databases services, open the desired one, and click the `Requests` tab. This table regroups ongoing queries with some filtering options, such as showing idle or active connections.

The query table shows the exact query, along with detailed information: PID (Process ID), Duration, Database, Client address, and Application name.

In the table list, find the session you want to terminate, then click the _more options_ `...` button at the end of the row for that entry. You will be able to terminate the session and related backend processes directly.

The query will disappear, but if your client application is coded to redo the query after a failure, another query and PID will reappear.

 

### Terminate long-running queries with CLI (psql)

PostgreSQL® CLI, called `psql`, provides native administration functions such as `pg_terminate_backend()`.

For more information, please refer to their [official documentation](https://www.postgresql.org/docs/current/functions-admin.html).

 

#### Connect to your database using psql

Please refer to [this guide](https://docs.us.ovhcloud.com/en/public-cloud/databases/postgresql-connect-cli.md) to find out how to connect to your database using `psql`.

 

#### Make sure to get enough access privileges first

You can terminate a session only when you are the owner of the database.

To verify your roles and grants, run the following command in `psql`:

```
\l
```

It will return the list of databases and access privileges. As an example:

```bash
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
-----------+----------+----------+-------------+-------------+-----------------------
 defaultdb | avnadmin | UTF8     | en_US.UTF-8 | en_US.UTF-8 | avnadmin
           |          |          |             |             | bastien
```

 

If you don't see your user, you will have to grant them with required access privileges on the appropriate database. Otherwise, the instructions below will return an error.

 

#### Find long-running queries

Once you are connected to your database, type (for example):

```sql
SELECT * FROM pg_stat_activity WHERE state <> 'idle';
```

It will show you a list of queries and PIDs.

For more information about statistics, please follow the [official documentation](https://www.postgresql.org/docs/current/monitoring-stats.html).

 

#### Terminate a specific query

Now that you have the PID, you can terminate a specific query with the following command:

```sql
SELECT pg_terminate_backend(pid);
```

Where `pid` is the unique identifier found during the previous step. This command does not return any result when successful.

As explained here, the query will disappear if you check the statistics again, but if your client application is coded to launch the query again after a failure, another query and PID will reappear.

 

## Go further

Browse all the [Databases guides](https://docs.us.ovhcloud.com/en/public-cloud/databases/landing-page-databases.md).

Browse the rest of the [OVHcloud US documentation](https://docs.us.ovhcloud.com/en/index.md) for guides on our other products and services.

Learn more about [Managed Databases](https://us.ovhcloud.com/public-cloud/databases/) on the OVHcloud US website.

OVHcloud Managed Databases and Analytics:

- Grafana® is a registered trademark of Grafana Labs and is used with the permission of Grafana Labs. OVH SAS and its subsidiaries are not affiliated with or endorsed by Grafana Labs.
- Kafka® is a registered trademark of The Apache Software Foundation and has been licensed for use by OVHcloud, who has no affiliation with and is not endorsed by The Apache Software Foundation.
- MongoDB® is a registered trademark of MongoDB, Inc.
- MySQL® is a registered trademark of Oracle and/or its affiliates.
- PostgreSQL® is a registered trademark of the PostgreSQL Community Association of Canada.
