Skip to content

Commit f28b79b

Browse files
committed
sql: enable create_table_with_schema_locked by default
Previously, users needed to manually enable the schema_locked setting on each new table and toggle it for each schema change to achieve optimal changefeed behavior. This patch enables the create_table_with_schema_locked session variable by default, so all new tables are created with their schema locked. The SQL package is also updated to automatically disable schema_locked when transactions are used or when the legacy schema changer is employed. To support this change, tests across various packages, including backup, changefeedccl, multiregion, rttanalysis, partitionccl, importccl, acceptance, crosscluster, spanconfigccl, telemetryccl, and kvfollowereadccl, have been updated to accommodate the new default setting. Additionally, the movr workload has been adjusted to handle schema-locked tables by default when setting locality. Fixes: #129694 Release note (SQL changes): To improve changefeed performance, the session variable create_table_with_schema_locked is now enabled by default. This means all new tables are created with schema_locked. This setting must be explicitly unset for explicit transactions or for schema changes that do not support automatic disabling (e.g., ALTER TABLE ... SET LOCALITY).
1 parent 061c2ab commit f28b79b

File tree

83 files changed

+422
-129
lines changed

Some content is hidden

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

83 files changed

+422
-129
lines changed

pkg/backup/backup_test.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8777,8 +8777,15 @@ func TestBackupOnlyPublicIndexes(t *testing.T) {
87778777
// appear in the backup once it is PUBLIC.
87788778
var g errgroup.Group
87798779
g.Go(func() error {
8780+
// This test intentionally uses hooks that require the legacy schema changer. Because
8781+
// the legacy schema changer cannot toggle schema_locked for backfilling schema changes,
8782+
// we will manually do that here.
8783+
_, err := sqlDB.DB.ExecContext(ctx, `ALTER TABLE data.bank SET (schema_locked=false)`)
8784+
if err != nil {
8785+
return errors.Wrap(err, "unsetting schema_locked")
8786+
}
87808787
// We use the underlying DB since the goroutine should not call t.Fatal.
8781-
_, err := sqlDB.DB.ExecContext(ctx,
8788+
_, err = sqlDB.DB.ExecContext(ctx,
87828789
`CREATE INDEX new_balance_idx ON data.bank(balance)`)
87838790
return errors.Wrap(err, "creating index")
87848791
})
@@ -11158,7 +11165,7 @@ CREATE TABLE child_pk (k INT8 PRIMARY KEY REFERENCES parent);
1115811165
sqlDB.Exec(t, `CREATE DATABASE test`)
1115911166
sqlDB.Exec(t, fmt.Sprintf(`RESTORE TABLE test.circular FROM LATEST IN $1 AS OF SYSTEM TIME %s`, ts[0]), localFoo)
1116011167
require.Equal(t, [][]string{
11161-
{"test.public.circular", "CREATE TABLE public.circular (\n\tk INT8 NOT NULL,\n\tselfid INT8 NULL,\n\tCONSTRAINT circular_pkey PRIMARY KEY (k ASC),\n\tCONSTRAINT self_fk FOREIGN KEY (selfid) REFERENCES public.circular(selfid) NOT VALID,\n\tUNIQUE INDEX circular_selfid_key (selfid ASC)\n);"},
11168+
{"test.public.circular", "CREATE TABLE public.circular (\n\tk INT8 NOT NULL,\n\tselfid INT8 NULL,\n\tCONSTRAINT circular_pkey PRIMARY KEY (k ASC),\n\tCONSTRAINT self_fk FOREIGN KEY (selfid) REFERENCES public.circular(selfid) NOT VALID,\n\tUNIQUE INDEX circular_selfid_key (selfid ASC)\n) WITH (schema_locked = true);"},
1116211169
}, sqlDB.QueryStr(t, `SHOW CREATE TABLE test.circular`))
1116311170
sqlDB.Exec(t, fmt.Sprintf(`RESTORE TABLE test.parent, test.child FROM LATEST IN $1 AS OF SYSTEM TIME %s `, ts[0]), localFoo)
1116411171
sqlDB.Exec(t, `SELECT * FROM pg_catalog.pg_constraint`)

pkg/backup/show_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ ORDER BY object_type, object_name`, full)
229229
b INT8 NULL,
230230
CONSTRAINT tablea_pkey PRIMARY KEY (a ASC),
231231
INDEX tablea_b_idx (b ASC)
232-
)`
232+
) WITH (schema_locked = true)`
233233
expectedCreateView := "CREATE VIEW viewa (\n\ta\n) AS SELECT a FROM data.public.tablea"
234234
expectedCreateSeq := `CREATE SEQUENCE seqa MINVALUE 1 MAXVALUE 20 INCREMENT 2 START 1`
235235

@@ -263,13 +263,13 @@ ORDER BY object_type, object_name`, full)
263263
b INT8 NULL,
264264
CONSTRAINT fkreftable_pkey PRIMARY KEY (a ASC),
265265
CONSTRAINT fkreftable_b_fkey FOREIGN KEY (b) REFERENCES public.fksrc(a)
266-
)`
266+
) WITH (schema_locked = true)`
267267
wantDiffDB := `CREATE TABLE fkreftable (
268268
a INT8 NOT NULL,
269269
b INT8 NULL,
270270
CONSTRAINT fkreftable_pkey PRIMARY KEY (a ASC),
271271
CONSTRAINT fkreftable_b_fkey FOREIGN KEY (b) REFERENCES data.public.fksrc(a)
272-
)`
272+
) WITH (schema_locked = true)`
273273

274274
showBackupRows = sqlDBRestore.QueryStr(t, fmt.Sprintf(`SELECT create_statement FROM [SHOW BACKUP SCHEMAS FROM LATEST IN '%s'] WHERE object_type='table'`, includedFK))
275275
createStmtSameDB := showBackupRows[1][0]
@@ -293,7 +293,7 @@ ORDER BY object_type, object_name`, full)
293293
a INT8 NOT NULL,
294294
b INT8 NULL,
295295
CONSTRAINT fkreftable_pkey PRIMARY KEY (a ASC)
296-
)`
296+
) WITH (schema_locked = true)`
297297

298298
showBackupRows = sqlDBRestore.QueryStr(t, fmt.Sprintf(`SELECT create_statement FROM [SHOW BACKUP SCHEMAS FROM LATEST IN '%s'] WHERE object_type='table'`, missingFK))
299299
createStmt := showBackupRows[0][0]

pkg/backup/testdata/backup-restore/multiregion-mismatch-table-locality

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ defaultdb root <nil> <nil> {} <nil>
2222
postgres root <nil> <nil> {} <nil>
2323
system node <nil> <nil> {} <nil>
2424

25+
# We need to currently unset and set schema_locked when
26+
# modifying table locality, since this functionality is not
27+
# supported in the declarative schema changer.
28+
exec-sql
29+
ALTER TABLE d.t SET (schema_locked=false);
30+
----
31+
2532
# make our table regional by row
2633
exec-sql
2734
ALTER TABLE d.t SET LOCALITY REGIONAL BY ROW;

pkg/backup/testdata/backup-restore/online-restore-auto-stats

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ data.public.reg CREATE TABLE public.reg (
4444
i INT8 NOT NULL,
4545
s STRING NULL,
4646
CONSTRAINT reg_pkey PRIMARY KEY (i ASC)
47-
);
47+
) WITH (schema_locked = true);
4848

4949
query-sql
5050
SHOW CREATE TABLE data.stats
@@ -53,7 +53,7 @@ data.public.stats CREATE TABLE public.stats (
5353
i INT8 NOT NULL,
5454
s STRING NULL,
5555
CONSTRAINT stats_pkey PRIMARY KEY (i ASC)
56-
) WITH (sql_stats_automatic_collection_enabled = true);
56+
) WITH (sql_stats_automatic_collection_enabled = true, schema_locked = true);
5757

5858
query-sql
5959
SHOW CREATE TABLE data.nostats
@@ -62,4 +62,4 @@ data.public.nostats CREATE TABLE public.nostats (
6262
i INT8 NOT NULL,
6363
s STRING NULL,
6464
CONSTRAINT nostats_pkey PRIMARY KEY (i ASC)
65-
) WITH (sql_stats_automatic_collection_enabled = false);
65+
) WITH (sql_stats_automatic_collection_enabled = false, schema_locked = true);

pkg/backup/testdata/backup-restore/online-restore-cluster

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,3 @@ SELECT count(*) FROM [SHOW JOBS] WHERE description LIKE '%Background Data Downlo
3333
1
3434

3535

36-

pkg/backup/testdata/backup-restore/restore-regionless-regional-by-row

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ defaultdb root <nil> <nil> {} <nil>
2727
postgres root <nil> <nil> {} <nil>
2828
system node <nil> <nil> {} <nil>
2929

30+
# We need to currently unset and set schema_locked when
31+
# modifying table locality, since this functionality is not
32+
# supported in the declarative schema changer.
33+
exec-sql
34+
ALTER TABLE d.t SET (schema_locked=false);
35+
----
36+
3037
# make our table regional by row
3138
exec-sql
3239
ALTER TABLE d.t SET LOCALITY REGIONAL BY ROW;

pkg/backup/testdata/backup-restore/restore-schema-only

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ d.public.t1 CREATE TABLE public.t1 (
2020
x INT8 NULL,
2121
rowid INT8 NOT VISIBLE NOT NULL DEFAULT unique_rowid(),
2222
CONSTRAINT t1_pkey PRIMARY KEY (rowid ASC)
23-
);
23+
) WITH (schema_locked = true);
2424
COMMENT ON TABLE public.t1 IS 'This comment better get restored from the backed up system table!';
2525

2626
# drop and create defaultDB to ensure it has a higher ID than by default. We will check that when
@@ -104,7 +104,7 @@ d.public.t1 CREATE TABLE public.t1 (
104104
x INT8 NULL,
105105
rowid INT8 NOT VISIBLE NOT NULL DEFAULT unique_rowid(),
106106
CONSTRAINT t1_pkey PRIMARY KEY (rowid ASC)
107-
);
107+
) WITH (schema_locked = true);
108108
COMMENT ON TABLE public.t1 IS 'This comment better get restored from the backed up system table!';
109109

110110
# Ensure the defaultdb from the backed up cluster was restored.

pkg/backup/testdata/backup-restore/restore-validation-only

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ d.public.t1 CREATE TABLE public.t1 (
2626
x INT8 NULL,
2727
rowid INT8 NOT VISIBLE NOT NULL DEFAULT unique_rowid(),
2828
CONSTRAINT t1_pkey PRIMARY KEY (rowid ASC)
29-
);
29+
) WITH (schema_locked = true);
3030
COMMENT ON TABLE public.t1 IS 'This comment better get restored from the backed up system table!';
3131

3232
# drop and create defaultDB to ensure it has a higher ID than by default. We will check that when
@@ -111,7 +111,7 @@ d.public.t1 CREATE TABLE public.t1 (
111111
x INT8 NULL,
112112
rowid INT8 NOT VISIBLE NOT NULL DEFAULT unique_rowid(),
113113
CONSTRAINT t1_pkey PRIMARY KEY (rowid ASC)
114-
);
114+
) WITH (schema_locked = true);
115115
COMMENT ON TABLE public.t1 IS 'This comment better get restored from the backed up system table!';
116116

117117
# Ensure the defaultdb from the backed up cluster was restored.

pkg/backup/testdata/backup-restore/row_level_ttl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ CREATE TABLE public.t (
3939
id INT8 NOT NULL,
4040
crdb_internal_expiration TIMESTAMPTZ NOT VISIBLE NOT NULL DEFAULT current_timestamp():::TIMESTAMPTZ + '00:10:00':::INTERVAL ON UPDATE current_timestamp():::TIMESTAMPTZ + '00:10:00':::INTERVAL,
4141
CONSTRAINT t_pkey PRIMARY KEY (id ASC)
42-
) WITH (ttl = 'on', ttl_expire_after = '00:10:00':::INTERVAL);
42+
) WITH (ttl = 'on', ttl_expire_after = '00:10:00':::INTERVAL, schema_locked = true);
4343

4444
query-sql
4545
SELECT count(1) FROM [SHOW SCHEDULES]
@@ -72,7 +72,7 @@ CREATE TABLE public.t (
7272
id INT8 NOT NULL,
7373
crdb_internal_expiration TIMESTAMPTZ NOT VISIBLE NOT NULL DEFAULT current_timestamp():::TIMESTAMPTZ + '00:10:00':::INTERVAL ON UPDATE current_timestamp():::TIMESTAMPTZ + '00:10:00':::INTERVAL,
7474
CONSTRAINT t_pkey PRIMARY KEY (id ASC)
75-
) WITH (ttl = 'on', ttl_expire_after = '00:10:00':::INTERVAL);
75+
) WITH (ttl = 'on', ttl_expire_after = '00:10:00':::INTERVAL, schema_locked = true);
7676

7777
query-sql
7878
SELECT count(1) FROM [SHOW SCHEDULES]
@@ -109,7 +109,7 @@ CREATE TABLE public.t (
109109
id INT8 NOT NULL,
110110
crdb_internal_expiration TIMESTAMPTZ NOT VISIBLE NOT NULL DEFAULT current_timestamp():::TIMESTAMPTZ + '00:10:00':::INTERVAL ON UPDATE current_timestamp():::TIMESTAMPTZ + '00:10:00':::INTERVAL,
111111
CONSTRAINT t_pkey PRIMARY KEY (id ASC)
112-
) WITH (ttl = 'on', ttl_expire_after = '00:10:00':::INTERVAL);
112+
) WITH (ttl = 'on', ttl_expire_after = '00:10:00':::INTERVAL, schema_locked = true);
113113

114114
query-sql
115115
SELECT count(1) FROM [SHOW SCHEDULES]

pkg/backup/testdata/backup-restore/user-defined-functions-in-checks

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ CREATE TABLE sc1.t1 (
5757
b INT8 NULL,
5858
CONSTRAINT t1_pkey PRIMARY KEY (a ASC),
5959
CONSTRAINT check_b CHECK (sc1.f1(b) > 1:::INT8)
60-
);
60+
) WITH (schema_locked = true);
6161

6262
# Make sure that the CHECK constraint still applies correctly
6363
query-sql
@@ -142,7 +142,7 @@ CREATE TABLE sc1.t1 (
142142
b INT8 NULL,
143143
CONSTRAINT t1_pkey PRIMARY KEY (a ASC),
144144
CONSTRAINT check_b CHECK (sc1.f1(b) > 1:::INT8)
145-
);
145+
) WITH (schema_locked = true);
146146

147147
# Make sure that CHECK constraint still applies correctly
148148
query-sql
@@ -231,7 +231,7 @@ CREATE TABLE sc1.t1 (
231231
a INT8 NOT NULL,
232232
b INT8 NULL,
233233
CONSTRAINT t1_pkey PRIMARY KEY (a ASC)
234-
);
234+
) WITH (schema_locked = true);
235235

236236
exec-sql
237237
USE db1
@@ -272,4 +272,4 @@ CREATE TABLE sc1.t1 (
272272
a INT8 NOT NULL,
273273
b INT8 NULL,
274274
CONSTRAINT t1_pkey PRIMARY KEY (a ASC)
275-
);
275+
) WITH (schema_locked = true);

0 commit comments

Comments
 (0)