Lab-verifiedReproduced and fixed on PostgreSQL 17.12 · 2026-11-30 · container image postgres:17
25P01 (no active SQL transaction) means you ran a command that only makes sense inside BEGIN ... COMMIT. psql and most drivers run in autocommit by default, so each statement is its own transaction unless you start one.
In the lab, SAVEPOINT, LOCK TABLE and DECLARE CURSOR all failed with 25P01 in autocommit (AUTOCOMMIT = on). The same statements worked after BEGIN;.
01SAVEPOINT without BEGIN
SAVEPOINT before_update; on its own failed with SAVEPOINT can only be used in transaction blocks. A savepoint marks a point inside a transaction, and in autocommit there is no open transaction to mark.
With BEGIN; first, the savepoint was created, the UPDATE was undone by ROLLBACK TO SAVEPOINT, and after COMMIT the balance was still 100.
Setup
CREATE TABLE accounts (id int PRIMARY KEY, balance int);
INSERT INTO accounts VALUES (1, 100);CREATE TABLE
INSERT 0 1SAVEPOINT before_update;ERROR: 25P01: SAVEPOINT can only be used in transaction blocks\echo AUTOCOMMIT = :AUTOCOMMITAUTOCOMMIT = onStart with BEGIN; (or turn off autocommit in your driver), then use savepoints.
BEGIN;
SAVEPOINT before_update;
UPDATE accounts SET balance = balance - 30 WHERE id = 1;
ROLLBACK TO SAVEPOINT before_update;
COMMIT;
SELECT * FROM accounts;BEGIN
SAVEPOINT
UPDATE 1
ROLLBACK
COMMIT
id | balance
----+---------
1 | 100
(1 row)02LOCK TABLE without BEGIN
LOCK TABLE stock IN EXCLUSIVE MODE; failed with LOCK TABLE can only be used in transaction blocks. A table lock lasts until the transaction ends, so outside a transaction it would end immediately.
Inside BEGIN; ... COMMIT; the lock was taken: pg_locks showed ExclusiveLock granted, and the UPDATE ran while holding it.
Setup
CREATE TABLE stock (item text PRIMARY KEY, qty int);
INSERT INTO stock VALUES ('pen', 10);CREATE TABLE
INSERT 0 1LOCK TABLE stock IN EXCLUSIVE MODE;ERROR: 25P01: LOCK TABLE can only be used in transaction blocks\echo AUTOCOMMIT = :AUTOCOMMITAUTOCOMMIT = onPut LOCK TABLE and the statements it protects in one BEGIN ... COMMIT block.
BEGIN;
LOCK TABLE stock IN EXCLUSIVE MODE;
SELECT mode, granted FROM pg_locks WHERE relation = 'stock'::regclass AND pid = pg_backend_pid();
UPDATE stock SET qty = qty - 1 WHERE item = 'pen';
COMMIT;BEGIN
LOCK TABLE
mode | granted
---------------+---------
ExclusiveLock | t
(1 row)
UPDATE 1
COMMIT03DECLARE CURSOR without BEGIN
DECLARE c CURSOR FOR SELECT ...; failed with DECLARE CURSOR can only be used in transaction blocks. A normal cursor is closed when its transaction ends.
After BEGIN;, the cursor was declared, FETCH 2 returned ids 1 and 2, and it was closed before COMMIT.
Setup
CREATE TABLE logs (id int);
INSERT INTO logs SELECT generate_series(1, 5);CREATE TABLE
INSERT 0 5DECLARE c CURSOR FOR SELECT id FROM logs ORDER BY id;ERROR: 25P01: DECLARE CURSOR can only be used in transaction blocks\echo AUTOCOMMIT = :AUTOCOMMITAUTOCOMMIT = onOpen the cursor inside a transaction and FETCH from it before COMMIT.
BEGIN;
DECLARE c CURSOR FOR SELECT id FROM logs ORDER BY id;
FETCH 2 FROM c;
CLOSE c;
COMMIT;BEGIN
DECLARE CURSOR
id
----
1
2
(2 rows)
CLOSE CURSOR
COMMITFAQ
What does "can only be used in transaction blocks" mean?
The command needs an open transaction. In autocommit mode there is none, so run BEGIN; first. In the lab SAVEPOINT, LOCK TABLE and DECLARE CURSOR all worked after BEGIN.
Is psql in autocommit mode by default?
Yes. In the lab \echo :AUTOCOMMIT printed on. Every statement commits on its own unless you start a transaction.
Why does LOCK TABLE need a transaction?
The lock is released at the end of the transaction. In the lab, inside BEGIN, pg_locks showed ExclusiveLock granted until COMMIT.
Is 25P01 the same as 3B001?
No. 25P01 is SAVEPOINT with no transaction at all. 3B001 is a transaction where the named savepoint does not exist.
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