Skip to content

sql,schemachanger: ADD PRIMARY KEY NOT VALID returns an error#97746

Merged
craig[bot] merged 1 commit intocockroachdb:masterfrom
Xiang-Gu:support-alter-table-add-primary-key-not-valid
Feb 28, 2023
Merged

sql,schemachanger: ADD PRIMARY KEY NOT VALID returns an error#97746
craig[bot] merged 1 commit intocockroachdb:masterfrom
Xiang-Gu:support-alter-table-add-primary-key-not-valid

Conversation

@Xiang-Gu
Copy link
Copy Markdown
Contributor

Fixes #96729

Release note (sql change): When we add an unvalidated PK constraint to a table, assuming its PK was on the implicit "rowid" column, will return an error. This is compatible to what PG will do.

@cockroach-teamcity
Copy link
Copy Markdown
Member

This change is Reviewable

@Xiang-Gu Xiang-Gu marked this pull request as ready for review February 27, 2023 20:12
@Xiang-Gu Xiang-Gu requested a review from a team February 27, 2023 20:12
Copy link
Copy Markdown
Contributor

@ajwerner ajwerner left a comment

Choose a reason for hiding this comment

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

I'm glad this was a small change. I think the release note is hard to read. How about using examples? Maybe something like:

Release note (sql change): Prior to this change, ALTER TABLE ... ADD PRIMARY KEY ... NOT VALID was supported, and resulted in the NOT VALID clause being ignored. Now, the NOT VALID clause will result in an error. This behavior matches that of postgres.

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @Xiang-Gu)


pkg/sql/logictest/testdata/logic_test/alter_table line 2950 at r1 (raw file):

CREATE TABLE t_96729 (i INT NOT NULL)

statement error PRIMARY KEY constraints cannot be marked NOT VALID

nit: add a pgcode to this test and make sure it matches postgres's

Copy link
Copy Markdown
Contributor

@ajwerner ajwerner left a comment

Choose a reason for hiding this comment

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

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @Xiang-Gu)


pkg/sql/alter_table.go line 233 at r1 (raw file):

				if d.PrimaryKey {
					if t.ValidationBehavior == tree.ValidationSkip {
						return errors.New("PRIMARY KEY constraints cannot be marked NOT VALID")

this needs a pgcode and so does the one in the declarative schema changer

@Xiang-Gu Xiang-Gu force-pushed the support-alter-table-add-primary-key-not-valid branch from 2b5388e to bdc8bfb Compare February 27, 2023 21:52
Copy link
Copy Markdown
Contributor Author

@Xiang-Gu Xiang-Gu left a comment

Choose a reason for hiding this comment

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

Thanks for the prompt feedback. I've addressed them.

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @ajwerner)


pkg/sql/alter_table.go line 233 at r1 (raw file):

Previously, ajwerner wrote…

this needs a pgcode and so does the one in the declarative schema changer

done


pkg/sql/logictest/testdata/logic_test/alter_table line 2950 at r1 (raw file):

Previously, ajwerner wrote…

nit: add a pgcode to this test and make sure it matches postgres's

done

Copy link
Copy Markdown
Contributor

@ajwerner ajwerner left a comment

Choose a reason for hiding this comment

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

:lgtm:

Reviewed 1 of 4 files at r1, 5 of 6 files at r2, all commit messages.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained

@Xiang-Gu
Copy link
Copy Markdown
Contributor Author

TFTR!

bors r+

@Xiang-Gu
Copy link
Copy Markdown
Contributor Author

bors r-

@craig
Copy link
Copy Markdown
Contributor

craig bot commented Feb 28, 2023

Canceled.

Release note (sql change): Previously, `ADD PRIMARY KEY NOT VALID` is
processed by ignoring the NOT VALID qualifier. This is inadequate
because PG throws an error instead. This PR ensures CRDB throws the same
error, "PRIMARY KEY constraints cannot be marked NOT VALID", for such
statements.
@Xiang-Gu Xiang-Gu force-pushed the support-alter-table-add-primary-key-not-valid branch from bdc8bfb to 5bc319b Compare February 28, 2023 15:26
@Xiang-Gu
Copy link
Copy Markdown
Contributor Author

bors r+

@craig
Copy link
Copy Markdown
Contributor

craig bot commented Feb 28, 2023

Build succeeded:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

schemachanger: Support ADD PRIMARY KEY NOT VALID

3 participants