Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support creating multiple indexes concurrently in a single migration postgresql #284

Closed
julieqiu opened this issue Sep 12, 2019 · 11 comments · Fixed by #495
Closed

Support creating multiple indexes concurrently in a single migration postgresql #284

julieqiu opened this issue Sep 12, 2019 · 11 comments · Fixed by #495

Comments

@julieqiu
Copy link

Describe the Bug
I'd like to add multiple indexes to my postgresql schema in a single migration file.

Steps to Reproduce

  1. My migrations look like '...'
CREATE INDEX CONCURRENTLY idx_my_table_col1 ON my_table(col1);
CREATE INDEX CONCURRENTLY idx_my_table_col2 ON my_table(col2);
  1. I ran migrate with the following options '....'
migrate -source file:migrations -database "postgres://localhost:5432/my-database?sslmode=disable" up
  1. See error
 (details: pq: CREATE INDEX CONCURRENTLY cannot run inside a transaction block)

Expected Behavior
I expected the migration to succeed and add both indexes. This migration does succeed if I only try to create one index at a time and have two sequential migration files with the following contents:

CREATE INDEX CONCURRENTLY idx_my_table_col1 ON my_table(col1);
```
```
CREATE INDEX CONCURRENTLY idx_my_table_col2 ON my_table(col2);
```

**Migrate Version**
v4.6.2

**Loaded Source Drivers**
Source drivers: github, github-ee, gitlab, go-bindata, godoc-vfs, gcs, file, s3

**Loaded Database Drivers**
Database drivers: cockroachdb, crdb-postgres, postgres, redshift, postgresql, spanner, sqlserver, cassandra, clickhouse, cockroach, mongodb, mysql, stub


**Go Version**
go version go1.13 darwin/amd64
@roopakv
Copy link
Contributor

roopakv commented Sep 19, 2019

So psql is able to do what you expect above since they have autocommit turned on. You can repro that this wont work with psql by setting autocommit off (/set AUTOCOMMIT off)

To make this ^ work we would need to figure out how to turn on autocommit.

@psuresh309
Copy link
Contributor

Note this happens with CREATE INDEX CONCURRENTLY and any other sql command and not just another CREATE INDEX CONCURRENTLY.

@dhui
Copy link
Member

dhui commented Sep 14, 2020

Closing as this is not a restriction of migrate but instead what postgres allows in a single execute statement. Another otion would be to add support for multiple statements using multistmt

@dhui dhui closed this as completed Sep 14, 2020
@frederikhors
Copy link

Is there any news on this? May I ask you how did you solve, @julieqiu?

@frederikhors
Copy link

@dhui I'm new to golang-migrate, can you please explain what to do? I'm in the same situation as @julieqiu...

@dhui
Copy link
Member

dhui commented Dec 7, 2020

For now, I'd break up your statements into separate migration files until the postgres driver supports multiple statements in a single migration file.

@frederikhors
Copy link

If I have 30 tables and I need many indexes on each of them how many files will i need? 30 * 3~? Really?

@julieqiu
Copy link
Author

julieqiu commented Dec 7, 2020

For now, I'd break up your statements into separate migration files until the postgres driver supports multiple statements in a single migration file.

This is what I ended up doing. Alternatively, I would just run CREATE INDEX without CONCURRENTLY.

@dhui
Copy link
Member

dhui commented Dec 8, 2020

@frederikhors It may be easier to add support for multiple statements than to write 90+ migrations
Feel free to open a PR. For example usage, see the neo4j, cassandra, and clickhouse db drivers.

@frederikhors
Copy link

I would like to be able to do that.

@AnatolyRugalev
Copy link
Contributor

Gonna work on it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants