Skip to content

sql: add ALTER TABLE ... SET/ADD IDENTITY to the declarative schema…#157144

Merged
craig[bot] merged 2 commits intocockroachdb:masterfrom
shghasemi:set-genid
Nov 12, 2025
Merged

sql: add ALTER TABLE ... SET/ADD IDENTITY to the declarative schema…#157144
craig[bot] merged 2 commits intocockroachdb:masterfrom
shghasemi:set-genid

Conversation

@shghasemi
Copy link
Copy Markdown
Contributor

… changer

This change adds support for ALTER TABLE ... SET/ADD IDENTITY to the declarative schema changer. This schema change operation runs using the legacy schema changer in versions older than 26.1.

Epic: CRDB-31283

Fixes #142918

Release note (sql change): ALTER TABLE ... SET/ADD GENERATED AS IDENTITY is supported by the declerative schema changer in 26.1 or later.

@shghasemi shghasemi self-assigned this Nov 10, 2025
@shghasemi shghasemi requested a review from a team as a code owner November 10, 2025 18:49
@blathers-crl
Copy link
Copy Markdown

blathers-crl bot commented Nov 10, 2025

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

rel.EntityAttr(DescID, "TableID"),
rel.EntityAttr(ColumnID, "ColumnID"),
rel.EntityAttr(GeneratedAsIdentityType, "Type"),
rel.EntityAttr(GeneratedAsIdentitySequenceOption, "SequenceOption"),
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Lets use the Expr attribute type here to have uniqueness. That will at least cut back on the changes needed to support more attributes.

@blathers-crl
Copy link
Copy Markdown

blathers-crl bot commented Nov 11, 2025

It looks like your PR touches production code but doesn't add or edit any test code. Did you consider adding tests to your PR?

🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf.

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.

@shghasemi Great work, a few minor comments

@fqazi reviewed 3 of 43 files at r1, 74 of 74 files at r2, 53 of 53 files at r3, all commit messages.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @shghasemi)


pkg/sql/schemachanger/screl/attr.go line 58 at r2 (raw file):

	// ReferencedDescID is the descriptor ID to which this element refers.
	ReferencedDescID
	// A string value, including:

Start with with the name of the field

// Value is a string value, for example:
...

Also lets add a Note here:

Fields referred to the Value attribute cannot be used in rules, and only used as a park of the key to make an element unique.

pkg/sql/schemachanger/scbuild/internal/scbuildstmt/alter_table_alter_column_add_identity.go line 35 at r3 (raw file):

	colTypeElem := mustRetrieveColumnTypeElem(b, tbl.TableID, columnID)
	// Ensure that column is an integer
	if colTypeElem.Type == nil || colTypeElem.Type.InternalType.Family != types.IntFamily {

Is there any scenario where the type would be nil?


pkg/sql/schemachanger/scbuild/internal/scbuildstmt/alter_table_alter_column_add_identity.go line 102 at r3 (raw file):

	colDef, expr := alterTableAddColumnSerialOrGeneratedIdentity(b, colDef, tn)
	// 2. add the sequence owner
	b.Add(&scpb.SequenceOwner{

If there is some commonality between the add column we could break this ould into a function.


pkg/sql/schemachanger/scbuild/internal/scbuildstmt/alter_table_alter_column_add_identity.go line 103 at r3 (raw file):

	// 2. add the sequence owner
	b.Add(&scpb.SequenceOwner{
		SequenceID: expr.UsesSequenceIDs[0],

Nit: Paranoid me wants to assert that this is always 1.


pkg/sql/schemachanger/scbuild/internal/scbuildstmt/alter_table_alter_column_add_identity.go line 113 at r3 (raw file):

		Expression: *expr,
	})
	// ?? b.IncrementSchemaChangeAddColumnQualificationCounter("default_expr")

These are limited only to new columns to not needed.

This change renames `Comment` to `Value` to be used as a generic string
type in other schema changer elements. This attr is not used to filter
schema change element, and can be used as a generic type to avoid
adding new attrs.

Epic: CRDB-31283

Release note: None
@github-actions
Copy link
Copy Markdown
Contributor

Potential Bug(s) Detected

The three-stage Claude Code analysis has identified potential bug(s) in this PR that may warrant investigation.

Next Steps:
Please review the detailed findings in the workflow run.

Note: When viewing the workflow output, scroll to the bottom to find the Final Analysis Summary.

After you review the findings, please tag the issue as follows:

  • If the detected issue is real or was helpful in any way, please tag the issue with O-AI-Review-Real-Issue-Found
  • If the detected issue was not helpful in any way, please tag the issue with O-AI-Review-Not-Helpful

@github-actions github-actions bot added the o-AI-Review-Potential-Issue-Detected AI reviewer found potential issue. Never assign manually—auto-applied by GH action only. label Nov 11, 2025
@shghasemi
Copy link
Copy Markdown
Contributor Author

pkg/sql/schemachanger/screl/attr.go line 58 at r2 (raw file):
Start with with the name of the field
Also lets add a Note here:

Done

pkg/sql/schemachanger/scbuild/internal/scbuildstmt/alter_table_alter_column_add_identity.go line 35 at r3 (raw file):

	colTypeElem := mustRetrieveColumnTypeElem(b, tbl.TableID, columnID)
	// Ensure that column is an integer
	if colTypeElem.Type == nil || colTypeElem.Type.InternalType.Family != types.IntFamily {

Is there any scenario where the type would be nil?

No, you are correct. Fixed.

pkg/sql/schemachanger/scbuild/internal/scbuildstmt/alter_table_alter_column_add_identity.go line 102 at r3 (raw file):

	colDef, expr := alterTableAddColumnSerialOrGeneratedIdentity(b, colDef, tn)
	// 2. add the sequence owner
	b.Add(&scpb.SequenceOwner{

If there is some commonality between the add column we could break this ould into a function.

Most of the logic is shared. I made a small refactor to separate the create sequence functionality.

pkg/sql/schemachanger/scbuild/internal/scbuildstmt/alter_table_alter_column_add_identity.go line 103 at r3 (raw file):

	// 2. add the sequence owner
	b.Add(&scpb.SequenceOwner{
		SequenceID: expr.UsesSequenceIDs[0],

Nit: Paranoid me wants to assert that this is always 1.

Done

pkg/sql/schemachanger/scbuild/internal/scbuildstmt/alter_table_alter_column_add_identity.go line 113 at r3 (raw file):

		Expression: *expr,
	})
	// ?? b.IncrementSchemaChangeAddColumnQualificationCounter("default_expr")

These are limited only to new columns to not needed.

Removed.

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.

:lgtm_strong:

@fqazi reviewed 41 of 41 files at r4, 41 of 41 files at r5, all commit messages.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @shghasemi)


pkg/sql/schemachanger/scbuild/internal/scbuildstmt/alter_table_add_column.go line 405 at r5 (raw file):

}

func alterTableCreateSequence(

Maybe somerthing like: alterTableCreateColumnSequence

… changer

This change adds support for `ALTER TABLE ... SET/ADD IDENTITY` to the
declarative schema changer. This schema change operation runs using the
legacy schema changer in versions older than 26.1.

Epic: CRDB-31283

Fixes cockroachdb#142918

Release note (sql change): `ALTER TABLE ... SET/ADD GENERATED AS IDENTITY`
is supported by the declerative schema changer in 26.1 or later.
@shghasemi shghasemi added the O-AI-Review-Real-Issue-Found AI reviewer found real issue label Nov 12, 2025
@shghasemi
Copy link
Copy Markdown
Contributor Author

pkg/sql/schemachanger/scbuild/internal/scbuildstmt/alter_table_add_column.go line 405 at r5 (raw file):

}

func alterTableCreateSequence(

Maybe somerthing like: alterTableCreateColumnSequence

Done

@shghasemi
Copy link
Copy Markdown
Contributor Author

TFTR!

bors r+

craig bot pushed a commit that referenced this pull request Nov 12, 2025
155872: changefeedccl: fix timestamp for desc fetches during planning r=andyyang890,aerfrei a=log-head

When a changefeed is planned and executed, there are several places where target table descriptors are fetched. With a db-level changefeed, the set of tables can change during changefeed startup. Now, the timestamp is set to the schema timestamp for retrieving table descriptors.

Adding a schema_ts to the protobuf for ChangeAggregatorSpec and ChangeFrontierSpec.

Fixes: #154549
Epic: CRDB-1421

Release note: None

157144: sql: add `ALTER TABLE ... SET/ADD IDENTITY` to the declarative schema… r=shghasemi a=shghasemi

… changer

This change adds support for `ALTER TABLE ... SET/ADD IDENTITY` to the declarative schema changer. This schema change operation runs using the legacy schema changer in versions older than 26.1.

Epic: CRDB-31283

Fixes #142918

Release note (sql change): `ALTER TABLE ... SET/ADD GENERATED AS IDENTITY` is supported by the declerative schema changer in 26.1 or later.

157158: roachtest: do not build arm64 and fips if probabilitiy is 0 r=DarrylWong a=rail

Previously, even when the probabilities for arm64 and fips builds were set to 0, the nightly roachtest script would still build those variants, which is unnecessary. This change modifies the script to skip building arm64 and fips variants when their respective probabilities are set to 0.

Epic: none
Release note: none

Co-authored-by: Matthew Lougheed <matthew.lougheed@cockroachlabs.com>
Co-authored-by: Shadi Ghasemitaheri <shadi.ghasemitaheri@cockroachlabs.com>
Co-authored-by: Rail Aliiev <rail@iqchoice.com>
@craig
Copy link
Copy Markdown
Contributor

craig bot commented Nov 12, 2025

Build failed (retrying...):

craig bot pushed a commit that referenced this pull request Nov 12, 2025
155872: changefeedccl: fix timestamp for desc fetches during planning r=andyyang890,aerfrei a=log-head

When a changefeed is planned and executed, there are several places where target table descriptors are fetched. With a db-level changefeed, the set of tables can change during changefeed startup. Now, the timestamp is set to the schema timestamp for retrieving table descriptors.

Adding a schema_ts to the protobuf for ChangeAggregatorSpec and ChangeFrontierSpec.

Fixes: #154549
Epic: CRDB-1421

Release note: None

157144: sql: add `ALTER TABLE ... SET/ADD IDENTITY` to the declarative schema… r=shghasemi a=shghasemi

… changer

This change adds support for `ALTER TABLE ... SET/ADD IDENTITY` to the declarative schema changer. This schema change operation runs using the legacy schema changer in versions older than 26.1.

Epic: CRDB-31283

Fixes #142918

Release note (sql change): `ALTER TABLE ... SET/ADD GENERATED AS IDENTITY` is supported by the declerative schema changer in 26.1 or later.

Co-authored-by: Matthew Lougheed <matthew.lougheed@cockroachlabs.com>
Co-authored-by: Shadi Ghasemitaheri <shadi.ghasemitaheri@cockroachlabs.com>
@craig
Copy link
Copy Markdown
Contributor

craig bot commented Nov 12, 2025

This PR was included in a batch that successfully built, but then failed to merge into master (it was a non-fast-forward update). It will be automatically retried.

@craig
Copy link
Copy Markdown
Contributor

craig bot commented Nov 12, 2025

@craig craig bot merged commit b4ddc73 into cockroachdb:master Nov 12, 2025
23 of 24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

o-AI-Review-Potential-Issue-Detected AI reviewer found potential issue. Never assign manually—auto-applied by GH action only. O-AI-Review-Real-Issue-Found AI reviewer found real issue v26.1.0-prerelease

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sql/schemachanger: support ALTER TABLE ... ALTER COLUMN ... [ADD | SET] ... IDENTITY in declarative schema changer

3 participants