High-Throughput Bulk Ingestion in PostgreSQL: Why Binary COPY Beats Multi-Row INSERT by 14x
Shafikul Islam
Backend & Cloud Infrastructure Engineer · Infrastructure & Cloud Team Lead

The 65,535 Parameter Limit and Parser Overhead of Multi-Row INSERTs
When engineers optimize slow loop inserts in PostgreSQL, they typically switch to multi-row INSERT INTO table VALUES ($1, $2), ($3, $4)... batches.
While better than row-by-row round-trips, multi-row INSERT hits two walls:
- PostgreSQL Wire Protocol Limit: The extended query protocol uses a 16-bit integer for parameter count, crashing with extended protocol limited to 65535 parameters if a 10-column table batch exceeds 6,553 rows.
- SQL Parser and Planner Overhead: PostgreSQL still parses, rewrites, and plans a massive SQL syntax tree for every batch.
1. Streaming Binary Rows Directly with pgx.CopyFrom in Go
PostgreSQL's COPY FROM STDIN (FORMAT BINARY) bypasses the SQL parser and planner completely, streaming binary tuples directly into heap pages:
func BulkIngestTelemetry(ctx context.Context, tx pgx.Tx, rows []TelemetryEvent) (int64, error) {
_, err := tx.Exec(ctx, `
CREATE TEMP TABLE staging_telemetry (
tenant_id UUID,
device_id VARCHAR(64),
metric_val DOUBLE PRECISION,
recorded_at TIMESTAMPTZ
) ON COMMIT DROP;
`)
if err != nil {
return 0, err
}
_, err = tx.CopyFrom(
ctx,
pgx.Identifier{"staging_telemetry"},
[]string{"tenant_id", "device_id", "metric_val", "recorded_at"},
pgx.CopyFromSlice(len(rows), func(i int) ([]any, error) {
r := rows[i]
return []any{r.TenantID, r.DeviceID, r.MetricVal, r.RecordedAt}, nil
}),
)
if err != nil {
return 0, err
}
tag, err := tx.Exec(ctx, `
INSERT INTO telemetry_events (tenant_id, device_id, metric_val, recorded_at)
SELECT tenant_id, device_id, metric_val, recorded_at
FROM staging_telemetry
ON CONFLICT (tenant_id, device_id, recorded_at) DO NOTHING;
`)
return tag.RowsAffected(), err
}
By streaming into a transaction-scoped TEMP TABLE ... ON COMMIT DROP via CopyFrom and merging with a single set-based INSERT ... SELECT ON CONFLICT, we achieved 104,000+ rows/sec with full conflict deduplication.