Skip to content

fix(query): advanced queries skip recycled nodes, text property node values match, :current-page in a zoomed block - #13894

Closed
yuxi-liu-wired wants to merge 4 commits into
logseq:masterfrom
yuxi-liu-wired:fix/dbtest-query
Closed

yuxi-liu-wired wants to merge 4 commits into
logseq:masterfrom
yuxi-liu-wired:fix/dbtest-query

Conversation

@yuxi-liu-wired

@yuxi-liu-wired yuxi-liu-wired commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Fixes logseq/db-test#1208, logseq/db-test#1085 and logseq/db-test#1383.

Three independent query fixes on branch fix/dbtest-query, one commit each (a PR can take them together or split them by commit).

logseq/db-test#1085: a text property with nodes could not be queried for those nodes

What the user sees

A text property with multiple values can hold plain text and nodes ([[x]]). (property note [[x]]) found the blocks whose value is the plain text x but not the ones where x is a node.

Cause

A node in a text property is a value block titled [[<uuid of x>]]. The query strips [[x]] to the name x, and the ref->val rule (deps/db/src/logseq/db/frontend/rules.cljc) compares it with the value block's :block/title, which only equals x for plain text.

Change

ref->val gets a third branch: a value block whose whole title is a reference to a node ([[<uuid>]] in :block/refs) has that node's title as its value, as for a :node property. The change does not touch query_dsl.cljs, so it does not interact with #13871. The extra branch costs one :block/refs lookup per property value.

Tests

property-default-type-node-value-queries in frontend.db.query-dsl-test: a text property with a plain x, a node x and a plain y. (property note [[x]]) and (property note x) both find the plain value and the node; (property note [[y]]) finds only the y block. Fails without the change (the node block is missing), passes with it.

logseq/db-test#1383: :current-page in a query inside a zoomed-in block used the block's title

What the user sees

A query that uses :current-page returns nothing, or the result for an unrelated page with the same name as the zoomed block, once you zoom into the block that holds it. Unzooming gives the right result again.

Cause

page-render-config (src/main/frontend/components/page.cljs) takes :current-page-title from the rendered entity. When zoomed into a block, that entity is the block, so its title was used as the page name.

Change

:current-page-title is the title of the entity's :block/page when it has one, else its own title (a page).

Tests

query-current-page-is-the-page-holding-a-zoomed-block-test in frontend.components.page-first-paint-test: a page gives its own title, a block gives the title of its page. Fails without the change ("Milestone" instead of "Host page"), passes with it.

logseq/db-test#1208: recycled pages and blocks still affected advanced queries

What the user sees

After deleting a page or block (moved to the Recycle page), advanced queries still counted it inside joins, not clauses and aggregates until it was permanently deleted. For example, with the issue's "leaf streams" query, deleting the only child of a stream did not make the parent a leaf; the child was hidden from the output, so the parent was simply missing.

Cause

Recycling keeps every attribute of the entity (including user node property values), puts :logseq.property/deleted-at on the root only, and its descendants carry nothing. execute-custom-query (src/main/frontend/worker/handler/query.cljs) ran the query on the full db and only dropped recycled rows from the final output.

Change

execute-custom-query runs the query on a d/filtered db with no datom of a recycled root or its descendants and none that points to one (only for ref attributes: the schema's and the ref-typed properties), as if they had been permanently deleted. The recycled ids are computed once per query from the deleted-at index and the :block/parent index; when nothing is recycled the db is used as is, so graphs without a Recycle bin pay nothing. The query inputs (:current-page, :today, ...) are still resolved on the unfiltered db.
Invalidation needs no change: the watch keys of a datalog query already include :logseq.property/deleted-at and :block/parent (query-watch-keys in render_resource/query.cljs), and queries with opaque rules watch the whole graph.
This covers the advanced (datalog) queries, which is what the issue reports (:thread-api/query-custom and the rendered query blocks). The simple DSL queries (query-dsl/execute-query) are unchanged. Plugin calls to :thread-api/query-custom also no longer see recycled entities.

Tests

New frontend.worker.handler.query-test:

  • custom-query-ignores-recycled-page-test is the issue's reproduction: a recycled child page no longer makes its parent a non-leaf, and a restored child counts again.
  • custom-query-ignores-descendants-of-recycled-block-test: the reference sits on a nested block under a recycled block, which carries no deleted-at itself.
    Both fail without the change (#{"child"} / #{} instead of #{"parent"}), pass with it.

Validation

  • Build with the three source fixes reverted and the tests kept: query-test 2 failures, query-dsl-test 2 failures, page-first-paint-test 1 failure (the assertions above). With the fixes: the three namespaces pass (2 tests/8 assertions, 26/112, 6/28).
  • Whole unit suite (pnpm cljs:run-test) passes with the fixes. deps/db suite (pnpm test, nbb) passes: 200 tests, 763 assertions.
  • clj-kondo on the changed files: 0 errors, 0 warnings. typos clean.

In a DB graph, a text property can hold both plain text and nodes (typing [[x]] in the value). A query such as (property note [[x]]) found only the plain-text values and missed the nodes.

A node in a text property is a value block titled [[<uuid of x>]], while the query compares the value with the plain name "x", which only equals the title of a plain-text value block. The ref->val rule now also accepts a value block whose whole title is a reference to a node, with that node's title as the value, as a :node property already does.

The new test in query_dsl_test builds a text property with a plain value, a node value and another plain value, and checks that both query forms (page ref and text) find the plain value and the node, and that an unrelated value still matches only its own text.

Fixes logseq/db-test#1085
…block

A query that uses :current-page returned nothing, or the result for an unrelated page of the same name, after zooming into the block that holds it. Unzooming restored the right result.

The page component takes the title of the rendered entity as the current page. When zoomed into a block, that entity is the block, so its own title was used as the page name. page-render-config now uses the title of the block's page when the entity has one.

The new test in page_first_paint_test checks the current page title for a page and for a block of that page.

Fixes logseq/db-test#1383
After deleting a page or block, an advanced query still counted it inside joins, not clauses and aggregates until the item was permanently deleted from the Recycle bin. For example a "leaf" query (not [?child :streamOf ?parent]) did not return a parent whose only child was recycled. Only the final result was filtered, and so the recycled child just vanished from it.

Recycling keeps every attribute on the entity, adds :logseq.property/deleted-at to the root only, and the descendants of the root carry nothing. execute-custom-query ran the query on the full db. It now runs on a filtered db that has no datom of a recycled root or its descendants and none that points to one, as if they were permanently deleted. When nothing is recycled the db is used as is. The filtering needs no new invalidation: the watch keys of a query already include :logseq.property/deleted-at and :block/parent.

The new tests (frontend.worker.handler.query-test) recycle a page, and a block with a nested child that holds the reference, check that the parent becomes a leaf, and check that it stops being one once the page is restored.

Fixes logseq/db-test#1208

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Devin Review found 1 potential issue.

Devin Review

Comment on lines +273 to +276
(let [recycled (recycled-eids db)]
(if (empty? recycled)
db
(let [ref-attrs (ref-attributes db)]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔍 Repeated preparation for advanced queries

With recycled entities present, every execute-custom-query call recomputes the recycled subtree and reference attributes. Multiple query blocks repeat that work for the same snapshot; measure its cost on a representative graph.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

With recycled entities in the graph, every advanced query block of a page recomputed the recycled subtree and the reference attributes, although they all run on the same db snapshot.

The filtered db is now cached per db value in a WeakMap, as logseq.db.common.view does for its exclusion sets. A transaction gives a new db value, so the next query filters again, and the entry goes away with its snapshot.

The new test checks that two lookups on one snapshot return the same filtered db, that running a query does not replace it, and that a snapshot after a transaction is filtered again while the older one keeps its own.

Refs logseq/db-test#1208
@yuxi-liu-wired

Copy link
Copy Markdown
Contributor Author

Thanks, that was a fair point. The filtered db is now cached per db value (new commit 1599bff5e6, perf(query): filter recycled entities once per db snapshot).

Change. without-recycled keeps its result in a WeakMap keyed on the db value, the same way logseq.db.common.view caches its exclusion sets. All query blocks on one snapshot share one filtered db. A transaction gives a new db value, so the next query filters again. The entry is dropped with its snapshot, so there is nothing to invalidate by hand. (A :max-tx key would work too, but the db value cannot be stale.)

Test. recycled-filter-is-computed-once-per-db-snapshot-test in frontend.worker.handler.query-test:

  • two lookups on one snapshot return the same filtered db (identical?), and running a query does not replace it;
  • after a new transaction (another page recycled), the new snapshot is filtered again and hides that page, while the old snapshot keeps its own filtered db, which still shows it.

It fails without the cache (identical? is false) and passes with it. Related namespaces pass (query-test, render-resource-test, db-core-test, query-dsl-test; the last one's timing test block-property-query-performance failed once under load and passes alone).

Measurement. A generated test db: 1000 live blocks on a page, plus 1000 recycled blocks (250 recycled roots with 3 children each, recycled in one transaction), about 17k datoms. Node, test bundle, the box shared with other jobs and run at the lowest priority, so read the ratios, not the absolute values. 15 runs each, median in ms. The benchmark namespace is not committed; it is kept at ~/tmp/query_bench_test.cljs.

Cost of the preparation on this graph, per call: filter-recycled 41.7 ms, of which the recycled-subtree walk (recycled-eids) is 37.2 ms and the reference-attribute scan 2.1 ms. This is what every query block paid before.

One advanced query:

query before (filter recomputed every call) cache miss (first call on a snapshot) cache hit (later calls) nothing recycled (same graph, for reference)
pull of the 1000 live blocks of a page, with a join and a predicate 60.7 58.0 20.1 12.9
whole-db scan with a not clause (count of blocks that have no child) 58.1 69.1 34.4 not measured

A page with 10 such query blocks on one snapshot (total ms): 538 before, 222 after for the first query; 703 before, 371 after for the second.

So the cache removes the roughly 40 ms preparation from every query block after the first. What remains over the no-recycle case (20.1 vs 12.9 ms) is the cost of running on a filtered db (each datom goes through the predicate), which a cache cannot remove. With nothing recycled the cost is unchanged (12.9, 12.6, 12.0 ms for before, miss, hit), as the db is used as is.

The remaining first-call cost is mostly the subtree walk (one :block/parent index lookup per recycled entity). If that matters on graphs with a large Recycle bin it could be made incremental, but it now runs once per snapshot, so I left it.

@yuxi-liu-wired

Copy link
Copy Markdown
Contributor Author

Combined into #13871 with the other PRs of this theme, to keep the number of open PRs down; the commits are unchanged there.

@yuxi-liu-wired
yuxi-liu-wired deleted the fix/dbtest-query branch November 17, 2026 02:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DB graph: recycled (soft-deleted) pages/blocks still affect query evaluation until permanently deleted

1 participant