Skip to content

perf: close a connection without building an error nothing reads - #169

Merged
okomarov merged 1 commit into
mainfrom
perf/close-without-error
Oct 20, 2026
Merged

okomarov merged 1 commit into
mainfrom
perf/close-without-error

Conversation

@okomarov

@okomarov okomarov commented Oct 20, 2026 •

Copy link
Copy Markdown

A graceful close builds its CONNECTION_CLOSED error only when a COPY stream, a final callback, the current or a sent query, an acquisition or a reserved owner will receive it. The pool builds its own in closedWhileEnding() when a reconnect closes gracefully with queries still queued. The new closing-no-error startup case counts Errors.connection calls across sql.end() and sql.end({ timeout: 0 }): two on main, none here.

Closes #167

Measurements (CPU per request in ms, connect, select 1, end; release → this branch)

Local workerd profile, 200 rounds (https://github.com/steadybrew/postgresjs/actions/runs/39428234607): errors.js falls from 1.3% (fetch_types off) and 1.0% (on) of samples to 0.0%; medians unchanged at 2.53 and 3.46, p = 0.078 and 0.49.

Node.js 24, same run: median 1.91 → 1.85 with fetch_types off (p = 0.00062), 2.39 → 2.34 with it on (p = 0.019).

Edge cpu job, 150 rounds (https://github.com/steadybrew/postgresjs/actions/runs/39425601839): medians and p95 identical in all three groups (Supabase 8/12 and 12/17, Neon 9/13); gate passes (p = 0.38 and 0.48). p99 17 → 29 (Supabase, fetch_types off) and 15 → 25 (Neon) rest on the top one or two of about 128 requests.

Edge A/B against main, 150 rounds, Supabase pooler, fetch_types false (https://github.com/steadybrew/postgresjs/actions/runs/39437582660): median 6 and 6, p95 10 → 8, p99 18 → 9, max 27 → 18; costs less, p = 0.12, so no detectable change on the edge. The A/A run (https://github.com/steadybrew/postgresjs/actions/runs/39437577733) shows warm-isolate spikes of 26 ms on unchanged code.

Every graceful close built a CONNECTION_CLOSED error with a stack trace:
enterClosed() took it as its default after sql.end() (the Closing phase's
clean socket close, its timeout and terminate) and on terminate while
idle. Only a COPY stream, a final callback, the current or a sent query,
an acquisition or a reserved owner receives it, and a normal sql.end()
has none of them. On Workers a client is created and ended per request,
and the error was 1.4-2% of samples on connect, select 1, end under the
local workerd profiler.

enterClosed() now builds it only when one of those exists. The pool's
closedWhileEnding() builds its own when a reconnect closes gracefully
with queries still queued, as leaseError() does for leases.

The closing-no-error startup case counts Errors.connection calls across
sql.end() and sql.end({ timeout: 0 }); it saw two on main and none now.

Closes #167
@okomarov
okomarov force-pushed the perf/close-without-error branch from 0f283c3 to 9ff3b83 Compare October 20, 2026 12:57
@okomarov
okomarov merged commit 9ff3b83 into main Oct 20, 2026
33 of 34 checks passed
@okomarov
okomarov deleted the perf/close-without-error branch October 20, 2026 14:54
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.

[FIXED in 4.6.0] Every graceful close builds a CONNECTION_CLOSED error that nothing reads

1 participant