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

Add syntax for to use "IF NOT EXISTS" modifier to "ALTER TABLE add CONSTRAINT" #53007

Closed
glennfawcett opened this issue Aug 19, 2020 · 4 comments · Fixed by #71257
Closed

Add syntax for to use "IF NOT EXISTS" modifier to "ALTER TABLE add CONSTRAINT" #53007

glennfawcett opened this issue Aug 19, 2020 · 4 comments · Fixed by #71257
Assignees
Labels
A-sql-semantics A-sql-syntax Issues strictly related to the SQL grammar, with no semantic aspect C-wishlist A wishlist feature. T-sql-foundations SQL Foundations Team (formerly SQL Schema + SQL Sessions)

Comments

@glennfawcett
Copy link

To simplify pre-check coding, it would be great to have the "IF NOT EXISTS" modifier added. Consider the following example:

root@localhost:26257/defaultdb> CREATE TABLE feedback (id uuid primary key, hash string);
CREATE TABLE

Time: 9.601966ms

root@localhost:26257/defaultdb> ALTER TABLE feedback ADD CONSTRAINT feedback_hash_key UNIQUE (hash);
ALTER TABLE

Time: 166.699451ms

root@localhost:26257/defaultdb> ALTER TABLE feedback ADD CONSTRAINT feedback_hash_key UNIQUE (hash);
ERROR: duplicate constraint name: "feedback_hash_key"
SQLSTATE: 42710

Would like to have the following return without throwing an error:

ALTER TABLE feedback ADD CONSTRAINT IF NOT EXISTS feedback_hash_key UNIQUE (hash);
@glennfawcett glennfawcett added A-sql-semantics C-wishlist A wishlist feature. A-sql-syntax Issues strictly related to the SQL grammar, with no semantic aspect labels Aug 19, 2020
@awoods187 awoods187 removed their assignment Aug 19, 2020
@awoods187
Copy link
Contributor

@rohany have you looked at this recently in the context of some of your other work?

@rohany
Copy link
Contributor

rohany commented Aug 19, 2020

I haven't seen this before in PG. I'd imagine that they don't have an ifne clause for constraints because the ifne could make you think you added a constraint of a certain type, which a constraint of a different type satisfied the ifne which is unexpected behavior IMO.

@rafiss rafiss added the T-sql-foundations SQL Foundations Team (formerly SQL Schema + SQL Sessions) label May 12, 2021
@blathers-crl blathers-crl bot added the T-sql-schema-deprecated Use T-sql-foundations instead label Sep 28, 2021
@ajwerner ajwerner added A-sql-pgcompat Semantic compatibility with PostgreSQL and removed T-sql-schema-deprecated Use T-sql-foundations instead A-sql-pgcompat Semantic compatibility with PostgreSQL labels Sep 28, 2021
@ajwerner
Copy link
Contributor

Slack convo on the topic:
Andrew Werner:

Like, say the first one succeeds:
ALTER TABLE ... ADD CONSTRAINT IF NOT EXISTS c UNIQUE (a);
This second one should no-op:
ALTER TABLE ... ADD CONSTRAINT IF NOT EXISTS c UNIQUE (a);
What about?
ALTER TABLE ... ADD CONSTRAINT IF NOT EXISTS c CHECK (a > 1);
Or:
ALTER TABLE ... ADD CONSTRAINT IF NOT EXISTS c UNIQUE (a, b);
I can see an argument for failure if the constraint isn’t exactly what exists.

Peter Mattis:

What do we do for ALTER TABLE … ADD COLUMN IF NOT EXISTS when the column type or nullability are different?

Andrew Werner:

no-op I’m pretty sure. Same for CREATE TABLE IF NOT EXISTS with a totally different table definition.

Peter Mattis:

I’d stick with that behavior for constraints as well. No reason to make them special.


Seems like a strong vote in favor of adding it.

@petermattis
Copy link
Collaborator

The lack of ADD CONSTRAINT IF NOT EXISTS has causing a bit of headache with schema migrations in CC the past week. I suspect this isn't terribly difficult to implement, but would have an outsize impact on the quality of life for some CC engineers.

postamar pushed a commit to postamar/cockroach that referenced this issue Oct 7, 2021
Previously, it was not possible to execute ALTER TABLE ... ADD
CONSTRAINT IF NOT EXISTS ... statements. This commit adds this IF NOT
EXISTS modifier to the ADD CONSTRAINT command of the ALTER TABLE
statement. When the modifier is present and the defined constraint name
already exists in the table, the statement no-ops instead of erroring.

Note that this syntax is not supported in PostgreSQL, however PostgreSQL
has transactional schema changes and PL/pgSQL which make it possible to
achieve the same thing. CockroachDB has none of that yet.

Fixes cockroachdb#53007.

Release note (sql change): added ALTER TABLE ... ADD CONSTRAINT IF NOT
EXISTS.
@postamar postamar self-assigned this Oct 7, 2021
postamar pushed a commit to postamar/cockroach that referenced this issue Oct 14, 2021
Previously, it was not possible to execute ALTER TABLE ... ADD
CONSTRAINT IF NOT EXISTS ... statements. This commit adds this IF NOT
EXISTS modifier to the ADD CONSTRAINT command of the ALTER TABLE
statement. When the modifier is present and the defined constraint name
already exists in the table, the statement no-ops instead of erroring.

Note that this syntax is not supported in PostgreSQL, however PostgreSQL
has transactional schema changes and PL/pgSQL which make it possible to
achieve the same thing. CockroachDB has none of that yet.

This commit also fixes some minor bugs or inconsistencies in the
handling of duplicate index names.

Fixes cockroachdb#53007.

Release note (sql change): added ALTER TABLE ... ADD CONSTRAINT IF NOT
EXISTS.
postamar pushed a commit to postamar/cockroach that referenced this issue Oct 26, 2021
Previously, it was not possible to execute ALTER TABLE ... ADD
CONSTRAINT IF NOT EXISTS ... statements. This commit adds this IF NOT
EXISTS modifier to the ADD CONSTRAINT command of the ALTER TABLE
statement. When the modifier is present and the defined constraint name
already exists in the table, the statement no-ops instead of erroring.

Note that this syntax is not supported in PostgreSQL, however PostgreSQL
has transactional schema changes and PL/pgSQL which make it possible to
achieve the same thing. CockroachDB has none of that yet.

This commit also fixes some minor bugs or inconsistencies in the
handling of duplicate index names.

Fixes cockroachdb#53007.

Release note (sql change): added ALTER TABLE ... ADD CONSTRAINT IF NOT
EXISTS.
craig bot pushed a commit that referenced this issue Nov 12, 2021
71257: sql: Add IF NOT EXISTS modifier to ALTER TABLE ... ADD CONSTRAINT r=postamar a=postamar

Previously, it was not possible to execute ALTER TABLE ... ADD
CONSTRAINT IF NOT EXISTS ... statements. This commit adds this IF NOT
EXISTS modifier to the ADD CONSTRAINT command of the ALTER TABLE
statement. When the modifier is present and the defined constraint name
already exists in the table, the statement no-ops instead of erroring.

Note that this syntax is not supported in PostgreSQL, however PostgreSQL
has transactional schema changes and PL/pgSQL which make it possible to
achieve the same thing. CockroachDB has none of that yet.

Fixes #53007.

Release note (sql change): added ALTER TABLE ... ADD CONSTRAINT IF NOT
EXISTS.

Co-authored-by: Marius Posta <[email protected]>
@craig craig bot closed this as completed in e89093f Nov 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-sql-semantics A-sql-syntax Issues strictly related to the SQL grammar, with no semantic aspect C-wishlist A wishlist feature. T-sql-foundations SQL Foundations Team (formerly SQL Schema + SQL Sessions)
Projects
None yet
7 participants