SQLSTATE 25001 ERROR Class 25: Invalid Transaction State

active_sql_transaction VACUUM cannot run inside a transaction block — 25001

PostgreSQL error "VACUUM cannot run inside a transaction block" (SQLSTATE 25001): what it means, common causes, and how to fix it.

PG 12, 13, 14, 15, 16, 17, 18 Official docs
Last reviewed May 2025 Grounded in source

Diagnostic Queries

Symptoms

VACUUM was issued inside an explicit transaction block (after BEGIN). VACUUM manages its own transactions and cannot run inside one, so PostgreSQL raises SQLSTATE 25001 (active_sql_transaction).

  • Common when a tool wraps every statement in a transaction.
  • Same restriction applies to several maintenance commands.
  • VACUUM must be sent as its own statement with autocommit on.

What the server log shows

ERROR:  VACUUM cannot run inside a transaction block

Why PostgreSQL raises this — what the manual says

the VACUUM reference (Notes):

“VACUUM cannot be executed inside a transaction block.”

VACUUM opens and commits multiple internal transactions and may run concurrently with other work. That is incompatible with executing inside an outer transaction, so PostgreSQL rejects it with 25001.

Common causes

  • Running VACUUM after BEGIN.
  • A client/ORM that auto-wraps statements in a transaction.
  • A migration tool that disables autocommit globally.

How to fix it

  1. Run VACUUM with autocommit on, outside any transaction block.
  2. In psql, ensure you are not inside a BEGIN; just run VACUUM;.
  3. In code, switch the connection to autocommit before issuing VACUUM.

Related & next steps

Reference: PostgreSQL 18 — VACUUM.

Was this helpful?