PostgreSQL 17.12Lab-verified · 2026-11-30

PostgreSQL error 25P01: can only be used in transaction blocks

A command that only works inside a transaction block was run in autocommit mode.

PostgreSQL 17.12
25P01: SAVEPOINT can only be used in transaction blocks

Jump to a cause

  1. 01SAVEPOINT without BEGIN
  2. 02LOCK TABLE without BEGIN
  3. 03DECLARE CURSOR without BEGIN

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
Setuprun as lab_a
CREATE TABLE accounts (id int PRIMARY KEY, balance int);
INSERT INTO accounts VALUES (1, 100);
OUTPUT
CREATE TABLE
INSERT 0 1
Reproducerun as lab_a
SAVEPOINT before_update;
✕ ERROR
ERROR:  25P01: SAVEPOINT can only be used in transaction blocks
Checkrun as lab_a
\echo AUTOCOMMIT = :AUTOCOMMIT
OUTPUT
AUTOCOMMIT = on

Start with BEGIN; (or turn off autocommit in your driver), then use savepoints.

After the fixrun as lab_a
BEGIN;
SAVEPOINT before_update;
UPDATE accounts SET balance = balance - 30 WHERE id = 1;
ROLLBACK TO SAVEPOINT before_update;
COMMIT;
SELECT * FROM accounts;
✓ SUCCESS
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
Setuprun as lab_a
CREATE TABLE stock (item text PRIMARY KEY, qty int);
INSERT INTO stock VALUES ('pen', 10);
OUTPUT
CREATE TABLE
INSERT 0 1
Reproducerun as lab_a
LOCK TABLE stock IN EXCLUSIVE MODE;
✕ ERROR
ERROR:  25P01: LOCK TABLE can only be used in transaction blocks
Checkrun as lab_a
\echo AUTOCOMMIT = :AUTOCOMMIT
OUTPUT
AUTOCOMMIT = on

Put LOCK TABLE and the statements it protects in one BEGIN ... COMMIT block.

After the fixrun as lab_a
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;
✓ SUCCESS
BEGIN
LOCK TABLE
     mode      | granted
---------------+---------
 ExclusiveLock | t
(1 row)

UPDATE 1
COMMIT

03DECLARE 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
Setuprun as lab_a
CREATE TABLE logs (id int);
INSERT INTO logs SELECT generate_series(1, 5);
OUTPUT
CREATE TABLE
INSERT 0 5
Reproducerun as lab_a
DECLARE c CURSOR FOR SELECT id FROM logs ORDER BY id;
✕ ERROR
ERROR:  25P01: DECLARE CURSOR can only be used in transaction blocks
Checkrun as lab_a
\echo AUTOCOMMIT = :AUTOCOMMIT
OUTPUT
AUTOCOMMIT = on

Open the cursor inside a transaction and FETCH from it before COMMIT.

After the fixrun as lab_a
BEGIN;
DECLARE c CURSOR FOR SELECT id FROM logs ORDER BY id;
FETCH 2 FROM c;
CLOSE c;
COMMIT;
✓ SUCCESS
BEGIN
DECLARE CURSOR
 id
----
  1
  2
(2 rows)

CLOSE CURSOR
COMMIT

FAQ

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