Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion crates/storage-sqlite/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ Key differences from the PostgreSQL backend:
- GSI propagation via a persistent `gsi_pending` queue with a configurable delay
(a delay of 0 applies updates synchronously on the write path); LSI
propagation is synchronous
- OR-expansion for pagination (SQLite lacks row-value tuple comparison)
- The pagination cursor is the same row-value comparison as on PostgreSQL,
with `?` placeholders and no `COLLATE` (SQLite's default `BINARY` collation
already compares text by byte value)

It shares the PostgreSQL backend's catalog version gate: the server refuses to
start unless its compiled `CATALOG_VERSION` matches the stored `catalog_version`.
Expand Down
131 changes: 106 additions & 25 deletions docs/design/04-component-storage.md
Original file line number Diff line number Diff line change
Expand Up @@ -409,10 +409,12 @@ The PostgreSQL backend uses two categories of tables:
same way). The query builders also qualify every string key predicate and
`ORDER BY` with `COLLATE "C"`, so results are byte-ordered on tables
created before the columns carried the collation; on those tables the
indexes cannot serve the predicates (see technical debt C-11). The same
columns on index tables (`pk`, `sk_s`, `base_pk`, `base_sk_s`) carry the
collation, so a GSI or LSI cursor is a row comparison the ordering index
serves in one seek.
indexes cannot serve the predicates (see technical debt C-11). On index
tables created with the collated key columns, the text key columns (`pk`,
`sk_s`, `base_pk`, `base_sk_s` and their numbered variants) carry the
collation. A GSI or LSI Query cursor there is served in one seek, by the
ordering index, or by the primary key on a hash-only GSI (section 7 has the
Scan cases).

- **Item storage**: `item_data` JSONB contains the complete item including key
attributes, matching the DynamoDB model where key attributes are part of the
Expand Down Expand Up @@ -733,40 +735,90 @@ serialized as standard DynamoDB JSON.
pub type PaginationKey = BTreeMap<String, AttributeValue>;
```

The storage backend translates this to a SQL `WHERE` clause:

**Base table pagination (forward scan):**
The PostgreSQL and SQLite backends translate this to a SQL `WHERE` clause. The
cursor compares the same columns the statement's `ORDER BY` sorts on, in the
same order and in the same direction. So the predicate is one row comparison,
`>` for a forward page and `<` for a reverse one. It is a plain comparison when
the cursor has one column. In every shape below, a key index whose columns
follow the cursor's serves the page. The page comes out in order with no sort
step, except the Scan on an index with more than one RANGE attribute (see
Index Scan). For the base table Scan and Query that index is the primary key.
For a GSI or LSI it is the
ordering index `(pk, sk_*, base_pk, base_sk_*)`. A hash-only index has no
ordering index, because `create_index_data_table` creates one only when the
index declares a sort key. Its page comes from the primary key
`(pk, base_pk, base_sk_*)` or from the base-key index `(base_pk, base_sk_*)`
with a filter on `pk`. All of this holds on tables created with the collated
key columns. On older PostgreSQL tables the key columns keep the database
collation, so the indexes do not serve a `COLLATE "C"` predicate or
`ORDER BY`. The statements put `COLLATE "C"` on `base_pk` and on every string
sort key column. Where the first cursor column other than `pk` is one of
these, PostgreSQL reads the rows through the `pk` prefix of the key index. It
then filters the cursor and sorts the rest, on every page (technical debt
C-11). Where a later cursor column is one of these, PostgreSQL seeks on the
leading columns and sorts the rest (Incremental Sort). The statements below
show PostgreSQL. SQLite emits the same shape with `?` placeholders and without
`COLLATE`, because its text columns already compare by byte value.

The plans here are for the statements shown, which carry no sort key
condition. With an equality on the index sort key, PostgreSQL planned a Sort
over a Bitmap Index Scan on every GSI page. A `BETWEEN` kept the Index Scan.
On an LSI with 50 items per sort key value, both plans of each page with an
equality were an Index Scan at Limit 10. At Limit 101, the custom plan of the

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional. "Limit 10" here counts the Query Limit (GROUP_PAGE in lsi_query_plan.rs:71, sent as SQL LIMIT 11), and "Limit 101" counts the SQL LIMIT of a Query Limit of 100. Both plans hold as measured. "Query Limit 10" and "Query Limit 100" would use one unit.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in #1485, since this PR merged first. The sentence now names Query Limit 10 and Query Limit 100. The plans stay as measured.

first page was a Sort over a Bitmap Heap Scan. SQLite seeks on the condition's
terms and applies the cursor as a filter.

**Base table Scan:**

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

scan_impl on an index compares the full index row key, (pk, sk_s COLLATE "C", base_pk COLLATE "C", base_sk_n) > (...) with the same ORDER BY, and (pk, base_pk COLLATE "C", base_sk_n) on a hash-only index. The LSI Scan includes base_pk, which the LSI Query drops, and PostgreSQL serves it from the ordering index with no sort node. Please add an "Index Scan" block.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in a6fd620 as an "Index Scan" block. It shows (pk, sk_s COLLATE "C", base_pk COLLATE "C", base_sk_n) > (...) with the same ORDER BY for a GSI or LSI with one sort key, and (pk, base_pk COLLATE "C", base_sk_n) > (...) for a hash-only index. It notes that the LSI Scan keeps base_pk. EXPLAIN: the LSI Scan is one Index Scan on the ordering index with no sort node. The hash-only index Scan is one Index Scan on the primary key. The block also records that a Scan on an index with more than one RANGE attribute orders by the first RANGE column only. The ordering index does not serve that order without a sort step (Incremental Sort on PostgreSQL, temporary B-tree on SQLite). That is a code gap, filed separately.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code gap is #1253. No unit test pins the index Scan cursor. Changing its > to >= in scan_impl passes every unit test in both crates. On the wire I put that change into the two SQLite scan_impl branches for an index over a base table with a sort key. Two cases of tests/test_query_scan.py then fail: test_index_scan_pagination_uses_base_key_schema (15 items for 12) and test_scan_paginate_returns_all (13 for 7). test_gsi_scan_pagination_exactly_once in tests/test_read_paging.py passes with the change. Its index and base table are hash-only, so its cursor is the (pk, base_pk) branch, which the change does not touch. #1254 now lists the index Scan cursor with this measurement.

```sql
WHERE (pk = $last_pk AND sk_n > $last_sk)
OR pk > $last_pk
WHERE (pk, sk_n) > ($last_pk, $last_sk)
ORDER BY pk, sk_n
```

**Base table pagination (reverse scan):**
**Base table Query:** the key condition fixes `pk`, so the sort key alone is
the cursor. `ScanIndexForward = false` flips the comparison and the sort:
```sql
WHERE pk = $pk AND sk_n > $last_sk
ORDER BY sk_n ASC
```
```sql
WHERE (pk = $last_pk AND sk_n < $last_sk)
OR pk < $last_pk
WHERE pk = $pk AND sk_n < $last_sk
ORDER BY sk_n DESC
```

**GSI pagination (forward scan):**
GSI keys are not unique, so the base table primary key is used as a tiebreaker.
Every column of the cursor runs in the same direction as the `ORDER BY`, so the
predicate is one row comparison, which the ordering index on
`(pk, sk_s, base_pk, base_sk_*)` serves as a single seek:
**GSI Query (forward):**
GSI keys are not unique, so the base table primary key follows the index sort
key as a tiebreaker, in the cursor and in the `ORDER BY`:
```sql
WHERE pk = $gsi_pk
AND (sk_s COLLATE "C", base_pk COLLATE "C", base_sk_n) > ($gsi_sk, $base_pk, $base_sk)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On main, #429 adds one column per RANGE attribute ahead of the base key: order_by_columns emits (sk_s COLLATE "C", sk2_n, sk3_s COLLATE "C", base_pk COLLATE "C", base_sk_s COLLATE "C") (test multi_range_gsi_orders_and_resumes_over_every_range_attribute). Live DynamoDB orders a GSI with RANGE r1 (S), r2 (N) by r1, then r2 numerically, and main matches. Please add the multi-RANGE form after a rebase, for example (sk_s COLLATE "C", sk2_n, base_pk COLLATE "C", base_sk_n).

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merged main (#429 included) in a6fd620. The section now has a "GSI with more than one RANGE attribute" block with the (sk_s COLLATE "C", sk2_n, base_pk COLLATE "C", base_sk_n) cursor and its ORDER BY. I ran EXPLAIN on a GSI with RANGE r1 (S) and r2 (N) over a base table with a numeric sort key. The plan is one Index Scan on the ordering index (pk, sk_s, sk2_n, base_pk, base_sk_n). That holds forward and backward. The row comparison sits in the Index Cond and there is no sort node.

ORDER BY sk_s COLLATE "C", base_pk COLLATE "C", base_sk_n
ORDER BY sk_s COLLATE "C" ASC, base_pk COLLATE "C" ASC, base_sk_n ASC
```

For a reverse query (`ScanIndexForward = false`) the index sort key runs
backwards while the base-key tiebreaker stays ascending, so the cursor keeps
the expanded form:
**GSI Query (reverse, `ScanIndexForward = false`):** every column runs
backwards, the base-key tiebreaker included, so the cursor stays a row
comparison and the page sequence is the exact mirror of the forward one. The
service has the same mirror property: a reverse Query returns the forward
order reversed, among items that share an index sort key and among the items
of a hash-only index partition (measured 2026-09-17). An ascending tiebreaker
under a descending sort key is not a mirror, so the reverse cursor compares
every column, the tiebreaker included, with `<`.
```sql
WHERE pk = $gsi_pk
AND (sk_s COLLATE "C" < $gsi_sk
OR (sk_s COLLATE "C" = $gsi_sk
AND (base_pk COLLATE "C" > $base_pk
OR (base_pk = $base_pk AND base_sk_n > $base_sk))))
AND (sk_s COLLATE "C", base_pk COLLATE "C", base_sk_n) < ($gsi_sk, $base_pk, $base_sk)
ORDER BY sk_s COLLATE "C" DESC, base_pk COLLATE "C" DESC, base_sk_n DESC
```

**GSI with more than one RANGE attribute:** every RANGE attribute of the index
key schema has its own column (`sk_s`, `sk2_n`, ...), in key-schema order. All
of them precede the base key in the cursor and in the `ORDER BY`. A GSI with
RANGE attributes `r1` (S) and `r2` (N) over a base table with a numeric sort
key compares four columns. The ordering index
`(pk, sk_s, sk2_n, base_pk, base_sk_n)` serves the page in one seek in both
directions:
```sql
WHERE pk = $gsi_pk
AND (sk_s COLLATE "C", sk2_n, base_pk COLLATE "C", base_sk_n) > ($r1, $r2, $base_pk, $base_sk)
ORDER BY sk_s COLLATE "C" ASC, sk2_n ASC, base_pk COLLATE "C" ASC, base_sk_n ASC
```

**LSI Query:** an LSI cursor compares the index sort key, then `base_pk`, then
Expand All @@ -791,6 +843,35 @@ WHERE pk = $lsi_pk
ORDER BY sk_s COLLATE "C" ASC, base_pk COLLATE "C" ASC, base_sk_n ASC
```

**Hash-only index:** there is no index sort key, so the cursor is the base

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A hash-only index has no ordering index. create_index_data_table creates it only when the index declares a sort key. The primary key (pk, base_pk, base_sk_*) serves this page (Index Scan using _ddb_hgsi_pkey).

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in a6fd620. The intro now says a hash-only index has no ordering index, because create_index_data_table creates one only when the index declares a sort key. Its page comes from the primary key (pk, base_pk, base_sk_*) or from the base-key index (base_pk, base_sk_*) with a filter on pk. On the tables the server created here, PostgreSQL chose the base-key index with Filter: (pk = 'h1'), and SQLite the primary key. Neither plan has a sort node.

primary key alone, `(base_pk COLLATE "C", base_sk_n) > ($base_pk, $base_sk)`
forward and `<` in reverse.

**Index Scan:** a Scan on an index has no fixed partition, so the cursor
compares the whole index row key, `pk` first. On a GSI or LSI with one sort
key the cursor is
`(pk, sk_s COLLATE "C", base_pk COLLATE "C", base_sk_n) > ($last_pk, $last_sk, $base_pk, $base_sk)`,
with `ORDER BY pk, sk_s COLLATE "C", base_pk COLLATE "C", base_sk_n`. On a
hash-only index it is `(pk, base_pk COLLATE "C", base_sk_n) > ($last_pk, $base_pk, $base_sk)`,
with `ORDER BY pk, base_pk COLLATE "C", base_sk_n`. The ordering index serves
the LSI Scan with no sort step. The primary key serves the hash-only index
Scan. A Scan on an index
with more than one RANGE attribute compares and orders by the first RANGE
column only (`sk_s`, `sk_n` or `sk_b`), ahead of `base_pk`. That is still a
total order, because the cursor holds the full base key. The ordering index
does not serve it without a sort step: PostgreSQL adds an Incremental Sort,
SQLite a temporary B-tree.

On a base table without a sort key, the Scan cursor is `pk > $last_pk` with
`ORDER BY pk`. A Query with an `ExclusiveStartKey` on such a table is refused
with `ValidationException` (`The provided exclusive start key is invalid`)
before it reaches storage (`crates/engine/src/start_key.rs`), because the key
condition fixes the only key column. The `base_sk_*` column drops out of every
index cursor, Query and Scan alike. A GSI Query cursor is then
`(sk_s COLLATE "C", base_pk COLLATE "C")`, a
hash-only index Query cursor is `base_pk COLLATE "C"` alone, and an LSI cannot
exist.

For GSI queries, the pagination key includes both the GSI key attributes and the base table primary key (needed to
uniquely identify the position, since GSI keys are not unique). This is why the GSI PostgreSQL table includes `base_pk`
and `base_sk_*` as actual columns.
Expand Down
Loading