Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sql: ADD CONSTRAINT...NOT VALID can be specified for unique constraints even though it is ignored. #59706

Closed
vy-ton opened this issue Feb 2, 2021 · 2 comments
Assignees
Labels
A-sql-pgcompat Semantic compatibility with PostgreSQL C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. E-easy Easy issue to tackle, requires little or no CockroachDB experience E-starter Might be suitable for a starter project for new employees or team members. T-sql-foundations SQL Foundations Team (formerly SQL Schema + SQL Sessions)

Comments

@vy-ton
Copy link
Contributor

vy-ton commented Feb 2, 2021

Actual behavior

In CRDB,

[email protected]:26257/defaultdb> select * from vy;
  i
-----
  1
  1
(2 rows)
Time: 1ms total (execution 1ms / network 0ms)
[email protected]:26257/defaultdb> alter table vy add constraint c unique (i) not valid;
ERROR: duplicate key value violates unique constraint "c"
SQLSTATE: 23505
DETAIL: Key (i)=(1) already exists.
CONSTRAINT: c

Specifying NOT VALID should not enforce the constraint for existing rows until VALIDATE CONSTRAINT is called. Here, the NOT VALID appears to be ignored

Expected behavior
PG only allows specifying NOT VALID for check and foreign key constraints. CRDB should either error when NOT VALID is used with a unique constraint or defer enforcing the constraint for existing rows.

postgres=# alter table vy add constraint c unique (i) not valid;
ERROR:  UNIQUE constraints cannot be marked NOT VALID

Jira issue: CRDB-3249

@vy-ton vy-ton added the C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. label Feb 2, 2021
@blathers-crl blathers-crl bot added the T-sql-foundations SQL Foundations Team (formerly SQL Schema + SQL Sessions) label Jul 18, 2023
@rafiss rafiss added E-easy Easy issue to tackle, requires little or no CockroachDB experience E-starter Might be suitable for a starter project for new employees or team members. A-sql-pgcompat Semantic compatibility with PostgreSQL labels May 7, 2024
@spilchen
Copy link
Contributor

This one looks like is has been addressed already in PR #97746 (#96729). I manually verified the behaviour matches postgres now:

root@localhost:26257/defaultdb> create table vy (c1 int);
CREATE TABLE

Time: 6ms total (execution 6ms / network 0ms)

root@localhost:26257/defaultdb> insert into vy values (1), (1);
INSERT 0 2

Time: 8ms total (execution 8ms / network 0ms)

root@localhost:26257/defaultdb> alter table vy add constraint c unique (i) not valid;
ERROR: UNIQUE constraints cannot be marked NOT VALID
SQLSTATE: 0A000

@rafiss
Copy link
Collaborator

rafiss commented May 28, 2024

Nice find! In that case I'll close this out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-sql-pgcompat Semantic compatibility with PostgreSQL C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. E-easy Easy issue to tackle, requires little or no CockroachDB experience E-starter Might be suitable for a starter project for new employees or team members. T-sql-foundations SQL Foundations Team (formerly SQL Schema + SQL Sessions)
Projects
No open projects
Status: Done
Development

No branches or pull requests

3 participants