Commit Graph

8732 Commits

Author SHA1 Message Date
TcMits
5dddc5e00b introduce OP_Explain 2025-09-12 17:31:50 +07:00
Pekka Enberg
aa32574554 core/mvcc: Fix begin_exclusive_tx()
The RwLock elimination patches conflicted with the BEGIN CONCURRENT
changes.
2025-09-12 08:42:14 +03:00
Pekka Enberg
a9a48f6272 Merge 'core/schema: Optimize get_dependent_materialized_views() when no views' from Pekka Enberg
Eliminates get_dependent_materialized_views() overhead when there are no
views. Note that we need to optimize the case when there are views as
well because this ends up being pretty hot in write-intensive workloads.

Closes #3046
2025-09-12 08:29:24 +03:00
Pekka Enberg
a349e7684a Merge 'perf: Add simple throughput benchmark' from Pekka Enberg
This adds a simple throughput benchmark for rusqlite and Turso, allowing
to compare the two, but also MVCC and SQLite transactions.

Closes #3047
2025-09-12 08:29:14 +03:00
Pekka Enberg
06371d8894 Merge 'Add BEGIN CONCURRENT support for MVCC mode' from Pekka Enberg
Currently, when MVCC is enabled, every transaction mode supports
concurrent reads and writes, which makes it hard to adopt for existing
applications that use `BEGIN DEFERRED` or `BEGIN IMMEDIATE`.
Therefore, add support for `BEGIN CONCURRENT` transactions when MVCC is
enabled. The transaction mode allows multiple concurrent read/write
transactions that don't block each other, with conflicts resolved at
commit time. Furthermore, implement the correct semantics for `BEGIN
DEFERRED` and `BEGIN IMMEDIATE` by taking advantage of the pager level
write lock when transaction upgrades to write. This means that now
concurrent MVCC transactions are serialized against the legacy ones when
needed.
The implementation includes:
- Parser support for CONCURRENT keyword in BEGIN statements
- New Concurrent variant in TransactionMode to distinguish from regular
read/write transactions
- MVCC store tracking of exclusive transactions to support IMMEDIATE and
EXCLUSIVE modes alongside CONCURRENT
- Proper transaction state management for all transaction types in MVCC
This enables better concurrency for applications that can handle
optimistic concurrency control, while still supporting traditional
SQLite transaction semantics via IMMEDIATE and EXCLUSIVE modes.

Reviewed-by: Pere Diaz Bou <pere-altea@homail.com>

Closes #3021
2025-09-12 07:38:53 +03:00
Pekka Enberg
964dd0cd43 perf: Add simple throughput benchmark
This adds a simple throughput benchmark for rusqlite and Turso, allowing
to compare the two, but also MVCC and SQLite transactions.
2025-09-12 07:35:57 +03:00
Pekka Enberg
d80814fa2c core/schema: Optimize get_dependent_materialized_views() when no views
Eliminates get_dependent_materialized_views() overhead when there are no
views. Note that we need to optimize the case when there are views as
well because this ends up being pretty hot in write-intensive workloads.
2025-09-12 07:22:18 +03:00
Preston Thorpe
f9f7a44955 Merge 'add explicit usize type annotation to range iterator in test' from Denizhan Dakılır
very small fix when i was reading the codebase with rust-analyser while
trying to find a bug for simulator.
original error:
`non-primitive cast: <Range<i32> as Iterator>::Item as i32 rust-analyzer
E0605`

Reviewed-by: Preston Thorpe <preston@turso.tech>

Closes #3043
2025-09-11 21:12:32 -04:00
Denizhan Dakılır
70102f5f6e add explicit usize type annotation to range iterator in test 2025-09-12 02:18:49 +03:00
Preston Thorpe
e9944f5d1f Merge 'Fix automatic indexes' from Jussi Saurio
Closes #2993
## Background
When a `CREATE TABLE` statement specifies constraints like `col UNIQUE`,
`col PRIMARY KEY`, `UNIQUE (col1, col2)`, `PRIMARY KEY(col3, col4)`,
SQLite creates indexes for these constraints automatically with the
naming scheme `sqlite_autoindex_<table_name>_<increasing_number>`.
## Problem
SQLite expects these indexes to be created in table definition order.
For example:
```sql
CREATE TABLE t(x UNIQUE, y PRIMARY KEY, c, d, UNIQUE(c,d));
```
Should result in:
```sql
sqlite_autoindex_t_1 -- x UNIQUE
sqlite_autoindex_t_2 -- y PRIMARY KEY
sqlite_autoindex_t_3-- UNIQUE(c,d)
```
However, `tursodb` currently doesn't uphold this invariant -- for
example: the PRIMARY KEY index is always constructed first. SQLite flags
this as a corruption error (see #2993).
## Solution
- Process "unique sets" in table definition order. "Unique sets" are
groups of 1-n columns that are part of either a UNIQUE or a PRIMARY KEY
constraint.
- Deduplicate unique sets properly: a PRIMARY KEY of a rowid alias
(INTEGER PRIMARY KEY) is not a unique set. `UNIQUE (a desc, b)` and
`PRIMARY KEY(a, b)` are a single unique set, not two.
- Unify logic for creating automatic indexes and parsing them - remove
separate logic in `check_automatic_pk_index_required()` and use the
existing `create_table()` utility in both index creation and
deserialization.
- Deserialize a single automatic index per unique set, and assert that
`unique_sets.len() == autoindexes.len()`.
- Verify consistent behavior by adding a fuzz tests that creates 1000
databases with 1 table each and runs `PRAGMA integrity_check` on all of
them with SQLite.
## Trivia
Apart from fixing the exact issue #2993, this PR also fixes other bugs
related to autoindex construction - namely cases where too many indexes
were created due to improper deduplication of unique sets.

Reviewed-by: Preston Thorpe <preston@turso.tech>

Closes #3018
2025-09-11 17:04:53 -04:00
Pekka Enberg
5b9e849415 Merge 'core/mvcc: Eliminate RwLock wrapping Transaction' from Pekka Enberg
The write and read sets in Transaction use SkipSet, which is thread-
safe. Therefore, drop the RwLock wrapping Transaction everywhere,
increasing MVCC throughput by almost 30%.
Before:
```
Running write throughput benchmark with 1 threads, 1000 batch size, 1000 iterations, mode: Mvcc
Database created at: write_throughput_test.db
Thread 0: 1000000 inserts in 6.50s (153927.21 inserts/sec)

=== BENCHMARK RESULTS ===
Total inserts: 1000000
Total time: 6.50s
Overall throughput: 153758.85 inserts/sec
Threads: 1
Batch size: 1000
Iterations per thread: 1000
```
After:
```
Running write throughput benchmark with 1 threads, 1000 batch size, 1000 iterations, mode: Mvcc
Database created at: write_throughput_test.db
Thread 0: 1000000 inserts in 5.10s (195927.13 inserts/sec)

=== BENCHMARK RESULTS ===
Total inserts: 1000000
Total time: 5.11s
Overall throughput: 195663.94 inserts/sec
Threads: 1
Batch size: 1000
Iterations per thread: 1000
```

Closes #3035
2025-09-11 20:55:14 +03:00
Pekka Enberg
45288b1297 core/mvcc: Eliminate RwLock wrapping Transaction
The write and read sets in Transaction use SkipSet, which is thread-safe.
Therefore, drop the RwLock wrapping Transaction everywhere, increasing
MVCC throughput by almost 30%.

Before:

```
Running write throughput benchmark with 1 threads, 1000 batch size, 1000 iterations, mode: Mvcc
Database created at: write_throughput_test.db
Thread 0: 1000000 inserts in 6.50s (153927.21 inserts/sec)

=== BENCHMARK RESULTS ===
Total inserts: 1000000
Total time: 6.50s
Overall throughput: 153758.85 inserts/sec
Threads: 1
Batch size: 1000
Iterations per thread: 1000
```

After:

```
Running write throughput benchmark with 1 threads, 1000 batch size, 1000 iterations, mode: Mvcc
Database created at: write_throughput_test.db
Thread 0: 1000000 inserts in 5.10s (195927.13 inserts/sec)

=== BENCHMARK RESULTS ===
Total inserts: 1000000
Total time: 5.11s
Overall throughput: 195663.94 inserts/sec
Threads: 1
Batch size: 1000
Iterations per thread: 1000
```
2025-09-11 20:31:19 +03:00
Pekka Enberg
1559cd127e Merge ' bindings/java: PreparedStatement executeUpdate ' from zongkx
JDBC Driver  Add PreparedStatement executeUpdate return

Closes #3022
2025-09-11 18:43:58 +03:00
Pekka Enberg
61c5b4530c Merge 'handle EXPLAIN like sqlite' from Lâm Hoàng Phúc
we are hard coding `EXPLAIN` for debugging
```sh
turso> EXPLAIN SELECT 1; EXPLAIN SELECT 1;
addr  opcode             p1    p2    p3    p4             p5  comment
----  -----------------  ----  ----  ----  -------------  --  -------
0     Init               0     3     0                    0   Start at 3
1     ResultRow          1     1     0                    0   output=r[1]
2     Halt               0     0     0                    0
3     Integer            1     1     0                    0   r[1]=1
4     Goto               0     1     0                    0
```
```sh
sqlite> EXPLAIN SELECT 1; EXPLAIN SELECT 1;
addr  opcode         p1    p2    p3    p4             p5  comment
----  -------------  ----  ----  ----  -------------  --  -------------
0     Init           0     4     0                    0   Start at 4
1     Integer        1     1     0                    0   r[1]=1
2     ResultRow      1     1     0                    0   output=r[1]
3     Halt           0     0     0                    0
4     Goto           0     1     0                    0
addr  opcode         p1    p2    p3    p4             p5  comment
----  -------------  ----  ----  ----  -------------  --  -------------
0     Init           0     4     0                    0   Start at 4
1     Integer        1     1     0                    0   r[1]=1
2     ResultRow      1     1     0                    0   output=r[1]
3     Halt           0     0     0                    0
4     Goto           0     1     0                    0
```

Closes #3005
2025-09-11 18:43:24 +03:00
Pekka Enberg
7d8a1a0d5f Merge 'whopper: A new DST with concurrency' from Pekka Enberg
Our simulator is currently limited to concurrency of one. This
introduces a much less sophisticated DST with focus on finding
concurrency bugs.

Closes #2985
2025-09-11 18:42:45 +03:00
Pekka Enberg
453ca6c531 Merge 'Document DEFERRED and IMMEDIATE transaction modes' from Pekka Enberg
Closes #3011
2025-09-11 18:21:17 +03:00
Pekka Enberg
ebd9da4369 Merge 'Fix tx isolation test semantics after #3023' from Jussi Saurio
After the fix in #3023, the transaction isolation fuzz test now
incorrectly takes a shadow snapshot of the DB state too early - before
it is determined that the connection successfully started a read
transaction.
Fix: take the snapshot after we've verified that the read TX started.
Closes #3025

Closes #3026
2025-09-11 18:21:00 +03:00
Jussi Saurio
6a7bead482 Fix tx isolation test semantics after #3023
The test now incorrectly takes a shadow snapshot of the DB state
before it is determined that the connection successfully started
a read transaction.

Fix: take the snapshot after we've verified that the read TX started.
2025-09-11 16:44:28 +03:00
Jussi Saurio
aeb3c217e1 Merge 'Fix: read transaction cannot be allowed to start with a stale max frame' from Jussi Saurio
If both of the following are true:
1. All read locks are already held
2. The highest readmark of any read lock is less than the committed max
frame
Then we must return Busy to the reader, because otherwise they would
begin a transaction with a stale local max frame, and thus not see some
committed changes.
Closes #3016

Reviewed-by: Preston Thorpe <preston@turso.tech>

Closes #3023
2025-09-11 16:20:05 +03:00
Pekka Enberg
433b60555f Add BEGIN CONCURRENT support for MVCC mode
Currently, when MVCC is enabled, every transaction mode supports
concurrent reads and writes, which makes it hard to adopt for existing
applications that use `BEGIN DEFERRED` or `BEGIN IMMEDIATE`.

Therefore, add support for `BEGIN CONCURRENT` transactions when MVCC is
enabled. The transaction mode allows multiple concurrent read/write
transactions that don't block each other, with conflicts resolved at
commit time. Furthermore, implement the correct semantics for `BEGIN
DEFERRED` and `BEGIN IMMEDIATE` by taking advantage of the pager level
write lock when transaction upgrades to write. This means that now
concurrent MVCC transactions are serialized against the legacy ones when
needed.

The implementation includes:

- Parser support for CONCURRENT keyword in BEGIN statements

- New Concurrent variant in TransactionMode to distinguish from regular
  read/write transactions

- MVCC store tracking of exclusive transactions to support IMMEDIATE and
  EXCLUSIVE modes alongside CONCURRENT

- Proper transaction state management for all transaction types in MVCC

This enables better concurrency for applications that can handle
optimistic concurrency control, while still supporting traditional
SQLite transaction semantics via IMMEDIATE and EXCLUSIVE modes.
2025-09-11 16:05:52 +03:00
Jussi Saurio
c30d320cab Fix: read transaction cannot be allowed to start with a stale max frame
If both of the following are true:

1. All read locks are already held
2. The highest readmark of any read lock is less than the committed max frame

Then we must return Busy to the reader, because otherwise they would begin a
transaction with a stale local max frame, and thus not see some committed
changes.
2025-09-11 15:58:13 +03:00
zongkx
92e211f278 Merge remote-tracking branch 'origin/main' 2025-09-11 12:26:52 +00:00
zongkx
d7096bdd28 fix executeUpdate updated count 2025-09-11 12:25:14 +00:00
zongkx
22cbd3a02c Merge branch 'tursodatabase:main' into main 2025-09-11 20:18:11 +08:00
zongkx
5d6e97b46b add executeUpdate updated count 2025-09-11 12:17:05 +00:00
TcMits
4c17fa87c5 remove .explain() 2025-09-11 18:28:46 +07:00
TcMits
68e8d5a36b clippy 2025-09-11 18:16:01 +07:00
TcMits
830e10da8f resolve merge conflict 2025-09-11 18:13:29 +07:00
Jussi Saurio
e3bd00883b Fix creation of automatic indexes
indexes with the naming scheme "sqlite_autoindex_<tblname>_<number>"
are automatically created when a table is created with UNIQUE or
PRIMARY KEY definitions.

these indexes must map to the table definition SQL in definition order,
i.e. sqlite_autoindex_foo_1 must be the first instance of UNIQUE or
PRIMARY KEY and so on.

this commit fixes our autoindex creation / parsing so that this invariant
is upheld.
2025-09-11 14:11:30 +03:00
Jussi Saurio
b9e2879f74 Add fuzz test for CREATE TABLE
This fuzz test verifies that various CREATE TABLE definitions with
UNIQUE and PRIMARY KEY definitions pass sqlite integrity_check.
2025-09-11 14:11:30 +03:00
TcMits
b56cb0b403 useless file 2025-09-11 18:06:54 +07:00
TcMits
b574b4bcea finish EXPLAIN 2025-09-11 18:04:59 +07:00
Jussi Saurio
bb74b2eaf9 Merge 'Refactor parseschema' from Jussi Saurio
Extracts out duplicated logic from `Schema::make_from_btree()` and
`parse_schema_rows()`

Closes #3015
2025-09-11 13:35:36 +03:00
Jussi Saurio
dc9fc8c0c6 Merge 'Fix value conversion for function parameters' from Levy A.
Value conversion to float for math functions work in a more strict way
than general numeric conversion. For example, valid prefixes that can be
converted to a integer, like `"44s"` will be converted to `Value::Null`
instead of trying to recover like the math operators.

Reviewed-by: Jussi Saurio <jussi.saurio@gmail.com>

Closes #3012
2025-09-11 13:34:07 +03:00
Jussi Saurio
7ac18a6952 Merge 'Remove some traces in super hot paths in btree' from Preston Thorpe
Particularly we were tracing `ImmutableRecord` / `BTreeKey` which would
then trace the bytes of records. These are super super hot paths and I
think we can probably remove even more to under debug assertions so we
dont eat those atomics/branches all the time.
This PR also introduces the `tracing_release` feature, which turns all
`trace!` and `debug!` macro invocations to noops at compile time, and
makes that feature available for all bindings.
it also removes the unused `lru` dependency, and cleans up the makefile
a bit

Reviewed-by: Jussi Saurio <jussi.saurio@gmail.com>

Closes #2995
2025-09-11 13:33:25 +03:00
Pekka Enberg
ae3c1fc2a6 Turso 0.2.0-pre.1 2025-09-11 11:44:42 +03:00
Pekka Enberg
5ae4756760 Merge 'Sync package opfs' from Nikita Sivukhin
This PR adds OPFS support to the sync package and also restructure it in
the similar fashion as in #2799
The structure for sync packages looks like this:
1. `@tursodatabase/sync-common` - package with common abstract TS code
shared between node and browser
2. `@tursodatabase/sync` - native package for node
3. `@tursodatabase/sync-browser` - browser package
Also, additional package `@tursodatabase/database-browser-common` was
extracted to share some common OPFS related code between
`@tursodatabase/database-browser` and `@tursodatabase/sync-browser`
packages.
Also, this PR moves JS bindings for sync directly to the
`bindings/javascript` folder because this allows to use all 7 packages
within same workspace.

Closes #3014
2025-09-11 11:44:26 +03:00
TcMits
a7373c9a97 update some helper function 2025-09-11 15:12:38 +07:00
Nikita Sivukhin
020b3f61f3 add CI step for debugging in future 2025-09-11 11:41:28 +04:00
Pekka Enberg
772af0d692 Merge 'Ensure that Connection::query() checks whether its schema is up to date' from Jussi Saurio
Closes #2997
Fixes issue #2997 where connection 2 cannot see tables created by
another connection 1, because `Connection::query()` was not checking
whether its copy of the schema was stale.

Reviewed-by: Preston Thorpe <preston@turso.tech>

Closes #3008
2025-09-11 10:10:39 +03:00
Jussi Saurio
f17997fc5d Extract methods for populating indices/views from schema 2025-09-11 09:51:46 +03:00
Jussi Saurio
07944e23b5 Extract common logic for handling sqlite_schema rows 2025-09-11 09:45:40 +03:00
Pekka Enberg
89d1c9a421 whopper: Switch to mmap2 crate to fix Windows build 2025-09-11 08:35:18 +03:00
Pekka Enberg
c5ca259abc whopper: Run cargo clippy --fix 2025-09-11 08:35:18 +03:00
Pekka Enberg
ae920c435d whopper: Ragnarök mode with cosmic rays 2025-09-11 08:35:18 +03:00
Pekka Enberg
a9694c87b1 whopper: A new DST with concurrency
This is a new deterministic simulator for Turso that focuses on finding
concurrency bugs.

You can run whopper with:

```console
penberg@vonneumann turso % SEED=1234 ./whopper/bin/run
   Compiling turso_whopper v0.1.5-pre.3 (/Users/penberg/src/tursodatabase/turso/whopper)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 2.22s
seed = 1234
       .             I/U/D/C
       .             44/19/11/2
       .             68/33/21/2
       |             104/51/29/3
       |             121/69/41/3
      ╱|╲            150/84/51/3
     ╱╲|╱╲           184/97/59/3
    ╱╲╱|╲╱╲          199/105/64/4
   ╱╲╱╲|╱╲╱╲         206/115/69/5
  ╱╲╱╲╱|╲╱╲╱╲        234/138/82/6
 ╱╲╱╲╱╲|╱╲╱╲╱╲       269/164/91/7
```
2025-09-11 08:35:18 +03:00
Pekka Enberg
74c14efdfa sql_generation: Add support for DROP INDEX 2025-09-11 08:35:18 +03:00
Pekka Enberg
b572366a2b core/vbe: Demote op_transaction() logging to debug 2025-09-11 08:35:18 +03:00
Pekka Enberg
ca51a60b3c core/storage: Demote restart_log() logging to debug 2025-09-11 08:35:18 +03:00
Levy A.
4070e05cd2 fix: math function parameter conversion 2025-09-10 20:49:30 -03:00