Skip to content

Commit b87852a

Browse files
craig[bot]chrisseto
andcommitted
108047: sql: emit NOTICEs when implicitly dropping indexes r=chrisseto a=chrisseto **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. **sql: warn users that DROP COLUMN may drop indexes** Previously, `ALTER TABLE ... DROP COLUMN ...` statements would warn users that all data held by that column would be destroyed when `sql_safe_updates = on`. Unlike the [documentation for `DROP COLUMN`](https://www.cockroachlabs.com/docs/stable/alter-table#drop-column), this message does not indicate that indexes may be dropped. This commit extends the warning to also inform users that any indexes referencing that column will also be dropped to match the warnings in our documentation. Epic: none Informs: cockroachdb#106907 Release note: (sql change): Attempting to drop a column when `sql_safe_updates = on` now additionally warns users that indexes referencing that column will be automatically dropped. Co-authored-by: Chris Seto <[email protected]>
2 parents 1382b26 + 5ec47b2 commit b87852a

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

pkg/sql/alter_table.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ func (n *alterTableNode) startExec(params runParams) error {
488488
case *tree.AlterTableDropColumn:
489489
if params.SessionData().SafeUpdates {
490490
err := pgerror.DangerousStatementf("ALTER TABLE DROP COLUMN will " +
491-
"remove all data in that column")
491+
"remove all data in that column and drop any indexes that reference that column")
492492
if !params.extendedEvalCtx.TxnIsSingleStmt {
493493
err = errors.WithIssueLink(err, errors.IssueLink{
494494
IssueURL: "https://github.com/cockroachdb/cockroach/issues/46541",
@@ -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: 7 additions & 1 deletion
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"
@@ -53,7 +54,7 @@ func checkSafeUpdatesForDropColumn(b BuildCtx) {
5354
return
5455
}
5556
err := pgerror.DangerousStatementf("ALTER TABLE DROP COLUMN will " +
56-
"remove all data in that column")
57+
"remove all data in that column and drop any indexes that reference that column")
5758
if !b.EvalCtx().TxnIsSingleStmt {
5859
err = errors.WithIssueLink(err, errors.IssueLink{
5960
IssueURL: "https://github.com/cockroachdb/cockroach/issues/46541",
@@ -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)