Skip to content

Commit e654896

Browse files
craig[bot]michae2
andcommitted
Merge #160051
160051: sql: add distsql_prevent_partitioning_soft_limited_scans r=yuzefovich a=michae2 **sql: add distsql_prevent_partitioning_soft_limited_scans** Add a setting to the physical planner which prevents partitioning of soft-limited scans, to make them match hard-limited scans. This makes them lazier. Fixes: #160530 Release note (performance improvement): Add a new session variable, `distsql_prevent_partitioning_soft_limited_scans`, which, when true, prevents scans with soft limits from being planned as multiple TableReaders by the physical planner. This should decrease the initial setup costs of some fully-distributed query plans. --- **sql: enable distsql_prevent_partitioning_soft_limited_scans by default** Release note (performance improvement): enable setting `distsql_prevent_partitioning_soft_limited_scans` by default. Co-authored-by: Michael Erickson <[email protected]>
2 parents 073e04c + 7fda5a4 commit e654896

File tree

11 files changed

+181
-14
lines changed

11 files changed

+181
-14
lines changed

pkg/sql/distsql_physical_planner.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1858,14 +1858,11 @@ func (dsp *DistSQLPlanner) planTableReaders(
18581858
ignoreMisplannedRanges bool
18591859
err error
18601860
)
1861+
sd := planCtx.ExtendedEvalCtx.SessionData()
18611862
if planCtx.isLocal {
18621863
spanPartitions, parallelizeLocal = dsp.maybeParallelizeLocalScans(ctx, planCtx, info)
1863-
} else if info.post.Limit == 0 {
1864-
// No hard limit - plan all table readers where their data live. Note
1865-
// that we're ignoring soft limits for now since the TableReader will
1866-
// still read too eagerly in the soft limit case. To prevent this we'll
1867-
// need a new mechanism on the execution side to modulate table reads.
1868-
// TODO(yuzefovich): add that mechanism.
1864+
} else if info.post.Limit == 0 && (info.spec.LimitHint == 0 || !sd.DistSQLPreventPartitioningSoftLimitedScans) {
1865+
// No limits - plan all table readers where their data live.
18691866
bound := PartitionSpansBoundDefault
18701867
if info.desc.NumFamilies() > 1 {
18711868
bound = PartitionSpansBoundCFWithinRow
@@ -1875,7 +1872,7 @@ func (dsp *DistSQLPlanner) planTableReaders(
18751872
return err
18761873
}
18771874
} else {
1878-
// If the scan has a hard limit, use a single TableReader to avoid
1875+
// If the scan has a hard or soft limit, use a single TableReader to avoid
18791876
// reading more rows than necessary.
18801877
sqlInstanceID, err := dsp.getInstanceIDForScan(ctx, planCtx, info.spans, info.reverse)
18811878
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: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
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+
plan type: custom
41+
rows decoded from KV: 10 (80 B, 20 KVs, 10 gRPC calls)
42+
regions: <hidden>
43+
·
44+
• scan
45+
sql nodes: <hidden>
46+
kv nodes: <hidden>
47+
regions: <hidden>
48+
KV time: 0µs
49+
KV rows decoded: 10
50+
actual row count: 10
51+
estimated row count: 10 (10% of the table; stats collected <hidden> ago)
52+
table: abc@abc_pkey
53+
spans: [/0 - /99]
54+
limit: 10
55+
·
56+
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=
57+
58+
statement ok
59+
SET distsql_prevent_partitioning_soft_limited_scans = off
60+
61+
query T
62+
EXPLAIN ANALYZE (DISTSQL) SELECT a FROM abc WHERE a >= 0 AND a < 100 AND b != 5 ORDER BY a LIMIT 10
63+
----
64+
planning time: 10µs
65+
execution time: 100µs
66+
distribution: <hidden>
67+
plan type: custom
68+
rows decoded from KV: 60 (480 B, 120 KVs, 60 gRPC calls)
69+
DistSQL network usage: <hidden>
70+
regions: <hidden>
71+
·
72+
• limit
73+
│ count: 10
74+
75+
└── • filter
76+
│ sql nodes: <hidden>
77+
│ regions: <hidden>
78+
│ execution time: 0µs
79+
│ actual row count: 50
80+
│ estimated row count: 90
81+
│ filter: b != 5
82+
83+
└── • scan
84+
sql nodes: <hidden>
85+
kv nodes: <hidden>
86+
regions: <hidden>
87+
KV time: 0µs
88+
KV rows decoded: 60
89+
actual row count: 60
90+
estimated row count: 12 - 100 (100% of the table; stats collected <hidden> ago)
91+
table: abc@abc_pkey
92+
spans: [/0 - /99]
93+
·
94+
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==
95+
96+
statement ok
97+
SET distsql_prevent_partitioning_soft_limited_scans = on
98+
99+
query T
100+
EXPLAIN ANALYZE (DISTSQL) SELECT a FROM abc WHERE a >= 0 AND a < 100 AND b != 5 ORDER BY a LIMIT 10
101+
----
102+
planning time: 10µs
103+
execution time: 100µs
104+
distribution: <hidden>
105+
plan type: generic, reused
106+
rows decoded from KV: 12 (96 B, 24 KVs, 12 gRPC calls)
107+
regions: <hidden>
108+
·
109+
• limit
110+
│ count: 10
111+
112+
└── • filter
113+
│ sql nodes: <hidden>
114+
│ regions: <hidden>
115+
│ execution time: 0µs
116+
│ actual row count: 10
117+
│ estimated row count: 90
118+
│ filter: b != 5
119+
120+
└── • scan
121+
sql nodes: <hidden>
122+
kv nodes: <hidden>
123+
regions: <hidden>
124+
KV time: 0µs
125+
KV rows decoded: 12
126+
actual row count: 12
127+
estimated row count: 12 - 100 (100% of the table; stats collected <hidden> ago)
128+
table: abc@abc_pkey
129+
spans: [/0 - /99]
130+
·
131+
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==
132+
133+
statement ok
134+
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)