cockroachdb / cockroachdb/cockroach

sql: support CREATE/DROP VIEW in stored procedures

Open
#173,164 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 VIEW` and `DROP VIEW` 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`, and [#173163](https://github.com/cockroachdb/cockroach/issues/173163) tracks `CREATE/DROP DATABASE` — but view DDL is not yet supported.

**Describe the solution you'd like**

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

- Add the `CREATE VIEW` and `DROP VIEW` 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).

**Scope**

- This covers standard (non-materialized) views only. Materialized views (`CREATE MATERIALIZED VIEW` / `DROP MATERIALIZED VIEW`) are explicitly out of scope for this issue and should not be added to the allowlist here.

**Describe alternatives you've considered**

- Issuing the `CREATE VIEW` / `DROP VIEW` 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), and [#173163](https://github.com/cockroachdb/cockroach/issues/173163)).
- 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-66490](https://cockroachlabs.atlassian.net/browse/CRDB-66490)

Epic CRDB-65937

Contributor guide

Open the contributing guide

Research direction

Start at the optbuilder allowlist for statements permitted in stored procedure bodies and use the CREATE TABLE/DROP TABLE support from #168464 as the reference. Add standard CREATE VIEW and DROP VIEW with cluster-version gating, while keeping them limited to stored procedures and excluding materialized views, functions, and DO blocks. Done means these statements work inside procedures and remain rejected in the excluded contexts.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.