72
What is the output of
SELECT *
FROM pg_locks l
JOIN pg_class t ON l.relation = t.oid AND t.relkind = 'r'
WHERE t.relname = 'Bill';
It might be that there’re other sessions using your table in parallel and you cannot obtain Access Exclusive lock to drop it.
27
Just do
SELECT pid, relname
FROM pg_locks l
JOIN pg_class t ON l.relation = t.oid AND t.relkind = 'r'
WHERE t.relname = 'Bill';
And then kill every pid by
kill 1234
Where 1234 is your actual pid from query results.
You can pipe it all together like this (so you don’t have to copy-paste every pid manually):
psql -c "SELECT pid FROM pg_locks l
JOIN pg_class t ON l.relation = t.oid AND t.relkind = 'r'
WHERE t.relname = 'Bill';" | tail -n +3 | head -n -2 | xargs kill
- [Django]-How to set current user to user field in Django Rest Framework?
- [Django]-Django Model MultipleChoice
- [Django]-Sending an SMS to a Cellphone using Django
14
If this is for AWS postgres run the first statement to get the PID (Process ID) and then run the second query to terminate the process (it would be very similar to do kill -9 but since this is in the cloud that’s what AWS recommends)
-- gets you the PID
SELECT pid, relname FROM pg_locks l JOIN pg_class t ON l.relation = t.oid AND t.relkind = 'r' WHERE t.relname = 'YOUR_TABLE_NAME'
-- what actually kills the PID ...it is select statement but it kills the job!
SELECT pg_terminate_backend(YOUR_PID_FROM_PREVIOUS_QUERY);
- [Django]-How to force application version on AWS Elastic Beanstalk
- [Django]-Select between two dates with Django
- [Django]-Django post_save() signal implementation
11
So I was hitting my head against the wall for some hours trying to solve the same issue, and here is the solution that worked for me:
Check if PostgreSQL has a pending prepared transaction that’s never been committed or rolled back:
SELECT database, gid FROM pg_prepared_xacts;
If you get a result, then for each transaction gid you must execute a ROLLBACK from the database having the problem:
ROLLBACK PREPARED 'the_gid';
For further information, click here.
- [Django]-List_display – boolean icons for methods
- [Django]-Django Admin nested inline
- [Django]-How to change User representation in Django Admin when used as Foreign Key?
7
I ran into this today, I was issuing a:
DROP TABLE TableNameHere
and getting ERROR: table "tablenamehere" does not exist
. I realized that for case-sensitive tables (as was mine), you need to quote the table name:
DROP TABLE "TableNameHere"
- [Django]-Celery: WorkerLostError: Worker exited prematurely: signal 9 (SIGKILL)
- [Django]-How does Django's nested Meta class work?
- [Django]-Warning: Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'
- [Django]-Unable to find a locale path to store translations for file __init__.py
- [Django]-Django vs. Model View Controller
- [Django]-How to get a list of all users with a specific permission group in Django
3
The same thing happened for me–except that it was because I forgot the semicolon. face palm
- [Django]-Django: using blocks in included templates
- [Django]-How to set up a PostgreSQL database in Django
- [Django]-Error: "dictionary update sequence element #0 has length 1; 2 is required" on Django 1.4
2
Old question but ran into a similar issue. Could not reboot the database so tested a few things until this sequence worked :
- truncate table foo;
- drop index concurrently foo_something; times 4-5x
- alter table foo drop column whatever_foreign_key; times 3x
- alter table foo drop column id;
- drop table foo;
- [Django]-Django set range for integer model field as constraint
- [Django]-Django url pattern – string parameter
- [Django]-Django – Render the <label> of a single form field