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: warn when creating non-partitioned index #39154

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions pkg/ccl/logictestccl/testdata/logic_test/partitioning
Original file line number Diff line number Diff line change
Expand Up @@ -833,3 +833,17 @@ scan ok12
# implicit NOT NULL contrainst of a primary key.
statement error null value in column "a" violates not-null constraint
INSERT INTO ok12 (a, b, c) VALUES (NULL, 2, 3)

statement ok
SET sql_safe_updates = true

statement error non-partitioned index on partitioned table
CREATE INDEX ON ok1 (c)

statement error non-partitioned index on partitioned table
CREATE TABLE t (a INT PRIMARY KEY, b INT, INDEX (b)) PARTITION BY LIST (a) (
PARTITION p1 VALUES IN (1)
)

statement ok
RESET sql_safe_updates
7 changes: 7 additions & 0 deletions pkg/sql/create_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ func (n *createIndexNode) startExec(params runParams) error {
}
}

// Guard against creating a non-partitioned index on a partitioned table,
// which is undesirable in most cases.
if params.SessionData().SafeUpdates && n.n.PartitionBy == nil &&
n.tableDesc.PrimaryIndex.Partitioning.NumColumns > 0 {
return pgerror.DangerousStatementf("non-partitioned index on partitioned table")
}

indexDesc, err := MakeIndexDescriptor(n.n)
if err != nil {
return err
Expand Down
12 changes: 12 additions & 0 deletions pkg/sql/create_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,18 @@ func (n *createTableNode) startExec(params runParams) error {
return err
}

// Guard against creating non-partitioned indexes on a partitioned table,
// which is undesirable in most cases.
if params.SessionData().SafeUpdates && n.n.PartitionBy != nil {
for _, def := range n.n.Defs {
if d, ok := def.(*tree.IndexTableDef); ok {
if d.PartitionBy == nil {
return pgerror.DangerousStatementf("non-partitioned index on partitioned table")
}
}
}
}

id, err := GenerateUniqueDescID(params.ctx, params.extendedEvalCtx.ExecCfg.DB)
if err != nil {
return err
Expand Down