|
| 1 | +-- start_matchsubs |
| 2 | +-- m/\(actual rows=[^)]*\)/ |
| 3 | +-- s/\(actual rows=[^)]*\)/(actual rows=...)/ |
| 4 | +-- end_matchsubs |
| 5 | +-- start_matchignore |
| 6 | +-- m/^\s*Buckets: \d+ Batches: \d+ Memory Usage: \d+kB/ |
| 7 | +-- end_matchignore |
| 8 | +CREATE EXTENSION parallel_customscan; |
| 9 | +-- Set up data BEFORE enabling our hook so ANALYZE doesn't traverse it. |
| 10 | +CREATE TABLE pcs_t (a int) |
| 11 | + WITH (parallel_workers = 2) |
| 12 | + DISTRIBUTED BY (a); |
| 13 | +INSERT INTO pcs_t SELECT generate_series(1, 1000); |
| 14 | +ANALYZE pcs_t; |
| 15 | +SET optimizer = off; |
| 16 | +SET enable_parallel = on; |
| 17 | +SET enable_seqscan = off; |
| 18 | +SET min_parallel_table_scan_size = 0; |
| 19 | +SET parallel_setup_cost = 0; |
| 20 | +SET parallel_tuple_cost = 0; |
| 21 | +SET max_parallel_workers_per_gather = 2; |
| 22 | +-- (1) Enabling the hook replaces the Parallel Seq Scan with a Custom Scan. |
| 23 | +EXPLAIN (COSTS OFF) SELECT count(*) FROM pcs_t; |
| 24 | + QUERY PLAN |
| 25 | +------------------------------------------------ |
| 26 | + Finalize Aggregate |
| 27 | + -> Gather Motion 6:1 (slice1; segments: 6) |
| 28 | + -> Partial Aggregate |
| 29 | + -> Parallel Seq Scan on pcs_t |
| 30 | + Optimizer: Postgres query optimizer |
| 31 | +(5 rows) |
| 32 | + |
| 33 | +SET parallel_customscan.enabled = on; |
| 34 | +EXPLAIN (COSTS OFF) SELECT count(*) FROM pcs_t; |
| 35 | + QUERY PLAN |
| 36 | +------------------------------------------------------------- |
| 37 | + Finalize Aggregate |
| 38 | + -> Gather Motion 6:1 (slice1; segments: 6) |
| 39 | + -> Partial Aggregate |
| 40 | + -> Parallel Custom Scan (ParallelCustomScan) |
| 41 | + -> Parallel Seq Scan on pcs_t |
| 42 | + Optimizer: Postgres query optimizer |
| 43 | +(6 rows) |
| 44 | + |
| 45 | +-- (2) Correctness: results match only if every parallel worker runs our scan. |
| 46 | +SELECT count(*) FROM pcs_t; |
| 47 | + count |
| 48 | +------- |
| 49 | + 1000 |
| 50 | +(1 row) |
| 51 | + |
| 52 | +SELECT sum(a) FROM pcs_t; |
| 53 | + sum |
| 54 | +-------- |
| 55 | + 500500 |
| 56 | +(1 row) |
| 57 | + |
| 58 | +-- (3) Scan-level qualifier and projection through the custom scan. |
| 59 | +EXPLAIN (COSTS OFF) SELECT a FROM pcs_t WHERE a > 990; |
| 60 | + QUERY PLAN |
| 61 | +------------------------------------------------- |
| 62 | + Gather Motion 6:1 (slice1; segments: 6) |
| 63 | + -> Parallel Custom Scan (ParallelCustomScan) |
| 64 | + -> Parallel Seq Scan on pcs_t |
| 65 | + Filter: (a > 990) |
| 66 | + Optimizer: Postgres query optimizer |
| 67 | +(5 rows) |
| 68 | + |
| 69 | +SELECT a FROM pcs_t WHERE a > 990 ORDER BY a; |
| 70 | + a |
| 71 | +------ |
| 72 | + 991 |
| 73 | + 992 |
| 74 | + 993 |
| 75 | + 994 |
| 76 | + 995 |
| 77 | + 996 |
| 78 | + 997 |
| 79 | + 998 |
| 80 | + 999 |
| 81 | + 1000 |
| 82 | +(10 rows) |
| 83 | + |
| 84 | +SELECT count(*) FROM pcs_t WHERE a % 2 = 0; |
| 85 | + count |
| 86 | +------- |
| 87 | + 500 |
| 88 | +(1 row) |
| 89 | + |
| 90 | +-- (4) Join with both inputs scanned by the custom scan, plus a scan-level qual. |
| 91 | +CREATE TABLE pcs_t2 (a int) |
| 92 | + WITH (parallel_workers = 2) |
| 93 | + DISTRIBUTED BY (a); |
| 94 | +INSERT INTO pcs_t2 SELECT generate_series(1, 500); |
| 95 | +ANALYZE pcs_t2; |
| 96 | +EXPLAIN (COSTS OFF) |
| 97 | + SELECT count(*) FROM pcs_t x JOIN pcs_t2 y ON x.a = y.a WHERE x.a <= 100; |
| 98 | + QUERY PLAN |
| 99 | +------------------------------------------------------------------------- |
| 100 | + Finalize Aggregate |
| 101 | + -> Gather Motion 6:1 (slice1; segments: 6) |
| 102 | + -> Partial Aggregate |
| 103 | + -> Parallel Hash Join |
| 104 | + Hash Cond: (x.a = y.a) |
| 105 | + -> Parallel Custom Scan (ParallelCustomScan) |
| 106 | + -> Parallel Seq Scan on pcs_t x |
| 107 | + Filter: (a <= 100) |
| 108 | + -> Parallel Hash |
| 109 | + -> Parallel Custom Scan (ParallelCustomScan) |
| 110 | + -> Parallel Seq Scan on pcs_t2 y |
| 111 | + Filter: (a <= 100) |
| 112 | + Optimizer: Postgres query optimizer |
| 113 | +(13 rows) |
| 114 | + |
| 115 | +SELECT count(*) FROM pcs_t x JOIN pcs_t2 y ON x.a = y.a WHERE x.a <= 100; |
| 116 | + count |
| 117 | +------- |
| 118 | + 100 |
| 119 | +(1 row) |
| 120 | + |
| 121 | +-- (5) Empty relation: the custom scan must handle an immediate end-of-scan. |
| 122 | +CREATE TABLE pcs_empty (a int) |
| 123 | + WITH (parallel_workers = 2) |
| 124 | + DISTRIBUTED BY (a); |
| 125 | +ANALYZE pcs_empty; |
| 126 | +EXPLAIN (COSTS OFF) SELECT count(*) FROM pcs_empty; |
| 127 | + QUERY PLAN |
| 128 | +------------------------------------------------ |
| 129 | + Aggregate |
| 130 | + -> Gather Motion 3:1 (slice1; segments: 3) |
| 131 | + -> Custom Scan (ParallelCustomScan) |
| 132 | + -> Seq Scan on pcs_empty |
| 133 | + Optimizer: Postgres query optimizer |
| 134 | +(5 rows) |
| 135 | + |
| 136 | +SELECT count(*) FROM pcs_empty; |
| 137 | + count |
| 138 | +------- |
| 139 | + 0 |
| 140 | +(1 row) |
| 141 | + |
| 142 | +SELECT * FROM pcs_empty; |
| 143 | + a |
| 144 | +--- |
| 145 | +(0 rows) |
| 146 | + |
| 147 | +-- (6) Serial path: with no workers, a non-parallel Custom Scan is used. |
| 148 | +SET max_parallel_workers_per_gather = 0; |
| 149 | +EXPLAIN (COSTS OFF) SELECT count(*) FROM pcs_t; |
| 150 | + QUERY PLAN |
| 151 | +---------------------------------------------------- |
| 152 | + Finalize Aggregate |
| 153 | + -> Gather Motion 3:1 (slice1; segments: 3) |
| 154 | + -> Partial Aggregate |
| 155 | + -> Custom Scan (ParallelCustomScan) |
| 156 | + -> Seq Scan on pcs_t |
| 157 | + Optimizer: Postgres query optimizer |
| 158 | +(6 rows) |
| 159 | + |
| 160 | +SELECT count(*) FROM pcs_t; |
| 161 | + count |
| 162 | +------- |
| 163 | + 1000 |
| 164 | +(1 row) |
| 165 | + |
| 166 | +SELECT a FROM pcs_t WHERE a > 995 ORDER BY a; |
| 167 | + a |
| 168 | +------ |
| 169 | + 996 |
| 170 | + 997 |
| 171 | + 998 |
| 172 | + 999 |
| 173 | + 1000 |
| 174 | +(5 rows) |
| 175 | + |
| 176 | +SET max_parallel_workers_per_gather = 2; |
| 177 | +-- (7) EXPLAIN ANALYZE: exercises planstate_walk_kids' custom_ps recursion. |
| 178 | +EXPLAIN (ANALYZE, TIMING OFF, COSTS OFF, SUMMARY OFF) |
| 179 | + SELECT count(*) FROM pcs_t; |
| 180 | + QUERY PLAN |
| 181 | +--------------------------------------------------------------------------------------- |
| 182 | + Finalize Aggregate (actual rows=1 loops=1) |
| 183 | + -> Gather Motion 6:1 (slice1; segments: 6) (actual rows=6 loops=1) |
| 184 | + -> Partial Aggregate (actual rows=1 loops=1) |
| 185 | + -> Parallel Custom Scan (ParallelCustomScan) (actual rows=340 loops=1) |
| 186 | + -> Parallel Seq Scan on pcs_t (actual rows=340 loops=1) |
| 187 | + Optimizer: Postgres query optimizer |
| 188 | +(6 rows) |
| 189 | + |
| 190 | +EXPLAIN (ANALYZE, TIMING OFF, COSTS OFF, SUMMARY OFF) |
| 191 | + SELECT count(*) FROM pcs_t x JOIN pcs_t2 y ON x.a = y.a; |
| 192 | + QUERY PLAN |
| 193 | +------------------------------------------------------------------------------------------------- |
| 194 | + Finalize Aggregate (actual rows=1 loops=1) |
| 195 | + -> Gather Motion 6:1 (slice1; segments: 6) (actual rows=6 loops=1) |
| 196 | + -> Partial Aggregate (actual rows=1 loops=1) |
| 197 | + -> Parallel Hash Join (actual rows=0 loops=1) |
| 198 | + Hash Cond: (x.a = y.a) |
| 199 | + -> Parallel Custom Scan (ParallelCustomScan) (actual rows=340 loops=1) |
| 200 | + -> Parallel Seq Scan on pcs_t x (actual rows=340 loops=1) |
| 201 | + -> Parallel Hash (actual rows=0 loops=1) |
| 202 | + Buckets: 524288 Batches: 1 Memory Usage: 4128kB |
| 203 | + -> Parallel Custom Scan (ParallelCustomScan) (actual rows=0 loops=1) |
| 204 | + -> Parallel Seq Scan on pcs_t2 y (actual rows=0 loops=1) |
| 205 | + Optimizer: Postgres query optimizer |
| 206 | +(12 rows) |
| 207 | + |
| 208 | +-- cleanup |
| 209 | +DROP TABLE pcs_t2; |
| 210 | +DROP TABLE pcs_empty; |
| 211 | +DROP TABLE pcs_t; |
| 212 | +DROP EXTENSION parallel_customscan; |
0 commit comments