Lab-verifiedReproduced and fixed on PostgreSQL 17.12 · 2026-11-30 · container image postgres:17
57P01 (admin_shutdown) is a FATAL error: the server ended your connection. In the lab, a superuser ran pg_terminate_backend(pid) on a session that had an open transaction. The next statement in that session got FATAL: 57P01: terminating connection due to administrator command and the connection was closed.
The same code is also sent when the server is shut down, but that was not run here. What the lab shows: the uncommitted UPDATE was rolled back, and running the work again in a new connection succeeded.
01Session killed with pg_terminate_backend while in a transaction
Session A (application_name = 'erp_batch') began a transaction and ran UPDATE stock SET qty = qty - 1. Session B (postgres) saw it as idle in transaction in pg_stat_activity and ran pg_terminate_backend(pid), which returned t. When A then sent COMMIT, it got FATAL 57P01 and server closed the connection unexpectedly.
Checking afterwards, qty was still 100: the UPDATE was not committed. Reconnecting and running the same transaction again committed, and qty became 99.
Setup
CREATE TABLE stock (item_id int PRIMARY KEY, qty int);
INSERT INTO stock VALUES (1, 100);CREATE TABLE
INSERT 0 1SET application_name = 'erp_batch';
BEGIN;
UPDATE stock SET qty = qty - 1 WHERE item_id = 1;SET
BEGIN
UPDATE 1SELECT pid, usename, state FROM pg_stat_activity WHERE application_name = 'erp_batch';
SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE application_name = 'erp_batch'; pid | usename | state
-----+---------+---------------------
582 | lab_a | idle in transaction
(1 row)
pg_terminate_backend
----------------------
t
(1 row)COMMIT;FATAL: 57P01: terminating connection due to administrator command
server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
connection to server was lostSELECT item_id, qty FROM stock; item_id | qty
---------+-----
1 | 100
(1 row)The killed session cannot be reused. Reconnect and re-run the whole transaction. To find who did it, look for pg_terminate_backend calls (scripts, monitoring tools, DBAs) around that time.
SET application_name = 'erp_batch';
BEGIN;
UPDATE stock SET qty = qty - 1 WHERE item_id = 1;
COMMIT;
SELECT item_id, qty FROM stock;SET
BEGIN
UPDATE 1
COMMIT
item_id | qty
---------+-----
1 | 99
(1 row)FAQ
What does terminating connection due to administrator command mean?
The server ended your session on purpose. In the lab it was caused by pg_terminate_backend(pid) from another session. The client sees it on its next statement.
Is my transaction committed after 57P01?
No. In the lab the UPDATE in the open transaction was rolled back; the row kept its old value.
How do I find sessions before terminating them?
Query pg_stat_activity (for example by application_name) to see pid, usename and state. Sessions in idle in transaction hold their changes until they end.
How we verify
Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.
lab_a and lab_b are ordinary roles created for the test, each owning a schema of the same name; postgres is the superuser. PostgreSQL 17.12