Fix numbering of PostgreSQL params across reusable queries - #292
Open
Niols wants to merge 2 commits into
Open
Conversation
Add tests covering `include: reuse` queries spliced as CTEs via `WITH x AS &ref`, compiled with `-params postgresql`. The expected output captures the current behaviour, where each fragment restarts `$N` numbering at `$1` so the outer query's placeholders collide with the reused query's.
When a reusable query (`include: reuse`) is spliced into an outer query as a CTE via `WITH x AS &ref`, its parameters and the outer query's parameters are emitted into a single statement and bound positionally by `set_params`. However, `substitute_vars` restarted the positional counter (`parami`) at 0 for the spliced fragment and then continued the outer query with the unchanged counter, so both fragments numbered their PostgreSQL `$N` placeholders from `$1`. Thread `parami` through the shared-query splice: `loop_and_squash` now takes the current counter and returns the advanced one, so numbering runs continuously across the whole statement. Only the PostgreSQL params mode was affected; named/oracle key by parameter name and unnamed uses `?`.
Niols
added a commit
to Niols/sqlgg
that referenced
this pull request
Jul 4, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When a reusable query (
-- @foo | include: reuse) is spliced into an outer query as a CTE viaWITH x AS &ref, the reused query's parameters and the outer query's parameters end up in a single generated statement, bound positionally byset_params.With
-params postgresql, the positional counter was reset to0for the spliced fragment and left unchanged when the outer query resumed, so both fragments numbered their$Nplaceholders starting from$1. The outer parameters therefore collided with the reused ones. Example:On such an input,
-params postgresqlgenerated:Note the
$1used twice.(This was actually caught by PostgreSQL because
set_paramsstill supplies a value for every position, so I got “bind message supplies N parameters, but prepared statement requires M”.)I checked this only affects the
postgresqlparameter style; others use names (namedandoracle) or use positional?(unnamed).A first commit adds tests exhibiting this behaviour; a second threads the positional counter through the shared-query splice in
substitute_vars.