Using RDS postgresql and terminating other processes

Sometimes it may be necessary to terminate a postgres users process, for example if it were holding a lock an excessive amount of time that is blocking an important process from running.

 select pg_terminate_backend(<pid>);
-- or 
select pg_cancel_backend(<pid>);

Something else you can do to find blocking locks, that processes/PID are holding where you may want to use pg_terminate_backend or pg_cancel_backend is create a view that simplifies finding locks or blocking locks, like this:

CREATE OR REPLACE VIEW public.active_locks
AS SELECT t.schemaname,
   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;

You can also further qualify when using this view by table_name by using relname=’your table’ and or schemaname=’your schema’

Here’s an excellent blog that talks about a number of things related to finding locks, blocking processes, long running queries etc. It talks about a lot of things the blog is informative on subjects related to locks certainly:

Leave a Comment

Your email address will not be published. Required fields are marked *