SoFunction
Updated on 2025-03-03

Methods to close deadlock processes in PostgreSQL

Due to the PostgreSQL database used, there is no information. I had to google.

Finally, a solution was found in an English forum.

as follows:

1. Retrieve the ID of the deadlock process

Copy the codeThe code is as follows:

SELECT * FROM pg_stat_activity WHERE datname='Deadlocked Database ID';

In the retrieved field, the [wating] field, the one with data t, is the deadlock process. Find the corresponding value of the [procpid] column.

2. Kill the process

Copy the codeThe code is as follows:

SELECT pg_cancel_backend('The procpid value of that data deadlocked');

Result: After running, update this table again and SQL executes smoothly.
 
ps: I found the database's own function list and found that the pg_terminate_backend() function can also kill processes.

Content given on the official website:/docs/9.0/static/