Skip to content

Commit 103d69b

Browse files
committed
schemachanger: check dependents when dropping hash-sharded index
Release note (bug fix): Fixed a bug where DROP INDEX on a hash-sharded index did not properly detect dependencies from functions and procedures on the shard column. This bug would cause the DROP INDEX statement to fail with an internal validation error. Now, the statement returns a correct error message, and using DROP INDEX ... CASCADE works as expected by dropping the dependent function/procedure.
1 parent 5374696 commit 103d69b

File tree

4 files changed

+87
-10
lines changed

4 files changed

+87
-10
lines changed

pkg/sql/drop_index.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ func (n *dropIndexNode) startExec(params runParams) error {
198198
// Only drop this shard column if it's not a physical column (as is the case for all hash-sharded index in 22.1
199199
// and after), or, CASCADE is set.
200200
if shardColDesc.IsVirtual() || n.n.DropBehavior == tree.DropCascade {
201-
ok, err := n.maybeQueueDropShardColumn(tableDesc, shardColDesc)
201+
ok, err := n.maybeQueueDropShardColumn(params, index.tn, tableDesc, shardColDesc)
202202
if err != nil {
203203
return err
204204
}
@@ -246,7 +246,7 @@ func (n *dropIndexNode) queueDropColumn(tableDesc *tabledesc.Mutable, col catalo
246246
//
247247
// Assumes that the given index is sharded.
248248
func (n *dropIndexNode) maybeQueueDropShardColumn(
249-
tableDesc *tabledesc.Mutable, shardColDesc catalog.Column,
249+
params runParams, tn *tree.TableName, tableDesc *tabledesc.Mutable, shardColDesc catalog.Column,
250250
) (bool, error) {
251251
if catalog.FindNonDropIndex(tableDesc, func(otherIdx catalog.Index) bool {
252252
colIDs := otherIdx.CollectKeyColumnIDs()
@@ -258,7 +258,7 @@ func (n *dropIndexNode) maybeQueueDropShardColumn(
258258
}) != nil {
259259
return false, nil
260260
}
261-
if err := n.dropShardColumnAndConstraint(tableDesc, shardColDesc); err != nil {
261+
if err := n.dropShardColumnAndConstraint(params, tn, tableDesc, shardColDesc); err != nil {
262262
return false, err
263263
}
264264
return true, nil
@@ -267,7 +267,7 @@ func (n *dropIndexNode) maybeQueueDropShardColumn(
267267
// dropShardColumnAndConstraint drops the given shard column and its associated check
268268
// constraint.
269269
func (n *dropIndexNode) dropShardColumnAndConstraint(
270-
tableDesc *tabledesc.Mutable, shardCol catalog.Column,
270+
params runParams, tn *tree.TableName, tableDesc *tabledesc.Mutable, shardCol catalog.Column,
271271
) error {
272272
validChecks := tableDesc.Checks[:0]
273273
for _, check := range tableDesc.CheckConstraints() {
@@ -284,8 +284,14 @@ func (n *dropIndexNode) dropShardColumnAndConstraint(
284284
if len(validChecks) != len(tableDesc.Checks) {
285285
tableDesc.Checks = validChecks
286286
}
287-
288-
n.queueDropColumn(tableDesc, shardCol)
287+
if _, err := dropColumnImpl(
288+
params, tn, tableDesc, tableDesc.GetRowLevelTTL(),
289+
&tree.AlterTableDropColumn{
290+
Column: shardCol.ColName(),
291+
DropBehavior: n.n.DropBehavior},
292+
); err != nil {
293+
return err
294+
}
289295
return nil
290296
}
291297

pkg/sql/logictest/testdata/logic_test/drop_index

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,4 +527,61 @@ CREATE INDEX roaches_value_idx ON roaches(value);
527527
statement error pq: rejected \(sql_safe_updates = true\): DROP INDEX
528528
DROP INDEX IF EXISTS roaches@roaches_value_idx;
529529

530+
statement ok
531+
SET sql_safe_updates = false;
532+
533+
subtest drop_hash_sharded_index_depended_on_by_procedure
534+
535+
statement ok
536+
CREATE TABLE tab_145100 (
537+
id UUID PRIMARY KEY,
538+
i INT NOT NULL,
539+
j int not null,
540+
INDEX (i ASC) USING HASH,
541+
FAMILY (id, i, j)
542+
)
543+
544+
statement ok
545+
CREATE PROCEDURE proc_insert_145100(in_id UUID, in_i INT) LANGUAGE SQL AS $$
546+
INSERT INTO tab_145100 (id, i) VALUES (in_id, in_i);
547+
$$;
548+
549+
# Note: Due to https://github.com/cockroachdb/cockroach/issues/145098, the
550+
# procedure has a dependency on the shard column. When that issue is resolved,
551+
# this DROP statement should succeed.
552+
statement error cannot drop column "crdb_internal_i_shard_16" because function "proc_insert_145100" depends on it
553+
DROP INDEX tab_145100@tab_145100_i_idx
554+
555+
statement ok
556+
DROP PROCEDURE proc_insert_145100
557+
558+
statement ok
559+
CREATE PROCEDURE proc_select_145100() LANGUAGE SQL AS $$
560+
SELECT *, crdb_internal_i_shard_16 FROM tab_145100;
561+
$$;
562+
563+
statement error cannot drop column "crdb_internal_i_shard_16" because function "proc_select_145100" depends on it
564+
DROP INDEX tab_145100@tab_145100_i_idx
565+
566+
statement ok
567+
DROP INDEX tab_145100@tab_145100_i_idx CASCADE
568+
569+
query T
570+
SELECT create_statement FROM [SHOW CREATE TABLE tab_145100]
571+
----
572+
CREATE TABLE public.tab_145100 (
573+
id UUID NOT NULL,
574+
i INT8 NOT NULL,
575+
j INT8 NOT NULL,
576+
CONSTRAINT tab_145100_pkey PRIMARY KEY (id ASC),
577+
FAMILY fam_0_id_i_j (id, i, j)
578+
)
579+
580+
# DROP INDEX ... CASCADE should have caused the procedure to be dropped.
581+
statement error procedure proc_select_145100 does not exist
582+
CALL proc_select_145100()
583+
584+
statement ok
585+
DROP TABLE tab_145100
586+
530587
subtest end

pkg/sql/schemachanger/scbuild/internal/scbuildstmt/alter_table_drop_column.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,11 @@ func dropColumn(
170170
if indexTargetStatus == scpb.ToAbsent {
171171
return
172172
}
173+
// If we entered this function because of a DROP INDEX statement (e.g. for
174+
// a hash-sharded index), avoid recursive calls to drop the index again.
175+
if _, isDropIndex := stmt.(*tree.DropIndex); isDropIndex {
176+
return
177+
}
173178
name := tree.TableIndexName{
174179
Table: *tn,
175180
Index: tree.UnrestrictedName(indexName.Name),
@@ -179,7 +184,7 @@ func dropColumn(
179184
indexName.Name,
180185
cn.Name,
181186
))
182-
dropSecondaryIndex(b, &name, behavior, e)
187+
dropSecondaryIndex(b, &name, behavior, e, stmt)
183188
case *scpb.View:
184189
if behavior != tree.DropCascade {
185190
_, _, ns := scpb.FindNamespace(b.QueryByID(col.TableID))

pkg/sql/schemachanger/scbuild/internal/scbuildstmt/drop_index.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func maybeDropIndex(
104104
"use CASCADE if you really want to drop it.",
105105
))
106106
}
107-
dropSecondaryIndex(b, indexName, n.DropBehavior, sie)
107+
dropSecondaryIndex(b, indexName, n.DropBehavior, sie, n)
108108
return sie
109109
}
110110

@@ -115,6 +115,7 @@ func dropSecondaryIndex(
115115
indexName *tree.TableIndexName,
116116
dropBehavior tree.DropBehavior,
117117
sie *scpb.SecondaryIndex,
118+
stmt tree.Statement,
118119
) {
119120
{
120121
next := b.WithNewSourceElementID()
@@ -141,7 +142,9 @@ func dropSecondaryIndex(
141142

142143
// If shard index, also drop the shard column and all check constraints that
143144
// uses this shard column if no other index uses the shard column.
144-
maybeDropAdditionallyForShardedIndex(next, sie, indexName.Index.String(), dropBehavior)
145+
maybeDropAdditionallyForShardedIndex(
146+
next, sie, indexName.Index.String(), stmt, dropBehavior,
147+
)
145148

146149
// If expression index, also drop the expression column if no other index is
147150
// using the expression column.
@@ -208,7 +211,7 @@ func maybeDropDependentFunctions(
208211
if forwardRef.IndexID != toBeDroppedIndex.IndexID {
209212
continue
210213
}
211-
// This view depends on the to-be-dropped index;
214+
// This function depends on the to-be-dropped index.
212215
if dropBehavior != tree.DropCascade {
213216
// Get view name for the error message
214217
_, _, fnName := scpb.FindFunctionName(b.QueryByID(e.FunctionID))
@@ -292,6 +295,7 @@ func maybeDropAdditionallyForShardedIndex(
292295
b BuildCtx,
293296
toBeDroppedIndex *scpb.SecondaryIndex,
294297
toBeDroppedIndexName string,
298+
stmt tree.Statement,
295299
dropBehavior tree.DropBehavior,
296300
) {
297301
if toBeDroppedIndex.Sharding == nil || !toBeDroppedIndex.Sharding.IsSharded {
@@ -349,6 +353,11 @@ func maybeDropAdditionallyForShardedIndex(
349353
b.Drop(e)
350354
}
351355
})
356+
tbl := b.QueryByID(toBeDroppedIndex.TableID).FilterTable().MustGetOneElement()
357+
ns := b.QueryByID(toBeDroppedIndex.TableID).FilterNamespace().MustGetOneElement()
358+
tn := tree.MakeTableNameFromPrefix(b.NamePrefix(tbl), tree.Name(ns.Name))
359+
shardCol := shardColElms.FilterColumn().MustGetOneElement()
360+
dropColumn(b, &tn, tbl, stmt, stmt, shardCol, shardColElms, dropBehavior)
352361
}
353362

354363
// dropAdditionallyForExpressionIndex attempts to drop the additional

0 commit comments

Comments
 (0)