Skip to content

Commit e6a2064

Browse files
committed
schemachanger: when taking a vector index offline, notify client
A previous patch disabled mutations on vector indexes if sql_safe_updates was enabled. This patch send the associated error message as a notice even if sql_safe_updates is disabled, so there are no surprises. Informs: #144443 Release note (sql change): CREATE VECTOR INDEX and ALTER PRIMARY KEY will now send a notice that vector indexes will be offline during the change operation when sql_safe_updates is disabled.
1 parent 7a28a7b commit e6a2064

File tree

5 files changed

+39
-21
lines changed

5 files changed

+39
-21
lines changed

pkg/sql/alter_primary_key.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ func (p *planner) AlterPrimaryKey(
5555
alterPrimaryKeyLocalitySwap *alterPrimaryKeyLocalitySwap,
5656
) error {
5757
// Check if sql_safe_updates is enabled and the table has vector indexes
58-
if p.EvalContext().SessionData().SafeUpdates {
59-
for _, idx := range tableDesc.AllIndexes() {
60-
if idx.GetType() == idxtype.VECTOR {
61-
return pgerror.DangerousStatementf("ALTER PRIMARY KEY on a table with vector indexes will disable writes to the table while the index is being rebuilt")
62-
}
58+
if len(tableDesc.VectorIndexes()) > 0 {
59+
if p.EvalContext().SessionData().SafeUpdates {
60+
return pgerror.DangerousStatementf("ALTER PRIMARY KEY on a table with vector indexes will disable writes to the table while the index is being rebuilt")
61+
} else {
62+
p.BufferClientNotice(ctx, pgnotice.Newf("ALTER PRIMARY KEY on a table with vector indexes will disable writes to the table while the index is being rebuilt"))
6363
}
6464
}
6565

pkg/sql/create_index.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,12 @@ func (p *planner) CreateIndex(ctx context.Context, n *tree.CreateIndex) (planNod
6060
}
6161

6262
// Check if sql_safe_updates is enabled and this is a vector index
63-
if n.Type == idxtype.VECTOR && p.EvalContext().SessionData().SafeUpdates {
64-
return nil, pgerror.DangerousStatementf("CREATE VECTOR INDEX will disable writes to the table while the index is being built")
63+
if n.Type == idxtype.VECTOR {
64+
if p.EvalContext().SessionData().SafeUpdates {
65+
return nil, pgerror.DangerousStatementf("CREATE VECTOR INDEX will disable writes to the table while the index is being built")
66+
} else {
67+
p.BufferClientNotice(ctx, pgnotice.Newf("CREATE VECTOR INDEX will disable writes to the table while the index is being built"))
68+
}
6569
}
6670

6771
_, tableDesc, err := p.ResolveMutableTableDescriptor(

pkg/sql/logictest/testdata/logic_test/vector_index

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,14 @@ ALTER TABLE simple ALTER PRIMARY KEY USING COLUMNS (b)
2626
statement ok
2727
SET sql_safe_updates = false
2828

29-
statement ok
29+
statement notice CREATE VECTOR INDEX will disable writes to the table while the index is being built
3030
CREATE VECTOR INDEX ON simple (vec1)
3131

32+
statement notice ALTER PRIMARY KEY on a table with vector indexes will disable writes to the table while the index is being rebuilt
33+
ALTER TABLE simple ALTER PRIMARY KEY USING COLUMNS (b)
34+
3235
# Alternate syntax.
33-
statement ok
36+
statement notice CREATE VECTOR INDEX will disable writes to the table while the index is being built
3437
CREATE INDEX ON simple USING cspann (vec1 ASC);
3538

3639
query TT
@@ -40,9 +43,10 @@ simple CREATE TABLE public.simple (
4043
a INT8 NOT NULL,
4144
b INT8 NOT NULL,
4245
vec1 VECTOR(3) NULL,
43-
CONSTRAINT simple_pkey PRIMARY KEY (a ASC),
46+
CONSTRAINT simple_pkey PRIMARY KEY (b ASC),
4447
VECTOR INDEX simple_vec1_idx (vec1),
4548
VECTOR INDEX simple_vec1_idx1 (vec1),
49+
UNIQUE INDEX simple_a_key (a ASC),
4650
VECTOR INDEX simple_vec1_idx2 (vec1),
4751
FAMILY fam_0_a_vec1_b (a, vec1, b)
4852
)
@@ -68,7 +72,7 @@ CREATE TABLE alt_syntax (
6872
FAMILY (a, vec1)
6973
)
7074

71-
statement ok
75+
statement notice CREATE VECTOR INDEX will disable writes to the table while the index is being built
7276
CREATE VECTOR INDEX another_index ON alt_syntax (vec1)
7377

7478
query TT
@@ -403,7 +407,7 @@ INSERT INTO backfill_test VALUES
403407
(2, 'jill', 20, '[4.0, 5.0, 6.0]', '[6.0, 5.0, 4.0]'),
404408
(3, 'ash', 30, '[7.0, 8.0, 9.0]', '[9.0, 8.0, 7.0]');
405409

406-
statement ok
410+
statement notice CREATE VECTOR INDEX will disable writes to the table while the index is being built
407411
CREATE VECTOR INDEX ON backfill_test (enc)
408412

409413
query ITITT
@@ -455,7 +459,7 @@ SELECT * FROM backfill_test@backfill_test_enc_idx ORDER BY enc <-> '[1.0, 2.0, 3
455459
3 ash 30 [7,8,9] [9,8,7]
456460
6 ash 60 [7.5,8,9] [9,8,7.5]
457461

458-
statement ok
462+
statement notice CREATE VECTOR INDEX will disable writes to the table while the index is being built
459463
CREATE VECTOR INDEX ON backfill_test (username, prefix_enc)
460464

461465
query ITITT

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/cockroachdb/cockroach/pkg/sql/paramparse"
2020
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode"
2121
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
22+
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgnotice"
2223
"github.com/cockroachdb/cockroach/pkg/sql/privilege"
2324
"github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scerrors"
2425
"github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scpb"
@@ -60,14 +61,19 @@ func alterPrimaryKey(
6061
b BuildCtx, tn *tree.TableName, tbl *scpb.Table, stmt tree.Statement, t alterPrimaryKeySpec,
6162
) {
6263
// Check if sql_safe_updates is enabled and the table has a vector index
63-
if b.EvalCtx().SessionData().SafeUpdates {
64-
tableElts := b.QueryByID(tbl.TableID).Filter(notFilter(absentTargetFilter))
65-
scpb.ForEachSecondaryIndex(tableElts, func(_ scpb.Status, _ scpb.TargetStatus, idx *scpb.SecondaryIndex) {
66-
if idx.Type == idxtype.VECTOR {
64+
tableElts := b.QueryByID(tbl.TableID).Filter(notFilter(absentTargetFilter))
65+
noticeSent := false
66+
scpb.ForEachSecondaryIndex(tableElts, func(_ scpb.Status, _ scpb.TargetStatus, idx *scpb.SecondaryIndex) {
67+
if idx.Type == idxtype.VECTOR {
68+
if b.EvalCtx().SessionData().SafeUpdates {
6769
panic(pgerror.DangerousStatementf("ALTER PRIMARY KEY on a table with vector indexes will disable writes to the table while the index is being rebuilt"))
70+
} else if !noticeSent {
71+
noticeSender := b.EvalCtx().ClientNoticeSender
72+
noticeSender.BufferClientNotice(b, pgnotice.Newf("ALTER PRIMARY KEY on a table with vector indexes will disable writes to the table while the index is being rebuilt"))
73+
noticeSent = true
6874
}
69-
})
70-
}
75+
}
76+
})
7177

7278
// Panic on certain forbidden `ALTER PRIMARY KEY` cases (e.g. one of
7379
// the new primary key column is a virtual column). See the comments

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,12 @@ import (
4444
// CreateIndex implements CREATE INDEX.
4545
func CreateIndex(b BuildCtx, n *tree.CreateIndex) {
4646
// Check if sql_safe_updates is enabled and this is a vector index
47-
if n.Type == idxtype.VECTOR && b.EvalCtx().SessionData().SafeUpdates {
48-
panic(pgerror.DangerousStatementf("CREATE VECTOR INDEX will disable writes to the table while the index is being built"))
47+
if n.Type == idxtype.VECTOR {
48+
if b.EvalCtx().SessionData().SafeUpdates {
49+
panic(pgerror.DangerousStatementf("CREATE VECTOR INDEX will disable writes to the table while the index is being built"))
50+
} else {
51+
b.EvalCtx().ClientNoticeSender.BufferClientNotice(b, pgnotice.Newf("CREATE VECTOR INDEX will disable writes to the table while the index is being built"))
52+
}
4953
}
5054

5155
b.IncrementSchemaChangeCreateCounter("index")

0 commit comments

Comments
 (0)