Planet PostgreSQL·· 6d ago
Hollis Varden: Upserts, Partitions, Triggers and String Matching on PostgreSQL 18
Hollis Varden: Upserts, partitions, triggers and string matching on PostgreSQL 18
Brief · Based on the source excerpt
Hollis Varden ran 21 small benchmarks on PostgreSQL 18. 6 built from source with default settings and C collation. On a 1,000,000-row table with 100,000 incoming rows (50,000 existing), `INSERT .
Why It Matters
Filtering no-change upserts with IS DISTINCT FROM sharply reduces WAL volume, though the original still recorded 4.6 MB of WAL, so the optimization is not write-free.
Technical Breakdown · Distilled from the original source
Setup and Method
- PostgreSQL 18.6 built from source with default settings (
work_mem4MB,shared_buffers128MB), C collation, on a MacBook Air M5 with 24 GB RAM. - Query times come from
EXPLAIN (ANALYZE, BUFFERS, TIMING OFF)execution time, run several times per session with the first run discarded and the median of the rest (3 unless stated); inserts, upserts andMERGEran inside a rolled-back transaction so each run starts from the same table. - Wall-clock seconds are from psql and include connecting (0.005 s for
SELECT 1); buffers are from the top plan node; parallel workers were off unless stated.
Upsert Comparison
- On a 1,000,000-row int-primary-key table with 100,000 incoming rows of which 50,000 already exist:
INSERT ... ON CONFLICT DO NOTHING71.3 ms,INSERT ... WHERE NOT EXISTS (...)82.6 ms,INSERT ... ON CONFLICT DO UPDATE153.8 ms,MERGE(update matched, insert the rest) 165.45 ms. - The author notes the
NOT EXISTSform differs under concurrency: two sessions can both see "not there" and one fails on the key; only a single session was tested.
WAL Cost of No-Change Upserts
- Sending 50,000 rows that all exist with identical values, with a
CHECKPOINTbefore each committed run:ON CONFLICT DO UPDATE SET v = EXCLUDED.vwrote 16.2 MB of WAL in 0.107 s; addingWHERE x_up.v IS DISTINCT FROM EXCLUDED.vgave 4.6 MB in 0.040 s. - Without the
WHERE, every row gets a new version even when nothing changed; with it, nothing is updated yet 4.6 MB of WAL was still written, and the author did not check what those records were.
Deletion
- A 2,000,000-row table with the first 1,000,000 deleted, comparing one statement against 100 statements of 10,000 rows each committing separately, three runs each on a freshly loaded table, recording wall clock and WAL.
Source: Planet PostgreSQL· postgr.es
PG Radar summarizes verified changes and impact. Read the original post for full technical details and code samples.