Repository navigation
perf(postgresql): read rows in libpq chunks instead of one result per row - #4566
Merged
Merged
Conversation
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
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
PGresultper row.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 walks0..<PQntuplesper chunk. A row cap stops inside a chunk and leaves the rest undecoded; cancel, drain, savepoint and pipeline row counts are unchanged.singleRowReadSyncis nowrowByRowReadSync;LibPQPipelineTally.takeRow()is nowtakeRows(_:).Measured
Client CPU (
getrusage) of a libpq probe against PostgreSQL 17.12, reading and touching every cell, 3 runs each:256 and 1,024 rows gain nothing more.
SELECT pg_sleep(0.01), g FROM generate_series(1, 200) greturns 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
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
LibPQRowModeTestsagainst the scripted server: 2 x 8 + 5 rows arrive as chunks of 8, 8, 5 and an emptyPGRES_TUPLES_OK;chunked: falsegives 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.LibPQStreamHeaderTeststakesPGRES_TUPLES_CHUNKas a row description.LibPQSingleRowReadTests,LibPQBoundedReadOnlyTests,LibPQCappedUserReadTests,LibPQPipelineTests,LibPQStopBeforeStatementTests,LibPQSavepointProbeTests) run every loop over chunks; caps of 3 stop inside a chunk.verify.sh testover those 12 suites: 86 executed, 86 passed.verify.sh buildpasses.row(_:of:oids:)over main'ssingleRow(of:oids:), dropsLibPQSingleRowCapacityTestsforLibPQRowCapacityTests, and takes main's ClickHouse decoder and its tests.verify.sh --jobs 4on the merged head 0a4085a:buildPASS,pluginsPASS;testoverLibPQRowModeTests,LibPQRowCapacityTests,LibPQStreamHeaderTests,LibPQSingleRowReadTests,LibPQBoundedReadOnlyTests,LibPQCappedUserReadTests,LibPQCappedCursorReadTests,LibPQPipelineTests,LibPQStopBeforeStatementTests,LibPQSavepointProbeTests,LibPQReadBoundTestsandLibPQCellDecodingTests: 85 executed, 85 passed.Closes #4547