Skip to content

Conversation

@mgartner
Copy link
Collaborator

@mgartner mgartner commented Nov 26, 2025

sql: add optimizer_build_routine_params_as_placeholders

The optimizer_build_routine_params_as_placeholders session setting has
been added. It currently has no effect on any behavior. It will be fully
implemented in a future commit.

Release note: None

opt: refactor parameter column synthesis

optbuilder.Builder has a new method for synthesizing columns that
represent reference to routine parameters, called
synthesizeParameterColumn.

Release note: None

opt: implement optimizer_build_routine_params_as_placeholders

Routine parameter references have always been represented as columns in
optimizer expressions. They are considered outer columns because no
expression within a routine statement produces them. One consequence of
this is that, when a routine statement is built, normalization rules
might leave the expression tree in a state that cannot be fully
optimized later.

For example, decorrelation rules can pull filters with parameter
references up and away from base relations. As another example, filter
push-down rules may fail to push a filter referencing a parameter down
towards a scan. In both cases, when the parameter references are
replaced with constant values, those filters cannot be pushed into scans
and lookup joins because that are not adjacent to based relations.

The optimizer_build_routine_params_as_placeholders session setting,
when enabled, instructs the optimizer to build parameter reference
columns that are outside the current scope as placeholders instead of
columns. This prevents rules from treating parameter references as outer
columns and misguidedly transforming the expression tree.

Informs #157840

Release note (performance improvement): The
optimizer_build_routine_params_as_placeholders session setting has
been added. It is disabled by default. When enabled, it may lead to
better query plans for statements within user-defined functions and
stored-procedures.

wip

The `optimizer_build_routine_params_as_placeholders` session setting has
been added. It currently has no effect on any behavior. It will be fully
implemented in a future commit.

Release note: None
`optbuilder.Builder` has a new method for synthesizing columns that
represent reference to routine parameters, called
`synthesizeParameterColumn`.

Release note: None
Routine parameter references have always been represented as columns in
optimizer expressions. They are considered outer columns because no
expression within a routine statement produces them. One consequence of
this is that, when a routine statement is built, normalization rules
might leave the expression tree in a state that cannot be fully
optimized later.

For example, decorrelation rules can pull filters with parameter
references up and away from base relations. As another example, filter
push-down rules may fail to push a filter referencing a parameter down
towards a scan. In both cases, when the parameter references are
replaced with constant values, those filters cannot be pushed into scans
and lookup joins because that are not adjacent to based relations.

The `optimizer_build_routine_params_as_placeholders` session setting,
when enabled, instructs the optimizer to build parameter reference
columns that are outside the current scope as placeholders instead of
columns. This prevents rules from treating parameter references as outer
columns and misguidedly transforming the expression tree.

Informs cockroachdb#157840

Release note (performance improvement): The
`optimizer_build_routine_params_as_placeholders` session setting has
been added. It is disabled by default. When enabled, it may lead to
better query plans for statements within user-defined functions and
stored-procedures.
@mgartner mgartner requested a review from a team November 26, 2025 22:17
@blathers-crl
Copy link

blathers-crl bot commented Nov 26, 2025

Your pull request contains more than 1000 changes. It is strongly encouraged to split big PRs into smaller chunks.

🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf.

@cockroach-teamcity
Copy link
Member

This change is Reviewable

│ │ │ │ │ │ │ │ │ └── columns: crdb_internal.kv_catalog_comments.classoid:176!null crdb_internal.kv_catalog_comments.objoid:177!null crdb_internal.kv_catalog_comments.objsubid:178!null crdb_internal.kv_catalog_comments.description:179!null
│ │ │ │ │ │ │ │ └── filters
│ │ │ │ │ │ │ │ ├── crdb_internal.kv_catalog_comments.classoid:176 != 4294967075 [outer=(176), constraints=(/176: (/NULL - /4294967074] [/4294967076 - ]; tight)]
│ │ │ │ │ │ │ │ └── crdb_internal.kv_catalog_comments.objoid:177 = c.oid:1 [outer=(1,177), constraints=(/1: (/NULL - ]; /177: (/NULL - ]), fd=(1)==(177), (177)==(1)]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Huh, any idea why the regression here? It's not obvious to me why we're suddenly not decorrelating.

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.

3 participants