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.