Skip to content

perf(export): stream gzip while writing a dump and while reading one back #4553

Done

Description

@datlechin

Problem

  • Export. With "Compress with gzip" on, the SQL export writes the whole dump uncompressed to a temp file, then reads it back and compresses it. Compression starts only after the last row is read, so it cannot overlap the database read. Peak temp disk use is the uncompressed size plus the compressed size. A compressed export also cannot be split into parts. Estimate, not measured: zlib at the default level on SQL text runs at roughly 80-150 MB/s on an M4, so a 2 GB dump adds about 13-25 s after the data phase.
  • Import. Picking a .sql.gz inflates the whole file to a temp .sql before the dialog can show its preview. The dialog then parses the whole file to count statements, and the import parses it a second time. Every byte is inflated and written once, then read and lexed twice. Estimate, not measured: a 1 GB .sql.gz holding about 8 GB of SQL costs about 15-30 s of inflate and 8 GB of disk writes before Import means anything.

Cause

Export:

  • When compressWithGzip is on, the writer targets a temp .sql in the temporary directory (apple/Plugins/SQLExportPlugin/SQLExportPlugin.swift:103-111), and split is turned off with a warning (:113-118).
  • After the data phase, SQLExportCompressor.compress runs (SQLExportPlugin.swift:221-226, apple/Plugins/SQLExportPlugin/SQLExportCompressor.swift:16-52). It calls GzipFile.compress, which reads the temp file back and deflates it (apple/Packages/TableProCore/Sources/TableProGzip/GzipFile.swift:61-84).
  • A streaming GzipDeflater with deflate and finish already exists (apple/Packages/TableProCore/Sources/TableProGzip/GzipDeflater.swift:25, :54, :70). The writer writes each statement straight to a FileHandle (apple/Plugins/SQLExportPlugin/SQLExportFileWriter.swift:91-102) and rotates parts in one place (:163).

Import:

  • ImportDialog awaits FileDecompressor.decompressIfNeeded on file pick (apple/TablePro/Views/Import/ImportDialog.swift:408). That inflates the whole file to a temp file (apple/TablePro/Core/Utilities/File/FileDecompressor.swift:20-27) before the 5 MB preview is read (ImportDialog.swift:437-438).
  • It then starts ImportStatementCount.count (ImportDialog.swift:465-468, :566-583), which runs SQLFileParser.countStatements over the whole file (apple/Packages/TableProCore/Sources/TableProSQLFile/SQLFileParser.swift:676). The count only feeds a label and the progress total (knownStatementCount, ImportDialog.swift:523).
  • The import reuses the temp file (ImportDialog.swift:505-523, apple/TablePro/Core/Plugins/SqlFileImportSource.swift:132-150) and parses it again.
  • The parser can only read a file through a FileHandle (SQLFileParser.swift:1033-1037). A streaming GzipInflater already exists (apple/Packages/TableProCore/Sources/TableProGzip/GzipInflater.swift:30, :62). The import source already reports bytes read against file size (SqlFileImportSource.swift:63).

Change

Export:

  • Give SQLExportFileWriter an output sink that deflates as it writes, using GzipDeflater(container: .gzip), behind the existing atomic temp-file pair. The temp .sql and SQLExportCompressor's second pass go.
  • Each split part is its own gzip file, finished at rotation. Split and gzip then work together, and the "split size was not applied" warning goes.
  • Deflate on its own serial queue fed by a bounded buffer, so compression overlaps the database read instead of following it.

Import:

  • Let the parser read from a byte source: the file itself, or a GzipInflater over the file. Nothing is inflated to disk.
  • Build the preview from the first 5 MB of inflated text.
  • Drop the up-front statement count. Drive progress by compressed bytes read for .gz and by bytes read for .sql, through reportsReading. Show the statement count as a running number.
  • The disk-space budget for inflating to a temp file (GzipFile.swift:12-24) is no longer needed on this path. The parser's statement size limit still bounds memory.

The batch-separator pre-scan for SQL Server grammar is #4555's.

Measure

  • Export a 1M-row and a 10M-row PostgreSQL table as .sql.gz. Record wall time and bytes written with fs_usage or Instruments File Activity. Before: two uncompressed-size writes and one uncompressed-size read. After: only compressed bytes written, and wall time close to the uncompressed export.
  • Export with gzip and a split size: every part decompresses on its own, and the parts restore in order.
  • Import a 1 GB .sql.gz: time from file pick to the preview, time from Import to the first statement, total wall time, and peak temp directory size (du on the temporary directory). After: no temp .sql, and no countStatements samples in Time Profiler.
  • The round trip test apple/Plugins/FormatPluginTests/ExportImportRoundTripTests.swift:32 ("A gzipped SQL dump decompresses to the whole dump and restores every row") keeps passing, with a split-and-gzip case added.

Part of the performance plan.

Activity

  1. added
    enhancement
    platform-macos
    on Nov 25, 2026
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