Sql – How to release possible Postgres row locks

databasepostgresqlsql

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?

Best Answer

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