cockroachdb / cockroachdb/cockroach

sql: support CREATE/DROP DATABASE in stored procedures

Open
#173,163 0 comments 0 reactions 0 assignees View on GitHub
A-sql-plpgsql C-enhancement T-sql-foundations
Dominant language
Go
Stars
32.5k
Forks
4.1k
PR merge metrics
PR metrics pending

Description

**Is your feature request related to a problem? Please describe.**

`CREATE DATABASE` and `DROP DATABASE` are not currently allowed inside PL/pgSQL stored procedure bodies. This is a continuation of the DDL support we have been adding to stored procedures — [#168464](https://github.com/cockroachdb/cockroach/issues/168464) added `CREATE TABLE` / `DROP TABLE`, [#169427](https://github.com/cockroachdb/cockroach/issues/169427) tracks `TRUNCATE TABLE`, and [#172760](https://github.com/cockroachdb/cockroach/issues/172760) tracks `CREATE TABLE AS` — but database-level DDL is not yet supported.

**Describe the solution you'd like**

Allow `CREATE DATABASE` and `DROP DATABASE` inside stored procedure bodies, following the same pattern established for the other DDL statements:

- Add the `CREATE DATABASE` and `DROP DATABASE` AST nodes to the optbuilder allowlist for stored procedures.
- Gate the feature behind the appropriate cluster version.
- Limit the feature to stored procedures (not functions or DO blocks), consistent with the pattern established in [#168464](https://github.com/cockroachdb/cockroach/issues/168464).

**Describe alternatives you've considered**

- Issuing the `CREATE DATABASE` / `DROP DATABASE` statements outside the procedure — works today but defeats the purpose of encapsulating provisioning/teardown logic in a procedure.

**Additional context**

- Part of the ongoing DDL-in-stored-procedures effort (same line of work as [#169427](https://github.com/cockroachdb/cockroach/issues/169427) and [#172760](https://github.com/cockroachdb/cockroach/issues/172760)).
- As noted in [#168464](https://github.com/cockroachdb/cockroach/issues/168464), at compile time all statement references are resolved against the current catalog. This is a known divergence from PostgreSQL's late-binding model and will be addressed separately.

Jira issue: CRDB-66489

Epic CRDB-65937

Contributor guide

Open the contributing guide

Research direction

Read the stored-procedure DDL support from #168464, then start at the optbuilder allowlist for the CREATE DATABASE and DROP DATABASE AST nodes. Check the appropriate cluster-version gate and verify that the statements are accepted in stored procedures but rejected in functions and DO blocks.

Written by the indexing model from the issue text.

Assessment

Tech stack
go, sql
Domain
databases
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
62/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.