Skip to content

Commit 5ec47b2

Browse files
committed
sql: emit NOTICEs when implicitly dropping indexes
Previously, users would have to either know ahead of time or later discover if an index would be implicitly dropped by dropping a column that the index referenced. At best, this was mildly surprising. At worst, workloads could be dramatically impacted without much indication as to why. To ensure implicit drops do not go unnoticed, this commit updates `ALTER TABLE ... DROP COLUMN ...` to emit NOTICEs for each implicitly dropped index. Fixes: cockroachdb#106907 Release note (sql change): NOTICEs are now emitted for each index dropped by an `ALTER TABLE ... DROP COLUMN ...` statement.
1 parent c6c787e commit 5ec47b2

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

pkg/sql/alter_table.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1718,6 +1718,11 @@ func dropColumnImpl(
17181718
}
17191719

17201720
for _, idxName := range idxNamesToDelete {
1721+
params.EvalContext().ClientNoticeSender.BufferClientNotice(params.ctx, pgnotice.Newf(
1722+
"dropping index %q which depends on column %q",
1723+
idxName,
1724+
colToDrop.ColName(),
1725+
))
17211726
jobDesc := fmt.Sprintf(
17221727
"removing index %q dependent on column %q which is being dropped; full details: %s",
17231728
idxName,

pkg/sql/logictest/testdata/logic_test/alter_table

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,9 @@ ALTER TABLE t DROP COLUMN IF EXISTS q
391391
statement error cannot drop column "e" because view "v" depends on it
392392
ALTER TABLE t DROP COLUMN IF EXISTS e
393393

394-
statement ok
394+
# Negative test to assert that NOTICEs are NOT emitted if there are no indexes
395+
# being dropped.
396+
statement notice ^$
395397
ALTER TABLE t DROP COLUMN IF EXISTS e CASCADE
396398

397399
statement ok
@@ -403,12 +405,16 @@ CREATE TABLE o (gf INT REFERENCES t (g), h INT, i INT, INDEX oi (i), INDEX oh (h
403405
statement error "t_g_key" is referenced by foreign key from table "o"
404406
ALTER TABLE t DROP COLUMN g
405407

406-
statement ok
408+
statement notice NOTICE: dropping index "t_g_key" which depends on column "g"
407409
ALTER TABLE t DROP COLUMN g CASCADE
408410

409411
# Dropping columns that are indexed or stored in indexes drops those indexes
410-
# too.
411-
statement ok
412+
# too. Excuse the nasty regex, the legacy schema changer emits additional
413+
# NOTICEs about GCing. We're not testing for that, so we just "eat" them.
414+
# Example: "NOTICE: the data for dropped indexes is reclaimed
415+
# asynchronously\nHINT: The reclamation delay can be customized in the zone
416+
# configuration for the table."
417+
statement notice (NOTICE: dropping index "(oh|oih)" which depends on column "h"(\nNOTICE: .+\nHINT: .+\n)?\n?){2}
412418
ALTER TABLE o DROP COLUMN h
413419

414420
query TTBITTTBBBF colnames,rowsort

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/cockroachdb/cockroach/pkg/sql/parser"
1919
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode"
2020
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
21+
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgnotice"
2122
"github.com/cockroachdb/cockroach/pkg/sql/privilege"
2223
"github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scerrors"
2324
"github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scpb"
@@ -227,6 +228,11 @@ func dropColumn(
227228
Table: *tn,
228229
Index: tree.UnrestrictedName(indexName.Name),
229230
}
231+
b.EvalCtx().ClientNoticeSender.BufferClientNotice(b, pgnotice.Newf(
232+
"dropping index %q which depends on column %q",
233+
indexName.Name,
234+
cn.Name,
235+
))
230236
dropSecondaryIndex(b, &name, behavior, e)
231237
case *scpb.View:
232238
if behavior != tree.DropCascade {

0 commit comments

Comments
 (0)