Skip to content

perf: defer wide scan columns until after filtering - #26969

Merged
mergify[bot] merged 6 commits into
matrixorigin:mainfrom
aptend:perf/scan-late-materialization
Aug 12, 2026
Merged

mergify[bot] merged 6 commits into
matrixorigin:mainfrom
aptend:perf/scan-late-materialization

Conversation

@aptend

@aptend aptend commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • API-change
  • BUG
  • Improvement
  • Documentation
  • Feature
  • Test and CI
  • Code Refactoring

Which issue(s) this PR fixes:

issue #26960

What this PR does / why we need it:

Table scans currently materialize every projected column before evaluating residual filters. On the workload from #26960, the scan read 11.45 million rows / 7.32 GB and spent 62.78 seconds even though the query returned no rows. The captured CPU profile was dominated by copying and constructing varlen output (memmove, Vector.Union, and varlena construction), rather than predicate evaluation.

This PR adds block-local late materialization for output-only wide varlen columns:

  • predicate columns and fixed/small output columns are loaded first;
  • visibility and tombstones are applied before the residual filter;
  • surviving logical row positions are mapped back to physical block offsets;
  • deferred columns are loaded only for those surviving rows.

The capability is optional on engine.Reader, so unsupported readers remain correct without changing the existing reader interface. In-memory data, storage TopN, sharding readers, transaction data tracing, reader summaries, and unsupported expression forms keep the eager path. The change also preserves exact-PK feedback and separates synchronous filter CPU time from filesystem wait accounting.

Local A/B on the same persisted 200,000-row table with a 328 MiB text payload:

Query Metric Before After
zero survivors Table Scan InputSize 335.10 MiB 2.29 MiB
zero survivors ReadSize 4.32 MiB 796.72 KiB
zero survivors MemorySize 14.47 MiB 291.31 KiB
200 survivors Table Scan InputSize 334.34 MiB 1.86 MiB
200 survivors OutputSize 328.24 MiB 341.19 KiB
200 survivors MemorySize 28.44 MiB 275.27 KiB

The selective query still returns 200 rows with IDs 1000 through 200000 and 344576 payload bytes. An all-survivor warm-cache control still materializes 334.34 MiB and showed about 10 ms of additional local read-call overhead, so the optimization is deliberately limited to output-only wide varlen columns.

Validation:

  • go test -p 4 ./pkg/vm/process ./pkg/sql/compile ./pkg/sql/colexec/... ./pkg/vm/engine/... -count=1
  • go test -race -p 2 ./pkg/vm/process ./pkg/sql/colexec/table_scan ./pkg/vm/engine/readutil ./pkg/vm/engine/tae/blockio -count=1
  • go vet -p 4 ./pkg/vm/process ./pkg/sql/colexec/table_scan ./pkg/vm/engine/readutil ./pkg/vm/engine/tae/blockio ./pkg/sql/compile ./pkg/vm/engine/disttae
  • make build
  • local persisted-table EXPLAIN ANALYZE checks for zero, selective, and all-survivor cases

@qodo-code-review

Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@XuPeng-SH XuPeng-SH 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.

Deep-reviewed exact head d8f7a01 and its merge with current main. I traced the two-stage persisted read through storage selections, appendable visibility, tombstones, residual-selection remapping, late vector ownership, merge-reader fallback, in-memory/TopN paths, tracing/reader-summary fallbacks, exact-PK feedback, cancellation/error cleanup, and analyzer accounting. I found no functional correctness blocker in those paths. The full affected table_scan, readutil, blockio, and process package tests pass locally, and the head merges cleanly with main.

Requesting changes for the current required SCA failure: pkg/sql/colexec/table_scan/table_scan.go:360 wraps a range with if loadedColumns != nil. Ranging a nil slice is already a no-op, and staticcheck S1031 rejects the redundant guard. Remove the guard and keep the loop; this should make the required SCA check green.

Performance note, non-blocking: a high-pass/all-survivor predicate still splits one eager multi-column read into two serial phases. The PR reports about 10 ms warm-cache overhead on its control, which is a reasonable measured tradeoff for this first scoped version; please watch cold object-store latency and add a cost/selectivity gate later if production broad predicates regress. No additional review finding changes the verdict.

@XuPeng-SH XuPeng-SH 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.

Deep-reviewed exact head 9da3627 against current base/main 9a7c98b. The previous SCA blocker is resolved by the semantics-preserving nil-range cleanup, all current required checks are green, and there are no unresolved review threads.

I re-audited the complete late-materialization closure: persisted storage selections; appendable visibility and abort masks; tombstones; zero, partial, and all-survivor logical-to-physical remapping; multiple residual-filter selection composition; rowid and sorted-column handling; exact-PK feedback; eager fallbacks for in-memory data, storage TopN, sharding/unsupported readers, transaction data tracing, and reader summaries; merge-reader close/error behavior; partial-read/OOM/cancellation ownership; operator Reset/Free reuse; and analyzer row, byte, memory, and filesystem-wait accounting. I found no correctness, compatibility, lifecycle, or concurrency blocker.

Local verification at the exact head passed with no test cache for pkg/sql/colexec/table_scan, pkg/vm/engine/readutil, pkg/vm/engine/tae/blockio, and pkg/vm/process, both normally and under -race; git diff --check also passes.

Non-blocking performance note: an all-survivor predicate still pays two serial materialization phases. The scope guard limits this to residual-filter scans with output-only wide varlen columns, and the PR reports only about 10 ms warm-cache overhead versus large selective wins. Continue watching cold object-store broad-predicate latency and add a cost/selectivity gate only if production evidence warrants it.

@mergify mergify Bot added the queued label Aug 12, 2026
@mergify

mergify Bot commented Aug 12, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-08-12 04:05 UTC · Rule: main · triggered by rule Automatic queue on approval for main
  • ✅ Checks passed · in-place
  • ✅ Merged — 2026-08-12 04:59 UTC · at 78a04ee3695f6a3c72498af472c61ac055e7004f · squash

This pull request spent 53 minutes 23 seconds in the queue, including 52 minutes 51 seconds running CI.

Required conditions to merge
  • #review-threads-unresolved = 0 [🛡 GitHub branch protection]
  • github-review-approved [🛡 GitHub branch protection]
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
    • check-neutral = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
    • check-skipped = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone CI / UT Test on Ubuntu/x86
    • check-neutral = Matrixone CI / UT Test on Ubuntu/x86
    • check-skipped = Matrixone CI / UT Test on Ubuntu/x86
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone CI / SCA Test on Linux/arm64
    • check-neutral = Matrixone CI / SCA Test on Linux/arm64
    • check-skipped = Matrixone CI / SCA Test on Linux/arm64
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
    • check-neutral = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
    • check-skipped = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Utils CI / Coverage
    • check-neutral = Matrixone Utils CI / Coverage
    • check-skipped = Matrixone Utils CI / Coverage
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
    • check-neutral = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
    • check-skipped = Matrixone UT Coverage / UT Coverage on Ubuntu/x86

@mergify
mergify Bot merged commit c04df06 into matrixorigin:main Aug 12, 2026
25 of 26 checks passed
@mergify mergify Bot removed the queued label Aug 12, 2026
XuPeng-SH added a commit that referenced this pull request Aug 13, 2026
## What type of PR is this?

- [ ] API-change
- [ ] BUG
- [x] Improvement
- [ ] Documentation
- [ ] Feature
- [ ] Test and CI
- [ ] Code Refactoring

## Which issue(s) this PR fixes:

issue #26960

## What this PR does / why we need it:

Backport of #26969 to 4.2-dev.

Table scans currently materialize every projected column before
evaluating residual filters. On the workload from #26960, the scan read
11.45 million rows / 7.32 GB and spent 62.78 seconds even though the
query returned no rows. The captured CPU profile was dominated by
copying and constructing varlen output rather than predicate evaluation.

This change adds block-local late materialization for output-only wide
varlen columns:

- predicate columns and fixed/small output columns are loaded first;
- visibility and tombstones are applied before the residual filter;
- surviving logical row positions are mapped back to physical block
offsets;
- deferred columns are loaded only for surviving rows.

The capability is optional on engine.Reader, so unsupported readers keep
the eager path. In-memory data, storage TopN, sharding readers,
transaction-data tracing, reader summaries, and unsupported expression
forms also remain eager.

4.2-dev predates the merge-reader ownership fix present on main. This
backport includes the required close-on-exhaustion/error behavior so the
new ReadWithFilter path has the same exactly-once reader cleanup
contract; focused tests cover both terminal paths.

Validation:

- GOWORK=off go build -mod=readonly ./pkg/vm/process
./pkg/sql/colexec/table_scan ./pkg/vm/engine/readutil
./pkg/vm/engine/tae/blockio ./pkg/vm/engine ./pkg/sql/compile
./pkg/vm/engine/disttae
- GOWORK=off go vet -mod=readonly over the same owning/dependent
packages
- focused late-materialization, row-mapping, reader-ownership, and
metric tests
- full owning/dependent package tests
- all eight focused tests with -race -count=100
- full process, table_scan, readutil, and blockio package tests with
-race

Co-authored-by: XuPeng-SH <[email protected]>
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
Table scans currently materialize every projected column before evaluating residual filters. On the workload from matrixorigin#26960, the scan read 11.45 million rows / 7.32 GB and spent 62.78 seconds even though the query returned no rows. The captured CPU profile was dominated by copying and constructing varlen output (`memmove`, `Vector.Union`, and varlena construction), rather than predicate evaluation.

This PR adds block-local late materialization for output-only wide varlen columns:

- predicate columns and fixed/small output columns are loaded first;
- visibility and tombstones are applied before the residual filter;
- surviving logical row positions are mapped back to physical block offsets;
- deferred columns are loaded only for those surviving rows.

The capability is optional on `engine.Reader`, so unsupported readers remain correct without changing the existing reader interface. In-memory data, storage TopN, sharding readers, transaction data tracing, reader summaries, and unsupported expression forms keep the eager path. The change also preserves exact-PK feedback and separates synchronous filter CPU time from filesystem wait accounting.

Local A/B on the same persisted 200,000-row table with a 328 MiB text payload:

| Query | Metric | Before | After |
| --- | ---: | ---: | ---: |
| zero survivors | Table Scan InputSize | 335.10 MiB | 2.29 MiB |
| zero survivors | ReadSize | 4.32 MiB | 796.72 KiB |
| zero survivors | MemorySize | 14.47 MiB | 291.31 KiB |
| 200 survivors | Table Scan InputSize | 334.34 MiB | 1.86 MiB |
| 200 survivors | OutputSize | 328.24 MiB | 341.19 KiB |
| 200 survivors | MemorySize | 28.44 MiB | 275.27 KiB |

The selective query still returns 200 rows with IDs 1000 through 200000 and 344576 payload bytes. An all-survivor warm-cache control still materializes 334.34 MiB and showed about 10 ms of additional local read-call overhead, so the optimization is deliberately limited to output-only wide varlen columns.

Validation:

- `go test -p 4 ./pkg/vm/process ./pkg/sql/compile ./pkg/sql/colexec/... ./pkg/vm/engine/... -count=1`
- `go test -race -p 2 ./pkg/vm/process ./pkg/sql/colexec/table_scan ./pkg/vm/engine/readutil ./pkg/vm/engine/tae/blockio -count=1`
- `go vet -p 4 ./pkg/vm/process ./pkg/sql/colexec/table_scan ./pkg/vm/engine/readutil ./pkg/vm/engine/tae/blockio ./pkg/sql/compile ./pkg/vm/engine/disttae`
- `make build`
- local persisted-table `EXPLAIN ANALYZE` checks for zero, selective, and all-survivor cases

Approved by: @XuPeng-SH
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
Table scans currently materialize every projected column before evaluating residual filters. On the workload from matrixorigin#26960, the scan read 11.45 million rows / 7.32 GB and spent 62.78 seconds even though the query returned no rows. The captured CPU profile was dominated by copying and constructing varlen output (`memmove`, `Vector.Union`, and varlena construction), rather than predicate evaluation.

This PR adds block-local late materialization for output-only wide varlen columns:

- predicate columns and fixed/small output columns are loaded first;
- visibility and tombstones are applied before the residual filter;
- surviving logical row positions are mapped back to physical block offsets;
- deferred columns are loaded only for those surviving rows.

The capability is optional on `engine.Reader`, so unsupported readers remain correct without changing the existing reader interface. In-memory data, storage TopN, sharding readers, transaction data tracing, reader summaries, and unsupported expression forms keep the eager path. The change also preserves exact-PK feedback and separates synchronous filter CPU time from filesystem wait accounting.

Local A/B on the same persisted 200,000-row table with a 328 MiB text payload:

| Query | Metric | Before | After |
| --- | ---: | ---: | ---: |
| zero survivors | Table Scan InputSize | 335.10 MiB | 2.29 MiB |
| zero survivors | ReadSize | 4.32 MiB | 796.72 KiB |
| zero survivors | MemorySize | 14.47 MiB | 291.31 KiB |
| 200 survivors | Table Scan InputSize | 334.34 MiB | 1.86 MiB |
| 200 survivors | OutputSize | 328.24 MiB | 341.19 KiB |
| 200 survivors | MemorySize | 28.44 MiB | 275.27 KiB |

The selective query still returns 200 rows with IDs 1000 through 200000 and 344576 payload bytes. An all-survivor warm-cache control still materializes 334.34 MiB and showed about 10 ms of additional local read-call overhead, so the optimization is deliberately limited to output-only wide varlen columns.

Validation:

- `go test -p 4 ./pkg/vm/process ./pkg/sql/compile ./pkg/sql/colexec/... ./pkg/vm/engine/... -count=1`
- `go test -race -p 2 ./pkg/vm/process ./pkg/sql/colexec/table_scan ./pkg/vm/engine/readutil ./pkg/vm/engine/tae/blockio -count=1`
- `go vet -p 4 ./pkg/vm/process ./pkg/sql/colexec/table_scan ./pkg/vm/engine/readutil ./pkg/vm/engine/tae/blockio ./pkg/sql/compile ./pkg/vm/engine/disttae`
- `make build`
- local persisted-table `EXPLAIN ANALYZE` checks for zero, selective, and all-survivor cases

Approved by: @XuPeng-SH
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
Table scans currently materialize every projected column before evaluating residual filters. On the workload from matrixorigin#26960, the scan read 11.45 million rows / 7.32 GB and spent 62.78 seconds even though the query returned no rows. The captured CPU profile was dominated by copying and constructing varlen output (`memmove`, `Vector.Union`, and varlena construction), rather than predicate evaluation.

This PR adds block-local late materialization for output-only wide varlen columns:

- predicate columns and fixed/small output columns are loaded first;
- visibility and tombstones are applied before the residual filter;
- surviving logical row positions are mapped back to physical block offsets;
- deferred columns are loaded only for those surviving rows.

The capability is optional on `engine.Reader`, so unsupported readers remain correct without changing the existing reader interface. In-memory data, storage TopN, sharding readers, transaction data tracing, reader summaries, and unsupported expression forms keep the eager path. The change also preserves exact-PK feedback and separates synchronous filter CPU time from filesystem wait accounting.

Local A/B on the same persisted 200,000-row table with a 328 MiB text payload:

| Query | Metric | Before | After |
| --- | ---: | ---: | ---: |
| zero survivors | Table Scan InputSize | 335.10 MiB | 2.29 MiB |
| zero survivors | ReadSize | 4.32 MiB | 796.72 KiB |
| zero survivors | MemorySize | 14.47 MiB | 291.31 KiB |
| 200 survivors | Table Scan InputSize | 334.34 MiB | 1.86 MiB |
| 200 survivors | OutputSize | 328.24 MiB | 341.19 KiB |
| 200 survivors | MemorySize | 28.44 MiB | 275.27 KiB |

The selective query still returns 200 rows with IDs 1000 through 200000 and 344576 payload bytes. An all-survivor warm-cache control still materializes 334.34 MiB and showed about 10 ms of additional local read-call overhead, so the optimization is deliberately limited to output-only wide varlen columns.

Validation:

- `go test -p 4 ./pkg/vm/process ./pkg/sql/compile ./pkg/sql/colexec/... ./pkg/vm/engine/... -count=1`
- `go test -race -p 2 ./pkg/vm/process ./pkg/sql/colexec/table_scan ./pkg/vm/engine/readutil ./pkg/vm/engine/tae/blockio -count=1`
- `go vet -p 4 ./pkg/vm/process ./pkg/sql/colexec/table_scan ./pkg/vm/engine/readutil ./pkg/vm/engine/tae/blockio ./pkg/sql/compile ./pkg/vm/engine/disttae`
- `make build`
- local persisted-table `EXPLAIN ANALYZE` checks for zero, selective, and all-survivor cases

Approved by: @XuPeng-SH

This branch was previously deployed

1 inactive deployment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/enhancement size/XL Denotes a PR that changes [1000, 1999] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants