Skip to content

Commit e9cebce

Browse files
committed
opt: do not add unnecessary columns to routine deps
Previously, computed columns would be added to the depended-on columns list for a routine that performs an INSERT. This is unnecessary, since values for computed columns are automatically generated when the routine is invoked depending on the table schema at the time of invocation. Because of this behavior, a routine with an INSERT statement previously prevented dropping computed columns from the target table, including the implicit column used for hash-sharded indexes. This commit skips computed columns when adding insert target columns to the routine's dependency list. Note that there was already logic to do this for columns with default values. This commit also adds a session variable `use_improved_routine_dependency_tracking` to control the new behavior, default off for backports. The follow-up will flip the variable on for master. Fixes #145098 Release note (sql change): Fixed a bug that caused a routine with an INSERT statement to unnecessarily block dropping a hash-sharded index or computed column on the target table. Note that the fix applies only to newly-created routines. In releases prior to v25.3, the fix will be off by default, and can be enabled by setting the session variable `use_improved_routine_dependency_tracking`.
1 parent 80173b2 commit e9cebce

File tree

6 files changed

+228
-11
lines changed

6 files changed

+228
-11
lines changed

pkg/sql/logictest/testdata/logic_test/drop_index

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -563,29 +563,26 @@ CREATE TABLE tab_145100 (
563563
)
564564

565565
statement ok
566-
CREATE PROCEDURE proc_insert_145100(in_id UUID, in_i INT) LANGUAGE SQL AS $$
567-
INSERT INTO tab_145100 (id, i) VALUES (in_id, in_i);
566+
CREATE PROCEDURE proc_select_145100() LANGUAGE SQL AS $$
567+
SELECT *, crdb_internal_i_shard_16 FROM tab_145100;
568568
$$;
569569

570-
# Note: Due to https://github.com/cockroachdb/cockroach/issues/145098, the
571-
# procedure has a dependency on the shard column. When that issue is resolved,
572-
# this DROP statement should succeed.
573-
statement error cannot drop column "crdb_internal_i_shard_16" because function "proc_insert_145100" depends on it
570+
statement error cannot drop column "crdb_internal_i_shard_16" because function "proc_select_145100" depends on it
574571
DROP INDEX tab_145100@tab_145100_i_idx
575572

576573
statement ok
577-
DROP PROCEDURE proc_insert_145100
574+
DROP PROCEDURE proc_select_145100
578575

579576
statement ok
580-
CREATE PROCEDURE proc_select_145100() LANGUAGE SQL AS $$
581-
SELECT *, crdb_internal_i_shard_16 FROM tab_145100;
577+
CREATE PROCEDURE proc_insert_145100(in_id UUID, in_i INT) LANGUAGE SQL AS $$
578+
INSERT INTO tab_145100 (id, i) VALUES (in_id, in_i);
582579
$$;
583580

584-
statement error cannot drop column "crdb_internal_i_shard_16" because function "proc_select_145100" depends on it
581+
statement ok
585582
DROP INDEX tab_145100@tab_145100_i_idx
586583

587584
statement ok
588-
DROP INDEX tab_145100@tab_145100_i_idx CASCADE
585+
DROP PROCEDURE proc_insert_145100
589586

590587
skipif config local-schema-locked
591588
query T

pkg/sql/logictest/testdata/logic_test/udf_delete

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,3 +324,40 @@ statement ok
324324
SELECT f146414()
325325

326326
subtest end
327+
328+
# Make sure that the routine does not add unnecessary columns as dependencies.
329+
subtest drop_column
330+
331+
statement ok
332+
CREATE TABLE table_drop (
333+
a INT NOT NULL,
334+
b INT NOT NULL,
335+
c INT NOT NULL,
336+
d INT AS (a + b) STORED,
337+
-- Hash-sharded indexes generate a hidden computed column.
338+
INDEX i (b ASC) USING HASH
339+
);
340+
INSERT INTO table_drop VALUES (1,2,3), (4,5,6), (7,8,9);
341+
342+
statement ok
343+
CREATE FUNCTION f_delete() RETURNS INT LANGUAGE SQL AS $$
344+
DELETE FROM table_drop WHERE a = b - 1;
345+
SELECT 1;
346+
$$;
347+
348+
statement ok
349+
DROP INDEX i;
350+
351+
statement ok
352+
ALTER TABLE table_drop DROP COLUMN d;
353+
354+
statement ok
355+
ALTER TABLE table_drop DROP COLUMN c;
356+
357+
statement error pgcode 2BP01 pq: cannot drop column "b" because function "f_delete" depends on it
358+
ALTER TABLE table_drop DROP COLUMN b;
359+
360+
statement error pgcode 2BP01 pq: cannot drop column "a" because function "f_delete" depends on it
361+
ALTER TABLE table_drop DROP COLUMN a;
362+
363+
subtest end

pkg/sql/logictest/testdata/logic_test/udf_insert

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,3 +286,105 @@ statement ok
286286
SELECT f146414()
287287

288288
subtest end
289+
290+
subtest regression_145098
291+
292+
statement ok
293+
CREATE TABLE t_computed (
294+
a INT NOT NULL,
295+
b INT AS (a + 1) STORED,
296+
c INT AS (a * 2) VIRTUAL,
297+
INDEX i (a ASC) USING HASH
298+
)
299+
300+
statement ok
301+
CREATE FUNCTION f145098() RETURNS INT LANGUAGE SQL AS $$
302+
INSERT INTO t_computed VALUES (100);
303+
SELECT 1;
304+
$$;
305+
306+
statement ok
307+
SELECT f145098();
308+
309+
query III
310+
SELECT * FROM t_computed;
311+
----
312+
100 101 200
313+
314+
# The internal computed column used by the hash-sharded index should not be
315+
# included in the routine's dependencies, and the drop should succeed.
316+
statement ok
317+
DROP INDEX i;
318+
319+
statement ok
320+
SELECT f145098();
321+
322+
query III
323+
SELECT * FROM t_computed;
324+
----
325+
100 101 200
326+
100 101 200
327+
328+
statement ok
329+
ALTER TABLE t_computed DROP COLUMN c;
330+
331+
statement ok
332+
SELECT f145098();
333+
334+
query II
335+
SELECT * FROM t_computed;
336+
----
337+
100 101
338+
100 101
339+
100 101
340+
341+
statement ok
342+
ALTER TABLE t_computed DROP COLUMN b;
343+
344+
statement ok
345+
SELECT f145098();
346+
347+
query I
348+
SELECT * FROM t_computed;
349+
----
350+
100
351+
100
352+
100
353+
100
354+
355+
statement error pgcode 2BP01 pq: cannot drop column "a" because function "f145098" depends on it
356+
ALTER TABLE t_computed DROP COLUMN a;
357+
358+
statement ok
359+
DROP FUNCTION f145098;
360+
361+
statement ok
362+
DROP TABLE t_computed;
363+
364+
# Case where the INSERT statement has a RETURNING clause that references the
365+
# hash-sharded index column. In this case, a dependency *should* be added.
366+
statement ok
367+
CREATE TABLE t_hash_sharded (
368+
a INT NOT NULL,
369+
INDEX i (a ASC) USING HASH
370+
)
371+
372+
statement ok
373+
CREATE FUNCTION f145098() RETURNS INT LANGUAGE SQL AS $$
374+
INSERT INTO t_hash_sharded VALUES (100) RETURNING crdb_internal_a_shard_16;
375+
SELECT 1;
376+
$$;
377+
378+
statement error pgcode 2BP01 pq: cannot drop column "crdb_internal_a_shard_16" because function "f145098" depends on it
379+
DROP INDEX i;
380+
381+
statement ok
382+
SELECT f145098();
383+
384+
statement ok
385+
DROP FUNCTION f145098;
386+
387+
statement ok
388+
DROP TABLE t_hash_sharded;
389+
390+
subtest end

pkg/sql/logictest/testdata/logic_test/udf_update

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,3 +348,41 @@ statement ok
348348
SELECT f146414()
349349

350350
subtest end
351+
352+
# Make sure that the routine does not add unnecessary columns as dependencies.
353+
subtest drop_column
354+
355+
statement ok
356+
CREATE TABLE table_drop (
357+
a INT NOT NULL,
358+
b INT NOT NULL,
359+
c INT NOT NULL,
360+
d INT AS (a + b) STORED,
361+
-- Hash-sharded indexes generate a hidden computed column.
362+
INDEX i (b ASC) USING HASH
363+
);
364+
INSERT INTO table_drop VALUES (1,2,3), (4,5,6), (7,8,9);
365+
366+
statement ok
367+
DROP FUNCTION f_update;
368+
CREATE FUNCTION f_update() RETURNS INT LANGUAGE SQL AS $$
369+
UPDATE table_drop SET b = b + 1 WHERE a = 1;
370+
SELECT 1;
371+
$$;
372+
373+
statement ok
374+
DROP INDEX i;
375+
376+
statement ok
377+
ALTER TABLE table_drop DROP COLUMN d;
378+
379+
statement ok
380+
ALTER TABLE table_drop DROP COLUMN c;
381+
382+
statement error pgcode 2BP01 pq: cannot drop column "b" because function "f_update" depends on it
383+
ALTER TABLE table_drop DROP COLUMN b;
384+
385+
statement error pgcode 2BP01 pq: cannot drop column "a" because function "f_update" depends on it
386+
ALTER TABLE table_drop DROP COLUMN a;
387+
388+
subtest end

pkg/sql/logictest/testdata/logic_test/udf_upsert

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,3 +260,41 @@ statement ok
260260
SELECT f146414()
261261

262262
subtest end
263+
264+
# Make sure that the routine does not add unnecessary columns as dependencies.
265+
subtest drop_column
266+
267+
statement ok
268+
CREATE TABLE table_drop (
269+
a INT NOT NULL,
270+
b INT NOT NULL,
271+
c INT NOT NULL,
272+
d INT AS (a + b) STORED,
273+
-- Hash-sharded indexes generate a hidden computed column.
274+
INDEX i (b ASC) USING HASH
275+
);
276+
INSERT INTO table_drop VALUES (1,2,3), (4,5,6), (7,8,9);
277+
278+
statement ok
279+
DROP FUNCTION f_upsert;
280+
CREATE FUNCTION f_upsert() RETURNS INT LANGUAGE SQL AS $$
281+
UPSERT INTO table_drop (a, b) VALUES (100, 200);
282+
SELECT 1;
283+
$$;
284+
285+
statement ok
286+
DROP INDEX i;
287+
288+
statement ok
289+
ALTER TABLE table_drop DROP COLUMN d;
290+
291+
statement ok
292+
ALTER TABLE table_drop DROP COLUMN c;
293+
294+
statement error pgcode 2BP01 pq: cannot drop column "b" because function "f_upsert" depends on it
295+
ALTER TABLE table_drop DROP COLUMN b;
296+
297+
statement error pgcode 2BP01 pq: cannot drop column "a" because function "f_upsert" depends on it
298+
ALTER TABLE table_drop DROP COLUMN a;
299+
300+
subtest end

pkg/sql/opt/optbuilder/mutation_builder.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -848,6 +848,11 @@ func (mb *mutationBuilder) addSynthesizedComputedCols(colIDs opt.OptionalColList
848848
// Remember id of newly synthesized column.
849849
colIDs[i] = newCol
850850

851+
// Track columns that were not explicitly set in the insert statement.
852+
if mb.b.trackSchemaDeps {
853+
mb.implicitInsertCols.Add(newCol)
854+
}
855+
851856
// Add corresponding target column.
852857
mb.targetColList = append(mb.targetColList, tabColID)
853858
mb.targetColSet.Add(tabColID)

0 commit comments

Comments
 (0)