postgres: set lock_timeout and attempt to run migration repeatedly until success #378
wkalt
started this conversation in
Feature Ideas
Replies: 2 comments
-
it would probably be reasonable for the annotation to allow the desired timeout value to be specified |
Beta Was this translation helpful? Give feedback.
0 replies
-
note that this option should preclude transaction:false, since migrations run outside a transaction may not be safe to retry on failure. Though, going off memory there may already be a guard in place against multiple statements in a transaction:false migration. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
A common issue when running postgres migrations that require even a short-duration access exclusive lock, is that lock acquisition blocks until any transactions on a table prior to the lock is requested commit. The transaction requesting the access exclusive lock will in turn block any other transactions initialized after it, until it is able to complete. This means that even for migrations that execute very quickly, such as "alter table x add column y int", if there happens to be a long-running select running on the table before the migration is requested, you can still cause downtime.
To demonstrate this, you can open three psql sessions and try this:
session 1:
session 2 (the dbmate migration):
session 3 (a read request made after the migration is requested):
until the first session commits, session 3 and any subsequent requests will block on session 2. Once session 1 commits, session 2 will execute immediately.
One strategy to get around this is, before running session 2, set a short lock timeout:
and instead of running the migration once, run it repeatedly in a loop until it succeeds. This prevents subsequent transactions like session 3 from piling up in the lock queue behind the migration.
It would be useful if dbmate could cause this behavior via an annotation in the migration file, similar to transaction:false.
Beta Was this translation helpful? Give feedback.
All reactions