PostgreSQL Error 55006 Object in Use: How to Fix It
In this article
PostgreSQL error 55006 occurs when you attempt to modify or drop a database object (such as a table, view, or sequence) that is currently being accessed by another active session. To fix this, you must identify the process ID (PID) of the session holding the lock and terminate that session using the pg_terminate_backend function. This releases the lock and allows your administrative command to proceed.
What You'll Need
- A PostgreSQL client (such as psql, pgAdmin, or DBeaver).
- Superuser privileges or the
pg_signal_backendrole to terminate other users' sessions. - The name of the object (table or index) that is causing the conflict.
Step-by-Step
- Identify the locked object: First, confirm which object is causing the error. Usually, the error message explicitly states the name of the table or relation that is "in use." If you are unsure, note the exact name of the object you were trying to
DROP,ALTER, orVACUUM FULL. - Query the activity view: You need to find the Process ID (PID) of the session that is blocking your request. Run the following query in your SQL editor to see all active sessions and their current state. Note that menu labels and query results may vary slightly depending on whether you are using psql or a GUI tool like pgAdmin.
SELECT pid, usename, state, query FROM pg_stat_activity WHERE state != 'idle';This list shows you who is connected and what query they are currently running. - Locate the specific lock: To find the exact PID holding a lock on your specific table, use a join between
pg_locksandpg_stat_activity. Replace 'your_table_name' with the actual name of your object:SELECT pid, locktype, mode, granted FROM pg_locks WHERE relation = 'your_table_name'::regclass;Thepidcolumn in the result is the unique identifier for the session that is preventing your change. - Analyze the session state: Before killing the process, check the
stateof that PID inpg_stat_activity. If the state is 'active', a long-running query is currently using the table. If the state is 'idle in transaction', a user started a transaction (e.g.,BEGIN) but never issued aCOMMITorROLLBACK, leaving the lock open indefinitely. - Terminate the blocking session: Once you have identified the PID (for example, PID 1234), use the
pg_terminate_backendfunction to force the session to close. This is the most effective way to clear error 55006:SELECT pg_terminate_backend(1234);
Replace 1234 with the actual PID you found in the previous steps. This command sends a signal to the backend process to terminate immediately. - Retry your original command: Now that the lock has been released, execute your original
ALTER TABLE,DROP TABLE, orTRUNCATEcommand. It should now execute without the 55006 error.
Common Mistakes
One of the most frequent mistakes is attempting to use pg_cancel_backend instead of pg_terminate_backend. While pg_cancel_backend stops a specific query from running, it does not close the connection. If the session is 'idle in transaction,' cancelling the query does nothing because there is no active query to stop—the lock remains held by the open transaction. You must terminate the entire backend process to be certain the lock is released.
Another common error is attempting to run these commands without sufficient permissions. If you are logged in as a standard application user, you cannot terminate sessions belonging to other users or the database administrator. You must be logged in as a superuser (usually the postgres user) to manage other PIDs.
Users also frequently forget to check for "Idle in Transaction" states. Many developers open a transaction in a GUI tool like pgAdmin, make a change, and then leave the tool open without clicking 'Commit'. This creates a "ghost lock" that persists even if no active query is running, leading to the 55006 error for anyone else trying to modify the schema.
Tips
To prevent this error from happening in production environments, avoid performing schema changes during peak traffic hours. Heavy read/write loads increase the likelihood of lock contention. If you must perform an update, consider using the SET lock_timeout setting. By setting a timeout (e.g., SET lock_timeout = '5s';), your command will fail quickly if it cannot acquire a lock, rather than hanging and potentially blocking other incoming queries in a queue.
When writing application code, always ensure that transactions are wrapped in try...finally blocks or use context managers that guarantee a COMMIT or ROLLBACK. This prevents the 'idle in transaction' state that causes most 55006 errors.
If you are using a connection pooler like PgBouncer, be aware that the PID you see in the pooler is not the same as the PID in the actual PostgreSQL backend. Always run your diagnostic queries directly on the PostgreSQL instance to get the correct backend PID.
Troubleshooting
If you have terminated the PID but the error persists, it is possible that the object is being locked by multiple sessions. Run the lock query again to see if a new PID has appeared. In high-concurrency environments, as soon as one lock is released, another waiting process may grab it immediately.
If the PID refuses to die after pg_terminate_backend, check the operating system level. In extreme cases, a backend process may become a "zombie" or be stuck in an uninterruptible sleep state (D state in Linux). In this scenario, you may need to use the OS kill -9 command on the process ID, but be extremely cautious. Killing a PostgreSQL process at the OS level can sometimes trigger a database crash and a subsequent recovery restart to ensure data integrity.
If you cannot find any locks in pg_locks but still receive the error, ensure you are connected to the correct database. Locks are database-specific; if you are querying pg_stat_activity in the postgres database but the lock is in the app_db database, you may not see the relevant details unless you are using a global view.
Frequently Asked Questions
Why does this happen during a simple DROP TABLE?
A DROP TABLE command requires an ACCESS EXCLUSIVE lock. This is the strongest lock level in PostgreSQL. If any other session has even a simple ACCESS SHARE lock (which is acquired by a basic SELECT query), the DROP command cannot proceed and will eventually throw error 55006 if it cannot acquire the lock.
Is it safe to use pg_terminate_backend on a production server?
It is generally safe, but it will cause the connection for that specific user to be dropped. Any uncommitted work in that session will be rolled back. You should always check the query column in pg_stat_activity first to ensure you aren't killing a critical system process or a massive data migration that is 99% complete.
Can I avoid this error by using a different command?
No, because the error is not about the command you are using, but about the lock requirements of that command. Any operation that requires an exclusive lock (like ALTER TABLE, TRUNCATE, or DROP) will trigger this error if the object is in use. The only solution is to clear the existing locks.
What is the difference between a lock and a block?
A lock is a mechanism used by PostgreSQL to ensure data consistency. A block occurs when one session requests a lock that is incompatible with a lock already held by another session. Error 55006 is the result of this blocking behavior where the requested lock cannot be granted because the object is "in use."
Does restarting the database server fix error 55006?
Yes, restarting the PostgreSQL service will terminate all active connections and release all locks. However, this is a nuclear option that causes downtime for all users. Using pg_terminate_backend is the preferred surgical approach as it only affects the blocking session.