Skip to content

Commit

Permalink
sql: disallow partial unique constraints in ALTER TABLE
Browse files Browse the repository at this point in the history
Previously, a `WHERE` clause in `ALTER TABLE .. ADD CONSTRAINT .. UNIQUE`
statements was parsed successfully but ignored so that the resulting
unique constraint was not partial. This commit disallows the `WHERE`
clause. Postgres allows partial unique indexes, but not partial unique
constraints, so we will likely remove support for partial unique
constraints in `CREATE TABLE` statements in an upcoming major release.
See discussion in cockroachdb#65825.

Fixes cockroachdb#67234

Release note (bug fix/sql change): A bug was identified that created
non-partial unique constraints when a user attempted to create a partial
unique constraint in `ALTER TABLE` statements. Creating partial unique
constraints in `ALTER TABLE` is now disallowed. A partial unique index
can be created instead with `CREATE UNIQUE INDEX .. WHERE`.
  • Loading branch information
mgartner committed Aug 6, 2021
1 parent 6115e79 commit a50bbb6
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
6 changes: 6 additions & 0 deletions pkg/sql/alter_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,12 @@ func (n *alterTableNode) startExec(params runParams) error {
continue
}

if d.Predicate != nil {
return pgerror.New(pgcode.InvalidTableDefinition,
"partial unique constraints cannot be added in ALTER TABLE, "+
"use CREATE UNIQUE INDEX instead")
}

if d.PrimaryKey {
// We only support "adding" a primary key when we are using the
// default rowid primary index or if a DROP PRIMARY KEY statement
Expand Down
7 changes: 6 additions & 1 deletion pkg/sql/logictest/testdata/logic_test/alter_table
Original file line number Diff line number Diff line change
Expand Up @@ -1481,7 +1481,12 @@ ALTER TABLE unique_without_index ADD COLUMN f INT UNIQUE WITHOUT INDEX
# constraint.
statement ok
ALTER TABLE unique_without_index ADD COLUMN f INT;
ALTER TABLE unique_without_index ADD CONSTRAINT my_unique_f UNIQUE WITHOUT INDEX (f);
ALTER TABLE unique_without_index ADD CONSTRAINT my_unique_f UNIQUE WITHOUT INDEX (f)

# Partial predicates are allowed for UNIQUE WITHOUT INDEX, but not UNIQUE.
# TODO(mgartner): This is confusing, and something we should think more about
# if we ever make UNIQUE WITHOUT INDEX available by default.
statement ok
ALTER TABLE unique_without_index ADD CONSTRAINT my_partial_unique_f UNIQUE WITHOUT INDEX (f) WHERE f > 0

# The unique constraint predicate must be valid. It cannot reference
Expand Down
8 changes: 8 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/partial_index
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ CREATE TABLE t4 (a INT, UNIQUE INDEX (a) WHERE a = 0)
statement error expected index predicate expression to have type bool, but '1' has type int
CREATE TABLE error (a INT, UNIQUE INDEX (a) WHERE 1)

# Disallow partial unique constraints in ALTER TABLE.

statement ok
CREATE TABLE up (a INT)

statement error partial unique constraints cannot be added in ALTER TABLE, use CREATE UNIQUE INDEX instead
ALTER TABLE up ADD CONSTRAINT partial_unique UNIQUE (a) WHERE a > 0

# Validate CREATE INDEX predicate.

statement ok
Expand Down
3 changes: 3 additions & 0 deletions pkg/sql/parser/sql.y
Original file line number Diff line number Diff line change
Expand Up @@ -6801,6 +6801,9 @@ constraint_elem:
Expr: $3.expr(),
}
}
// TODO(mgartner): Postgres does not allow partial unique constraints. It only
// allows partial unique indexes. The opt_where_clause should be removed so that
// it is a syntax error. See discussion in #65825.
| UNIQUE opt_without_index '(' index_params ')'
opt_storing opt_interleave opt_partition_by_index opt_deferrable opt_where_clause
{
Expand Down

0 comments on commit a50bbb6

Please sign in to comment.