Menu
Amazon Redshift
Database Developer Guide (API Version 2012-12-01)

PG_TERMINATE_BACKEND

Terminates a session. You can terminate a session owned by your user. A superuser can terminate any session.

Syntax

Copy
pg_terminate_backend( pid )

Arguments

pid

The process ID of the session to be terminated. Requires an integer value.

Return Type

None

Usage Notes

If you are close to reaching the limit for concurrent connections, use PG_TERMINATE_BACKEND to terminate idle sessions and free up the connections. For more information, see Limits in Amazon Redshift.

If queries in multiple sessions hold locks on the same table, you can use PG_TERMINATE_BACKEND to terminate one of the sessions, which forces any currently running transactions in the terminated session to release all locks and roll back the transaction. Query the STV_LOCKS system table to view currently held locks.

If a query is not in a transaction block (BEGIN … END), you can cancel the query by using the CANCEL command or the PG_CANCEL_BACKEND function.

Examples

The following statement queries the STV_LOCKS table to view all locks in effect for current transactions:

Copy
select table_id, last_update, lock_owner, lock_owner_pid, lock_status from stv_locks; table_id | last_update | lock_owner | lock_owner_pid | lock_status ----------+----------------------------+------------+----------------+------------------------ 100295 | 2014-01-06 23:50:56.290917 | 95402 | 7723 | Holding write lock 100304 | 2014-01-06 23:50:57.408457 | 95402 | 7723 | Holding write lock 100304 | 2014-01-06 23:50:57.409986 | 95402 | 7723 | Holding insert lock (3 rows)

The following statement terminates the session holding the locks:

Copy
select pg_terminate_backend(7723);