Skip to content

[4.2] perf: defer wide scan columns until after filtering - #27099

Merged
XuPeng-SH merged 2 commits into
matrixorigin:4.2-devfrom
aptend:backport/4.2-26969-late-materialization
Aug 13, 2026
Merged

XuPeng-SH merged 2 commits into
matrixorigin:4.2-devfrom
aptend:backport/4.2-26969-late-materialization

Conversation

@aptend

@aptend aptend commented Aug 13, 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:

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

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

(cherry picked from commit c04df06)
@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 →

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