Skip to content

Commit 8c55935

Browse files
craig[bot]fqazi
andcommitted
Merge #143273
143273: sql/schemachanger: set schema_locked automatically for declarative r=fqazi a=fqazi Previously, the schema_locked setting had to be manually toggled by the user before executing DDL on a table. This meant that setting / unsetting schema_locked was a manual process for users. The declarative schema changer makes it much easier to tie setting / unsetting the locked setting within the schema change path. This patch adds a new TRANSIENT_PUBLIC state that allows elements to be toggled. The builder is modified to emit this schema_locked element in a TRANSIENT_PUBLIC state. Informs: #129694 Release note: None Co-authored-by: Faizan Qazi <[email protected]>
2 parents ddb8f15 + 2f474d0 commit 8c55935

File tree

89 files changed

+7159
-110
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

89 files changed

+7159
-110
lines changed

pkg/ccl/schemachangerccl/backup_base_generated_test.go

Lines changed: 56 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/sql/catalog/internal/validate/schema_changer_state.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func validateSchemaChangerState(d catalog.Descriptor, vea catalog.ValidationErro
4545
i, got, d.GetID()))
4646
}
4747
switch t.TargetStatus {
48-
case scpb.Status_PUBLIC, scpb.Status_ABSENT, scpb.Status_TRANSIENT_ABSENT:
48+
case scpb.Status_PUBLIC, scpb.Status_ABSENT, scpb.Status_TRANSIENT_ABSENT, scpb.Status_TRANSIENT_PUBLIC:
4949
// These are valid target status values.
5050
default:
5151
report(errors.Errorf("target %d is targeting an invalid status %s",

pkg/sql/logictest/testdata/logic_test/schema_locked

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,34 +83,43 @@ CREATE TABLE t (i INT PRIMARY KEY, j INT, UNIQUE INDEX idx (j)) WITH (schema_loc
8383
statement ok
8484
INSERT INTO t SELECT i, i+1 FROM generate_series(1,10) AS tmp(i);
8585

86+
onlyif config local-mixed-24.3 local-mixed-25.1 local-legacy-schema-changer
8687
statement error pgcode 57000 schema changes are disallowed on table "t" because it is locked
8788
ALTER TABLE t ADD COLUMN k INT DEFAULT 30;
8889

90+
onlyif config local-mixed-24.3 local-mixed-25.1 local-legacy-schema-changer
8991
statement error pgcode 57000 schema changes are disallowed on table "t" because it is locked
9092
ALTER TABLE t DROP COLUMN j;
9193

94+
onlyif config local-mixed-24.3 local-mixed-25.1 local-legacy-schema-changer
9295
statement error pgcode 57000 schema changes are disallowed on table "t" because it is locked
9396
ALTER TABLE t RENAME TO t2;
9497

98+
onlyif config local-mixed-24.3 local-mixed-25.1 local-legacy-schema-changer
9599
statement error pgcode 57000 schema changes are disallowed on table "t" because it is locked
96100
ALTER TABLE t RENAME COLUMN j TO j2;
97101

102+
onlyif config local-mixed-24.3 local-mixed-25.1 local-legacy-schema-changer
98103
statement error pgcode 57000 schema changes are disallowed on table "t" because it is locked
99104
ALTER INDEX idx RENAME TO idx2;
100105

106+
onlyif config local-mixed-24.3 local-mixed-25.1 local-legacy-schema-changer
101107
statement error pgcode 57000 schema changes are disallowed on table "t" because it is locked
102108
ALTER INDEX idx INVISIBLE;
103109

110+
onlyif config local-mixed-24.3 local-mixed-25.1 local-legacy-schema-changer
104111
statement error pgcode 57000 schema changes are disallowed on table "t" because it is locked
105112
DROP INDEX idx;
106113

114+
onlyif config local-mixed-24.3 local-mixed-25.1 local-legacy-schema-changer
107115
statement error pgcode 57000 schema changes are disallowed on table "t" because it is locked
108116
CREATE INDEX idx2 ON t(j);
109117

110118
statement ok
111119
CREATE TABLE ref (a INT PRIMARY KEY, b INT)
112120

113121
# Locked tables cannot be referenced by foreign keys.
122+
onlyif config local-mixed-24.3 local-mixed-25.1 local-legacy-schema-changer
114123
statement error pgcode 57000 schema changes are disallowed on table "t" because it is locked
115124
ALTER TABLE ref ADD CONSTRAINT fk FOREIGN KEY (b) REFERENCES t(j);
116125

@@ -125,18 +134,52 @@ statement ok
125134
COMMENT ON TABLE t IS 't is a table';
126135
COMMENT ON INDEX t@idx IS 'idx is an index';
127136
COMMENT ON COLUMN t.i IS 'i is a column';
137+
subtest end
138+
139+
# Declarative schema change can automatically unset and set schema locked.
140+
subtest declarative_allows_schema_changes
128141

142+
skipif config local-mixed-24.3 local-mixed-25.1 local-legacy-schema-changer
129143
statement ok
130-
ALTER TABLE t RESET (schema_locked)
144+
ALTER TABLE t ADD COLUMN k INT DEFAULT 30;
145+
146+
skipif config local-mixed-24.3 local-mixed-25.1 local-legacy-schema-changer
147+
statement ok
148+
CREATE INDEX idx2 ON t(j);
131149

150+
skipif config local-mixed-24.3 local-mixed-25.1 local-legacy-schema-changer
151+
statement ok
152+
DROP INDEX idx2;
153+
154+
skipif config local-mixed-24.3 local-mixed-25.1 local-legacy-schema-changer
155+
statement ok
156+
ALTER TABLE ref ADD CONSTRAINT fk FOREIGN KEY (b) REFERENCES t(j);
157+
158+
skipif config local-mixed-24.3 local-mixed-25.1 local-legacy-schema-changer
159+
statement ok
160+
ALTER TABLE t DROP COLUMN j CASCADE;
161+
162+
# Confirm that the table definition still has schema_locked
163+
query I
164+
SELECT count(create_statement) FROM [SHOW CREATE TABLE t] WHERE create_statement LIKE '%schema_locked = true%'
165+
----
166+
1
167+
168+
onlyif config local-mixed-24.3 local-mixed-25.1 local-legacy-schema-changer
169+
statement ok
170+
ALTER TABLE t SET (schema_locked = false);
171+
172+
# Finally drop the table.
132173
statement ok
133174
DROP TABLE t;
175+
subtest end
134176

135177
subtest zone_config
136178

137179
statement ok
138180
ALTER TABLE ref SET (schema_locked = true);
139181

182+
onlyif config local-mixed-24.3 local-mixed-25.1 local-legacy-schema-changer
140183
statement error pgcode 57000 schema changes are disallowed on table "ref" because it is locked
141184
ALTER TABLE ref CONFIGURE ZONE USING num_replicas = 11;
142185

pkg/sql/schemachanger/scbuild/build.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ func makeNameMappings(elementStates []elementState) (ret scpb.NameMappings) {
341341
return &ret[idx], true /* isNew */
342342
}
343343
isNotDropping := func(ts scpb.TargetStatus) bool {
344-
return ts != scpb.ToAbsent && ts != scpb.Transient
344+
return ts != scpb.ToAbsent && ts != scpb.TransientAbsent
345345
}
346346
for _, es := range elementStates {
347347
switch e := es.element.(type) {
@@ -447,7 +447,11 @@ func (b buildCtx) Add(element scpb.Element) {
447447
}
448448

449449
func (b buildCtx) AddTransient(element scpb.Element) {
450-
b.Ensure(element, scpb.Transient, b.TargetMetadata())
450+
b.Ensure(element, scpb.TransientAbsent, b.TargetMetadata())
451+
}
452+
453+
func (b buildCtx) DropTransient(element scpb.Element) {
454+
b.Ensure(element, scpb.TransientPublic, b.TargetMetadata())
451455
}
452456

453457
// Drop implements the scbuildstmt.BuildCtx interface.

pkg/sql/schemachanger/scbuild/builder_state.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func (b *builderState) QueryByID(id catid.DescID) scbuildstmt.ElementResultSet {
6262
func (b *builderState) Ensure(e scpb.Element, target scpb.TargetStatus, meta scpb.TargetMetadata) {
6363
dst := b.getExistingElementState(e)
6464
switch target {
65-
case scpb.ToAbsent, scpb.ToPublic, scpb.Transient:
65+
case scpb.ToAbsent, scpb.ToPublic, scpb.TransientAbsent, scpb.TransientPublic:
6666
// Sanity check.
6767
default:
6868
panic(errors.AssertionFailedf("unsupported target %s", target.Status()))
@@ -108,7 +108,7 @@ func (b *builderState) Ensure(e scpb.Element, target scpb.TargetStatus, meta scp
108108
// of it and investigate it further if needed.
109109
if dst.current == scpb.Status_ABSENT &&
110110
dst.target == scpb.ToAbsent &&
111-
(target == scpb.ToPublic || target == scpb.Transient) &&
111+
(target == scpb.ToPublic || target == scpb.TransientAbsent) &&
112112
dst.metadata.IsLinkedToSchemaChange() {
113113
panic(scerrors.NotImplementedErrorf(
114114
nil,
@@ -156,7 +156,7 @@ func (b *builderState) Ensure(e scpb.Element, target scpb.TargetStatus, meta scp
156156
// the transient path so it can be done but we must take care to migrate
157157
// the current status to its transient equivalent if need be in order to
158158
// avoid the possibility of a future transition to PUBLIC.
159-
if target == scpb.Transient {
159+
if target == scpb.TransientAbsent {
160160
var ok bool
161161
dst.current, ok = scpb.GetTransientEquivalent(dst.current)
162162
if !ok {
@@ -167,7 +167,7 @@ func (b *builderState) Ensure(e scpb.Element, target scpb.TargetStatus, meta scp
167167
}
168168
}
169169
return
170-
case scpb.Transient:
170+
case scpb.TransientAbsent:
171171
// Here the new target is either to-absent, which effectively undoes the
172172
// incumbent target, or it's to-public. In both cases if the current
173173
// status is TRANSIENT_ we need to migrate it to its non-transient

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func AlterPolicy(b BuildCtx, n *tree.AlterPolicy) {
1717
tableElems := b.ResolveTable(n.TableName, ResolveParams{
1818
RequireOwnership: true,
1919
})
20-
panicIfSchemaChangeIsDisallowed(tableElems, n)
20+
checkTableSchemaChangePrerequisites(b, tableElems, n)
2121
tbl := tableElems.FilterTable().MustGetOneElement()
2222

2323
// Alter of a policy implies that it must exist.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ func AlterTable(b BuildCtx, n *tree.AlterTable) {
123123
panic(pgerror.Newf(pgcode.ObjectNotInPrerequisiteState,
124124
"table %q is being dropped, try again later", n.Table.Object()))
125125
}
126-
panicIfSchemaChangeIsDisallowed(elts, n)
126+
checkTableSchemaChangePrerequisites(b, elts, n)
127127
tn.ObjectNamePrefix = b.NamePrefix(tbl)
128128
b.SetUnresolvedNameAnnotation(n.Table, &tn)
129129
b.IncrementSchemaChangeAlterCounter("table")

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -497,8 +497,8 @@ func addColumn(b BuildCtx, spec addColumnSpec, n tree.NodeFormatter) (backing *s
497497
// recognize this and drop redundant primary indexes appropriately.
498498
addStoredColumnToPrimaryIndexTargeting(b, spec.tbl.TableID, inflatedChain.oldSpec.primary, spec.col, scpb.ToPublic)
499499
}
500-
addStoredColumnToPrimaryIndexTargeting(b, spec.tbl.TableID, inflatedChain.inter1Spec.primary, spec.col, scpb.Transient)
501-
addStoredColumnToPrimaryIndexTargeting(b, spec.tbl.TableID, inflatedChain.inter2Spec.primary, spec.col, scpb.Transient)
500+
addStoredColumnToPrimaryIndexTargeting(b, spec.tbl.TableID, inflatedChain.inter1Spec.primary, spec.col, scpb.TransientAbsent)
501+
addStoredColumnToPrimaryIndexTargeting(b, spec.tbl.TableID, inflatedChain.inter2Spec.primary, spec.col, scpb.TransientAbsent)
502502
addStoredColumnToPrimaryIndexTargeting(b, spec.tbl.TableID, inflatedChain.finalSpec.primary, spec.col, scpb.ToPublic)
503503
return inflatedChain.finalSpec.primary
504504
}
@@ -521,8 +521,8 @@ func addColumn(b BuildCtx, spec addColumnSpec, n tree.NodeFormatter) (backing *s
521521

522522
// addStoredColumnToPrimaryIndexTargeting adds a stored column `col` to primary
523523
// index `idx` and its associated temporary index.
524-
// The column in primary index is targeting `target` (either ToPublic or Transient),
525-
// and the column in its temporary index is always targeting Transient.
524+
// The column in primary index is targeting `target` (either ToPublic or TransientAbsent),
525+
// and the column in its temporary index is always targeting TransientAbsent.
526526
func addStoredColumnToPrimaryIndexTargeting(
527527
b BuildCtx,
528528
tableID catid.DescID,
@@ -531,7 +531,7 @@ func addStoredColumnToPrimaryIndexTargeting(
531531
target scpb.TargetStatus,
532532
) {
533533
addIndexColumnToInternal(b, tableID, idx.IndexID, col.ColumnID, scpb.IndexColumn_STORED, target)
534-
addIndexColumnToInternal(b, tableID, idx.TemporaryIndexID, col.ColumnID, scpb.IndexColumn_STORED, scpb.Transient)
534+
addIndexColumnToInternal(b, tableID, idx.TemporaryIndexID, col.ColumnID, scpb.IndexColumn_STORED, scpb.TransientAbsent)
535535
}
536536

537537
func addIndexColumnToInternal(
@@ -569,7 +569,7 @@ func addIndexColumnToInternal(
569569
switch target {
570570
case scpb.ToPublic:
571571
b.Add(&indexCol)
572-
case scpb.Transient:
572+
case scpb.TransientAbsent:
573573
b.AddTransient(&indexCol)
574574
default:
575575
panic(errors.AssertionFailedf("programming error: add index column element "+

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ func alterTableAddForeignKey(
281281
"and is no longer supported."))
282282
}
283283
// Disallow schema change if the FK references a table whose schema is locked.
284-
panicIfSchemaChangeIsDisallowed(b.QueryByID(referencedTableID), stmt)
284+
checkTableSchemaChangePrerequisites(b, b.QueryByID(referencedTableID), stmt)
285285

286286
// 6. Check that temporary tables can only reference temporary tables, or,
287287
// permanent tables can only reference permanent tables.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ func astToZoneConfigObject(b BuildCtx, n *tree.SetZoneConfig) (zoneConfigObject,
156156
}
157157
tblName := zs.TableOrIndex.Table.ToUnresolvedObjectName()
158158
elems := b.ResolvePhysicalTable(tblName, ResolveParams{})
159-
panicIfSchemaChangeIsDisallowed(elems, n)
159+
checkTableSchemaChangePrerequisites(b, elems, n)
160160
var tableID catid.DescID
161161
elems.ForEach(func(_ scpb.Status, _ scpb.TargetStatus, e scpb.Element) {
162162
switch e := e.(type) {

0 commit comments

Comments
 (0)