I\'m trying to create a new table in postgres but when I do it just hangs after the CREATE TABLE
call.
$ sudo usermod -s /bin/bash postgres
$ sudo s
If restarting postgres is an option, then that will most likely solve the issue and will save you from spending time reading the rest of this answer :-)
Check the pg_stat_activity
view, there is probably some other transaction blocking the schema change.
select * from pg_stat_activity
where
wait_event_type is NULL and xact_start is not NULL order by xact_start;
(the pg_stat_activity is changed a bit in every major pg release, try this for elder versions):
select * from pg_stat_activity
where
not waiting and xact_start is not NULL order by xact_start;
The first row to show up is probably the one causing problems. It is often an "idle in transaction" - this may very well hold locks, and if it's an old transaction it may as well kill performance. Probably the programmer forgot to ensure ending the transaction with "commit" or "rollback", or maybe some db session got stuck due to network problems.
To terminate transaction with pid 1234, use select pg_cancel_backend(1234);
, if that fails, select pg_terminate_backend(1234)
. With shell access, the equivalent commands are kill -INT 1234
and kill 1234
. (keep in mind, kill -9 1234
is a really bad idea).
There is also a view pg_locks
which may give some insight, though it may probably not be that easy to get any useful info out from it. If granted
is true, the lock is held, when granted
is false it means the query is waiting for the lock. Here are some more hints here on how to extract useful info from pg_locks: http://wiki.postgresql.org/wiki/Lock_Monitoring
If everything else fails, then it's probably time to go for the simple solution, restart that database server.
This is explained in this thread; http://www.postgresql.org/message-id/75218696-61be-4730-89f6-dd6058fa9eda@a28g2000prb.googlegroups.com
According to Tom Lane,
this create operation references auth_user table and PostgreSQL should add triggers to both tables. So if there is a long-running (probably idle) open transaction that's holding AccessShare lock on auth_user Postgres is blocked waiting for that xact to finish and release its lock. Everything else queues up behind the Create.
That PostgreSQL Mail thread is a good read.