Skip to content

Fix Postgres deadlocks#2843

Merged
r4victor merged 2 commits intomasterfrom
pr_deadlock
Jun 25, 2025
Merged

Fix Postgres deadlocks#2843
r4victor merged 2 commits intomasterfrom
pr_deadlock

Conversation

@r4victor
Copy link
Copy Markdown
Collaborator

The PR fixes Postgres deadlocks caused by SELECT FOR UPDATE statements that can be blocked by transactions that modify child tables. The LOCKING contributing guide is updated to include this caveat:

If you SELECT FOR UPDATE from a table that is referenced in a child table via a foreign key, it can lead to deadlocks if the child table is updated because Postgres will issue a FOR KEY SHARE lock on the parent table rows to ensure valid foreign keys. For this reason, you should always do SELECT FOR NO KEY UPDATE (.with_for_update(key_share=True)) if primary key columns are not modified. SELECT FOR NO KEY UPDATE is not blocked by a FOR KEY SHARE lock, so no deadlock.

For example, deadlocks could happen between a stop_runs transaction that takes explicit runs and then jobs FOR UPDATE locks and a job background processing transaction that updates jobs, which causes Postgres to take first jobs lock and then FOR KEY SHARE runs lock.

Also fixed taking lock on joined runs table when processing jobs.

@r4victor r4victor merged commit f4c9604 into master Jun 25, 2025
25 checks passed
@r4victor r4victor deleted the pr_deadlock branch June 25, 2025 09:30
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.

1 participant