70 Commits

Author SHA1 Message Date
Pekka Enberg
0325d32770 docs: stop calling MVCC experimental
MVCC has been a supported journal mode since the experimental_mvcc flag
was renamed to mvcc. The manual, the agent guide and AGENTS.md still
described it as experimental and listed limitations that no longer hold:
indexes work, the logical log is recovered on open, and old versions are
garbage collected. The stale wording led to MVCC bugs being triaged as
low priority.

Only passive checkpointing is still behind an experimental flag, so the
docs now say that instead of blanket warnings about panics and wrong
results.
2026-09-22 20:18:46 +03:00
Claude
389186f911 core/index_method: give index methods a core-owned backing store handle
An index method that stores data used to do the work of core itself.
FTS built its internal table name, found the backing index in the
schema, kept its root page, and derived the MVCC table id from that
root at its snapshot. It also ran nested SQL DDL to create and drop the
store. The toy vector index had its own copy of the DDL path. A new
method would have copied all of this, and the MVCC binding is the part
that is easy to get wrong.

Add a `BackingSchema` that lists the tables and indexes a method owns.
A `BackingTable` is a table that core creates. A `BackingIndex` is a
`backing_btree` index on a backing table or on any existing table, with
the key layout that the method writes through it. The context creates
and drops the whole schema through an operation that the cursor steps
from `create` and `destroy`, so the nested DDL lives in core and gives
its I/O to the caller. For each index the context returns a
`BackingStore` handle. The handle binds the index to the current
transaction and snapshot one time, opens MVCC cursors under MVCC and
plain B-tree cursors under WAL, and takes the maintenance lease. The
name-based `open_index_cursor` on the context is removed.

FTS declares one internal table and one index. The toy vector index
declares its two existing indexes on the user table. Neither changes
its schema objects, so existing databases do not change.

Tests: `toy_index_backing_store_ddl_yields_real_io_on_a_deferred_backend`
in the statement lifecycle tests fails without this change, because the
toy index ran its DDL I/O inside the opcode. The toy create/destroy and
VACUUM tests check that its schema objects did not change. The FTS
unit, integration, lifecycle, and fuzz groups cover the FTS change.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Qq7sftGL6RXjxsx6Wf5CfX
2026-09-10 04:54:53 +00:00
Avinash Sajjanshetty
7299bbcf71 add codec documentation 2026-08-03 23:13:14 +05:30
Pekka Enberg
1287c62374 docs: note CDC commit record behavior for empty transactions
An explicit transaction that captures no changes commits without writing
a COMMIT record, while an autocommit statement that changes no rows
still writes one with change_txn_id = -1. Document both cases so users
are not surprised by missing boundaries or the -1 transaction id.
2026-07-28 09:25:24 +03:00
Pekka Enberg
ea4669f67c docs: document CDC commit records and change_txn_id
CDC v2 emits a COMMIT record (change_type=2, all data columns NULL) at
every transaction boundary and stamps each record with a change_txn_id,
but neither the CLI manual nor the database manual mentioned them. Users
reading the manual mistook the commit records for corrupt empty rows.

Also correct two inaccurate claims: CDC rows are written in the same
transaction as the DML (visible before commit, discarded on rollback),
and changes to the CDC table itself are never captured.

Fixes #7815
2026-07-13 13:53:53 +03:00
Glauber Costa
b9503e7d0b feat: add PostgreSQL-style sequences and MVCC-safe AUTOINCREMENT
Add CREATE SEQUENCE / DROP SEQUENCE / nextval() / setval() / currval()
as first-class schema objects, and reimplement AUTOINCREMENT on top of
the same infrastructure so it works correctly under MVCC.

Sequences are the standard mechanism for generating monotonically
increasing (or decreasing) identifiers. This commit adds full support
for user-created sequences (CREATE SEQUENCE name [INCREMENT BY n]
[MINVALUE n] [MAXVALUE n] [START n] [CYCLE | NO CYCLE]) and the
functions nextval(), setval(), and currval(). AUTOINCREMENT tables now
implicitly create a sequence named __turso_internal_autoincrement_<table>
so high-water-mark logic is unified across both features.

Design: disk is the source of truth
-----------------------------------

Every sequence is backed by a hidden B-tree table:

    CREATE TABLE "__turso_internal_seq_<name>"(
        value INTEGER PRIMARY KEY,    -- rowid alias = the sequence value
        is_called INTEGER,
        start INTEGER, inc INTEGER,
        min INTEGER, max INTEGER, cycle INTEGER)

`value` is INTEGER PRIMARY KEY (the rowid alias). The B-tree is keyed
by the sequence value, so MIN(value) / MAX(value) is an O(1) seek to
the first/last leaf -- the B-tree *is* the watermark.

The in-memory `Sequence` is pure schema data -- start, increment, min,
max, cycle. No AtomicI64, no is_called flag, no dirty bit, no
in-process current-value cache. This is a deliberate change from an
earlier in-memory-atomic design which couldn't be made safe across
processes (multiple workers on the same disk would each maintain their
own atomic and diverge). Reading from disk on every call is the only
design that survives multiprocess WAL, MVCC, and crash-recovery
uniformly.

nextval execution
-----------------

At compile time, nextval() emits `begin_write_on_database`, making the
statement a write transaction. At runtime the translator emits an RMW
sequence (see core/translate/sequence.rs::emit_disk_read_nextval):

1. SequenceBeginInnerTx -- under MVCC, open an autonomous inner
   Concurrent tx scoped to this RMW. The outer tx (if any) is saved
   off. Under WAL or when the outer tx is exclusive, this is a no-op
   -- the RMW runs in the outer tx; the single-writer WAL lock
   provides the same serialization the inner tx provides under MVCC.
2. Cursor RMW: Rewind/Last on the backing table -> read the single
   watermark row -> SequenceComputeNext opcode computes the next value
   from (current, is_called, start, inc, min, max, cycle) baked into
   the program -> Insert the new row keyed by the new value (since
   `value` is INTEGER PRIMARY KEY, distinct nextvals land on distinct
   B-tree keys and never key-conflict).
3. Inline backing-table compaction: a small loop emitted right after
   the insert deletes every row except the new watermark. After every
   successful nextval the backing table holds exactly one row. (This
   used to run at commit time via flush_dirty_sequences, but the
   implementation issued nested prepare_internal() calls that drove
   pager.io.step() synchronously and broke the vdbe async contract --
   the outer Statement::step() would block instead of returning
   StepResult::IO. Moving the compaction inline fixed that.)
4. For AUTOINCREMENT-backing sequences, mirror the watermark into
   sqlite_sequence in the same inner tx so SQLite-compat readers see
   the high-water mark without needing a checkpoint.
5. SequenceCommitInnerTx -- commit the inner tx. On WriteWriteConflict
   (against another nextval racing on the same backing table),
   op_sequence_commit_inner_tx retries the whole RMW with a bounded
   budget (ProgramState::sequence_inner_retry_count); when the budget
   is exhausted it surfaces LimboError::Busy so the caller can retry
   at their level. The outer tx (if any) is restored.

Why an inner tx under MVCC: a nextval that aborts because its outer tx
rolls back must still leave its watermark advance durable on disk --
otherwise a parallel reader could observe the same value twice (the
first reader sees the in-memory state, the rolled-back tx releases its
disk write, the next reader recomputes and lands on the same value).
The autonomous inner tx commits the backing-table write independent of
the outer tx, so a rolled-back nextval still "consumed" the value.
This is the standard PostgreSQL semantics (rollback does not reclaim
nextval'd values; gaps are expected).

setval
------

setval(seq, value) can place the watermark anywhere, which cannot be
expressed as a conflict-free append (two concurrent setval(s, 5)
followed by setval(s, 3) are not commutative). It requires an
exclusive write transaction and is rejected inside BEGIN CONCURRENT.

currval
-------

currval(seq) is per-connection. It returns the value most recently
emitted by nextval or setval on this connection. It's a register
lookup populated by the SetSequenceCurrval opcode at the tail of
every nextval/setval; it does not touch disk.

Restart / recovery
------------------

On database open, sequences are initialized from disk:
- User sequences: read the single watermark row from each
  __turso_internal_seq_* backing table.
- AUTOINCREMENT: read sqlite_sequence and seed the implicit sequence
  to that value.

If a crash occurs after a nextval committed but before its compaction
loop deleted the prior row, the backing table may briefly contain
multiple rows. The compaction always re-runs on the next nextval, and
the open-time read uses MAX(value) for ascending or MIN(value) for
descending sequences, so multi-row state is recovered cleanly.

The MVCC checkpoint state machine also rewrites sqlite_sequence from
the in-schema sequence list during checkpoint, ensuring the on-disk
autoinc state is clean after every checkpoint cycle.

Testing
-------

- Integration tests covering all sequence operations, concurrent
  nextval from multiple connections, AUTOINCREMENT interaction, ATTACH
  database sequences, transaction rollback semantics, descending and
  CYCLE sequences, and restart-recovery scenarios.
- Simulator (limbo_sim): sequence-aware query generation
  (CREATE/DROP SEQUENCE, nextval, setval) and a SequenceMonotonicity
  property -- creates a fresh sequence in a reserved namespace, runs N
  nextvals, asserts the engine returned start + k*increment (or
  correct CYCLE wrap).
- Concurrent simulator (whopper): dedicated sequence workloads
  testing MVCC concurrent nextval, setval exclusivity, in-flight
  setval that survives worker death, autocommit and in-tx nextval
  interleavings, CYCLE wrap-aware duplicate / wrong-direction
  tracking, and cross-checkpoint persistence. Coverage includes
  InsertSeqDefault (INSERT into table with DEFAULT (nextval('s'))) and
  tracker handling for sequences advanced through that opaque path.
- SQL tests (sqltest): sequence.sqltest, mvcc_sequence.sqltest, and
  attach/sequence.sqltest covering the full feature surface.

Closes #5688
2026-06-08 15:25:05 -04:00
Josh Lyon
af00a616d7 docs: fix javascript documentation reference
* `docs/` prefix was unnecessary as it's a sibling file in the same current `docs/` directory as the `manual.md`
2026-05-05 16:16:47 -05:00
Pekka Enberg
ab3da1f042 docs/manual.md: Add link to UTF-8 issue 2026-04-29 12:44:56 +03:00
Marc-André Moreau
67732e2c6d Add Windows multiprocess WAL support
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-04-22 15:02:58 -04:00
PThorpe92
aee50010e4 Gate cross-process access to the same .db file behind DatabaseOpts flag 2026-04-20 13:52:06 -04:00
Pekka Enberg
3861ffcc20 Merge 'core/mvcc: disable autoincrement for MVCC' from Pere Diaz Bou
* Disable AUTOINCREMENT under MVCC mode as it fails due to
`sqlite_sequence` table write-write conflicts with concurrent writers.
* Add few tests that track these failures if we were to enable again.

Closes #5687
2026-03-04 12:08:16 +02:00
Pere Diaz Bou
1067980bc3 core/mvcc: disable autoincrement 2026-03-02 14:18:22 +01:00
Eren Turkoglu
366e3572a7 Rename experimental_mvcc to mvcc 2026-02-27 14:12:38 +01:00
Nikita Sivukhin
63683b7db0 add stable PRAGMA capture_data_changes_conn name and keep old one around for compatibility 2026-02-25 20:13:06 +01:00
Glauber Costa
17ffe6ff99 make strict mode non-experimental
Strict mode has been tested extensively with the differential fuzzer
and is no more buggy than the baseline. Remove the experimental gate
so it is always enabled.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-24 17:51:43 -06:00
Nikita Sivukhin
c43256345c Document vector8, vector1bit, and vector_distance_dot in manual
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-15 13:01:51 +01:00
Jussi Saurio
29cc972c41 docs: split AGENTS.MD into separate agent-guides files 2026-01-15 10:24:43 +02:00
PThorpe92
fafac9a4aa Add MATCH syntax to fts docs 2026-01-13 20:18:22 -05:00
PThorpe92
c43a8caa28 Add FTS to README and docs/manual.md 2026-01-13 20:18:20 -05:00
Pekka Enberg
edd45ff7b8 Improve MVCC DX by dropping --experimental-mvcc flag
The DX is right now pretty terrible:

```
penberg@vonneumann turso % cargo run -- hello.db
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.15s
     Running `target/debug/tursodb hello.db`
Turso v0.4.0-pre.18
Enter ".help" for usage hints.
Did you know that Turso supports live materialized views? Type .manual materialized-views to learn more.
This software is in BETA, use caution with production data and ensure you have backups.
turso> PRAGMA journal_mode = 'experimental_mvcc';
  × Invalid argument supplied: MVCC is not enabled. Enable it with `--experimental-mvcc` flag in the CLI or by setting the MVCC option in `DatabaseOpts`

turso>
```

To add insult to the injury, many SDKs don't even have a way to enable
MVCC via database options. Therefore, let's remove the flag altogether.
2025-12-19 12:59:42 +02:00
pedrocarlo
fc6ce8920a add section to docs/manual.md 2025-12-17 10:55:25 -03:00
Pekka Enberg
194d43edde Add explanation for concurrent transactions
Make it explicit that Turso -- like most databases -- allows a single
active transaction per connection at a time. If you need concurrency,
you need multiple connections, even with BEGIN CONCURRENT.
2025-12-06 09:44:25 +02:00
Nikita Sivukhin
94a39cef3a add some docs for index method 2025-11-03 16:40:18 +04:00
Pekka Enberg
ff83b8218a docs: Add vector search section to database manual 2025-10-24 11:04:47 +03:00
Yevhen Kostryka
08efce510e Fix typo in manual.md
In `SELECT` section there is `GROU BY` instead of `GROUP BY`
2025-10-13 18:45:31 +03:00
Pekka Enberg
295a6fe6a1 docs: Explain BEGIN CONCURRENT 2025-10-03 10:24:28 +03:00
Pekka Enberg
7c83ebca41 docs: Document more CLI command line options 2025-10-03 09:35:57 +03:00
Pekka Enberg
6d06dce2bb docs: Add section on MVCC limitations
Document limitations in MVCC pointed out by Jussi.
2025-10-02 18:49:12 +03:00
Jussi Saurio
a9d782e319 Merge 'Add encryption internals docs' from Avinash Sajjanshetty
preview - https://github.com/tursodatabase/turso/blob/8d2ef700c9b087a7e2
904c25052e4365395b33b3/docs/manual.md#encryption-1

Closes #3461
2025-10-02 07:04:16 +03:00
Avinash Sajjanshetty
ca0d738f4d Add encryption internals docs 2025-10-02 00:14:28 +05:30
Avinash Sajjanshetty
eb438237fe update documentation 2025-09-30 19:15:48 +05:30
rajajisai
89caa868f9 Encryption support for database header page 2025-09-16 10:04:30 -04:00
Pekka Enberg
656b0af1be Document DEFERRED and IMMEDIATE transaction modes 2025-09-10 19:39:58 +03:00
Pekka Enberg
a2a5b852b8 Document query result ordering limitation 2025-09-08 14:19:48 +03:00
Pavan-Nambi
b343b79613 add details about table columns and how cdc behaves with transactions and stuff 2025-09-01 18:56:59 +05:30
Pavan Nambi
668213888d Update docs/manual.md
Co-authored-by: Jamie Barton <jamie@notrab.dev>
2025-09-01 18:16:53 +05:30
Pavan Nambi
5a6db2c2d5 Update docs/manual.md
Co-authored-by: Jamie Barton <jamie@notrab.dev>
2025-09-01 18:16:40 +05:30
Pavan Nambi
6d6e8d2944 Update docs/manual.md
Co-authored-by: Jamie Barton <jamie@notrab.dev>
2025-09-01 18:16:29 +05:30
Pavan Nambi
fa51256d4f Update docs/manual.md
Co-authored-by: Jamie Barton <jamie@notrab.dev>
2025-09-01 18:16:18 +05:30
Pavan Nambi
826c6c7ee6 Update docs/manual.md
Co-authored-by: Jamie Barton <jamie@notrab.dev>
2025-09-01 16:18:23 +05:30
Pavan Nambi
2fb2366105 Update docs/manual.md
Co-authored-by: Jamie Barton <jamie@notrab.dev>
2025-09-01 16:18:16 +05:30
Pavan Nambi
4538455fe4 Update docs/manual.md
Co-authored-by: Jamie Barton <jamie@notrab.dev>
2025-09-01 16:05:53 +05:30
Pavan Nambi
d969902809 Update docs/manual.md
Co-authored-by: Jamie Barton <jamie@notrab.dev>
2025-09-01 16:05:46 +05:30
Pavan Nambi
98f6ed6b61 Update docs/manual.md
Co-authored-by: Jamie Barton <jamie@notrab.dev>
2025-09-01 16:05:39 +05:30
Pavan Nambi
3aa85a46e0 Update docs/manual.md
Co-authored-by: Jamie Barton <jamie@notrab.dev>
2025-09-01 16:05:32 +05:30
Pavan Nambi
7a26351efc Update docs/manual.md
Co-authored-by: Jamie Barton <jamie@notrab.dev>
2025-09-01 16:05:23 +05:30
Pavan Nambi
635720ed74 Update docs/manual.md
Co-authored-by: Jamie Barton <jamie@notrab.dev>
2025-09-01 16:05:15 +05:30
Pavan-Nambi
b122e0cc0e add docs to cdc 2025-09-01 13:31:21 +05:30
Avinash Sajjanshetty
4d7b4bb711 Add some docs on encryption 2025-08-26 23:11:48 +05:30
Pekka Enberg
aaf7b39d96 Update manual.md 2025-08-13 18:52:14 +03:00