What this thread does
Do first: set migrationsTransactionMode: "each" and give the concurrent-index migration transaction = false before running it.
Short answer
As of November 2026, this card targets TypeORM 1.1.2 with PostgreSQL 18. CREATE INDEX CONCURRENTLY cannot run inside a transaction block means an active wrapper still surrounds the statement (reported error, current restriction). The per-migration transaction flag can only be used with each or none.
Choose / don't choose
each: choose to retain a separate transaction around ordinary migrations and opt out only for the concurrent index. Don't expect the whole migration set to roll back atomically.none: choose when the set is intentionally nontransactional; individual migrations may opt back in withtransaction = true. Don't remove transaction protection from every migration just to accommodate one index (interface defaults).
Not for: Drizzle migration generation; see drizzle-kit-push-or-generate-migrate-which-one-for-my-project.
Settings that matter
| Setting or flag | Value / default | Applies to | Source |
|---|---|---|---|
migrationsTransactionMode | "each"; default "all" wraps the whole run | TypeORM current docs | setup |
transaction | false on the concurrent-index migration; override allowed only in each or none | TypeORM 1.1.2 | interface |
--transaction | each for this run; inspect CLI overrides, especially all | TypeORM CLI | modes, 1.1.2 CLI source |
CREATE INDEX CONCURRENTLY | Standalone statement, outside any explicit transaction | PostgreSQL 18 | indexing |
DROP INDEX CONCURRENTLY | Standalone cleanup of the named failed index; one index, no CASCADE | PostgreSQL 18 | drop restrictions |
The migration body can use the documented pattern:
transaction = false
async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`CREATE INDEX CONCURRENTLY post_names_idx ON post(name)`
)
}
Order of operations
1. Configure each, declare transaction = false, and inspect the actual runner/CLI invocation. The all branch sets options.transaction = "all"; remove manual transaction wrappers too (CLI source, modes). 2. Execute the index command through the migration's QueryRunner. Do not call a transaction-starting wrapper inside this migration. With each, the migration transaction flag defaults to true (interface). 3. If the build fails, inspect the specific target's catalog state:
SELECT indexrelid::regclass, indisvalid
FROM pg_index WHERE indrelid = 'public.post'::regclass;
indisvalid = false identifies an index unsafe for queries (catalog). Check that no builder still owns it before cleanup. 4. Remove only the failed migration's invalid index with standalone DROP INDEX CONCURRENTLY public.post_names_idx, repair the original cause, then retry the migration. DROP INDEX CONCURRENTLY cannot remove an index on a partitioned table (DROP INDEX). An invalid unique index can still enforce uniqueness; account for that before retrying (CREATE INDEX). 5. Confirm a valid target index and consistent migration history before dependent schema changes. Do not use IF NOT EXISTS to mistake an existing invalid index for completion (indexing).
Known failures and fixes
Diagnostic: CREATE INDEX CONCURRENTLY cannot run inside a transaction block
| Error (short quote) | Cause | Fix | Source |
|---|---|---|---|
CREATE INDEX CONCURRENTLY cannot run inside a transaction block | The statement is inside a transaction block | Use each plus migration transaction = false; remove the remaining wrapper | error report, current rule, modes |
INVALID | A scan problem can leave an invalid index | Inspect indisvalid, drop the identified failed index, repair cause, retry | indexing, catalog |
pedrolcn's November 18, 2020 request describes why disabling every migration transaction was inadequate. The current interface provides the per-migration choice.
Missing
- The linked modes example places these members in a migration implementing
MigrationInterface. Keep this migration narrowly scoped because its earlier successful statements cannot be rolled back as a unit. (The linked example does not explicitly establish the rollback-unit assertion). - No rollback of already committed nontransactional DDL follows from migration failure. Recovery must reconcile schema state with migration history.
Checked
2026-11-12; TypeORM release, setup/mode docs, tagged interface/CLI source, PostgreSQL release/index/drop/catalog references, dated pedrolcn request and veem v error report read. No migrations executed.
Started from a post by @TypeORM, packaged by the editor. Original post