Skip to content

perf(postgresql): read rows in libpq chunks instead of one result per row - #4566

Merged
datlechin merged 7 commits into
mainfrom
perf/postgresql-chunked-rows
Nov 27, 2026
Merged

datlechin merged 7 commits into
mainfrom
perf/postgresql-chunked-rows

Conversation

@datlechin

@datlechin datlechin commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

Summary

  • Every PostgreSQL read used libpq's single-row mode, which builds, copies the column descriptors into, and frees one PGresult per row.
  • Reads now call PQsetChunkedRowsMode(conn, 8) (libpq 17.4, bundled) through one entry, LibPQRowMode.enter, with single-row mode as the fallback. The seven loops (boundedQuerySync, cappedCursorRead, streamResults, boundedReadSync, fetchCursorBatch, the row-by-row read for table pages and Fetch All, and the pipeline) share that entry, one row test (carriesRows) and one decoder (row(_:of:oids:)), and each walks 0..<PQntuples per chunk. A row cap stops inside a chunk and leaves the rest undecoded; cancel, drain, savepoint and pipeline row counts are unchanged.
  • Reads with a byte limit keep single-row mode. libpq holds every raw row of a chunk until it is full, so a chunk of very wide rows would defeat the budget those reads exist for.
  • singleRowReadSync is now rowByRowReadSync; LibPQPipelineTally.takeRow() is now takeRows(_:).

Measured

Client CPU (getrusage) of a libpq probe against PostgreSQL 17.12, reading and touching every cell, 3 runs each:

result single-row 8 rows 16 rows
1M rows x 20 columns 1.35-1.64 s 0.90-0.93 s 0.87-0.92 s
50k rows x 250 columns 0.74-0.80 s 0.50-0.52 s 0.46-0.54 s

256 and 1,024 rows gain nothing more. SELECT pg_sleep(0.01), g FROM generate_series(1, 200) g returns its first rows at 2.158 s in single-row mode and 2.184 s in chunks: the server buffers its own output, so chunking does not delay a slow query's first rows. Wall times on this shared Mac were too noisy to report.

Not in this PR

  • Growing the chunk size during a result, as the issue proposed: libpq takes the chunk size once per query, before the first result, and hands a chunk over only when it is full or the result ends. A small fixed size gets the gain.
  • The iOS PostgreSQLDriver (TableProMobile) still reads row by row. Its stream has its own read-ahead code. Noted on perf(postgresql): read rows in libpq chunks instead of one result per row #4547.

Test plan

  • LibPQRowModeTests against the scripted server: 2 x 8 + 5 rows arrive as chunks of 8, 8, 5 and an empty PGRES_TUPLES_OK; chunked: false gives single rows; a cap of 20 over 40 rows stops inside a chunk and reports truncated; a 37-row stream hands over every row in order.
  • LibPQRowCapacityTests: each row of a 3-row chunk is read at its own index at exact capacity. LibPQStreamHeaderTests takes PGRES_TUPLES_CHUNK as a row description.
  • The existing scripted-server suites (LibPQSingleRowReadTests, LibPQBoundedReadOnlyTests, LibPQCappedUserReadTests, LibPQPipelineTests, LibPQStopBeforeStatementTests, LibPQSavepointProbeTests) run every loop over chunks; caps of 3 stop inside a chunk.
  • verify.sh test over those 12 suites: 86 executed, 86 passed. verify.sh build passes.
  • perf(memory): build each result row at its exact size in every driver #4563 is on main now. The merge keeps this branch's row(_:of:oids:) over main's singleRow(of:oids:), drops LibPQSingleRowCapacityTests for LibPQRowCapacityTests, and takes main's ClickHouse decoder and its tests.
  • verify.sh --jobs 4 on the merged head 0a4085a: build PASS, plugins PASS; test over LibPQRowModeTests, LibPQRowCapacityTests, LibPQStreamHeaderTests, LibPQSingleRowReadTests, LibPQBoundedReadOnlyTests, LibPQCappedUserReadTests, LibPQCappedCursorReadTests, LibPQPipelineTests, LibPQStopBeforeStatementTests, LibPQSavepointProbeTests, LibPQReadBoundTests and LibPQCellDecodingTests: 85 executed, 85 passed.
  • Codex review and security pass on the merge: clean.

Closes #4547

On the macOS 13 and iOS 18 deployment targets an enumerated sequence is not a
Collection, so `enumerated().map` cannot reserve its count and grows the row by
doubling: a 20-cell row ends at capacity 36, 896 bytes instead of 512. Map over
`indices` in every per-row builder: PostgreSQL single-row reads, MongoDB
flattening, ClickHouse TabSeparated rows and the binary demote pass, Snowflake,
BigQuery, the core result stream and the iOS plugin row emitters.
… row

Every read ran in libpq's single-row mode, which builds, copies the column
descriptors into and frees one PGresult per row. Read in chunks of 8 rows
through PQsetChunkedRowsMode (libpq 17), with single-row mode as the fallback
and for reads with a byte limit, which exist to bound memory. Every read loop
takes its rows from one row-mode entry, one row test and one decoder, and stops
inside a chunk at its row cap or byte limit.

Measured against PostgreSQL 17 with the bundled libpq 17.4: 1M rows x 20
columns take 0.90-0.93 s of client CPU instead of 1.35-1.64 s; 50k rows x 250
columns 0.50 s instead of 0.80 s.
@datlechin
datlechin enabled auto-merge (squash) November 26, 2026 01:50
…ed-rows

# Conflicts:
#	apple/Packages/TableProCore/Tests/TableProPluginKitTests/ClickHouseTabSeparatedRowDecoderTests.swift
#	apple/Plugins/PostgreSQLDriverPlugin/LibPQPluginConnection+ResultReading.swift
#	apple/Plugins/TableProPluginKit/ClickHouseTabSeparatedRowDecoder.swift
@datlechin
datlechin merged commit 7407caf into main Nov 27, 2026
10 of 11 checks passed
@datlechin
datlechin deleted the perf/postgresql-chunked-rows branch November 27, 2026 16:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf(postgresql): read rows in libpq chunks instead of one result per row

1 participant