Ingest performance

How the PostgreSQL write path moved from ~312 to ~33,595 rows per second without changing the indexer's correctness guarantees.

On this page

Benchmark

~108× faster ingest

100k rows · 3 runs · Bun · PostgreSQL 18.6 local

108×naive → chunked
Naive INSERT312 rows/s
COPY, row-by-row8,517 rows/s
COPY, ~1 MB chunks33,595 rows/s

Median throughput; measured locally, so ratios matter more than absolute numbers.

This document is about the data-ingestion pipeline itself — how rows get into PostgreSQL — independent of the blockchain domain. The indexer is simply the workload; the same progression applies to any high-volume ingest.

Summary

Three generations of the ingest path, measured on a 100k-row synthetic dataset with PostgreSQL 18.6 locally:

StageApproachMedian rows/svs naive
1Naive per-row INSERT~3121×
2COPY into staging, row-by-row feed~8,517~27×
3COPY into staging, ~1 MB chunked feed~33,595~108×

Within a representative run: roughly 27× from 1→2, 4× from 2→3, and 108× from 1→3. Absolute numbers move with machine load and database cache state; the ordering and approximate ratios are stable.

Stage 1 — Naive sequential INSERT

For each row, send a parameterized INSERT INTO ... VALUES (...) ON CONFLICT DO NOTHING, all inside one transaction.

The time goes into the protocol round-trip, server parse/plan/execute, heap insertion, index maintenance, WAL writes, and the ON CONFLICT probe. The per-row cost is large, and it gets worse as indexes grow inside the same transaction.

Empirically, a 3,000-row batch reached roughly 1,300–1,700 rows/s, while a 100,000-row batch fell to about 312 rows/s. Naive is not a constant rate; it degrades.

Stage 2 — COPY into a staging table

  1. COPY staging_table (cols) FROM STDIN with tab-separated text, \N for NULL, and Postgres array literals.
  2. INSERT INTO target SELECT * FROM staging ON CONFLICT DO NOTHING.
  3. Drop the staging table.

The staging table exists because COPY cannot express ON CONFLICT. It lets the pipeline bulk-load raw text and then merge idempotently in one set-based statement.

COPY bypasses the SQL parse/plan/execute path, so throughput jumps. The remaining bottleneck is client-side: one stream write, one small CopyData frame, and one socket write per row.

Stage 3 — COPY with chunked feed

The same staging and merge, but rows are accumulated into a roughly 1 MB buffer and pushed as one chunk. One push becomes one large CopyData frame and one socket write per megabyte.

Per-row client overhead collapses. The server still parses COPY rows internally, but that is cheap. The bottleneck moves to COPY parsing, heap/index insertion, and the INSERT ... SELECT ... ON CONFLICT merge; chunking only accelerates the COPY phase.

The benchmark

The benchmark is packages/db-core/bench/copy-streams-bench.mjs, a standalone ESM script that runs under Node or Bun with no build step. It creates and drops its own table and measures all three paths.

Each run uses 100,000 deterministic rows that exercise COPY edge cases: a NULL column, a TEXT[] array, a large exact NUMERIC, and a BIGINT primary key.

Modes:

  • --baseline for Stage 1;
  • --no-batch for Stage 2;
  • default for Stage 3.

Every mode checks correctness: NULL and array round-trips, exact values, matching row counts, and idempotency after a rerun. All pass in both directions and both runtimes.

Environment: Node 26.7 / Bun 1.3.10, PostgreSQL 18.6 on localhost.

Results

Representative run, 100k rows × 3, Bun:

StageMedian rows/sBest rows/s
1. Naive INSERT312—
2. COPY, no chunk8,5179,830
3. COPY, chunked33,59540,664

Across runs, Stage 2 medians were 6,022, 8,517, and 16,695 rows/s. Chunked runs reached 33,595, 45,714, and 75,833 rows/s on Bun; one Node run reached 80,916 rows/s. Chunking removes most runtime-specific per-row cost, so both runtimes converge toward the database and network ceiling.

Interpreting the progression

  1. SQL execution overhead → COPY. Bypassing parse/plan/execute per row is the biggest structural win.
  2. Per-row client flush → chunking. The row-by-row COPY feed is limited by one small protocol message and socket write per row, not by the database.
  3. After chunking, the database is the bottleneck. Further gains would come from fewer hot-path indexes, partition-local merges, binary COPY, or server-side tuning.

Reproducing

bash
1# Stage 1
2bun packages/db-core/bench/copy-streams-bench.mjs --rows 100000 --runs 3 --baseline
3
4# Stage 2
5bun packages/db-core/bench/copy-streams-bench.mjs --rows 100000 --runs 3 --no-batch
6
7# Stage 3
8bun packages/db-core/bench/copy-streams-bench.mjs --rows 100000 --runs 3

Requires a running PostgreSQL and pg-copy-streams resolvable from packages/db-core.

Caveats

  • Single machine, single client; no network-latency simulation.
  • Absolute throughput is load-sensitive; compare ratios within a run, not absolutes across runs.
  • The naive baseline was measured with --runs 1 at 100k rows because repeated runs are impractical, and it degrades superlinearly with batch size.