Repository navigation
Done
Labels
Description
Problem
- CSV, JSON and XLSX imports and Table Transfer send one parameterized multi-row
INSERTper batch and wait for it before building the next. A 1M-row import is at least 1,000 sequential round trips (up to 1,000 rows per statement, fewer for wide tables). At +50 ms RTT that is close to a minute of pure latency, before the server parses and plans each batch. PostgreSQL'sCOPYand MySQL'sLOAD DATAare the engines' documented bulk paths, and neither is used for row imports. - With the error policy "Skip and Continue", every row is its own autocommitted statement: one round trip and one commit per row, whether or not any row fails. Estimate, not measured: 1M rows at about 0.2 ms local RTT plus a 0.5-2 ms commit flush is roughly 10-35 minutes.
- On SQL Server every imported statement or row batch pays a second round trip to read the transaction state.
- Table Transfer builds each row's dictionary, coerces it and maps it to the target columns on the main thread, so a large transfer takes main-thread time in every window. Not measured.
Cause
ImportRowWriter.writeawaits onehost.executeper batch (apple/TablePro/Core/Transfer/Shared/ImportRowWriter.swift:186-195). Batches stop at 1,000 rows or 1 MiB (apple/TablePro/Core/Transfer/Shared/SQLWriteBatchBudget.swift:10-11). The CSV plugin hands over 500 rows at a time (apple/Plugins/CSVImportPlugin/CSVImportPlugin.swift:48).- The PostgreSQL driver already streams copy-in for SQL file imports (
apple/Plugins/PostgreSQLDriverPlugin/LibPQDriverCore.swift:505-513,apple/Plugins/PostgreSQLDriverPlugin/LibPQCopyIn.swift), but row imports never use it. - The MySQL client turns local infile off at connect (
apple/Plugins/MySQLDriverPlugin/MariaDBPluginConnection.swift:492-493). The vendored connector hasmysql_set_local_infile_handler(apple/Plugins/MySQLDriverPlugin/CMariaDB/include/mysql.h:533), which feedsLOAD DATA LOCALfrom a callback instead of a file the server names. - Rows travel as
[String: PluginCellValue]dictionaries (apple/Plugins/TableProPluginKit/RowImportRunner.swift:14). For every row,ImportRowWriter.mappedColumnsAndValueslowercases each field name, builds a set and a filtered dictionary, groups the unmatched fields and sorts the pairs by column name (ImportRowWriter.swift:358-374). The mapping never changes during an import. A positional entry,insertRows(columns:values:), exists (apple/TablePro/Core/Plugins/ImportDataSinkAdapter.swift:388-398), but only row generation uses it (apple/TablePro/Core/RowGeneration/RowGenerationLiveSource.swift:235). - Skip and Continue: the runner turns the transaction off (
RowImportRunner.swift:49-50) and callssink.insertRowonce per row (:111-140). ImportSinkRuncallsobserve()after every statement and row batch (apple/TablePro/Core/Plugins/ImportSinkRun.swift:128,:136,:164,:183), andobservereadsheldSessionTransactionState(:375-388). SQL Server answers with a probe query (apple/Plugins/MSSQLDriverPlugin/MSSQLPlugin.swift:549-561). MySQL and PostgreSQL answer from client-side status.TableTransferServiceis@MainActor(apple/TablePro/Core/Services/Export/TableTransferService.swift:65). Its row loop builds a dictionary per row and runs the coercer (:444-462,:497-504), then callsImportDataSinkAdapter.insertRows(ImportDataSinkAdapter.swift:377), which is nonisolated async in an app target built with Approachable Concurrency, so the mapping and the INSERT text building also run on the main actor.
Change
- Positional rows. Resolve the column mapping once per import into a plan: for each target column, its source field index and value traits. Carry rows as
[PluginCellValue]from parser to writer through the positionalinsertRows(columns:values:), for CSV, JSON, XLSX and Table Transfer. - A bulk-rows capability on the import sink, implemented per driver with its documented path:
- PostgreSQL:
COPY <table> (<columns>) FROM STDINin text format, streamed through the existing copy-in. - MySQL and MariaDB:
LOAD DATA LOCAL INFILEfed bymysql_set_local_infile_handlerfrom the import's own rows, when the server haslocal_infileon. The handler serves only that stream, so the client still never reads a file the server names. When the server refuses, prepare the multi-row INSERT once per batch shape and execute it per batch. - Every other engine keeps the parameterized INSERT.
- PostgreSQL:
- Overlap. Build batch N+1 while batch N is on the wire, through a bounded producer.
- Skip and Continue keeps batching. Run inside a transaction. On PostgreSQL, where an error aborts the transaction, set a savepoint before each batch. On failure, roll back to it and bisect the batch to find and record the bad rows, then continue. On MySQL and MariaDB a failed statement rolls back only itself, so the batch is bisected without savepoints. Engines without savepoints keep today's row-by-row path.
COPY ... ON_ERROR ignore(PostgreSQL 17) skips only values that fail input conversion, not constraint failures, so skip mode uses INSERT batches. - Read transaction state only when it can change. Observe after statements the classifier marks as transaction control or as implicitly committing, after an error, at checkpoints and at the end, not after every batch.
- Table Transfer off the main actor. Move the row loop into a
@concurrentfunction that holds the source, sink and coercer, and report counts to the main-actor state on a throttle.
Overlaps #4269: SQ-7 covers the read side of Table Transfer from SQLite (the whole source table buffered). This issue changes only the write side.
Measure
- Rows per second for a 1M-row x 20-column CSV into local PostgreSQL, MySQL and SQLite, and again at +50 ms RTT with Network Link Conditioner. Count round trips with
pg_stat_statementscalls or a packet capture. SQLite is the control: expect small gains there. - Skip and Continue: import 100k rows with 10 bad rows into PostgreSQL and MySQL. Wall time and the commit count (
pg_stat_database.xact_commitdelta) before and after. The same 10 rows are skipped and reported with their lines. - SQL Server: import a 10k-statement script and count
sql_batch_completedevents (Extended Events). Before: about 2 per statement. After: about 1. - Table Transfer of 1M rows: Time Profiler shows no
mappedColumnsAndValues, coercer or INSERT building on the main thread. Animation Hitches while scrolling a grid in another window stay flat.
Part of the performance plan.
Activity
datlechin commentedon Nov 26, 2026
#4602 ships three parts of this issue:
Still to do, each its own change:
COPY ... FROM STDINin text format through the existing copy-in on PostgreSQL, andLOAD DATA LOCAL INFILEfed bymysql_set_local_infile_handleron MySQL and MariaDB when the server haslocal_infileon, with a prepared multi-row INSERT per batch shape when it refuses. It needs a PluginKit capability, so it goes through the ABI check and a plugin release.[PluginCellValue]from the CSV, JSON and XLSX parsers and from Table Transfer throughinsertRows(columns:values:), with the mapping plan resolved to source indexes.The Measure section above still applies to each of them.