feat: Make ix_tasks_metadata_gin index creation concurrent#234
Merged
Conversation
1129f0e to
8bfb99f
Compare
8bfb99f to
c8392a3
Compare
declan-scale
approved these changes
May 13, 2026
Comment on lines
19
to
27
| def upgrade() -> None: | ||
| op.execute( | ||
| "CREATE INDEX IF NOT EXISTS ix_tasks_metadata_gin " | ||
| "ON tasks USING GIN (task_metadata jsonb_path_ops)" | ||
| ) | ||
| with op.get_context().autocommit_block(): | ||
| op.execute( | ||
| "CREATE INDEX CONCURRENTLY IF NOT EXISTS ix_tasks_metadata_gin " | ||
| "ON tasks USING GIN (task_metadata jsonb_path_ops)" | ||
| ) | ||
|
|
||
|
|
||
| def downgrade() -> None: |
There was a problem hiding this comment.
PostgreSQL forbids
CREATE INDEX CONCURRENTLY (and DROP INDEX CONCURRENTLY) inside a transaction block. env.py sets transaction_per_migration=True, which wraps every migration in its own BEGIN/COMMIT, so calling op.execute("CREATE INDEX CONCURRENTLY …") directly will immediately raise ERROR: CREATE INDEX CONCURRENTLY cannot run inside a transaction block and abort the migration. The newer sibling migration a9959ebcbe98 (which also has down_revision = 'e9c4ff9e6542') correctly wraps its CONCURRENTLY statements in with op.get_context().autocommit_block(): — the same pattern documented in env.py's own inline comment. Both upgrade() and downgrade() need the same fix here.
Suggested change
| def upgrade() -> None: | |
| op.execute( | |
| "CREATE INDEX IF NOT EXISTS ix_tasks_metadata_gin " | |
| "ON tasks USING GIN (task_metadata jsonb_path_ops)" | |
| ) | |
| with op.get_context().autocommit_block(): | |
| op.execute( | |
| "CREATE INDEX CONCURRENTLY IF NOT EXISTS ix_tasks_metadata_gin " | |
| "ON tasks USING GIN (task_metadata jsonb_path_ops)" | |
| ) | |
| def downgrade() -> None: | |
| def upgrade() -> None: | |
| with op.get_context().autocommit_block(): | |
| op.execute( | |
| "CREATE INDEX CONCURRENTLY IF NOT EXISTS ix_tasks_metadata_gin " | |
| "ON tasks USING GIN (task_metadata jsonb_path_ops)" | |
| ) | |
| def downgrade() -> None: | |
| with op.get_context().autocommit_block(): | |
| op.execute("DROP INDEX CONCURRENTLY IF EXISTS ix_tasks_metadata_gin") |
Prompt To Fix With AI
This is a comment left during a code review.
Path: agentex/database/migrations/alembic/versions/2026_05_04_1111_add_tasks_metadata_gin_index_e9c4ff9e6542.py
Line: 19-27
Comment:
PostgreSQL forbids `CREATE INDEX CONCURRENTLY` (and `DROP INDEX CONCURRENTLY`) inside a transaction block. `env.py` sets `transaction_per_migration=True`, which wraps every migration in its own `BEGIN`/`COMMIT`, so calling `op.execute("CREATE INDEX CONCURRENTLY …")` directly will immediately raise `ERROR: CREATE INDEX CONCURRENTLY cannot run inside a transaction block` and abort the migration. The newer sibling migration `a9959ebcbe98` (which also has `down_revision = 'e9c4ff9e6542'`) correctly wraps its CONCURRENTLY statements in `with op.get_context().autocommit_block():` — the same pattern documented in `env.py`'s own inline comment. Both `upgrade()` and `downgrade()` need the same fix here.
```suggestion
def upgrade() -> None:
with op.get_context().autocommit_block():
op.execute(
"CREATE INDEX CONCURRENTLY IF NOT EXISTS ix_tasks_metadata_gin "
"ON tasks USING GIN (task_metadata jsonb_path_ops)"
)
def downgrade() -> None:
with op.get_context().autocommit_block():
op.execute("DROP INDEX CONCURRENTLY IF EXISTS ix_tasks_metadata_gin")
```
How can I resolve this? If you propose a fix, please make it concise.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Greptile Summary
CREATE INDEX CONCURRENTLYandDROP INDEX CONCURRENTLYinop.get_context().autocommit_block()for bothupgrade()anddowngrade(), correctly matching the pattern used in the sibling migrationa9959ebcbe98and satisfying PostgreSQL's requirement thatCONCURRENTLYcannot run inside a transaction block.Confidence Score: 5/5
Safe to merge — the fix correctly implements autocommit_block for both upgrade and downgrade, matching the established pattern in the codebase.
No P0 or P1 issues found. The PR fully addresses the previous review comment by adding autocommit_block() wrapping and CONCURRENTLY to both operations, exactly matching the pattern in sibling migration a9959ebcbe98.
No files require special attention.
Important Files Changed
Flowchart
%%{init: {'theme': 'neutral'}}%% flowchart TD A[Alembic runs migration] --> B{transaction_per_migration=True} B --> C[BEGIN transaction] C --> D[autocommit_block context manager] D --> E[Connection set to AUTOCOMMIT mode] E --> F["CREATE INDEX CONCURRENTLY IF NOT EXISTS ix_tasks_metadata_gin\nON tasks USING GIN (task_metadata jsonb_path_ops)"] F --> G[Index built without table lock] G --> H[Connection restored to transactional mode] H --> I[COMMIT outer transaction] style F fill:#d4edda,stroke:#28a745 style G fill:#d4edda,stroke:#28a745Reviews (2): Last reviewed commit: "feat: Make ix_tasks_metadata_gin index c..." | Re-trigger Greptile