Skip to content

refactor: unify SQL resource accounting semantics - #25843

Merged
XuPeng-SH merged 65 commits into
matrixorigin:mainfrom
aptend:refactor/sql-resource-accounting
Jul 23, 2026
Merged

XuPeng-SH merged 65 commits into
matrixorigin:mainfrom
aptend:refactor/sql-resource-accounting

Conversation

@aptend

@aptend aptend commented Jul 17, 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:

Fixes #22263
Fixes #18449

What this PR does / why we need it:

This PR refactors SQL resource accounting into one authoritative statement-level aggregation path.

  • gives retry attempts independent resource generations and merges every attempt without producing negative CPU;
  • unifies local, remote, nested-remote, and local-fallback accounting without double counting;
  • records meaningful active time, classified wait time, S3/network/spill usage, exact statement memory-domain totals, quality flags, and terminal outcomes;
  • projects the resulting summary consistently to statement_info.stats and EXPLAIN ANALYZE while keeping the persisted schema unchanged;
  • adds focused unit, race, BVT, and local acceptance coverage, and documents the semantics and known limits in the RFC.

Observability and AI-analysis benefits

The result is a stable, machine-readable statement-level resource summary that downstream diagnostics and AI analysis can use without reconstructing semantics from unrelated counters.

  • active time and classified wait time make CPU, lock, filesystem, remote, and network bottlenecks distinguishable;
  • attempt count and retry wall time expose retry amplification instead of folding retries into ambiguous or negative values;
  • allocated/freed bytes, maximum memory-domain peak, bounded sum of domain peaks, spill bytes, and S3/network facts provide useful evidence for OOM and resource-pressure investigations;
  • outcome, quality flags, and missing fragment/domain counts let analysis reject or qualify incomplete samples instead of treating them as exact;
  • the same semantics in statement_info.stats and EXPLAIN ANALYZE make workload comparison and query-level diagnosis consistent.

This PR intentionally provides statement and plan-level facts, not exact per-operator or per-CN memory attribution. A process killed before terminal publication may still have no final statement record; these limits are documented in the RFC.

Validation completed locally:

  • pkg/sql/compile and pkg/vm/process tests;
  • pkg/sql/compile with the race detector;
  • go vet for the affected packages;
  • make build.

@mergify mergify Bot added kind/bug Something isn't working kind/enhancement kind/test-ci kind/refactor Code refactor labels Jul 17, 2026
@matrix-meow matrix-meow added the size/L Denotes a PR that changes [500,999] lines label Jul 17, 2026
@aptend
aptend force-pushed the refactor/sql-resource-accounting branch from ca8452f to 456431f Compare July 17, 2026 16:35
@matrix-meow matrix-meow added size/XL Denotes a PR that changes [1000, 1999] lines and removed size/L Denotes a PR that changes [500,999] lines labels Jul 17, 2026
@aptend
aptend force-pushed the refactor/sql-resource-accounting branch from 456431f to 5ec7398 Compare July 21, 2026 11:13
@aptend

aptend commented Jul 23, 2026

Copy link
Copy Markdown
Contributor Author

Addressed both findings in f8e1a76 after rebasing onto current main (0d7eeb3).

  • Response finalization now uses the enriched ExecCtx request context, including panic/error paths, so parse/plan/compile root phases and their StatsInfo are retained through deferred statement completion.
  • Output wait now spans the complete statement response at the physical net.Conn.Write boundary, covering buffered flushes, metadata, terminal OK/EOF, and errors.
  • Immediate writes executed inside Output.Call continue to be published through the operator-owned counter and subtracted from active time. A lightweight per-response tracker records total physical-write wait, and finalization merges only the remainder not already owned by the operator, preventing double counting.
  • Trackers rotate between multi-statements and detach at request completion; same-session derived SQL remains owned by the outer response lifecycle.

Validation on the rebased head:

  • focused frontend accounting tests repeated 10x;
  • full pkg/frontend, pkg/sql/colexec/output, and pkg/vm/process tests;
  • race tests for those packages;
  • go vet with the required CGo environment;
  • make build, range-diff inspection, and git diff --check.

@mergify

mergify Bot commented Jul 23, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-07-23 10:56 UTC · Rule: main · triggered by rule Automatic queue on approval for main
  • 🟠 Checks running · in-place
  • 🚫 Left the queue — 2026-07-23 11:25 UTC · at 831994ab6c4b3d1c21565a0b8a92da3ef587744c

This pull request spent 29 minutes 4 seconds in the queue, with no time running CI.

Waiting for
  • any of: [🛡 GitHub branch protection]
    • check-neutral = Matrixone Compose CI / multi cn e2e bvt test docker compose(PESSIMISTIC)
    • check-skipped = Matrixone Compose CI / multi cn e2e bvt test docker compose(PESSIMISTIC)
    • check-success = Matrixone Compose CI / multi cn e2e bvt test docker compose(PESSIMISTIC)
  • any of: [🛡 GitHub branch protection]
    • check-neutral = Matrixone Standlone CI / Multi-CN e2e BVT Test on Linux/x64(LAUNCH, PROXY)
    • check-skipped = Matrixone Standlone CI / Multi-CN e2e BVT Test on Linux/x64(LAUNCH, PROXY)
    • check-success = Matrixone Standlone CI / Multi-CN e2e BVT Test on Linux/x64(LAUNCH, PROXY)
  • any of: [🛡 GitHub branch protection]
    • 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)
    • check-success = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
  • any of: [🛡 GitHub branch protection]
    • check-neutral = Matrixone CI / UT Test on Ubuntu/x86
    • check-skipped = Matrixone CI / UT Test on Ubuntu/x86
    • check-success = Matrixone CI / UT Test on Ubuntu/x86
  • any of: [🛡 GitHub branch protection]
    • check-neutral = Matrixone Utils CI / Coverage
    • check-skipped = Matrixone Utils CI / Coverage
    • check-success = Matrixone Utils CI / Coverage
  • any of: [🛡 GitHub branch protection]
    • check-neutral = Matrixone CI / SCA Test on Linux/arm64
    • check-skipped = Matrixone CI / SCA Test on Linux/arm64
    • check-success = Matrixone CI / SCA Test on Linux/arm64
All conditions
  • any of [🛡 GitHub branch protection]:
    • check-neutral = Matrixone Compose CI / multi cn e2e bvt test docker compose(PESSIMISTIC)
    • check-skipped = Matrixone Compose CI / multi cn e2e bvt test docker compose(PESSIMISTIC)
    • check-success = Matrixone Compose CI / multi cn e2e bvt test docker compose(PESSIMISTIC)
  • any of [🛡 GitHub branch protection]:
    • check-neutral = Matrixone Standlone CI / Multi-CN e2e BVT Test on Linux/x64(LAUNCH, PROXY)
    • check-skipped = Matrixone Standlone CI / Multi-CN e2e BVT Test on Linux/x64(LAUNCH, PROXY)
    • check-success = Matrixone Standlone CI / Multi-CN e2e BVT Test on Linux/x64(LAUNCH, PROXY)
  • any of [🛡 GitHub branch protection]:
    • 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)
    • check-success = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
  • any of [🛡 GitHub branch protection]:
    • check-neutral = Matrixone CI / UT Test on Ubuntu/x86
    • check-skipped = Matrixone CI / UT Test on Ubuntu/x86
    • check-success = Matrixone CI / UT Test on Ubuntu/x86
  • any of [🛡 GitHub branch protection]:
    • check-neutral = Matrixone Utils CI / Coverage
    • check-skipped = Matrixone Utils CI / Coverage
    • check-success = Matrixone Utils CI / Coverage
  • any of [🛡 GitHub branch protection]:
    • check-neutral = Matrixone CI / SCA Test on Linux/arm64
    • check-skipped = Matrixone CI / SCA Test on Linux/arm64
    • check-success = Matrixone CI / SCA Test on Linux/arm64
  • #review-threads-unresolved = 0 [🛡 GitHub branch protection]
  • github-review-approved [🛡 GitHub branch protection] (documentation)
  • github-review-decision = APPROVED [🛡 GitHub branch protection] (documentation)

Reason

Pull request #25843 has been dequeued

Pull request from fork cannot be queued. This pull request comes from a fork, and Mergify needs the author's permission to update its branch.

The author needs to enable "Allow edits from maintainers" on this pull request.

Failing checks:

Hint

You should look at the reason for the failure and decide if the pull request needs to be fixed or if you want to requeue it.
If you do update this pull request, it will automatically be requeued once the queue conditions match again.
If you think this was a flaky issue instead, you can requeue the pull request, without updating it, by posting a @mergifyio queue comment.

Tick the box to put this pull request back in the merge queue (same as @mergifyio queue).

  • Requeue this pull request

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/bug Something isn't working kind/enhancement kind/refactor Code refactor kind/test-ci size/XL Denotes a PR that changes [1000, 1999] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: negative cpu reported [Tech request]: Resource statistics when compile retry execute

5 participants