PostgreSQL Deadlock Handling

Source: Internet
Author: User
Tags postgresql

Origin

When a strange phenomenon occurs, the Select and delete tables are executed normally, but the truncate and drop tables run all the time, and no error is encountered.

Reason

"Drop table" and "TRUNCATE TABLE" need to apply for exclusive lock "ACCESS EXCLUSIVE", the execution of this command is stuck, it shows that there are still operations on this table, such as query, etc.
Then only wait for this query operation to complete, "drop table" or "TRUNCATE TABLE" or add the field of SQL to get the "Access EXCLUSIVE" lock on this table, the operation can proceed.

Solution

1. Retrieves the ID of the deadlock process.

Select Oid,relname from Pg_class where relname= ' all_data ';

In the retrieved field, the "wating" field, the one with the data T, is the deadlock process. Locate the value of the corresponding "Procpid" column.

2. Kill the process.

SELECT pg_cancel_backend (' procpid value of the deadlock data ');

Result: After running, update the table again and SQL executes successfully.

If Pg_stat_activity does not have a record, query pg_locks if there is a lock on this object

Select locktype,pid,relation,mode,granted,* from where relation= ' oid ' above queried;

Kill the process

Select Pg_cancel_backend (' Process id ');

Another pg_terminate_backend () function can also kill a process.

Summarize
Select Locktype,database,pid,relation, mode from pg_locks where relation= above Oid;select * from pg_stat_activity where PID = Above Pidselect pg_terminate_backend (upper PID);
Delete Database said to be linked

Abnormal

db_v43=> drop Database test_db; ERROR:  Database "test_db" is being accessed by other Usersdetail:  There are 1 other session using the database.

Solve

Then you can delete the database.

PostgreSQL Deadlock Handling

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.