Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to release possible Postgres row locks?

I ran an update statement on a large PostgreSQL table through the phpPgAdmin interface. This timed out as it ran for too long.

I can now update some rows from that table but not all. Trying to update some rows will hang.

Are the rows locked? How can I allow these rows to be updated?

like image 398
Liam Avatar asked Jun 30 '09 10:06

Liam


People also ask

How do you check if there is any lock on table in PostgreSQL?

pid = bl. pid WHERE NOT bl. granted; then it returns queries which are waiting to acquire a lock.

Do Postgres transactions lock rows?

PostgreSQL doesn't remember any information about modified rows in memory, so there is no limit on the number of rows locked at one time. However, locking a row might cause a disk write, e.g., SELECT FOR UPDATE modifies selected rows to mark them locked, and so will result in disk writes.


1 Answers

It's possible to see the locks.

Here is a view to make it a bit easier than using pg_locks directly:

CREATE OR REPLACE VIEW public.active_locks AS   SELECT t.schemaname,     t.relname,     l.locktype,     l.page,     l.virtualtransaction,     l.pid,     l.mode,     l.granted    FROM pg_locks l    JOIN pg_stat_all_tables t ON l.relation = t.relid   WHERE t.schemaname <> 'pg_toast'::name AND t.schemaname <> 'pg_catalog'::name   ORDER BY t.schemaname, t.relname; 

Then you just select from the view:

SELECT * FROM active_locks; 

And kill it with:

SELECT pg_cancel_backend('%pid%'); 

Other solutions: http://wiki.postgresql.org/wiki/Lock_Monitoring

like image 65
Chris Avatar answered Oct 22 '22 10:10

Chris