Skip to content

sql: add DDL support for CREATE/DROP TABLE in stored procedures#168464

Merged
trunk-io[bot] merged 1 commit into
cockroachdb:masterfrom
spilchen:general/260414/proc/infra-step-0
Apr 17, 2026
Merged

sql: add DDL support for CREATE/DROP TABLE in stored procedures#168464
trunk-io[bot] merged 1 commit into
cockroachdb:masterfrom
spilchen:general/260414/proc/infra-step-0

Conversation

@spilchen
Copy link
Copy Markdown
Contributor

@spilchen spilchen commented Apr 16, 2026

Allow CREATE TABLE and DROP TABLE inside PL/pgSQL stored procedurebodies, gated by the cluster version (v26.3). This is a lightweight temporary gate — it blocks the feature in mixed-version clusters and
auto-enables once the upgrade to v26.3 is finalized.

The optbuilder allowlist is extended to permit these two DDL statements in procedure definitions (not functions or DO blocks). At compile time, all statement references are resolved against the current catalog, so DDL followed by DML on the newly created table in the same procedure body fails. This is a known divergence from PostgreSQL's late-binding model and will be changed later.

Closes #168460
Epic: CRDB-31256
Release note: None

@spilchen spilchen self-assigned this Apr 16, 2026
@trunk-io
Copy link
Copy Markdown
Contributor

trunk-io Bot commented Apr 16, 2026

😎 Merged directly without going through the merge queue, as the queue was empty and the PR was up to date with the target branch - details.

@blathers-crl
Copy link
Copy Markdown

blathers-crl Bot commented Apr 16, 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

@blathers-crl
Copy link
Copy Markdown

blathers-crl Bot commented Apr 16, 2026

Detected infrastructure failure (matched: self-hosted runner lost communication with the server). Automatically rerunning failed jobs. (run link)

@spilchen spilchen force-pushed the general/260414/proc/infra-step-0 branch from 859a024 to d832d9d Compare April 16, 2026 15:20
@spilchen spilchen requested a review from a team April 16, 2026 16:47
@spilchen spilchen marked this pull request as ready for review April 16, 2026 16:47
@spilchen spilchen requested a review from a team as a code owner April 16, 2026 16:47
@spilchen spilchen requested review from ZhouXing19 and removed request for a team April 16, 2026 16:47
@michae2 michae2 requested review from michae2 and removed request for ZhouXing19 April 16, 2026 16:58
Copy link
Copy Markdown
Collaborator

@fqazi fqazi left a comment

Choose a reason for hiding this comment

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

@spilchen Looks good and straight forward.

:lgtm_strong:

@fqazi reviewed 11 files and all commit messages, and made 3 comments.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on michae2 and spilchen).


pkg/sql/opt/optbuilder/builder.go line 390 at r1 (raw file):

		case *tree.DoBlock:
		case *tree.CreateTable, *tree.DropTable:
			// DDL is allowed inside stored procedures when the cluster has

We probably wan to move this comment down.


pkg/sql/logictest/testdata/logic_test/procedure_ddl line 795 at r1 (raw file):

#
# TODO(spilchen): this will be something we need to address. We must upgrade to
# SERIALIZABLE for DDL in procedures to ensure safety.

Yeah, this would impact other schema changes

Copy link
Copy Markdown
Collaborator

@michae2 michae2 left a comment

Choose a reason for hiding this comment

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

:lgtm: Wow, didn't realize that's (pretty much) all it would take!

@michae2 reviewed 13 files and all commit messages, and made 2 comments.
Reviewable status: :shipit: complete! 2 of 0 LGTMs obtained (waiting on fqazi and spilchen).


pkg/sql/logictest/testdata/logic_test/procedure_ddl line 795 at r1 (raw file):

Previously, fqazi (Faizan Qazi) wrote…

Yeah, this would impact other schema changes

+1

Allow CREATE TABLE and DROP TABLE inside PL/pgSQL stored procedure
bodies, gated by the cluster version (v26.3). This is a lightweight
temporary gate — it blocks the feature in mixed-version clusters and
auto-enables once the upgrade to v26.3 is finalized.

The optbuilder allowlist is extended to permit these two DDL statements
in procedure definitions (not functions or DO blocks). At compile time,
all statement references are resolved against the current catalog, so
DDL followed by DML on the newly created table in the same procedure
body fails — a known divergence from PostgreSQL's late-binding model.

The logic test exercises: transaction commit/rollback, exception handler
savepoints, nested procedures, autocommit_before_ddl interaction,
compile-time binding behavior, and weak isolation levels (READ COMMITTED,
REPEATABLE READ).

Closes cockroachdb#168460
Epic: none
Release note: None

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@spilchen spilchen force-pushed the general/260414/proc/infra-step-0 branch from d832d9d to 417c33f Compare April 17, 2026 11:53
Copy link
Copy Markdown
Contributor Author

@spilchen spilchen left a comment

Choose a reason for hiding this comment

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

@spilchen made 1 comment.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (and 1 stale) (waiting on fqazi and michae2).


pkg/sql/opt/optbuilder/builder.go line 390 at r1 (raw file):

Previously, fqazi (Faizan Qazi) wrote…

We probably wan to move this comment down.

Done.

@spilchen
Copy link
Copy Markdown
Contributor Author

TFTRs!

/trunk merge

@trunk-io trunk-io Bot merged commit 8da09f8 into cockroachdb:master Apr 17, 2026
24 of 25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sql: add DDL support for CREATE/DROP TABLE in stored procedures

4 participants