Repository navigation
docs(design): describe the query cursor as the row comparison the code emits #428
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
6924eb3
78f5d83
a6fd620
6482fb5
a8841ce
fc0083a
8187363
8189010
2cf975e
0d18433
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
|
@@ -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 | ||
| 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:** | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added in a6fd620 as an "Index Scan" block. It shows
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| ```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) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| 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 | ||
|
|
@@ -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 | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A hash-only index has no ordering index.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| 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. | ||
|
|
||
There was a problem hiding this comment.
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_PAGEinlsi_query_plan.rs:71, sent as SQLLIMIT 11), and "Limit 101" counts the SQLLIMITof a Query Limit of 100. Both plans hold as measured. "Query Limit 10" and "Query Limit 100" would use one unit.There was a problem hiding this comment.
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.