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
CREATE TABLE orders (id int PRIMARY KEY, customer_id int);
INSERT INTO orders SELECT g, g % 100 FROM generate_series(1, 1000) g;CREATE TABLE
INSERT 0 1000BEGIN;
CREATE INDEX CONCURRENTLY orders_customer_idx ON orders (customer_id);
COMMIT;BEGIN
ERROR: 25001: CREATE INDEX CONCURRENTLY cannot run inside a transaction block
ROLLBACKSELECT indexname FROM pg_indexes WHERE tablename = 'orders' ORDER BY indexname; 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.
CREATE INDEX CONCURRENTLY orders_customer_idx ON orders (customer_id);
SELECT indexname FROM pg_indexes WHERE tablename = 'orders' ORDER BY indexname;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
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;CREATE TABLE
INSERT 0 1000
DELETE 900BEGIN;
DELETE FROM logs WHERE id <= 950;
VACUUM logs;
COMMIT;BEGIN
DELETE 50
ERROR: 25001: VACUUM cannot run inside a transaction block
ROLLBACKSELECT count(*) FROM logs; count
-------
100
(1 row)Commit the DELETE first, then run VACUUM by itself. The delete is kept (50 rows remain) and VACUUM completes.
BEGIN;
DELETE FROM logs WHERE id <= 950;
COMMIT;
VACUUM logs;
SELECT count(*) FROM logs;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
CREATE TABLE accounts (id int PRIMARY KEY, balance int);
INSERT INTO accounts VALUES (1, 100);CREATE TABLE
INSERT 0 1BEGIN;
SELECT balance FROM accounts WHERE id = 1;
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
COMMIT;BEGIN
balance
---------
100
(1 row)
ERROR: 25001: SET TRANSACTION ISOLATION LEVEL must be called before any query
ROLLBACKSHOW default_transaction_isolation; default_transaction_isolation
-------------------------------
read committed
(1 row)Set the level when you start: BEGIN ISOLATION LEVEL SERIALIZABLE. SHOW transaction_isolation then returns serializable.
BEGIN ISOLATION LEVEL SERIALIZABLE;
SELECT balance FROM accounts WHERE id = 1;
SHOW transaction_isolation;
COMMIT;BEGIN
balance
---------
100
(1 row)
transaction_isolation
-----------------------
serializable
(1 row)
COMMIT04CREATE 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
DROP DATABASE IF EXISTS shop;NOTICE: 00000: database "shop" does not exist, skipping
DROP DATABASEBEGIN;
CREATE DATABASE shop;
COMMIT;BEGIN
ERROR: 25001: CREATE DATABASE cannot run inside a transaction block
ROLLBACKSELECT count(*) FROM pg_database WHERE datname = 'shop'; 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.
CREATE DATABASE shop;
SELECT datname FROM pg_database WHERE datname = 'shop';
DROP DATABASE shop;CREATE DATABASE
datname
---------
shop
(1 row)
DROP DATABASEFAQ
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