Skip to content

fix(db): restore :max-tx of pipeline transactions - #13468

Merged
tiensonqin merged 4 commits into
logseq:masterfrom
yuxi-liu-wired:fix/restore-max-tx
Sep 30, 2026
Merged

tiensonqin merged 4 commits into
logseq:masterfrom
yuxi-liu-wired:fix/restore-max-tx

Conversation

@yuxi-liu-wired

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

Copy link
Copy Markdown
Contributor

What changes

Since #13415, opening a graph in the db worker recomputes the whole-graph sync checksum when the tx the stored checksum covers differs from the graph's :max-tx (reconcile-local-checksum!, src/main/frontend/worker/sync.cljs). After a session whose last transaction went through the worker pipeline, the reopened graph's :max-tx is below the one the graph had when it closed, so nearly every open after an edit recomputes. On a graph of 157,614 checksum-eligible entities (1.33 million :eavt datoms, imported from a file graph), that recompute is most of the worker start: 5.56 s against 0.04 s in the release build.

Why the :max-tx falls short:

  1. ldb/transact! applies a transaction with d/with, and the worker's transact-pipeline extends it with 1 or 2 more d/with calls (derived tx data, then rebuilt refs and :block/tx-id stamps). Each call takes the next tx id. transact-sync stores the joined report with dc/store-after-transact!, which appends it to the stored tail as 1 entry.
  2. The db listener stores the checksum with (:max-tx (:db-after tx-report)), the last of those ids.
  3. d/restore-conn replays each tail entry, then sets :max-tx to the tx of the entry's first datom (db-with-tail in the datascript fork's storage.cljs), the first of those ids.

A unit test on master, with 1 block edit through the pipeline and a reopen through get-storage-conn, shows what the restore does with the other ids:

  • The edit's tail entry holds the title datoms at tx 536870915 and the :block/tx-id stamp at 536870916.
  • The replay keeps each datom's own tx: the reopened graph's datoms, tx included, equal those of the graph before the reopen.
  • The reopened :max-tx is 536870915.
  • The reopened graph therefore hands out 536870916 a second time: the next edit's title datoms get tx 536870916, which the first edit's stamp already carries.

get-storage-conn (deps/db/src/logseq/db/common/sqlite.cljs) now sets :max-tx to the largest tx among the replayed tail's datoms when that is larger. Every restore in Logseq goes through it: the worker's graph open, the sync download, the db-sync server's storage and the CLI.

(if-let [conn (d/restore-conn storage)]
  (let [max-tx (tail-max-tx conn)]
    (when (> max-tx (:max-tx @conn))
      (swap! conn assoc :max-tx max-tx))
    conn)
  (d/create-conn schema {:storage storage}))

The reopened graph keeps its :max-tx, its next transaction takes a new tx id, and the checksum stored after the last edit still covers it.

Worker start on the imported graph, whose import session ended with a pipeline transaction (a new journal page: 7 tail datoms at tx 536873413, 3 at 536873414), from the worker's first log line to its ready line, 3 runs per build:

  • master: release build 5.56 s (5.30 - 5.80), dev build 6.43 s (6.36 - 7.50). In all 6 runs the reopened :max-tx is 536873413, and the start recomputes the checksum and stores it as covering 536873413.
  • with this change: release build 0.04 s (0.03 - 0.04), dev build 0.05 s (0.03 - 0.06). In all 6 runs the reopened :max-tx is 536873414 and the stored checksum, covering 536873414, is kept. These builds also carried the recompute changes of perf/checksum-one-walk and 1 change to the sqlite read path; this start runs no recompute, so they do not act on it. Master's own start with the recompute skipped (the stored covered tx set by hand to the :max-tx master restores) took 0.04 s (0.03 - 0.04) release and 0.05 s (0.04 - 0.06) dev, 3 runs each.

One case stays (read in code, not observed): when the last d/with of a pipeline transaction writes no datom, no tail datom carries the final tx id, and the reopen after it still recomputes once.

Where the root fix could go

The first-datom rule is right when 1 tail entry is 1 transaction, as with datascript's own transact!; upstream datascript's storage.clj has the same rule. Logseq stores several d/with calls as 1 entry. The restore is the one place that sees every datom it replays, so the guarantee that :max-tx is at least every tx on the restored datoms fits best in the fork's db-with-tail. A drafted change for logseq/datascript:

(defn- max-tx-of [max-tx datoms]
  (reduce (fn [max-tx datom] (max max-tx (:tx datom))) max-tx datoms))

;; db-with-tail, per entry, was (assoc % :max-tx (:tx (first datoms)))
(assoc % :max-tx (max-tx-of (:max-tx db) datoms))

With that fork change on the classpath and master's get-storage-conn, the new test below passes (frontend.worker.db-sync-test 234 tests, 2229 assertions, 0 failures). The change adds a case to the fork's datascript.test.storage, 1 tail entry spanning 2 tx ids; run inside Logseq's test build, that namespace has 3 tests and 59 assertions, with 0 failures with the change and 2 without it. That route needs a fork commit and the sha bump in the 5 deps.edn files that pin the fork, after which the check in get-storage-conn never fires and can go. This PR keeps the Logseq-side change, since it covers every restore Logseq does and needs no dependency bump. I can send the fork change as a pull request to logseq/datascript if you prefer that route.

Tests

  • New reopened-graph-keeps-max-tx-of-pipeline-transaction-test in frontend.worker.db-sync-test: builds a storage-backed graph, edits 1 block through ldb/transact! with the worker pipeline registered, and reopens it with get-storage-conn. It checks that the edit's tail entry spans 2 tx ids, that the replayed datoms keep their tx ids, that the reopened :max-tx equals the one before, that no datom carries a tx above it, and that reconcile-local-checksum! keeps the stored checksum. On master it fails 3 times: :max-tx 536870915 against 536870916, the stamp datom at 536870916 above it, and the stored checksum replaced by a recompute. With this change it passes.
  • frontend.worker.db-sync-test 234 tests, 2229 assertions; frontend.worker.sync.client-op-test 6 tests, 22 assertions; logseq.db-sync.checksum-test (run from deps/db-sync) 28 tests, 78 assertions: 0 failures.
  • The whole unit suite at TZ=Etc/GMT+12 (2503 tests, 12504 assertions) and its time-zone-dependent namespaces at TZ=Pacific/Kiritimati (1470 tests, 9063 assertions): 0 failures, 0 errors.
  • clj-kondo on the 2 changed files: 0 errors, 0 warnings.

Allow edits from maintainers.

Merge integration

Merged master at 768f52591697ca1fdc6b667c054448dd466c570e, preserving both sets of checksum tests. The reopen regression now marks its graph remote before seeding storage, so master's new remote-graph guard still exercises checksum reconciliation.

Validation after the merge:

  • Focused reopen regression: 1 test, 7 assertions, 0 failures.
  • clj-kondo on both changed files: 0 errors, 0 warnings.
  • frontend.worker.db-sync-test: 239 tests, 2,245 assertions, 1 failure in apply-history-action-redo-replays-upsert-property-test. The same assertion also fails when run alone on the exact master parent above (1 test, 13 assertions, 1 failure), independently of this PR.

Link to Devin session: https://app.devin.ai/sessions/a288f458ecce4451a2fd7a526509ee89
Open in Devin Desktop: https://app.devin.ai/desktop/session/a288f458ecce4451a2fd7a526509ee89?variant=devin
Requested by: @tiensonqin


Devin Review

The worker pipeline extends a transaction with 1 or 2 more d/with
calls, each its own tx id, and the stored tail keeps their datoms as 1
entry. A restore replays the entry, whose datoms keep their own tx ids,
and sets :max-tx to its first datom's tx, below the :max-tx the graph
had. The next transaction then took a tx id that datoms of the graph
already carried, and since the sync checksum is stored with the
:max-tx it covers, every reopen after an edit that went through the
pipeline recomputed the whole-graph checksum. Take the largest tx
among the tail's datoms.

@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: No Issues Found

Devin Review analyzed this PR and found no bugs or issues to report.

Devin Review

@devin-ai-integration

Copy link
Copy Markdown
Contributor

The current unit-test job has failures already present in the exact master-parent CI run at f632f9eb7ab7e442ee2c55ae44061f7f5bbb0eab:

  • History redo for an upserted property returns :applied? false.
  • Electron close-db records an extra update-thread-atom call.
  • Both undo-property restoration tests error on a missing :db/index true marker.

The focused storage-reopen regression and clj-kondo passed after the conflict-resolution merge. No changes were made to repair these preexisting failures.

Written by Devin

@tiensonqin
tiensonqin merged commit eb9eb1a into logseq:master Sep 30, 2026
32 of 33 checks passed
devin-ai-integration Bot pushed a commit that referenced this pull request Sep 30, 2026
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Sep 30, 2026
logseq#13545, logseq#13546 and logseq#13547 replace the 9 open pull requests they bundle;
logseq#13468, logseq#13437, logseq#13475 and logseq#13528 are in upstream master.
@yuxi-liu-wired
yuxi-liu-wired deleted the fix/restore-max-tx branch September 30, 2026 20:02
devin-ai-integration Bot pushed a commit that referenced this pull request Oct 1, 2026
The worker pipeline extends a transaction with 1 or 2 more d/with
calls, each its own tx id, and the stored tail keeps their datoms as 1
entry. A restore replays the entry, whose datoms keep their own tx ids,
and sets :max-tx to its first datom's tx, below the :max-tx the graph
had. The next transaction then took a tx id that datoms of the graph
already carried, and since the sync checksum is stored with the
:max-tx it covers, every reopen after an edit that went through the
pipeline recomputed the whole-graph checksum. Take the largest tx
among the tail's datoms.

Co-authored-by: Tienson Qin <[email protected]>
devin-ai-integration Bot pushed a commit that referenced this pull request Oct 1, 2026
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Oct 1, 2026
logseq#13545, logseq#13546 and logseq#13547 replace the 9 open pull requests they bundle;
logseq#13468, logseq#13437, logseq#13475 and logseq#13528 are in upstream master.
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Oct 4, 2026
logseq#13545, logseq#13546 and logseq#13547 replace the 9 open pull requests they bundle;
logseq#13468, logseq#13437, logseq#13475 and logseq#13528 are in upstream master.
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Oct 8, 2026
logseq#13545, logseq#13546 and logseq#13547 replace the 9 open pull requests they bundle;
logseq#13468, logseq#13437, logseq#13475 and logseq#13528 are in upstream master.
devin-ai-integration Bot pushed a commit that referenced this pull request Oct 8, 2026
The worker pipeline extends a transaction with 1 or 2 more d/with
calls, each its own tx id, and the stored tail keeps their datoms as 1
entry. A restore replays the entry, whose datoms keep their own tx ids,
and sets :max-tx to its first datom's tx, below the :max-tx the graph
had. The next transaction then took a tx id that datoms of the graph
already carried, and since the sync checksum is stored with the
:max-tx it covers, every reopen after an edit that went through the
pipeline recomputed the whole-graph checksum. Take the largest tx
among the tail's datoms.

Co-authored-by: Tienson Qin <[email protected]>
devin-ai-integration Bot pushed a commit that referenced this pull request Oct 8, 2026
devin-ai-integration Bot pushed a commit that referenced this pull request Oct 8, 2026
The worker pipeline extends a transaction with 1 or 2 more d/with
calls, each its own tx id, and the stored tail keeps their datoms as 1
entry. A restore replays the entry, whose datoms keep their own tx ids,
and sets :max-tx to its first datom's tx, below the :max-tx the graph
had. The next transaction then took a tx id that datoms of the graph
already carried, and since the sync checksum is stored with the
:max-tx it covers, every reopen after an edit that went through the
pipeline recomputed the whole-graph checksum. Take the largest tx
among the tail's datoms.

Co-authored-by: Tienson Qin <[email protected]>
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Oct 11, 2026
logseq#13545, logseq#13546 and logseq#13547 replace the 9 open pull requests they bundle;
logseq#13468, logseq#13437, logseq#13475 and logseq#13528 are in upstream master.
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Oct 13, 2026
logseq#13545, logseq#13546 and logseq#13547 replace the 9 open pull requests they bundle;
logseq#13468, logseq#13437, logseq#13475 and logseq#13528 are in upstream master.
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Oct 16, 2026
logseq#13545, logseq#13546 and logseq#13547 replace the 9 open pull requests they bundle;
logseq#13468, logseq#13437, logseq#13475 and logseq#13528 are in upstream master.
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.

2 participants