@@ -28,15 +28,14 @@ SELECT pathman.create_hash_partitions('test.mytbl', 'id1', 8);
2828INSERT INTO test.fk VALUES (1, 1);
2929INSERT INTO test.mytbl VALUES (1, 1, 5), (1, 1, 6);
3030/* gather statistics on test tables to have deterministic plans */
31- ANALYZE test.fk;
32- ANALYZE test.mytbl;
31+ ANALYZE;
3332/* run test queries */
3433EXPLAIN (COSTS OFF) /* test plan */
3534SELECT m.tableoid::regclass, id1, id2, key, start_key, end_key
3635FROM test.mytbl m JOIN test.fk USING(id1, id2)
3736WHERE NOT key <@ int4range(6, end_key);
38- QUERY PLAN
39- ------------------------------------------------------------------------------------
37+ QUERY PLAN
38+ -------------------------------------------------------------------------------------------------------
4039 Nested Loop
4140 -> Seq Scan on fk
4241 -> Custom Scan (RuntimeAppend)
@@ -71,17 +70,14 @@ WHERE NOT key <@ int4range(6, end_key);
7170 Filter: ((fk.id2 = id2) AND (NOT (key <@ int4range(6, fk.end_key))))
7271 -> Bitmap Index Scan on mytbl_5_pkey
7372 Index Cond: (id1 = fk.id1)
74- -> Bitmap Heap Scan on mytbl_6 m
75- Recheck Cond: (id1 = fk.id1)
76- Filter: ((fk.id2 = id2) AND (NOT (key <@ int4range(6, fk.end_key))))
77- -> Bitmap Index Scan on mytbl_6_pkey
78- Index Cond: (id1 = fk.id1)
73+ -> Seq Scan on mytbl_6 m
74+ Filter: ((fk.id1 = id1) AND (fk.id2 = id2) AND (NOT (key <@ int4range(6, fk.end_key))))
7975 -> Bitmap Heap Scan on mytbl_7 m
8076 Recheck Cond: (id1 = fk.id1)
8177 Filter: ((fk.id2 = id2) AND (NOT (key <@ int4range(6, fk.end_key))))
8278 -> Bitmap Index Scan on mytbl_7_pkey
8379 Index Cond: (id1 = fk.id1)
84- (44 rows)
80+ (41 rows)
8581
8682/* test joint data */
8783SELECT m.tableoid::regclass, id1, id2, key, start_key, end_key
@@ -92,7 +88,85 @@ WHERE NOT key <@ int4range(6, end_key);
9288 test.mytbl_6 | 1 | 1 | 5 | |
9389(1 row)
9490
91+ /*
92+ * Test case by @dimarick
93+ */
94+ CREATE TABLE test.parent (
95+ id SERIAL NOT NULL,
96+ owner_id INTEGER NOT NULL
97+ );
98+ CREATE TABLE test.child (
99+ parent_id INTEGER NOT NULL,
100+ owner_id INTEGER NOT NULL
101+ );
102+ CREATE TABLE test.child_nopart (
103+ parent_id INTEGER NOT NULL,
104+ owner_id INTEGER NOT NULL
105+ );
106+ INSERT INTO test.parent (owner_id) VALUES (1), (2), (3), (3);
107+ INSERT INTO test.child (parent_id, owner_id) VALUES (1, 1), (2, 2), (3, 3), (5, 3);
108+ INSERT INTO test.child_nopart (parent_id, owner_id) VALUES (1, 1), (2, 2), (3, 3), (5, 3);
109+ SELECT pathman.create_hash_partitions('test.child', 'owner_id', 2);
110+ create_hash_partitions
111+ ------------------------
112+ 2
113+ (1 row)
114+
115+ /* gather statistics on test tables to have deterministic plans */
116+ ANALYZE;
117+ /* Query #1 */
118+ EXPLAIN (COSTS OFF) SELECT * FROM test.parent
119+ LEFT JOIN test.child ON test.child.parent_id = test.parent.id AND
120+ test.child.owner_id = test.parent.owner_id
121+ WHERE test.parent.owner_id = 3 and test.parent.id IN (3, 4);
122+ QUERY PLAN
123+ -----------------------------------------------------------------------------------------------------
124+ Nested Loop Left Join
125+ -> Seq Scan on parent
126+ Filter: ((id = ANY ('{3,4}'::integer[])) AND (owner_id = 3))
127+ -> Custom Scan (RuntimeAppend)
128+ Prune by: ((child.owner_id = 3) AND (child.owner_id = parent.owner_id))
129+ -> Seq Scan on child_1 child
130+ Filter: ((owner_id = 3) AND (owner_id = parent.owner_id) AND (parent_id = parent.id))
131+ (7 rows)
132+
133+ SELECT * FROM test.parent
134+ LEFT JOIN test.child ON test.child.parent_id = test.parent.id AND
135+ test.child.owner_id = test.parent.owner_id
136+ WHERE test.parent.owner_id = 3 and test.parent.id IN (3, 4);
137+ id | owner_id | parent_id | owner_id
138+ ----+----------+-----------+----------
139+ 3 | 3 | 3 | 3
140+ 4 | 3 | |
141+ (2 rows)
142+
143+ /* Query #2 */
144+ EXPLAIN (COSTS OFF) SELECT * FROM test.parent
145+ LEFT JOIN test.child ON test.child.parent_id = test.parent.id AND
146+ test.child.owner_id = 3
147+ WHERE test.parent.owner_id = 3 and test.parent.id IN (3, 4);
148+ QUERY PLAN
149+ ----------------------------------------------------------------------
150+ Nested Loop Left Join
151+ Join Filter: (child_1.parent_id = parent.id)
152+ -> Seq Scan on parent
153+ Filter: ((id = ANY ('{3,4}'::integer[])) AND (owner_id = 3))
154+ -> Append
155+ -> Seq Scan on child_1
156+ Filter: (owner_id = 3)
157+ (7 rows)
158+
159+ SELECT * FROM test.parent
160+ LEFT JOIN test.child ON test.child.parent_id = test.parent.id AND
161+ test.child.owner_id = 3
162+ WHERE test.parent.owner_id = 3 and test.parent.id IN (3, 4);
163+ id | owner_id | parent_id | owner_id
164+ ----+----------+-----------+----------
165+ 3 | 3 | 3 | 3
166+ 4 | 3 | |
167+ (2 rows)
168+
95169DROP SCHEMA test CASCADE;
96- NOTICE: drop cascades to 10 other objects
170+ NOTICE: drop cascades to 15 other objects
97171DROP EXTENSION pg_pathman CASCADE;
98172DROP SCHEMA pathman CASCADE;
0 commit comments