cockroachdb / cockroachdb/cockroach
sql: support CREATE/DROP TYPE in stored procedures
- 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 TYPE` and `DROP TYPE` 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`, [#172760](https://github.com/cockroachdb/cockroach/issues/172760) tracks `CREATE TABLE AS`, [#173163](https://github.com/cockroachdb/cockroach/issues/173163) tracks `CREATE/DROP DATABASE`, [#173164](https://github.com/cockroachdb/cockroach/issues/173164) tracks `CREATE/DROP VIEW`, and [#173165](https://github.com/cockroachdb/cockroach/issues/173165) tracks `CREATE/DROP SEQUENCE` — but type DDL is not yet supported.
**Describe the solution you'd like**
Allow `CREATE TYPE` and `DROP TYPE` inside stored procedure bodies, following the same pattern established for the other DDL statements:
- Add the `CREATE TYPE` and `DROP TYPE` 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 TYPE` / `DROP TYPE` 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), [#172760](https://github.com/cockroachdb/cockroach/issues/172760), [#173163](https://github.com/cockroachdb/cockroach/issues/173163), [#173164](https://github.com/cockroachdb/cockroach/issues/173164), and [#173165](https://github.com/cockroachdb/cockroach/issues/173165)).
- 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-66492
Epic CRDB-65937
Contributor guide
Research direction
Start in the optbuilder allowlist and compare the existing CREATE TABLE/DROP TABLE stored-procedure support referenced by issue #168464. Check how the appropriate cluster-version gate and procedure-only restriction are applied. Done means CREATE TYPE and DROP TYPE work in stored procedures while remaining unsupported 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
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 68/100