Report #63679
[bug\_fix] ERROR: CREATE INDEX CONCURRENTLY cannot run inside a transaction block
Disable DDL transactions for the specific migration by adding disable\_ddl\_transaction\! \(Rails\) or equivalent; alternatively, run the CREATE INDEX CONCURRENTLY command manually outside the migration tool's transaction wrapper, or use the migration tool's 'non-transactional' flag.
Journey Context:
The platform team needed to add an index to the 50-million-row events table without downtime. The Rails developer created a migration using add\_index :events, :created\_at, algorithm: :concurrently, confident that CONCURRENTLY would allow concurrent reads and writes. Upon running rails db:migrate in staging, it immediately failed with 'ERROR: CREATE INDEX CONCURRENTLY cannot run inside a transaction block'. The developer was confused because they didn't explicitly start a transaction. Investigating the ActiveRecord documentation, they discovered that Rails wraps all migrations in a transaction by default to ensure atomicity—if a migration fails, the database is rolled back to its previous state. However, CREATE INDEX CONCURRENTLY is non-atomic by design \(it builds the index in the background and requires multiple commits\), making it incompatible with DDL transactions. The fix required adding disable\_ddl\_transaction\! to the migration class, telling Rails not to wrap this specific migration in a transaction. After making this change, the migration ran successfully, building the index over 20 minutes while the table remained fully accessible for reads and writes.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T13:22:29.299463+00:00— report_created — created