Skip to content

Postgres: use pg_try_advisory_lock instead of pg_advisory_lock - #962

Open
AkuSilvenius wants to merge 19 commits into
golang-migrate:masterfrom
AkuSilvenius:960_add_try_advisory_lock
Open

AkuSilvenius wants to merge 19 commits into
golang-migrate:masterfrom
AkuSilvenius:960_add_try_advisory_lock

Conversation

@AkuSilvenius

@AkuSilvenius AkuSilvenius commented Jul 26, 2023 •

Copy link
Copy Markdown

Issue

#960

Changes

  • Use pg_try_advisory_lock instead of pg_advisory_lock in Postgres Lock to fix the expected behavior of waiting for acquiring lock indefinitely
  • Add configurable exponential backoff for retrying when lock is not available.
  • Add config parameter x-lock-retry-max-interval to configure the max interval to be used for exponential backoff when retrying Lock

@coveralls

coveralls commented Jan 23, 2024 •

Copy link
Copy Markdown

Coverage Status

coverage: 56.463% (+0.1%) from 56.335%
when pulling 21469a7 on AkuSilvenius:960_add_try_advisory_lock
into 0c456c4 on golang-migrate:master.

@AkuSilvenius
AkuSilvenius marked this pull request as ready for review January 23, 2024 10:39

@dhui dhui left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

left a comment in case we decide to go this route

Comment thread database/postgres/postgres.go Outdated
break
}

time.Sleep(100 * time.Millisecond)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the backoff library with an exponential backoff + jitter as the default. Note, this may cause other nodes/hosts to take longer to deploy due to the longer wait period so the backoff should be configurable.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dhui thanks for the feedback, I've added a configurable exponential retry.

this may cause other nodes/hosts to take longer to deploy due to the longer wait period

I've added defaults and kept them quite small to address the above

@AkuSilvenius

Copy link
Copy Markdown
Author

unfortunately the tests keep failing because device is running out of space

@eelco

eelco commented Apr 18, 2024

Copy link
Copy Markdown

I assume the tests were temporarily broken, so this might work after a rebase? I’m interested in this fix landing 😊

@AkuSilvenius

Copy link
Copy Markdown
Author

I assume the tests were temporarily broken, so this might work after a rebase? I’m interested in this fix landing 😊

@eelco thanks for noting, looks like #1072 fixed the tests and pipeline here in the PR is working and passing now - unfortunately this PR got stuck in sleep mode for quite a while due to the unstable pipeline, I hope we can merge this soon :)

@simonjpartridge

Copy link
Copy Markdown

I assume the tests were temporarily broken, so this might work after a rebase? I’m interested in this fix landing 😊

@eelco thanks for noting, looks like #1072 fixed the tests and pipeline here in the PR is working and passing now - unfortunately this PR got stuck in sleep mode for quite a while due to the unstable pipeline, I hope we can merge this soon :)

Is there anything we can do to help get this PR unblocked and merged?

@AkuSilvenius

Copy link
Copy Markdown
Author

@simonjpartridge all tests are passing now, so just approvals are needed I guess :)

@AkuSilvenius

Copy link
Copy Markdown
Author

anyone of the recent participants here in the PR willing to give it a review as the pipeline is stable now, and approve if it looks good so we get this one merged and the issue closed? @dhui @eelco @simonjpartridge

@simonjpartridge simonjpartridge left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good overall, one comment that should be actioned IMO around setting of the max retry interval

Comment on lines +230 to 232
if maxRetryInterval > DefaultLockInitialRetryInterval {
lockConfig.MaxRetryInterval = maxRetryInterval
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This if statement means a user can't set the max retry interval to less than the default. I can't see a good reason to limit it in this way so why not do this?

Suggested change
if maxRetryInterval > DefaultLockInitialRetryInterval {
lockConfig.MaxRetryInterval = maxRetryInterval
}
if maxRetryInterval >= 0 {
lockConfig.MaxRetryInterval = maxRetryInterval
}

@AkuSilvenius AkuSilvenius Jul 8, 2024 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the suggestion - the retry strategy here is to use exponential backoff, meaning if we end up retrying, the later retries should wait for longer than the previous ones. If we'd allow the user to set maxRetryInterval to less than InitialRetryInterval then the retry interval wouldn't increase which is against the idea of exponential backoff

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

however your comment raises a valid point that the user might not be aware of this initial retry interval and could try setting it to less than that without knowing it would be ignored - I've added a note about this to the readme be32ada

Comment thread database/postgres/README.md Outdated
Comment thread database/postgres/postgres.go Outdated
})
}

func testPostgresConcurrentMigrations(t *testing.T) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ran these tests against master and can confirm they fail 🎉. This change makes the tests pass

@AkuSilvenius

Copy link
Copy Markdown
Author

Looks like there has been some updates on the pipeline as the tests run without issues now. Maybe we could finally get this one merged? 😁 pinging reviewers @dhui @eelco @simonjpartridge

@eelco

eelco commented Dec 3, 2024

Copy link
Copy Markdown

@AkuSilvenius I would approve but I don’t have write access in this repo, so it would not matter…

@simonjpartridge simonjpartridge left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice work! Unfortunately I don't think I have write access either. @dhui is the reviewer we need

@amirsalarsafaei

amirsalarsafaei commented Sep 17, 2026 •

Copy link
Copy Markdown

Hi I believe this to be a serious issue in the library. Our prod got affected by it. Seems the like the pr is stale is there anything I can do to move it forward?
@Fontinalis

@amirsalarsafaei

Copy link
Copy Markdown

@AkuSilvenius any chance you could rebase this on master? It's pretty far behind now and has conflicts. If you don't have time for it I can open a new PR with your commits rebased (you'd stay the author) and link it back here.

@Fontinalis pinging again in case you missed my last mention. We hit #960 in production and this PR fixes it. dhui's backoff feedback was addressed and simonjpartridge already approved, so it should be pretty close. Happy to help with whatever is needed to get it merged.

amirsalarsafaei pushed a commit to amirsalarsafaei/migrate that referenced this pull request Oct 3, 2026
pg_advisory_lock blocks until the lock is free, so concurrent migrations
can hang forever (golang-migrate#960). Poll pg_try_advisory_lock instead and retry with
an exponential backoff. The max retry interval can be set with
x-lock-retry-max-interval.

Rebased from golang-migrate#962.

Fixes golang-migrate#960

This branch has not been deployed

No deployments
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.

6 participants