PostgreSQL 17.12Lab-verified · 2026-11-30

PostgreSQL error 25001: active SQL transaction (cannot run inside a transaction block)

The command must run outside an explicit transaction (or before any query in it), but a BEGIN was already open.

PostgreSQL 17.12
25001: CREATE INDEX CONCURRENTLY cannot run inside a transaction block

Jump to a cause

  1. 01CREATE INDEX CONCURRENTLY inside BEGIN … COMMIT
  2. 02VACUUM inside a transaction block
  3. 03SET TRANSACTION ISOLATION LEVEL after a query in the same transaction
  4. 04CREATE DATABASE inside a transaction block

Lab-verifiedReproduced and fixed on PostgreSQL 17.12 · 2026-11-30 · container image postgres:17

Some commands manage their own transactions and refuse to run after BEGIN: CREATE INDEX CONCURRENTLY, VACUUM, CREATE DATABASE and others. SET TRANSACTION ISOLATION LEVEL shares the same SQLSTATE when it comes after a query.

The error aborts the whole transaction. In the runs below the final COMMIT prints ROLLBACK, so any change made earlier in that block is lost too. Migration tools and GUI clients often wrap every script in a transaction, so this can happen even if you never typed BEGIN.

01CREATE INDEX CONCURRENTLY inside BEGIN … COMMIT

CONCURRENTLY builds the index without blocking writes by using several transactions internally, so it cannot run inside one you opened. Afterwards only orders_pkey exists.

Setup
Setuprun as lab_a
CREATE TABLE orders (id int PRIMARY KEY, customer_id int);
INSERT INTO orders SELECT g, g % 100 FROM generate_series(1, 1000) g;
OUTPUT
CREATE TABLE
INSERT 0 1000
Reproducerun as lab_a
BEGIN;
CREATE INDEX CONCURRENTLY orders_customer_idx ON orders (customer_id);
COMMIT;
✕ ERROR
BEGIN
ERROR:  25001: CREATE INDEX CONCURRENTLY cannot run inside a transaction block
ROLLBACK
Checkrun as lab_a
SELECT indexname FROM pg_indexes WHERE tablename = 'orders' ORDER BY indexname;
OUTPUT
  indexname
-------------
 orders_pkey
(1 row)

Run it on its own, outside any transaction. The index orders_customer_idx is created. In migration tools, turn off the per-migration transaction for this step.

After the fixrun as lab_a
CREATE INDEX CONCURRENTLY orders_customer_idx ON orders (customer_id);
SELECT indexname FROM pg_indexes WHERE tablename = 'orders' ORDER BY indexname;
✓ SUCCESS
CREATE INDEX
      indexname
---------------------
 orders_customer_idx
 orders_pkey
(2 rows)

02VACUUM inside a transaction block

DELETE 50 ran first, then VACUUM logs failed. COMMIT turned into ROLLBACK, and count(*) still shows 100 rows: the 50 deleted rows came back.

Setup
Setuprun as lab_a
CREATE TABLE logs (id int PRIMARY KEY, msg text);
INSERT INTO logs SELECT g, 'x' FROM generate_series(1, 1000) g;
DELETE FROM logs WHERE id <= 900;
OUTPUT
CREATE TABLE
INSERT 0 1000
DELETE 900
Reproducerun as lab_a
BEGIN;
DELETE FROM logs WHERE id <= 950;
VACUUM logs;
COMMIT;
✕ ERROR
BEGIN
DELETE 50
ERROR:  25001: VACUUM cannot run inside a transaction block
ROLLBACK
Checkrun as lab_a
SELECT count(*) FROM logs;
OUTPUT
 count
-------
   100
(1 row)

Commit the DELETE first, then run VACUUM by itself. The delete is kept (50 rows remain) and VACUUM completes.

After the fixrun as lab_a
BEGIN;
DELETE FROM logs WHERE id <= 950;
COMMIT;
VACUUM logs;
SELECT count(*) FROM logs;
✓ SUCCESS
BEGIN
DELETE 50
COMMIT
VACUUM
 count
-------
    50
(1 row)

03SET TRANSACTION ISOLATION LEVEL after a query in the same transaction

The isolation level is fixed when the transaction runs its first query. The SELECT already ran, so the message is SET TRANSACTION ISOLATION LEVEL must be called before any query.

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
BEGIN;
SELECT balance FROM accounts WHERE id = 1;
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
COMMIT;
✕ ERROR
BEGIN
 balance
---------
     100
(1 row)

ERROR:  25001: SET TRANSACTION ISOLATION LEVEL must be called before any query
ROLLBACK
Checkrun as lab_a
SHOW default_transaction_isolation;
OUTPUT
 default_transaction_isolation
-------------------------------
 read committed
(1 row)

Set the level when you start: BEGIN ISOLATION LEVEL SERIALIZABLE. SHOW transaction_isolation then returns serializable.

After the fixrun as lab_a
BEGIN ISOLATION LEVEL SERIALIZABLE;
SELECT balance FROM accounts WHERE id = 1;
SHOW transaction_isolation;
COMMIT;
✓ SUCCESS
BEGIN
 balance
---------
     100
(1 row)

 transaction_isolation
-----------------------
 serializable
(1 row)

COMMIT

04CREATE DATABASE inside a transaction block

CREATE DATABASE cannot be rolled back, so PostgreSQL refuses to run it inside BEGIN. Setup scripts that wrap everything in one transaction hit this.

Setup
Setuprun as postgres
DROP DATABASE IF EXISTS shop;
OUTPUT
NOTICE:  00000: database "shop" does not exist, skipping
DROP DATABASE
Reproducerun as postgres
BEGIN;
CREATE DATABASE shop;
COMMIT;
✕ ERROR
BEGIN
ERROR:  25001: CREATE DATABASE cannot run inside a transaction block
ROLLBACK
Checkrun as postgres
SELECT count(*) FROM pg_database WHERE datname = 'shop';
OUTPUT
 count
-------
     0
(1 row)

Run CREATE DATABASE as a separate statement outside the transaction. pg_database then shows shop. DROP DATABASE has the same rule.

After the fixrun as postgres
CREATE DATABASE shop;
SELECT datname FROM pg_database WHERE datname = 'shop';
DROP DATABASE shop;
✓ SUCCESS
CREATE DATABASE
 datname
---------
 shop
(1 row)

DROP DATABASE

FAQ

How do I fix CREATE INDEX CONCURRENTLY cannot run inside a transaction block?

Run the statement by itself, not between BEGIN and COMMIT. In migration tools, disable the transaction for that migration.

Why does VACUUM cannot run inside a transaction block happen in my client?

Your client or tool opened a transaction (autocommit off). Commit first, then run VACUUM in autocommit mode.

How do I set the isolation level in PostgreSQL?

Use BEGIN ISOLATION LEVEL SERIALIZABLE, or SET TRANSACTION right after BEGIN before any query.

Is my earlier work saved after error 25001?

No. The transaction is aborted and COMMIT becomes ROLLBACK, so earlier changes in the block are lost. Run them again.

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