---
title: "When upserts don’t update but still write: Debugging Postgres performance at scale"
description: "A Postgres `INSERT ... ON CONFLICT DO UPDATE ... WHERE` upsert still locks the conflicting row even when the WHERE clause blocks the update."
url: "https://swe.quest/articles/when-upserts-don-t-update-but-still-write-debugging-postgres-performance-at-cd64ef72ac9ca8ec"
canonical: "https://www.datadoghq.com/blog/engineering/debugging-postgres-performance/"
source: "Datadog Engineering"
published: "2026-03-23"
difficulty: "medium"
kind: "Case study"
reading_minutes: 11
tags: ["postgres","performance","storage-engines","debugging","sql"]
---

# When upserts don’t update but still write: Debugging Postgres performance at scale

A post on [Datadog Engineering](https://swe.quest/sources/datadog.md), published 2026-03-23, 11 min read, Case study. Original: [https://www.datadoghq.com/blog/engineering/debugging-postgres-performance/](https://www.datadoghq.com/blog/engineering/debugging-postgres-performance/)

## Summary

A Postgres \`INSERT ... ON CONFLICT DO UPDATE ... WHERE\` upsert still locks the conflicting row even when the WHERE clause blocks the update. That lock assigns a transaction ID and writes a Heap LOCK record plus a COMMIT to the WAL, forcing a WAL sync per query. Datadog's host last-seen tracker hit this: at 500 upserts/s it doubled write IOPS against a gp3 disk limit of roughly 1,000 fsyncs/s, which made the planned 25,000/s impossible. Using pg\_walinspect and an lldb backtrace to find the cause, the team switched to a CTE that does \`INSERT ... ON CONFLICT DO NOTHING\` followed by a conditional UPDATE, accepting a minor race condition in exchange for zero WAL writes on no-op calls and HOT updates otherwise.

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)
- [Performance engineering](https://swe.quest/tags/performance.md) (Reliability & operations)
- [Storage engines](https://swe.quest/tags/storage-engines.md) (Data & databases)
- [Debugging](https://swe.quest/tags/debugging.md) (Engineering practices)
- [SQL](https://swe.quest/tags/sql.md) (Languages)

## Prerequisites

- PostgreSQL MVCC ([easy posts on PostgreSQL](https://swe.quest/tags/postgres.md?difficulty=easy))
- Write-ahead logging ([easy posts on Observability](https://swe.quest/tags/observability.md?difficulty=easy))
- SQL upserts ([easy posts on SQL](https://swe.quest/tags/sql.md?difficulty=easy))

## Keywords

PostgreSQL, pg\_walinspect, ON CONFLICT DO UPDATE, Heap-Only Tuples, WAL, pg\_test\_fsync, Datadog

## Related posts

- [Debugging deadlocks in Postgres](https://swe.quest/articles/debugging-deadlocks-in-postgres-e07d42201c9784eb.md): incident.io · 2025-02-04 · Medium · 6 min read · PostgreSQL, Concurrency, Debugging, Consistency models
  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.
- [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.
- [You can’t UPDATE what you can’t find: ClickHouse vs PostgreSQL](https://swe.quest/articles/you-can-t-update-what-you-can-t-find-clickhouse-vs-postgresql-6396a03839ca899c.md): ClickHouse · 2025-08-13 · Medium · 15 min read · Analytics & OLAP, PostgreSQL, Performance engineering, Databases, Storage engines
  ClickHouse 25.7's SQL-standard UPDATEs were benchmarked against PostgreSQL 17.5 on identical hardware using the 600M-row TPC-H lineitem table, with cold and hot runs.
- [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.
- [Read your writes: WAIT FOR in PostgreSQL 19](https://swe.quest/articles/read-your-writes-wait-for-in-postgresql-19-ead8d2a645bd23c3.md): ClickHouse · 2026-08-25 · Medium · 6 min read · PostgreSQL, Replication, Consistency models
  PostgreSQL 19 adds a top-level \`WAIT FOR LSN\` command that blocks a session until a standby has written, flushed or replayed WAL up to a given LSN, giving read-your-writes consistency on asynchronous replicas without the per-commit cost of \`synchronous\_commit = remote\_apply\`.

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)
