Commit Graph

6977 Commits

Author SHA1 Message Date
PThorpe92
3048e4fa97 Add optional register_fixed_buffer method to IO trait 2025-08-01 14:54:26 -04:00
Pekka Enberg
d161c2652c Merge 'core/mvcc: Move commit_txn() to generic state machinery ' from Pere Diaz Bou
Unfortunately it seems we are never reaching the point to remove state
machines, so might as well make it easier to make.
There are two points that must be highlighted:
1. There is a `StateTransition` trait implemented like:
```rust
pub trait StateTransition {
    type State;
    type Context;

    fn transition<'a>(&mut self, context: &Self::Context) ->
Result<TransitionResult>;
    fn finalize<'a>(&mut self, context: &Self::Context) -> Result<()>;
    fn is_finalized(&self) -> bool;
}
```
where there exists `transition` which tries to move state forward, and
`finalize` which marks the state machine as "finalized" so that **no
other call to finalize will forward the state and it will panic instead.
2. Before, we would store the state of a state machine inside the
callee's struct, but I'm proposing we do something different where the
callee will return the state machine and the caller will be responsible
of advancing it. This way we don't need to track many reset operations
in case of failures or rollbacks, and instead we could simply drop a
state machine and all other nested state machines will drop in a
cascade.

Closes #2384
2025-08-01 19:28:16 +03:00
Pekka Enberg
9a1ead44f1 Merge 'bindings/javascript: Reduce VM/native crossing overhead' from Pekka Enberg
Before:
```
penberg@vonneumann perf % node perf-turso.js
cpu: Apple M1
runtime: node v22.16.0 (arm64-darwin)

benchmark                            time (avg)             (min … max)       p75       p99      p999
----------------------------------------------------------------------- -----------------------------
• Statement
----------------------------------------------------------------------- -----------------------------
Statement.get() bind parameters   1'525 ns/iter   (1'482 ns … 1'720 ns)  1'534 ns  1'662 ns  1'720 ns

summary for Statement
  Statement.get() bind parameters
penberg@vonneumann perf % bun perf-turso.js
cpu: Apple M1
runtime: bun 1.2.15 (arm64-darwin)

benchmark                            time (avg)             (min … max)       p75       p99      p999
----------------------------------------------------------------------- -----------------------------
• Statement
----------------------------------------------------------------------- -----------------------------
Statement.get() bind parameters   1'198 ns/iter   (1'157 ns … 1'495 ns)  1'189 ns  1'456 ns  1'495 ns

summary for Statement
  Statement.get() bind parameters
```
After:
```

benchmark                            time (avg)             (min … max)       p75       p99      p999
----------------------------------------------------------------------- -----------------------------
• Statement
----------------------------------------------------------------------- -----------------------------
Statement.get() bind parameters   1'206 ns/iter   (1'180 ns … 1'402 ns)  1'208 ns  1'365 ns  1'402 ns

summary for Statement
  Statement.get() bind parameters
penberg@vonneumann perf % bun perf-turso.js
cpu: Apple M1
runtime: bun 1.2.15 (arm64-darwin)

benchmark                            time (avg)             (min … max)       p75       p99      p999
----------------------------------------------------------------------- -----------------------------
• Statement
----------------------------------------------------------------------- -----------------------------
Statement.get() bind parameters   1'019 ns/iter     (980 ns … 1'360 ns)  1'005 ns  1'270 ns  1'360 ns

summary for Statement
  Statement.get() bind parameters
```

Closes #2391
2025-08-01 19:18:33 +03:00
Pekka Enberg
f1794b6270 bindings/javascript: Add INSERT benchmark too 2025-08-01 18:17:13 +03:00
Pekka Enberg
d4633415a7 Merge 'Enable indexes by default' from Jussi Saurio
Enables indexes by default in Rust and Python bindings + the CLI, while
leaving the feature flag in place.
Comments out a single ALTER TABLE test that fails due to #2390

Closes #2389
2025-08-01 17:36:35 +03:00
Pekka Enberg
358c0bfc27 cargo fmt 2025-08-01 17:17:01 +03:00
Pekka Enberg
1db0637a5e bindings/javascript: Improve benchmark 2025-08-01 16:55:04 +03:00
Pere Diaz Bou
764523a8bb core/mvcc: fix tests with state machines 2025-08-01 15:48:09 +02:00
Pekka Enberg
94efe9dd46 bindings/javascript: Reduce VM/native crossing overhead
Before:

```
penberg@vonneumann perf % node perf-turso.js
cpu: Apple M1
runtime: node v22.16.0 (arm64-darwin)

benchmark                            time (avg)             (min … max)       p75       p99      p999
----------------------------------------------------------------------- -----------------------------
• Statement
----------------------------------------------------------------------- -----------------------------
Statement.get() bind parameters   1'525 ns/iter   (1'482 ns … 1'720 ns)  1'534 ns  1'662 ns  1'720 ns

summary for Statement
  Statement.get() bind parameters
penberg@vonneumann perf % bun perf-turso.js
cpu: Apple M1
runtime: bun 1.2.15 (arm64-darwin)

benchmark                            time (avg)             (min … max)       p75       p99      p999
----------------------------------------------------------------------- -----------------------------
• Statement
----------------------------------------------------------------------- -----------------------------
Statement.get() bind parameters   1'198 ns/iter   (1'157 ns … 1'495 ns)  1'189 ns  1'456 ns  1'495 ns

summary for Statement
  Statement.get() bind parameters
```

After:

```

benchmark                            time (avg)             (min … max)       p75       p99      p999
----------------------------------------------------------------------- -----------------------------
• Statement
----------------------------------------------------------------------- -----------------------------
Statement.get() bind parameters   1'206 ns/iter   (1'180 ns … 1'402 ns)  1'208 ns  1'365 ns  1'402 ns

summary for Statement
  Statement.get() bind parameters
penberg@vonneumann perf % bun perf-turso.js
cpu: Apple M1
runtime: bun 1.2.15 (arm64-darwin)

benchmark                            time (avg)             (min … max)       p75       p99      p999
----------------------------------------------------------------------- -----------------------------
• Statement
----------------------------------------------------------------------- -----------------------------
Statement.get() bind parameters   1'019 ns/iter     (980 ns … 1'360 ns)  1'005 ns  1'270 ns  1'360 ns

summary for Statement
  Statement.get() bind parameters
```
2025-08-01 16:45:03 +03:00
Pekka Enberg
a51c35c979 bindings/javascript: Fix silly typo in package.json 2025-08-01 16:04:59 +03:00
Pekka Enberg
29688e69d1 serverless: v0.1.1 2025-08-01 15:56:39 +03:00
Pekka Enberg
eae6e056cb Add JavaScript API reference document 2025-08-01 15:56:08 +03:00
Pekka Enberg
1acb396814 Merge 'JavaScript serverless driver fixes' from Pekka Enberg
...align it with the native bindings.

Closes #2386
2025-08-01 15:55:46 +03:00
Jussi Saurio
c5c3898896 tcl: comment out test that fails due to #2390 2025-08-01 15:45:36 +03:00
Jussi Saurio
86b1232268 chore: enable indexes by default 2025-08-01 15:44:56 +03:00
Pere Diaz Bou
69b20d9d43 state_machine: add result to StateTransition 2025-08-01 14:07:07 +02:00
Pere Diaz Bou
c3f00475eb state_machine: rename transition -> step 2025-08-01 13:56:57 +02:00
Pekka Enberg
47860b6df5 serverless: Fix bind parameters 2025-08-01 14:21:04 +03:00
Pekka Enberg
335d4a19c8 serverless: Implement Statement.raw() 2025-08-01 14:19:54 +03:00
Pekka Enberg
86581197bf serverless: Fix Statement.get() to return undefined
...aligns with the native bindings semantics.
2025-08-01 14:01:44 +03:00
Pekka Enberg
a67b0a8a1d Merge 'chore: move tx isolation fuzz test to 'tests'' from Jussi Saurio
Closes #2383
2025-08-01 13:50:04 +03:00
Pere Diaz Bou
0f70e7101f core/state_machine: move state_machine to its own file 2025-08-01 12:49:32 +02:00
Pekka Enberg
994a0e0852 Turso 0.1.4-pre.1 2025-08-01 13:38:12 +03:00
Pere Diaz Bou
27757ab4eb core/mvcc commit_txn generic state machinery
Unfortunately it seems we are never reaching the point to remove state
machines, so might as well make it easier to make.

There are two points that must be highlighted:
1. There is a `StateTransition` trait implemented like:

```rust
pub trait StateTransition {
    type State;
    type Context;

    fn transition<'a>(&mut self, context: &Self::Context) ->
Result<TransitionResult>;
    fn finalize<'a>(&mut self, context: &Self::Context) -> Result<()>;
    fn is_finalized(&self) -> bool;
}
```

where there exists `transition` which tries to move state forward, and
`finalize` which marks the state machine as "finalized" so that **no
other call to finalize will forward the state and it will panic instead.

2. Before, we would store the state of a state machine inside the
callee's struct, but I'm proposing we do something different where the
callee will return the state machine and the caller will be responsible
of advancing it. This way we don't need to track many reset operations
in case of failures or rollbacks, and instead we could simply drop a
state machine and all other nested state machines will drop in a
cascade.
2025-08-01 12:36:02 +02:00
Pere Diaz Bou
d616a375ee core/mvcc: commit_tx state machine 2025-08-01 12:36:02 +02:00
Pekka Enberg
38dbf75364 Merge 'Implement JavaScript bindings with minimal Rust core' from Pekka Enberg
This rewrites the JavaScript bindings completely by exposing only
primitive operations from Rust NAPI-RS code. For example, there is
prepare(), bind(), and step(), but high level interfaces like all() and
get() are implemented in JavaScript.
We're doing this so that we can implement async interfaces in the
JavaScript layer instead of having to bring in Tokio.

Closes #2372
2025-08-01 13:35:33 +03:00
Jussi Saurio
69fc1ea238 Merge 'perf/btree: improve performance of rowid() function' from Jussi Saurio
if the table is an intkey table, we can read the rowid directly without
deserializing the full cell, and we also don't need to start
deserializing the record if only the rowid is requested.
```sql
Benchmarking Execute `SELECT * FROM users LIMIT ?`/limbo_execute_select_rows/1: Collecting 100 samples in estimated 5.0007 s (11M i
Execute `SELECT * FROM users LIMIT ?`/limbo_execute_select_rows/1
                        time:   [469.38 ns 470.77 ns 472.40 ns]
                        change: [-5.8959% -5.5232% -5.1840%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 4 outliers among 100 measurements (4.00%)
  2 (2.00%) high mild
  2 (2.00%) high severe
Benchmarking Execute `SELECT * FROM users LIMIT ?`/limbo_execute_select_rows/10: Collecting 100 samples in estimated 5.0088 s (1.9M
Execute `SELECT * FROM users LIMIT ?`/limbo_execute_select_rows/10
                        time:   [2.6523 µs 2.6596 µs 2.6685 µs]
                        change: [-8.7117% -8.4083% -8.0949%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 7 outliers among 100 measurements (7.00%)
  1 (1.00%) low mild
  3 (3.00%) high mild
  3 (3.00%) high severe
Benchmarking Execute `SELECT * FROM users LIMIT ?`/limbo_execute_select_rows/50: Collecting 100 samples in estimated 5.0197 s (399k
Execute `SELECT * FROM users LIMIT ?`/limbo_execute_select_rows/50
                        time:   [12.514 µs 12.545 µs 12.578 µs]
                        change: [-9.5243% -9.0562% -8.6227%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 4 outliers among 100 measurements (4.00%)
  2 (2.00%) high mild
  2 (2.00%) high severe
Benchmarking Execute `SELECT * FROM users LIMIT ?`/limbo_execute_select_rows/100: Collecting 100 samples in estimated 5.0600 s (202
Execute `SELECT * FROM users LIMIT ?`/limbo_execute_select_rows/100
                        time:   [25.135 µs 25.291 µs 25.470 µs]
                        change: [-8.8822% -8.3943% -7.8854%] (p = 0.00 < 0.05)
                        Performance has improved.
```
"only" 4x slower than sqlite on `SELECT * FROM users LIMIT 100` after
this!

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

Closes #2382
2025-08-01 13:35:02 +03:00
Jussi Saurio
e1dd028136 Merge 'Fix vector deserialization alignment and blob/text empty mismatch' from bit-aloo
* Previously, deserializing an empty vector used `Vec::new()`, resulting
in zero capacity, which is not guaranteed to be aligned for `f32`/`f64`.
This could lead to undefined behavior when interpreting the data.
* We also inconsistently treated empty input: `"[]"` (text) was accepted
as a zero-length vector, but empty blobs (`&[]`) were rejected.
* Now:
  * We initialize empty vectors with at least one element’s capacity to
preserve alignment.
  * We allow zero-sized blobs and treat them the same as `"[]""` input
as empty vectors.

Closes #2371
2025-08-01 13:03:20 +03:00
Jussi Saurio
addb067416 chore: move tx isolation fuzz test to 'tests' 2025-08-01 13:02:05 +03:00
Jussi Saurio
75be68092b Merge 'test/fuzz/transactions: add "PRAGMA wal_checkpoint" to txn isolation fuzz test' from Jussi Saurio
This PR is now ready, since i cannot find any new bugs with the fuzzer.
## Changes
- adds `PRAGMA wal_checkpoint` to transaction isolation fuzz test
## Fixes extracted as separate PRs from this one:
#2360
#2362
#2365
#2366
#2367
#2380

Closes #2364
2025-08-01 12:53:59 +03:00
Pekka Enberg
0204a53394 Merge 'core/mvcc: Persist changes through pager on commit' from Pere Diaz Bou
**draft** because I need to fix tests now since they can't work with
random payloads without properly serialize them.

Closes #2345
2025-08-01 12:39:17 +03:00
Pekka Enberg
773e4eed90 bindings/javascript: Add micro-benchmarks 2025-08-01 12:09:05 +03:00
Pekka Enberg
95b701aa1f testing/javascript: Fix async tests to await 2025-08-01 12:09:05 +03:00
Pekka Enberg
845fc13d6e bindings/javascript: Remove test suite
We have `testing/javascript` to test both the native bindings and
serverless driver, so let's use that instead.
2025-08-01 12:09:05 +03:00
Pere Diaz Bou
0cefb01395 mvcc_benchmark: clippy 2025-08-01 11:01:29 +02:00
Jussi Saurio
c9a3a65942 perf/btree: don't waste time reading contents twice 2025-08-01 11:49:41 +03:00
Jussi Saurio
111c1e64c4 perf/btree: improve performance of rowid() function
if the table is an intkey table, we can read the rowid directly
without deserializing the full cell, and we also don't need to start
deserializing the record if only the rowid is requested.

```sql
Benchmarking Execute `SELECT * FROM users LIMIT ?`/limbo_execute_select_rows/1: Collecting 100 samples in estimated 5.0007 s (11M i
Execute `SELECT * FROM users LIMIT ?`/limbo_execute_select_rows/1
                        time:   [469.38 ns 470.77 ns 472.40 ns]
                        change: [-5.8959% -5.5232% -5.1840%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 4 outliers among 100 measurements (4.00%)
  2 (2.00%) high mild
  2 (2.00%) high severe
Benchmarking Execute `SELECT * FROM users LIMIT ?`/limbo_execute_select_rows/10: Collecting 100 samples in estimated 5.0088 s (1.9M
Execute `SELECT * FROM users LIMIT ?`/limbo_execute_select_rows/10
                        time:   [2.6523 µs 2.6596 µs 2.6685 µs]
                        change: [-8.7117% -8.4083% -8.0949%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 7 outliers among 100 measurements (7.00%)
  1 (1.00%) low mild
  3 (3.00%) high mild
  3 (3.00%) high severe
Benchmarking Execute `SELECT * FROM users LIMIT ?`/limbo_execute_select_rows/50: Collecting 100 samples in estimated 5.0197 s (399k
Execute `SELECT * FROM users LIMIT ?`/limbo_execute_select_rows/50
                        time:   [12.514 µs 12.545 µs 12.578 µs]
                        change: [-9.5243% -9.0562% -8.6227%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 4 outliers among 100 measurements (4.00%)
  2 (2.00%) high mild
  2 (2.00%) high severe
Benchmarking Execute `SELECT * FROM users LIMIT ?`/limbo_execute_select_rows/100: Collecting 100 samples in estimated 5.0600 s (202
Execute `SELECT * FROM users LIMIT ?`/limbo_execute_select_rows/100
                        time:   [25.135 µs 25.291 µs 25.470 µs]
                        change: [-8.8822% -8.3943% -7.8854%] (p = 0.00 < 0.05)
                        Performance has improved.
```
2025-08-01 11:44:53 +03:00
Pere Diaz Bou
c807b035c5 core/mvcc: fix tests again
had to create connections for every different txn
2025-08-01 10:44:19 +02:00
Pere Diaz Bou
5ad7d10790 core/mvcc: fix use of rwlock 2025-08-01 10:38:41 +02:00
Pere Diaz Bou
b518e1f839 core/mvcc: add missing arc import 2025-08-01 10:38:41 +02:00
Pere Diaz Bou
c4318cac36 core/mvcc: fix tests 2025-08-01 10:38:41 +02:00
Pere Diaz Bou
49a00ff338 core/mvcc: load table's rowid on initialization
We need to load rowids into mvcc's store in order before doing any read
in case there are rows.

This has a performance penalty for now as expected because we should,
ideally, scan for row ids lazily instead.
2025-08-01 10:38:41 +02:00
Pere Diaz Bou
b399ddea1b core/mvcc: begin pager read txn on mvcc begin_txn 2025-08-01 10:38:41 +02:00
Pere Diaz Bou
b4ac38cd25 core/mvcc: persist writes on mvcc commit
On Mvcc `commit_txn` we need to persist changes to database, for this case we re-use pager's semantics of transactions:
1. If there are no conflicts, we start `pager.begin_write_txn`
2. `pager.end_txn`: We flush changes to WAL
3. We finish Mvcc transaction by marking rows with new timestamp.
2025-08-01 10:38:41 +02:00
Jussi Saurio
24c8c3430f test/fuzz/tx: add 'PRAGMA wal_checkpoint' to tx isolation fuzzer 2025-08-01 11:28:38 +03:00
Jussi Saurio
2233bb41c3 Merge 'fix/wal: reset ongoing checkpoint state when checkpoint fails' from Jussi Saurio
## What
The following sequence of actions is possible:
```sql
-- TRUNCATE checkpoint fails during WAL restart,
-- but OngoingCheckpoint.state is still left at Done for conn 0
Connection 0(op=23): PRAGMA wal_checkpoint(TRUNCATE)
Connection 0(op=23) Checkpoint TRUNCATE: OK: false, wal_page_count: NULL, checkpointed_count: NULL

-- TRUNCATE checkpoint succeeds for conn 1
Connection 1(op=26): PRAGMA wal_checkpoint(TRUNCATE)
Connection 1(op=26) Checkpoint TRUNCATE: OK: true, wal_page_count: 0, checkpointed_count: 0

-- Conn 0 now does a PASSIVE checkpoint, and immediately thinks
-- it's in the Done state, and thinks it checkpointed 17 frames.
-- since mode is PASSIVE, it now thinks both the WAL and the DB have those 17 frames
-- so the first 17 frames of the WAL can be ignored from now on.
Connection 0(op=27): PRAGMA wal_checkpoint(PASSIVE)
Connection 0(op=27) Checkpoint PASSIVE: OK: true, wal_page_count: 0, checkpointed_count: 17

-- Connection 0 starts a txn with min=18 (ignore first 17 frames in WAL),
-- and deletes rowid=690, which becomes WAL frame number 1
Connection 0(op=28): DELETE FROM test_table WHERE id = 690
begin_read_tx(min=18, max=0, slot=1, max_frame_in_wal=0)

-- Connection 1 starts a txn with min=18 (ignore first 17 frames in WAL),
-- and inserts rowid=1128, which becomes WAL frame number 2
Connection 1(op=28): INSERT INTO test_table (id, text) VALUES (1128, text_560)
begin_read_tx(min=18, max=1, slot=1, max_frame_in_wal=1)

-- Connection 0 again starts tx with min=18, and performs a read, and two wrong things happen:
-- 1. it doesn't see row 690 as deleted, because it's in WAL frame 1, which it ignores
-- 2. it doesn't see the new row 1128, because it's in WAL frame 2, which it ignores
Connection 0(op=29): SELECT * FROM test_table
begin_read_tx(min=18, max=2, slot=1, max_frame_in_wal=2)
```
## Fix
Reset `ongoing_checkpoint.state` to `Start` when checkpoint fails.
Issue found in #2364 .

Reviewed-by: bit-aloo (@Shourya742)

Closes #2380
2025-08-01 11:28:04 +03:00
Jussi Saurio
d465abeced Merge 'Open a temporary on-disk file for ephemeral tables' from Jussi Saurio
Closes #2219
## What
Ephemeral tables and indexes should use a temporary database file
instead of being backed only by memory.
## Why
This makes them able to spill to disk when necessary when their page
cache is nearing its memory limit. However, they should spill directly
to the temporary database file without WAL journaling, since a WAL is
not necessary (or even desirable) for ephemeral tables. Spilling is not
implemented yet for any use case - this is just an enabler for it.
## Implementation details
- Create random filename using `io.generate_random_number()` in
platform-specific temporary directory
- Make `pager.wal` an optional property again, removing `DummyWAL`
- Remove `FileMemoryStorage` as it is never used

Closes #2315
2025-08-01 11:06:08 +03:00
Jussi Saurio
c19e7d20c1 Merge 'Force Sqlite to parse schema on connection benchmark' from Levy A.
Resolves #2312.
<img width="973" height="213" alt="image" src="https://github.com/user-
attachments/assets/a243d61c-9987-4520-9155-6bef5d162179" />
```
Open/Connect/limbo_schema/
                        time:   [11.669 ms 11.683 ms 11.700 ms]
                        change: [-4.3350% -1.8204% +0.2040%] (p = 0.11 > 0.05)
                        No change in performance detected.
Found 9 outliers among 100 measurements (9.00%)
  5 (5.00%) high mild
  4 (4.00%) high severe
Open/Connect/sqlite_schema/
                        time:   [10.479 ms 10.693 ms 10.969 ms]
                        change: [+0.3783% +2.4616% +5.2808%] (p = 0.02 < 0.05)
                        Change within noise threshold.
Found 8 outliers among 100 measurements (8.00%)
  3 (3.00%) high mild
  5 (5.00%) high severe
  ```

Closes #2375
2025-08-01 10:24:03 +03:00
Jussi Saurio
7259751eba Merge 'Support the OFFSET clause for Compound select' from meteorgan
Closes #2376
2025-08-01 10:18:13 +03:00
Jussi Saurio
77666b1eb5 Merge 'Fix parser error for repetition in row values' from Diego Reis
Closes #1948
This PR also adds pretty basic support for [row values in UPDATE stateme
nts](https://sqlite.org/rowvalue.html#row_values_in_update_statements),
but it only accepts expressions like:
```sql
UPDATE t SET (a, b) = (2 + 2, 'joe');
```
While SQLite accepts whole new statements, like:
```sql
UPDATE tab3 
   SET (a,b,c) = (SELECT x,y,z
                    FROM tab4
                   WHERE tab4.w=tab3.d)
 WHERE tab3.e BETWEEN 55 AND 66;
 ```
I noticed we don't explicitly have the concept of row values, maybe
doing some plumbing in that matter could solve it?
If there is a way to implement that with our current infrastructure
(a.k.a skill issue from my side) please comment here.

Closes #2355
2025-08-01 10:17:05 +03:00