Skip to content

Skip RETURNING-INTO regex in NewStmt when text cannot match - #790

Open
tructxn wants to merge 1 commit into
sijms:masterfrom
tructxn:fix/789-newstmt-regex-prefilter
Open

tructxn wants to merge 1 commit into
sijms:masterfrom
tructxn:fix/789-newstmt-regex-prefilter

Conversation

@tructxn

@tructxn tructxn commented Nov 14, 2026

Copy link
Copy Markdown

Fixes #789

What

NewStmt runs (\bRETURNING\b|\bRETURN\b)\s+.*\s+\bINTO\b on every non-PL/SQL statement. Without a statement cache this happens on every query. The regex can only match text that contains both INTO and RETURN, so a strings.Contains pre-check now skips it in the common case.

Applied to both v2/command.go and v3/command.go. Behavior is unchanged: both checks run on the same upper-cased uCmdText, and RETURNING contains RETURN.

Tests

newstmt_test.go (in v2 and v3) adds:

  • TestNewStmtReturnClause: 7 cases (UPDATE/DELETE ... RETURNING/RETURN ... INTO, lower-case SQL, INSERT ... SELECT, INSERT ... VALUES, string literal containing RETURNING x INTO y). It passes on master before and after this change.
  • BenchmarkNewStmtSelect29Cols

Benchmark

go1.26.3, darwin/arm64, -benchtime 2s -count 3:

v2 ns/op v3 ns/op allocs/op
master 13,570 – 13,764 13,819 – 14,521 5
this PR 1,487 – 1,510 1,474 – 1,506 5

Note

Several existing _test.go files on master do not compile (v2: result_set_test.go, parameter_encode_test.go, utils_test.go; v3: same three plus bulkcopy_test.go), so I ran the new test with those files moved aside locally. They are not touched in this PR. Happy to fix them in a separate PR.

🤖 Generated with Claude Code

NewStmt runs an unanchored regex with `.*` on every non-PL/SQL
statement, and without a statement cache it runs on every query.
The regex can only match text containing both INTO and RETURN, so a
substring pre-check skips it for the common case with no behavior
change. NewStmt for a 29-column SELECT drops from ~13.6us to ~1.5us.

Fixes sijms#789

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

NewStmt runs the RETURNING-INTO regex on every statement (~90% of NewStmt time for a wide SELECT)

1 participant