Repository navigation
perf: defer wide scan columns until after filtering - #26969
Conversation
Signed-off-by: aptend <[email protected]>
Qodo reviews are paused for this user.Troubleshooting steps vary by plan Learn more → On a Teams plan? Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center? |
XuPeng-SH
left a comment
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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.
Merge Queue Status
This pull request spent 53 minutes 23 seconds in the queue, including 52 minutes 51 seconds running CI. Required conditions to merge
|
## 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]>
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
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
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
What type of PR is this?
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:
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:
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=1go test -race -p 2 ./pkg/vm/process ./pkg/sql/colexec/table_scan ./pkg/vm/engine/readutil ./pkg/vm/engine/tae/blockio -count=1go 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/disttaemake buildEXPLAIN ANALYZEchecks for zero, selective, and all-survivor cases