Skip to content

Feature: reusing matching and duplicated positional parameters in prepared statements - #1191

Draft
TeeAaTeeUu wants to merge 1 commit into
porsager:masterfrom
TeeAaTeeUu:deduplicated-prepared-statement-parameters
Draft

Feature: reusing matching and duplicated positional parameters in prepared statements#1191
TeeAaTeeUu wants to merge 1 commit into
porsager:masterfrom
TeeAaTeeUu:deduplicated-prepared-statement-parameters

Conversation

@TeeAaTeeUu

@TeeAaTeeUu TeeAaTeeUu commented Aug 12, 2026

Copy link
Copy Markdown

Assuming there usually are only handful of unique positional parameters used per prepared statement, we can rather efficiently find matching ones. This practically moves some of the processing away from Postgres server, to be simplified already on javascript-client side.

Escaping and encoding parameters still happens fully on the Postgres server side.

Input

await sql`
  WITH organization AS (
    SELECT organization_name
    FROM our_schema.organizations
    WHERE organization_id = ${organization_id}
    LIMIT 1
  )

  SELECT
    asset_name,
    organization_name
  FROM our_schema.assets
  CROSS JOIN organization
  WHERE
    organization_id = ${organization_id}
    AND asset_id = ${asset_id}
  LIMIT 1`

Output

WITH organization AS (
  SELECT organization_name
  FROM our_schema.organizations
  WHERE organization_id = $1
  LIMIT 1
)

SELECT
  asset_name,
  organization_name
FROM our_schema.assets
CROSS JOIN organization
WHERE
  organization_id = $1
  AND asset_id = $2
LIMIT 1

I'm not fully convinced myself of this actually being something we would want, but leaving it here in case this would be deemed useful by others.

Deduplicate positional parameters, to simplify prepared statements positional parameters in case there are duplicates.

For example `${organization_id}` a few times in a more complex nested statement.
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.

1 participant