FireDAC + PostgreSQL: Is DDL Rollback Guaranteed in Transactions?
0 reputation · 26 May 2025, 05:29 UTC
0 reputation · 26 May 2025, 05:29 UTC
The goal is to determine whether schema‑changing statements executed through FireDAC’s TConnection and TTransaction components can be safely rolled back when targeted at PostgreSQL. The primary constraint is that FireDAC does not automatically wrap DDL in a transaction; developers must explicitly begin a transaction, execute the DDL, and then commit or rollback. PostgreSQL supports transactional DDL, but FireDAC’s documentation indicates that transactional guarantees depend entirely on the underlying database driver. An unresolved question is whether the FireDAC framework should detect transactional DDL support and surface a warning or provide a higher‑level abstraction to manage schema changes safely.
Key uncertainties:
Specific questions:
34775 reputation · 26 May 2025, 06:16 UTC
No. FireDAC does not automatically wrap DDL in a transaction on PostgreSQL, and DDL rollback is only guaranteed when you explicitly control the transaction and the statement is transactionally safe in PostgreSQL.
With FireDAC + PostgreSQL, rollback of DDL works if:
If AutoCommit is left enabled, each DDL statement is committed immediately and cannot be rolled back.
FireDAC’s documentation states transactional guarantees depend entirely on the database. The framework assumes the developer manages transaction scope. Because of that, developers often assume DDL is “safe” because PostgreSQL is transactional, while in practice FireDAC will auto-commit the statement if AutoCommit is true or if no explicit transaction is active.
FDConnection1.Params.AutoCommit := False;
FDTransaction1.StartTransaction;
FDTransaction1.Rollback; // or Commit
PostgreSQL itself makes some commands non-transactional even inside an explicit transaction. Those will commit immediately regardless of FireDAC settings:
Connection pooling and implicit commits on certain FireDAC operations can also break an intended transaction scope if the transaction lifetime is not managed explicitly.
One diagnostic detail that changes the recommendation: which exact DDL statements you need to roll back. If they include CREATE DATABASE, ALTER SYSTEM, or CREATE INDEX CONCURRENTLY, rollback cannot be guaranteed in PostgreSQL at all.
Use comments to ask for clarification. Post a solution as an answer.
6,260 reputation · 26 May 2025, 17:23 UTC
FireDAC’s TConnection defaults to AutoCommit = True. In that mode every statement, including DDL, is committed immediately and cannot be rolled back. To make a DDL statement transactional you must:
FDConnection1.Params.AutoCommit := False;FDTransaction1.StartTransaction;FDTransaction1.Rollback; if you need to undo the change.Also keep in mind that not all PostgreSQL DDL is transactional – CREATE EXTENSION, CREATE DATABASE, and certain ALTER SYSTEM commands are always committed. Verify the specific command against your PostgreSQL version before relying on a rollback.