Running the server

The zsql binary is the CLI and the server. zsql serve runs the server in the terminal; zsql server start keeps it running in the background. Everything the docs describe, the query API, deploys and the MCP endpoint, is served by that one process, on your machine or on a host you run. Nothing phones home.

The server plans and runs: a spec in, the rows out. It plans the spec against the deployed model into one SQL statement, runs it on the warehouse with the credential the datasource was deployed with, returns the rows, and keeps the result to answer the same question again. Asked for SQL instead, it returns the statement and connects to nothing.

Serve the project you are editing

Inside a project directory:

$ zsql serve
zsqld: 0 deployment(s) loaded from /home/you/.zsql/data
zsqld: listening on http://127.0.0.1:3699
zsql: serving /home/you/tpcds as tpcds/main; redeploying as its files change
deployed tpcds/main: 3 tables, 5 fields, 2 joins, 2 paths, 0 policies, 0 sampled dimensions
warehouse: no credential: a run needs one in its request
no tests (tests/*.yml)

The server comes up on 127.0.0.1:3699, deploys the directory as the checked-out git branch, and redeploys every time a .yml or .yaml under the project changes. One command gives you a live endpoint over the model you are editing:

curl -s http://127.0.0.1:3699/projects/tpcds/branches/main/run \
  -H "Content-Type: application/json" \
  -d '{"expr": "month(date), web net paid"}'

There is no key and no Authorization header. The CLI’s other commands look at the same address by default, so zsql sql -r "..." plans against the server’s copy with no configuration. Ctrl-C stops it; a client mid-request is answered first.

Deployed models live under the data directory, ~/.zsql/data unless --data-dir or ZSQL_DATA_DIR says otherwise, so a restart reloads them. Two servers cannot share a data directory; the second is told which process has it.

In the background

$ zsql server start
started process 41213 on 127.0.0.1:3699; log at /home/you/.zsql/server.log
$ zsql server status
running as process 41213 at http://127.0.0.1:3699; it answers
$ zsql server logs --lines 20
$ zsql server stop
stopped process 41213

start takes every flag serve takes and runs the same thing detached, with its output in $ZSQL_HOME/server.log. status asks the data directory’s lock whether the server it started is still the one running, then calls its liveness route. stop sends SIGTERM and waits, escalating after 15 seconds.

Outside a project

zsql serve --no-project, or zsql serve from a directory with no project.yml, serves whatever the data directory holds and deploys nothing by itself. Projects reach it with zsql deploy:

cd ~/projects/tpcds
zsql deploy --server http://127.0.0.1:3699

The CLI defaults to that address, so inside a project zsql deploy alone is enough. This is the shape a shared server takes: one process, several projects, each deployed from its own repository by its own zsql deploy, or from CI.

Running a query

A run plans the request, runs the statement on the warehouse and returns the rows. (POST .../sql takes the same body and returns only the statement, for an application that runs it on its own connection.)

POST /projects/{uid}/branches/{branch}/runPlans a spec or an expression under its context, as sql does, and runs the statement on the datasource the plan chose
POST /projects/{uid}/branches/{branch}/sql-execRuns one SQL statement of the caller’s own on a named datasource. Never cached, outside the model, and it may write
GET /results/{id}The record of a run, and a page of its rows once it has them
DELETE /results/{id}Cancels a run
POST /cache/invalidateStops every stored result that read the named tables of a datasource from answering
curl -s http://127.0.0.1:3699/projects/tpcds/branches/main/run \
  -H "Content-Type: application/json" \
  -d '{"expr": "month(date), web net paid"}'

The answer is the record of the run: its id, status, cache, row_count, columns and rows. Ask again and cache is hit: the stored result answers and the warehouse is not asked.

  • The wait. A request waits for the rows as long as the datasource’s query_wait_time says, ten seconds unless it says otherwise, or the request’s own wait. Ready in time, the rows are in the answer. Otherwise the answer is 202 and the id, and GET /results/{id} returns the rows a page at a time (offset, limit), or the whole result as NDJSON or CSV when Accept asks for application/x-ndjson or text/csv, gzipped when Accept-Encoding takes it.
  • The timeout. A statement runs no longer than the datasource’s query_timeout.
  • Seats and joining. Every run is recorded before anything else. ZSQL_CONCURRENT_RUNS statements run at once and up to ZSQL_QUEUE_MAX wait for a seat; past that a run is refused as busy. A semantic request identical to one already in flight joins it rather than running twice.
  • More than one node. With ZSQL_DB the records of runs live in a Postgres table every node shares, and with ZSQL_OBJECT_STORE the results live in S3, GCS, Azure or a directory, with a size-limited copy on each node. Without them a server keeps both on its own disk.
  • The cache. A planned query’s result answers the same question again until its cache_ttl passes: the request’s, else the datasource’s, else 24 hours. It is found by the org, the project and the planned statement, which already carries 0sql’s row policies; not by the user or the credential. So policies the warehouse itself applies per user are not seen: give such a datasource cache_ttl: 0, which keeps nothing and refuses (400) a request that asks to cache. "fresh": true on a request runs the statement whatever is stored, and POST /cache/invalidate with a datasource and the tables that changed stops every result that read them from answering.
  • A TTL per request. "cache_ttl" on /run (and on a blend, and the MCP run tool) is whole seconds or a number with a unit: 1800, 10s, 30m, 1h, 10d. 0 keeps nothing and -1 never expires. It works both ways: the result expires that long after it finished, and a stored result older than that does not answer the request, so a request for 30m is never handed a result another caller kept for a day. The answer says how long its result is kept, as cache_ttl in seconds or "never" (with expires_at 9999-12-31T00:00:00Z). A rerun of the same question replaces what was stored: a result that never expired ends at once, and any other is cut to expire when the rerun does.
  • Limits are the caller’s. A query with no limit returns every row, unless ZSQL_DEFAULT_SQL_LIMIT gives one to requests that have none. Rows go to disk as they arrive, so a large result costs disk and time, not memory.
  • Nothing checks that a statement is a read. The planner only emits reads, but sql-exec runs what it is sent, and no row policy applies to it. What an account may do is the database’s to enforce: deploy a credential that may only read, and do not forward sql-exec unless running arbitrary SQL is the point.

A failed run is not a failed request: run answers 200 with a record whose status is failed and an error. The API reference has each route.

Datasource credentials

The server has no setting for a warehouse credential. A datasource’s credential arrives with a deploy.

On your machine, a datasource is its entry in datasources.yml with what the gitignored .zsql holds under the same key laid over it, or ZSQL_DS_<KEY>_<FIELD> in the environment (ZSQL_DS_WAREHOUSE_PASSWORD). zsql datasource auth writes a secret to .zsql.

warehouse:
  password: ...
  • A deploy carries it. zsql deploy sends each datasource’s secrets, and on the server they are that datasource’s credential for that org, project and branch. zsql serve inside a project deploys it the same way. So whoever deploys sets who the server runs as, for every caller.
  • A request’s own replaces it whole. A run, a sql-exec or a probe that sends a secret in connection connects with exactly the secrets it sent; nothing deployed is blended in. Your application can send each user’s own credential, or send none and let every user run as the deployed account.
  • A deploy that carries none keeps the one the server has, so a pipeline can deploy a model without holding the secret. It is kept only while the datasource points where it did: a changed host, database or username drops it.
  • To take one away, zsql deploy --clear-secrets, or zsql undeploy, which removes the branch and its credential with it. To rotate one, deploy again with the new secret.
  • A validation carries none. zsql deploy --dry-run and zsql check -r send the datasources with every secret taken out.
  • A branch deployed for the first time has none. It borrows from no other branch.
  • The server’s own environment is not read. ZSQL_DS_* set on the server does nothing. The one exception is the cloud identity of the server’s process: with no secret at all, Athena falls back to its AWS identity and BigQuery to its application-default credentials. Run the server as an identity that can reach nothing you would not give every caller.

Each deploy says what became of each credential (set, kept, dropped or none), and GET /projects/{uid}/branches/{branch}/datasources names the secrets held and shows none. On the server a deployed secret is on disk, in the clear, readable by the server’s user alone (what is stored), and a deploy now carries a secret through whatever sits in front of the server: send it over TLS or a tunnel.

Securing access

The server is a component, used the way a database is. It does not know who is calling, so everything about who may call is done in front of it:

Who does it
Who may ask, and for which projectyour application or proxy
The security context a plan runs underthe caller sends it: context in the request body, the Zsql-Context header on /mcp
Rate limitsyour proxy or application
TLSyour proxy
Which credential a run connects withyour application sends one in connection, or the deployed one is used
Who may deploy, validate, test and removeyour application or proxy, or the network: the server asks nobody

What reaching the port gives a caller:

  • Every deployed model. GET /projects lists every project and branch, and the catalog of each (tables, fields, descriptions, SQL expressions, policies as compiled) is readable. There is no per-project access.
  • SQL under any context. The context is whatever the request says. A caller who can reach the server can claim system_admin, or any group, and get the statement a policy would have filtered for someone else. Row-level policies protect data through the application that sets the context and runs the SQL; they are not a defense against the network.
  • Rows. With a credential deployed, a caller needs nothing but reach to have a plan run and read the result, and to do anything that account may do through sql-exec.
  • A connection from the server’s network position. The probe routes (POST .../datasources/{ds}/tables) open a connection to whatever host the request’s connection names.
  • Deploys and removals, of any branch of any project. The log line for one names the project and branch, not who sent it.

Before 0.7, what leaked from an exposed server was the shape of your warehouse and SQL text. It is now rows, and with sql-exec and a deployed credential, writes. A port opened on the old understanding should be closed.

So, in practice:

  • Bind the loopback or a private address, never a public interface. The default is 127.0.0.1:3699.
  • Let only your application reach it. The browser, the agent and the end user talk to your application; your application authenticates them, decides which project they may ask about, builds the context from its own session, and calls 0sql. Never pass a context through from the client.
  • If something outside must reach it directly (an MCP client, a teammate’s CLI), put an authenticating reverse proxy in front: nginx or Caddy with your SSO, Cloudflare Access, a tailnet. Have it forward only the routes those callers need: refuse the four write routes, and run, sql-exec and the probe routes unless they are meant.
  • Rate limit there too. The server caps how much work runs at once and answers 503 with Retry-After when it is full, which keeps the process alive under a flood. It does not keep one caller from taking every seat.
  • Deploy an account that may only read, unless writing is the point.

No credential of its own

The server checks nothing a request carries. A deploy, a validate, a test and a removal are as open as a read, on every address it listens on, and an Authorization header is not read. Releases before 0.7 took a deploy token; that is gone. ZSQL_DEPLOY_TOKEN is ignored, zsql auth no longer exists, and ZSQL_API_KEY or an api_key left in a .zsql is not read.

So who may deploy is decided where every other question of who is decided: by your application or proxy, or by who can reach the address. Treat reach to the write routes like warehouse credentials. A model carries SQL expressions that are run with a datasource’s credential, and the row policies over them; whoever can deploy can drop the policies or choose that SQL.

Beyond the loopback

--bind with an address that is not the loopback is refused unless --open, or ZSQL_OPEN=1, says the exposure is meant:

$ zsql serve --bind 10.0.0.5:3699
error: 10.0.0.5:3699 is not a loopback address, and anyone who reaches it may deploy, remove and run statements; add --open, or set ZSQL_OPEN=1, to mean it

--open checks no caller. It is for a server other machines reach, a node of a cluster or one behind a proxy elsewhere, and nothing else then stands between that address and a deploy or a run. Bind the private interface your application is on, not 0.0.0.0, on any host that also has a public one. The usual arrangement keeps the loopback bind and puts a proxy on the same host in front:

ZSQL_PUBLIC_URL=https://0sql.internal.example.com zsql serve --bind 127.0.0.1:3699

ZSQL_PUBLIC_URL (or --public-url) is only what zsql server status reports as the address the server is reached at.

Deploying to a server elsewhere

Deploy to the server directly, the way migrations are run against a database: on a path of its own, not through whatever the application’s queries pass through.

Where you deploy fromHow it reaches the server
the same private network or VPNthe server’s private address
anywhere elsea tailnet, or ssh -L 3699:127.0.0.1:3699 <host> and the default address
hosted CIa tailnet in the workflow, or the proxy

The direct path is plain HTTP. A tailnet or an SSH tunnel encrypts it; a flat VPN or VPC may not, and then each datasource’s secret crosses it in the clear on every deploy that carries one.

With deploys on their own path, the proxy in front carries reads only and refuses POST .../deploy, .../validate, .../test and DELETE /projects/{uid}/branches/{branch}. A proxy that forwards a fixed list of read routes does this without trying. If the proxy does carry deploys, its own check is the only thing between a caller and a deploy: it forwards the write routes only for a caller it has decided may deploy. The same goes for run, sql-exec and the probe routes: forward each only for callers you would trust with what it reaches.

Orgs

A project belongs to an org, and a server may keep the projects of several. An org is a name and a directory of the data directory, nothing more: the server has no list of orgs and no idea who is in one.

  • The org is in the path. Every route about a project, a result or the cache, and the MCP endpoint, is also served under /orgs/{org}: /orgs/acme/projects/shop/branches/main/sql, /orgs/acme/results/{id}, /orgs/acme/mcp.
  • With no org in the path, the org is default. /projects/shop/branches/main/sql and /orgs/default/projects/shop/branches/main/sql are the same request.
  • zsql takes the org from org: in project.yml, or from --org, which wins.
  • Nothing crosses. A listing, a stored result, an invalidation, a deployed credential and a warehouse connection are one org’s. Two orgs may each have a project of the same uid.
  • Who may ask for which org is decided in front, like everything else. A proxy that keeps orgs apart allows each caller the one prefix /orgs/<theirs>/; GET /orgs lists every org with something deployed.

Settings

Flags configure the process at hand; ZSQL_* variables are the same settings as an environment file a deployment loads. A flag wins over its variable.

FlagVariableDefaultMeaning
--bind ADDRZSQL_BIND127.0.0.1:3699Address to listen on; port 0 takes a free one and logs it
--data-dir DIRZSQL_DATA_DIR$ZSQL_HOME/dataWhere deployed models live
--openZSQL_OPENAllow a bind beyond the loopback. It checks no caller; it says the exposure is meant
--no-projectServe only the data directory, even inside a project
--public-url URLZSQL_PUBLIC_URLthe bind addressWhere the server is reached from outside, for zsql server status
ZSQL_ADMIT_PLAN, ZSQL_ADMIT_EXPLORE, ZSQL_ADMIT_HEAVY, ZSQL_ADMIT_PROBE, ZSQL_ADMIT_PREPcores × 4, cores, 2, cores × 2, cores ÷ 2 (at least 2)How much of each kind of work runs at once
ZSQL_CONCURRENT_RUNS100How many statements run at once
ZSQL_QUEUE_MAX5,000How many runs may wait for a seat before the next is refused as busy
ZSQL_DEFAULT_SQL_LIMITnoneThe row limit a run is given when its request has none; unset is no limit
ZSQL_POOL_MAX, ZSQL_POOL_IDLE_SECS200, 60Warehouse connections kept open for reuse, and how long an idle one is kept. A connection is reused only by a run with the same datasource, settings and credential
ZSQL_OBJECT_STOREnoneWhere results are kept for every node to read: s3://bucket/prefix, gs://, az:// or file:///dir. With it set, each deploy is also kept there as a zip, without its secrets. Unset, a result is only on the disk of the node that made it. Sources and datasets are copied there too (backup and restore)
ZSQL_DATASETSonoff turns data prep off, and its routes answer 404
ZSQL_DATASET_MAX_ROWS, ZSQL_DATASET_MAX_BYTES1,000,000, 256 MiBThe most rows and bytes a snapshot, an upload or a dataset version holds
ZSQL_PREP_MEMORY, ZSQL_PREP_THREADS1GB, 2The memory and threads of the embedded engine graphs run in. See Data Prep
ZSQL_DB, ZSQL_DB_USER, ZSQL_DB_PASSWORDnoneA Postgres to keep the records of runs in, for more than one node; unset, they are kept on this node’s disk
ZSQL_LOCAL_CACHE_MAX_PERCENT60The share of the disk this node’s results may take
ZSQL_CACHE_SWEEP_SECS300How often results that no longer answer are deleted
--log FILTER, --log-format FORMATZSQL_LOG, ZSQL_LOG_FORMATinfo, plainWhat is logged, to stderr, and as plain or json
ZSQL_HOME~/.zsqlWhere the data directory, the pidfile, the log and the repl history live

Three settings belong to a datasource, in datasources.yml: query_timeout, query_wait_time and cache_ttl (seconds or 30m, 1h, 10d; 0 keeps nothing, -1 never expires).

Work is admitted by cost: planning, exploring and field search, the heavy routes (deploy, validate, test) and the probes each have a cap. A request that finds no seat within its wait (one second, five for heavy work) is answered 503 Busy with Retry-After; one that outlives its deadline (30 seconds, 90 for heavy work) is answered 503 Timeout without it. zsql waits and retries a 503 that names a delay.

What the machine needs

Linux with glibc 2.34 or newer, on x86_64 or arm64: Ubuntu 22.04, Debian 12, RHEL 9, Amazon Linux 2023, or a container based on one. ldd --version says which a machine has. The binary asks for nothing else. It does not run on Alpine or in a scratch container; through 0.7.0 it was a static build that did. macOS 11 or newer runs it too.

The ADBC drivers are optional and worth having where a datasource is Snowflake or BigQuery: with one a result arrives typed and in parallel streams, without one over REST, a page at a time, every column as text.

curl -LsSf https://dbc.columnar.tech/install.sh | sh
dbc install --level system snowflake
dbc install --level system bigquery
zsql datasource drivers

A server looks for them in ADBC_DRIVER_PATH, the ~/.config/adbc/drivers of the user it runs as, and /etc/adbc/drivers, which is where --level system puts them. Under the systemd unit below use the last: the unit hides home directories. Install the same drivers on every node, or the same datasource answers typed from one and as text from the next.

Results are written to the data directory, so give it disk: this node’s results may take 60% of it (ZSQL_LOCAL_CACHE_MAX_PERCENT). Past that, without an object store a run fails saying the disk is full; with one, the least recently read local copies are removed. A deployed model is held in memory for the life of the process, about 5 MB for one of the TPC-DS sample’s size, and nothing caps how many are deployed.

As a system service

On a host, run the same binary under systemd with the settings in an environment file. The unit below is the one the project ships in ops/zsqld.service, trimmed to what matters; the full one adds the usual hardening (no capabilities, read-only filesystem outside the state directory, a system call filter).

[Unit]
Description=0sql server
After=network-online.target
Wants=network-online.target

[Service]
User=zsqld
Group=zsqld
EnvironmentFile=/etc/zsqld.env
Environment=ZSQL_DATA_DIR=/var/lib/zsqld
Environment=ZSQL_HOME=/var/lib/zsqld
ExecStart=/usr/local/bin/zsql serve
StateDirectory=zsqld
Restart=on-failure
RestartSec=2

[Install]
WantedBy=multi-user.target
ZSQL_BIND=127.0.0.1:3699
ZSQL_PUBLIC_URL=https://0sql.internal.example.com

Install the binary, create the zsqld user, systemctl enable --now zsqld, and put your application or proxy in front of 127.0.0.1:3699. journalctl -u zsqld -n 10 shows the startup line: how many deployments loaded. Compare it with the previous one after every restart. A branch the build cannot load is logged by name with its reason and left out; the fix is to deploy that branch again from its repository. A restart reloads deployments from disk in well under a second.

Back up the data directory if you like: it holds nothing that cannot be rebuilt by deploying each branch again from its repository. A backup holds the deployed secrets and the stored results, so guard it as you would the warehouse.

What is stored

Everything the server keeps is in the data directory. Per deployed branch, under <data dir>/<org>/<project>/<branch>/ (default is the org of a deploy that names none):

  • the compiled model (tables, fields, joins, universes, policies) and each datasource’s entry as written, without its secrets
  • the branch’s tests
  • its sampled values, when the project deployed any
  • secrets.json: the secrets its datasources were deployed with, in the clear, readable by the server’s user alone; absent when there are none. No route serves one

Per run, under <data dir>/.cache/:

  • the rows of each result, as gzipped JSON lines with their layout beside them, until the result no longer answers
  • the record of each run: the statement, the spec or expression that asked for it, its status and times. Never a credential, and never the security context

With ZSQL_OBJECT_STORE set, results are also kept there, and each deploy as a zip (the project files, the compiled model and what the deploy answered, with every secret left out). With ZSQL_DB set, the records are in that Postgres.

Never stored:

  • anything a planning route is sent. sql, explain and explore plan in memory and write nothing: no spec, no context, no statement
  • a credential a request sends in connection. It is used for that run and dropped

Each deploy and each removal writes one log line naming the project and branch. A credential is never logged.

What a run stores is customer data, and so are the sampled values. A request to a planning route opens no connection and receives no row. (The zsql command line reads the warehouse too, on your own machine, for catalog, generate, sample and run; none of that passes through the server.)

Coming from an earlier release

From 0.6.10 to 0.7.2:

  • The deploy token is gone. ZSQL_DEPLOY_TOKEN is ignored and nothing says so: a server that took a token now takes a deploy from anyone who reaches it. Before upgrading, make sure the write routes are not forwarded to callers who should not deploy, or that the port is not reachable by them at all. A non-loopback bind now needs --open or ZSQL_OPEN=1.
  • Reach is rows. See securing access.
  • The data directory has orgs in it. A branch deployed before orgs is at <project>/<branch>/; the server leaves it where it is, does not serve it, and names it at startup. Stop the server and move each <project> directory into default/, or deploy each branch again.
  • A datasource’s credential comes with a deploy. ZSQL_DS_<KEY>_<FIELD> in the server’s environment is no longer read, and nothing says so: a run that relied on one is refused for want of a password until the branch is deployed again from a machine that holds the secret.
  • The Linux binary needs glibc 2.34. On Alpine or an older glibc it does not start. The installer tries a download before it replaces what is installed; anything that copies the release asset into place itself does not.

Coming from a release with accounts

Releases before 0.6.10 could run with --accounts: users, personal and query keys, sign-up, a web app with a playground, rate limits. All of it is gone.

  • ZSQL_ACCOUNTS is no longer read, and nothing stops a server that still sets it: one that authenticated every request comes back up answering anyone who reaches it. Remove it from the environment, along with ZSQL_SIGNUP, ZSQL_LIMITS, ZSQL_MAIL_*, ZSQL_TRUSTED_PROXY and ZSQL_MAX_BRANCHES_*, which are no longer read either.
  • Deployments lived under <account>/<project>/<branch>/ and are now looked for at <org>/<project>/<branch>/. Deploy each again.
  • accounts.db is not read, and nothing says so. Keep a copy if the users and audit log in it matter to you, then delete it.
  • zsk_ and zqk_ keys stop being checked. An application that sent a query key keeps working, and so does anyone else who can reach the port. A context that was bound to a query key’s grant must now be sent by the caller.
  • /app, /login, /signup, /me, /account/... and /projects/{uid}/access answer 404.

MCP

The same server speaks MCP at /mcp: an agent gets the reads as tools. The tools that run a statement, run and result, are off unless ZSQL_MCP_TOOLS names them; a model has no credential to send, so it runs as the deployed account. It is as open as the rest of the server, so the same rule applies: on the loopback for your own agent, behind your service for anyone else. See MCP.

Next steps