cockroachdb / cockroachdb/cockroach

sql: support CREATE/DROP SEQUENCE in stored procedures

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

**Describe the solution you'd like**

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

- Add the `CREATE SEQUENCE` and `DROP SEQUENCE` 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 SEQUENCE` / `DROP SEQUENCE` 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), and [#173164](https://github.com/cockroachdb/cockroach/issues/173164)).
- 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-66491

Epic CRDB-65937

Contributor guide

Open the contributing guide

Research direction

Start in the optbuilder allowlist for stored-procedure statements and compare the CREATE TABLE/DROP TABLE support from #168464. Trace the CREATE SEQUENCE and DROP SEQUENCE AST nodes and the existing cluster-version gate, ensuring the statements are permitted only in stored procedures, not functions or DO blocks. Done means both statements are covered and the appropriate version-gated tests pass.

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
62/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.