---
title: "Debugging deadlocks in Postgres"
description: "Postgres deadlocks come from transactions acquiring locks in inconsistent orders, and the main fix is to restructure transactions so they always lock resources in the same sequence."
url: "https://swe.quest/articles/debugging-deadlocks-in-postgres-e07d42201c9784eb"
canonical: "https://incident.io/blog/debugging-deadlocks-in-postgres"
source: "incident.io"
published: "2025-02-04"
difficulty: "medium"
kind: "Case study"
reading_minutes: 6
tags: ["postgres","concurrency","debugging","consistency"]
---

# Debugging deadlocks in Postgres

A post on [incident.io](https://swe.quest/sources/incident-io.md), published 2025-02-04, 6 min read, Case study. Original: [https://incident.io/blog/debugging-deadlocks-in-postgres](https://incident.io/blog/debugging-deadlocks-in-postgres)

## Summary

Postgres deadlocks come from transactions acquiring locks in inconsistent orders, and the main fix is to restructure transactions so they always lock resources in the same sequence. Explicit FOR UPDATE or advisory locks, narrower FOR NO KEY UPDATE locks, and jittered retries also work, but they all reduce parallelism. In a real case at incident.io, two seemingly identical bulk upserts in a Catalog sync deadlocked. Postgres acquires row locks one at a time during a multi-row INSERT ... ON CONFLICT, and the rows were randomly ordered after passing through a Go map, so sorting the rows before upserting eliminated the deadlock.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [PostgreSQL](https://swe.quest/tags/postgres.md) (Data & databases)
- [Concurrency](https://swe.quest/tags/concurrency.md) (Compilers & runtimes)
- [Debugging](https://swe.quest/tags/debugging.md) (Engineering practices)
- [Consistency models](https://swe.quest/tags/consistency.md) (Distributed systems)

## Prerequisites

- SQL transactions ([easy posts on SQL](https://swe.quest/tags/sql.md?difficulty=easy))
- Postgres row locking basics ([easy posts on PostgreSQL](https://swe.quest/tags/postgres.md?difficulty=easy))

## Keywords

PostgreSQL, deadlock, upsert, INSERT ON CONFLICT, FOR UPDATE, advisory locks, Google Cloud Logging

## Related posts

- [When upserts don’t update but still write: Debugging Postgres performance at scale](https://swe.quest/articles/when-upserts-don-t-update-but-still-write-debugging-postgres-performance-at-cd64ef72ac9ca8ec.md): Datadog Engineering · 2026-03-23 · Medium · 11 min read · PostgreSQL, Performance engineering, Storage engines, Debugging, SQL
  A Postgres \`INSERT ... ON CONFLICT DO UPDATE ... WHERE\` upsert still locks the conflicting row even when the WHERE clause blocks the update.
- [Mitigating Deadlocks in high concurrency environments](https://swe.quest/articles/mitigating-deadlocks-in-high-concurrency-environments-f958e9ca77901412.md): Shopify Engineering · 2024-08-14 · Medium · 3 min read · MySQL, Concurrency, Storage engines, Databases
  Concurrent batch upserts into an InnoDB table keyed by a sequential id can deadlock even when processes touch different rows, because the gap locks taken on unique-key collisions span neighbouring index entries that other processes also need.
- [PostgreSQL Stories: A simple query with a big problem](https://swe.quest/articles/postgresql-stories-a-simple-query-with-a-big-problem-6dd72fff3c9a1d47.md): Render · 2024-04-03 · Medium · 10 min read · PostgreSQL, Debugging, Incidents & postmortems, Schema migrations
  A seemingly simple indexed join with LIMIT 1 ran for over four hours on Render's staging PostgreSQL, holding a lock that blocked CREATE OR REPLACE VIEW migrations and stalled deploys.
- [How savepoints quietly throttled our Postgres queue](https://swe.quest/articles/how-savepoints-quietly-throttled-our-postgres-queue-a32737dd90a62c34.md): incident.io · 2026-10-06 · Hard · 15 min read · PostgreSQL, Performance engineering, Storage engines, Concurrency, Scaling
  Using a SAVEPOINT per tick in a Postgres-backed job queue made every locked row carry a MultiXact ID in xmax.
- [Navigating PostgreSQL Locks with Neon and Atlas](https://swe.quest/articles/navigating-postgresql-locks-with-neon-and-atlas-61f1baeebb69a86a.md): Neon · 2025-03-14 · Medium · 11 min read · Schema migrations, PostgreSQL, CI/CD, Static analysis
  Adding a UNIQUE constraint or setting a column NOT NULL in Postgres triggers a full table scan under an ACCESS EXCLUSIVE lock, which blocks all reads and writes and can take down an API on large tables.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
