Skip to content

perf(core): optimize mat view writing by skipping transactions that are fully replaced by future commits or full refresh#6416

Merged
bluestreak01 merged 19 commits intomasterfrom
feat-optmise-mat-view-wal-apply
Nov 25, 2025
Merged

perf(core): optimize mat view writing by skipping transactions that are fully replaced by future commits or full refresh#6416
bluestreak01 merged 19 commits intomasterfrom
feat-optmise-mat-view-wal-apply

Conversation

@ideoma
Copy link
Copy Markdown
Collaborator

@ideoma ideoma commented Nov 19, 2025

Pull Request Overview

This PR introduces an optimization to skip processing WAL (Write-Ahead Log) transactions with replace ranges that are fully covered by future transactions or if a TRUNCATE operation is visible in the future transactions, improving performance when handling multiple overlapping replace range operations.

Also, this PR fixed a few minor issues:

  • Removes error logging about lock errors of wa*.lock files; these are now logged as a debug category
  • Fixes wrong writerTxn in wal_tables query, sometimes sporadically observed.

@ideoma ideoma requested a review from Copilot November 19, 2025 14:42
@ideoma
Copy link
Copy Markdown
Collaborator Author

ideoma commented Nov 19, 2025

@coderabbitai review

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Nov 19, 2025

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR introduces an optimization to skip processing WAL (Write-Ahead Log) transactions with replace ranges that are fully covered by future transactions, improving performance when handling multiple overlapping replace range operations.

Key changes:

  • Added isReplaceRangeCoveredByFutureTransactions() method to detect when a transaction's replace range is superseded by a future transaction
  • Integrated the optimization check into processWalCommit() to skip redundant transaction processing
  • Added stress test testStressReplaceLastMinuteRepeatedly() to validate the optimization with 1,000 consecutive replace operations

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 7 comments.

File Description
core/src/main/java/io/questdb/cairo/TableWriter.java Implements the skip optimization logic for replace range transactions, including commented-out experimental code and the new coverage detection method
core/src/test/java/io/questdb/test/cairo/wal/WalWriterReplaceRangeTest.java Adds stress test for repeated replace range operations to validate the optimization works correctly under high load

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@ideoma
Copy link
Copy Markdown
Collaborator Author

ideoma commented Nov 20, 2025

@coderabbitai review

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Nov 20, 2025

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@questdb questdb deleted a comment from coderabbitai bot Nov 20, 2025
@ideoma ideoma changed the title feat(core): optimize mat view writing by skipping transactions that are fully replaced by future commits perf(core): optimize mat view writing by skipping transactions that are fully replaced by future commits or full refresh Nov 20, 2025
Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 4

📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 5555187 and 7547af1.

📒 Files selected for processing (6)
  • core/src/main/java/io/questdb/cairo/TableUtils.java (2 hunks)
  • core/src/main/java/io/questdb/cairo/TableWriter.java (1 hunks)
  • core/src/main/java/io/questdb/cairo/wal/ApplyWal2TableJob.java (4 hunks)
  • core/src/main/java/io/questdb/cairo/wal/WalPurgeJob.java (1 hunks)
  • core/src/test/java/io/questdb/test/cairo/wal/WalWriterReplaceRangeTest.java (3 hunks)
  • core/src/test/java/io/questdb/test/cutlass/http/ExpParquetExportTest.java (1 hunks)
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2025-11-07T00:59:31.522Z
Learnt from: bluestreak01
Repo: questdb/questdb PR: 0
File: :0-0
Timestamp: 2025-11-07T00:59:31.522Z
Learning: In QuestDB's Cairo engine, transaction (_txn) files have a strong invariant: they are never truncated below TX_BASE_HEADER_SIZE. Once created, they are either fully formed (size >= header size) or completely removed along with the entire table directory when the table is dropped.

Applied to files:

  • core/src/main/java/io/questdb/cairo/wal/ApplyWal2TableJob.java
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (34)
  • GitHub Check: New pull request (SelfHosted Running tests with cover on linux-other)
  • GitHub Check: New pull request (SelfHosted Running tests with cover on linux-pgwire)
  • GitHub Check: New pull request (SelfHosted Running tests with cover on linux-cairo-sub)
  • GitHub Check: New pull request (SelfHosted Running tests with cover on linux-cairo-root)
  • GitHub Check: New pull request (SelfHosted Running tests with cover on linux-fuzz2)
  • GitHub Check: New pull request (SelfHosted Running tests with cover on linux-fuzz1)
  • GitHub Check: New pull request (SelfHosted Running tests with cover on linux-griffin-sub)
  • GitHub Check: New pull request (Rust Test and Lint on linux-jdk17)
  • GitHub Check: New pull request (SelfHosted Other tests on linux-x86-graal)
  • GitHub Check: New pull request (SelfHosted Other tests on linux-x64-zfs)
  • GitHub Check: New pull request (SelfHosted Running tests with cover on linux-griffin-root)
  • GitHub Check: New pull request (SelfHosted Other tests on linux-arm64)
  • GitHub Check: New pull request (Hosted Running tests on windows-other-2)
  • GitHub Check: New pull request (Hosted Running tests on windows-other-1)
  • GitHub Check: New pull request (Hosted Running tests on windows-pgwire)
  • GitHub Check: New pull request (Hosted Running tests on windows-cairo-2)
  • GitHub Check: New pull request (Hosted Running tests on windows-cairo-1)
  • GitHub Check: New pull request (Hosted Running tests on windows-fuzz2)
  • GitHub Check: New pull request (Hosted Running tests on windows-fuzz1)
  • GitHub Check: New pull request (Hosted Running tests on windows-griffin-sub)
  • GitHub Check: New pull request (Hosted Running tests on windows-griffin-base)
  • GitHub Check: New pull request (Hosted Running tests on mac-other)
  • GitHub Check: New pull request (Hosted Running tests on mac-pgwire)
  • GitHub Check: New pull request (Hosted Running tests on mac-cairo-fuzz)
  • GitHub Check: New pull request (Hosted Running tests on mac-cairo)
  • GitHub Check: New pull request (SelfHosted Cairo tests on linux-x64-zfs)
  • GitHub Check: New pull request (Hosted Running tests on mac-griffin)
  • GitHub Check: New pull request (SelfHosted Griffin tests on linux-x86-graal)
  • GitHub Check: New pull request (SelfHosted Cairo tests on linux-arm64)
  • GitHub Check: New pull request (SelfHosted Cairo tests on linux-x86-graal)
  • GitHub Check: New pull request (SelfHosted Griffin tests on linux-arm64)
  • GitHub Check: New pull request (SelfHosted Griffin tests on linux-x64-zfs)
  • GitHub Check: New pull request (Check Changes Check changes)
  • GitHub Check: build
🔇 Additional comments (7)
core/src/test/java/io/questdb/test/cutlass/http/ExpParquetExportTest.java (1)

124-124: Verify the connection between this test change and the PR objectives.

The date change from '1971-01-01' to '1970-01-02' reduces the generated dataset from approximately 1 year (~6.3M rows) to 1 day (~17K rows) of 5-second intervals. While this makes the test more efficient and appropriate for a basic sanity check, it's unclear how this relates to the PR's stated objectives around WAL transaction skipping optimization for materialized views.

Is this test adjustment:

  1. An incidental correction discovered during development?
  2. Related to underlying changes in date/time processing from the WAL optimization work?
  3. Addressing a previous test error?

Please confirm this change is intentional and clarify its relationship to the PR scope.

core/src/main/java/io/questdb/cairo/wal/WalPurgeJob.java (1)

285-285: LGTM! Appropriate logging level for routine WAL discovery.

The change to debug-level logging (verbose=false, logDebug=true) is appropriate for this context where lock failures during WAL segment discovery are routine operations, not exceptional conditions. This aligns well with the existing debug-level logging for successful lock acquisition on line 287.

core/src/main/java/io/questdb/cairo/TableUtils.java (3)

53-53: LGTM! Import required for new logging pattern.

The LogRecord import is necessary to support the conditional log level selection in the updated lock() method implementations.


1091-1128: LGTM! Well-designed logging control for lock operations.

The refactored lock() method provides fine-grained control over logging behavior:

  • Error paths (lines 1099-1119): Log at ERROR level when verbose=true and logDebug=false, otherwise at DEBUG level when logDebug=true
  • Success path (lines 1124-1126): Always logs at DEBUG level when logging is enabled (appropriate for routine operations)

The overload chain maintains backward compatibility while enabling callers to choose appropriate logging levels based on operational context (e.g., expected vs. exceptional lock failures).


1130-1132: LGTM! Backward compatibility maintained.

The existing convenience overload continues to delegate appropriately, ensuring no breaking changes for existing callers.

core/src/main/java/io/questdb/cairo/wal/ApplyWal2TableJob.java (2)

621-636: Verification complete: atomic behavior of trySkipWalTransactions is correctly implemented.

The method exhibits proper all-or-nothing semantics:

  1. Precondition check before state modification: The lag row/txn count check at line 3075 occurs before any state modifications via commitSeqTxn, ensuring the precondition (no pending WAL writes) is validated first.

  2. Atomic state transitions:

    • If the precondition fails (lag counts non-zero), the method returns false immediately without modifying writer state.
    • If the precondition passes, it calls commitSeqTxn(seqTxn + skipTxnCount - 1) and returns true.
  3. Safe fallback: When trySkipWalTransactions returns false at lines 624-627 in ApplyWal2TableJob.java, the writer state remains unchanged, allowing the fallback to commitWalInsertTransactions at lines 631-635 to proceed safely.

The implementation fully addresses the verification requirements outlined in the original review comment.


638-681: Forward progress is guaranteed by both code paths; no issues found.

The verification confirms that the return value calculation at line 681 is safe:

  1. Skip path (line 3080): Explicitly calls commitSeqTxn(seqTxn + skipTxnCount - 1), ensuring getAppliedSeqTxn() >= seqTxn.

  2. Commit path (lines 1277–1363): The unconditional assertion at line 1361—assert txWriter.getLagTxnCount() == (seqTxn - txWriter.getSeqTxn())—guarantees that getAppliedSeqTxn() (which equals txWriter.getSeqTxn() + txWriter.getLagTxnCount()) always equals the method's seqTxn parameter at completion, regardless of whether data was committed to the main table or buffered in LAG.

Consequently, the loop at line 643 (line 643: for (long s = seqTxn; s <= lastCommittedSeqTxn; s++)) always executes at least once, ensuring the return value (int) (lastCommittedSeqTxn - seqTxn + 1) is at least 1, satisfying the assertion at line 484.

@questdb questdb deleted a comment from coderabbitai bot Nov 21, 2025
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Nov 21, 2025

Important

Review skipped

Auto reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Note

Other AI code review bot(s) detected

CodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review.

✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feat-optmise-mat-view-wal-apply

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@nwoolmer nwoolmer left a comment

Choose a reason for hiding this comment

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

Looks good, a nit and maybe an additional safety check

nwoolmer
nwoolmer previously approved these changes Nov 21, 2025
Copy link
Copy Markdown
Contributor

@nwoolmer nwoolmer left a comment

Choose a reason for hiding this comment

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

LGTM

@bluestreak01 bluestreak01 added the DO NOT MERGE These changes should not be merged to main branch label Nov 21, 2025
@bluestreak01
Copy link
Copy Markdown
Member

I would like to merge this after Ent release, to de-risk Enterprise

@bluestreak01 bluestreak01 removed the DO NOT MERGE These changes should not be merged to main branch label Nov 25, 2025
@glasstiger
Copy link
Copy Markdown
Contributor

[PR Coverage check]

😍 pass : 51 / 51 (100.00%)

file detail

path covered line new line coverage
🔵 io/questdb/cairo/TableWriter.java 9 9 100.00%
🔵 io/questdb/cairo/wal/ApplyWal2TableJob.java 42 42 100.00%

@bluestreak01 bluestreak01 merged commit 818d227 into master Nov 25, 2025
41 checks passed
@bluestreak01 bluestreak01 deleted the feat-optmise-mat-view-wal-apply branch November 25, 2025 19:52
ideoma added a commit that referenced this pull request Nov 26, 2025
…s that are fully replaced by future commits or full refresh (#6416)"

This reverts commit 818d227.
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.

5 participants