Learn how to detect and terminate specific sessions via the OVHcloud Control Panel and Command Line Interface (CLI).
Queries on a PostgreSQL® 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 in your OVHcloud account
- A PostgreSQL® database running on your OVHcloud Managed Databases (also called Cloud Databases); this guide can help you to meet this requirement
- Access to the OVHcloud Control Panel
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.
Connect to your database using psql
Please refer to this guide 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:
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):
It will show you a list of queries and PIDs.
For more information about statistics, please follow the official documentation.
Terminate a specific query
Now that you have the PID, you can terminate a specific query with the following command:
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
For more information and tutorials, please see our other Managed Databases & Analytics or Platform as a Service guides. You can also explore the guides for other OVHcloud products and services.
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.