Streaming Large CSV Exports Without Running Out of Memory

Datasette's November 2026 streaming work shows why page size dominates a big CSV. Stream rows, cap the file, and do not buffer the whole export in RAM.

By Muhammad Saif and Aashan Khan · Techtribe co-founders

Streaming CSV exports are a solved problem that generated apps keep getting wrong, because the wrong version is a one-liner and it works on the seed data. On November 15, 2026, Datasette 1.0a43 made streaming the normal path for its CSV and TSV output. The same day's follow-up, pull request 3070, fixed how that stream asks the database for rows. A 200,000-row table did not get smaller. The export got faster because it stopped doing ten times as many queries as it needed. That is the whole lesson, and it is not specific to Datasette. If your export loads every row into a list, or pages them 100 at a time across a chatty link, you will feel it the week the table stops being a demo.

Large CSV export performance is mostly about not holding the file and not chatting with the database more than you have to. Memory is the outage. Round trips are the latency. A cap is how you stay up when someone asks for all of it anyway. This piece uses Datasette's notes as the news hook, then walks the pattern for any app server in front of any database.

An agent that can query the database will eventually ask for the CSV too. The permission question is in what a database agent should be allowed to read. The export question is here. A read-only role can still ruin a server by streaming nothing and buffering everything.

What 1.0a43 actually changed

Willison's release note says he had been using a coding agent to push the 1.0 milestone, and that streaming output renderers were the big 1.0 item in 1.0a43. CSV and TSV, which Datasette has long offered as an alternative representation of a table or a query, now go through that streaming path. Plugins can register a renderer that streams every row in one response, using a new Response.stream() method. There were also security fixes in the release, including one he considered worth a GitHub advisory: private stored queries could expose their title and SQL, without allowing the query to run. Streaming and that advisory landed together. Ship the stream. Also stop leaking the text of queries you thought were private. They are different bugs. Both are in the same note so you do not only celebrate the fast path.

Datasette's CSV documentation has described streaming for a while. By default a CSV is the first page of rows, not the whole table. A flag, ?_stream=on, walks every matching row and sends one file. Because tables get large, that mode has been capped, on the order of 100 MB, after which the file ends with a truncation message unless you raise max_csv_mb. You can turn the feature off. The cap is the grown-up part. A stream that never ends is not a feature. It is a worker pinned to a socket until someone notices the bill.

The export dialog learned the same idea. "Include all rows" used to apply to CSV and TSV only. PR 3070 groups any format that can stream into that part of the form, so a plugin does not have to hope the user will edit the URL. Formats that cannot stream stay on the short list. The UI now matches the capability. That is a small product decision and the right one: do not offer "all rows" on a renderer that will buffer them.

The benchmark is about queries, not about CSV

The pull request explains a mistake that is easy to ship. Streaming walked the table using the default page size of 100. A large export therefore issued far more queries than a user staring at a page would. The change uses the largest page size for a streaming request. An explicit page size in the URL is still honored. A size set on the table for browsing no longer applies to the stream. The stream asks for up to max_returned_rows per page.

The microbenchmark is specific, so keep it specific. Local server, 200,000 rows, four columns, about 7 MB of CSV, median of five runs. At 100-row pages, 5.21 seconds. At 1,000-row pages, 1.09 seconds. The "before" number was measured on the same branch by forcing the small page. This is not a claim about your production network. It is a demonstration that the dominant cost was the loop, not the bytes. Five seconds versus one second on 7 MB is almost embarrassing, which is why it is useful. The file was never the problem. The number of times the code went back to the database was the problem.

If your exporter uses an ORM that loads a related object per row, you do not have a page-size problem. You have an N+1 problem wearing a download button. One query that selects the columns you intend to emit, in a stable order, paged by a key, beats a hundred convenient queries. The CSV writer should see tuples, not a graph of objects you happen to have in memory because the template layer used them.

Page size is a trade. A bigger page is fewer round trips and a larger burst of memory. A smaller page is the opposite. The Datasette change moved from a default that was correct for a web page to a default that was correct for a file. You should do the same split. Browsing can stay at 50 or 100. Exporting should use a page sized to the row, perhaps a few thousand narrow rows, or a server-side cursor that does not page through OFFSET at all.

OFFSET pagination is the trap on the second million rows. LIMIT 1000 OFFSET 500000 makes the database walk half a million rows to throw them away. Keyset pagination, where the next page starts at the last id you emitted, stays cheap. If you stream with OFFSET because it was easy, the export gets slower as it goes, and a timeout in the middle gives the user a truncated file that looks complete. Prefer a cursor or a key. If you must use OFFSET, say so in a comment and keep the cap small enough that you never reach the ugly part.

Do not build the file in RAM

The failure mode is boring. A handler runs a query, appends each row to a string or a list of strings, joins them, and returns the result. On a thousand rows it works. On a million it uses a multiple of the file size, because the database driver, the list, and the encoded string all hold copies, and then the framework copies the body again. Two concurrent exports double it. The process hits its limit, the orchestrator restarts it, and the other requests on that process die too. The user retries. Now you have four.

Stream. In Python that is a generator or an async iterator written to the response. In Node it is a stream or an async iterator piped to the response. The important property is not the language. It is that the bytes for row N can be discarded before row N plus a million exists. Flush as you go. Do not wrap the generator in a helper that "just collects it" for logging.

Set the response headers before the first row if you can. A filename via Content-Disposition is fine. A Content-Length is not available if you do not know the size, and guessing wrong breaks clients. Chunked transfer is the normal choice. If a proxy buffers the whole response before sending it, you have moved the RAM problem one machine to the left. Check the proxy. A download that sits at zero percent and then appears all at once is a buffered proxy, not a stream.

Timeouts have to be longer than a page view and shorter than forever. Align the load balancer, the app, and the database statement timeout so the export either finishes under the cap or fails cleanly. A database timeout at row 400,000, after the HTTP status was already 200, produces a file the user's spreadsheet will open with a missing tail and no error. That is worse than a 413 or a 400 at the start. If you cannot know the size up front, watch the byte count as you write and abort with a clear final line, the way Datasette's truncation message does, or abort before you start by counting with a cheaper query.

Write to object storage when the file is for later, not for a browser tab that must stay open. A worker selects rows, writes a CSV object, and emails a link, or shows one, that expires. The web process stays small. The user can retry the download without rerunning the query. This is the right shape for "export everything" in a SaaS product. A synchronous stream is the right shape for a filter the user can see, under a cap. Offer both. Do not offer only the synchronous one with no cap.

Backpressure matters when the client is slow. If you pull a million rows from the database into a buffer because the laptop on the other end is on a cafe Wi-Fi, you have reintroduced the RAM problem. Use a stream implementation that waits when the socket is full. If your database cursor holds a transaction open the whole time, a slow client holds a transaction open. That can block vacuums and migrations. Consider a shorter transaction that reads a key range, or a replica dedicated to exports, so a download cannot stall the primary. Observability here is simple: export duration, bytes, row count, and how often you hit the cap. A graph that only shows request latency will hide a worker that is busy for ninety seconds and "successful."

What the file contains is a product decision

Streaming does not decide which columns are safe. An export of a table is a copy of the table. If the table has tokens, passwords, or another tenant's rows, the CSV is a breach that the user asked for politely. Apply the same view you would give a read-only agent. Strip secrets. Filter the tenant in SQL, not after you have streamed someone else's rows into the file. A bug that checks the tenant in the UI and not in the export query is a classic, and generated CRUD apps love it. Test it. Two tenants, one export, zero foreign rows.

Dates, time zones, and Excel are the support burden. A stream of ISO timestamps will be "wrong" for someone who opens the file in a spreadsheet that converts them. Document the format. Do not silently change it between versions, because people script against exports. A header row should be stable. Adding a column in the middle breaks those scripts. Add columns at the end, or version the export.

Commas, quotes, and newlines in fields are why you use a real CSV writer and not string concatenation. A hand-rolled join will corrupt the row that contains a comma in a name, and it will do it only for that customer, who will then tell you your numbers are wrong. Use the library your language already ships. This is not a place to be clever.

Compression is optional and often wrong for a small streamed response, and right for a stored object. Gzip in front of a tiny export spends CPU to save nothing. Gzip on a multi-hundred-megabyte object in storage is a kindness. Measure before you turn it on for the synchronous path. The Datasette benchmark's 7 MB file does not need a lecture about compression. Your 4 GB export does.

Authorization belongs on the route every time. A signed URL for a stored object should expire, and it should be scoped to the object, not to the bucket. Do not log the full URL with the signature into a shared error tracker. The export you protected in the app is public if the link leaks. Rotate by letting it expire.

An agent will request the unbounded one

Coding agents and in-product assistants like exports because a CSV is an easy tool result. They will call it with no filter. Put the cap in the tool, not in the prompt. Return an error that says the filter is required, or that the result was truncated, in a form the model can repeat to the user without inventing a reason. A tool that returns a megabyte of CSV into a model context is also a token bill and a privacy leak to the model vendor. Prefer "here is a link" or "here is a count" over "here are the rows" when the caller is a model.

If a background job creates the file, record who asked, the filter, and the byte size. You will need that when a customer says they never requested an export and a file of their users exists in a bucket. The log is the difference between an incident and a mystery.

Idempotency helps. Two clicks should not start two full scans if the first is still running. A lock per user per filter, or a button that disables, is enough. The Datasette page-size fix assumes one export. Your users will double-click.

What to change in the handler you have

  1. Find every path that builds a CSV, an XLSX, or a "download all." If it concatenates into one string, it is the bug.
  2. Stream from a single query, or from keyset pages sized for export, not from the UI page size. Stop using large OFFSETs.
  3. Cap rows or bytes. Fail with a message the user can act on, before the process is killed. Datasette's 100 MB default is a reasonable order of magnitude to argue with, not a number to copy blindly.
  4. Check the tenant and the column list in the query. Add a test with two tenants.
  5. Use a CSV library. Keep the header stable.
  6. If the export can run long, move it to a worker and a pre-signed link. Do not hold a database transaction open for a slow laptop.
  7. If an agent can call the export, put the same cap in the tool and do not feed the whole file back into the model.

A faster page size made Datasette's stream honest about where the time went. Your version of that honesty is a handler that can export a large table without becoming the size of the table. The seed data hid the bug. Production will not.

If this is your app, send the repo.

A 30-minute scope call. We read what is live, name what is dangerous, and quote a fixed scope.

How Techtribe replaces a download that kills the process

We find the export paths, stream them or move them to a job, and add the cap and the tenant test. You get a handler that can run while the table grows, and a note of what it refuses to put in the file. We do not leave a string concatenation in place because it worked on a hundred demo rows.

One broken download can be a fixed pass. A product whose customers live on exports, with permissions and audit, is a Production MVP if the rest of the app needs that level of care too. The scope call is 30 minutes with a founder, and it is where we say which you are in.

Bring the route that builds the file and how big the table is now. If the process has already been killed by an export, say that first.

The fixed scope for taking a generated app to production is a page per tool: Lovable, Bolt, v0, and Replit.

Questions

What did Datasette change for CSV exports?

On November 15, 2026 Simon Willison released Datasette 1.0a43, which streams output renderers. Built-in CSV and TSV exports use that path, and plugins can stream every row through a new response API. A follow-up the same day makes streamed exports fetch pages at the max page size instead of the default 100-row page.

Why did the page size matter so much?

A note on the change timed a 200,000-row, four-column export, about 7 MB of CSV, on a local server. Paging 100 rows at a time took a median 5.21 seconds. Paging 1,000 rows took 1.09 seconds. The file was the same. The number of database round trips was not.

Should a web handler load the whole CSV into a string?

No. Build and send the file as rows come off the cursor, or write through to object storage and hand the user a link. A string that holds every row will grow with the table until the process dies, usually while a second export is also running.

What cap should you put on an export?

Pick a maximum size or row count, return a clear error when the user hits it, and offer a narrower filter or an async job. Datasette's own streamed CSV defaults to stopping around 100 MB unless you raise the setting. A cap you can explain is better than a process you have to restart.

Book a free call

Have a vibe-coded app in production?

30-minute free call. We'll read what you have, name what is dangerous, and quote a fixed scope.