|
10 | 10 |
|
11 | 11 | #include"pg_compat.h"
|
12 | 12 |
|
| 13 | +#include"catalog/pg_proc.h" |
| 14 | +#include"foreign/fdwapi.h" |
| 15 | +#include"optimizer/clauses.h" |
13 | 16 | #include"optimizer/pathnode.h"
|
14 | 17 | #include"optimizer/prep.h"
|
15 | 18 | #include"port.h"
|
16 | 19 | #include"utils.h"
|
| 20 | +#include"utils/lsyscache.h" |
17 | 21 |
|
18 | 22 | #include<math.h>
|
19 | 23 |
|
@@ -101,4 +105,213 @@ make_result(List *tlist,
|
101 | 105 |
|
102 | 106 | returnnode;
|
103 | 107 | }
|
| 108 | + |
| 109 | +/* |
| 110 | + * If this relation could possibly be scanned from within a worker, then set |
| 111 | + * its consider_parallel flag. |
| 112 | + */ |
| 113 | +void |
| 114 | +set_rel_consider_parallel(PlannerInfo*root,RelOptInfo*rel, |
| 115 | +RangeTblEntry*rte) |
| 116 | +{ |
| 117 | +/* |
| 118 | + * The flag has previously been initialized to false, so we can just |
| 119 | + * return if it becomes clear that we can't safely set it. |
| 120 | + */ |
| 121 | +Assert(!rel->consider_parallel); |
| 122 | + |
| 123 | +/* Don't call this if parallelism is disallowed for the entire query. */ |
| 124 | +Assert(root->glob->parallelModeOK); |
| 125 | + |
| 126 | +/* This should only be called for baserels and appendrel children. */ |
| 127 | +Assert(rel->reloptkind==RELOPT_BASEREL|| |
| 128 | +rel->reloptkind==RELOPT_OTHER_MEMBER_REL); |
| 129 | + |
| 130 | +/* Assorted checks based on rtekind. */ |
| 131 | +switch (rte->rtekind) |
| 132 | +{ |
| 133 | +caseRTE_RELATION: |
| 134 | + |
| 135 | +/* |
| 136 | + * Currently, parallel workers can't access the leader's temporary |
| 137 | + * tables. We could possibly relax this if the wrote all of its |
| 138 | + * local buffers at the start of the query and made no changes |
| 139 | + * thereafter (maybe we could allow hint bit changes), and if we |
| 140 | + * taught the workers to read them. Writing a large number of |
| 141 | + * temporary buffers could be expensive, though, and we don't have |
| 142 | + * the rest of the necessary infrastructure right now anyway. So |
| 143 | + * for now, bail out if we see a temporary table. |
| 144 | + */ |
| 145 | +if (get_rel_persistence(rte->relid)==RELPERSISTENCE_TEMP) |
| 146 | +return; |
| 147 | + |
| 148 | +/* |
| 149 | + * Table sampling can be pushed down to workers if the sample |
| 150 | + * function and its arguments are safe. |
| 151 | + */ |
| 152 | +if (rte->tablesample!=NULL) |
| 153 | +{ |
| 154 | +Oidproparallel=func_parallel(rte->tablesample->tsmhandler); |
| 155 | + |
| 156 | +if (proparallel!=PROPARALLEL_SAFE) |
| 157 | +return; |
| 158 | +if (has_parallel_hazard((Node*)rte->tablesample->args, |
| 159 | +false)) |
| 160 | +return; |
| 161 | +} |
| 162 | + |
| 163 | +/* |
| 164 | + * Ask FDWs whether they can support performing a ForeignScan |
| 165 | + * within a worker. Most often, the answer will be no. For |
| 166 | + * example, if the nature of the FDW is such that it opens a TCP |
| 167 | + * connection with a remote server, each parallel worker would end |
| 168 | + * up with a separate connection, and these connections might not |
| 169 | + * be appropriately coordinated between workers and the leader. |
| 170 | + */ |
| 171 | +if (rte->relkind==RELKIND_FOREIGN_TABLE) |
| 172 | +{ |
| 173 | +Assert(rel->fdwroutine); |
| 174 | +if (!rel->fdwroutine->IsForeignScanParallelSafe) |
| 175 | +return; |
| 176 | +if (!rel->fdwroutine->IsForeignScanParallelSafe(root,rel,rte)) |
| 177 | +return; |
| 178 | +} |
| 179 | + |
| 180 | +/* |
| 181 | + * There are additional considerations for appendrels, which we'll |
| 182 | + * deal with in set_append_rel_size and set_append_rel_pathlist. |
| 183 | + * For now, just set consider_parallel based on the rel's own |
| 184 | + * quals and targetlist. |
| 185 | + */ |
| 186 | +break; |
| 187 | + |
| 188 | +caseRTE_SUBQUERY: |
| 189 | + |
| 190 | +/* |
| 191 | + * There's no intrinsic problem with scanning a subquery-in-FROM |
| 192 | + * (as distinct from a SubPlan or InitPlan) in a parallel worker. |
| 193 | + * If the subquery doesn't happen to have any parallel-safe paths, |
| 194 | + * then flagging it as consider_parallel won't change anything, |
| 195 | + * but that's true for plain tables, too. We must set |
| 196 | + * consider_parallel based on the rel's own quals and targetlist, |
| 197 | + * so that if a subquery path is parallel-safe but the quals and |
| 198 | + * projection we're sticking onto it are not, we correctly mark |
| 199 | + * the SubqueryScanPath as not parallel-safe. (Note that |
| 200 | + * set_subquery_pathlist() might push some of these quals down |
| 201 | + * into the subquery itself, but that doesn't change anything.) |
| 202 | + */ |
| 203 | +break; |
| 204 | + |
| 205 | +caseRTE_JOIN: |
| 206 | +/* Shouldn't happen; we're only considering baserels here. */ |
| 207 | +Assert(false); |
| 208 | +return; |
| 209 | + |
| 210 | +caseRTE_FUNCTION: |
| 211 | +/* Check for parallel-restricted functions. */ |
| 212 | +if (has_parallel_hazard((Node*)rte->functions, false)) |
| 213 | +return; |
| 214 | +break; |
| 215 | + |
| 216 | +caseRTE_VALUES: |
| 217 | +/* Check for parallel-restricted functions. */ |
| 218 | +if (has_parallel_hazard((Node*)rte->values_lists, false)) |
| 219 | +return; |
| 220 | +break; |
| 221 | + |
| 222 | +caseRTE_CTE: |
| 223 | + |
| 224 | +/* |
| 225 | + * CTE tuplestores aren't shared among parallel workers, so we |
| 226 | + * force all CTE scans to happen in the leader. Also, populating |
| 227 | + * the CTE would require executing a subplan that's not available |
| 228 | + * in the worker, might be parallel-restricted, and must get |
| 229 | + * executed only once. |
| 230 | + */ |
| 231 | +return; |
| 232 | +} |
| 233 | + |
| 234 | +/* |
| 235 | + * If there's anything in baserestrictinfo that's parallel-restricted, we |
| 236 | + * give up on parallelizing access to this relation. We could consider |
| 237 | + * instead postponing application of the restricted quals until we're |
| 238 | + * above all the parallelism in the plan tree, but it's not clear that |
| 239 | + * that would be a win in very many cases, and it might be tricky to make |
| 240 | + * outer join clauses work correctly. It would likely break equivalence |
| 241 | + * classes, too. |
| 242 | + */ |
| 243 | +if (has_parallel_hazard((Node*)rel->baserestrictinfo, false)) |
| 244 | +return; |
| 245 | + |
| 246 | +/* |
| 247 | + * Likewise, if the relation's outputs are not parallel-safe, give up. |
| 248 | + * (Usually, they're just Vars, but sometimes they're not.) |
| 249 | + */ |
| 250 | +if (has_parallel_hazard((Node*)rel->reltarget->exprs, false)) |
| 251 | +return; |
| 252 | + |
| 253 | +/* We have a winner. */ |
| 254 | +rel->consider_parallel= true; |
| 255 | +} |
| 256 | + |
| 257 | +/* |
| 258 | + * create_plain_partial_paths |
| 259 | + * Build partial access paths for parallel scan of a plain relation |
| 260 | + */ |
| 261 | +void |
| 262 | +create_plain_partial_paths(PlannerInfo*root,RelOptInfo*rel) |
| 263 | +{ |
| 264 | +intparallel_workers; |
| 265 | + |
| 266 | +/* |
| 267 | + * If the user has set the parallel_workers reloption, use that; otherwise |
| 268 | + * select a default number of workers. |
| 269 | + */ |
| 270 | +if (rel->rel_parallel_workers!=-1) |
| 271 | +parallel_workers=rel->rel_parallel_workers; |
| 272 | +else |
| 273 | +{ |
| 274 | +intparallel_threshold; |
| 275 | + |
| 276 | +/* |
| 277 | + * If this relation is too small to be worth a parallel scan, just |
| 278 | + * return without doing anything ... unless it's an inheritance child. |
| 279 | + * In that case, we want to generate a parallel path here anyway. It |
| 280 | + * might not be worthwhile just for this relation, but when combined |
| 281 | + * with all of its inheritance siblings it may well pay off. |
| 282 | + */ |
| 283 | +if (rel->pages< (BlockNumber)min_parallel_relation_size&& |
| 284 | +rel->reloptkind==RELOPT_BASEREL) |
| 285 | +return; |
| 286 | + |
| 287 | +/* |
| 288 | + * Select the number of workers based on the log of the size of the |
| 289 | + * relation. This probably needs to be a good deal more |
| 290 | + * sophisticated, but we need something here for now. Note that the |
| 291 | + * upper limit of the min_parallel_relation_size GUC is chosen to |
| 292 | + * prevent overflow here. |
| 293 | + */ |
| 294 | +parallel_workers=1; |
| 295 | +parallel_threshold=Max(min_parallel_relation_size,1); |
| 296 | +while (rel->pages >= (BlockNumber) (parallel_threshold*3)) |
| 297 | +{ |
| 298 | +parallel_workers++; |
| 299 | +parallel_threshold *=3; |
| 300 | +if (parallel_threshold>INT_MAX /3) |
| 301 | +break;/* avoid overflow */ |
| 302 | +} |
| 303 | +} |
| 304 | + |
| 305 | +/* |
| 306 | + * In no case use more than max_parallel_workers_per_gather workers. |
| 307 | + */ |
| 308 | +parallel_workers=Min(parallel_workers,max_parallel_workers_per_gather); |
| 309 | + |
| 310 | +/* If any limit was set to zero, the user doesn't want a parallel scan. */ |
| 311 | +if (parallel_workers <=0) |
| 312 | +return; |
| 313 | + |
| 314 | +/* Add an unordered partial path based on a parallel sequential scan. */ |
| 315 | +add_partial_path(rel,create_seqscan_path(root,rel,NULL,parallel_workers)); |
| 316 | +} |
104 | 317 | #endif
|