Joining SQL Server and Postgres on your laptop
Join results from SQL Server and Postgres in a per-notebook DuckDB database, with copy prompts, provenance, quotas and a locked-down engine.
Revenue is in SQL Server. Sales targets are in a Postgres database somebody set up for the finance team. You want one table: country, revenue, target, gap. The usual options are a linked server on the SQL Server side, tds_fdw on the Postgres side, or exporting both to CSV and doing it in a spreadsheet. The first two need someone with rights on a server. The third is what most people actually do.
SQLly notebooks now do this on your laptop with DuckDB. This post walks through how, what gets copied where, and the limits. It's the fourth in a series about SQLly notebooks.
The revenue-against-targets notebook running: the copy prompt, the join in local DuckDB data, the source chips and the local data rail. Recorded against SQL Server and a local Postgres. Watch the whole recording, with chapters.
Every notebook gets its own DuckDB database
Each notebook can keep a small DuckDB database on your computer, shown as Local (this notebook). It's one file per notebook in SQLly's data folder. Before this, SQLly had one shared in-memory DuckDB for the whole app, and the only way to get rows into it was as text. Now results go in through DuckDB's appender with their types mapped, and each notebook's tables stay with that notebook.
To use it, open a SQL cell's ⋯ menu and choose Run on Local Data. The cell's environment badge turns into a Local data badge, it runs DuckDB SQL, and IntelliSense knows the local tables and their columns. It still runs through the same results grid as any query, with streaming, spill to disk and Cancel.
In a local cell, a reference means "copy that result in"
Here's the whole join. revenue is a cell on SQL Server, targets is a cell on Postgres, and this cell runs on local data:
-- a cell on Local (this notebook)
SELECT r.country, r.revenue, t.target, r.revenue - t.target AS gap
FROM {{revenue}} AS r
JOIN {{targets}} AS t USING (country)
ORDER BY gap
Between two cells on the same connection, a reference folds the other query in as a CTE. Across connections a normal cell refuses and offers to switch to local data. In a local cell, {{revenue}} means "that cell's result, copied into local data". When the cell runs, SQLly checks each input. If the copy is missing or out of date, because the source SQL changed or there's a newer result than the copy, it copies it in first and then runs the join.
It also asks before it copies. The first time a notebook copies results from a connection, you get "Keep copies of results from Storefront Prod on this computer?" with an explanation of where they go, and a production connection is called out. That's once per connection. Then each run that needs copies asks "Copy 2 inputs for vs_target into local data?" until you tick the box to copy automatically in this notebook.
A partial result never gets copied as if it were whole
A result on screen isn't always the whole result. A row limit may have cut it short, it may have spilled to disk, or long values may have been shortened for display. Copying that into a join would give you a confident-looking wrong answer.
So any input that isn't wholly on screen is fetched again with its own query, read-only, and the copy gets every row. The exception is a cell that's allowed to change data. SQLly never runs that one again just to copy it. It copies the result on screen if it's complete, and otherwise asks you to run the cell yourself first. If you materialize a cell by hand with Materialize Locally As… and only part of it is on screen, SQLly asks "Run orders again to copy every row?" instead of quietly copying what it has. Progress shows in the activity panel with a Cancel button.
After the join runs, the cell header shows a chip per input with its source connection and environment. Under the results, one line per input says where it came from, when it was read, how many rows it has, and whether a limit cut it short.
Every copied table remembers where it came from
Each local table records its provenance: the connection's name, an opaque id, the engine, database and environment, a hash of the source SQL, the cell and run that produced it, when it was read and saved, row counts, column types, whether it's complete and why not, and a checksum of its rows. Copies never contain passwords or connection details. If a value doesn't fit the column type SQLly picked, that column falls back to text instead of losing the value, and the column is marked as kept as text.
The Local data page on the left rail shows all of it: space used against the limit, every table with its rows, size and source, and buttons to refresh, export as Parquet or CSV, or drop a table. You can also bring files in with an Attachment cell: CSV (plain, gzip or zstd), Parquet, JSON, NDJSON or a SQLite file. The file is copied into the notebook's private folder first and its hash kept, so SQLly notices when it changes. In a trusted notebook, a PNG or JPEG attachment can also show up inside a Markdown cell with . Markdown never loads images from the web, so anything else shows as a small placeholder.
The local database is locked down
A notebook can come from someone else, and DuckDB is very good at reading files. That's a bad combination. So each local database runs with extensions off (no install, no auto-load, no community extensions), no access to files outside its own exchange folder, settings locked, and every query inside a read-only transaction. Only reading statements are accepted: SELECT, WITH, VALUES, FROM, TABLE, SHOW, DESCRIBE, SUMMARIZE, EXPLAIN, PIVOT and UNPIVOT. read_csv, COPY ... TO, ATTACH, INSTALL, SET and CREATE SECRET are refused. A hostile notebook can't use local SQL to read your home folder. Attachments have their own rule: a file outside the notebook's folder is read only after you pick it on this computer, and a path that leads out through a linked folder is refused.
There are quotas too. By default: 1 GiB of memory per local database, 10 million rows and 2 GiB per table, 4 GiB of disk per notebook, 2 GiB per imported file, and 100,000 rows returned from a local query to a cell. All notebooks' local data together is capped at 20 GiB. Past that, the notebooks used least recently are cleared first, never one that's open. A copy that hits the row limit is marked partial. The size limits are estimates of how big the data is rather than byte counts on disk.
What doesn't work yet
Encryption is clunkier than I'd like. The Local data page has an Encrypt local data switch, with the key kept in your system keychain, but a DuckDB file can't be encrypted in place, so the switch only works while the notebook has no local tables. With tables, SQLly asks you to clear local data first, and they come back when their cells run again. On Windows, importing and exporting through long \\?\ paths hasn't been verified. Arrow IPC files aren't imported because DuckDB would need a downloadable extension, so use Parquet. A SQLite file's pending write-ahead log isn't read.
The command-line runner imports attachment files now, but it doesn't run Materialize, and its local cells make their own copies. A scheduled run only reads attachment files inside the notebook's folder. Local data isn't available in the browser build at all, so local cells there show Unavailable.
The full walkthrough is on Local data with DuckDB, and if you mostly want DuckDB for files, there's an older post on querying Parquet and CSV in SQLly. The next post turns results like this join into charts and dashboards. If you have two databases you've always wanted in one query, try it. I'd like to hear which pair.