Skip to content

perf(import): bulk-load rows with COPY and LOAD DATA, and batch when skipping errors #4554

Done

Description

@datlechin

Problem

  • CSV, JSON and XLSX imports and Table Transfer send one parameterized multi-row INSERT per 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's COPY and MySQL's LOAD DATA are 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.write awaits one host.execute per 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 has mysql_set_local_infile_handler (apple/Plugins/MySQLDriverPlugin/CMariaDB/include/mysql.h:533), which feeds LOAD DATA LOCAL from 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.mappedColumnsAndValues lowercases 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 calls sink.insertRow once per row (:111-140).
  • ImportSinkRun calls observe() after every statement and row batch (apple/TablePro/Core/Plugins/ImportSinkRun.swift:128, :136, :164, :183), and observe reads heldSessionTransactionState (:375-388). SQL Server answers with a probe query (apple/Plugins/MSSQLDriverPlugin/MSSQLPlugin.swift:549-561). MySQL and PostgreSQL answer from client-side status.
  • TableTransferService is @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 calls ImportDataSinkAdapter.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 positional insertRows(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 STDIN in text format, streamed through the existing copy-in.
    • MySQL and MariaDB: LOAD DATA LOCAL INFILE fed by mysql_set_local_infile_handler from the import's own rows, when the server has local_infile on. 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.
  • 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 @concurrent function 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_statements calls 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_commit delta) 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_completed events (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

  1. added
    enhancement
    platform-macos
    on Nov 25, 2026
  2. datlechin commented on Nov 26, 2026

    @datlechin
    MemberAuthor

    #4602 ships three parts of this issue:

    • The transaction state is read only after a call that can have moved it, so SQL Server no longer pays a probe query after each plain statement or row batch inside the import's transaction.
    • The column mapping is resolved once per field layout, not for every row.
    • Table Transfer's row loop runs off the main actor, and the row count reaches the sheet on a 100 ms throttle.

    Still to do, each its own change:

    • Bulk load. A bulk-rows capability on the import sink, per driver: COPY ... FROM STDIN in text format through the existing copy-in on PostgreSQL, and LOAD DATA LOCAL INFILE fed by mysql_set_local_infile_handler on MySQL and MariaDB when the server has local_infile on, 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.
    • Skip and Continue keeps batching. Run inside a transaction; on PostgreSQL set a savepoint per batch, roll back to it on failure and bisect to find the bad rows; on MySQL and MariaDB bisect without savepoints; engines without savepoints keep the row-by-row path.
    • Overlap. A bounded producer that builds batch N+1 while batch N is on the wire.
    • Positional rows. Carry rows as [PluginCellValue] from the CSV, JSON and XLSX parsers and from Table Transfer through insertRows(columns:values:), with the mapping plan resolved to source indexes.

    The Measure section above still applies to each of them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementplatform-macos

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions