Skip to content

fix(export): stream chunks incrementally for SQL, CSV, and JSON dumps to prevent OOM - #313

Open
Saik2789 wants to merge 1 commit into
outerbase:mainfrom
Saik2789:fix/streaming-database-dumps-59
Open

Saik2789 wants to merge 1 commit into
outerbase:mainfrom
Saik2789:fix/streaming-database-dumps-59

Conversation

@Saik2789

Copy link
Copy Markdown

Purpose

Fixes #59

Database dumps on /export/dump, /export/csv/:tableName, and /export/json/:tableName previously failed on large databases because they loaded the entire dataset into memory at once as strings/buffers before creating a Blob response. On larger datasets or Cloudflare Workers with a 128MB memory budget, this caused out-of-memory errors and request timeouts.

Changes

  • Incremental Streaming via ReadableStream:
    • /export/dump (SQL): Streams -- Table: ..., CREATE TABLE ..., and chunked INSERT INTO ... statements via a ReadableStream. Properly formats NULL values.
    • /export/csv/:tableName (CSV): Streams headers followed by CSV records incrementally with RFC 4180 quote/comma/newline escaping.
    • /export/json/:tableName (JSON): Streams formatted JSON array entries ([\n { ... },\n ... \n]) incrementally over a ReadableStream.
  • Bounded Memory via Pagination:
    • Reads data in chunks using LIMIT 1000 OFFSET <n> so peak memory consumption is bounded to O(chunk) rather than O(entire_database).
  • Export Response Layer:
    • src/export/index.ts: Enhanced createExportResponse to accept ReadableStream as BodyInit while preserving attachment headers and chunked transfer.

Verification

  • Unit test suite: pnpm vitest run src/export/ (all 31 tests passing).
  • Prettier check passes cleanly with zero formatting errors.

Bounty Claim

@algora-pbc /claim #59

Payout Information

  • Wallet (USDC on Base network): 0x20A0525b47f087f1Cc498cda257A0feB78D2320B

- Refactor /export/dump, /export/csv/:tableName, and /export/json/:tableName to use ReadableStream
- Page table data in chunks using LIMIT/OFFSET pagination (1000 rows/page) to prevent OOM
- Update createExportResponse to support ReadableStream
- Add unit and integration tests verifying incremental streaming and pagination
- Fixes outerbase#59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Database dumps do not work on large databases

1 participant