Skip to content

perf(paging): seek on the primary key for next, previous and last page - #4604

Merged
datlechin merged 9 commits into
mainfrom
perf/keyset-paging
Nov 27, 2026
Merged

datlechin merged 9 commits into
mainfrom
perf/keyset-paging

Conversation

@datlechin

@datlechin datlechin commented Nov 26, 2026 •

Copy link
Copy Markdown
Member

Summary

  • Next, Previous and Last page on a table tab now seek on the primary key instead of skipping rows with OFFSET, so page 5,000 and the last page cost about what page 1 costs. Next reads WHERE key > <last key on screen> ORDER BY key LIMIT n; Previous and Last read in reverse key order and the rows are flipped back before they reach the grid.
  • A seek runs only when the page order is exactly the primary key in one direction (the default key sort or the user's own), the host writes the SQL, and the engine enforces its keys. A new curated capability, seeksKeyedPages, is true for MySQL, MariaDB, PostgreSQL and SQLite only and survives plugin registration. Redshift, Snowflake and BigQuery accept duplicate keys, and KingbaseES opts out of the copy it takes from PostgreSQL.
  • Everything else keeps OFFSET: Go to Page, page 1, a sort on other columns, tables without a key, views, browse search, and plugins that write their own page queries.
  • A key value is used only when it round-trips exactly: integers, whole decimals, and quoted text and dates. Floats, ENUMs, TIMESTAMP, masked cells, number-like text in SQLite, untyped SQLite columns, text holding a NUL (standard escaping drops it) and keys the catalog says can hold NULL fall back to OFFSET. That includes SQLite's lone INTEGER key: INTEGER PRIMARY KEY DESC is not the rowid and takes NULL, and the catalog reports both the same.
  • The edge row is read only when the rows on screen came from the page's own statement, so a page still loading, stopped or failed never seeds a seek. A seek reads its page once, for the move that reached it. Any later read of that page, after a save, a refresh, a tick of auto-refresh or a return to a stale tab, reads it at its offset, so page 1 and the pages beside it stay in step.

Test plan

  • TableKeysetPageQueryTests: the SQL for next, previous, last, descending and composite keys, filters kept in their parentheses, and every refused literal.
  • PaginationCoordinatorTests: next, previous, back to page 1, last, a jump, an unkeyed table, Redshift, a nullable key, a masked key, rows of another page, and re-reads at the offset.
  • MainContentCoordinatorSortTests/backwardPageIsFlipped: a run of the backward statement through a recording driver comes back in key order, and any other statement is left as it arrived; it fails with the flip removed.
  • PluginMetadataRegistryCuratedCapabilityTests: which engines seek, and that the flag survives registration.
  • PaginationCoordinatorTests/nullableKeyKeepsTheOffset now expects a nullable lone INTEGER key to keep the offset, on purpose: the rowid shortcut is gone. TableKeysetPageQueryTests/refusesWhatCannotSeek adds a text key holding a NUL.
  • After the merge with main: verify.sh lint PASS. Build and tests not run locally: the PR is blocked on the findings below, and the shared Mac's disk is below the build floor. CI's macOS Tests runs them on this push.

Open, blocks merge

Codex round 2 and the security pass, on the branch with the fixes above:

  • [high] A seek literal is escaped with the browsing session's string rules, but the page can run on another database's parked or pooled connection. On PostgreSQL with standard_conforming_strings differing between the two, a text key holding \' closes the literal early and runs the rest as SQL. Needs the key bound as a parameter, or a literal form that does not depend on the session (E'...').
  • [medium] The seek statement, with the edge row's key in it, becomes content.query, which saved tabs and query history store. A table keyed by tokens or session ids writes one to disk on Next. Needs the key kept out of the stored query.
  • [P2] A PostgreSQL inheritance parent's key is not unique across its children, which the SELECT reads, so a duplicate on a page edge is skipped. Needs a catalog flag, or no seek on parents.
  • [P2] A SQLite NUMERIC key holding a REAL near an integer prints as 1.0 on older SQLite, so id > 1.0 reads the edge row again. Accepting only digit-only decimal text closes it; DECIMAL(10,2) keys then keep the offset.
  • [P2] Key and sort column names compare with Swift String equality, which joins NFC and NFD spellings PostgreSQL and SQLite keep apart.

Closes #4526

A table page after the first was read with OFFSET, so page N cost about N times page 1. When the page order is the primary key and the engine enforces it (MySQL, MariaDB, PostgreSQL, SQLite), Next, Previous and Last now seek past the edge row on screen, reading backward and flipping the rows for Previous and Last. Everything else keeps OFFSET.
# Conflicts:
#	apple/TablePro/Core/Coordinators/QueryExecutionCoordinator+SingleRun.swift
#	apple/TablePro/Core/Plugins/PluginMetadataRegistry+CuratedDefaults.swift
#	apple/TablePro/Core/Plugins/PluginMetadataRegistry+KingbaseESDefaults.swift
…d tabs and history

A PostgreSQL inheritance parent, a SQLite REAL key and column names that differ only in NFC or NFD spelling keep the offset.
…ollated SQLite keys

The key answer is asked before each keyed read instead of cached.
@datlechin
datlechin disabled auto-merge November 27, 2026 15:08
@datlechin
datlechin enabled auto-merge (squash) November 27, 2026 15:08
@datlechin
datlechin disabled auto-merge November 27, 2026 15:09
@datlechin
datlechin merged commit 065acfd into main Nov 27, 2026
9 of 10 checks passed
@datlechin
datlechin deleted the perf/keyset-paging branch November 27, 2026 15:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf(grid): page table tabs by seeking on the key instead of offset

1 participant