Repository navigation
Done
Labels
Description
Problem
Every PostgreSQL read the user sees runs in libpq's single-row mode, so libpq builds and frees a whole PGresult for each row. On the same Mac and data, the full 1M-row trips_1m query took 1.46-1.51 s on PostgreSQL and 1.06-1.07 s on MySQL (results/research/tablepro-query-*-trips_1m-full.json, 3 runs). How much of that gap is the per-row result is not measured yet.
The cost grows with columns. In partial-result mode with a chunk size of 1, libpq copies the column descriptors and names into a new PGresult for every row: about 1 KB per row at 20 columns and 12-16 KB per row at 250 columns (wide_250), often more than the row's own data. Estimated 0.3-0.6 µs per row at 20 columns, not measured.
Cause
- Seven read loops call
PQsetSingleRowModeand handlePGRES_SINGLE_TUPLE, each on its own, inapple/Plugins/PostgreSQLDriverPlugin/LibPQPluginConnection.swift:boundedQuerySync(:1334),cappedCursorRead(:1528),streamResultsfor export, copy and compare (:1803),boundedReadSync(:2150),fetchCursorBatch(:2297),singleRowReadSyncfor table pages and Fetch All (:2377) andreadsRowByRow(:2655). - Each loop makes one
PQgetResult,PQresultStatus, decode andPQclearper row (singleRowReadSync,:2387-2405), decoding row 0 throughsingleRow(apple/Plugins/PostgreSQLDriverPlugin/LibPQPluginConnection+ResultReading.swift:96-101), and takes thePluginQueryCancellationGateNSLockonce per row (apple/Plugins/TableProPluginKit/PluginQueryCancellationGate.swift:35-39). - The bundled libpq is 17.4 (
apple/scripts/build-libpq.sh:27) and hasPQsetChunkedRowsModeandPGRES_TUPLES_CHUNK(apple/Plugins/PostgreSQLDriverPlugin/CLibPQ/include/libpq-fe.h:50-51,:133,:512). Nothing inapple/uses them.
Change
- Fold the seven loops into one shared row reader first, so the change lands and is tested once. The header check
carriesRowDescription(LibPQPluginConnection.swift:1761-1763) moves with it. - In that reader, call
PQsetChunkedRowsMode(conn, n)after the send, the documented libpq 17 API, and decode rows0..<PQntuplesfrom eachPGRES_TUPLES_CHUNK. Read column OIDs and metadata once from the first chunk, as today. - Choose
nadaptively: start at 1 and grow (for example 1, 16, then up to about 1 MiB of row data per chunk), so first-row latency and Stop stay as they are for a slow query. A fixed chunk would hold back the first rows of a nested loop or a per-row function untilnrows exist. - Check cancel and abort once per chunk. Keep the row-cap and byte-limit stops: stop mid-chunk, then run the existing
cancelAndDrainorfinishPendingResults. - Fall back to single-row mode when
PQsetChunkedRowsModereturns 0. Test PGlite and the other PostgreSQL-compatible engines that use the same libpq. - The per-row timing label becomes time to first chunk; rename it to say so.
Measure
trips_1mandwide_250on the bench fixtures: Time Profiler time underPQcopyResult,pqResultAlloc,malloc,free,PQclearandPQgetResult, before and after.- Count
PQcopyResultcalls during the fetch with dtrace: about 1M today fortrips_1m, about 1M / n after. - Wall time of the full 1M-row query, 3 runs, and time to first row for
SELECT pg_sleep(0.01), g FROM generate_series(1, 200) g(it must not get slower).
Part of the performance plan.
Activity
datlechin commentedon Nov 25, 2026
#4566 ships the macOS part: every read in
LibPQPluginConnectiontakes its rows in chunks of 8 throughPQsetChunkedRowsMode, with single-row mode as the fallback and for reads with a byte limit. Client CPU for 1M rows x 20 columns drops from 1.35-1.64 s to 0.90-0.93 s.What is left:
pg_sleep(0.01)x 200: 2.158 s single-row, 2.184 s at 16).boundedReadSyncand the cursor fetches with a byte budget) stay in single-row mode, since a chunk holds every raw row of it before the budget can stop the read.PostgreSQLDriver(apple/TableProMobile/TableProMobile/Drivers/PostgreSQLDriver.swift, around:900and:1006-1075) still callsPQsetSingleRowMode. Its stream has its own read-ahead, so it needs its own change and tests on the iOS target.