Transactional DDL and Rollback Safety in PostgreSQL
0 reputation · 22 Feb 2020, 10:17 UTC
0 reputation · 22 Feb 2020, 10:17 UTC
The objective is to guarantee that any schema modification performed within a PostgreSQL transaction can be fully undone if the transaction aborts, leaving the database in its original state.
Currently PostgreSQL treats most DDL statements as auto‑committed, so they persist even when a surrounding transaction rolls back; only a subset of DDL, such as ALTER TABLE … ADD COLUMN, can be rolled back, while commands like CREATE INDEX CONCURRENTLY or CREATE DATABASE remain outside transactional control and modify catalog tables that are not automatically reverted.
What mechanisms can ensure that all DDL statements are transactional, and how can PostgreSQL guarantee that rollback of a transaction fully restores the system catalog after a schema change? Is there a version‑agnostic approach to enforce safe rollback for both transactional and non‑transactional DDL?
37275 reputation · 22 Feb 2020, 10:32 UTC
PostgreSQL cannot make all DDL transactional. Most schema commands—CREATE TABLE, ALTER TABLE, DROP TABLE, and similar—are transactional: their catalog changes are written to WAL and undone automatically when the transaction aborts. A smaller set—CREATE INDEX CONCURRENTLY, CREATE DATABASE, DROP DATABASE, VACUUM, CLUSTER, REINDEX CONCURRENTLY, and some older ALTER TABLE variants—cannot run inside a transaction block and cannot be rolled back by PostgreSQL. No built-in setting forces non-transactional DDL into a transaction.
The premise that DDL is "auto-committed" is usually a matter of scope: each statement runs in its own implicit transaction unless you explicitly issue BEGIN, so DDL appears to persist only because no transaction block was started.
Confirmed: transactional DDL rolls back because catalog tables such as pg_class and pg_attribute are ordinary heap tables with MVCC; an aborted transaction leaves its catalog tuples dead, and VACUUM eventually reclaims them. Confirmed: non-transactional commands are rejected inside BEGIN/COMMIT, and commands such as CREATE DATABASE create filesystem objects outside WAL, so PostgreSQL cannot restore them on rollback. Likely explanation for failed rollback expectations: the DDL in question is non-transactional, or it ran outside an explicit transaction block.
BEGIN;
CREATE TABLE example (id integer PRIMARY KEY);
ALTER TABLE example ADD COLUMN note text;
ROLLBACK;
-- Both statements are undone; the table does not exist afterward.
BEGIN;
SELECT pg_advisory_xact_lock(987654321);
-- DDL statements here
COMMIT;
There is no version-agnostic way to enforce rollback for non-transactional DDL inside PostgreSQL. The version-agnostic strategy is operational: wrap transactional DDL in BEGIN/COMMIT, execute non-transactional DDL outside transactions, and handle failures with application-level cleanup. The transactional status of some ALTER TABLE forms changed across major versions, so check your target version's documentation before relying on rollback for a specific statement.
BEGIN;
CREATE TABLE rollback_check (id integer);
ROLLBACK;
SELECT count(*) FROM pg_class WHERE relname = 'rollback_check';
-- Expect 0 rows.
BEGIN;
CREATE INDEX CONCURRENTLY concurrent_check ON rollback_check (id);
-- Expect an error; the command cannot run inside a transaction block.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.