Skip to content

fix: improve scope building performance - #1312

Merged
ferhatelmas merged 1 commit into
masterfrom
ferhat/scope-perf
Aug 10, 2026
Merged

fix: improve scope building performance#1312
ferhatelmas merged 1 commit into
masterfrom
ferhat/scope-perf

Conversation

@ferhatelmas

Copy link
Copy Markdown
Member

What kind of change does this PR introduce?

Bug fix

What is the current behavior?

Scope building allocates an intermediate object and sql is rebuilt each time.

What is the new behavior?

Pass values directly, no copy, and initialize sql module level once.

Additional context

Related to #1309

Signed-off-by: ferhat elmas <elmas.ferhat@gmail.com>
@ferhatelmas
ferhatelmas requested a review from a team as a code owner August 10, 2026 13:10
Copilot AI lite review requested due to automatic review settings August 10, 2026 13:10

Copilot AI 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.

Pull request overview

Improves PostgreSQL request-scope statement construction by reducing per-call allocations and reusing prebuilt SQL fragments, while updating call sites and tests to the new API.

Changes:

  • Refactors buildScopeStatement to take connection options + payload strings directly and select from prebuilt SQL strings.
  • Adds ScopeConnectionOptions (derived from TenantConnectionOptions) and removes the prior Scope object shape from this module’s public surface.
  • Updates PgTenantConnection.setScope and expands test coverage for placeholder ordering (incl. timeout-only case).

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
src/internal/database/postgres/scope.ts Prebuilds scope SQL variants at module load and refactors scope statement builder to avoid dynamic setter list construction.
src/internal/database/postgres/scope.test.ts Updates tests to new buildScopeStatement signature and adds coverage for timeout-only placeholder behavior.
src/internal/database/pg-connection.ts Updates setScope to call the refactored buildScopeStatement API without constructing an intermediate object.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM — straightforward performance refactor with no behavior change.

Extended reasoning...

Overview

This PR refactors buildScopeStatement in src/internal/database/postgres/scope.ts to avoid allocating an intermediate options object on every call and to avoid rebuilding the SQL setter array/string each invocation. The four possible SQL text variants (no optional settings, timeout only, search path only, both) are now precomputed as module-level constants, and the function takes positional arguments (including the request-scoped options object directly) instead of spreading fields into a fresh object. The caller in pg-connection.ts is updated to match the new signature, passing this.options directly rather than constructing a new scope object.

Security risks

None identified. This is a pure refactor of internal SQL-building logic; all values are still passed as bound parameters ($1..$11), not interpolated into the SQL text, so there is no injection risk introduced. The set of settings applied (role, jwt claims, headers, method, path, operation, timeout, search path) is unchanged.

Level of scrutiny

Low-to-medium. This touches a function used on every tenant connection scope-setting query, but the change is mechanical: same values, same parameter ordering, same conditional SQL branches, just restructured to avoid redundant allocation/string-building work. I traced the value ordering through all four SQL variants and confirmed placeholder indices ($10/$11) match the values array in each case, and verified the new ScopeConnectionOptions type (a Pick of TenantConnectionOptions) matches how options.user.jwt, options.user.payload.sub, options.method, options.path, and options.operation?.() are used, matching the pre-refactor field access semantics exactly.

Other factors

Test coverage was updated in scope.test.ts to match the new signature and adds a new case (timeout-only, no search path) that wasn't previously covered. No outstanding review comments exist on this PR.

@coveralls

coveralls commented Aug 10, 2026

Copy link
Copy Markdown

Coverage Report for CI Build 31391575575

Coverage increased (+0.1%) to 80.864%

Details

  • Coverage increased (+0.1%) from the base build.
  • Patch coverage: 1 uncovered change across 1 file (15 of 16 lines covered, 93.75%).
  • 2232 coverage regressions across 113 files.

Uncovered Changes

File Changed Covered %
src/internal/database/postgres/scope.ts 16 15 93.75%

Coverage Regressions

2232 previously-covered lines in 113 files lost coverage.

Top 10 Files by Coverage Loss Lines Losing Coverage Coverage
src/storage/renderer/image.ts 156 54.55%
src/storage/protocols/iceberg/catalog/rest-catalog-client.ts 128 38.83%
src/internal/streams/hash-stream.ts 108 26.74%
src/storage/backend/file.ts 106 34.6%
src/internal/database/migrations/migrate.ts 104 46.21%
src/internal/concurrency/async-abort-controller.ts 73 25.41%
src/internal/queue/queue.ts 71 10.62%
src/internal/monitoring/system/cgroup-cpu-collector.ts 65 33.83%
src/storage/protocols/vector/vector-store.ts 63 65.73%
src/internal/database/migrations/progressive.ts 58 25.62%

Coverage Stats

Coverage Status
Relevant Lines: 12768
Covered Lines: 10749
Line Coverage: 84.19%
Relevant Branches: 7597
Covered Branches: 5719
Branch Coverage: 75.28%
Branches in Coverage %: Yes
Coverage Strength: 511.37 hits per line

💛 - Coveralls

@ferhatelmas
ferhatelmas merged commit ee53ddb into master Aug 10, 2026
50 of 52 checks passed
@ferhatelmas
ferhatelmas deleted the ferhat/scope-perf branch August 10, 2026 14:30
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.

4 participants