Skip to content

Commit be2cc31

Browse files
craig[bot]DrewKimballGuilherme1Rocha
committed
146250: sql: improve routine dependency tracking r=yuzefovich,michae2 a=DrewKimball #### sql: track routine dependencies on columns in RETURNING clause Previously, while building a routine, we did not explicitly add dependencies to columns referenced in the RETURNING clause of mutations within the body statements. This was not a problem for INSERT or UPSERT due to #145098, but could cause a routine with UPDATE or DELETE statements to be broken after a referenced column was dropped. This commit fixes the bug by tracking column references in the RETURNING clause. Fixes #146414 Release note (bug fix): Fixed a bug that allowed a column to be dropped from a table even if it was referenced in the RETURNING clause of an UPDATE or DELETE statement in a routine. 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`. #### 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`. #### sql: add session setting for routine dependency improvements This commit adds a session var `use_improved_routine_dependency_tracking` which enables the improvements in the previous two commits (do not add dependencies on unnecessary columns, and track dependencies in the RETURNING clause). The var is off by default in this commit for backports. Informs #145098 Informs #146414 Release note: None #### opt: enable improved routine dependency tracking This commit flips `use_improved_routine_dependency_tracking` to on by default. This means that newly-created routines will not add unnecessary implicit insert columns to the list of dependencies, and will correctly track dependencies in the RETURNING clause of UPDATE and DELETE statements. Informs #145098 Informs #146414 Release note: None 146430: sql: report unit for some session variables r=yuzefovich,drewkimball a=Guilherme1Rocha Previously, we never populated the `unit` column of `pg_settings` virtual table deviating from PostgreSQL. We now report the unit for time-based and memory-based settings. AFAIU the unit defines the default unit of the value which allows these settings take in integer numbers with the unit providing the necessary measurement dimension. Fixes: #30717 Release note: None Co-authored-by: Drew Kimball <[email protected]> Co-authored-by: Guilherme1Rocha <[email protected]>
3 parents 20591ca + 33505cf + 50c74c2 commit be2cc31

File tree

19 files changed

+523
-50
lines changed

19 files changed

+523
-50
lines changed

pkg/sql/exec_util.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4190,6 +4190,10 @@ func (m *sessionDataMutator) SetInitialRetryBackoffForReadCommitted(val time.Dur
41904190
m.data.InitialRetryBackoffForReadCommitted = val
41914191
}
41924192

4193+
func (m *sessionDataMutator) SetUseImprovedRoutineDependencyTracking(val bool) {
4194+
m.data.UseImprovedRoutineDependencyTracking = val
4195+
}
4196+
41934197
// Utility functions related to scrubbing sensitive information on SQL Stats.
41944198

41954199
// quantizeCounts ensures that the Count field in the

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/information_schema

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4133,6 +4133,7 @@ unbounded_parallel_scans off
41334133
unconstrained_non_covering_index_scan_enabled off
41344134
unsafe_allow_triggers_modifying_cascades off
41354135
use_cputs_on_non_unique_indexes off
4136+
use_improved_routine_dependency_tracking on
41364137
use_pre_25_2_variadic_builtins off
41374138
variable_inequality_lookup_join_enabled on
41384139
vector_search_beam_size 32

pkg/sql/logictest/testdata/logic_test/pg_catalog

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3128,6 +3128,7 @@ unconstrained_non_covering_index_scan_enabled off N
31283128
unsafe_allow_triggers_modifying_cascades off NULL NULL NULL string
31293129
use_cputs_on_non_unique_indexes off NULL NULL NULL string
31303130
use_declarative_schema_changer on NULL NULL NULL string
3131+
use_improved_routine_dependency_tracking on NULL NULL NULL string
31313132
use_pre_25_2_variadic_builtins off NULL NULL NULL string
31323133
variable_inequality_lookup_join_enabled on NULL NULL NULL string
31333134
vector_search_beam_size 32 NULL NULL NULL string
@@ -3181,7 +3182,7 @@ cost_scans_with_default_col_size off N
31813182
create_table_with_schema_locked off NULL user NULL off off
31823183
database test NULL user NULL · test
31833184
datestyle ISO, MDY NULL user NULL ISO, MDY ISO, MDY
3184-
deadlock_timeout 0 NULL user NULL 0s 0s
3185+
deadlock_timeout 0 ms user NULL 0s 0s
31853186
declare_cursor_statement_timeout_enabled on NULL user NULL on on
31863187
default_int_size 8 NULL user NULL 8 8
31873188
default_table_access_method heap NULL user NULL heap heap
@@ -3232,19 +3233,19 @@ experimental_hash_group_join_enabled off N
32323233
extra_float_digits 1 NULL user NULL 1 1
32333234
force_savepoint_restart off NULL user NULL off off
32343235
foreign_key_cascades_limit 10000 NULL user NULL 10000 10000
3235-
idle_in_transaction_session_timeout 0 NULL user NULL 0s 0s
3236-
idle_session_timeout 0 NULL user NULL 0s 0s
3237-
index_join_streamer_batch_size 8.0 MiB NULL user NULL 8.0 MiB 8.0 MiB
3236+
idle_in_transaction_session_timeout 0 ms user NULL 0s 0s
3237+
idle_session_timeout 0 ms user NULL 0s 0s
3238+
index_join_streamer_batch_size 8.0 MiB B user NULL 8.0 MiB 8.0 MiB
32383239
index_recommendations_enabled off NULL user NULL on false
32393240
initial_retry_backoff_for_read_committed 2 NULL user NULL 2ms 2ms
32403241
inject_retry_errors_enabled off NULL user NULL off off
32413242
inject_retry_errors_on_commit_enabled off NULL user NULL off off
32423243
integer_datetimes on NULL user NULL on on
32433244
intervalstyle postgres NULL user NULL postgres postgres
32443245
is_superuser on NULL user NULL on on
3245-
join_reader_index_join_strategy_batch_size 4.0 MiB NULL user NULL 4.0 MiB 4.0 MiB
3246-
join_reader_no_ordering_strategy_batch_size 2.0 MiB NULL user NULL 2.0 MiB 2.0 MiB
3247-
join_reader_ordering_strategy_batch_size 100 KiB NULL user NULL 100 KiB 100 KiB
3246+
join_reader_index_join_strategy_batch_size 4.0 MiB B user NULL 4.0 MiB 4.0 MiB
3247+
join_reader_no_ordering_strategy_batch_size 2.0 MiB B user NULL 2.0 MiB 2.0 MiB
3248+
join_reader_ordering_strategy_batch_size 100 KiB B user NULL 100 KiB 100 KiB
32483249
large_full_scan_rows 0 NULL user NULL 0 0
32493250
lc_collate C.UTF-8 NULL user NULL C.UTF-8 C.UTF-8
32503251
lc_ctype C.UTF-8 NULL user NULL C.UTF-8 C.UTF-8
@@ -3255,7 +3256,7 @@ lc_time C.UTF-8 N
32553256
legacy_varchar_typing off NULL user NULL off off
32563257
locality region=test,dc=dc1 NULL user NULL region=test,dc=dc1 region=test,dc=dc1
32573258
locality_optimized_partitioned_index_scan on NULL user NULL on on
3258-
lock_timeout 0 NULL user NULL 0s 0s
3259+
lock_timeout 0 ms user NULL 0s 0s
32593260
log_timezone UTC NULL user NULL UTC UTC
32603261
max_connections -1 NULL user NULL -1 -1
32613262
max_identifier_length 128 NULL user NULL 128 128
@@ -3307,7 +3308,7 @@ pg_trgm.similarity_threshold 0.3 N
33073308
plan_cache_mode auto NULL user NULL auto auto
33083309
plpgsql_use_strict_into off NULL user NULL off off
33093310
prefer_lookup_joins_for_fks off NULL user NULL off off
3310-
prepared_statements_cache_size 0 B NULL user NULL 0 B 0 B
3311+
prepared_statements_cache_size 0 B B user NULL 0 B 0 B
33113312
propagate_admission_header_to_leaf_transactions on NULL user NULL on on
33123313
propagate_input_ordering off NULL user NULL off off
33133314
recursion_depth_limit 1000 NULL user NULL 1000 1000
@@ -3326,7 +3327,7 @@ show_primary_key_constraint_on_not_visible_columns on N
33263327
sql_safe_updates off NULL user NULL off off
33273328
ssl on NULL user NULL on on
33283329
standard_conforming_strings on NULL user NULL on on
3329-
statement_timeout 0 NULL user NULL 0s 0s
3330+
statement_timeout 0 ms user NULL 0s 0s
33303331
streamer_always_maintain_ordering off NULL user NULL off off
33313332
streamer_enabled on NULL user NULL on on
33323333
streamer_head_of_line_only_fraction 0.8 NULL user NULL 0.8 0.8
@@ -3352,13 +3353,14 @@ transaction_rows_read_log 0 N
33523353
transaction_rows_written_err 0 NULL user NULL 0 0
33533354
transaction_rows_written_log 0 NULL user NULL 0 0
33543355
transaction_status NoTxn NULL user NULL NoTxn NoTxn
3355-
transaction_timeout 0 NULL user NULL 0s 0s
3356+
transaction_timeout 0 ms user NULL 0s 0s
33563357
troubleshooting_mode off NULL user NULL off off
33573358
unbounded_parallel_scans off NULL user NULL off off
33583359
unconstrained_non_covering_index_scan_enabled off NULL user NULL off off
33593360
unsafe_allow_triggers_modifying_cascades off NULL user NULL off off
33603361
use_cputs_on_non_unique_indexes off NULL user NULL off off
33613362
use_declarative_schema_changer on NULL user NULL on on
3363+
use_improved_routine_dependency_tracking on NULL user NULL on on
33623364
use_pre_25_2_variadic_builtins off NULL user NULL off off
33633365
variable_inequality_lookup_join_enabled on NULL user NULL on on
33643366
vector_search_beam_size 32 NULL user NULL 32 32
@@ -3582,6 +3584,7 @@ unconstrained_non_covering_index_scan_enabled NULL NULL NULL
35823584
unsafe_allow_triggers_modifying_cascades NULL NULL NULL NULL NULL
35833585
use_cputs_on_non_unique_indexes NULL NULL NULL NULL NULL
35843586
use_declarative_schema_changer NULL NULL NULL NULL NULL
3587+
use_improved_routine_dependency_tracking NULL NULL NULL NULL NULL
35853588
use_pre_25_2_variadic_builtins NULL NULL NULL NULL NULL
35863589
variable_inequality_lookup_join_enabled NULL NULL NULL NULL NULL
35873590
vector_search_beam_size NULL NULL NULL NULL NULL

pkg/sql/logictest/testdata/logic_test/set

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -940,3 +940,22 @@ subtest end
940940
# Regression test for incorrectly marking this variable as boolean.
941941
statement ok
942942
SET copy_num_retries_per_batch = 5;
943+
944+
# Tests for a byte-size setting where integer values are provided (which are
945+
# treated as number of bytes).
946+
statement ok
947+
SET distsql_workmem = 1048576
948+
949+
query T
950+
SHOW distsql_workmem
951+
----
952+
1.0 MiB
953+
954+
statement error distsql_workmem cannot be set to a negative value
955+
SET distsql_workmem = -1
956+
957+
statement error distsql_workmem can only be set to a value greater than 1
958+
SET distsql_workmem = 1
959+
960+
statement ok
961+
RESET distsql_workmem

pkg/sql/logictest/testdata/logic_test/show_source

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ unconstrained_non_covering_index_scan_enabled off
242242
unsafe_allow_triggers_modifying_cascades off
243243
use_cputs_on_non_unique_indexes off
244244
use_declarative_schema_changer on
245+
use_improved_routine_dependency_tracking on
245246
use_pre_25_2_variadic_builtins off
246247
variable_inequality_lookup_join_enabled on
247248
vector_search_beam_size 32

pkg/sql/logictest/testdata/logic_test/udf_delete

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,3 +302,62 @@ SELECT * FROM u_a;
302302
2 b 20
303303

304304
subtest end
305+
306+
subtest regression_146414
307+
308+
statement ok
309+
CREATE TABLE t146414 (
310+
a INT NOT NULL,
311+
b INT AS (a + 1) VIRTUAL
312+
)
313+
314+
statement ok
315+
CREATE FUNCTION f146414() RETURNS INT LANGUAGE SQL AS $$
316+
DELETE FROM t146414 WHERE a = 1 RETURNING b;
317+
SELECT 1;
318+
$$;
319+
320+
statement error pgcode 2BP01 pq: cannot drop column "b" because function "f146414" depends on it
321+
ALTER TABLE t146414 DROP COLUMN b;
322+
323+
statement ok
324+
SELECT f146414()
325+
326+
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

0 commit comments

Comments
 (0)