From 6924eb39de491cb0cc42f1e09c4001087bf1fcc3 Mon Sep 17 00:00:00 2001 From: Lee Hannigan Date: Wed, 21 Oct 2026 18:17:36 +0000 Subject: [PATCH 1/6] docs(design): describe the query cursor as the row comparison the code emits The reverse GSI cursor example in docs/design/04-component-storage.md showed an expanded OR with an ascending base-key tiebreaker under a descending sort key. The PostgreSQL and SQLite query builders emit one row comparison for every cursor shape, with every column, tiebreaker included, following ScanIndexForward, so a reverse page is the exact mirror of the forward one as it is on the service. The base table Scan examples in the same section showed an OR form too; the Scan cursor is the row comparison (pk, sk) > ($pk, $sk) and Scan has no reverse direction. Section 7 states the rule (cursor columns equal the ORDER BY columns, same order, same direction, > forward and < reverse, a plain comparison when the cursor has one column), shows the Scan, base table Query, forward GSI and reverse GSI statements with their ORDER BY, and describes the LSI shape (index sort key then base sort key, which the ordering index does not serve without a sort step because it carries base_pk between them), the hash-only index shape, and the forms on a base table without a sort key. The section 5.1 sentence on collation now limits the one-seek claim to GSI cursors. Closes #370 --- docs/design/04-component-storage.md | 78 ++++++++++++++++++++--------- 1 file changed, 55 insertions(+), 23 deletions(-) diff --git a/docs/design/04-component-storage.md b/docs/design/04-component-storage.md index 5587b8ccc..8aea1f455 100755 --- a/docs/design/04-component-storage.md +++ b/docs/design/04-component-storage.md @@ -395,8 +395,8 @@ The PostgreSQL backend uses two categories of 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. + collation, so a GSI cursor is a row comparison the ordering index serves + in one seek (the LSI case is in section 7). - **Item storage**: `item_data` JSONB contains the complete item including key attributes, matching the DynamoDB model where key attributes are part of the @@ -692,42 +692,74 @@ serialized as standard DynamoDB JSON. pub type PaginationKey = BTreeMap; ``` -The storage backend translates this to a SQL `WHERE` clause: - -**Base table pagination (forward scan):** +The storage backend translates 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 (a plain comparison when the cursor has one +column). For the Scan, base table Query, GSI and hash-only index shapes below, +the ordering index serves the page in order with no sort step; the LSI shape is +described after them. PostgreSQL is shown; SQLite emits the same shape with `?` +placeholders and without `COLLATE`, because its text columns already compare by +byte value. + +**Base table Scan:** ```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) -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, and on a hash-only index, where +the tiebreaker is the whole order, an ascending cursor under a descending +`ORDER BY` repeated the first page's items and skipped the rest. ```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 ``` +**LSI Query:** every row of an LSI partition shares the base partition key, so +the cursor and the `ORDER BY` use the index sort key and the base sort key, +`(sk_s COLLATE "C", base_sk_n) > ($lsi_sk, $base_sk)` forward and `<` in +reverse. The ordering index on the LSI table is `(pk, sk_s, base_pk, base_sk_n)`, +so after the fixed `pk` the index orders by `sk_s` only and the database sorts +the rows that share an index sort key value before it can apply the cursor +(PostgreSQL adds an Incremental Sort node, SQLite a temporary B-tree); the +result and page boundaries are unchanged by that step. + +**Hash-only index:** there is no index sort key, so the cursor is the base +primary key alone, `(base_pk COLLATE "C", base_sk_n) > ($base_pk, $base_sk)` +forward and `<` in reverse. + +On a base table without a sort key the `base_sk_*` column drops out of every +index cursor: a GSI cursor is `(sk_s COLLATE "C", base_pk COLLATE "C")`, a +hash-only index 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. From a6fd620c51e976eefb49515a99cb75b24305cf49 Mon Sep 17 00:00:00 2001 From: Lee Hannigan <29015211+LeeroyHannigan@users.noreply.github.com> Date: Fri, 20 Nov 2026 13:54:11 +0000 Subject: [PATCH 2/6] docs(design): qualify the cursor plans by collation and show the index Scan cursor Section 7 of docs/design/04-component-storage.md now names the index that serves each cursor shape and the tables it holds on. The no-sort statement applies to tables created with the collated key columns. The Scan on an index with more than one RANGE attribute still sorts. On older PostgreSQL tables the indexes do not serve a COLLATE "C" predicate. 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 sorts them on every page (technical debt C-11). A hash-only index has no ordering index, so its page comes from the primary key or from the base-key index. The LSI paragraph now says that the database applies the cursor while it reads the index. It then sorts the rows that share an index sort key value and applies the LIMIT. EXPLAIN (ANALYZE) on PostgreSQL 16.15 shows the row comparison in the Index Cond under an Incremental Sort with Presorted Key sk_s. EXPLAIN QUERY PLAN on SQLite shows SEARCH (pk=? AND sk_s>?) and then a temporary B-tree for the last ORDER BY term. The section gains three blocks. The cursor of a GSI with more than one RANGE attribute, which #429 added to main. The index Scan cursor (pk, sk, base_pk, base_sk) with its ORDER BY. The hash-only base table cases: the Scan cursor is pk > $last_pk, and a Query with an ExclusiveStartKey returns no items without a statement. The intro names the PostgreSQL and SQLite backends, because MongoDB and Cassandra do not translate the cursor to SQL. Each plan statement comes from EXPLAIN on tables the server created and on copies whose key columns keep the database collation. The SQLite plans come from EXPLAIN QUERY PLAN on the database the server created. --- docs/design/04-component-storage.md | 93 ++++++++++++++++++++++------- 1 file changed, 71 insertions(+), 22 deletions(-) diff --git a/docs/design/04-component-storage.md b/docs/design/04-component-storage.md index f6ffeae77..5c9f9c439 100755 --- a/docs/design/04-component-storage.md +++ b/docs/design/04-component-storage.md @@ -409,10 +409,11 @@ 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 cursor is a row comparison the ordering index serves - in one seek (the LSI case is in section 7). + indexes cannot serve the predicates (see technical debt C-11). On index + tables created with the collated key columns, the same columns (`pk`, + `sk_s`, `base_pk`, `base_sk_s`) carry the collation. A GSI cursor there is + a row comparison the ordering index serves in one seek (the LSI case is in + section 7). - **Item storage**: `item_data` JSONB contains the complete item including key attributes, matching the DynamoDB model where key attributes are part of the @@ -733,15 +734,29 @@ serialized as standard DynamoDB JSON. pub type PaginationKey = BTreeMap; ``` -The storage backend translates 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 (a plain comparison when the cursor has one -column). For the Scan, base table Query, GSI and hash-only index shapes below, -the ordering index serves the page in order with no sort step; the LSI shape is -described after them. PostgreSQL is shown; SQLite emits the same shape with `?` -placeholders and without `COLLATE`, because its text columns already compare by -byte value. +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 the Scan, base table Query, GSI and hash-only +index shapes 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 Scan and +the base table Query that index is the primary key. For a GSI 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). The statements below show PostgreSQL. SQLite emits the same shape with +`?` placeholders and without `COLLATE`, because its text columns already +compare by byte value. **Base table Scan:** ```sql @@ -784,22 +799,56 @@ WHERE pk = $gsi_pk ORDER BY sk_s COLLATE "C" DESC, base_pk COLLATE "C" DESC, base_sk_n DESC ``` -**LSI Query:** every row of an LSI partition shares the base partition key, so -the cursor and the `ORDER BY` use the index sort key and the base sort key, +**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:** every row of an LSI partition shares the base partition key. So +the cursor and the `ORDER BY` use the index sort key and the base sort key: `(sk_s COLLATE "C", base_sk_n) > ($lsi_sk, $base_sk)` forward and `<` in reverse. The ordering index on the LSI table is `(pk, sk_s, base_pk, base_sk_n)`, -so after the fixed `pk` the index orders by `sk_s` only and the database sorts -the rows that share an index sort key value before it can apply the cursor -(PostgreSQL adds an Incremental Sort node, SQLite a temporary B-tree); the -result and page boundaries are unchanged by that step. +so after the fixed `pk` it orders by `sk_s` only. The database applies the +cursor while it reads the index. It then sorts the rows that share an index +sort key value by the base sort key, and applies the `LIMIT` after that sort. +PostgreSQL adds an Incremental Sort node with `Presorted Key: sk_s`, and SQLite +a temporary B-tree for the last `ORDER BY` term. The first page sorts the same +way. The sort changes neither the result nor the page boundaries. **Hash-only index:** there is no index sort key, so the cursor is the base primary key alone, `(base_pk COLLATE "C", base_sk_n) > ($base_pk, $base_sk)` forward and `<` in reverse. -On a base table without a sort key the `base_sk_*` column drops out of every -index cursor: a GSI cursor is `(sk_s COLLATE "C", base_pk COLLATE "C")`, a -hash-only index cursor is `base_pk COLLATE "C"` alone, and an LSI cannot exist. +**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 LSI Scan keeps +`base_pk`, which the LSI Query drops, so the ordering index serves it 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 returns no +items and runs no statement, 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` From a8841ce4059b2731c916cd628d4f2c1c88559799 Mon Sep 17 00:00:00 2001 From: Lee Hannigan <29015211+LeeroyHannigan@users.noreply.github.com> Date: Tue, 24 Nov 2026 02:29:35 +0000 Subject: [PATCH 3/6] docs(storage-sqlite): describe the pagination cursor as the row comparison the code emits The README listed OR-expansion for pagination as a difference from the PostgreSQL backend, because SQLite lacked row-value comparison. The SQLite builder emits the same row comparison as PostgreSQL, with ? placeholders and no COLLATE, in scan_impl and append_query_pagination of crates/storage-sqlite/src/data/query_scan.rs. The line now says so. --- crates/storage-sqlite/README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/crates/storage-sqlite/README.md b/crates/storage-sqlite/README.md index e9c29daf5..60e62c24c 100644 --- a/crates/storage-sqlite/README.md +++ b/crates/storage-sqlite/README.md @@ -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`. From fc0083af4272bf8302c0a03f0fc0c5e922e1528d Mon Sep 17 00:00:00 2001 From: Lee Hannigan <29015211+LeeroyHannigan@users.noreply.github.com> Date: Tue, 24 Nov 2026 02:29:35 +0000 Subject: [PATCH 4/6] docs(design): limit the cursor plan statements to the statements shown Section 7 said the primary key serves "the Scan", which covered index Scans. The ordering index serves a GSI or LSI Scan. It now says the base table Scan and Query. The plan statements hold for the statements shown, which carry no sort key condition. A paragraph after the intro says what PostgreSQL and SQLite plan with one. PostgreSQL planned a Sort on every page with an equality on the index sort key, and SQLite seeks on the condition's terms. The legacy-table text gains the case where a later cursor column carries the collation (an Incremental Sort). The reverse GSI paragraph states the rule for the tiebreaker in place of the history of a fixed defect. Section 5.1 named four collated columns and said the ordering index serves every GSI cursor in one seek. A hash-only GSI has no ordering index, and the numbered sort key columns carry the collation too. The sentence now names the text key columns and both indexes. --- docs/design/04-component-storage.md | 31 ++++++++++++++++++----------- 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/docs/design/04-component-storage.md b/docs/design/04-component-storage.md index 5c9f9c439..e7b3f4842 100755 --- a/docs/design/04-component-storage.md +++ b/docs/design/04-component-storage.md @@ -410,10 +410,11 @@ The PostgreSQL backend uses two categories of tables: `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). On index - tables created with the collated key columns, the same columns (`pk`, - `sk_s`, `base_pk`, `base_sk_s`) carry the collation. A GSI cursor there is - a row comparison the ordering index serves in one seek (the LSI case is in - section 7). + 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 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 LSI and + 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 @@ -741,8 +742,8 @@ same order and in the same direction. So the predicate is one row comparison, the cursor has one column. In the Scan, base table Query, GSI and hash-only index shapes 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 Scan and -the base table Query that index is the primary key. For a GSI it is the +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 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 @@ -754,9 +755,16 @@ collation, so the indexes do not serve a `COLLATE "C"` predicate or 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). The statements below show PostgreSQL. SQLite emits the same shape with -`?` placeholders and without `COLLATE`, because its text columns already -compare by byte value. +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 +on every page (over a Bitmap Index Scan on a GSI, over the Index Scan on an +LSI) while a `BETWEEN` kept the Index Scan, and SQLite seeks on the +condition's terms and applies the cursor as a filter. **Base table Scan:** ```sql @@ -790,9 +798,8 @@ 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, and on a hash-only index, where -the tiebreaker is the whole order, an ascending cursor under a descending -`ORDER BY` repeated the first page's items and skipped the rest. +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", base_pk COLLATE "C", base_sk_n) < ($gsi_sk, $base_pk, $base_sk) From 81873637ef3c2c8a949883e0b54b2cd9f03093d6 Mon Sep 17 00:00:00 2001 From: Lee Hannigan <29015211+LeeroyHannigan@users.noreply.github.com> Date: Tue, 24 Nov 2026 05:19:34 +0000 Subject: [PATCH 5/6] docs(design): say that a Query resuming on a table without a sort key is refused Section 7 said that a Query with an ExclusiveStartKey on a table without a sort key returns no items and runs no statement. The engine refuses it before storage: start_key.rs answers ValidationException with The provided exclusive start key is invalid. The sentence now says so. --- docs/design/04-component-storage.md | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/docs/design/04-component-storage.md b/docs/design/04-component-storage.md index e7b3f4842..439fe46bb 100755 --- a/docs/design/04-component-storage.md +++ b/docs/design/04-component-storage.md @@ -850,10 +850,12 @@ 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 returns no -items and runs no statement, 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 +`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. From 2cf975e2362513e098660f48039897f78da61937 Mon Sep 17 00:00:00 2001 From: Lee Hannigan <29015211+LeeroyHannigan@users.noreply.github.com> Date: Tue, 24 Nov 2026 12:42:54 +0000 Subject: [PATCH 6/6] docs(design): describe the LSI cursor main has since #432 #432 added base_pk to the LSI Query cursor and to its ORDER BY on PostgreSQL and SQLite. Three passages of this branch still described the cursor without it. - Section 5.1 names the LSI Query cursor beside the GSI one. Section 7 keeps the Scan cases. - The section 7 introduction says that a key index serves every shape below, and that for a GSI or LSI it is the ordering index. - The plan paragraph gives the LSI plans of the plan test that #432 added. Both plans of each page with an equality are an Index Scan at Limit 10. At Limit 101 the custom plan of the first page is a Sort over a Bitmap Heap Scan. --- docs/design/04-component-storage.md | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/docs/design/04-component-storage.md b/docs/design/04-component-storage.md index 8d0d67702..02e1e3be3 100755 --- a/docs/design/04-component-storage.md +++ b/docs/design/04-component-storage.md @@ -412,8 +412,8 @@ The PostgreSQL backend uses two categories of tables: 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 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 LSI and + 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 @@ -739,11 +739,11 @@ 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 the Scan, base table Query, GSI and hash-only -index shapes 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 it is the +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 @@ -762,9 +762,11 @@ show PostgreSQL. SQLite emits the same shape with `?` placeholders and without 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 -on every page (over a Bitmap Index Scan on a GSI, over the Index Scan on an -LSI) while a `BETWEEN` kept the Index Scan, and SQLite seeks on the -condition's terms and applies the cursor as a filter. +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:** ```sql