select open change scope Open full search

PG.CENTER connects PostgreSQL documentation, reference, and ecosystem knowledge. Maintained by Pigsty.

SQLSTATE / CLASS 25 · INVALID TRANSACTION STATE

active_sql_transaction

SQLSTATE
25001
Condition name
active_sql_transaction
Class
Invalid Transaction State
Source macro
ERRCODE_ACTIVE_SQL_TRANSACTION
Evidence
Observed at runtime in the source evidence
Analysis & operational context

English SQLSTATE atlas: authored explanations and source/runtime evidence are separate from the manual definitions. View source ↗

<h1>25001 — active_sql_transaction</h1>

At a glance

25001 means the command requires a transaction boundary that the current session violates. The selected case runs VACUUM after BEGIN, receives the exact active-transaction error, rolls back, and then successfully runs VACUUM outside the transaction block.

Meaning

PostgreSQL uses 25001 for several paths. PreventInTransactionBlock rejects a prohibited utility in an explicit transaction block, subtransaction, or function; the source formats these as %s cannot run inside a transaction block, %s cannot run inside a subtransaction, and %s cannot be executed from a function. Other paths reject creating a logical replication slot after transaction writes, exporting a snapshot from a subtransaction, or setting an imported snapshot after a query. BEGIN inside an already active block is a separate WARNING, so severity and recovery depend on the source path.

Diagnosis

Record SQLSTATE, severity, primary message, transaction status, and the command. In the selected case the VACUUM ERROR moves the explicit session to INERROR; only ROLLBACK restores IDLE. For a function or subtransaction report, end that context and issue the command as one top-level utility statement outside any explicit transaction block (for example, a single command on an autocommit connection); retrying inside the same wrapper cannot satisfy PreventInTransactionBlock. For logical-slot and snapshot reports, inspect whether the transaction has writes, a subtransaction, or an earlier query. A WARNING that a transaction is already in progress is not the VACUUM ERROR and does not by itself require rollback. The selected repair then executes VACUUM items outside the transaction block and asserts success plus IDLE, proving the repair boundary instead of merely proving the connection is usable.

Response

Move a prohibited utility outside the transaction block, function, or subtransaction that violates its contract: issue it as one top-level command outside any explicit transaction block, typically on an autocommit connection. After an ERROR in an explicit transaction, issue ROLLBACK before reuse; do not put the retry inside another BEGIN. A logical replication slot must be created in a transaction with no prior writes; export a snapshot outside a subtransaction, and call SET TRANSACTION SNAPSHOT before any query. Preserve an already active transaction after the WARNING branch instead of rolling it back solely because BEGIN was repeated. Follow the exact source path rather than applying the VACUUM repair mechanically.

Observed diagnostics

18.6 (Homebrew) / latest:SQLSTATE 25001; primary VACUUM cannot run inside a transaction block; after_error INERROR; after_rollback IDLE; status_after_vacuum IDLE. 10.21 (Debian 10.21-1.pgdg90+1) / pg10:SQLSTATE 25001; primary VACUUM cannot run inside a transaction block; after_error INERROR; after_rollback IDLE; status_after_vacuum IDLE.

Source message templates

The selected VACUUM run does not cover these source-backed branches:

  • ERROR %s cannot run inside a subtransaction (the command name is substituted).
  • ERROR %s cannot be executed from a function (the command name is substituted).
  • ERROR cannot create logical replication slot in transaction that has performed writes.
  • ERROR cannot export a snapshot from a subtransaction.
  • ERROR SET TRANSACTION SNAPSHOT must be called before any query.
  • WARNING there is already a transaction in progress.

These templates are source evidence, not additional runtime claims for this case.

Representative case

In this example, VACUUM is rejected inside an explicit BEGIN block; after ROLLBACK, the same utility succeeds as a standalone command outside that block. The complete setup, assertions, and cleanup are in the case export:

-- create
CREATE TABLE items(id integer PRIMARY KEY, note text NOT NULL);
-- seed
INSERT INTO items VALUES (1, 'seed');
-- begin
BEGIN;
-- trigger
VACUUM items;
-- rollback
ROLLBACK;
-- followup
SELECT 1 AS usable;
-- repair
VACUUM items;

The SQLSTATE, diagnostic, transaction-state, and repair assertions for this excerpt are produced from the checked case registry; see the structured evidence and case export.

Authored evidence IDs: identity, utility-path, other-paths, runtime. Selected runtime records: runtime.25001-batch1-latest2-20260909.latest, runtime.25001-batch1-pg10b-20260909.pg10.

Versions

The locked catalogue observes the condition by 7.4 and in all listed formal snapshots. The selected VACUUM case passes on 18.6 and 10.21, including rollback recovery and the successful outside-transaction repair; other 25001 paths are source-backed only.

Sources

  • src.errcodes.18.6 (SHA-256 6e8de346643ba84aa3c9c6a73360acfc7b2dfb89162c06c08ce9bf5bcd5bbcba)
  • src.calls.REL_18_6 (SHA-256 9ee8a0e81d8f0825c5c1ae45583439859a26e602bdd4ce2f2a62aa278867ccbf)
  • src.xact.18.6 (SHA-256 75b012c0b047d1dc905a30975c244beec366e45eac0dbf109fd21bcd611a8e39)
  • src.logical.18.6 (SHA-256 3f1bd4c3e627fe78522c4dc9bacf9fa8200e6c82c2d01f7670706eee102b76d1)
  • src.snapmgr-export.18.6 (SHA-256 b605b69e77a026c143f4cabca079de364b9a8732bfc40f7595be44fb0971ee8)
  • src.snapmgr-set-snapshot.18.6 (SHA-256 b605b69e77a026c143f4cabca079de364b9a8732bfc40f7595be44fb0971ee8)
  • doc.vacuum.18.6 (SHA-256 80ca5592cda7b74938385f84f09faac33574374c1a05d605a2d55982e4cf1bbf) · official documentation

Source evidence

Evidence belongs to the frozen source and runtime versions listed here. It is not a runtime verification of the selected manual version.

25001 is active_sql_transaction in Class 25.

Method: Read the fixed condition row and macro.

Limits: The code covers several operations that require a transaction boundary, including subtransactions and function execution.

src.errcodes.18.6

A utility such as VACUUM cannot run inside an explicit transaction block; xact.c formats the statement name into the 25001 primary message.

Method: Trace PreventInTransactionBlock and the VACUUM transaction restriction.

Limits: Some commands are allowed inside transactions; inspect the command-specific contract rather than retrying blindly.

src.xact.18.6 doc.vacuum.18.6 src.calls.REL_18_6

The same SQLSTATE has distinct source templates for subtransactions, function execution, logical replication slots after writes, and snapshot operations; a WARNING “there is already a transaction in progress” is a separate severity path.

Method: Read all fixed 25001 report groups selected by the source call scan.

Limits: Severity and recovery depend on the exact path; do not treat every 25001 as an ERROR.

src.xact.18.6 src.calls.REL_18_6 src.logical.18.6 src.snapmgr-export.18.6 src.snapmgr-set-snapshot.18.6

The selected VACUUM case returned 25001 on PG18.6 and PG10.21, entered INERROR, recovered after ROLLBACK, accepted a follow-up query, and then completed VACUUM outside the transaction block with the connection IDLE.

Method: Read the selected summaries and raw diagnostics.

Limits: The case covers VACUUM in a transaction, not logical replication slots, snapshot export, function calls, or warning handling.

runtime.25001-batch1-latest2-20260909.latest runtime.25001-batch1-pg10b-20260909.pg10

Message templates

ERROR · transaction-block

Primary

%s cannot run inside a transaction block

The %s substitution is the command name; this is one ERROR path for 25001.

ERROR · subtransaction

Primary

%s cannot run inside a subtransaction

The format string is source-backed; substitutions are supplied by the executing path.

WARNING · already-in-progress

Primary

there is already a transaction in progress

This source path is explicitly WARNING and must not be presented as the VACUUM ERROR template.

ERROR · function

Primary

%s cannot be executed from a function

The %s substitution is the command name; this branch is rejected because it is called from a function.

ERROR · logical-slot-after-write

Primary

cannot create logical replication slot in transaction that has performed writes

The source condition is a transaction that already has a top-level transaction ID from writes.

ERROR · snapshot-subtransaction

Primary

cannot export a snapshot from a subtransaction

The branch rejects export from the current subtransaction; prior committed subtransactions are handled separately by the source.

ERROR · snapshot-after-query

Primary

SET TRANSACTION SNAPSHOT must be called before any query

The command must be at the top level of a fresh transaction before a query establishes the snapshot.

Reproduction & repair cases

vacuum_inside_transaction · PG 10, 18

Preconditions

  • A disposable table exists
  • VACUUM is issued after an explicit BEGIN

Trigger: Run VACUUM inside an explicit transaction block.

Expected assertions

  • SQLSTATE is 25001
  • The transaction enters INERROR and rollback restores IDLE
  • The same connection can execute a follow-up query
  • VACUUM succeeds after rollback outside the transaction block

Repair: Run transaction-control-restricted utility commands outside the transaction block; rollback the failed transaction before reuse.

Cleanup: Drop the case schema with an owner connection.

Recorded runtime evidence

18.6 (Homebrew) · passed

Run: 25001-batch1-latest2-20260909

{
  "usable": 1,
  "primary": "VACUUM cannot run inside a transaction block",
  "sqlstate": "25001",
  "after_error": "INERROR",
  "after_rollback": "IDLE",
  "status_after_vacuum": "IDLE",
  "vacuum_outside_transaction": true
}
10.21 (Debian 10.21-1.pgdg90+1) · passed

Run: 25001-batch1-pg10b-20260909

{
  "usable": 1,
  "primary": "VACUUM cannot run inside a transaction block",
  "sqlstate": "25001",
  "after_error": "INERROR",
  "after_rollback": "IDLE",
  "status_after_vacuum": "IDLE",
  "vacuum_outside_transaction": true
}

Definition snapshot: english-manuals:56cbb6ba8ff9cd9a7ea787551bd… · English manual source