Files
turso/docs/sql-reference/pragmas.mdx
Iann29 d195edd38c core: implement PRAGMA wal_autocheckpoint
Applications that run checkpoints themselves, or that want the WAL to
grow more or less than 1000 pages, set PRAGMA wal_autocheckpoint as
SQLite documents it. Turso ignored the pragma as an unknown name, so a
connection that turned automatic checkpoints off still got one after
any commit that left more than 1000 frames in the WAL.

PRAGMA wal_autocheckpoint returns the connection's threshold, 1000 by
default. PRAGMA wal_autocheckpoint = N sets it for this connection only
and returns the new value. Zero, a negative value, or a value too large
for a 32-bit int turns automatic checkpoints off and reads back as 0,
as in SQLite.

Closing the last connection still checkpoints the WAL: SQLite runs that
checkpoint whatever this setting is. Like SQLite, the pragma has no
pragma_wal_autocheckpoint table-valued function.

Tests: cargo test -p core_tester --test integration_tests pragma_wal_autocheckpoint
Tests: sqltest run sqlite-sqltests/pragma/memory.sqltest -f 'pragma-wal-autocheckpoint-*'
2026-10-03 12:17:13 -03:00

583 lines
14 KiB
Plaintext

---
title: PRAGMA Statements
description: Database configuration and metadata commands in Turso
sidebarTitle: PRAGMAs
---
# PRAGMA Statements
PRAGMA statements are special commands used to query or modify database configuration, retrieve metadata, and control database behavior. Unlike standard SQL, PRAGMAs are specific to SQLite and Turso.
## Syntax
```sql
PRAGMA pragma-name;
PRAGMA pragma-name = value;
PRAGMA pragma-name(value);
```
## Database Metadata
### database_list
Returns one row for each attached database.
```sql
PRAGMA database_list;
-- seq | name | file
-- 0 | main | /path/to/database.db
```
### page_count
Returns the total number of pages in the database file.
```sql
PRAGMA page_count;
-- 42
```
### page_size
Returns or sets the page size of the database. The page size can only be set before any tables are created.
```sql
PRAGMA page_size;
-- 4096
PRAGMA page_size = 8192;
```
### max_page_count
Returns or sets the maximum number of pages allowed in the database file.
```sql
PRAGMA max_page_count;
PRAGMA max_page_count = 1000000;
```
### freelist_count
Returns the number of unused pages in the database file.
```sql
PRAGMA freelist_count;
```
### encoding
Returns the text encoding used by the database.
```sql
PRAGMA encoding;
-- UTF-8
```
### schema_version
Returns the schema version number. This value is incremented each time the schema changes.
```sql
PRAGMA schema_version;
-- 5
```
### application_id
Returns or sets the application ID stored in the database header. Applications can use this 32-bit integer to identify the database file format.
```sql
PRAGMA application_id;
PRAGMA application_id = 12345;
```
### user_version
Returns or sets the user version number. This is a 32-bit integer available for application use.
```sql
PRAGMA user_version;
PRAGMA user_version = 3;
```
## Schema Introspection
### table_info
Returns one row for each column in the named table.
```sql
PRAGMA table_info(table-name);
```
| Column | Type | Description |
|--------|------|-------------|
| cid | INTEGER | Column index (0-based) |
| name | TEXT | Column name |
| type | TEXT | Declared type name |
| notnull | INTEGER | 1 if NOT NULL constraint exists |
| dflt_value | TEXT | Default value expression, or NULL |
| pk | INTEGER | 1 if column is part of the PRIMARY KEY |
```sql
CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT NOT NULL, email TEXT);
PRAGMA table_info(users);
-- cid | name | type | notnull | dflt_value | pk
-- 0 | id | INTEGER | 0 | | 1
-- 1 | name | TEXT | 1 | | 0
-- 2 | email | TEXT | 0 | | 0
```
### table_xinfo
Similar to `table_info` but also includes hidden columns and additional metadata.
```sql
PRAGMA table_xinfo(table-name);
```
Returns the same columns as `table_info` plus a `hidden` column (0 for normal columns, non-zero for hidden columns in virtual tables).
### table_list
Returns one row for each table and view in the database.
```sql
PRAGMA table_list;
-- schema | name | type | ncol | wr | strict
-- main | users | table | 3 | 0 | 0
-- main | orders | table | 4 | 0 | 0
```
### index_list
Returns one row for each index on the named table.
```sql
PRAGMA index_list(table-name);
-- seq | name | unique | origin | partial
-- 0 | idx_email | 1 | c | 0
```
| Column | Type | Description |
|--------|------|-------------|
| seq | INTEGER | Index sequence number |
| name | TEXT | Index name |
| unique | INTEGER | 1 if the index is UNIQUE |
| origin | TEXT | `c` for CREATE INDEX, `u` for UNIQUE constraint, `pk` for PRIMARY KEY |
| partial | INTEGER | 1 if the index is a partial index |
### index_info
Returns one row for each column in the named index.
```sql
PRAGMA index_info(index-name);
-- seqno | cid | name
-- 0 | 2 | email
```
### index_xinfo
Similar to `index_info` but includes additional columns.
```sql
PRAGMA index_xinfo(index-name);
```
### function_list
Returns one row for each SQL function available.
```sql
PRAGMA function_list;
-- name | builtin | type | enc | narg | flags
```
### pragma_list
Returns the list of all supported PRAGMA commands.
```sql
PRAGMA pragma_list;
```
## Database Configuration
### journal_mode
Returns or sets the journal mode.
```sql
PRAGMA journal_mode;
-- wal
PRAGMA journal_mode = wal;
```
Turso supports WAL (Write-Ahead Logging) mode. Rollback journal modes (DELETE, TRUNCATE, PERSIST, MEMORY) are not supported.
<Info>
**Turso Extension**: Turso supports an experimental MVCC journal mode for concurrent writes:
```sql
PRAGMA journal_mode = mvcc;
```
When MVCC mode is active, you can use `BEGIN CONCURRENT` for optimistic concurrent write transactions. See [Transactions](/docs/sql-reference/statements/transactions#begin-concurrent) for details.
</Info>
### cache_size
Returns or sets the suggested maximum number of database pages held in memory.
```sql
PRAGMA cache_size;
PRAGMA cache_size = 2000; -- positive: number of pages
PRAGMA cache_size = -2000; -- negative: kilobytes of memory
```
### cache_spill
Enables or disables cache spilling (writing dirty pages to the WAL before the cache is full).
```sql
PRAGMA cache_spill;
PRAGMA cache_spill = 0; -- disable
PRAGMA cache_spill = 1; -- enable
```
### synchronous
Controls the fsync behavior for durability guarantees.
```sql
PRAGMA synchronous;
PRAGMA synchronous = OFF; -- no fsync (fastest, risk of corruption on crash)
PRAGMA synchronous = FULL; -- fsync after every transaction (safest)
```
Only `OFF` and `FULL` are supported in Turso.
### temp_store
Controls where temporary tables and indexes are stored.
```sql
PRAGMA temp_store;
PRAGMA temp_store = 0; -- DEFAULT
PRAGMA temp_store = 1; -- FILE
PRAGMA temp_store = 2; -- MEMORY
```
### busy_timeout
Sets the busy timeout in milliseconds. When a table is locked, Turso waits up to this many milliseconds before returning SQLITE_BUSY.
```sql
PRAGMA busy_timeout;
PRAGMA busy_timeout = 5000; -- 5 seconds
```
### query_only
When enabled, prevents any changes to the database.
```sql
PRAGMA query_only = 1; -- enable read-only mode
PRAGMA query_only = 0; -- disable read-only mode
```
### foreign_keys
Enables or disables foreign key constraint enforcement.
```sql
PRAGMA foreign_keys;
PRAGMA foreign_keys = ON;
PRAGMA foreign_keys = OFF;
```
Foreign key enforcement is off by default for SQLite compatibility.
### legacy_file_format
Returns the legacy file format flag.
```sql
PRAGMA legacy_file_format;
```
### ignore_check_constraints
When enabled, CHECK constraints are not enforced.
```sql
PRAGMA ignore_check_constraints = 1; -- disable CHECK constraints
PRAGMA ignore_check_constraints = 0; -- enable CHECK constraints
```
### data_sync_retry
Controls whether Turso retries a disk sync (fsync) after a failure instead of treating it as fatal.
```sql
PRAGMA data_sync_retry = 1; -- retry on sync failure
PRAGMA data_sync_retry = 0; -- do not retry (default)
```
### require_where
<Info>
**Turso Extension**: a safety pragma with no SQLite equivalent.
</Info>
When enabled, Turso rejects any `UPDATE` or `DELETE` that does not include a `WHERE` clause, guarding against accidental full-table modifications. `i_am_a_dummy` is an alias that enables the same behavior.
```sql
PRAGMA require_where = 1;
DELETE FROM users; -- rejected: no WHERE clause
DELETE FROM users WHERE id = 1; -- allowed
PRAGMA i_am_a_dummy = 1; -- equivalent to require_where = 1
```
## Integrity Checks
### integrity_check
Performs a thorough integrity check of the entire database.
```sql
PRAGMA integrity_check;
-- ok
PRAGMA integrity_check(N); -- check only the first N errors
```
Returns `ok` if no problems are found, otherwise returns one row per error.
### quick_check
Performs a faster but less thorough integrity check than `integrity_check`.
```sql
PRAGMA quick_check;
```
## WAL Operations
### wal_checkpoint
Forces a WAL checkpoint.
```sql
PRAGMA wal_checkpoint;
```
A checkpoint writes pages from the WAL file back to the database file.
### wal_autocheckpoint
Returns or sets how many pages the WAL can hold before a commit on this connection runs a checkpoint. The default is 1000 pages. Zero or a negative value turns automatic checkpoints off.
```sql
PRAGMA wal_autocheckpoint;
PRAGMA wal_autocheckpoint = 1000;
PRAGMA wal_autocheckpoint = 0; -- turn automatic checkpoints off
```
The setting applies only to the connection that sets it. Closing the last connection to the database still checkpoints the WAL. In MVCC mode the setting has no effect; use `mvcc_checkpoint_threshold` instead.
## MVCC Tuning
<Info>
**Turso Extension**: these pragmas apply only when MVCC mode is enabled (`PRAGMA journal_mode = mvcc`).
</Info>
### mvcc_checkpoint_threshold
Sets the amount of committed work that accumulates before Turso triggers an MVCC checkpoint.
```sql
PRAGMA mvcc_checkpoint_threshold = 1000;
```
### mvcc_gc_threshold
Sets the threshold that controls how aggressively Turso garbage-collects obsolete MVCC row versions.
```sql
PRAGMA mvcc_gc_threshold = 1000;
```
### mvcc_group_commit
Lets concurrent MVCC commits batch their logical-log writes so one of them appends
every waiting transaction's record and the whole batch shares a single fsync.
Off by default.
Exclusive transactions started with `BEGIN IMMEDIATE` or `BEGIN EXCLUSIVE` commit
on the solo path and do not join the queue. The setting applies to every
connection on the store.
```sql
PRAGMA mvcc_group_commit = on;
```
## Change Data Capture
<Info>
**Turso Extension**: Change Data Capture (CDC) is a Turso-specific feature that tracks all data changes for replication, auditing, and reactive applications.
</Info>
### capture_data_changes_conn
Enables CDC for the current connection. Changes are captured to a designated table.
```sql
PRAGMA capture_data_changes_conn('mode');
PRAGMA capture_data_changes_conn('mode,table_name');
```
The legacy name `unstable_capture_data_changes_conn` is still accepted for backwards compatibility.
| Parameter | Description |
|-----------|-------------|
| mode | Capture mode: `off`, `id`, `before`, `after`, `full` |
| table_name | Custom table name for storing changes (default: `turso_cdc`) |
#### Capture Modes
| Mode | Description |
|------|-------------|
| `off` | Disable CDC for this connection |
| `id` | Capture only the primary key/rowid of changed rows |
| `before` | Capture row state before changes (for updates/deletes) |
| `after` | Capture row state after changes (for inserts/updates) |
| `full` | Capture both before and after states, plus update details |
#### CDC Table Structure
The CDC table contains the following columns:
| Column | Type | Description |
|--------|------|-------------|
| change_id | INTEGER | Auto-incrementing unique identifier |
| change_time | INTEGER | Unix epoch timestamp |
| change_type | INTEGER | 1 (INSERT), 0 (UPDATE), -1 (DELETE) |
| table_name | TEXT | Name of the changed table |
| id | varies | Primary key/rowid of the changed row |
| before | BLOB | Row data before the change (modes: before, full) |
| after | BLOB | Row data after the change (modes: after, full) |
| updates | BLOB | Updated column details (mode: full) |
#### CDC Examples
```sql
-- Enable full CDC
PRAGMA capture_data_changes_conn('full');
-- Make changes
CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT);
INSERT INTO users VALUES (1, 'Alice');
UPDATE users SET name = 'Alicia' WHERE id = 1;
DELETE FROM users WHERE id = 1;
-- Query the changes
SELECT change_type, table_name, id FROM turso_cdc;
-- 1 | users | 1 (INSERT)
-- 0 | users | 1 (UPDATE)
-- -1 | users | 1 (DELETE)
-- Use a custom table name
PRAGMA capture_data_changes_conn('full,audit_log');
-- Disable CDC
PRAGMA capture_data_changes_conn('off');
```
CDC respects transaction boundaries. Changes are only recorded when a transaction commits. If a transaction rolls back, no CDC entries are created.
## Encryption
<Info>
**Turso Extension**: At-rest encryption is a Turso-specific feature. This feature is experimental and must be [enabled before use](/docs/sql-reference/experimental-features).
</Info>
### cipher
Sets the encryption cipher for the database.
```sql
PRAGMA cipher = 'aegis256';
```
Supported ciphers:
| Cipher | Key Size | Description |
|--------|----------|-------------|
| `aes128gcm` | 16 bytes | AES-128 in Galois/Counter Mode |
| `aes256gcm` | 32 bytes | AES-256 in Galois/Counter Mode |
| `aegis128l` | 16 bytes | AEGIS-128L |
| `aegis256` | 32 bytes | AEGIS-256 (recommended) |
| `aegis128x2` | 16 bytes | AEGIS-128 with 2x parallelization |
| `aegis128x4` | 16 bytes | AEGIS-128 with 4x parallelization |
| `aegis256x2` | 32 bytes | AEGIS-256 with 2x parallelization |
| `aegis256x4` | 32 bytes | AEGIS-256 with 4x parallelization |
### hexkey
Sets the encryption key as a hexadecimal string.
```sql
PRAGMA hexkey = '2d7a30108d3eb3e45c90a732041fe54778bdcf707c76749fab7da335d1b39c1d';
```
#### Encryption Example
```sql
-- Set cipher and key before creating tables
PRAGMA cipher = 'aegis256';
PRAGMA hexkey = '2d7a30108d3eb3e45c90a732041fe54778bdcf707c76749fab7da335d1b39c1d';
CREATE TABLE secrets (id INTEGER PRIMARY KEY, data TEXT);
INSERT INTO secrets VALUES (1, 'sensitive data');
```
Alternatively, specify encryption parameters in the database URI:
```
file:database.db?cipher=aegis256&hexkey=2d7a30...
```
To open an existing encrypted database, the cipher and key must be provided as URI parameters.
## Custom Types
<Info>
**Turso Extension**: Custom types are a Turso-specific feature.
</Info>
### list_types
Lists all available types (built-in and custom) with their metadata.
```sql
PRAGMA list_types;
-- type | parent | encode | decode | default | operators
-- INTEGER | | | | |
-- REAL | | | | |
-- TEXT | | | | |
-- BLOB | | | | |
-- ANY | | | | |
```
See [CREATE TYPE](/docs/sql-reference/statements/create-type) for creating custom types.
## See Also
- [Transactions](/docs/sql-reference/statements/transactions) for transaction control
- [CREATE TYPE](/docs/sql-reference/statements/create-type) for custom type definitions
- [Compatibility](/docs/sql-reference/compatibility) for the full list of supported PRAGMAs