Killing a process ID in PostgreSQL
Today, we will learn how to kill or stop a running query in the background when working with PostgreSQL database.
This may happen if the frontend stops working but a background process is still running. In this case, you may want to kill the process.
pg_cancel_backend
Kill the process ID in PostgreSQL
using
First, view all running tasks using this command:
select * from pg_stat_activity;
Now, if you want to delete the task from here, use the following query:
SELECT pg_cancel_backend(11080), pg_terminate_backend(11080) FROM pg_stat_activity WHERE state = 'active';
We use 11080 for active
the process and terminate it using this statement. It will return to later idle
.
Therefore, pg_cancel_backend
we tend to be a bit lenient when it comes to killing a process, as it takes some time. But in many cases it may come back running; hence we use pg_terminate_backend
hard kill of the process.
Do not call this as the only active process in the system, as this may close your current query. Therefore, it is wise to use these features only when needed.
You always have the option of terminating your current PostgreSQL session and restarting it instead of killing the process.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Difference between timestamps with and without time zone in PostgreSQL
Publish Date:2025/04/27 Views:83 Category:PostgreSQL
-
This article will discuss the timestamp types in PostgreSQL and show how they differ. Timestamps in PostgreSQL In PostgreSQL, there are two types of timestamps. Timestamp without time zone Timestamp with time zone The first stores the local
Terminate the PostgreSQL connection
Publish Date:2025/04/11 Views:200 Category:PostgreSQL
-
In this article, we will learn how to terminate a PostgreSQL session. Any open connections are run by background processes or tasks, PSQL which may no longer exist despite exiting the user interface or command line tool. Use ps -ef or grep
Single query to rename and change column type in PostgreSQL
Publish Date:2025/04/11 Views:166 Category:PostgreSQL
-
This article describes how to rename a column and change its type in PostgreSQL using only a single query. Renaming and changing column types in MySQL In MySQL , if you want to change the column type and rename it, you can use a simple stat
Joining columns using Select in PostgreSQL
Publish Date:2025/04/11 Views:177 Category:PostgreSQL
-
MySQL PostgreSQL is an object-relational database system, which means it can support more complex data types than its competitors . Today we will learn how to use SELECT the operator to join the columns of a table. Using operators to || joi
Using CASE in PostgreSQL
Publish Date:2025/04/11 Views:124 Category:PostgreSQL
-
This article shows how to use the statement in PostgreSQL CASE . CASE How to use the statement in PostgreSQL case Statements are similar to those in general-purpose programming languages if-else . But in SQL, if you want to write IF-ELSE ,
Using NOT IN with subqueries in PostgreSQL
Publish Date:2025/04/11 Views:93 Category:PostgreSQL
-
NOT IN The inverts the result of NOT simply using IN the operator. NOT IN The right side of the operator must have a subquery in which multiple columns are returned to check whether the expression matches the data. NOT IN Tends to return tr
Using variables in PostgreSQL
Publish Date:2025/04/11 Views:171 Category:PostgreSQL
-
This article will demonstrate how we can declare and assign values to variables in PostgreSQL. In PostgreSQL, DECLARE variables are declared using Often you will need variables in your PL/SQL scripts. In DECLARE the section called , y
Connect to PostgreSQL using a password
Publish Date:2025/04/11 Views:171 Category:PostgreSQL
-
This article shows various ways to connect to PostgreSQL using a password. It can be through the command line, pgpass a file, PGPASSWORD an environment variable or a connection string. Connecting to PostgreSQL with a password using the comm
Deleting a database in PostgreSQL via PSQL
Publish Date:2025/04/11 Views:166 Category:PostgreSQL
-
There are two ways to access PostgreSQL objects and databases on your system. One is through an interface, such as a graphical interface like PGADMIN, and the other is the basic command line tool psql. Today, we will look at DROP DATABASE t