Skip to content

Commit b714498

Browse files
authoredFeb 5, 2025
Support routing based purely on target list. (#1009)
1 parent 47a272e commit b714498

File tree

8 files changed

+147
-1
lines changed

8 files changed

+147
-1
lines changed
 

‎go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ require (
2020
github.com/lib/pq v1.10.9
2121
github.com/libp2p/go-reuseport v0.4.0
2222
github.com/opentracing/opentracing-go v1.2.0
23-
github.com/pg-sharding/lyx v0.0.0-20250205095044-3d4593d06b55
23+
github.com/pg-sharding/lyx v0.0.0-20250205112935-771f430d614f
2424
github.com/pkg/errors v0.9.1
2525
github.com/rs/zerolog v1.33.0
2626
github.com/sevlyar/go-daemon v0.1.6

‎go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ github.com/pg-sharding/lyx v0.0.0-20250203080453-fc228647d0af h1:NnvzvVzlzj/FHdD
170170
github.com/pg-sharding/lyx v0.0.0-20250203080453-fc228647d0af/go.mod h1:2dPBQAhqv/30mhzj2yBXQkXhsGJQ8GhM+oWOfbGua58=
171171
github.com/pg-sharding/lyx v0.0.0-20250205095044-3d4593d06b55 h1:Z6yI8iwmpuinCD8NOTSBHzuZRAxhD6kGfFU+e0MzhHo=
172172
github.com/pg-sharding/lyx v0.0.0-20250205095044-3d4593d06b55/go.mod h1:2dPBQAhqv/30mhzj2yBXQkXhsGJQ8GhM+oWOfbGua58=
173+
github.com/pg-sharding/lyx v0.0.0-20250205112935-771f430d614f h1:z4UC1xwZhRjpx6S2P+crm6nT1b33ptVxK5T0eHHSQWY=
174+
github.com/pg-sharding/lyx v0.0.0-20250205112935-771f430d614f/go.mod h1:2dPBQAhqv/30mhzj2yBXQkXhsGJQ8GhM+oWOfbGua58=
173175
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
174176
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
175177
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=

‎router/qrouter/proxy_routing.go

+9
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,12 @@ func (qr *ProxyQrouter) routeWithRules(ctx context.Context, rm *rmeta.RoutingMet
717717
if e.Name == "current_schema" || e.Name == "set_config" || e.Name == "pg_is_in_recovery" || e.Name == "version" {
718718
return plan.RandomDispatchPlan{}, ro, nil
719719
}
720+
for _, innerExp := range e.Args {
721+
switch iE := innerExp.(type) {
722+
case *lyx.Select:
723+
_, _ = qr.planQueryV1(ctx, iE, rm)
724+
}
725+
}
720726
/* Expression like SELECT 1, SELECT 'a', SELECT 1.0, SELECT true, SELECT false */
721727
case *lyx.AExprIConst, *lyx.AExprSConst, *lyx.AExprNConst, *lyx.AExprBConst:
722728
return plan.RandomDispatchPlan{}, ro, nil
@@ -726,8 +732,11 @@ func (qr *ProxyQrouter) routeWithRules(ctx context.Context, rm *rmeta.RoutingMet
726732
if e.ColName == "current_schema" {
727733
return plan.RandomDispatchPlan{}, ro, nil
728734
}
735+
case *lyx.Select:
736+
_, _ = qr.planQueryV1(ctx, e, rm)
729737
}
730738
}
739+
731740
} else if node.LArg != nil && node.RArg != nil {
732741
/* deparse populates the FromClause info,
733742
* so it do recurse into both branches, even if an error is encountered

‎test/regress/schedule/router

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ test: copy_reference_table
2020
test: copy_inside_singleshard_connection
2121
test: distributed_relation_multishard_modify
2222
test: part_table
23+
test: target_list_routing
2324
test: reference_table
2425
test: ddl
2526
test: engine_v2

‎test/regress/tests/router/expected/ddl.out

+2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ NOTICE: send query to shard(s) : sh1,sh2,sh3,sh4
5151
ALTER TABLE "table_2" RENAME TO "table_1";
5252
ALTER TABLE "tmp" RENAME TO "table_2";
5353
COMMIT;
54+
DROP SCHEMA sh1;
55+
NOTICE: send query to shard(s) : sh1,sh2,sh3,sh4
5456
DROP TABLE table_1 CASCADE;
5557
NOTICE: send query to shard(s) : sh1,sh2,sh3,sh4
5658
DROP TABLE table_2;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
\c spqr-console
2+
3+
SPQR router admin console
4+
Here you can configure your routing rules
5+
------------------------------------------------
6+
You can find documentation here
7+
https://github.com/pg-sharding/spqr/tree/master/docs
8+
9+
CREATE DISTRIBUTION ds1 COLUMN TYPES integer;
10+
add distribution
11+
------------------------
12+
distribution id -> ds1
13+
(1 row)
14+
15+
CREATE KEY RANGE kridi1 from 0 route to sh1 FOR DISTRIBUTION ds1;
16+
add key range
17+
---------------
18+
bound -> 0
19+
(1 row)
20+
21+
CREATE KEY RANGE kridi2 from 11 route to sh2 FOR DISTRIBUTION ds1;
22+
add key range
23+
---------------
24+
bound -> 11
25+
(1 row)
26+
27+
ALTER DISTRIBUTION ds1 ATTACH RELATION tlt1 DISTRIBUTION KEY i;
28+
attach table
29+
-------------------------
30+
relation name -> tlt1
31+
distribution id -> ds1
32+
(2 rows)
33+
34+
\c regress
35+
CREATE SCHEMA sh2;
36+
NOTICE: send query to shard(s) : sh1,sh2,sh3,sh4
37+
CREATE TABLE sh2.tlt1(i int, j int);
38+
NOTICE: send query to shard(s) : sh1,sh2,sh3,sh4
39+
INSERT INTO sh2.tlt1 (i, j) VALUES(1, 12);
40+
NOTICE: send query to shard(s) : sh1
41+
INSERT INTO sh2.tlt1 (i, j) VALUES(1, 12);
42+
NOTICE: send query to shard(s) : sh1
43+
INSERT INTO sh2.tlt1 (i, j) VALUES(2, 13);
44+
NOTICE: send query to shard(s) : sh1
45+
INSERT INTO sh2.tlt1 (i, j) VALUES(2, 13);
46+
NOTICE: send query to shard(s) : sh1
47+
INSERT INTO sh2.tlt1 (i, j) VALUES(12, 12);
48+
NOTICE: send query to shard(s) : sh2
49+
INSERT INTO sh2.tlt1 (i, j) VALUES(12, 14);
50+
NOTICE: send query to shard(s) : sh2
51+
INSERT INTO sh2.tlt1 (i, j) VALUES(122, 124);
52+
NOTICE: send query to shard(s) : sh2
53+
INSERT INTO sh2.tlt1 (i, j) VALUES(112, 124);
54+
NOTICE: send query to shard(s) : sh2
55+
INSERT INTO sh2.tlt1 (i, j) VALUES(113, 125);
56+
NOTICE: send query to shard(s) : sh2
57+
select (select sum(j) from sh2.tlt1 where i = 112);
58+
NOTICE: send query to shard(s) : sh2
59+
sum
60+
-----
61+
124
62+
(1 row)
63+
64+
select (select sum(j) from sh2.tlt1 where i = 112), (select sum(j) from sh2.tlt1 where sh2.tlt1.i = 113);
65+
NOTICE: send query to shard(s) : sh2
66+
sum | sum
67+
-----+-----
68+
124 | 125
69+
(1 row)
70+
71+
select coalesce((select sum(j) from sh2.tlt1 where i = 1), 0), coalesce((select sum(j) from sh2.tlt1 where i = 2 and j not in (select 12)), 0);
72+
NOTICE: send query to shard(s) : sh1
73+
coalesce | coalesce
74+
----------+----------
75+
24 | 26
76+
(1 row)
77+
78+
DROP SCHEMA sh2 CASCADE;
79+
NOTICE: send query to shard(s) : sh1,sh2,sh3,sh4
80+
\c spqr-console
81+
82+
SPQR router admin console
83+
Here you can configure your routing rules
84+
------------------------------------------------
85+
You can find documentation here
86+
https://github.com/pg-sharding/spqr/tree/master/docs
87+
88+
DROP DISTRIBUTION ALL CASCADE;
89+
drop distribution
90+
------------------------
91+
distribution id -> ds1
92+
(1 row)
93+
94+
DROP KEY RANGE ALL;
95+
drop key range
96+
----------------
97+
(0 rows)
98+

‎test/regress/tests/router/sql/ddl.sql

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ ALTER TABLE "table_2" RENAME TO "table_1";
2121
ALTER TABLE "tmp" RENAME TO "table_2";
2222
COMMIT;
2323

24+
DROP SCHEMA sh1;
25+
2426
DROP TABLE table_1 CASCADE;
2527
DROP TABLE table_2;
2628

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
\c spqr-console
3+
CREATE DISTRIBUTION ds1 COLUMN TYPES integer;
4+
CREATE KEY RANGE kridi1 from 0 route to sh1 FOR DISTRIBUTION ds1;
5+
CREATE KEY RANGE kridi2 from 11 route to sh2 FOR DISTRIBUTION ds1;
6+
ALTER DISTRIBUTION ds1 ATTACH RELATION tlt1 DISTRIBUTION KEY i;
7+
8+
\c regress
9+
10+
CREATE SCHEMA sh2;
11+
CREATE TABLE sh2.tlt1(i int, j int);
12+
13+
INSERT INTO sh2.tlt1 (i, j) VALUES(1, 12);
14+
INSERT INTO sh2.tlt1 (i, j) VALUES(1, 12);
15+
INSERT INTO sh2.tlt1 (i, j) VALUES(2, 13);
16+
INSERT INTO sh2.tlt1 (i, j) VALUES(2, 13);
17+
INSERT INTO sh2.tlt1 (i, j) VALUES(12, 12);
18+
INSERT INTO sh2.tlt1 (i, j) VALUES(12, 14);
19+
INSERT INTO sh2.tlt1 (i, j) VALUES(122, 124);
20+
INSERT INTO sh2.tlt1 (i, j) VALUES(112, 124);
21+
INSERT INTO sh2.tlt1 (i, j) VALUES(113, 125);
22+
23+
select (select sum(j) from sh2.tlt1 where i = 112);
24+
select (select sum(j) from sh2.tlt1 where i = 112), (select sum(j) from sh2.tlt1 where sh2.tlt1.i = 113);
25+
select coalesce((select sum(j) from sh2.tlt1 where i = 1), 0), coalesce((select sum(j) from sh2.tlt1 where i = 2 and j not in (select 12)), 0);
26+
27+
DROP SCHEMA sh2 CASCADE;
28+
29+
\c spqr-console
30+
DROP DISTRIBUTION ALL CASCADE;
31+
DROP KEY RANGE ALL;
32+

0 commit comments

Comments
 (0)