When upserts don’t update but still write: Debugging Postgres performance at scale
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.
Related posts
- Debugging deadlocks in Postgres
incident.io mediumAdeptPostgres 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.
- How savepoints quietly throttled our Postgres queue
incident.io hardMasterUsing a SAVEPOINT per tick in a Postgres-backed job queue made every locked row carry a MultiXact ID in xmax. FOR UPDATE SKIP LOCKED scans then contended on the LWLock-guarded pg_multixact cache, so adding workers made acquisition slower. incident.io fixed it by moving the queue to a slim escalation_jobs table, replacing subtransactions with a claimed_until lease (one transaction per tick), keeping claimed_until unindexed with fillfactor 85 to allow HOT updates, and running one dispatcher goroutine per pod that feeds workers over a channel. Together these raised peak escalation capacity 3.5x, at the cost of up to 10 seconds of delay if a pod dies mid-claim.
- You can’t UPDATE what you can’t find: ClickHouse vs PostgreSQL
ClickHouse mediumAdept
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. ClickHouse wins bulk updates by up to 4,000× because most bulk WHERE clauses can't use the primary key index. That forces full scans, where PostgreSQL takes 380–390 s and ClickHouse's columnar, parallel scan takes 0.09–5.23 s. Point updates are roughly at parity and cached point updates stay competitive. The comparison isn't apples-to-apples: PostgreSQL wraps every statement in a transaction and fsyncs WAL at commit by default, while ClickHouse skips per-statement transactions and defaults to fsync_after_insert=false.
- PostgreSQL Stories: A simple query with a big problem
Render mediumAdept
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. The team traced it by watching oldest-transaction age, querying pg_stat_activity for long-running active queries, and using pg_blocking_pids() to map which backends blocked which migrations. The culprit was an engineer's ad-hoc psql query: pressing Ctrl+C in psql stopped reading results but left the server-side query running. Killing the backend fixed it immediately. Key lessons are to keep transactions short, because they block locks, autovacuum and risk XID wraparound, and to set application_name and inspect usename so queries can be traced to their source.
- Read your writes: WAIT FOR in PostgreSQL 19
ClickHouse mediumAdept
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`. The pattern is to capture `pg_current_wal_insert_lsn()` after a write on the primary, then run `WAIT FOR` on the standby before reading. Options include MODE (standby_replay, standby_flush, standby_write, primary_flush), TIMEOUT and NO_THROW. It must run as a snapshot-free utility command outside functions and above READ COMMITTED, because a held snapshot can stall WAL replay and self-deadlock. The feature took about 10 years and three reverts to land. Timeline changes after promotion are not tracked, and connection poolers and proxies are natural places to inject it transparently.