yb_cancel_transaction()
Synopsis
When running concurrent transactions in YugabyteDB, queries may become blocked waiting for locks held by other transactions. This can cause performance degradation and application timeouts. yb_cancel_transaction() is a function that allows you to cancel a running distributed transaction, from any node in the cluster. The function takes a transaction ID for input.
It is intended for superusers and DB admins to manually cancel problematic transactions, similar in spirit to PostgreSQL's backend-cancel functions. yb_cancel_transaction() is the preferred way to abort a blocking transaction, because it only needs the transaction ID and works cluster‑wide; you don't need to know which TServer the backend is on. (Unlike single-node PostgreSQL, the blocking query may be running on a different TServer than where you're connected.)
Instructions
Basic usage:
SELECT yb_cancel_transaction('<yb_txn_id>');
Input is the YSQL transaction ID (UUID-style) as a string.
The function returns a boolean:
- true - the transaction was canceled.
- false - there was nothing to cancel (for example, the transaction was already completed).
Example
To identify blocking transactions, use pg_locks.
For example, to find all blocked transactions, you can filter for locks that have not yet been granted:
SELECT * FROM pg_locks WHERE granted = false;
The output provides the blocking transaction UUID (blocking_txn), as well as the IP address of the TServer where the blocking query is running.
Take the blocking_txn value and cancel it from any node:
SELECT yb_cancel_transaction('<blocking_txn>');