When facing locking issues in a PostgreSQL database, and you're the developer in charge without a database administrator, here are immediate actionable steps:
SELECT pid, relation::regclass, mode, state, query
FROM pg_locks l
JOIN pg_stat_activity a ON l.pid = a.pid
WHERE NOT granted;
SELECT a.pid, a.query, age(clock_timestamp(), query_start) AS "age", state
FROM pg_stat_activity a
JOIN pg_locks l ON l.pid = a.pid
WHERE a.datname = 'your_db_name' AND l.mode = 'ExclusiveLock' AND state = 'active';
'your_db_name'
with your actual database name.SELECT pg_terminate_backend(pid);
pid
with the process ID of the blocking query. Be cautious with this action as it may interrupt important queries or transactions.SELECT relation::regclass, mode, locktype, page, tuple, virtualxid, transactionid, classid, objid, objsubid, virtualtransaction, pid
FROM pg_locks
WHERE pid IN (SELECT pid FROM pg_stat_activity WHERE datname = 'your_db_name');
SELECT pid, age(now(), xact_start) AS xact_age, query
FROM pg_stat_activity
WHERE state IN ('idle in transaction', 'active')
ORDER BY xact_age DESC;
Remember, before terminating any process or making changes, ensure you understand the impact of these actions on your application and data integrity. These steps are immediate actions; further analysis and adjustments to application logic or database design may be necessary to prevent future locking issues.
(Perfect for DevOps & SREs)
(Perfect for DevOps & SREs)