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:
| Stage | Approach | Median rows/s | vs naive |
|---|---|---|---|
| 1 | Naive per-row INSERT | ~312 | 1× |
| 2 | COPY into staging, row-by-row feed | ~8,517 | ~27× |
| 3 | COPY 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
COPY staging_table (cols) FROM STDINwith tab-separated text,\Nfor NULL, and Postgres array literals.INSERT INTO target SELECT * FROM staging ON CONFLICT DO NOTHING.- 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:
--baselinefor Stage 1;--no-batchfor 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:
| Stage | Median rows/s | Best rows/s |
|---|---|---|
| 1. Naive INSERT | 312 | — |
| 2. COPY, no chunk | 8,517 | 9,830 |
| 3. COPY, chunked | 33,595 | 40,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
- SQL execution overhead → COPY. Bypassing parse/plan/execute per row is the biggest structural win.
- 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.
- 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
1# Stage 12bun packages/db-core/bench/copy-streams-bench.mjs --rows 100000 --runs 3 --baseline34# Stage 25bun packages/db-core/bench/copy-streams-bench.mjs --rows 100000 --runs 3 --no-batch67# Stage 38bun 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 1at 100k rows because repeated runs are impractical, and it degrades superlinearly with batch size.