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

Allow collection.Version() calls during a migration #89

Merged
merged 1 commit into from
Jul 19, 2020
Merged

Allow collection.Version() calls during a migration #89

merged 1 commit into from
Jul 19, 2020

Conversation

maximerety
Copy link
Contributor

As of today, if you try to call collection.Version(db) when a migration is running, it will wait for the LOCK TABLE gopg_migrations lock to be released before returning.

If you have long-running migrations (e.g. CREATE INDEX CONCURRENTLY ... that is running in background) and other pieces of code that rely on getting the current database version, this becomes a problem.

I stumbled upon this issue when trying to start services that checked the current database version at startup while a long-running migration was executed.

To allow this use case, we can update the lock command to LOCK TABLE ? IN EXCLUSIVE MODE which allows concurrent SELECT commands to be run on the migrations table, while still forbidding other operations.

From the documentation:

The EXCLUSIVE mode allows only concurrent ACCESS SHARE locks, i.e., only reads from the table can proceed in parallel with a transaction holding this lock mode.

See:

I've targeted the v7 branch for this patch, since v8 is not released yet. I can update this pull request if this is not the best target.

Thanks,

The EXCLUSIVE mode allows only concurrent ACCESS SHARE locks,
i.e., only reads from the table can proceed in parallel with
a transaction holding this lock mode.

See:
https://www.postgresql.org/docs/12/sql-lock.html
https://www.postgresql.org/docs/12/explicit-locking.html#LOCKING-TABLES
@vmihailenco vmihailenco merged commit 8e98a45 into go-pg:v7 Jul 19, 2020
@vmihailenco
Copy link
Member

Looks good - thanks.

@vmihailenco
Copy link
Member

Released as v7.1.11

@maximerety maximerety deleted the max/allow-select-version-during-migration branch July 20, 2020 07:31
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 this pull request may close these issues.

2 participants