Skip to content

Revert #33011: typed prepared parameter refactor (Sysbench regression) - #33074

Closed
LeftHandCold wants to merge 1 commit into
matrixorigin:mainfrom
LeftHandCold:revert-33011-exact-33064
Closed

LeftHandCold wants to merge 1 commit into
matrixorigin:mainfrom
LeftHandCold:revert-33011-exact-33064

Conversation

@LeftHandCold

@LeftHandCold LeftHandCold commented Nov 23, 2026 •

Copy link
Copy Markdown
Collaborator

Purpose

Requested exact revert of #33011, for the Main Sysbench regression tracked in #33064. Draft: not mergeable on current main. This is a one-commit inverse on top of the original squash 520ee5ceabb8, not a reset of main and not a proposed rollback of subsequent PRs. The resulting Git tree exactly matches a43c86bb8e2d (tree 0f21cdb3e5320f730b4ab4de6bbbe600254419f8).

Integration blocker

Applying the same revert to main@4cadd4f2f6ed produced 108 unmerged paths (87 content conflicts, 21 modify/delete). Examples: vector ownership/metadata, batch/pipeline codecs, parameter evaluation and process serialization. Intervening PRs include #33017, #33024, #33033, #33054, #33052 and a later prepared-DML compatibility change #32002. Do not resolve by taking historical files wholesale: that can roll back unrelated work. No current-main merge build, mixed-version test, or post-revert TPS is claimed.

Performance evidence

The Nov 23 Main Weekly TP comparison tested dea4f40b17d0 and again reported random_points 3,181 vs 6,607 TPS, random_ranges 3,609 vs 8,047 TPS, and nopk_random_points 1,842 vs 3,091 TPS (current vs historical median). All three workload measurements ran 300s with zero reported errors.

On TP CN nightly-regression-dis-tp-cn-9s5c2, CPU profiles over 300s starting Nov 22 16:40:32 and 16:45:34 UTC put 40.7% and 44.4% of samples, respectively, below ParamExpressionExecutor.evalTyped; 36.7% and 40.1% of all samples were in its runtime.Callers allocation-recording path. These are nested percentages, not additive. This corroborates the previously reported mechanism, not a controlled measurement of the gain from this revert.

For No-PK at 19:20:45 UTC, the main-labelled scrape was empty, but the same TP pod and MO pprof endpoint (10.143.86.97:6060) had a CPU scrape under its enable-conntrack discovery label. In that scrape, 33.8% was under MO evalTyped, 33.8% under its vector duplicate and 30.7% under its runtime.Callers path. Only that one scrape was used for this window.

Keep #33064 open until an integrated candidate preserves the prepared-parameter SQL and transport contracts and a same-environment, same-data, profiling-on comparison shows recovery of the affected workloads.

@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 →

@mergify

mergify Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

This pull request is closed, so it cannot be queued. Reopen it to get the box back.

@fengttt fengttt 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.

No we don't revert this. Refix.

@fengttt fengttt closed this Nov 23, 2026
fengttt added a commit that referenced this pull request Nov 23, 2026
…ecution (#33076) (#33081)

## What type of PR is this?

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

## Which issue(s) this PR fixes:

issue #33076

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

Fixes the sysbench regression from #33011 (typed prepared parameters)
without reverting it. The affected workloads are `random_points`,
`random_ranges` and `nopk_random_points`, with ps-mode=auto.

**Rule applied:** a prepared statement allocates what parameter binding
needs, holds it in the statement, and reuses it. After the first
execution, binding allocates only when a value needs more room than
before.

### Cause
Binding allocated on every EXECUTE and in every expression executor:
- `installTypedPrepareParams` built a new batch, plus one vector per
parameter, on every execution.
- Each `ParamExpressionExecutor` copied its parameter: a `Dup` of the
typed value, or a new constant for a string. The per-EXECUTE diagnostic
probes (`ProbePreparedDiagnosticCandidatesWithProof`) and `EvalFoldExpr`
build fresh executor trees, so every `?` in `k IN (?, …)` was copied
several times per statement.
- A `?` that the planner narrows to its consumer's integer type
(protocol BIGINT vs an INT column) built a whole CAST executor inside
each executor.

This matches the CI profiles in #33074, where 30–44% of CPU was under
`evalTyped` and `runtime.Callers`.

### Changes
- **Typed parameters.** `PrepareStmt` owns the typed parameter vectors
and an integer-conversion store (`process.TypedParamConversions`).
EXECUTE rewrites them in place and the process only borrows them.
- A narrowed integer is converted once per (position, type), then
rewritten in place.
- A value that doesn't fit keeps the CAST adapter, so error and
selection semantics are unchanged.
- **Executor reads.** `ParamExpressionExecutor` returns a read-only
reference instead of a copy.
- The reference is a descriptor embedded in the executor, filled by the
new `Vector.WindowInto` (a method only; no new Vector fields).
  - Typed values reference the statement-owned typed vector.
  - String values reference their row of the TEXT transport.
  - `EvalWithoutResultReusing` still hands out an owned copy.
- A folded value is re-read when the process holds different parameter
values, so a reference can't go stale.
- **COM_STMT_EXECUTE TEXT transport.** The vector is rewritten from an
empty area that keeps its capacity. `SetBytesAt` appends, so before this
change the area grew (and reallocated) on every execution that had a
string parameter longer than the inline varlena.
- **SQL `EXECUTE … USING`.** The statement owns the TEXT vector and its
metadata slices (`executeUserParams`), rewritten in place and borrowed
by the process. Previously they were built fresh on every execution.

### Benchmark
Setup: local single-node, ps-mode=auto, 16 threads, 60 s, 10 tables × 1M
rows. `sbidx` has PK(id) + KEY(k); `sbnopk` has KEY(id) + KEY(k) and no
PK, matching CI's nopk case. Runs are interleaved ABBA (main, fix, fix,
main) with `sync` and a 90 s settle between them, because a fixed run
order skewed earlier comparisons.

| workload (qps) | main A1 | fix B1 | fix B2 | main A2 | main avg | fix
avg | Δ |
|---|---|---|---|---|---|---|---|
| random_points | 12,329 | 18,664 | 19,332 | 13,846 | 13,088 | 18,998 |
**+45%** |
| random_ranges | 13,486 | 18,237 | 18,740 | 14,896 | 14,191 | 18,488 |
**+30%** |
| nopk_random_points | 11,501 | 17,069 | 17,628 | 12,715 | 12,108 |
17,348 | **+43%** |

main = 4cadd4f. The fix binary includes the typed-parameter and
string-parameter changes. The SQL `EXECUTE … USING` change is not
exercised by sysbench.

CPU profile of random_points:

| | main | this PR |
|---|---|---|
| `ParamExpressionExecutor.Eval` | 16.3% | 0.9% |
| `mpool.alloc` | 8.2% | 1.2% |

### Follow-ups
- **Separate PR:** pointer storage for the planner's `paramVals`
(`[]any` of `plan.ParamValue`), so building them no longer boxes each
value per execution.
- **#33082:** redesign so the planner reads the typed parameter batch
and the `ParamValue` string-conversion machinery goes away.

## Tests
- **New tests:**
- Typed binding: a second EXECUTE makes zero mpool allocations and
reuses the same vectors; the narrowed conversion is refreshed in place;
a value that no longer fits isn't served; release detaches the process.
- Fresh executors reading typed and string parameters allocate nothing
and don't mutate the shared value. `EvalWithoutResultReusing` returns an
owned copy. New TEXT parameters are re-read rather than served through a
stale reference.
- Integer conversion boundaries, plus out-of-range and negative→unsigned
values keeping the CAST error.
- COM_STMT_EXECUTE with a 40-byte string: no allocation on re-execute; a
longer value grows the area once.
- SQL `EXECUTE … USING`: storage reused with no allocation on
re-execute; release detaches the borrowing process.
- **Passing:** `pkg/frontend`, `pkg/sql/colexec`, `pkg/vm/process`,
`pkg/container/vector`.
- **Not run locally:** `make static-check`, plan/compile UTs and BVT. CI
will cover them.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>

This branch was successfully deployed

No deployments
ci — 5e5fbff6 Deployed Nov 23, 2026 by XuPeng-SH
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants