Skip to content

perf: skip session control regex for queries starting with SELECT, INSERT, etc - #2036

Merged
jkaczman merged 1 commit into
mainfrom
jk-regex-skip-common-queries
Nov 14, 2026
Merged

jkaczman merged 1 commit into
mainfrom
jk-regex-skip-common-queries

Conversation

@jkaczman

Copy link
Copy Markdown
Contributor

Previously, with query_parser set to auto or session_control, we were running the session control regex on all queries to look for SET, BEGIN, COMMIT, etc. This includes queries such as SELECT 1. In the pooler microbench flamegraph (#2035), the check was a pretty substantial amount (see below):

Screenshot 2026-11-13 at 2 23 54 PM

Now, if the query starts with SELECT, INSERT, UPDATE, DELETE or WITH, we return early and skip the expensive regex call.

How can we do this? All of the session control patterns occur at the very start of the query text (when there's no whitespace / comment present); i.e., if we have a query that starts with a keyword like SELECT, which isn't in the CMD_BASE keywords, there's no point in trying to match with CMD_RE.

session_control_and_locks behavior not changed

@jkaczman
jkaczman added this pull request to stack #2037 November 13, 2026 22:53
@codecov

codecov Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.42857% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
pgdog/src/frontend/regex_parser.rs 96.42% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@github-actions

Copy link
Copy Markdown
Contributor

Metal bench (arm64) passed. All 520 measures within bounds.

suite throughput cpu/txn user cycles/txn user instr/txn kernel cycles/txn kernel instr/txn ctx/txn
pooler 18.88 (-0.7% ± 1.4%) 17,870 (-1.6% ± 0.9%) 31,391 (-0.6% ± 0.05%) 30,442 (-0.2% ± 2.0%) 51,066 (+0.1% ± 0.3%)
lb 19.54 (-0.4% ± 0.7%) 18,494 (-1.7% ± 0.5%) 32,291 (-0.6% ± 0.1%) 31,327 (+0.7% ± 0.9%) 52,140 (+0.0% ± 0.5%)
sharding-w2 32.15 (-0.5% ± 1.2%) 42,982 (+0.0% ± 1.0%) 94,863 (-0.0% ± 0.02%) 40,326 (-1.2% ± 2.6%) 53,680 (+0.0% ± 0.06%) 1.547 (+0.0% ± 0.2%)
sharding-w8 242,588 (-0.0% ± 0.7%) 29.42 (-0.2% ± 1.2%) 44,389 (-0.3% ± 2.0%) 50,448 (+0.0% ± 0.04%) 36,839 (-0.2% ± 0.5%) 42,390 (+0.0% ± 0.1%) 0.1688 (+1.2% ± 3.7%)
Counters per txn
suite frontend stall l1i refill backend stall
pooler 7,536 (-3.1% ± 2.2%) 2,646 (-1.9% ± 0.8%)
lb 7,932 (-3.7% ± 0.8%) 2,705 (-2.2% ± 0.4%)
sharding-w2 17,437 (+0.2% ± 3.0%) 4,066 (+0.3% ± 1.6%)
sharding-w8 20,331 (+0.7% ± 1.8%) 4,121 (+0.7% ± 1.3%)

Microbench: all 507 measures within bounds.

Hot code per txn
path base head change
pooler 113.0 KB 111.1 KB -1.9 KB
sharding-w2 167.6 KB 166.7 KB -0.9 KB

pooler:

crate base head change
pgdog::frontend::client::query_engine 19.9 KB 19.8 KB -0.1 KB
tokio 18.4 KB 18.3 KB -0.1 KB
pgdog::backend::pool 17.5 KB 17.6 KB +0.1 KB
aws-lc (TLS crypto) 10.0 KB 10.1 KB +0.1 KB
rustls (TLS) 8.3 KB 8.4 KB +0.1 KB
pgdog::frontend (other) 7.8 KB 7.8 KB +0.1 KB
pgdog::backend::server 7.8 KB 7.7 KB -0.1 KB
other 5.8 KB 5.7 KB -0.1 KB
everything else 17.6 KB 15.8 KB -1.8 KB
function head change
pgdog::frontend::client::Client::run::{{closure}} 5.5 KB +0.1 KB
pgdog::frontend::client::query_engine::query::<impl pgdog::frontend::client::query_engine: 5.2 KB -0.1 KB
pgdog::frontend::client::query_engine::QueryEngine::handle::{{closure}} 4.3 KB +0.1 KB
pgdog::frontend::client::query_engine::query::<impl pgdog::frontend::client::query_engine: 3.2 KB +0.1 KB
tokio::runtime::task::raw::poll 3.1 KB -0.1 KB

sharding-w2:

crate base head change
pgdog::frontend::router 28.8 KB 28.5 KB -0.3 KB
pgdog::frontend::client::query_engine 21.1 KB 21.1 KB -0.1 KB
pgdog::backend::pool 18.4 KB 18.5 KB +0.1 KB
tokio 19.0 KB 18.4 KB -0.6 KB
aws-lc (TLS crypto) 12.7 KB 12.2 KB -0.4 KB
other 10.0 KB 9.8 KB -0.2 KB
pgdog::frontend (other) 8.8 KB 8.9 KB +0.1 KB
rustls (TLS) 8.2 KB 8.2 KB +0.1 KB
everything else 40.6 KB 40.9 KB +0.4 KB
function head change
pgdog::frontend::client::Client::run::{{closure}} 6.4 KB +0.0 KB
pgdog::frontend::client::query_engine::query::<impl pgdog::frontend::client::query_engine: 5.1 KB -0.1 KB
pgdog::frontend::client::query_engine::QueryEngine::handle::{{closure}} 4.0 KB +0.0 KB
pgdog::frontend::router::parser::query::select::<impl pgdog::frontend::router::parser::que 3.2 KB -0.1 KB
tokio::runtime::task::raw::poll 3.2 KB -0.3 KB
lb flamegraph vs 5bb8ba3

lb

Open interactive for hover, zoom and search.

lb removed

Open interactive.

microbench flamegraph vs 5bb8ba3

microbench

Open interactive for hover, zoom and search.

microbench removed

Open interactive.

pooler flamegraph vs 5bb8ba3

pooler

Open interactive for hover, zoom and search.

pooler removed

Open interactive.

sharding-w2 flamegraph vs 5bb8ba3

sharding-w2

Open interactive for hover, zoom and search.

sharding-w2 removed

Open interactive.

sharding-w8 flamegraph vs 5bb8ba3

sharding-w8

Open interactive for hover, zoom and search.

sharding-w8 removed

Open interactive.

Logs

lb, microbench, pooler, sharding-w2, sharding-w8, verdict.json

Compared 75bc8ac with 5bb8ba3 on c8g.metal-24xl (on-demand).

@github-actions

Copy link
Copy Markdown
Contributor

Metal bench (x86) passed. All 520 measures within bounds.

suite throughput cpu/txn user cycles/txn user instr/txn kernel cycles/txn kernel instr/txn ctx/txn
pooler 25.87 (-0.2% ± 0.5%) 18,630 (-0.6% ± 0.3%) 32,703 (-0.6% ± 0.2%) 33,694 (-0.2% ± 1.0%) 36,556 (-0.1% ± 0.3%)
lb 26.27 (-0.1% ± 0.5%) 19,188 (-0.6% ± 0.2%) 33,697 (-0.6% ± 0.04%) 33,839 (+0.1% ± 0.9%) 37,241 (-0.1% ± 0.6%)
sharding-w2 41.94 (+0.0% ± 2.3%) 47,025 (+0.3% ± 1.0%) 98,537 (-0.0% ± 0.08%) 38,442 (-0.3% ± 2.9%) 40,128 (-0.0% ± 0.1%) 1.527 (-0.0% ± 0.3%)
sharding-w8 206,091 (-0.1% ± 1.2%) 32.89 (-0.0% ± 0.9%) 45,919 (-0.1% ± 1.3%) 52,875 (+0.0% ± 0.07%) 31,266 (+0.1% ± 0.8%) 32,513 (-0.0% ± 0.2%) 0.1737 (+0.1% ± 0.9%)
Counters per txn
suite frontend stall l1i refill backend stall
pooler 3,422 (-1.3% ± 0.6%) 1,138 (-1.1% ± 1.2%) 38,275 (-0.5% ± 0.5%)
lb 3,630 (-0.8% ± 0.9%) 1,185 (-0.5% ± 0.7%) 38,733 (-0.6% ± 0.5%)
sharding-w2 6,711 (-0.8% ± 1.7%) 1,540 (-1.4% ± 2.0%) 112,654 (+1.4% ± 3.4%)
sharding-w8 3,617 (-0.7% ± 0.7%) 872 (-2.2% ± 1.2%) 176,660 (-0.1% ± 1.8%)

Microbench: all 507 measures within bounds.

Hot code per txn
path base head change
pooler 114.6 KB 114.2 KB -0.4 KB
sharding-w2 170.2 KB 168.9 KB -1.3 KB

pooler:

crate base head change
pgdog::frontend::client::query_engine 22.2 KB 22.4 KB +0.1 KB
pgdog::backend::pool 18.1 KB 18.4 KB +0.3 KB
tokio 18.4 KB 18.2 KB -0.2 KB
pgdog::frontend (other) 9.2 KB 9.2 KB +0.1 KB
aws-lc (TLS crypto) 8.8 KB 9.1 KB +0.3 KB
pgdog::backend::server 7.9 KB 7.8 KB -0.2 KB
rustls (TLS) 7.8 KB 7.7 KB -0.1 KB
other 5.6 KB 5.8 KB +0.2 KB
everything else 16.5 KB 15.6 KB -0.9 KB
function head change
pgdog::frontend::client::Client::run::{{closure}} 7.1 KB -0.1 KB
pgdog::frontend::client::query_engine::query::<impl pgdog::frontend::client::query_engine: 6.8 KB +0.2 KB
pgdog::frontend::client::query_engine::QueryEngine::handle::{{closure}} 4.6 KB -0.1 KB
tokio::runtime::task::raw::poll 3.7 KB +0.1 KB
pgdog::frontend::client::query_engine::query::<impl pgdog::frontend::client::query_engine: 3.6 KB -0.1 KB

sharding-w2:

crate base head change
pgdog::frontend::router 28.6 KB 28.1 KB -0.5 KB
pgdog::frontend::client::query_engine 23.2 KB 23.3 KB +0.1 KB
pgdog::backend::pool 19.2 KB 19.1 KB -0.1 KB
tokio 18.6 KB 18.5 KB -0.1 KB
pgdog::frontend (other) 10.8 KB 10.9 KB +0.1 KB
other 10.6 KB 10.7 KB +0.1 KB
aws-lc (TLS crypto) 10.6 KB 10.5 KB -0.1 KB
pgdog::backend::server 8.5 KB 8.4 KB -0.1 KB
everything else 40.1 KB 39.4 KB -0.8 KB
function head change
pgdog::frontend::client::Client::run::{{closure}} 8.4 KB +0.1 KB
pgdog::frontend::client::query_engine::query::<impl pgdog::frontend::client::query_engine: 6.4 KB +0.0 KB
pgdog::frontend::client::query_engine::QueryEngine::handle::{{closure}} 4.6 KB -0.1 KB
tokio::runtime::task::raw::poll 4.1 KB +0.1 KB
pgdog::frontend::router::parser::query::select::<impl pgdog::frontend::router::parser::que 4.1 KB -0.1 KB
lb flamegraph vs 5bb8ba3

lb

Open interactive for hover, zoom and search.

lb removed

Open interactive.

microbench flamegraph vs 5bb8ba3

microbench

Open interactive for hover, zoom and search.

microbench removed

Open interactive.

pooler flamegraph vs 5bb8ba3

pooler

Open interactive for hover, zoom and search.

pooler removed

Open interactive.

sharding-w2 flamegraph vs 5bb8ba3

sharding-w2

Open interactive for hover, zoom and search.

sharding-w2 removed

Open interactive.

sharding-w8 flamegraph vs 5bb8ba3

sharding-w8

Open interactive for hover, zoom and search.

sharding-w8 removed

Open interactive.

Logs

lb, microbench, pooler, sharding-w2, sharding-w8, verdict.json

Compared 75bc8ac with 5bb8ba3 on c7i.metal-48xl (on-demand).

@jkaczman
jkaczman force-pushed the jk-regex-skip-common-queries branch from 75bc8ac to 705da07 Compare November 14, 2026 03:50
Base automatically changed from jk-pooler-microbench to main November 14, 2026 03:51
@jkaczman
jkaczman force-pushed the jk-regex-skip-common-queries branch from 705da07 to bb31092 Compare November 14, 2026 03:51
@jkaczman
jkaczman merged commit da43527 into main Nov 14, 2026
13 checks passed
@jkaczman
jkaczman deleted the jk-regex-skip-common-queries branch November 14, 2026 03:51
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.

2 participants