Commit Graph

6229 Commits

Author SHA1 Message Date
Pekka Enberg
aa84daabf1 core/storage: Fix BTreeCursor::rowid() with MVCC 2025-07-17 16:23:31 +03:00
Pekka Enberg
cef0195b42 core/storage: Fix BTreeCursor::record() for MVCC
Respect immutable record invalidation.
2025-07-17 16:12:40 +03:00
Pekka Enberg
962987e9a1 core/mvcc: Fix MVCC cursor traversal
Add an explicit rewind() to move to the beginning. Change forward()
semantics so that *after* first forward() call, you are pointing to the
first row, which matches the get_next_record() semantics in B-tree
cursor.
2025-07-17 16:12:40 +03:00
Pekka Enberg
2b1ee907a9 core/vdbe: Fix op_new_rowid() with MVCC 2025-07-17 14:13:22 +03:00
Pekka Enberg
72df538a76 core/storage: Add MVCC asertion to BTreeCursor::seek_to_last() 2025-07-17 14:13:22 +03:00
Pekka Enberg
3aca9c54c7 core/storage: Fix BTreeCursor::record() with MVCC 2025-07-17 14:13:22 +03:00
Pekka Enberg
8e338d3e7a core/vdbe: Fix SetCookie when MVCC is enabled 2025-07-17 14:13:22 +03:00
Pekka Enberg
1fc6126157 core/storage: Allocate page1 lazily for MVCC transactions 2025-07-17 14:13:22 +03:00
Pekka Enberg
45c77f5e07 Merge 'bind/javascript: Fix presentation mode disabling logic' from Diego Reis
Presentation mode disabling logic was ~obviously~ wrong (my bad), it's
fixed now.

Closes #2125
2025-07-17 10:51:15 +03:00
Pekka Enberg
99cdcf5348 Merge 'core: Copy-on-write for in-memory schema' from Levy A.
<img height="400" alt="image" src="https://github.com/user-
attachments/assets/bdd5c0a8-1bbb-4199-9026-57f0e5202d73" />
<img height="400" alt="image" src="https://github.com/user-
attachments/assets/7ea63e58-2ab7-4132-b29e-b20597c7093f" />
We were copying the schema preemptively on each `Database::connect`, now
the schema is shared until a change needs to be made by sharing a single
`Arc` and mutating it via `Arc::make_mut`. This is faster as reduces
memory usage.

Closes #2022
2025-07-17 10:46:46 +03:00
Pekka Enberg
2c23d8d9e3 Merge 'simulator: Disable INSERT INTO .. SELECT for being slow' from Pekka Enberg
Refs #2129

Closes #2130
2025-07-17 10:07:47 +03:00
Pekka Enberg
e8ac707190 simulator: Disable INSERT INTO .. SELECT for being slow
Refs #2129
2025-07-17 09:20:00 +03:00
Pekka Enberg
cae1c289b2 github: Reduce simulator iterations
...hopefully fixes simulator runs timing out problem.
2025-07-17 08:52:06 +03:00
Pekka Enberg
ae4dcbad0f Merge 'Async IO: registration of file descriptors' from Preston Thorpe
### Async IO performance, part 0
Relatively small and focused PR that mainly does two things, will add a
.md document of the proposed/planned improvements to the io_uring module
to fully revamp our async IO.
1. **Registration of file descriptors.**
At startup, by calling `io_uring_register_files_sparse` we can allocate
an array in shared kernel/user space by calling register_files_sparse
which initializes each slot to `-1`, and when we open a file we call
`io_uring_register_files_update`, providing an index into this array and
`fd`.
Then for the IO submission, we can reference the index into this array
instead of the fd, saving the kernel the work of looking up the fd in
the process file table, incrementing the reference count, doing the
operation, then finally decrementing the refcount. Instead the kernel
can just index into the array and do the operation.
This especially provides an improvement for cases like this, where files
are open for long periods of time, which the kernel will perform many
operations on.
The eventual goal of this, is to use Fixed read/write operations, where
both the file descriptor and the underlying buffer is registered with
the kernel. There is another branch continuing this work, that
introduces a buffer pool that memlock's one large 32MB arena mmap and
tries to use that wherever possible.
These Fixed operations are essentially the "holy grail" of io_uring
performance (for file operations).
2. **!Vectored IO**
This is kind of backwards, because the goal is to indeed implement
proper vectored IO and I'm removing some of the plumbing in this PR, but
currently we have been using `Writev`/`Readv`, while never submitting >
1 iovec at a time.
Writes to the WAL, especially, would benefit immensely from vectored IO,
as it is append-only and therefore all writes are contiguous. Regular
checkpointing/cache flushing to disk can also be adapted to aggregate
these writes and submit many in a single system call/opcode.
Until this is implemented, the bookkeeping and iovecs are unnecessary
noise/overhead, so let's temporarily remove them and revert to normal
`read`/`write` until they are needed and it can be designed from
scratch.
3. **Flags**
`setup_single_issuer` hints to the kernel that `IOURING_ENTER` calls
will all be sent from a single thread, and `setup_coop_taskrun` removes
some unnecessary kernel interrupts for providing cqe's which most single
threaded applications do not need. Both these flags demonstrate modest
improvement of performance.

Closes #2127
2025-07-17 08:47:44 +03:00
Pekka Enberg
d2158ff201 Merge 'Clean up AST unparsing, remove ToSqlString' from Levy A.
Enables formatting `Expr::Column` by adding the context to `ToTokens`
instead of creating a new unparsing implementation for each node.
`ToTokens` implemented for:
- [x] `UpdatePlan`
- [x] `Plan`
- [x] `JoinedTable`
- [x] `SelectPlan`
- [x] `DeletePlan`

Reviewed-by: Pedro Muniz (@pedrocarlo)

Closes #1949
2025-07-17 08:44:31 +03:00
PThorpe92
ad2ae3e22f Use fallback to regular fd if file registration is unavailable in io_uring 2025-07-16 23:08:46 -04:00
PThorpe92
fb78cdade0 Increase ring size from 128 -> 512 2025-07-16 22:44:20 -04:00
PThorpe92
4d09f1ab65 Enable coop_taskrun flag to disable excessive interrupts for completions 2025-07-16 22:43:44 -04:00
PThorpe92
95c343586c Enable single_issuer flag for io_uring to signal submissions from single thread 2025-07-16 22:42:40 -04:00
PThorpe92
9dfadf7872 Add registered file descriptors to io_uring IO module 2025-07-16 22:41:47 -04:00
Diego Reis
21882d1db3 bind/js: Fix presentation mode disabling logic 2025-07-16 15:07:12 -03:00
Pekka Enberg
b03b06107b Turso 0.1.3-pre.2 2025-07-16 20:08:46 +03:00
Pekka Enberg
c378f8a8bb Merge 'compat: add integrity_check' from Pere Diaz Bou
Reviewed-by: Avinash Sajjanshetty (@avinassh)
Reviewed-by: Jussi Saurio <jussi.saurio@gmail.com>

Closes #2119
2025-07-16 20:08:32 +03:00
Pekka Enberg
e6c3a5a9b8 Merge 'rename operation_xxx to change_xxx to make naming more consistent' from Nikita Sivukhin
This PR renames CDC table column names to use "change"-centric
terminology and avoid using `operation_xxx` column names.
Just a small refactoring to bring more consistency as `turso-db` refer
to the feature as capture data **changes** - and there is no word
operation here.

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

Closes #2120
2025-07-16 20:08:23 +03:00
Pekka Enberg
af182d9895 Merge 'btree: fix post-balancing seek bug in delete path' from Jussi Saurio
Aftermath of seek-related refactor in #2065, which you can read for
background. The change in this PR is documented pretty well inline - if
we receive a `TryAdvance` seek result when seeking after balancing, we
need to - well - try to advance.
Closes #2116

Closes #2115
2025-07-16 20:08:15 +03:00
Levy A.
8e8f1682df add with_schema_mut
removes all repeated `Arc::make_mut`
2025-07-16 13:54:39 -03:00
Levy A.
d0e26db01a use lock for database schema 2025-07-16 13:54:39 -03:00
Levy A.
4c77d771ff only copy schema on writes 2025-07-16 13:54:36 -03:00
Jussi Saurio
bb0c017d9f Merge 'btree: fix trying to go upwards when we are already at the end of the entire btree' from Jussi Saurio
## What does this fix
This PR fixes an issue with BTree upwards traversal logic where we would
try to go up to a parent node in `next()` even though we are at the very
end of the btree. This behavior can leave the cursor incorrectly
positioned at an interior node when it should be at the right edge of
the rightmost leaf.
## Why doesn't it cause problems on main
This bug is masked on `main` by every table `insert()` (wastefully)
calling `find_cell()`:
- `op_new_rowid` called, let's say the current max rowid is `666`.
Cursor is left pointing at `666`.
- `insert()` is called with rowid `667`, cursor is currently pointing at
`666`, which is incorrect.
- `find_cell()` does a binary search every time, and hence somewhat
accidentally positions the cursor correctly _after_ `666` so that the
insert goes to the correct place
## Why was this issue found
in #1988, I am removing `find_cell()` entirely in favor of always
performing a seek to the correct location - and skipping `seek` when it
is not required, saving us from wasting a binary search on every insert
- but this change means that we need to call `next()` after
`op_new_rowid` to have the cursor positioned correctly at the new
insertion slot. Doing this surfaces this upwards traversal bug in that
PR branch.
## Details of solution
- Store `cell_count` together with `cell_idx` in pagestack, so that
chlidren can know whether their parents have reached their end without
doing IO
- To make this foolproof, pin pages on `PageStack` so the page cache
cannot evict them during tree traversal
- `cell_indices` renamed to `node_states` since it now carries more
information (cell index AND count, instead of just index)

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

Closes #2005
2025-07-16 19:44:21 +03:00
Jussi Saurio
43f0ab39dc Merge 'Separate user-callable cacheflush from internal cacheflush logic' from Diego Reis
Cacheflush should only spill pages to WAL as non-commit frames, without
checkpointing nor syncing.
- [docs](https://sqlite.org/c3ref/db_cacheflush.html)
- [sqlite3PagerFlush](https://github.com/sqlite/sqlite/blob/625d0b70febe
cb0864a81b2a047a961a59e8c17e/src/pager.c#L4669)

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

Closes #2044
2025-07-16 19:44:12 +03:00
Nikita Sivukhin
97b82fe6d8 rename operation_xxx to change_xxx to make naming more consistent 2025-07-16 20:16:24 +04:00
Levy A.
89911ee8d1 remove to_sql_string from simulator 2025-07-16 12:34:10 -03:00
Pere Diaz Bou
d559bf3d9f compat: add integrity_check 2025-07-16 17:19:51 +02:00
Levy A.
714225b9f0 remove ToSqlString trait 2025-07-16 12:16:34 -03:00
Levy A.
6fe2505425 add more ToTokens impls 2025-07-16 12:16:31 -03:00
Levy A.
e81c7b07fb modify tests for new formatter 2025-07-16 12:16:31 -03:00
Levy A.
373a4a26c4 fix: comma function 2025-07-16 12:16:28 -03:00
Levy A.
765b90aeb9 feat: implement ToTokens for UpdatePlan 2025-07-16 12:16:23 -03:00
Levy A.
9ff9c3fdc2 feat: add context to ToTokens 2025-07-16 12:12:15 -03:00
Diego Reis
b86674adbb Remove cache clearing in cacheflush 2025-07-16 11:11:52 -03:00
Jussi Saurio
8558675c4c page cache: pin pages on the stack 2025-07-16 17:09:05 +03:00
Diego Reis
5dd571483f Add cacheflush to Rust binding 2025-07-16 11:08:52 -03:00
Diego Reis
817ad8d50f Separate user-callable cacheflush from internal cacheflush logic
Cacheflush should only spill pages to WAL as non-commit frames, without checkpointing nor syncing. Check SQLite's sqlite3PagerFlush
2025-07-16 11:08:50 -03:00
Jussi Saurio
f7b9265c26 btree: fix trying to go upwards when at end of btree 2025-07-16 16:58:42 +03:00
Jussi Saurio
e0d797aac0 btree: use node_states instead of cell_indices (tracks cell count too) 2025-07-16 16:58:41 +03:00
Jussi Saurio
f0145fef5c btree: create BTreeNodeState struct for tracking cell idx and count 2025-07-16 16:58:11 +03:00
Jussi Saurio
ac065a79bb btree: fix post-balancing seek bug in delete path 2025-07-16 14:23:46 +03:00
Pekka Enberg
93634d56ba Turso 0.1.3-pre.1 2025-07-16 13:16:57 +03:00
Pekka Enberg
84d8842fbe Merge 'btree: fix interior cell replacement in btrees with depth >=3' from Jussi Saurio
## Background
When a divider cell is deleted from an index interior page, the
following algorithm is used:
1. Find predecessor: Move to largest key in left subtree of the current
page. This is always a leaf page.
2. Create replacement: Convert this predecessor leaf cell to interior
cell format, using original cell's left child page pointer
3. Replace: Drop original cell from parent page, insert replacement at
same position
4. Cleanup: Delete the taken predecessor cell from the leaf page
<img width="845" height="266" alt="Screenshot 2025-07-16 at 10 39 18"
src="https://github.com/user-
attachments/assets/30517da4-a4dc-471e-a8f5-c27ba0979c86" />
## The faulty code leading to the bug
The error in our logic was that we always expected to only traverse down
one level of the btree:
```rust
let parent_page = self.stack.parent_page().unwrap();
let leaf_page = self.stack.top();
```
This meant that when the deletion happened on say, level 1, and the
replacement cell was taken from level 3, we actually inserted the
replacement cell into level 2 instead of level 1.
## Manifestation of the bug in issue 2106
In #2106, this manifested as the following chain of pages, going from
parent to children:
3 -> 111 -> 119
- Cell to be deleted was on page 3 (whose left pointer is 111)
- Going to the largest key in the left subtree meant traversing from 3
to 111 and then from 111 to 119
- a replacement cell was taken from 119
- incorrectly inserted into 111
- and its left child pointer also set as 111!
- now whenever page 111 wanted to go to its left child page, it would
just traverse back to itself, eventually causing a crash because we have
a hard limit of the number of pages on the page stack.
## The fix
The fix is quite trivial: store the page we are on before we start
traversing down.
Closes #2106

Closes #2108
2025-07-16 13:15:54 +03:00
Pekka Enberg
7d94aea3d5 Merge 'make unixepoch to return i64' from Nikita Sivukhin
See no reason why it will be string
```
sqlite> select abs(unixepoch());
1752660221

turso> select abs(unixepoch());
┌────────────────────┐
│ abs (unixepoch ()) │
├────────────────────┤
│                0.0 │
└────────────────────┘
```

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

Closes #2112
2025-07-16 13:15:30 +03:00