Skip to content

Commit fe638dc

Browse files
committed
sql/schemachanger: implement TRUNCATE in declarative schema changer
Previously, we only supported truncate in the legacy schema changer, which meant that automatic toggling o schema_locked was not supported. This meant certain ORM tests were failing attempting to truncate tables with automatic schema locked enabled. This patch adds support for TRUNCATE in the declarative schema changer in majority of cases, as long as the index does not have references via index hints. Fixes: #153241 Fixes: #153248 Release note: None
1 parent eb447f5 commit fe638dc

File tree

33 files changed

+1161
-157
lines changed

33 files changed

+1161
-157
lines changed

pkg/bench/rttanalysis/testdata/benchmark_expectations

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,12 @@ exp,benchmark
120120
1,SystemDatabaseQueries/select_system.users_with_schema_Name
121121
1,SystemDatabaseQueries/select_system.users_without_schema_Name
122122
1,TriggerResolution/insert_into_table_with_trigger
123-
15,Truncate/truncate_1_column_0_rows
124-
15,Truncate/truncate_1_column_1_row
125-
15,Truncate/truncate_1_column_2_rows
126-
15,Truncate/truncate_2_column_0_rows
127-
15,Truncate/truncate_2_column_1_rows
128-
15,Truncate/truncate_2_column_2_rows
123+
18,Truncate/truncate_1_column_0_rows
124+
17,Truncate/truncate_1_column_1_row
125+
17,Truncate/truncate_1_column_2_rows
126+
18,Truncate/truncate_2_column_0_rows
127+
17,Truncate/truncate_2_column_1_rows
128+
17,Truncate/truncate_2_column_2_rows
129129
0,UDFResolution/select_from_udf
130130
2,UseManyRoles/use_2_roles
131131
2,UseManyRoles/use_50_roles

pkg/ccl/logictestccl/testdata/logic_test/partitioning

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1563,6 +1563,9 @@ PARTITION y OF INDEX test.public.bar@bar_pkey ALTER PARTITION y OF INDEX test.p
15631563
constraints = '[]',
15641564
lease_preferences = '[]'
15651565

1566+
# The declarative schema changer will correctly maintain the
1567+
# old zone configs until the index is GCed. The legacy one instantly
1568+
# removes the zone configs, which can lead to unexpected behavior.
15661569
query T rowsort
15671570
WITH subzone_spans AS (
15681571
SELECT json_array_elements(crdb_internal.pb_to_json('cockroach.config.zonepb.ZoneConfig', config) -> 'subzoneSpans') ->> 'key' AS key
@@ -1572,6 +1575,8 @@ WITH subzone_spans AS (
15721575
SELECT crdb_internal.pretty_key(decode(key, 'base64'), 0) AS pretty_key
15731576
FROM subzone_spans;
15741577
----
1578+
/1/1
1579+
/1/2
15751580
/2/1
15761581
/2/2
15771582

pkg/ccl/schemachangerccl/sctestbackupccl/backup_base_generated_test.go

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

pkg/server/application_api/metrics_test.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ func TestStoreProcedureCallStatementMetrics(t *testing.T) {
338338
)
339339

340340
_, err := db.Exec(`
341-
CREATE TABLE tbl (id SERIAL PRIMARY KEY, t text UNIQUE) WITH (schema_locked=false);
341+
CREATE TABLE tbl (id SERIAL PRIMARY KEY, t text UNIQUE);
342342
INSERT INTO tbl (t) VALUES ('d');`)
343343
require.NoError(t, err)
344344

@@ -376,11 +376,9 @@ func TestStoreProcedureCallStatementMetrics(t *testing.T) {
376376

377377
// This is to show that the metrics is globally aggregated across all calls of
378378
// store procedures.
379-
_, err = db.Exec(`
380-
TRUNCATE tbl;
381-
CALL inserttbl();
382-
`)
383-
379+
_, err = db.Exec(`TRUNCATE tbl;`)
380+
require.NoError(t, err)
381+
_, err = db.Exec(`CALL inserttbl();`)
384382
require.NoError(t, err)
385383

386384
expectedStartedInsertCount += 5
@@ -569,10 +567,9 @@ func TestUDFStatementMetrics(t *testing.T) {
569567

570568
// This is to show that the metrics is globally aggregated across all calls of
571569
// store procedures.
572-
_, err = db.Exec(`
573-
TRUNCATE tbl;
574-
SELECT inserttbl();
575-
`)
570+
_, err = db.Exec(`TRUNCATE tbl;`)
571+
require.NoError(t, err)
572+
_, err = db.Exec(`SELECT inserttbl();`)
576573
require.NoError(t, err)
577574

578575
expectedStartedInsertCount += 5

pkg/sql/index_split_scatter.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -196,12 +196,19 @@ func (is *indexSplitAndScatter) getSplitPointsWithStats(
196196

197197
// MaybeSplitIndexSpans implements the scexec.IndexSpanSplitter interface.
198198
func (is *indexSplitAndScatter) MaybeSplitIndexSpans(
199-
ctx context.Context, table catalog.TableDescriptor, indexToBackfill catalog.Index,
199+
ctx context.Context,
200+
table catalog.TableDescriptor,
201+
indexToBackfill catalog.Index,
202+
copyIndexSource catalog.Index,
200203
) error {
201-
// We will always pre-split index spans if there is partitioning.
202-
err := is.MaybeSplitIndexSpansForPartitioning(ctx, table, indexToBackfill)
203-
if err != nil {
204-
return err
204+
// If we are asked to copy a source indexes splits, then there is
205+
// no need split along partitioning.
206+
if copyIndexSource == nil {
207+
// We will always pre-split index spans if there is partitioning.
208+
err := is.MaybeSplitIndexSpansForPartitioning(ctx, table, indexToBackfill)
209+
if err != nil {
210+
return err
211+
}
205212
}
206213

207214
const backfillSplitExpiration = time.Hour
@@ -210,7 +217,11 @@ func (is *indexSplitAndScatter) MaybeSplitIndexSpans(
210217
nNodes := is.nodeDescs.GetNodeDescriptorCount()
211218
nSplits := preservedSplitsMultiple * nNodes
212219
var copySplitsFromIndexID descpb.IndexID
213-
for _, idx := range table.ActiveIndexes() {
220+
indexesToConsider := table.ActiveIndexes()
221+
if copyIndexSource != nil {
222+
indexesToConsider = []catalog.Index{copyIndexSource}
223+
}
224+
for _, idx := range indexesToConsider {
214225
if idx.GetID() != indexToBackfill.GetID() &&
215226
idx.CollectKeyColumnIDs().Equals(indexToBackfill.CollectKeyColumnIDs()) {
216227
copySplitsFromIndexID = idx.GetID()

pkg/sql/logictest/testdata/logic_test/event_log

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,15 +245,16 @@ ORDER BY "timestamp", info
245245
# Truncate a table
246246
##################
247247

248-
248+
onlyif config local-mixed-25.3
249249
statement ok
250250
ALTER TABLE a SET (schema_locked=false)
251251

252252
statement ok
253253
TRUNCATE TABLE a
254254

255+
onlyif config local-mixed-25.3
255256
statement ok
256-
ALTER TABLE a RESET (schema_locked)
257+
ALTER TABLE a SET (schema_locked=true)
257258

258259
query IT
259260
SELECT "reportingID", info::JSONB - 'Timestamp' - 'DescriptorID' - 'TxnReadTimestamp'

pkg/sql/logictest/testdata/logic_test/fk

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -753,9 +753,23 @@ orders orders_pkey PRIMARY KEY PRIMARY KEY (id ASC, shipment
753753
orders orders_product_fkey FOREIGN KEY FOREIGN KEY (product) REFERENCES products(sku) ON DELETE RESTRICT ON UPDATE RESTRICT true
754754
orders valid_customer FOREIGN KEY FOREIGN KEY (customer) REFERENCES customers(id) true
755755

756+
skipif config local-legacy-schema-changer
757+
skipif config local-mixed-25.2
758+
skipif config local-mixed-25.3
759+
statement error pq: "products_upc_key" is referenced by foreign key from table "delivery"
760+
DROP INDEX products@products_upc_key
761+
762+
skipif config local-legacy-schema-changer
763+
skipif config local-mixed-25.2
764+
skipif config local-mixed-25.3
765+
statement error pq: "products_upc_key" is referenced by foreign key from table "delivery"
766+
DROP INDEX products@products_upc_key RESTRICT
767+
768+
onlyif config local-legacy-schema-changer
756769
statement error pq: index "products_upc_key" is in use as unique constraint
757770
DROP INDEX products@products_upc_key
758771

772+
onlyif config local-legacy-schema-changer
759773
statement error pq: index "products_upc_key" is in use as unique constraint
760774
DROP INDEX products@products_upc_key RESTRICT
761775

pkg/sql/logictest/testdata/logic_test/truncate

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,14 @@ SELECT * FROM kview
3838
5 6
3939
7 8
4040

41+
onlyif config local-mixed-25.3
4142
statement ok
4243
ALTER TABLE kv SET (schema_locked=false)
4344

4445
statement ok
4546
TRUNCATE TABLE kv
4647

48+
onlyif config local-mixed-25.3
4749
statement ok
4850
ALTER TABLE kv RESET (schema_locked)
4951

@@ -67,12 +69,14 @@ CREATE TABLE selfref (
6769
Z INT REFERENCES selfref (y)
6870
)
6971

72+
onlyif config local-mixed-25.3
7073
statement ok
7174
ALTER TABLE selfref SET (schema_locked=false)
7275

7376
statement ok
7477
TRUNCATE table selfref
7578

79+
onlyif config local-mixed-25.3
7680
statement ok
7781
ALTER TABLE selfref RESET (schema_locked)
7882

@@ -95,12 +99,14 @@ CREATE TABLE bar (
9599
FAMILY "primary" (id, description)
96100
);
97101

102+
onlyif config local-mixed-25.3
98103
statement ok
99104
ALTER TABLE bar SET (schema_locked=false)
100105

101106
statement ok
102107
TRUNCATE bar
103108

109+
onlyif config local-mixed-25.3
104110
statement ok
105111
ALTER TABLE bar RESET (schema_locked)
106112

@@ -138,6 +144,7 @@ SELECT get_min_t0();
138144
statement ok
139145
ALTER TABLE t0 SET (schema_locked=false)
140146

147+
# Fallback to legacy schema changer due to references.
141148
statement ok
142149
TRUNCATE TABLE t0;
143150

@@ -212,12 +219,14 @@ COMMENT ON COLUMN t.x IS '''hi''); DROP TABLE t;';
212219
COMMENT ON COLUMN t.z IS 'comm"en"t2';
213220
COMMENT ON INDEX t@i2 IS 'comm''ent3';
214221

222+
onlyif config local-mixed-25.3
215223
statement ok
216224
ALTER TABLE t SET (schema_locked=false)
217225

218226
statement ok
219227
TRUNCATE t
220228

229+
onlyif config local-mixed-25.3
221230
statement ok
222231
ALTER TABLE t RESET (schema_locked)
223232

0 commit comments

Comments
 (0)