Skip to content

Commit 48e6e36

Browse files
authored
Merge pull request #160600 from cockroachdb/blathers/backport-release-26.1-160051
release-26.1: sql: add distsql_prevent_partitioning_soft_limited_scans
2 parents 48d36a5 + 03406ae commit 48e6e36

File tree

11 files changed

+210
-14
lines changed

11 files changed

+210
-14
lines changed

pkg/sql/distsql_physical_planner.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2423,14 +2423,11 @@ func (dsp *DistSQLPlanner) planTableReaders(
24232423
ignoreMisplannedRanges bool
24242424
err error
24252425
)
2426+
sd := planCtx.ExtendedEvalCtx.SessionData()
24262427
if planCtx.isLocal {
24272428
spanPartitions, parallelizeLocal = dsp.maybeParallelizeLocalScans(ctx, planCtx, info)
2428-
} else if info.post.Limit == 0 {
2429-
// No hard limit - plan all table readers where their data live. Note
2430-
// that we're ignoring soft limits for now since the TableReader will
2431-
// still read too eagerly in the soft limit case. To prevent this we'll
2432-
// need a new mechanism on the execution side to modulate table reads.
2433-
// TODO(yuzefovich): add that mechanism.
2429+
} else if info.post.Limit == 0 && (info.spec.LimitHint == 0 || !sd.DistSQLPreventPartitioningSoftLimitedScans) {
2430+
// No limits - plan all table readers where their data live.
24342431
bound := PartitionSpansBoundDefault
24352432
if info.desc.NumFamilies() > 1 {
24362433
bound = PartitionSpansBoundCFWithinRow
@@ -2440,7 +2437,7 @@ func (dsp *DistSQLPlanner) planTableReaders(
24402437
return err
24412438
}
24422439
} else {
2443-
// If the scan has a hard limit, use a single TableReader to avoid
2440+
// If the scan has a hard or soft limit, use a single TableReader to avoid
24442441
// reading more rows than necessary.
24452442
sqlInstanceID, err := dsp.getInstanceIDForScan(ctx, planCtx, info.spans, info.reverse)
24462443
if err != nil {

pkg/sql/logictest/testdata/logic_test/information_schema

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3808,6 +3808,7 @@ distribute_join_row_count_threshold 1000
38083808
distribute_scan_row_count_threshold 10000
38093809
distribute_sort_row_count_threshold 1000
38103810
distsql_plan_gateway_bias 2
3811+
distsql_prevent_partitioning_soft_limited_scans on
38113812
distsql_use_reduced_leaf_write_sets on
38123813
enable_auto_rehoming off
38133814
enable_create_stats_using_extremes on

pkg/sql/logictest/testdata/logic_test/pg_catalog

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3078,6 +3078,7 @@ distribute_scan_row_count_threshold 10000
30783078
distribute_sort_row_count_threshold 1000 NULL NULL NULL string
30793079
distsql off NULL NULL NULL string
30803080
distsql_plan_gateway_bias 2 NULL NULL NULL string
3081+
distsql_prevent_partitioning_soft_limited_scans on NULL NULL NULL string
30813082
distsql_use_reduced_leaf_write_sets on NULL NULL NULL string
30823083
enable_auto_rehoming off NULL NULL NULL string
30833084
enable_create_stats_using_extremes on NULL NULL NULL string
@@ -3328,6 +3329,7 @@ distribute_scan_row_count_threshold 10000
33283329
distribute_sort_row_count_threshold 1000 NULL user NULL 1000 1000
33293330
distsql off NULL user NULL off off
33303331
distsql_plan_gateway_bias 2 NULL user NULL 2 2
3332+
distsql_prevent_partitioning_soft_limited_scans on NULL user NULL on on
33313333
distsql_use_reduced_leaf_write_sets on NULL user NULL on on
33323334
enable_auto_rehoming off NULL user NULL off off
33333335
enable_create_stats_using_extremes on NULL user NULL on on
@@ -3565,6 +3567,7 @@ distribute_scan_row_count_threshold NULL NULL
35653567
distribute_sort_row_count_threshold NULL NULL NULL NULL NULL
35663568
distsql NULL NULL NULL NULL NULL
35673569
distsql_plan_gateway_bias NULL NULL NULL NULL NULL
3570+
distsql_prevent_partitioning_soft_limited_scans NULL NULL NULL NULL NULL
35683571
distsql_use_reduced_leaf_write_sets NULL NULL NULL NULL NULL
35693572
distsql_workmem NULL NULL NULL NULL NULL
35703573
enable_auto_rehoming NULL NULL NULL NULL NULL

pkg/sql/logictest/testdata/logic_test/show_source

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ distribute_scan_row_count_threshold 10000
9292
distribute_sort_row_count_threshold 1000
9393
distsql off
9494
distsql_plan_gateway_bias 2
95+
distsql_prevent_partitioning_soft_limited_scans on
9596
distsql_use_reduced_leaf_write_sets on
9697
enable_auto_rehoming off
9798
enable_create_stats_using_extremes on

pkg/sql/opt/exec/execbuilder/testdata/distsql_auto_mode

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ SET distsql=auto
1313
statement ok
1414
CREATE TABLE kv (k INT PRIMARY KEY, v INT);
1515
ALTER TABLE kv SPLIT AT SELECT i FROM generate_series(1,5) AS g(i);
16-
ALTER TABLE kv EXPERIMENTAL_RELOCATE SELECT ARRAY[i], i FROM generate_series(1, 5) as g(i);
16+
ALTER TABLE kv EXPERIMENTAL_RELOCATE SELECT ARRAY[(i+1)%5+1], i FROM generate_series(1, 5) as g(i);
1717

1818
# Full table scan (no stats) - distribute.
1919
query T
@@ -113,12 +113,11 @@ SELECT info FROM [EXPLAIN SELECT * FROM kv LIMIT 1] WHERE info LIKE 'distributio
113113
----
114114
distribution: local
115115

116-
# TODO(yuzefovich): we shouldn't distribute this query due to a soft limit, but
117-
# soft limits are currently ignored.
116+
# Soft limit in a large scan - don't distribute.
118117
query T
119118
SELECT info FROM [EXPLAIN SELECT * FROM kv UNION SELECT * FROM kv LIMIT 1] WHERE info LIKE 'distribution%'
120119
----
121-
distribution: full
120+
distribution: local
122121

123122
# Now consider all constrained scans through the end of the file small.
124123
statement ok
@@ -299,14 +298,14 @@ ALTER TABLE a SPLIT AT SELECT i FROM generate_series(1, 9) AS g(i)
299298

300299
retry
301300
statement ok
302-
ALTER TABLE a EXPERIMENTAL_RELOCATE SELECT ARRAY[i%5+1], i FROM generate_series(0, 9) AS g(i)
301+
ALTER TABLE a EXPERIMENTAL_RELOCATE SELECT ARRAY[(i+1)%5+1], i FROM generate_series(0, 9) AS g(i)
303302

304303
statement ok
305304
ALTER TABLE b SPLIT AT SELECT i FROM generate_series(1, 9) AS g(i)
306305

307306
retry
308307
statement ok
309-
ALTER TABLE b EXPERIMENTAL_RELOCATE SELECT ARRAY[i%5+1], i FROM generate_series(0, 9) AS g(i)
308+
ALTER TABLE b EXPERIMENTAL_RELOCATE SELECT ARRAY[(i+1)%5+1], i FROM generate_series(0, 9) AS g(i)
310309

311310
statement ok
312311
ALTER TABLE a INJECT STATISTICS '[
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
# LogicTest: 5node
2+
3+
# Regression test for lazy reading of soft-limited scans.
4+
5+
statement ok
6+
CREATE TABLE abc (a INT PRIMARY KEY, b INT, c STRING, FAMILY (a, b, c))
7+
8+
statement ok
9+
INSERT INTO abc SELECT i, i % 10, repeat('c', 16384) FROM generate_series(0, 99) AS s(i)
10+
11+
statement ok
12+
ALTER TABLE abc SPLIT AT SELECT i * 20 FROM generate_series(1, 4) AS s(i)
13+
14+
retry
15+
statement ok
16+
ALTER TABLE abc EXPERIMENTAL_RELOCATE
17+
SELECT ARRAY[i+1], i * 20 FROM generate_series(0, 4) as s(i)
18+
19+
# Verify data placement.
20+
query TTTI colnames,rowsort
21+
SELECT start_key, end_key, replicas, lease_holder from [SHOW RANGES FROM TABLE abc WITH DETAILS]
22+
ORDER BY 1
23+
----
24+
start_key end_key replicas lease_holder
25+
<before:/Table/XX> …/1/20 {1} 1
26+
…/1/20 …/1/40 {2} 2
27+
…/1/40 …/1/60 {3} 3
28+
…/1/60 …/1/80 {4} 4
29+
…/1/80 <after:/Max> {5} 5
30+
31+
statement ok
32+
ANALYZE abc
33+
34+
query T
35+
EXPLAIN ANALYZE (DISTSQL) SELECT a FROM abc WHERE a >= 0 AND a < 100 ORDER BY a LIMIT 10
36+
----
37+
planning time: 10µs
38+
execution time: 100µs
39+
distribution: <hidden>
40+
vectorized: <hidden>
41+
plan type: custom
42+
rows decoded from KV: 10 (80 B, 20 KVs, 10 gRPC calls)
43+
maximum memory usage: <hidden>
44+
DistSQL network usage: <hidden>
45+
regions: <hidden>
46+
isolation level: serializable
47+
priority: normal
48+
quality of service: regular
49+
·
50+
• scan
51+
sql nodes: <hidden>
52+
kv nodes: <hidden>
53+
regions: <hidden>
54+
actual row count: 10
55+
KV time: 0µs
56+
KV rows decoded: 10
57+
KV pairs read: 20
58+
KV bytes read: 80 B
59+
KV gRPC calls: 10
60+
estimated max memory allocated: 0 B
61+
estimated row count: 10 (10% of the table; stats collected <hidden> ago)
62+
table: abc@abc_pkey
63+
spans: [/0 - /99]
64+
limit: 10
65+
·
66+
Diagram: https://cockroachdb.github.io/distsqlplan/decode.html#eJyMktFq20wQhe__pxjmKgEFr_7elIVCG9ulpnYcZJNSWhNGq4mzWNKquyNsY_xYfYE-WVnJTtrSQPdCaM5-e-asRgcM30rUuBhPx8MlELzP5jOg3MCnD-NsDBcEX1ulXvEbUJfw7mb0pBhIlbqEeTYaZ3D9GQimk9lkCanCBGtX8A1VHFB_wRRXCTbeGQ7B-SgdOmBS7FCrBG3dtBLlVYLGeUZ9QLFSMmpcUl5yxlSwH0TjgoVs2dlSbt5Sbu6bDe8xwaEr26oOGggTXDQUXwfqapCqeG5qKyt9to93ELsHDXXal57X1kVeOEgvia1Yg_rxPZwQtw1QsHEFF_rJJ98LB_BMhYbXCq57dZ3dDsFQWYZnsiHrz-T_UZvdDYcQhBswrq0FLngnA1vLpQY1eAaYNy8BFe2g4sr5PVBZOkMSo_UpchLzyAFcK00rGiLfXeEspApXxwT78vTtg9CaUae_DGsyQq2Oyb_PK-PQuDrwb6N6qZP6o9NVelwlyMWa-58kuNYbvvXOdGxfzjujTig4SL-b9sWkPm8F8UxVH3-V4EPptve2QI3qtK7-8jgvjAdoHeLFFo9u29ku902M9UBl4ARntOERC_vK1jaINajFt3w8_vczAAD__xOuBa4=
67+
68+
statement ok
69+
SET distsql_prevent_partitioning_soft_limited_scans = off
70+
71+
query T
72+
EXPLAIN ANALYZE (DISTSQL) SELECT a FROM abc WHERE a >= 0 AND a < 100 AND b != 5 ORDER BY a LIMIT 10
73+
----
74+
planning time: 10µs
75+
execution time: 100µs
76+
distribution: <hidden>
77+
vectorized: <hidden>
78+
plan type: custom
79+
rows decoded from KV: 60 (480 B, 120 KVs, 60 gRPC calls)
80+
maximum memory usage: <hidden>
81+
DistSQL network usage: <hidden>
82+
regions: <hidden>
83+
isolation level: serializable
84+
priority: normal
85+
quality of service: regular
86+
·
87+
• limit
88+
│ count: 10
89+
90+
└── • filter
91+
│ sql nodes: <hidden>
92+
│ regions: <hidden>
93+
│ actual row count: 50
94+
│ execution time: 0µs
95+
│ estimated row count: 90
96+
│ filter: b != 5
97+
98+
└── • scan
99+
sql nodes: <hidden>
100+
kv nodes: <hidden>
101+
regions: <hidden>
102+
actual row count: 60
103+
KV time: 0µs
104+
KV rows decoded: 60
105+
KV pairs read: 120
106+
KV bytes read: 480 B
107+
KV gRPC calls: 60
108+
estimated max memory allocated: 0 B
109+
estimated row count: 12 - 100 (100% of the table; stats collected <hidden> ago)
110+
table: abc@abc_pkey
111+
spans: [/0 - /99]
112+
·
113+
Diagram: https://cockroachdb.github.io/distsqlplan/decode.html#eJzsV9tu4zYQfe9XTOcpQWWIlC-xCSyQruNFjeaycIwtijZY0NKsV4gkqiRdxw38Wf2BflkhKfJa2ti1AhQIsNaDAQ6HwzlnzpD0I5o_IhR4O7ocDacg4d3k5grkzIdffhpNRnByIuH3BWNtegPsFH68voDS4gNn7PTJNoPv30D3FG4mF6MJvP0VJFyOr8ZT4AwdTFRA1zImg-I35Oighw620cEOOtjFOwdTrXwyRunM5TFfMA4eUDAHwyRd2Mx856CvNKF4RBvaiFDgVM4impAMSLvZRgFZGUb5NnLmn8uZ_zG9pxU6OFTRIk6MAOnADB28TWU2clnL9bKVP3-AbFMjIOHFUNM8VJmPJWMLkw1jEsD--ds8uailgYB8FVAggHuFdbayZECTDAQMevC2sM4n74fgyygyXzxTGerS08u4uPowHIKxlIKvFomFE3qwbpjYUwEsB1g4EN3vcojlA8QUK70CGUXKlzZLjeVZzKT1P5MBtbDpwgrI_HMIpYF7eLd2sBg-UW6snBMKvlWj8QUKtnYOL9O7MLKkSbvdao0Ku4BzL5ePEGJ8Pe2jg5dhHNpCO_RA_sKGKqmy_99Y2E4sXg1LdyeWLxCUDkhTUM3_nP-Ad-tnAF-rlkpdXpPkTZbXOf_f8bVr-Hi1WPzwnuIv6imPtdzOsamaNxVvUqdNU_VeZVP1Kli8wzXnvUhzHdZye0fNNdec16ROG82dvUrNnVWwtA_XXPtFmuuxlts_aq655tpN6rTRXP9Vaq5fwdI5XHOdF2muz1ouZ0fRNRddp0mhNqIbvErRDZq8vidkUpUYqiDZtROr7dTi2fuWgjkV72GjFtqn91r5uW8xvMkD5YaAjC1meTEYJ-WUsZpkvPnzsB2J743kVSLx7UjdeiRvf05NkmrvDdXZHYnXI3WawpN5VTAhu1T6HiJpKfFXGymV9qUMbVVkARnSoYzCv-TXCiyX5arT5FP4Z34KsK258igoJ_tFI5bTMRkj5xWP7qHa3qaoV6eou5ei3m6yvXqk3pHsGtlndYrO9lLU3012ux6pfyS7Rna_TtFg_4nEdrPd-eqY3H_ifot0D7Lr6VOklh_DAAWyp6_1zE_5YbZAzk12R95-Vsucr-kqzW64TzIy5OCVvKcLsqTjMAmNDX0UVi9ovf7u3wAAAP__qpTOfA==
114+
115+
statement ok
116+
SET distsql_prevent_partitioning_soft_limited_scans = on
117+
118+
query T
119+
EXPLAIN ANALYZE (DISTSQL) SELECT a FROM abc WHERE a >= 0 AND a < 100 AND b != 5 ORDER BY a LIMIT 10
120+
----
121+
planning time: 10µs
122+
execution time: 100µs
123+
distribution: <hidden>
124+
vectorized: <hidden>
125+
plan type: generic, reused
126+
rows decoded from KV: 12 (96 B, 24 KVs, 12 gRPC calls)
127+
maximum memory usage: <hidden>
128+
DistSQL network usage: <hidden>
129+
regions: <hidden>
130+
isolation level: serializable
131+
priority: normal
132+
quality of service: regular
133+
·
134+
• limit
135+
│ count: 10
136+
137+
└── • filter
138+
│ sql nodes: <hidden>
139+
│ regions: <hidden>
140+
│ actual row count: 10
141+
│ execution time: 0µs
142+
│ estimated row count: 90
143+
│ filter: b != 5
144+
145+
└── • scan
146+
sql nodes: <hidden>
147+
kv nodes: <hidden>
148+
regions: <hidden>
149+
actual row count: 12
150+
KV time: 0µs
151+
KV rows decoded: 12
152+
KV pairs read: 24
153+
KV bytes read: 96 B
154+
KV gRPC calls: 12
155+
estimated max memory allocated: 0 B
156+
estimated row count: 12 - 100 (100% of the table; stats collected <hidden> ago)
157+
table: abc@abc_pkey
158+
spans: [/0 - /99]
159+
·
160+
Diagram: https://cockroachdb.github.io/distsqlplan/decode.html#eJyUUt1q20wQvf-eYr65imGDV-4P7UIgje1Q0zgOikkprQmr1cRZImnV3RGxCX6svkCfrKxkt0lo0lYXgjl79sycPXOH4WuBCs_HJ-PhHDQcp7Mp6MzAx_fjdAx7exq-NFK-oAOQPXh3OoIdYiCRsrfFMvj_AF71YJaOxikcfQINJ5PpZA6JRIGVy-lUlxRQfcYEFwJr7wyF4HyE7lrCJF-hkgJtVTcc4YVA4zyhukO2XBAqnOusoJR0Tr4fhXNibYtWVmfmUGfmsr6hNQocuqIpq6BAC8hQ4HmtY9WX-_1ExqsfLiB2DQqqpCs9La2LJKbAHcS2JAXy-7ewpbjbADkZl1OuIBl0aLZmCuBJ5wrevoajDl2mZ0MwuijCL2atrd8xBy9R4PRiOITAVINxTcWwRyvu24p7CmTrsCMQ3TxFKPUKSiqdX4MuCmc0x9FkO0Wm2VxTANdw3bCCyG8t7IBkgIuNwK7cvnlgvSRUyb2QJiNUciP-PqdjWzB58v3kYUgdruBw0O6LUmpyOn-DAmdxmsPIPrGl5W5taEWmYeuqhzn82ZV80tXgkavkX1ylFGpXBXrg6alO8lGn_WSzEEj5krqVD67xhs68My23K2etUAvkFLg7TbpiUu2OAnvS5c9Q7islzyoNnlNaCLwq3O2lzVGh3H77v_ntPowX9DLEJzq_dret7HxdR4NXuggkcKpvaERMvrSVDWwNKvYNbTb__QgAAP__DIZi7A==
161+
162+
statement ok
163+
RESET distsql_prevent_partitioning_soft_limited_scans

pkg/sql/opt/exec/execbuilder/tests/5node/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ go_test(
1212
"//build/toolchains:is_heavy": {"test.Pool": "heavy"},
1313
"//conditions:default": {"test.Pool": "large"},
1414
}),
15-
shard_count = 28,
15+
shard_count = 29,
1616
tags = ["cpu:3"],
1717
deps = [
1818
"//pkg/base",

pkg/sql/opt/exec/execbuilder/tests/5node/generated_test.go

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

pkg/sql/sessiondatapb/local_only_session_data.proto

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -770,6 +770,10 @@ message LocalOnlySessionData {
770770
// triggers or computed columns. The fix applies at routine creation time and
771771
// prevents unnecessary column dependencies from being recorded.
772772
bool use_improved_routine_deps_triggers_and_computed_cols = 196;
773+
// DistSQLPreventPartitioningSoftLimitedScans, when true, prevents the distsql
774+
// physical planner from partitioning scans with soft limits into multiple
775+
// TableReaders. When true, this matches the behavior for hard limits.
776+
bool distsql_prevent_partitioning_soft_limited_scans = 197 [(gogoproto.customname) = "DistSQLPreventPartitioningSoftLimitedScans"];
773777

774778
///////////////////////////////////////////////////////////////////////////
775779
// WARNING: consider whether a session parameter you're adding needs to //

pkg/sql/sessionmutator/mutator.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1118,3 +1118,7 @@ func (m *SessionDataMutator) SetPreventUpdateSetColumnDrop(val bool) {
11181118
func (m *SessionDataMutator) SetUseImprovedRoutineDepsTriggersAndComputedCols(val bool) {
11191119
m.Data.UseImprovedRoutineDepsTriggersAndComputedCols = val
11201120
}
1121+
1122+
func (m *SessionDataMutator) SetDistSQLPreventPartitioningSoftLimitedScans(val bool) {
1123+
m.Data.DistSQLPreventPartitioningSoftLimitedScans = val
1124+
}

0 commit comments

Comments
 (0)