Repository navigation
fix(core): keep the pivot join pinned for taxonomy listings with a temp sort - #3409
Conversation
🦋 Changeset detectedLatest commit: 9bc0364 The changes in this PR will be included in the next version bump. This PR includes changesets to release 12 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
@emdash-cms/admin
@emdash-cms/auth
@emdash-cms/auth-atproto
@emdash-cms/blocks
@emdash-cms/cloudflare
@emdash-cms/contentful-to-portable-text
emdash
create-emdash
@emdash-cms/gutenberg-to-portable-text
@emdash-cms/plugin-cli
@emdash-cms/plugin-test
@emdash-cms/plugin-types
@emdash-cms/registry-client
@emdash-cms/registry-lexicons
@emdash-cms/registry-loader
@emdash-cms/registry-moderation
@emdash-cms/registry-verification
@emdash-cms/sandbox-workerd
@emdash-cms/x402
@emdash-cms/plugin-ai-moderation
@emdash-cms/plugin-atproto
@emdash-cms/plugin-audit-log
@emdash-cms/plugin-color
@emdash-cms/plugin-embeds
@emdash-cms/plugin-field-kit
@emdash-cms/plugin-forms
@emdash-cms/plugin-webhook-notifier
commit: |
213edfe to
225dd4d
Compare
There was a problem hiding this comment.
The approach is correct and targeted. buildTaxonomyPivotQuery should only let the planner reorder the picked CTE when the sort key has an index that supports stopping early at LIMIT (published_at/created_at). For updated_at and field sorts the temp-sort branch collects every tagged entry, so releasing the pin allows D1 to walk the collection index; restoring CROSS JOIN forces content_taxonomies as the outer table. The conditional isPostgres(db) || isIndexedSort mirrors the join-order pinning pattern already used elsewhere in this file (e.g., the byline fold), and the dialect-agnostic unit tests cover both the indexed and temp-sort paths.
I read the diff, the surrounding loader code, the integration plan test, the unit pivot test, the changeset, and the relevant AGENTS.md conventions. The implementation is sound and the SQL is safe. The remaining items are documentation/comment polish: the changeset references issue #3300 and speaks in implementation terms, and the new test comment repeats PR narrative that will be stale to a future reader.
| "emdash": patch | ||
| --- | ||
|
|
||
| Fixes taxonomy-filtered listings sorted by `updated_at` or a field reading the whole collection on D1. The plain join from #3300 now applies only to `published_at` and `created_at` sorts, where the query can stop at its limit; other sorts start from the term's assignments again. |
There was a problem hiding this comment.
[suggestion] The issue reference #3300 and the "plain join" wording are implementation history, not guidance for someone reading the CHANGELOG. The first sentence already names the user-facing problem. Keep that and restate the affected sorts in terms of what readers can observe.
| Fixes taxonomy-filtered listings sorted by `updated_at` or a field reading the whole collection on D1. The plain join from #3300 now applies only to `published_at` and `created_at` sorts, where the query can stop at its limit; other sorts start from the term's assignments again. | |
| Fixes taxonomy-filtered listings sorted by `updated_at` or a custom field reading the whole collection on D1. `published_at` and `created_at` sorts still use the indexed path that stops at `LIMIT`; `updated_at` and field sorts now seek from the term's assignments again. |
| // The plan itself is not asserted here: which side SQLite drives from with | ||
| // a plain JOIN depends on planner statistics, and D1 chose the collection | ||
| // (10,471 rows, 3x the rows read) where local SQLite with the same | ||
| // `sqlite_stat1` chose the pivot. So this pins the join the builder emits. |
There was a problem hiding this comment.
[suggestion] This comment is addressed to the PR reader, not a future maintainer: it explains why the plan-assertion approach was rejected and repeats production row counts from the incident. The invariant the test enforces is important, but the surrounding narrative will be stale once the PR is merged. Trim it to the stable reason.
| // The plan itself is not asserted here: which side SQLite drives from with | |
| // a plain JOIN depends on planner statistics, and D1 chose the collection | |
| // (10,471 rows, 3x the rows read) where local SQLite with the same | |
| // `sqlite_stat1` chose the pivot. So this pins the join the builder emits. | |
| // `EXPLAIN QUERY PLAN` differs between D1 and local SQLite for the same plain | |
| // JOIN, so this test pins the join the builder emits as the stable contract. |
|
Both suggestions applied in 9bc0364: the changeset now describes what readers observe, and the test comment states the stable reason only. oxfmt clean, pivot tests pass (26). |
There was a problem hiding this comment.
The approach remains correct and targeted. The previous review's two documentation/comment findings have been addressed: the test comment is now a concise, stable explanation of why the test asserts the emitted join rather than the query plan, and the changeset no longer references implementation-history issue numbers or speaks in diff terms. The implementation safely restores the CROSS JOIN pin for the temp-sort branch on SQLite/D1 while keeping the indexed-sort branch on JOIN; tests cover updated_at, field, and published_at sorts; locale filtering and status/deleted re-checks are unchanged; and the changeset is user-facing and helpful.
The one remaining item is a comment gap in packages/core/src/loader.ts: the explanatory block above pivotContentJoin explains the SQLite planner rationale (indexed sorts want JOIN, temp sorts want CROSS JOIN) but never says why isPostgres(db) is part of the condition. A maintainer reading only the comment could think Postgres is grouped with indexed sorts for performance reasons and might be tempted to remove the check. The byline fold earlier in the same file already documents the same Postgres constraint (CROSS JOIN … ON is rejected), so adding it here is a straightforward consistency fix. I checked AGENTS.md conventions (SQL safety, API envelope, auth, locale filtering on content tables, index discipline, changeset quality, logged-out query counts, and comment discipline) and found no other issues.
Findings
-
[suggestion]
packages/core/src/loader.ts:951-964The
pivotContentJoincondition mixes two unrelated reasons into one expression:isPostgres(db)is there because Postgres rejectsCROSS JOIN … ONsyntax, whileisIndexedSortis there because SQLite/D1 can stop early atLIMITfor indexed sorts. The surrounding comment only explains the SQLite indexed-sort rationale and the temp-sort pin, so the Postgres exception is unexplained. A future reader could easily assumeisPostgres(db)is a performance grouping and remove it.The byline fold earlier in this file already documents the same Postgres constraint (line 136). Add the same note here.
// A plain JOIN lets SQLite reorder the `picked` CTE. For indexed sorts // (`published_at`/`created_at`) the planner can then drive from the // `(deleted_at, <sort> DESC, id DESC)` index on `ec_*`, probe the pivot // by primary key, and short-circuit at `LIMIT`. A `CROSS JOIN` pin would // force `content_taxonomies` as the outer table and require a temp sort, // producing a full nested loop over the collection on D1. Postgres // rejects `CROSS JOIN ... ON`, so it also falls back to a plain JOIN. // // The temp-sort branch keeps the pin. Its `picked` collects every tagged // entry with no `LIMIT`, so starting from `ec_*` can never stop early, and // D1 does start there when `ANALYZE` has underestimated // `deleted_at IS NULL` (a trash with many distinct deletion times): it // walks the collection's `(deleted_at, status)` index and probes the pivot // once per published row. const pivotContentJoin = isPostgres(db) || isIndexedSort ? sql`JOIN` : sql`CROSS JOIN`;
emdash-cms#3409 pinned the pivot join for temp sorts and freed it for every indexed sort. This branch frees it only for a single large term under an indexed sort with a LIMIT, which already keeps temp sorts pinned, so its join condition stays. The comment keeps emdash-cms#3409's reason for the temp-sort pin and drops the claim that D1 keeps no planner statistics, which emdash-cms#3409's production statistics contradict. emdash-cms#3409's join test moves into the large-term block, the only shape whose indexed sort still frees the join. emdash-cms#3510 replaced better-sqlite3, so the plan test uses the node:sqlite helper.
emdash-cms#3409 pinned the pivot join for updated_at and field sorts on main, so those listings no longer walk the collection and this change no longer fixes them. The entry now names only the indexed sorts it changes, and created_at as the default, so it does not repeat emdash-cms#3409's entry in the same release.
…rms (#3402) * fix(core): stop taxonomy listings walking the collection for small terms The plain JOIN in the pivot listing lets SQLite drive every taxonomy listing from the ec_* sort index. That fills a page quickly for a term with many entries, but for a small term it reads up to the whole collection, and the same happens for updated_at sorts and for listings that combine several terms or taxonomies. The term lookup now carries a capped count of the term's pivot rows. Only a single term with at least 200 of them, under a published_at or created_at sort with a LIMIT, walks ec_*. Every other listing seeks the pivot again, as it did before the plain JOIN. The count rides in the existing lookup query, so the query count is unchanged. Measured on local D1 with 20,000 posts, listing plus term lookup: a 2-post term reads 40 rows instead of 20,030, a 19,000-post term 486 instead of 285. * ci: update query-count snapshots * docs(core): narrow the changeset to published_at and created_at sorts #3409 pinned the pivot join for updated_at and field sorts on main, so those listings no longer walk the collection and this change no longer fixes them. The entry now names only the indexed sorts it changes, and created_at as the default, so it does not repeat #3409's entry in the same release. * test(core): cover the size probe's collection scope The size probe counts a term's pivot rows in the listed collection only. Without that condition, a term with 200 or more assignments in another collection would count as large and walk the whole collection for a listing where it tags a single entry. No plan test failed when the condition was removed; this one does, and it fails on main as well. * docs(core): say why Postgres gets the plain pivot join, drop the D1 statistics claim The join comment explained the SQLite choice only, leaving `isPostgres(db)` in the condition unexplained; Postgres rejects `CROSS JOIN ... ON`, as the byline fold in the same file already notes. The plan tests claimed D1 never keeps sqlite_stat1 and that their plans match D1 exactly. A D1 database has statistics once its operator runs ANALYZE or PRAGMA optimize (#3408 measured one). EmDash never runs either, which is why the tests run without them. --------- Co-authored-by: emdashbot[bot] <emdashbot[bot]@users.noreply.github.com>
What does this PR do?
Keeps
buildTaxonomyPivotQuery'sCROSS JOINpin in the temp-sort branch and leaves #3300's plainJOINin the indexed-sort branch:The temp-sort
pickedcollects every tagged entry and has noLIMIT, so starting fromec_*can never stop early. On D1, with the pin released, a collection whose trash holds many distinctdeleted_atvalues starts there anyway and walks every published row: a listings collection on one production site's data went from 4,209 to 13,453 rows read for the same 49 rows. Indexed sorts are equal or better withJOIN, as #3299 measured, and are unchanged by this PR. The measurements and plans are in #3408.Closes #3408
Type of change
Checklist
pnpm typecheckpassespnpm lintpassespnpm testpasses (or targeted tests for my change)pnpm formathas been runmessages.pochanges except in translation PRs — a workflow extracts catalogs on merge tomain.AI-generated code disclosure
Screenshots / test output
Not applicable (no UI).
About the test: I tried to assert the plan, the way
loader-taxonomy-pivot-plan.test.tsdoes. That doesn't work here. With the site'ssqlite_stat1rows loaded into the test database, local SQLite 3.51.0 keeps the pivot outer under a plainJOIN, where D1 does not, so a plan assertion passes onmainand proves nothing. The new test pins the join the builder emits for each branch instead:CROSS JOINforupdated_atand a field sort,JOINforpublished_at. It fails onmainand passes here.🤖 Generated with Claude Code