Skip to content

sql: add late binding for PL/pgSQL procedures behind cluster setting#170901

Draft
spilchen wants to merge 1 commit into
cockroachdb:masterfrom
spilchen:spilchen/gh-170651/pr1-late-binding-core
Draft

sql: add late binding for PL/pgSQL procedures behind cluster setting#170901
spilchen wants to merge 1 commit into
cockroachdb:masterfrom
spilchen:spilchen/gh-170651/pr1-late-binding-core

Conversation

@spilchen
Copy link
Copy Markdown
Contributor

Previously, PL/pgSQL procedure bodies were always analyzed at CREATE PROCEDURE time, recording back-references on referenced descriptors. References had to exist at CREATE time.

This commit adds a new public cluster setting
sql.procedures.plpgsql.late_binding.enabled (default false). When enabled, PL/pgSQL procedure bodies are parsed but not analyzed; references are resolved at CALL time, matching PostgreSQL PL/pgSQL semantics. LANGUAGE SQL procedures and UDFs always use early binding regardless of the setting.

The gate is centralized in PLpgSQLProcedureLateBindingEnabled (which also checks clusterversion.V26_3) and applied at the three CREATE / CREATE OR REPLACE entry points: the legacy planner, the declarative schema changer, and the optbuilder.

Improving the CREATE-time error message when a PL/pgSQL procedure body contains DDL but late binding is off is left to a follow-up; this commit preserves the existing V26_3 DDL-in-procedure behavior unchanged.

Epic: CRDB-31256
Informs: #170651

Release note (sql change): Added public cluster setting sql.procedures.plpgsql.late_binding.enabled (default false). When enabled, PL/pgSQL procedure bodies are not resolved at CREATE PROCEDURE time; references are resolved at CALL time instead, matching PostgreSQL PL/pgSQL semantics. LANGUAGE SQL procedures and functions are unaffected.

Previously, PL/pgSQL procedure bodies were always analyzed at CREATE
PROCEDURE time, recording back-references on referenced descriptors.
References had to exist at CREATE time.

This commit adds a new public cluster setting
`sql.procedures.plpgsql.late_binding.enabled` (default false). When
enabled, PL/pgSQL procedure bodies are parsed but not analyzed;
references are resolved at CALL time, matching PostgreSQL PL/pgSQL
semantics. LANGUAGE SQL procedures and UDFs always use early binding
regardless of the setting.

The gate is centralized in PLpgSQLProcedureLateBindingEnabled (which
also checks clusterversion.V26_3) and applied at the three CREATE /
CREATE OR REPLACE entry points: the legacy planner, the declarative
schema changer, and the optbuilder.

Improving the CREATE-time error message when a PL/pgSQL procedure
body contains DDL but late binding is off is left to a follow-up;
this commit preserves the existing V26_3 DDL-in-procedure behavior
unchanged.

Epic: CRDB-31256
Informs: cockroachdb#170651

Release note (sql change): Added public cluster setting
`sql.procedures.plpgsql.late_binding.enabled` (default false). When
enabled, PL/pgSQL procedure bodies are not resolved at CREATE
PROCEDURE time; references are resolved at CALL time instead,
matching PostgreSQL PL/pgSQL semantics. LANGUAGE SQL procedures and
functions are unaffected.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@spilchen spilchen self-assigned this May 25, 2026
@trunk-io
Copy link
Copy Markdown
Contributor

trunk-io Bot commented May 25, 2026

Merging to master in this repository is managed by Trunk.

  • To merge this pull request, check the box to the left or comment /trunk merge below.

After your PR is submitted to the merge queue, this comment will be automatically updated with its status. If the PR fails, failure details will also be posted here

@blathers-crl
Copy link
Copy Markdown

blathers-crl Bot commented May 25, 2026

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
Copy Markdown
Member

This change is Reviewable

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.

2 participants