SQL database
env.sql — your app's own SQLite database, for data read on every request and written now and then.
env.sql is your app's own SQL database: SQLite, one database per app,
with plain SQL in and rows out. It is made for data that is read a lot
and written rarely — the pages, products and settings a site reads on
every request and an editor changes a few times a day. It says so with
write limits that answer at once rather than a slow decline.
One manifest grant turns it on, "sql": true in pilvia.app.json;
without it every call throws and says so.
Deploy with pilvia 0.2.11 or later: it carries the grant into the
manifest and has the pilvia app sql commands. 0.2.10 and earlier deploy
the app without the grant, and every call throws.
const { rows } = await env.sql.query('SELECT slug, title FROM posts WHERE topic = ?', [topic]);
const { changes, lastRowId } = await env.sql.query(
'INSERT INTO votes (post) VALUES (?)', [id]);
await env.sql.batch([
['INSERT INTO posts (topic, body) VALUES (?, ?)', [topic, body]],
['UPDATE topics SET replies = replies + 1 WHERE id = ?', [topic]],
]); // atomic: one transactionThe API
| Call | Answers |
|---|---|
query(sql, params?, { rows: 'array' }?) |
{ rows, columns, changes, lastRowId } |
batch([[sql, params?] | { sql, params? }, ...]) |
one such answer per statement, all in ONE transaction |
- Parameters travel apart from the SQL, always:
?placeholders and an array. A string, a number,null, a boolean (stored as 0 or 1), aBigIntand aUint8Arrayare values; aDateor any other object is aTypeError, so serialize it yourself. - Rows are objects keyed by column name, or arrays of values with
{ rows: 'array' }. An integer past 2⁵³ comes back as aBigInt, a blob as aUint8Array. batchis the only transaction. It commits all of its statements or none of them. There is noBEGIN … COMMITheld open across calls.- SQLite 3.54 with JSON functions and FTS5 full-text search.
ATTACH, extensions and PRAGMAs that change settings are refused; the read-only ones ORMs inspect a schema with (table_info,index_list,foreign_key_listand similar) work. - Names starting with
_pilvia_belong to the platform.
ORMs
The binding is plain SQL, so an ORM is a driver in your own bundle. Drizzle's SQLite proxy driver takes it in a few lines:
import { drizzle } from 'drizzle-orm/sqlite-proxy';
export const db = (env) =>
drizzle(async (sql, params, method) => {
const { rows } = await env.sql.query(sql, params, { rows: 'array' });
return { rows: method === 'get' ? rows[0] : rows };
});Generate migrations with drizzle-kit generate and apply them with
pilvia app sql migrations apply.
Limits
Per database:
| Limit | Value |
|---|---|
| Writes | 10 per second, bursts of 50 |
| Size of one write | 4 MiB of changed pages |
| Written per day | 256 MiB (UTC day) |
| Database size | 500 MB |
| One call | 5 seconds, 100,000 rows and 16 MiB of answer |
| SQL text | 100 KB per statement, 999 parameters |
A write past a limit fails at once with the code sql_write_limit and a
message naming the limit — for the rate, with when to retry. Nothing is
queued. Reads have no rate limit.
Errors carry a code beside the message: sql_error (SQLite refused the
statement), sql_write_limit, sql_read_only (a preview
wrote), sql_too_large (the answer), sql_timeout and sql_unavailable
(retry).
Migrations
Migrations are .sql files applied in order, each once, the way D1 does
it. The CLI applies them through the deploy plane; nothing runs at deploy
or on a request.
pilvia app sql migrations list <app> [--dir migrations] [--pattern '*.sql']
pilvia app sql migrations apply <app> [--dir migrations] [--pattern '*.sql']- A migration is named by its path under
--dirand they apply in that order, so number or date them. Drizzle's folder-per-migration layout is--pattern '*/migration.sql'. - Each file is one transaction that also records it in the table
pilvia_migrations. A file that fails leaves nothing behind, neither its first statements nor its record, andapplystops there. - Applying the same file twice is refused. A file edited after it was
applied is reported by
listand is not run again. BEGINandCOMMITin a file fail: it is a transaction already. Drizzle'sPRAGMA foreign_keys=OFFis refused too; usePRAGMA defer_foreign_keys = ONwhen a migration rebuilds a table.- Forward only. A database belongs to the app, not to a release, so a rollback runs the older code on the newer schema. Change the schema in two steps — add what the new code needs, deploy, and remove what the old code needed in a later migration.
The database exists once the app is deployed, so deploy before the first migration (a branch deploy is enough).
From the command line
pilvia app sql exec <app> "SELECT count(*) FROM posts"
pilvia app sql exec <app> --file seed.sql [--json]
pilvia app sql export <app> [--output blog.sqlite] [--force]exec runs SQL as the app's operator: one statement, or a script of
several separated by ;, in one transaction. The CLI is held to the day's
volume but not to the rate, one write may change 16 MiB, and a call may run
30 seconds.
export downloads the whole database as one SQLite file, which sqlite3
opens as it is. It is your backup and your way out.
All of pilvia app sql needs a token that may manage the app: the
operator's, or a named token that is not pinned to one
deploy target.
Previews
A preview reads production's database and never
writes to it: a write from a branch URL, or from the build URL of any
release that is not the live one, fails with sql_read_only. The build URL
of the live release IS the live release, and writes like it. Rolling back a
release does not roll back its database.
How it runs
Each database is SQLite running inside a WebAssembly sandbox in the host, never in your app's isolate, so a bug in SQLite's C code stays inside that one database. On a server of several nodes every node keeps a full copy and answers reads from it; one node at a time writes, and every commit reaches the platform's store of record before it is visible anywhere. A request that writes reads its own write back at once on whichever node it lands. The store keeps seven days of history.
How fast
Measured on the hosted platform on 2026-11-21, from a client in the same datacentre:
- A read is local. A request that reads one row by its key answered in
3.2 ms at the median, against 2.6 ms for the same app's handler with no
SQL. A page that runs three reads in one
batchanswered in 3.6 ms. - A write answers once it is durable. It commits on the database's one writer and reaches the store before it answers: 29 ms at the median and 110 ms at p99, at 10 writes a second.
- One database runs one statement at a time on each node. Reads never
wait for a lock, but they do queue behind each other, so a query's own
time bounds that database's throughput: at 3 ms a query, about 300 a
second per node. Keep the queries a request makes short — an index for
every
WHEREandORDER BYa page uses, and aLIMITon a full-text search that may match thousands of rows.