Datadog
Datadog logoDatadog · Feature trail

Postgres Upsert Performance Optimization

1 post · Jun 2025 – Jun 2026

What this trail covers

Datadog's PostgreSQL high-availability architecture on Kubernetes has been re-architected for failover safety. The system now uses synchronous replication for failover candidates, coordinated by Patroni, to ensure durability and prevent data loss during network partitions or zonal failures. This involves reconfiguring the leader pool to prioritize data consistency over immediate availability when network latency spikes, ensuring that replicas are sufficiently up-to-date before a failover can occur. This post details the debugging of an upsert query that unexpectedly doubled disk writes and quadrupled WAL syncs by writing to WAL even when no data changed, due to row locking. The solution involved creating a dedicated table for `last_ingested` timestamps and optimizing the upsert query to leverage Heap-Only Tuples (HOT) updates by ensuring updates only modify unindexed columns and by setting an appropriate fillfactor, thereby reducing write amplification and WAL activity.

All Datadog trails