YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/postgres/src/backend/optimizer/path/allpaths.c
Line
Count
Source (jump to first uncovered line)
1
/*-------------------------------------------------------------------------
2
 *
3
 * allpaths.c
4
 *    Routines to find possible search paths for processing a query
5
 *
6
 * Portions Copyright (c) 1996-2018, PostgreSQL Global Development Group
7
 * Portions Copyright (c) 1994, Regents of the University of California
8
 *
9
 *
10
 * IDENTIFICATION
11
 *    src/backend/optimizer/path/allpaths.c
12
 *
13
 * The following only applies to changes made to this file as part of
14
 * YugaByte development.
15
 *
16
 * Portions Copyright (c) YugaByte, Inc.
17
 *
18
 * Licensed under the Apache License, Version 2.0 (the "License"); you
19
 * may not use this file except in compliance with the License.
20
 * You may obtain a copy of the License at
21
 *
22
 * http://www.apache.org/licenses/LICENSE-2.0
23
 *
24
 * Unless required by applicable law or agreed to in writing, software
25
 * distributed under the License is distributed on an "AS IS" BASIS,
26
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
27
 * implied.  See the License for the specific language governing
28
 * permissions and limitations under the License.
29
 *-------------------------------------------------------------------------
30
 */
31
32
#include "postgres.h"
33
34
#include <limits.h>
35
#include <math.h>
36
#include <utils/rel.h>
37
38
#include "miscadmin.h"
39
#include "access/sysattr.h"
40
#include "access/tsmapi.h"
41
#include "catalog/pg_class.h"
42
#include "catalog/pg_operator.h"
43
#include "catalog/pg_proc.h"
44
#include "catalog/pg_database.h"
45
#include "foreign/fdwapi.h"
46
#include "miscadmin.h"
47
#include "nodes/makefuncs.h"
48
#include "nodes/nodeFuncs.h"
49
#ifdef OPTIMIZER_DEBUG
50
#include "nodes/print.h"
51
#endif
52
#include "optimizer/clauses.h"
53
#include "optimizer/cost.h"
54
#include "optimizer/geqo.h"
55
#include "optimizer/pathnode.h"
56
#include "optimizer/paths.h"
57
#include "optimizer/plancat.h"
58
#include "optimizer/planner.h"
59
#include "optimizer/prep.h"
60
#include "optimizer/restrictinfo.h"
61
#include "optimizer/tlist.h"
62
#include "optimizer/var.h"
63
#include "parser/parse_clause.h"
64
#include "parser/parsetree.h"
65
#include "partitioning/partprune.h"
66
#include "rewrite/rewriteManip.h"
67
#include "utils/lsyscache.h"
68
69
/*  YB includes. */
70
#include "executor/ybc_fdw.h"
71
#include "pg_yb_utils.h"
72
73
/* results of subquery_is_pushdown_safe */
74
typedef struct pushdown_safety_info
75
{
76
  bool     *unsafeColumns;  /* which output columns are unsafe to use */
77
  bool    unsafeVolatile; /* don't push down volatile quals */
78
  bool    unsafeLeaky;  /* don't push down leaky quals */
79
} pushdown_safety_info;
80
81
/* These parameters are set by GUC */
82
bool    enable_geqo = false;  /* just in case GUC doesn't set it */
83
int     geqo_threshold;
84
int     min_parallel_table_scan_size;
85
int     min_parallel_index_scan_size;
86
87
/* Hook for plugins to get control in set_rel_pathlist() */
88
set_rel_pathlist_hook_type set_rel_pathlist_hook = NULL;
89
90
/* Hook for plugins to replace standard_join_search() */
91
join_search_hook_type join_search_hook = NULL;
92
93
94
static void set_base_rel_consider_startup(PlannerInfo *root);
95
static void set_base_rel_sizes(PlannerInfo *root);
96
static void set_base_rel_pathlists(PlannerInfo *root);
97
static void set_rel_size(PlannerInfo *root, RelOptInfo *rel,
98
       Index rti, RangeTblEntry *rte);
99
static void set_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
100
         Index rti, RangeTblEntry *rte);
101
static void set_plain_rel_size(PlannerInfo *root, RelOptInfo *rel,
102
           RangeTblEntry *rte);
103
static void create_plain_partial_paths(PlannerInfo *root, RelOptInfo *rel);
104
static void set_rel_consider_parallel(PlannerInfo *root, RelOptInfo *rel,
105
              RangeTblEntry *rte);
106
static void set_plain_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
107
             RangeTblEntry *rte);
108
static void set_tablesample_rel_size(PlannerInfo *root, RelOptInfo *rel,
109
             RangeTblEntry *rte);
110
static void set_tablesample_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
111
               RangeTblEntry *rte);
112
static void set_foreign_size(PlannerInfo *root, RelOptInfo *rel,
113
         RangeTblEntry *rte);
114
static void set_foreign_pathlist(PlannerInfo *root, RelOptInfo *rel,
115
           RangeTblEntry *rte);
116
static void set_append_rel_size(PlannerInfo *root, RelOptInfo *rel,
117
          Index rti, RangeTblEntry *rte);
118
static void set_append_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
119
            Index rti, RangeTblEntry *rte);
120
static void generate_mergeappend_paths(PlannerInfo *root, RelOptInfo *rel,
121
               List *live_childrels,
122
               List *all_child_pathkeys,
123
               List *partitioned_rels);
124
static Path *get_cheapest_parameterized_child_path(PlannerInfo *root,
125
                    RelOptInfo *rel,
126
                    Relids required_outer);
127
static void accumulate_append_subpath(Path *path,
128
              List **subpaths, List **special_subpaths);
129
static void set_subquery_pathlist(PlannerInfo *root, RelOptInfo *rel,
130
            Index rti, RangeTblEntry *rte);
131
static void set_function_pathlist(PlannerInfo *root, RelOptInfo *rel,
132
            RangeTblEntry *rte);
133
static void set_values_pathlist(PlannerInfo *root, RelOptInfo *rel,
134
          RangeTblEntry *rte);
135
static void set_tablefunc_pathlist(PlannerInfo *root, RelOptInfo *rel,
136
             RangeTblEntry *rte);
137
static void set_cte_pathlist(PlannerInfo *root, RelOptInfo *rel,
138
         RangeTblEntry *rte);
139
static void set_namedtuplestore_pathlist(PlannerInfo *root, RelOptInfo *rel,
140
               RangeTblEntry *rte);
141
static void set_worktable_pathlist(PlannerInfo *root, RelOptInfo *rel,
142
             RangeTblEntry *rte);
143
static RelOptInfo *make_rel_from_joinlist(PlannerInfo *root, List *joinlist);
144
static bool subquery_is_pushdown_safe(Query *subquery, Query *topquery,
145
              pushdown_safety_info *safetyInfo);
146
static bool recurse_pushdown_safe(Node *setOp, Query *topquery,
147
            pushdown_safety_info *safetyInfo);
148
static void check_output_expressions(Query *subquery,
149
             pushdown_safety_info *safetyInfo);
150
static void compare_tlist_datatypes(List *tlist, List *colTypes,
151
            pushdown_safety_info *safetyInfo);
152
static bool targetIsInAllPartitionLists(TargetEntry *tle, Query *query);
153
static bool qual_is_pushdown_safe(Query *subquery, Index rti, Node *qual,
154
            pushdown_safety_info *safetyInfo);
155
static void subquery_push_qual(Query *subquery,
156
           RangeTblEntry *rte, Index rti, Node *qual);
157
static void recurse_push_qual(Node *setOp, Query *topquery,
158
          RangeTblEntry *rte, Index rti, Node *qual);
159
static void remove_unused_subquery_outputs(Query *subquery, RelOptInfo *rel);
160
161
162
/*
163
 * make_one_rel
164
 *    Finds all possible access paths for executing a query, returning a
165
 *    single rel that represents the join of all base rels in the query.
166
 */
167
RelOptInfo *
168
make_one_rel(PlannerInfo *root, List *joinlist)
169
70.6k
{
170
70.6k
  RelOptInfo *rel;
171
70.6k
  Index   rti;
172
173
  /*
174
   * Construct the all_baserels Relids set.
175
   */
176
70.6k
  root->all_baserels = NULL;
177
159k
  for (rti = 1; rti < root->simple_rel_array_size; rti++)
178
88.6k
  {
179
88.6k
    RelOptInfo *brel = root->simple_rel_array[rti];
180
181
    /* there may be empty slots corresponding to non-baserel RTEs */
182
88.6k
    if (brel == NULL)
183
9.71k
      continue;
184
185
78.8k
    Assert(brel->relid == rti); /* sanity check on array */
186
187
    /* ignore RTEs that are "other rels" */
188
78.8k
    if (brel->reloptkind != RELOPT_BASEREL)
189
3.49k
      continue;
190
191
75.3k
    root->all_baserels = bms_add_member(root->all_baserels, brel->relid);
192
75.3k
  }
193
194
70.6k
  if (IsYugaByteEnabled())
195
70.6k
  {
196
159k
    for (rti = 1; rti < root->simple_rel_array_size; rti++)
197
88.5k
    {
198
88.5k
      RelOptInfo *relation = root->simple_rel_array[rti];
199
200
88.5k
      if (relation != NULL && relation->rtekind == RTE_RELATION)
201
63.2k
      {
202
63.2k
        RangeTblEntry *rte = root->simple_rte_array[rti];
203
63.2k
        if (IsYBRelationById(rte->relid)) {
204
          /*
205
           * Set the YugaByte FDW routine because we will use the foreign
206
           * scan API below.
207
           */
208
62.5k
          relation->fdwroutine = (FdwRoutine *) ybc_fdw_handler();
209
62.5k
        }
210
63.2k
      }
211
88.5k
    }
212
70.6k
  }
213
214
  /* Mark base rels as to whether we care about fast-start plans */
215
70.6k
  set_base_rel_consider_startup(root);
216
217
  /*
218
   * Compute size estimates and consider_parallel flags for each base rel,
219
   * then generate access paths.
220
   */
221
70.6k
  set_base_rel_sizes(root);
222
70.6k
  set_base_rel_pathlists(root);
223
224
  /*
225
   * Generate access paths for the entire join tree.
226
   */
227
70.6k
  rel = make_rel_from_joinlist(root, joinlist);
228
229
  /*
230
   * The result should join all and only the query's base rels.
231
   */
232
70.6k
  Assert(bms_equal(rel->relids, root->all_baserels));
233
234
70.6k
  return rel;
235
70.6k
}
236
237
/*
238
 * set_base_rel_consider_startup
239
 *    Set the consider_[param_]startup flags for each base-relation entry.
240
 *
241
 * For the moment, we only deal with consider_param_startup here; because the
242
 * logic for consider_startup is pretty trivial and is the same for every base
243
 * relation, we just let build_simple_rel() initialize that flag correctly to
244
 * start with.  If that logic ever gets more complicated it would probably
245
 * be better to move it here.
246
 */
247
static void
248
set_base_rel_consider_startup(PlannerInfo *root)
249
70.6k
{
250
  /*
251
   * Since parameterized paths can only be used on the inside of a nestloop
252
   * join plan, there is usually little value in considering fast-start
253
   * plans for them.  However, for relations that are on the RHS of a SEMI
254
   * or ANTI join, a fast-start plan can be useful because we're only going
255
   * to care about fetching one tuple anyway.
256
   *
257
   * To minimize growth of planning time, we currently restrict this to
258
   * cases where the RHS is a single base relation, not a join; there is no
259
   * provision for consider_param_startup to get set at all on joinrels.
260
   * Also we don't worry about appendrels.  costsize.c's costing rules for
261
   * nestloop semi/antijoins don't consider such cases either.
262
   */
263
70.6k
  ListCell   *lc;
264
265
70.6k
  foreach(lc, root->join_info_list)
266
575
  {
267
575
    SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(lc);
268
575
    int     varno;
269
270
575
    if ((sjinfo->jointype == JOIN_SEMI || sjinfo->jointype == JOIN_ANTI) &&
271
26
      bms_get_singleton_member(sjinfo->syn_righthand, &varno))
272
26
    {
273
26
      RelOptInfo *rel = find_base_rel(root, varno);
274
275
26
      rel->consider_param_startup = true;
276
26
    }
277
575
  }
278
70.6k
}
279
280
/*
281
 * set_base_rel_sizes
282
 *    Set the size estimates (rows and widths) for each base-relation entry.
283
 *    Also determine whether to consider parallel paths for base relations.
284
 *
285
 * We do this in a separate pass over the base rels so that rowcount
286
 * estimates are available for parameterized path generation, and also so
287
 * that each rel's consider_parallel flag is set correctly before we begin to
288
 * generate paths.
289
 */
290
static void
291
set_base_rel_sizes(PlannerInfo *root)
292
70.5k
{
293
70.5k
  Index   rti;
294
295
159k
  for (rti = 1; rti < root->simple_rel_array_size; rti++)
296
88.5k
  {
297
88.5k
    RelOptInfo *rel = root->simple_rel_array[rti];
298
88.5k
    RangeTblEntry *rte;
299
300
    /* there may be empty slots corresponding to non-baserel RTEs */
301
88.5k
    if (rel == NULL)
302
9.71k
      continue;
303
304
78.8k
    Assert(rel->relid == rti);  /* sanity check on array */
305
306
    /* ignore RTEs that are "other rels" */
307
78.8k
    if (rel->reloptkind != RELOPT_BASEREL)
308
3.49k
      continue;
309
310
75.3k
    rte = root->simple_rte_array[rti];
311
312
    /*
313
     * If parallelism is allowable for this query in general, see whether
314
     * it's allowable for this rel in particular.  We have to do this
315
     * before set_rel_size(), because (a) if this rel is an inheritance
316
     * parent, set_append_rel_size() will use and perhaps change the rel's
317
     * consider_parallel flag, and (b) for some RTE types, set_rel_size()
318
     * goes ahead and makes paths immediately.
319
     */
320
75.3k
    if (root->glob->parallelModeOK)
321
9.11k
      set_rel_consider_parallel(root, rel, rte);
322
323
75.3k
    set_rel_size(root, rel, rti, rte);
324
75.3k
  }
325
70.5k
}
326
327
/*
328
 * set_base_rel_pathlists
329
 *    Finds all paths available for scanning each base-relation entry.
330
 *    Sequential scan and any available indices are considered.
331
 *    Each useful path is attached to its relation's 'pathlist' field.
332
 */
333
static void
334
set_base_rel_pathlists(PlannerInfo *root)
335
70.6k
{
336
70.6k
  Index   rti;
337
338
159k
  for (rti = 1; rti < root->simple_rel_array_size; rti++)
339
88.5k
  {
340
88.5k
    RelOptInfo *rel = root->simple_rel_array[rti];
341
342
    /* there may be empty slots corresponding to non-baserel RTEs */
343
88.5k
    if (rel == NULL)
344
9.71k
      continue;
345
346
78.8k
    Assert(rel->relid == rti);  /* sanity check on array */
347
348
    /* ignore RTEs that are "other rels" */
349
78.8k
    if (rel->reloptkind != RELOPT_BASEREL)
350
3.49k
      continue;
351
352
75.3k
    set_rel_pathlist(root, rel, rti, root->simple_rte_array[rti]);
353
75.3k
  }
354
70.6k
}
355
356
/*
357
 * set_rel_size
358
 *    Set size estimates for a base relation
359
 */
360
static void
361
set_rel_size(PlannerInfo *root, RelOptInfo *rel,
362
       Index rti, RangeTblEntry *rte)
363
78.1k
{
364
78.1k
  if (rel->reloptkind == RELOPT_BASEREL &&
365
75.3k
    relation_excluded_by_constraints(root, rel, rte))
366
39
  {
367
    /*
368
     * We proved we don't need to scan the rel via constraint exclusion,
369
     * so set up a single dummy path for it.  Here we only check this for
370
     * regular baserels; if it's an otherrel, CE was already checked in
371
     * set_append_rel_size().
372
     *
373
     * In this case, we go ahead and set up the relation's path right away
374
     * instead of leaving it for set_rel_pathlist to do.  This is because
375
     * we don't have a convention for marking a rel as dummy except by
376
     * assigning a dummy path to it.
377
     */
378
39
    set_dummy_rel_pathlist(rel);
379
39
  }
380
78.0k
  else if (rte->inh)
381
199
  {
382
    /* It's an "append relation", process accordingly */
383
199
    set_append_rel_size(root, rel, rti, rte);
384
199
  }
385
77.8k
  else
386
77.8k
  {
387
77.8k
    switch (rel->rtekind)
388
77.8k
    {
389
62.2k
      case RTE_RELATION:
390
62.2k
        if (rte->relkind == RELKIND_FOREIGN_TABLE)
391
467
        {
392
          /* Foreign table */
393
467
          set_foreign_size(root, rel, rte);
394
467
        }
395
61.8k
        else if (rte->relkind == RELKIND_PARTITIONED_TABLE)
396
0
        {
397
          /*
398
           * A partitioned table without any partitions is marked as
399
           * a dummy rel.
400
           */
401
0
          set_dummy_rel_pathlist(rel);
402
0
        }
403
61.8k
        else if (rte->tablesample != NULL)
404
2
        {
405
2
          if (IsYBRelationById(rte->relid))
406
2
          {
407
            /* TODO we don't support tablesample queries yet. */
408
2
            ereport(ERROR,
409
2
                (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
410
2
                 errmsg("'TABLESAMPLE' clause is not yet "
411
2
                    "supported by YugaByte")));
412
2
          }
413
414
          /* Sampled relation */
415
2
          set_tablesample_rel_size(root, rel, rte);
416
2
        }
417
61.8k
        else
418
61.8k
        {
419
          /* Plain relation */
420
61.8k
          if (IsYBRelationById(rte->relid))
421
61.6k
          {
422
61.6k
            set_foreign_size(root, rel, rte);
423
61.6k
          }
424
218
          else
425
218
          {
426
            /* Use regular scan for initdb tables. */
427
218
            set_plain_rel_size(root, rel, rte);
428
218
          }
429
61.8k
        }
430
62.2k
        break;
431
1.60k
      case RTE_SUBQUERY:
432
433
        /*
434
         * Subqueries don't support making a choice between
435
         * parameterized and unparameterized paths, so just go ahead
436
         * and build their paths immediately.
437
         */
438
1.60k
        set_subquery_pathlist(root, rel, rti, rte);
439
1.60k
        break;
440
1.65k
      case RTE_FUNCTION:
441
1.65k
        set_function_size_estimates(root, rel);
442
1.65k
        break;
443
0
      case RTE_TABLEFUNC:
444
0
        set_tablefunc_size_estimates(root, rel);
445
0
        break;
446
2.10k
      case RTE_VALUES:
447
2.10k
        set_values_size_estimates(root, rel);
448
2.10k
        break;
449
10.1k
      case RTE_CTE:
450
451
        /*
452
         * CTEs don't support making a choice between parameterized
453
         * and unparameterized paths, so just go ahead and build their
454
         * paths immediately.
455
         */
456
10.1k
        if (rte->self_reference)
457
0
          set_worktable_pathlist(root, rel, rte);
458
10.1k
        else
459
10.1k
          set_cte_pathlist(root, rel, rte);
460
10.1k
        break;
461
0
      case RTE_NAMEDTUPLESTORE:
462
0
        set_namedtuplestore_pathlist(root, rel, rte);
463
0
        break;
464
0
      default:
465
0
        elog(ERROR, "unexpected rtekind: %d", (int) rel->rtekind);
466
0
        break;
467
78.0k
    }
468
78.0k
  }
469
470
  /*
471
   * We insist that all non-dummy rels have a nonzero rowcount estimate.
472
   */
473
78.0k
  Assert(rel->rows > 0 || IS_DUMMY_REL(rel));
474
78.0k
}
475
476
/*
477
 * set_rel_pathlist
478
 *    Build access paths for a base relation
479
 */
480
static void
481
set_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
482
         Index rti, RangeTblEntry *rte)
483
78.1k
{
484
78.1k
  if (IS_DUMMY_REL(rel))
485
65
  {
486
    /* We already proved the relation empty, so nothing more to do */
487
65
  }
488
78.0k
  else if (rte->inh)
489
197
  {
490
    /* It's an "append relation", process accordingly */
491
197
    set_append_rel_pathlist(root, rel, rti, rte);
492
197
  }
493
77.8k
  else
494
77.8k
  {
495
77.8k
    switch (rel->rtekind)
496
77.8k
    {
497
62.3k
      case RTE_RELATION:
498
62.3k
        if (rte->relkind == RELKIND_FOREIGN_TABLE)
499
466
        {
500
          /* Foreign table */
501
466
          set_foreign_pathlist(root, rel, rte);
502
466
        }
503
61.8k
        else if (rte->tablesample != NULL)
504
0
        {
505
0
          if (IsYBRelationById(rte->relid))
506
0
          {
507
            /* TODO we don't support tablesample queries yet. */
508
0
            ereport(ERROR,
509
0
                (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
510
0
                errmsg("'TABLESAMPLE' clause is not yet "
511
0
                     "supported by YugaByte")));
512
0
          }
513
514
          /* Sampled relation */
515
0
          set_tablesample_rel_pathlist(root, rel, rte);
516
0
        }
517
61.8k
        else
518
61.8k
        {
519
          /* Plain relation */
520
61.8k
          if (IsYBRelationById(rte->relid))
521
61.6k
          {
522
            /*
523
             * Using a foreign scan which will use the YB FDW by
524
             * default.
525
             */
526
61.6k
            set_foreign_pathlist(root, rel, rte);
527
61.6k
          }
528
246
          else
529
246
          {
530
            /* Use regular scan for initdb tables. */
531
246
            set_plain_rel_pathlist(root, rel, rte);
532
246
          }
533
61.8k
        }
534
62.3k
        break;
535
1.60k
      case RTE_SUBQUERY:
536
        /* Subquery --- fully handled during set_rel_size */
537
1.60k
        break;
538
1.65k
      case RTE_FUNCTION:
539
        /* RangeFunction */
540
1.65k
        set_function_pathlist(root, rel, rte);
541
1.65k
        break;
542
0
      case RTE_TABLEFUNC:
543
        /* Table Function */
544
0
        set_tablefunc_pathlist(root, rel, rte);
545
0
        break;
546
2.10k
      case RTE_VALUES:
547
        /* Values list */
548
2.10k
        set_values_pathlist(root, rel, rte);
549
2.10k
        break;
550
10.1k
      case RTE_CTE:
551
        /* CTE reference --- fully handled during set_rel_size */
552
10.1k
        break;
553
0
      case RTE_NAMEDTUPLESTORE:
554
        /* tuplestore reference --- fully handled during set_rel_size */
555
0
        break;
556
0
      default:
557
0
        elog(ERROR, "unexpected rtekind: %d", (int) rel->rtekind);
558
0
        break;
559
78.1k
    }
560
78.1k
  }
561
562
  /*
563
   * Allow a plugin to editorialize on the set of Paths for this base
564
   * relation.  It could add new paths (such as CustomPaths) by calling
565
   * add_path(), or add_partial_path() if parallel aware.  It could also
566
   * delete or modify paths added by the core code.
567
   */
568
78.1k
  if (set_rel_pathlist_hook)
569
78.1k
    (*set_rel_pathlist_hook) (root, rel, rti, rte);
570
571
  /*
572
   * If this is a baserel, we should normally consider gathering any partial
573
   * paths we may have created for it.  We have to do this after calling the
574
   * set_rel_pathlist_hook, else it cannot add partial paths to be included
575
   * here.
576
   *
577
   * However, if this is an inheritance child, skip it.  Otherwise, we could
578
   * end up with a very large number of gather nodes, each trying to grab
579
   * its own pool of workers.  Instead, we'll consider gathering partial
580
   * paths for the parent appendrel.
581
   *
582
   * Also, if this is the topmost scan/join rel (that is, the only baserel),
583
   * we postpone gathering until the final scan/join targetlist is available
584
   * (see grouping_planner).
585
   */
586
78.1k
  if (rel->reloptkind == RELOPT_BASEREL &&
587
75.3k
    bms_membership(root->all_baserels) != BMS_SINGLETON)
588
9.06k
    generate_gather_paths(root, rel, false);
589
590
  /* Now find the cheapest of the paths for this rel */
591
78.1k
  set_cheapest(rel);
592
593
#ifdef OPTIMIZER_DEBUG
594
  debug_print_rel(root, rel);
595
#endif
596
78.1k
}
597
598
/*
599
 * set_plain_rel_size
600
 *    Set size estimates for a plain relation (no subquery, no inheritance)
601
 */
602
static void
603
set_plain_rel_size(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
604
237
{
605
  /*
606
   * Test any partial indexes of rel for applicability.  We must do this
607
   * first since partial unique indexes can affect size estimates.
608
   */
609
237
  check_index_predicates(root, rel);
610
611
  /* Mark rel with estimated output rows, width, etc */
612
237
  set_baserel_size_estimates(root, rel);
613
237
}
614
615
/*
616
 * If this relation could possibly be scanned from within a worker, then set
617
 * its consider_parallel flag.
618
 */
619
static void
620
set_rel_consider_parallel(PlannerInfo *root, RelOptInfo *rel,
621
              RangeTblEntry *rte)
622
9.17k
{
623
  /*
624
   * The flag has previously been initialized to false, so we can just
625
   * return if it becomes clear that we can't safely set it.
626
   */
627
9.17k
  Assert(!rel->consider_parallel);
628
629
  /* Don't call this if parallelism is disallowed for the entire query. */
630
9.17k
  Assert(root->glob->parallelModeOK);
631
632
  /* This should only be called for baserels and appendrel children. */
633
9.17k
  Assert(IS_SIMPLE_REL(rel));
634
635
  /* Assorted checks based on rtekind. */
636
9.17k
  switch (rte->rtekind)
637
9.17k
  {
638
8.52k
    case RTE_RELATION:
639
640
      /*
641
       * Currently, parallel workers can't access the leader's temporary
642
       * tables.  We could possibly relax this if the wrote all of its
643
       * local buffers at the start of the query and made no changes
644
       * thereafter (maybe we could allow hint bit changes), and if we
645
       * taught the workers to read them.  Writing a large number of
646
       * temporary buffers could be expensive, though, and we don't have
647
       * the rest of the necessary infrastructure right now anyway.  So
648
       * for now, bail out if we see a temporary table.
649
       */
650
8.52k
      if (get_rel_persistence(rte->relid) == RELPERSISTENCE_TEMP)
651
99
        return;
652
653
      /*
654
       * Table sampling can be pushed down to workers if the sample
655
       * function and its arguments are safe.
656
       */
657
8.42k
      if (rte->tablesample != NULL)
658
0
      {
659
0
        char    proparallel = func_parallel(rte->tablesample->tsmhandler);
660
661
0
        if (proparallel != PROPARALLEL_SAFE)
662
0
          return;
663
0
        if (!is_parallel_safe(root, (Node *) rte->tablesample->args))
664
0
          return;
665
8.42k
      }
666
667
      /*
668
       * Ask FDWs whether they can support performing a ForeignScan
669
       * within a worker.  Most often, the answer will be no.  For
670
       * example, if the nature of the FDW is such that it opens a TCP
671
       * connection with a remote server, each parallel worker would end
672
       * up with a separate connection, and these connections might not
673
       * be appropriately coordinated between workers and the leader.
674
       */
675
8.42k
      if (rte->relkind == RELKIND_FOREIGN_TABLE)
676
339
      {
677
339
        Assert(rel->fdwroutine);
678
339
        if (!rel->fdwroutine->IsForeignScanParallelSafe)
679
316
          return;
680
23
        if (!rel->fdwroutine->IsForeignScanParallelSafe(root, rel, rte))
681
0
          return;
682
8.10k
      }
683
684
8.10k
      if (IsYugaByteEnabled())
685
8.10k
      {
686
        /* If YB scan, disable parallelization for now. */
687
8.10k
        return;
688
8.10k
      }
689
690
      /*
691
       * There are additional considerations for appendrels, which we'll
692
       * deal with in set_append_rel_size and set_append_rel_pathlist.
693
       * For now, just set consider_parallel based on the rel's own
694
       * quals and targetlist.
695
       */
696
0
      break;
697
698
141
    case RTE_SUBQUERY:
699
700
      /*
701
       * There's no intrinsic problem with scanning a subquery-in-FROM
702
       * (as distinct from a SubPlan or InitPlan) in a parallel worker.
703
       * If the subquery doesn't happen to have any parallel-safe paths,
704
       * then flagging it as consider_parallel won't change anything,
705
       * but that's true for plain tables, too.  We must set
706
       * consider_parallel based on the rel's own quals and targetlist,
707
       * so that if a subquery path is parallel-safe but the quals and
708
       * projection we're sticking onto it are not, we correctly mark
709
       * the SubqueryScanPath as not parallel-safe.  (Note that
710
       * set_subquery_pathlist() might push some of these quals down
711
       * into the subquery itself, but that doesn't change anything.)
712
       *
713
       * We can't push sub-select containing LIMIT/OFFSET to workers as
714
       * there is no guarantee that the row order will be fully
715
       * deterministic, and applying LIMIT/OFFSET will lead to
716
       * inconsistent results at the top-level.  (In some cases, where
717
       * the result is ordered, we could relax this restriction.  But it
718
       * doesn't currently seem worth expending extra effort to do so.)
719
       */
720
141
      {
721
141
        Query    *subquery = castNode(Query, rte->subquery);
722
723
141
        if (limit_needed(subquery))
724
15
          return;
725
126
      }
726
126
      break;
727
728
0
    case RTE_JOIN:
729
      /* Shouldn't happen; we're only considering baserels here. */
730
0
      Assert(false);
731
0
      return;
732
733
449
    case RTE_FUNCTION:
734
      /* Check for parallel-restricted functions. */
735
449
      if (!is_parallel_safe(root, (Node *) rte->functions))
736
207
        return;
737
242
      break;
738
739
0
    case RTE_TABLEFUNC:
740
      /* not parallel safe */
741
0
      return;
742
743
63
    case RTE_VALUES:
744
      /* Check for parallel-restricted functions. */
745
63
      if (!is_parallel_safe(root, (Node *) rte->values_lists))
746
0
        return;
747
63
      break;
748
749
2
    case RTE_CTE:
750
751
      /*
752
       * CTE tuplestores aren't shared among parallel workers, so we
753
       * force all CTE scans to happen in the leader.  Also, populating
754
       * the CTE would require executing a subplan that's not available
755
       * in the worker, might be parallel-restricted, and must get
756
       * executed only once.
757
       */
758
2
      return;
759
760
0
    case RTE_NAMEDTUPLESTORE:
761
762
      /*
763
       * tuplestore cannot be shared, at least without more
764
       * infrastructure to support that.
765
       */
766
0
      return;
767
431
  }
768
769
  /*
770
   * If there's anything in baserestrictinfo that's parallel-restricted, we
771
   * give up on parallelizing access to this relation.  We could consider
772
   * instead postponing application of the restricted quals until we're
773
   * above all the parallelism in the plan tree, but it's not clear that
774
   * that would be a win in very many cases, and it might be tricky to make
775
   * outer join clauses work correctly.  It would likely break equivalence
776
   * classes, too.
777
   */
778
431
  if (!is_parallel_safe(root, (Node *) rel->baserestrictinfo))
779
0
    return;
780
781
  /*
782
   * Likewise, if the relation's outputs are not parallel-safe, give up.
783
   * (Usually, they're just Vars, but sometimes they're not.)
784
   */
785
431
  if (!is_parallel_safe(root, (Node *) rel->reltarget->exprs))
786
0
    return;
787
788
  /* We have a winner. */
789
431
  rel->consider_parallel = true;
790
431
}
791
792
/*
793
 * set_plain_rel_pathlist
794
 *    Build access paths for a plain relation (no subquery, no inheritance)
795
 */
796
static void
797
set_plain_rel_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
798
237
{
799
237
  Relids    required_outer;
800
801
  /*
802
   * We don't support pushing join clauses into the quals of a seqscan, but
803
   * it could still have required parameterization due to LATERAL refs in
804
   * its tlist.
805
   */
806
237
  required_outer = rel->lateral_relids;
807
808
  /* Consider sequential scan */
809
237
  add_path(rel, create_seqscan_path(root, rel, required_outer, 0));
810
811
  /* If appropriate, consider parallel sequential scan */
812
237
  if (rel->consider_parallel && required_outer == NULL)
813
0
    create_plain_partial_paths(root, rel);
814
815
  /* Consider index scans */
816
237
  create_index_paths(root, rel);
817
818
  /* Consider TID scans */
819
237
  create_tidscan_paths(root, rel);
820
237
}
821
822
/*
823
 * create_plain_partial_paths
824
 *    Build partial access paths for parallel scan of a plain relation
825
 */
826
static void
827
create_plain_partial_paths(PlannerInfo *root, RelOptInfo *rel)
828
0
{
829
0
  int     parallel_workers;
830
831
0
  parallel_workers = compute_parallel_worker(rel, rel->pages, -1,
832
0
                         max_parallel_workers_per_gather);
833
834
  /* If any limit was set to zero, the user doesn't want a parallel scan. */
835
0
  if (parallel_workers <= 0)
836
0
    return;
837
838
  /* Add an unordered partial path based on a parallel sequential scan. */
839
0
  add_partial_path(rel, create_seqscan_path(root, rel, NULL, parallel_workers));
840
0
}
841
842
/*
843
 * set_tablesample_rel_size
844
 *    Set size estimates for a sampled relation
845
 */
846
static void
847
set_tablesample_rel_size(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
848
0
{
849
0
  TableSampleClause *tsc = rte->tablesample;
850
0
  TsmRoutine *tsm;
851
0
  BlockNumber pages;
852
0
  double    tuples;
853
854
  /*
855
   * Test any partial indexes of rel for applicability.  We must do this
856
   * first since partial unique indexes can affect size estimates.
857
   */
858
0
  check_index_predicates(root, rel);
859
860
  /*
861
   * Call the sampling method's estimation function to estimate the number
862
   * of pages it will read and the number of tuples it will return.  (Note:
863
   * we assume the function returns sane values.)
864
   */
865
0
  tsm = GetTsmRoutine(tsc->tsmhandler);
866
0
  tsm->SampleScanGetSampleSize(root, rel, tsc->args,
867
0
                 &pages, &tuples);
868
869
  /*
870
   * For the moment, because we will only consider a SampleScan path for the
871
   * rel, it's okay to just overwrite the pages and tuples estimates for the
872
   * whole relation.  If we ever consider multiple path types for sampled
873
   * rels, we'll need more complication.
874
   */
875
0
  rel->pages = pages;
876
0
  rel->tuples = tuples;
877
878
  /* Mark rel with estimated output rows, width, etc */
879
0
  set_baserel_size_estimates(root, rel);
880
0
}
881
882
/*
883
 * set_tablesample_rel_pathlist
884
 *    Build access paths for a sampled relation
885
 */
886
static void
887
set_tablesample_rel_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
888
0
{
889
0
  Relids    required_outer;
890
0
  Path     *path;
891
892
  /*
893
   * We don't support pushing join clauses into the quals of a samplescan,
894
   * but it could still have required parameterization due to LATERAL refs
895
   * in its tlist or TABLESAMPLE arguments.
896
   */
897
0
  required_outer = rel->lateral_relids;
898
899
  /* Consider sampled scan */
900
0
  path = create_samplescan_path(root, rel, required_outer);
901
902
  /*
903
   * If the sampling method does not support repeatable scans, we must avoid
904
   * plans that would scan the rel multiple times.  Ideally, we'd simply
905
   * avoid putting the rel on the inside of a nestloop join; but adding such
906
   * a consideration to the planner seems like a great deal of complication
907
   * to support an uncommon usage of second-rate sampling methods.  Instead,
908
   * if there is a risk that the query might perform an unsafe join, just
909
   * wrap the SampleScan in a Materialize node.  We can check for joins by
910
   * counting the membership of all_baserels (note that this correctly
911
   * counts inheritance trees as single rels).  If we're inside a subquery,
912
   * we can't easily check whether a join might occur in the outer query, so
913
   * just assume one is possible.
914
   *
915
   * GetTsmRoutine is relatively expensive compared to the other tests here,
916
   * so check repeatable_across_scans last, even though that's a bit odd.
917
   */
918
0
  if ((root->query_level > 1 ||
919
0
     bms_membership(root->all_baserels) != BMS_SINGLETON) &&
920
0
    !(GetTsmRoutine(rte->tablesample->tsmhandler)->repeatable_across_scans))
921
0
  {
922
0
    path = (Path *) create_material_path(rel, path);
923
0
  }
924
925
0
  add_path(rel, path);
926
927
  /* For the moment, at least, there are no other paths to consider */
928
0
}
929
930
/*
931
 * set_foreign_size
932
 *    Set size estimates for a foreign table RTE
933
 */
934
static void
935
set_foreign_size(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
936
62.0k
{
937
  /* Mark rel with estimated output rows, width, etc */
938
62.0k
  set_foreign_size_estimates(root, rel);
939
940
  /* Let FDW adjust the size estimates, if it can */
941
62.0k
  rel->fdwroutine->GetForeignRelSize(root, rel, rte->relid);
942
943
  /* ... but do not let it set the rows estimate to zero */
944
62.0k
  rel->rows = clamp_row_est(rel->rows);
945
62.0k
}
946
947
/*
948
 * set_foreign_pathlist
949
 *    Build access paths for a foreign table RTE
950
 */
951
static void
952
set_foreign_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
953
62.0k
{
954
  /* Call the FDW's GetForeignPaths function to generate path(s) */
955
62.0k
  rel->fdwroutine->GetForeignPaths(root, rel, rte->relid);
956
62.0k
}
957
958
/*
959
 * set_append_rel_size
960
 *    Set size estimates for a simple "append relation"
961
 *
962
 * The passed-in rel and RTE represent the entire append relation.  The
963
 * relation's contents are computed by appending together the output of the
964
 * individual member relations.  Note that in the non-partitioned inheritance
965
 * case, the first member relation is actually the same table as is mentioned
966
 * in the parent RTE ... but it has a different RTE and RelOptInfo.  This is
967
 * a good thing because their outputs are not the same size.
968
 */
969
static void
970
set_append_rel_size(PlannerInfo *root, RelOptInfo *rel,
971
          Index rti, RangeTblEntry *rte)
972
199
{
973
199
  int     parentRTindex = rti;
974
199
  bool    has_live_children;
975
199
  double    parent_rows;
976
199
  double    parent_size;
977
199
  double     *parent_attrsizes;
978
199
  int     nattrs;
979
199
  ListCell   *l;
980
199
  Relids    live_children = NULL;
981
199
  bool    did_pruning = false;
982
983
  /* Guard against stack overflow due to overly deep inheritance tree. */
984
199
  check_stack_depth();
985
986
199
  Assert(IS_SIMPLE_REL(rel));
987
988
  /*
989
   * Initialize partitioned_child_rels to contain this RT index.
990
   *
991
   * Note that during the set_append_rel_pathlist() phase, we will bubble up
992
   * the indexes of partitioned relations that appear down in the tree, so
993
   * that when we've created Paths for all the children, the root
994
   * partitioned table's list will contain all such indexes.
995
   */
996
199
  if (rte->relkind == RELKIND_PARTITIONED_TABLE)
997
154
    rel->partitioned_child_rels = list_make1_int(rti);
998
999
  /*
1000
   * If the partitioned relation has any baserestrictinfo quals then we
1001
   * attempt to use these quals to prune away partitions that cannot
1002
   * possibly contain any tuples matching these quals.  In this case we'll
1003
   * store the relids of all partitions which could possibly contain a
1004
   * matching tuple, and skip anything else in the loop below.
1005
   */
1006
199
  if (enable_partition_pruning &&
1007
199
    rte->relkind == RELKIND_PARTITIONED_TABLE &&
1008
154
    rel->baserestrictinfo != NIL)
1009
134
  {
1010
134
    live_children = prune_append_rel_partitions(root, rel);
1011
134
    did_pruning = true;
1012
134
  }
1013
1014
  /*
1015
   * If this is a partitioned baserel, set the consider_partitionwise_join
1016
   * flag; currently, we only consider partitionwise joins with the baserel
1017
   * if its targetlist doesn't contain a whole-row Var.
1018
   */
1019
199
  if (enable_partitionwise_join &&
1020
15
    rel->reloptkind == RELOPT_BASEREL &&
1021
15
    rte->relkind == RELKIND_PARTITIONED_TABLE &&
1022
15
    rel->attr_needed[InvalidAttrNumber - rel->min_attr] == NULL)
1023
11
    rel->consider_partitionwise_join = true;
1024
1025
  /*
1026
   * Initialize to compute size estimates for whole append relation.
1027
   *
1028
   * We handle width estimates by weighting the widths of different child
1029
   * rels proportionally to their number of rows.  This is sensible because
1030
   * the use of width estimates is mainly to compute the total relation
1031
   * "footprint" if we have to sort or hash it.  To do this, we sum the
1032
   * total equivalent size (in "double" arithmetic) and then divide by the
1033
   * total rowcount estimate.  This is done separately for the total rel
1034
   * width and each attribute.
1035
   *
1036
   * Note: if you consider changing this logic, beware that child rels could
1037
   * have zero rows and/or width, if they were excluded by constraints.
1038
   */
1039
199
  has_live_children = false;
1040
199
  parent_rows = 0;
1041
199
  parent_size = 0;
1042
199
  nattrs = rel->max_attr - rel->min_attr + 1;
1043
199
  parent_attrsizes = (double *) palloc0(nattrs * sizeof(double));
1044
1045
199
  foreach(l, root->append_rel_list)
1046
2.79k
  {
1047
2.79k
    AppendRelInfo *appinfo = (AppendRelInfo *) lfirst(l);
1048
2.79k
    int     childRTindex;
1049
2.79k
    RangeTblEntry *childRTE;
1050
2.79k
    RelOptInfo *childrel;
1051
2.79k
    List     *childquals;
1052
2.79k
    Index   cq_min_security;
1053
2.79k
    bool    have_const_false_cq;
1054
2.79k
    ListCell   *parentvars;
1055
2.79k
    ListCell   *childvars;
1056
2.79k
    ListCell   *lc;
1057
1058
    /* append_rel_list contains all append rels; ignore others */
1059
2.79k
    if (appinfo->parent_relid != parentRTindex)
1060
45
      continue;
1061
1062
2.75k
    childRTindex = appinfo->child_relid;
1063
2.75k
    childRTE = root->simple_rte_array[childRTindex];
1064
1065
    /*
1066
     * The child rel's RelOptInfo was already created during
1067
     * add_base_rels_to_query.
1068
     */
1069
2.75k
    childrel = find_base_rel(root, childRTindex);
1070
2.75k
    Assert(childrel->reloptkind == RELOPT_OTHER_MEMBER_REL);
1071
1072
    /*
1073
     * We have to copy the parent's targetlist and quals to the child,
1074
     * with appropriate substitution of variables.  However, only the
1075
     * baserestrictinfo quals are needed before we can check for
1076
     * constraint exclusion; so do that first and then check to see if we
1077
     * can disregard this child.
1078
     *
1079
     * The child rel's targetlist might contain non-Var expressions, which
1080
     * means that substitution into the quals could produce opportunities
1081
     * for const-simplification, and perhaps even pseudoconstant quals.
1082
     * Therefore, transform each RestrictInfo separately to see if it
1083
     * reduces to a constant or pseudoconstant.  (We must process them
1084
     * separately to keep track of the security level of each qual.)
1085
     */
1086
2.75k
    childquals = NIL;
1087
2.75k
    cq_min_security = UINT_MAX;
1088
2.75k
    have_const_false_cq = false;
1089
2.75k
    foreach(lc, rel->baserestrictinfo)
1090
2.68k
    {
1091
2.68k
      RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1092
2.68k
      Node     *childqual;
1093
2.68k
      ListCell   *lc2;
1094
1095
2.68k
      Assert(IsA(rinfo, RestrictInfo));
1096
2.68k
      childqual = adjust_appendrel_attrs(root,
1097
2.68k
                         (Node *) rinfo->clause,
1098
2.68k
                         1, &appinfo);
1099
2.68k
      childqual = eval_const_expressions(root, childqual);
1100
      /* check for flat-out constant */
1101
2.68k
      if (childqual && IsA(childqual, Const))
1102
0
      {
1103
0
        if (((Const *) childqual)->constisnull ||
1104
0
          !DatumGetBool(((Const *) childqual)->constvalue))
1105
0
        {
1106
          /* Restriction reduces to constant FALSE or NULL */
1107
0
          have_const_false_cq = true;
1108
0
          break;
1109
0
        }
1110
        /* Restriction reduces to constant TRUE, so drop it */
1111
0
        continue;
1112
0
      }
1113
      /* might have gotten an AND clause, if so flatten it */
1114
2.68k
      foreach(lc2, make_ands_implicit((Expr *) childqual))
1115
2.68k
      {
1116
2.68k
        Node     *onecq = (Node *) lfirst(lc2);
1117
2.68k
        bool    pseudoconstant;
1118
1119
        /* check for pseudoconstant (no Vars or volatile functions) */
1120
2.68k
        pseudoconstant =
1121
2.68k
          !contain_vars_of_level(onecq, 0) &&
1122
0
          !contain_volatile_functions(onecq);
1123
2.68k
        if (pseudoconstant)
1124
0
        {
1125
          /* tell createplan.c to check for gating quals */
1126
0
          root->hasPseudoConstantQuals = true;
1127
0
        }
1128
        /* reconstitute RestrictInfo with appropriate properties */
1129
2.68k
        childquals = lappend(childquals,
1130
2.68k
                   make_restrictinfo((Expr *) onecq,
1131
2.68k
                             rinfo->is_pushed_down,
1132
2.68k
                             rinfo->outerjoin_delayed,
1133
2.68k
                             pseudoconstant,
1134
2.68k
                             rinfo->security_level,
1135
2.68k
                             NULL, NULL, NULL));
1136
        /* track minimum security level among child quals */
1137
2.68k
        cq_min_security = Min(cq_min_security, rinfo->security_level);
1138
2.68k
      }
1139
2.68k
    }
1140
1141
    /*
1142
     * In addition to the quals inherited from the parent, we might have
1143
     * securityQuals associated with this particular child node.
1144
     * (Currently this can only happen in appendrels originating from
1145
     * UNION ALL; inheritance child tables don't have their own
1146
     * securityQuals, see expand_inherited_rtentry().)  Pull any such
1147
     * securityQuals up into the baserestrictinfo for the child.  This is
1148
     * similar to process_security_barrier_quals() for the parent rel,
1149
     * except that we can't make any general deductions from such quals,
1150
     * since they don't hold for the whole appendrel.
1151
     */
1152
2.75k
    if (childRTE->securityQuals)
1153
0
    {
1154
0
      Index   security_level = 0;
1155
1156
0
      foreach(lc, childRTE->securityQuals)
1157
0
      {
1158
0
        List     *qualset = (List *) lfirst(lc);
1159
0
        ListCell   *lc2;
1160
1161
0
        foreach(lc2, qualset)
1162
0
        {
1163
0
          Expr     *qual = (Expr *) lfirst(lc2);
1164
1165
          /* not likely that we'd see constants here, so no check */
1166
0
          childquals = lappend(childquals,
1167
0
                     make_restrictinfo(qual,
1168
0
                               true, false, false,
1169
0
                               security_level,
1170
0
                               NULL, NULL, NULL));
1171
0
          cq_min_security = Min(cq_min_security, security_level);
1172
0
        }
1173
0
        security_level++;
1174
0
      }
1175
0
      Assert(security_level <= root->qual_security_level);
1176
0
    }
1177
1178
    /*
1179
     * OK, we've got all the baserestrictinfo quals for this child.
1180
     */
1181
2.75k
    childrel->baserestrictinfo = childquals;
1182
2.75k
    childrel->baserestrict_min_security = cq_min_security;
1183
1184
2.75k
    if (have_const_false_cq)
1185
0
    {
1186
      /*
1187
       * Some restriction clause reduced to constant FALSE or NULL after
1188
       * substitution, so this child need not be scanned.
1189
       */
1190
0
      set_dummy_rel_pathlist(childrel);
1191
0
      continue;
1192
0
    }
1193
1194
2.75k
    if (did_pruning && !bms_is_member(appinfo->child_relid, live_children))
1195
27
    {
1196
      /* This partition was pruned; skip it. */
1197
27
      set_dummy_rel_pathlist(childrel);
1198
27
      continue;
1199
27
    }
1200
1201
2.72k
    if (relation_excluded_by_constraints(root, childrel, childRTE))
1202
0
    {
1203
      /*
1204
       * This child need not be scanned, so we can omit it from the
1205
       * appendrel.
1206
       */
1207
0
      set_dummy_rel_pathlist(childrel);
1208
0
      continue;
1209
0
    }
1210
1211
    /*
1212
     * CE failed, so finish copying/modifying targetlist and join quals.
1213
     *
1214
     * NB: the resulting childrel->reltarget->exprs may contain arbitrary
1215
     * expressions, which otherwise would not occur in a rel's targetlist.
1216
     * Code that might be looking at an appendrel child must cope with
1217
     * such.  (Normally, a rel's targetlist would only include Vars and
1218
     * PlaceHolderVars.)  XXX we do not bother to update the cost or width
1219
     * fields of childrel->reltarget; not clear if that would be useful.
1220
     */
1221
2.72k
    childrel->joininfo = (List *)
1222
2.72k
      adjust_appendrel_attrs(root,
1223
2.72k
                   (Node *) rel->joininfo,
1224
2.72k
                   1, &appinfo);
1225
2.72k
    childrel->reltarget->exprs = (List *)
1226
2.72k
      adjust_appendrel_attrs(root,
1227
2.72k
                   (Node *) rel->reltarget->exprs,
1228
2.72k
                   1, &appinfo);
1229
1230
    /*
1231
     * We have to make child entries in the EquivalenceClass data
1232
     * structures as well.  This is needed either if the parent
1233
     * participates in some eclass joins (because we will want to consider
1234
     * inner-indexscan joins on the individual children) or if the parent
1235
     * has useful pathkeys (because we should try to build MergeAppend
1236
     * paths that produce those sort orderings).
1237
     */
1238
2.72k
    if (rel->has_eclass_joins || has_useful_pathkeys(root, rel))
1239
147
      add_child_rel_equivalences(root, appinfo, rel, childrel);
1240
2.72k
    childrel->has_eclass_joins = rel->has_eclass_joins;
1241
1242
    /*
1243
     * Note: we could compute appropriate attr_needed data for the child's
1244
     * variables, by transforming the parent's attr_needed through the
1245
     * translated_vars mapping.  However, currently there's no need
1246
     * because attr_needed is only examined for base relations not
1247
     * otherrels.  So we just leave the child's attr_needed empty.
1248
     */
1249
1250
    /*
1251
     * If we consider partitionwise joins with the parent rel, do the same
1252
     * for partitioned child rels.
1253
     *
1254
     * Note: here we abuse the consider_partitionwise_join flag by setting
1255
     * it *even* for child rels that are not partitioned.  In that case,
1256
     * we set it to tell try_partitionwise_join() that it doesn't need to
1257
     * generate their targetlists and EC entries as they have already been
1258
     * generated here, as opposed to the dummy child rels for which the
1259
     * flag is left set to false so that it will generate them.
1260
     */
1261
2.72k
    if (rel->consider_partitionwise_join)
1262
21
      childrel->consider_partitionwise_join = true;
1263
1264
    /*
1265
     * If parallelism is allowable for this query in general, see whether
1266
     * it's allowable for this childrel in particular.  But if we've
1267
     * already decided the appendrel is not parallel-safe as a whole,
1268
     * there's no point in considering parallelism for this child.  For
1269
     * consistency, do this before calling set_rel_size() for the child.
1270
     */
1271
2.72k
    if (root->glob->parallelModeOK && rel->consider_parallel)
1272
64
      set_rel_consider_parallel(root, childrel, childRTE);
1273
1274
    /*
1275
     * Compute the child's size.
1276
     */
1277
2.72k
    set_rel_size(root, childrel, childRTindex, childRTE);
1278
1279
    /*
1280
     * It is possible that constraint exclusion detected a contradiction
1281
     * within a child subquery, even though we didn't prove one above. If
1282
     * so, we can skip this child.
1283
     */
1284
2.72k
    if (IS_DUMMY_REL(childrel))
1285
2
      continue;
1286
1287
    /* We have at least one live child. */
1288
2.72k
    has_live_children = true;
1289
1290
    /*
1291
     * If any live child is not parallel-safe, treat the whole appendrel
1292
     * as not parallel-safe.  In future we might be able to generate plans
1293
     * in which some children are farmed out to workers while others are
1294
     * not; but we don't have that today, so it's a waste to consider
1295
     * partial paths anywhere in the appendrel unless it's all safe.
1296
     * (Child rels visited before this one will be unmarked in
1297
     * set_append_rel_pathlist().)
1298
     */
1299
2.72k
    if (!childrel->consider_parallel)
1300
2.66k
      rel->consider_parallel = false;
1301
1302
    /*
1303
     * Accumulate size information from each live child.
1304
     */
1305
2.72k
    Assert(childrel->rows > 0);
1306
1307
2.72k
    parent_rows += childrel->rows;
1308
2.72k
    parent_size += childrel->reltarget->width * childrel->rows;
1309
1310
    /*
1311
     * Accumulate per-column estimates too.  We need not do anything for
1312
     * PlaceHolderVars in the parent list.  If child expression isn't a
1313
     * Var, or we didn't record a width estimate for it, we have to fall
1314
     * back on a datatype-based estimate.
1315
     *
1316
     * By construction, child's targetlist is 1-to-1 with parent's.
1317
     */
1318
2.72k
    forboth(parentvars, rel->reltarget->exprs,
1319
2.72k
        childvars, childrel->reltarget->exprs)
1320
5.54k
    {
1321
5.54k
      Var      *parentvar = (Var *) lfirst(parentvars);
1322
5.54k
      Node     *childvar = (Node *) lfirst(childvars);
1323
1324
5.54k
      if (IsA(parentvar, Var))
1325
5.53k
      {
1326
5.53k
        int     pndx = parentvar->varattno - rel->min_attr;
1327
5.53k
        int32   child_width = 0;
1328
1329
5.53k
        if (IsA(childvar, Var) &&
1330
5.52k
          ((Var *) childvar)->varno == childrel->relid)
1331
5.52k
        {
1332
5.52k
          int     cndx = ((Var *) childvar)->varattno - childrel->min_attr;
1333
1334
5.52k
          child_width = childrel->attr_widths[cndx];
1335
5.52k
        }
1336
5.53k
        if (child_width <= 0)
1337
14
          child_width = get_typavgwidth(exprType(childvar),
1338
14
                          exprTypmod(childvar));
1339
5.53k
        Assert(child_width > 0);
1340
5.53k
        parent_attrsizes[pndx] += child_width * childrel->rows;
1341
5.53k
      }
1342
5.54k
    }
1343
2.72k
  }
1344
1345
199
  if (has_live_children)
1346
197
  {
1347
    /*
1348
     * Save the finished size estimates.
1349
     */
1350
197
    int     i;
1351
1352
197
    Assert(parent_rows > 0);
1353
197
    rel->rows = parent_rows;
1354
197
    rel->reltarget->width = rint(parent_size / parent_rows);
1355
2.07k
    for (i = 0; i < nattrs; i++)
1356
1.88k
      rel->attr_widths[i] = rint(parent_attrsizes[i] / parent_rows);
1357
1358
    /*
1359
     * Set "raw tuples" count equal to "rows" for the appendrel; needed
1360
     * because some places assume rel->tuples is valid for any baserel.
1361
     */
1362
197
    rel->tuples = parent_rows;
1363
197
  }
1364
2
  else
1365
2
  {
1366
    /*
1367
     * All children were excluded by constraints, so mark the whole
1368
     * appendrel dummy.  We must do this in this phase so that the rel's
1369
     * dummy-ness is visible when we generate paths for other rels.
1370
     */
1371
2
    set_dummy_rel_pathlist(rel);
1372
2
  }
1373
1374
199
  pfree(parent_attrsizes);
1375
199
}
1376
1377
/*
1378
 * set_append_rel_pathlist
1379
 *    Build access paths for an "append relation"
1380
 */
1381
static void
1382
set_append_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
1383
            Index rti, RangeTblEntry *rte)
1384
197
{
1385
197
  int     parentRTindex = rti;
1386
197
  List     *live_childrels = NIL;
1387
197
  ListCell   *l;
1388
1389
  /*
1390
   * Generate access paths for each member relation, and remember the
1391
   * non-dummy children.
1392
   */
1393
197
  foreach(l, root->append_rel_list)
1394
2.79k
  {
1395
2.79k
    AppendRelInfo *appinfo = (AppendRelInfo *) lfirst(l);
1396
2.79k
    int     childRTindex;
1397
2.79k
    RangeTblEntry *childRTE;
1398
2.79k
    RelOptInfo *childrel;
1399
1400
    /* append_rel_list contains all append rels; ignore others */
1401
2.79k
    if (appinfo->parent_relid != parentRTindex)
1402
45
      continue;
1403
1404
    /* Re-locate the child RTE and RelOptInfo */
1405
2.74k
    childRTindex = appinfo->child_relid;
1406
2.74k
    childRTE = root->simple_rte_array[childRTindex];
1407
2.74k
    childrel = root->simple_rel_array[childRTindex];
1408
1409
    /*
1410
     * If set_append_rel_size() decided the parent appendrel was
1411
     * parallel-unsafe at some point after visiting this child rel, we
1412
     * need to propagate the unsafety marking down to the child, so that
1413
     * we don't generate useless partial paths for it.
1414
     */
1415
2.74k
    if (!rel->consider_parallel)
1416
2.68k
      childrel->consider_parallel = false;
1417
1418
    /*
1419
     * Compute the child's access paths.
1420
     */
1421
2.74k
    set_rel_pathlist(root, childrel, childRTindex, childRTE);
1422
1423
    /*
1424
     * If child is dummy, ignore it.
1425
     */
1426
2.74k
    if (IS_DUMMY_REL(childrel))
1427
24
      continue;
1428
1429
    /* Bubble up childrel's partitioned children. */
1430
2.72k
    if (rel->part_scheme)
1431
2.63k
      rel->partitioned_child_rels =
1432
2.63k
        list_concat(rel->partitioned_child_rels,
1433
2.63k
              list_copy(childrel->partitioned_child_rels));
1434
1435
    /*
1436
     * Child is live, so add it to the live_childrels list for use below.
1437
     */
1438
2.72k
    live_childrels = lappend(live_childrels, childrel);
1439
2.72k
  }
1440
1441
  /* Add paths to the append relation. */
1442
197
  add_paths_to_append_rel(root, rel, live_childrels);
1443
197
}
1444
1445
1446
/*
1447
 * add_paths_to_append_rel
1448
 *    Generate paths for the given append relation given the set of non-dummy
1449
 *    child rels.
1450
 *
1451
 * The function collects all parameterizations and orderings supported by the
1452
 * non-dummy children. For every such parameterization or ordering, it creates
1453
 * an append path collecting one path from each non-dummy child with given
1454
 * parameterization or ordering. Similarly it collects partial paths from
1455
 * non-dummy children to create partial append paths.
1456
 */
1457
void
1458
add_paths_to_append_rel(PlannerInfo *root, RelOptInfo *rel,
1459
            List *live_childrels)
1460
352
{
1461
352
  List     *subpaths = NIL;
1462
352
  bool    subpaths_valid = true;
1463
352
  List     *partial_subpaths = NIL;
1464
352
  List     *pa_partial_subpaths = NIL;
1465
352
  List     *pa_nonpartial_subpaths = NIL;
1466
352
  bool    partial_subpaths_valid = true;
1467
352
  bool    pa_subpaths_valid;
1468
352
  List     *all_child_pathkeys = NIL;
1469
352
  List     *all_child_outers = NIL;
1470
352
  ListCell   *l;
1471
352
  List     *partitioned_rels = NIL;
1472
352
  double    partial_rows = -1;
1473
1474
  /* If appropriate, consider parallel append */
1475
352
  pa_subpaths_valid = enable_parallel_append && rel->consider_parallel;
1476
1477
  /*
1478
   * AppendPath generated for partitioned tables must record the RT indexes
1479
   * of partitioned tables that are direct or indirect children of this
1480
   * Append rel.
1481
   *
1482
   * AppendPath may be for a sub-query RTE (UNION ALL), in which case, 'rel'
1483
   * itself does not represent a partitioned relation, but the child sub-
1484
   * queries may contain references to partitioned relations.  The loop
1485
   * below will look for such children and collect them in a list to be
1486
   * passed to the path creation function.  (This assumes that we don't need
1487
   * to look through multiple levels of subquery RTEs; if we ever do, we
1488
   * could consider stuffing the list we generate here into sub-query RTE's
1489
   * RelOptInfo, just like we do for partitioned rels, which would be used
1490
   * when populating our parent rel with paths.  For the present, that
1491
   * appears to be unnecessary.)
1492
   */
1493
352
  if (rel->part_scheme != NULL)
1494
304
  {
1495
304
    if (IS_SIMPLE_REL(rel))
1496
291
      partitioned_rels = list_make1(rel->partitioned_child_rels);
1497
13
    else if (IS_JOIN_REL(rel))
1498
13
    {
1499
13
      int     relid = -1;
1500
13
      List     *partrels = NIL;
1501
1502
      /*
1503
       * For a partitioned joinrel, concatenate the component rels'
1504
       * partitioned_child_rels lists.
1505
       */
1506
41
      while ((relid = bms_next_member(rel->relids, relid)) >= 0)
1507
28
      {
1508
28
        RelOptInfo *component;
1509
1510
28
        Assert(relid >= 1 && relid < root->simple_rel_array_size);
1511
28
        component = root->simple_rel_array[relid];
1512
28
        Assert(component->part_scheme != NULL);
1513
28
        Assert(list_length(component->partitioned_child_rels) >= 1);
1514
28
        partrels =
1515
28
          list_concat(partrels,
1516
28
                list_copy(component->partitioned_child_rels));
1517
28
      }
1518
1519
13
      partitioned_rels = list_make1(partrels);
1520
13
    }
1521
1522
304
    Assert(list_length(partitioned_rels) >= 1);
1523
304
  }
1524
1525
  /*
1526
   * For every non-dummy child, remember the cheapest path.  Also, identify
1527
   * all pathkeys (orderings) and parameterizations (required_outer sets)
1528
   * available for the non-dummy member relations.
1529
   */
1530
352
  foreach(l, live_childrels)
1531
5.36k
  {
1532
5.36k
    RelOptInfo *childrel = lfirst(l);
1533
5.36k
    ListCell   *lcp;
1534
5.36k
    Path     *cheapest_partial_path = NULL;
1535
1536
    /*
1537
     * For UNION ALLs with non-empty partitioned_child_rels, accumulate
1538
     * the Lists of child relations.
1539
     */
1540
5.36k
    if (rel->rtekind == RTE_SUBQUERY && childrel->partitioned_child_rels != NIL)
1541
0
      partitioned_rels = lappend(partitioned_rels,
1542
0
                     childrel->partitioned_child_rels);
1543
1544
    /*
1545
     * If child has an unparameterized cheapest-total path, add that to
1546
     * the unparameterized Append path we are constructing for the parent.
1547
     * If not, there's no workable unparameterized path.
1548
     *
1549
     * With partitionwise aggregates, the child rel's pathlist may be
1550
     * empty, so don't assume that a path exists here.
1551
     */
1552
5.36k
    if (childrel->pathlist != NIL &&
1553
5.36k
      childrel->cheapest_total_path->param_info == NULL)
1554
5.36k
      accumulate_append_subpath(childrel->cheapest_total_path,
1555
5.36k
                    &subpaths, NULL);
1556
0
    else
1557
0
      subpaths_valid = false;
1558
1559
    /* Same idea, but for a partial plan. */
1560
5.36k
    if (childrel->partial_pathlist != NIL)
1561
0
    {
1562
0
      cheapest_partial_path = linitial(childrel->partial_pathlist);
1563
0
      accumulate_append_subpath(cheapest_partial_path,
1564
0
                    &partial_subpaths, NULL);
1565
0
    }
1566
5.36k
    else
1567
5.36k
      partial_subpaths_valid = false;
1568
1569
    /*
1570
     * Same idea, but for a parallel append mixing partial and non-partial
1571
     * paths.
1572
     */
1573
5.36k
    if (pa_subpaths_valid)
1574
30
    {
1575
30
      Path     *nppath = NULL;
1576
1577
30
      nppath =
1578
30
        get_cheapest_parallel_safe_total_inner(childrel->pathlist);
1579
1580
30
      if (cheapest_partial_path == NULL && nppath == NULL)
1581
30
      {
1582
        /* Neither a partial nor a parallel-safe path?  Forget it. */
1583
30
        pa_subpaths_valid = false;
1584
30
      }
1585
0
      else if (nppath == NULL ||
1586
0
           (cheapest_partial_path != NULL &&
1587
0
            cheapest_partial_path->total_cost < nppath->total_cost))
1588
0
      {
1589
        /* Partial path is cheaper or the only option. */
1590
0
        Assert(cheapest_partial_path != NULL);
1591
0
        accumulate_append_subpath(cheapest_partial_path,
1592
0
                      &pa_partial_subpaths,
1593
0
                      &pa_nonpartial_subpaths);
1594
1595
0
      }
1596
0
      else
1597
0
      {
1598
        /*
1599
         * Either we've got only a non-partial path, or we think that
1600
         * a single backend can execute the best non-partial path
1601
         * faster than all the parallel backends working together can
1602
         * execute the best partial path.
1603
         *
1604
         * It might make sense to be more aggressive here.  Even if
1605
         * the best non-partial path is more expensive than the best
1606
         * partial path, it could still be better to choose the
1607
         * non-partial path if there are several such paths that can
1608
         * be given to different workers.  For now, we don't try to
1609
         * figure that out.
1610
         */
1611
0
        accumulate_append_subpath(nppath,
1612
0
                      &pa_nonpartial_subpaths,
1613
0
                      NULL);
1614
0
      }
1615
30
    }
1616
1617
    /*
1618
     * Collect lists of all the available path orderings and
1619
     * parameterizations for all the children.  We use these as a
1620
     * heuristic to indicate which sort orderings and parameterizations we
1621
     * should build Append and MergeAppend paths for.
1622
     */
1623
5.36k
    foreach(lcp, childrel->pathlist)
1624
5.45k
    {
1625
5.45k
      Path     *childpath = (Path *) lfirst(lcp);
1626
5.45k
      List     *childkeys = childpath->pathkeys;
1627
5.45k
      Relids    childouter = PATH_REQ_OUTER(childpath);
1628
1629
      /* Unsorted paths don't contribute to pathkey list */
1630
5.45k
      if (childkeys != NIL)
1631
89
      {
1632
89
        ListCell   *lpk;
1633
89
        bool    found = false;
1634
1635
        /* Have we already seen this ordering? */
1636
89
        foreach(lpk, all_child_pathkeys)
1637
53
        {
1638
53
          List     *existing_pathkeys = (List *) lfirst(lpk);
1639
1640
53
          if (compare_pathkeys(existing_pathkeys,
1641
53
                     childkeys) == PATHKEYS_EQUAL)
1642
47
          {
1643
47
            found = true;
1644
47
            break;
1645
47
          }
1646
53
        }
1647
89
        if (!found)
1648
42
        {
1649
          /* No, so add it to all_child_pathkeys */
1650
42
          all_child_pathkeys = lappend(all_child_pathkeys,
1651
42
                         childkeys);
1652
42
        }
1653
89
      }
1654
1655
      /* Unparameterized paths don't contribute to param-set list */
1656
5.45k
      if (childouter)
1657
0
      {
1658
0
        ListCell   *lco;
1659
0
        bool    found = false;
1660
1661
        /* Have we already seen this param set? */
1662
0
        foreach(lco, all_child_outers)
1663
0
        {
1664
0
          Relids    existing_outers = (Relids) lfirst(lco);
1665
1666
0
          if (bms_equal(existing_outers, childouter))
1667
0
          {
1668
0
            found = true;
1669
0
            break;
1670
0
          }
1671
0
        }
1672
0
        if (!found)
1673
0
        {
1674
          /* No, so add it to all_child_outers */
1675
0
          all_child_outers = lappend(all_child_outers,
1676
0
                         childouter);
1677
0
        }
1678
0
      }
1679
5.45k
    }
1680
5.36k
  }
1681
1682
  /*
1683
   * If we found unparameterized paths for all children, build an unordered,
1684
   * unparameterized Append path for the rel.  (Note: this is correct even
1685
   * if we have zero or one live subpath due to constraint exclusion.)
1686
   */
1687
352
  if (subpaths_valid)
1688
352
    add_path(rel, (Path *) create_append_path(root, rel, subpaths, NIL,
1689
352
                          NULL, 0, false,
1690
352
                          partitioned_rels, -1));
1691
1692
  /*
1693
   * Consider an append of unordered, unparameterized partial paths.  Make
1694
   * it parallel-aware if possible.
1695
   */
1696
352
  if (partial_subpaths_valid)
1697
0
  {
1698
0
    AppendPath *appendpath;
1699
0
    ListCell   *lc;
1700
0
    int     parallel_workers = 0;
1701
1702
    /* Find the highest number of workers requested for any subpath. */
1703
0
    foreach(lc, partial_subpaths)
1704
0
    {
1705
0
      Path     *path = lfirst(lc);
1706
1707
0
      parallel_workers = Max(parallel_workers, path->parallel_workers);
1708
0
    }
1709
0
    Assert(parallel_workers > 0);
1710
1711
    /*
1712
     * If the use of parallel append is permitted, always request at least
1713
     * log2(# of children) workers.  We assume it can be useful to have
1714
     * extra workers in this case because they will be spread out across
1715
     * the children.  The precise formula is just a guess, but we don't
1716
     * want to end up with a radically different answer for a table with N
1717
     * partitions vs. an unpartitioned table with the same data, so the
1718
     * use of some kind of log-scaling here seems to make some sense.
1719
     */
1720
0
    if (enable_parallel_append)
1721
0
    {
1722
0
      parallel_workers = Max(parallel_workers,
1723
0
                   fls(list_length(live_childrels)));
1724
0
      parallel_workers = Min(parallel_workers,
1725
0
                   max_parallel_workers_per_gather);
1726
0
    }
1727
0
    Assert(parallel_workers > 0);
1728
1729
    /* Generate a partial append path. */
1730
0
    appendpath = create_append_path(root, rel, NIL, partial_subpaths,
1731
0
                    NULL, parallel_workers,
1732
0
                    enable_parallel_append,
1733
0
                    partitioned_rels, -1);
1734
1735
    /*
1736
     * Make sure any subsequent partial paths use the same row count
1737
     * estimate.
1738
     */
1739
0
    partial_rows = appendpath->path.rows;
1740
1741
    /* Add the path. */
1742
0
    add_partial_path(rel, (Path *) appendpath);
1743
0
  }
1744
1745
  /*
1746
   * Consider a parallel-aware append using a mix of partial and non-partial
1747
   * paths.  (This only makes sense if there's at least one child which has
1748
   * a non-partial path that is substantially cheaper than any partial path;
1749
   * otherwise, we should use the append path added in the previous step.)
1750
   */
1751
352
  if (pa_subpaths_valid && pa_nonpartial_subpaths != NIL)
1752
0
  {
1753
0
    AppendPath *appendpath;
1754
0
    ListCell   *lc;
1755
0
    int     parallel_workers = 0;
1756
1757
    /*
1758
     * Find the highest number of workers requested for any partial
1759
     * subpath.
1760
     */
1761
0
    foreach(lc, pa_partial_subpaths)
1762
0
    {
1763
0
      Path     *path = lfirst(lc);
1764
1765
0
      parallel_workers = Max(parallel_workers, path->parallel_workers);
1766
0
    }
1767
1768
    /*
1769
     * Same formula here as above.  It's even more important in this
1770
     * instance because the non-partial paths won't contribute anything to
1771
     * the planned number of parallel workers.
1772
     */
1773
0
    parallel_workers = Max(parallel_workers,
1774
0
                 fls(list_length(live_childrels)));
1775
0
    parallel_workers = Min(parallel_workers,
1776
0
                 max_parallel_workers_per_gather);
1777
0
    Assert(parallel_workers > 0);
1778
1779
0
    appendpath = create_append_path(root, rel, pa_nonpartial_subpaths,
1780
0
                    pa_partial_subpaths,
1781
0
                    NULL, parallel_workers, true,
1782
0
                    partitioned_rels, partial_rows);
1783
0
    add_partial_path(rel, (Path *) appendpath);
1784
0
  }
1785
1786
  /*
1787
   * Also build unparameterized MergeAppend paths based on the collected
1788
   * list of child pathkeys.
1789
   */
1790
352
  if (subpaths_valid)
1791
352
    generate_mergeappend_paths(root, rel, live_childrels,
1792
352
                   all_child_pathkeys,
1793
352
                   partitioned_rels);
1794
1795
  /*
1796
   * Build Append paths for each parameterization seen among the child rels.
1797
   * (This may look pretty expensive, but in most cases of practical
1798
   * interest, the child rels will expose mostly the same parameterizations,
1799
   * so that not that many cases actually get considered here.)
1800
   *
1801
   * The Append node itself cannot enforce quals, so all qual checking must
1802
   * be done in the child paths.  This means that to have a parameterized
1803
   * Append path, we must have the exact same parameterization for each
1804
   * child path; otherwise some children might be failing to check the
1805
   * moved-down quals.  To make them match up, we can try to increase the
1806
   * parameterization of lesser-parameterized paths.
1807
   */
1808
352
  foreach(l, all_child_outers)
1809
0
  {
1810
0
    Relids    required_outer = (Relids) lfirst(l);
1811
0
    ListCell   *lcr;
1812
1813
    /* Select the child paths for an Append with this parameterization */
1814
0
    subpaths = NIL;
1815
0
    subpaths_valid = true;
1816
0
    foreach(lcr, live_childrels)
1817
0
    {
1818
0
      RelOptInfo *childrel = (RelOptInfo *) lfirst(lcr);
1819
0
      Path     *subpath;
1820
1821
0
      if (childrel->pathlist == NIL)
1822
0
      {
1823
        /* failed to make a suitable path for this child */
1824
0
        subpaths_valid = false;
1825
0
        break;
1826
0
      }
1827
1828
0
      subpath = get_cheapest_parameterized_child_path(root,
1829
0
                              childrel,
1830
0
                              required_outer);
1831
0
      if (subpath == NULL)
1832
0
      {
1833
        /* failed to make a suitable path for this child */
1834
0
        subpaths_valid = false;
1835
0
        break;
1836
0
      }
1837
0
      accumulate_append_subpath(subpath, &subpaths, NULL);
1838
0
    }
1839
1840
0
    if (subpaths_valid)
1841
0
      add_path(rel, (Path *)
1842
0
           create_append_path(root, rel, subpaths, NIL,
1843
0
                    required_outer, 0, false,
1844
0
                    partitioned_rels, -1));
1845
0
  }
1846
352
}
1847
1848
/*
1849
 * generate_mergeappend_paths
1850
 *    Generate MergeAppend paths for an append relation
1851
 *
1852
 * Generate a path for each ordering (pathkey list) appearing in
1853
 * all_child_pathkeys.
1854
 *
1855
 * We consider both cheapest-startup and cheapest-total cases, ie, for each
1856
 * interesting ordering, collect all the cheapest startup subpaths and all the
1857
 * cheapest total paths, and build a MergeAppend path for each case.
1858
 *
1859
 * We don't currently generate any parameterized MergeAppend paths.  While
1860
 * it would not take much more code here to do so, it's very unclear that it
1861
 * is worth the planning cycles to investigate such paths: there's little
1862
 * use for an ordered path on the inside of a nestloop.  In fact, it's likely
1863
 * that the current coding of add_path would reject such paths out of hand,
1864
 * because add_path gives no credit for sort ordering of parameterized paths,
1865
 * and a parameterized MergeAppend is going to be more expensive than the
1866
 * corresponding parameterized Append path.  If we ever try harder to support
1867
 * parameterized mergejoin plans, it might be worth adding support for
1868
 * parameterized MergeAppends to feed such joins.  (See notes in
1869
 * optimizer/README for why that might not ever happen, though.)
1870
 */
1871
static void
1872
generate_mergeappend_paths(PlannerInfo *root, RelOptInfo *rel,
1873
               List *live_childrels,
1874
               List *all_child_pathkeys,
1875
               List *partitioned_rels)
1876
352
{
1877
352
  ListCell   *lcp;
1878
1879
352
  foreach(lcp, all_child_pathkeys)
1880
42
  {
1881
42
    List     *pathkeys = (List *) lfirst(lcp);
1882
42
    List     *startup_subpaths = NIL;
1883
42
    List     *total_subpaths = NIL;
1884
42
    bool    startup_neq_total = false;
1885
42
    ListCell   *lcr;
1886
1887
    /* Select the child paths for this ordering... */
1888
42
    foreach(lcr, live_childrels)
1889
94
    {
1890
94
      RelOptInfo *childrel = (RelOptInfo *) lfirst(lcr);
1891
94
      Path     *cheapest_startup,
1892
94
             *cheapest_total;
1893
1894
      /* Locate the right paths, if they are available. */
1895
94
      cheapest_startup =
1896
94
        get_cheapest_path_for_pathkeys(childrel->pathlist,
1897
94
                         pathkeys,
1898
94
                         NULL,
1899
94
                         STARTUP_COST,
1900
94
                         false);
1901
94
      cheapest_total =
1902
94
        get_cheapest_path_for_pathkeys(childrel->pathlist,
1903
94
                         pathkeys,
1904
94
                         NULL,
1905
94
                         TOTAL_COST,
1906
94
                         false);
1907
1908
      /*
1909
       * If we can't find any paths with the right order just use the
1910
       * cheapest-total path; we'll have to sort it later.
1911
       */
1912
94
      if (cheapest_startup == NULL || cheapest_total == NULL)
1913
6
      {
1914
6
        cheapest_startup = cheapest_total =
1915
6
          childrel->cheapest_total_path;
1916
        /* Assert we do have an unparameterized path for this child */
1917
6
        Assert(cheapest_total->param_info == NULL);
1918
6
      }
1919
1920
      /*
1921
       * Notice whether we actually have different paths for the
1922
       * "cheapest" and "total" cases; frequently there will be no point
1923
       * in two create_merge_append_path() calls.
1924
       */
1925
94
      if (cheapest_startup != cheapest_total)
1926
0
        startup_neq_total = true;
1927
1928
94
      accumulate_append_subpath(cheapest_startup,
1929
94
                    &startup_subpaths, NULL);
1930
94
      accumulate_append_subpath(cheapest_total,
1931
94
                    &total_subpaths, NULL);
1932
94
    }
1933
1934
    /* ... and build the MergeAppend paths */
1935
42
    add_path(rel, (Path *) create_merge_append_path(root,
1936
42
                            rel,
1937
42
                            startup_subpaths,
1938
42
                            pathkeys,
1939
42
                            NULL,
1940
42
                            partitioned_rels));
1941
42
    if (startup_neq_total)
1942
0
      add_path(rel, (Path *) create_merge_append_path(root,
1943
0
                              rel,
1944
0
                              total_subpaths,
1945
0
                              pathkeys,
1946
0
                              NULL,
1947
0
                              partitioned_rels));
1948
42
  }
1949
352
}
1950
1951
/*
1952
 * get_cheapest_parameterized_child_path
1953
 *    Get cheapest path for this relation that has exactly the requested
1954
 *    parameterization.
1955
 *
1956
 * Returns NULL if unable to create such a path.
1957
 */
1958
static Path *
1959
get_cheapest_parameterized_child_path(PlannerInfo *root, RelOptInfo *rel,
1960
                    Relids required_outer)
1961
0
{
1962
0
  Path     *cheapest;
1963
0
  ListCell   *lc;
1964
1965
  /*
1966
   * Look up the cheapest existing path with no more than the needed
1967
   * parameterization.  If it has exactly the needed parameterization, we're
1968
   * done.
1969
   */
1970
0
  cheapest = get_cheapest_path_for_pathkeys(rel->pathlist,
1971
0
                        NIL,
1972
0
                        required_outer,
1973
0
                        TOTAL_COST,
1974
0
                        false);
1975
0
  Assert(cheapest != NULL);
1976
0
  if (bms_equal(PATH_REQ_OUTER(cheapest), required_outer))
1977
0
    return cheapest;
1978
1979
  /*
1980
   * Otherwise, we can "reparameterize" an existing path to match the given
1981
   * parameterization, which effectively means pushing down additional
1982
   * joinquals to be checked within the path's scan.  However, some existing
1983
   * paths might check the available joinquals already while others don't;
1984
   * therefore, it's not clear which existing path will be cheapest after
1985
   * reparameterization.  We have to go through them all and find out.
1986
   */
1987
0
  cheapest = NULL;
1988
0
  foreach(lc, rel->pathlist)
1989
0
  {
1990
0
    Path     *path = (Path *) lfirst(lc);
1991
1992
    /* Can't use it if it needs more than requested parameterization */
1993
0
    if (!bms_is_subset(PATH_REQ_OUTER(path), required_outer))
1994
0
      continue;
1995
1996
    /*
1997
     * Reparameterization can only increase the path's cost, so if it's
1998
     * already more expensive than the current cheapest, forget it.
1999
     */
2000
0
    if (cheapest != NULL &&
2001
0
      compare_path_costs(cheapest, path, TOTAL_COST) <= 0)
2002
0
      continue;
2003
2004
    /* Reparameterize if needed, then recheck cost */
2005
0
    if (!bms_equal(PATH_REQ_OUTER(path), required_outer))
2006
0
    {
2007
0
      path = reparameterize_path(root, path, required_outer, 1.0);
2008
0
      if (path == NULL)
2009
0
        continue;   /* failed to reparameterize this one */
2010
0
      Assert(bms_equal(PATH_REQ_OUTER(path), required_outer));
2011
2012
0
      if (cheapest != NULL &&
2013
0
        compare_path_costs(cheapest, path, TOTAL_COST) <= 0)
2014
0
        continue;
2015
0
    }
2016
2017
    /* We have a new best path */
2018
0
    cheapest = path;
2019
0
  }
2020
2021
  /* Return the best path, or NULL if we found no suitable candidate */
2022
0
  return cheapest;
2023
0
}
2024
2025
/*
2026
 * accumulate_append_subpath
2027
 *    Add a subpath to the list being built for an Append or MergeAppend.
2028
 *
2029
 * It's possible that the child is itself an Append or MergeAppend path, in
2030
 * which case we can "cut out the middleman" and just add its child paths to
2031
 * our own list.  (We don't try to do this earlier because we need to apply
2032
 * both levels of transformation to the quals.)
2033
 *
2034
 * Note that if we omit a child MergeAppend in this way, we are effectively
2035
 * omitting a sort step, which seems fine: if the parent is to be an Append,
2036
 * its result would be unsorted anyway, while if the parent is to be a
2037
 * MergeAppend, there's no point in a separate sort on a child.
2038
 * its result would be unsorted anyway.
2039
 *
2040
 * Normally, either path is a partial path and subpaths is a list of partial
2041
 * paths, or else path is a non-partial plan and subpaths is a list of those.
2042
 * However, if path is a parallel-aware Append, then we add its partial path
2043
 * children to subpaths and the rest to special_subpaths.  If the latter is
2044
 * NULL, we don't flatten the path at all (unless it contains only partial
2045
 * paths).
2046
 */
2047
static void
2048
accumulate_append_subpath(Path *path, List **subpaths, List **special_subpaths)
2049
5.55k
{
2050
5.55k
  if (IsA(path, AppendPath))
2051
2
  {
2052
2
    AppendPath *apath = (AppendPath *) path;
2053
2054
2
    if (!apath->path.parallel_aware || apath->first_partial_path == 0)
2055
2
    {
2056
      /* list_copy is important here to avoid sharing list substructure */
2057
2
      *subpaths = list_concat(*subpaths, list_copy(apath->subpaths));
2058
2
      return;
2059
2
    }
2060
0
    else if (special_subpaths != NULL)
2061
0
    {
2062
0
      List     *new_special_subpaths;
2063
2064
      /* Split Parallel Append into partial and non-partial subpaths */
2065
0
      *subpaths = list_concat(*subpaths,
2066
0
                  list_copy_tail(apath->subpaths,
2067
0
                           apath->first_partial_path));
2068
0
      new_special_subpaths =
2069
0
        list_truncate(list_copy(apath->subpaths),
2070
0
                apath->first_partial_path);
2071
0
      *special_subpaths = list_concat(*special_subpaths,
2072
0
                      new_special_subpaths);
2073
0
      return;
2074
0
    }
2075
5.55k
  }
2076
5.55k
  else if (IsA(path, MergeAppendPath))
2077
0
  {
2078
0
    MergeAppendPath *mpath = (MergeAppendPath *) path;
2079
2080
    /* list_copy is important here to avoid sharing list substructure */
2081
0
    *subpaths = list_concat(*subpaths, list_copy(mpath->subpaths));
2082
0
    return;
2083
0
  }
2084
2085
5.55k
  *subpaths = lappend(*subpaths, path);
2086
5.55k
}
2087
2088
/*
2089
 * set_dummy_rel_pathlist
2090
 *    Build a dummy path for a relation that's been excluded by constraints
2091
 *
2092
 * Rather than inventing a special "dummy" path type, we represent this as an
2093
 * AppendPath with no members (see also IS_DUMMY_PATH/IS_DUMMY_REL macros).
2094
 *
2095
 * This is exported because inheritance_planner() has need for it.
2096
 */
2097
void
2098
set_dummy_rel_pathlist(RelOptInfo *rel)
2099
70
{
2100
  /* Set dummy size estimates --- we leave attr_widths[] as zeroes */
2101
70
  rel->rows = 0;
2102
70
  rel->reltarget->width = 0;
2103
2104
  /* Discard any pre-existing paths; no further need for them */
2105
70
  rel->pathlist = NIL;
2106
70
  rel->partial_pathlist = NIL;
2107
2108
70
  add_path(rel, (Path *) create_append_path(NULL, rel, NIL, NIL, NULL,
2109
70
                        0, false, NIL, -1));
2110
2111
  /*
2112
   * We set the cheapest path immediately, to ensure that IS_DUMMY_REL()
2113
   * will recognize the relation as dummy if anyone asks.  This is redundant
2114
   * when we're called from set_rel_size(), but not when called from
2115
   * elsewhere, and doing it twice is harmless anyway.
2116
   */
2117
70
  set_cheapest(rel);
2118
70
}
2119
2120
/* quick-and-dirty test to see if any joining is needed */
2121
static bool
2122
has_multiple_baserels(PlannerInfo *root)
2123
1.52k
{
2124
1.52k
  int     num_base_rels = 0;
2125
1.52k
  Index   rti;
2126
2127
4.57k
  for (rti = 1; rti < root->simple_rel_array_size; rti++)
2128
3.05k
  {
2129
3.05k
    RelOptInfo *brel = root->simple_rel_array[rti];
2130
2131
3.05k
    if (brel == NULL)
2132
1.47k
      continue;
2133
2134
    /* ignore RTEs that are "other rels" */
2135
1.57k
    if (brel->reloptkind == RELOPT_BASEREL)
2136
1.52k
      if (++num_base_rels > 1)
2137
5
        return true;
2138
1.57k
  }
2139
1.51k
  return false;
2140
1.52k
}
2141
2142
/*
2143
 * set_subquery_pathlist
2144
 *    Generate SubqueryScan access paths for a subquery RTE
2145
 *
2146
 * We don't currently support generating parameterized paths for subqueries
2147
 * by pushing join clauses down into them; it seems too expensive to re-plan
2148
 * the subquery multiple times to consider different alternatives.
2149
 * (XXX that could stand to be reconsidered, now that we use Paths.)
2150
 * So the paths made here will be parameterized if the subquery contains
2151
 * LATERAL references, otherwise not.  As long as that's true, there's no need
2152
 * for a separate set_subquery_size phase: just make the paths right away.
2153
 */
2154
static void
2155
set_subquery_pathlist(PlannerInfo *root, RelOptInfo *rel,
2156
            Index rti, RangeTblEntry *rte)
2157
1.60k
{
2158
1.60k
  Query    *parse = root->parse;
2159
1.60k
  Query    *subquery = rte->subquery;
2160
1.60k
  Relids    required_outer;
2161
1.60k
  pushdown_safety_info safetyInfo;
2162
1.60k
  double    tuple_fraction;
2163
1.60k
  RelOptInfo *sub_final_rel;
2164
1.60k
  ListCell   *lc;
2165
2166
  /*
2167
   * Must copy the Query so that planning doesn't mess up the RTE contents
2168
   * (really really need to fix the planner to not scribble on its input,
2169
   * someday ... but see remove_unused_subquery_outputs to start with).
2170
   */
2171
1.60k
  subquery = copyObject(subquery);
2172
2173
  /*
2174
   * If it's a LATERAL subquery, it might contain some Vars of the current
2175
   * query level, requiring it to be treated as parameterized, even though
2176
   * we don't support pushing down join quals into subqueries.
2177
   */
2178
1.60k
  required_outer = rel->lateral_relids;
2179
2180
  /*
2181
   * Zero out result area for subquery_is_pushdown_safe, so that it can set
2182
   * flags as needed while recursing.  In particular, we need a workspace
2183
   * for keeping track of unsafe-to-reference columns.  unsafeColumns[i]
2184
   * will be set true if we find that output column i of the subquery is
2185
   * unsafe to use in a pushed-down qual.
2186
   */
2187
1.60k
  memset(&safetyInfo, 0, sizeof(safetyInfo));
2188
1.60k
  safetyInfo.unsafeColumns = (bool *)
2189
1.60k
    palloc0((list_length(subquery->targetList) + 1) * sizeof(bool));
2190
2191
  /*
2192
   * If the subquery has the "security_barrier" flag, it means the subquery
2193
   * originated from a view that must enforce row level security.  Then we
2194
   * must not push down quals that contain leaky functions.  (Ideally this
2195
   * would be checked inside subquery_is_pushdown_safe, but since we don't
2196
   * currently pass the RTE to that function, we must do it here.)
2197
   */
2198
1.60k
  safetyInfo.unsafeLeaky = rte->security_barrier;
2199
2200
  /*
2201
   * If there are any restriction clauses that have been attached to the
2202
   * subquery relation, consider pushing them down to become WHERE or HAVING
2203
   * quals of the subquery itself.  This transformation is useful because it
2204
   * may allow us to generate a better plan for the subquery than evaluating
2205
   * all the subquery output rows and then filtering them.
2206
   *
2207
   * There are several cases where we cannot push down clauses. Restrictions
2208
   * involving the subquery are checked by subquery_is_pushdown_safe().
2209
   * Restrictions on individual clauses are checked by
2210
   * qual_is_pushdown_safe().  Also, we don't want to push down
2211
   * pseudoconstant clauses; better to have the gating node above the
2212
   * subquery.
2213
   *
2214
   * Non-pushed-down clauses will get evaluated as qpquals of the
2215
   * SubqueryScan node.
2216
   *
2217
   * XXX Are there any cases where we want to make a policy decision not to
2218
   * push down a pushable qual, because it'd result in a worse plan?
2219
   */
2220
1.60k
  if (rel->baserestrictinfo != NIL &&
2221
8
    subquery_is_pushdown_safe(subquery, subquery, &safetyInfo))
2222
6
  {
2223
    /* OK to consider pushing down individual quals */
2224
6
    List     *upperrestrictlist = NIL;
2225
6
    ListCell   *l;
2226
2227
6
    foreach(l, rel->baserestrictinfo)
2228
6
    {
2229
6
      RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
2230
6
      Node     *clause = (Node *) rinfo->clause;
2231
2232
6
      if (!rinfo->pseudoconstant &&
2233
6
        qual_is_pushdown_safe(subquery, rti, clause, &safetyInfo))
2234
6
      {
2235
        /* Push it down */
2236
6
        subquery_push_qual(subquery, rte, rti, clause);
2237
6
      }
2238
0
      else
2239
0
      {
2240
        /* Keep it in the upper query */
2241
0
        upperrestrictlist = lappend(upperrestrictlist, rinfo);
2242
0
      }
2243
6
    }
2244
6
    rel->baserestrictinfo = upperrestrictlist;
2245
    /* We don't bother recomputing baserestrict_min_security */
2246
6
  }
2247
2248
1.60k
  pfree(safetyInfo.unsafeColumns);
2249
2250
  /*
2251
   * The upper query might not use all the subquery's output columns; if
2252
   * not, we can simplify.
2253
   */
2254
1.60k
  remove_unused_subquery_outputs(subquery, rel);
2255
2256
  /*
2257
   * We can safely pass the outer tuple_fraction down to the subquery if the
2258
   * outer level has no joining, aggregation, or sorting to do. Otherwise
2259
   * we'd better tell the subquery to plan for full retrieval. (XXX This
2260
   * could probably be made more intelligent ...)
2261
   */
2262
1.60k
  if (parse->hasAggs ||
2263
1.58k
    parse->groupClause ||
2264
1.58k
    parse->groupingSets ||
2265
1.58k
    parse->havingQual ||
2266
1.58k
    parse->distinctClause ||
2267
1.58k
    parse->sortClause ||
2268
1.52k
    has_multiple_baserels(root))
2269
90
    tuple_fraction = 0.0;  /* default case */
2270
1.51k
  else
2271
1.51k
    tuple_fraction = root->tuple_fraction;
2272
2273
  /* plan_params should not be in use in current query level */
2274
1.60k
  Assert(root->plan_params == NIL);
2275
2276
  /* Generate a subroot and Paths for the subquery */
2277
1.60k
  rel->subroot = subquery_planner(root->glob, subquery,
2278
1.60k
                  root,
2279
1.60k
                  false, tuple_fraction);
2280
2281
  /* Isolate the params needed by this specific subplan */
2282
1.60k
  rel->subplan_params = root->plan_params;
2283
1.60k
  root->plan_params = NIL;
2284
2285
  /*
2286
   * It's possible that constraint exclusion proved the subquery empty. If
2287
   * so, it's desirable to produce an unadorned dummy path so that we will
2288
   * recognize appropriate optimizations at this query level.
2289
   */
2290
1.60k
  sub_final_rel = fetch_upper_rel(rel->subroot, UPPERREL_FINAL, NULL);
2291
2292
1.60k
  if (IS_DUMMY_REL(sub_final_rel))
2293
2
  {
2294
2
    set_dummy_rel_pathlist(rel);
2295
2
    return;
2296
2
  }
2297
2298
  /*
2299
   * Mark rel with estimated output rows, width, etc.  Note that we have to
2300
   * do this before generating outer-query paths, else cost_subqueryscan is
2301
   * not happy.
2302
   */
2303
1.60k
  set_subquery_size_estimates(root, rel);
2304
2305
  /*
2306
   * For each Path that subquery_planner produced, make a SubqueryScanPath
2307
   * in the outer query.
2308
   */
2309
1.60k
  foreach(lc, sub_final_rel->pathlist)
2310
1.60k
  {
2311
1.60k
    Path     *subpath = (Path *) lfirst(lc);
2312
1.60k
    List     *pathkeys;
2313
2314
    /* Convert subpath's pathkeys to outer representation */
2315
1.60k
    pathkeys = convert_subquery_pathkeys(root,
2316
1.60k
                       rel,
2317
1.60k
                       subpath->pathkeys,
2318
1.60k
                       make_tlist_from_pathtarget(subpath->pathtarget));
2319
2320
    /* Generate outer path using this subpath */
2321
1.60k
    add_path(rel, (Path *)
2322
1.60k
         create_subqueryscan_path(root, rel, subpath,
2323
1.60k
                      pathkeys, required_outer));
2324
1.60k
  }
2325
2326
  /* If outer rel allows parallelism, do same for partial paths. */
2327
1.60k
  if (rel->consider_parallel && bms_is_empty(required_outer))
2328
86
  {
2329
    /* If consider_parallel is false, there should be no partial paths. */
2330
86
    Assert(sub_final_rel->consider_parallel ||
2331
86
         sub_final_rel->partial_pathlist == NIL);
2332
2333
    /* Same for partial paths. */
2334
86
    foreach(lc, sub_final_rel->partial_pathlist)
2335
0
    {
2336
0
      Path     *subpath = (Path *) lfirst(lc);
2337
0
      List     *pathkeys;
2338
2339
      /* Convert subpath's pathkeys to outer representation */
2340
0
      pathkeys = convert_subquery_pathkeys(root,
2341
0
                         rel,
2342
0
                         subpath->pathkeys,
2343
0
                         make_tlist_from_pathtarget(subpath->pathtarget));
2344
2345
      /* Generate outer path using this subpath */
2346
0
      add_partial_path(rel, (Path *)
2347
0
               create_subqueryscan_path(root, rel, subpath,
2348
0
                            pathkeys,
2349
0
                            required_outer));
2350
0
    }
2351
86
  }
2352
1.60k
}
2353
2354
/*
2355
 * set_function_pathlist
2356
 *    Build the (single) access path for a function RTE
2357
 */
2358
static void
2359
set_function_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
2360
1.65k
{
2361
1.65k
  Relids    required_outer;
2362
1.65k
  List     *pathkeys = NIL;
2363
2364
  /*
2365
   * We don't support pushing join clauses into the quals of a function
2366
   * scan, but it could still have required parameterization due to LATERAL
2367
   * refs in the function expression.
2368
   */
2369
1.65k
  required_outer = rel->lateral_relids;
2370
2371
  /*
2372
   * The result is considered unordered unless ORDINALITY was used, in which
2373
   * case it is ordered by the ordinal column (the last one).  See if we
2374
   * care, by checking for uses of that Var in equivalence classes.
2375
   */
2376
1.65k
  if (rte->funcordinality)
2377
0
  {
2378
0
    AttrNumber  ordattno = rel->max_attr;
2379
0
    Var      *var = NULL;
2380
0
    ListCell   *lc;
2381
2382
    /*
2383
     * Is there a Var for it in rel's targetlist?  If not, the query did
2384
     * not reference the ordinality column, or at least not in any way
2385
     * that would be interesting for sorting.
2386
     */
2387
0
    foreach(lc, rel->reltarget->exprs)
2388
0
    {
2389
0
      Var      *node = (Var *) lfirst(lc);
2390
2391
      /* checking varno/varlevelsup is just paranoia */
2392
0
      if (IsA(node, Var) &&
2393
0
        node->varattno == ordattno &&
2394
0
        node->varno == rel->relid &&
2395
0
        node->varlevelsup == 0)
2396
0
      {
2397
0
        var = node;
2398
0
        break;
2399
0
      }
2400
0
    }
2401
2402
    /*
2403
     * Try to build pathkeys for this Var with int8 sorting.  We tell
2404
     * build_expression_pathkey not to build any new equivalence class; if
2405
     * the Var isn't already mentioned in some EC, it means that nothing
2406
     * cares about the ordering.
2407
     */
2408
0
    if (var)
2409
0
      pathkeys = build_expression_pathkey(root,
2410
0
                        (Expr *) var,
2411
0
                        NULL, /* below outer joins */
2412
0
                        Int8LessOperator,
2413
0
                        rel->relids,
2414
0
                        false);
2415
0
  }
2416
2417
  /* Generate appropriate path */
2418
1.65k
  add_path(rel, create_functionscan_path(root, rel,
2419
1.65k
                       pathkeys, required_outer));
2420
1.65k
}
2421
2422
/*
2423
 * set_values_pathlist
2424
 *    Build the (single) access path for a VALUES RTE
2425
 */
2426
static void
2427
set_values_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
2428
2.10k
{
2429
2.10k
  Relids    required_outer;
2430
2431
  /*
2432
   * We don't support pushing join clauses into the quals of a values scan,
2433
   * but it could still have required parameterization due to LATERAL refs
2434
   * in the values expressions.
2435
   */
2436
2.10k
  required_outer = rel->lateral_relids;
2437
2438
  /* Generate appropriate path */
2439
2.10k
  add_path(rel, create_valuesscan_path(root, rel, required_outer));
2440
2.10k
}
2441
2442
/*
2443
 * set_tablefunc_pathlist
2444
 *    Build the (single) access path for a table func RTE
2445
 */
2446
static void
2447
set_tablefunc_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
2448
0
{
2449
0
  Relids    required_outer;
2450
2451
  /*
2452
   * We don't support pushing join clauses into the quals of a tablefunc
2453
   * scan, but it could still have required parameterization due to LATERAL
2454
   * refs in the function expression.
2455
   */
2456
0
  required_outer = rel->lateral_relids;
2457
2458
  /* Generate appropriate path */
2459
0
  add_path(rel, create_tablefuncscan_path(root, rel,
2460
0
                      required_outer));
2461
0
}
2462
2463
/*
2464
 * set_cte_pathlist
2465
 *    Build the (single) access path for a non-self-reference CTE RTE
2466
 *
2467
 * There's no need for a separate set_cte_size phase, since we don't
2468
 * support join-qual-parameterized paths for CTEs.
2469
 */
2470
static void
2471
set_cte_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
2472
10.1k
{
2473
10.1k
  Plan     *cteplan;
2474
10.1k
  PlannerInfo *cteroot;
2475
10.1k
  Index   levelsup;
2476
10.1k
  int     ndx;
2477
10.1k
  ListCell   *lc;
2478
10.1k
  int     plan_id;
2479
10.1k
  Relids    required_outer;
2480
2481
  /*
2482
   * Find the referenced CTE, and locate the plan previously made for it.
2483
   */
2484
10.1k
  levelsup = rte->ctelevelsup;
2485
10.1k
  cteroot = root;
2486
19.7k
  while (levelsup-- > 0)
2487
9.57k
  {
2488
9.57k
    cteroot = cteroot->parent_root;
2489
9.57k
    if (!cteroot)     /* shouldn't happen */
2490
0
      elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
2491
9.57k
  }
2492
2493
  /*
2494
   * Note: cte_plan_ids can be shorter than cteList, if we are still working
2495
   * on planning the CTEs (ie, this is a side-reference from another CTE).
2496
   * So we mustn't use forboth here.
2497
   */
2498
10.1k
  ndx = 0;
2499
10.1k
  foreach(lc, cteroot->parse->cteList)
2500
12.0k
  {
2501
12.0k
    CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
2502
2503
12.0k
    if (strcmp(cte->ctename, rte->ctename) == 0)
2504
10.1k
      break;
2505
1.81k
    ndx++;
2506
1.81k
  }
2507
10.1k
  if (lc == NULL)       /* shouldn't happen */
2508
0
    elog(ERROR, "could not find CTE \"%s\"", rte->ctename);
2509
10.1k
  if (ndx >= list_length(cteroot->cte_plan_ids))
2510
0
    elog(ERROR, "could not find plan for CTE \"%s\"", rte->ctename);
2511
10.1k
  plan_id = list_nth_int(cteroot->cte_plan_ids, ndx);
2512
10.1k
  Assert(plan_id > 0);
2513
10.1k
  cteplan = (Plan *) list_nth(root->glob->subplans, plan_id - 1);
2514
2515
  /* Mark rel with estimated output rows, width, etc */
2516
10.1k
  set_cte_size_estimates(root, rel, cteplan->plan_rows);
2517
2518
  /*
2519
   * We don't support pushing join clauses into the quals of a CTE scan, but
2520
   * it could still have required parameterization due to LATERAL refs in
2521
   * its tlist.
2522
   */
2523
10.1k
  required_outer = rel->lateral_relids;
2524
2525
  /* Generate appropriate path */
2526
10.1k
  add_path(rel, create_ctescan_path(root, rel, required_outer));
2527
10.1k
}
2528
2529
/*
2530
 * set_namedtuplestore_pathlist
2531
 *    Build the (single) access path for a named tuplestore RTE
2532
 *
2533
 * There's no need for a separate set_namedtuplestore_size phase, since we
2534
 * don't support join-qual-parameterized paths for tuplestores.
2535
 */
2536
static void
2537
set_namedtuplestore_pathlist(PlannerInfo *root, RelOptInfo *rel,
2538
               RangeTblEntry *rte)
2539
0
{
2540
0
  Relids    required_outer;
2541
2542
  /* Mark rel with estimated output rows, width, etc */
2543
0
  set_namedtuplestore_size_estimates(root, rel);
2544
2545
  /*
2546
   * We don't support pushing join clauses into the quals of a tuplestore
2547
   * scan, but it could still have required parameterization due to LATERAL
2548
   * refs in its tlist.
2549
   */
2550
0
  required_outer = rel->lateral_relids;
2551
2552
  /* Generate appropriate path */
2553
0
  add_path(rel, create_namedtuplestorescan_path(root, rel, required_outer));
2554
2555
  /* Select cheapest path (pretty easy in this case...) */
2556
0
  set_cheapest(rel);
2557
0
}
2558
2559
/*
2560
 * set_worktable_pathlist
2561
 *    Build the (single) access path for a self-reference CTE RTE
2562
 *
2563
 * There's no need for a separate set_worktable_size phase, since we don't
2564
 * support join-qual-parameterized paths for CTEs.
2565
 */
2566
static void
2567
set_worktable_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
2568
0
{
2569
0
  Path     *ctepath;
2570
0
  PlannerInfo *cteroot;
2571
0
  Index   levelsup;
2572
0
  Relids    required_outer;
2573
2574
  /*
2575
   * We need to find the non-recursive term's path, which is in the plan
2576
   * level that's processing the recursive UNION, which is one level *below*
2577
   * where the CTE comes from.
2578
   */
2579
0
  levelsup = rte->ctelevelsup;
2580
0
  if (levelsup == 0)     /* shouldn't happen */
2581
0
    elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
2582
0
  levelsup--;
2583
0
  cteroot = root;
2584
0
  while (levelsup-- > 0)
2585
0
  {
2586
0
    cteroot = cteroot->parent_root;
2587
0
    if (!cteroot)     /* shouldn't happen */
2588
0
      elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
2589
0
  }
2590
0
  ctepath = cteroot->non_recursive_path;
2591
0
  if (!ctepath)       /* shouldn't happen */
2592
0
    elog(ERROR, "could not find path for CTE \"%s\"", rte->ctename);
2593
2594
  /* Mark rel with estimated output rows, width, etc */
2595
0
  set_cte_size_estimates(root, rel, ctepath->rows);
2596
2597
  /*
2598
   * We don't support pushing join clauses into the quals of a worktable
2599
   * scan, but it could still have required parameterization due to LATERAL
2600
   * refs in its tlist.  (I'm not sure this is actually possible given the
2601
   * restrictions on recursive references, but it's easy enough to support.)
2602
   */
2603
0
  required_outer = rel->lateral_relids;
2604
2605
  /* Generate appropriate path */
2606
0
  add_path(rel, create_worktablescan_path(root, rel, required_outer));
2607
0
}
2608
2609
/*
2610
 * generate_gather_paths
2611
 *    Generate parallel access paths for a relation by pushing a Gather or
2612
 *    Gather Merge on top of a partial path.
2613
 *
2614
 * This must not be called until after we're done creating all partial paths
2615
 * for the specified relation.  (Otherwise, add_partial_path might delete a
2616
 * path that some GatherPath or GatherMergePath has a reference to.)
2617
 *
2618
 * If we're generating paths for a scan or join relation, override_rows will
2619
 * be false, and we'll just use the relation's size estimate.  When we're
2620
 * being called for a partially-grouped path, though, we need to override
2621
 * the rowcount estimate.  (It's not clear that the particular value we're
2622
 * using here is actually best, but the underlying rel has no estimate so
2623
 * we must do something.)
2624
 */
2625
void
2626
generate_gather_paths(PlannerInfo *root, RelOptInfo *rel, bool override_rows)
2627
15.3k
{
2628
15.3k
  Path     *cheapest_partial_path;
2629
15.3k
  Path     *simple_gather_path;
2630
15.3k
  ListCell   *lc;
2631
15.3k
  double    rows;
2632
15.3k
  double     *rowsp = NULL;
2633
2634
  /* If there are no partial paths, there's nothing to do here. */
2635
15.3k
  if (rel->partial_pathlist == NIL)
2636
15.3k
    return;
2637
2638
  /* Should we override the rel's rowcount estimate? */
2639
0
  if (override_rows)
2640
0
    rowsp = &rows;
2641
2642
  /*
2643
   * The output of Gather is always unsorted, so there's only one partial
2644
   * path of interest: the cheapest one.  That will be the one at the front
2645
   * of partial_pathlist because of the way add_partial_path works.
2646
   */
2647
0
  cheapest_partial_path = linitial(rel->partial_pathlist);
2648
0
  rows =
2649
0
    cheapest_partial_path->rows * cheapest_partial_path->parallel_workers;
2650
0
  simple_gather_path = (Path *)
2651
0
    create_gather_path(root, rel, cheapest_partial_path, rel->reltarget,
2652
0
               NULL, rowsp);
2653
0
  add_path(rel, simple_gather_path);
2654
2655
  /*
2656
   * For each useful ordering, we can consider an order-preserving Gather
2657
   * Merge.
2658
   */
2659
0
  foreach(lc, rel->partial_pathlist)
2660
0
  {
2661
0
    Path     *subpath = (Path *) lfirst(lc);
2662
0
    GatherMergePath *path;
2663
2664
0
    if (subpath->pathkeys == NIL)
2665
0
      continue;
2666
2667
0
    rows = subpath->rows * subpath->parallel_workers;
2668
0
    path = create_gather_merge_path(root, rel, subpath, rel->reltarget,
2669
0
                    subpath->pathkeys, NULL, rowsp);
2670
0
    add_path(rel, &path->path);
2671
0
  }
2672
0
}
2673
2674
/*
2675
 * make_rel_from_joinlist
2676
 *    Build access paths using a "joinlist" to guide the join path search.
2677
 *
2678
 * See comments for deconstruct_jointree() for definition of the joinlist
2679
 * data structure.
2680
 */
2681
static RelOptInfo *
2682
make_rel_from_joinlist(PlannerInfo *root, List *joinlist)
2683
70.8k
{
2684
70.8k
  int     levels_needed;
2685
70.8k
  List     *initial_rels;
2686
70.8k
  ListCell   *jl;
2687
2688
  /*
2689
   * Count the number of child joinlist nodes.  This is the depth of the
2690
   * dynamic-programming algorithm we must employ to consider all ways of
2691
   * joining the child nodes.
2692
   */
2693
70.8k
  levels_needed = list_length(joinlist);
2694
2695
70.8k
  if (levels_needed <= 0)
2696
0
    return NULL;     /* nothing to do? */
2697
2698
  /*
2699
   * Construct a list of rels corresponding to the child joinlist nodes.
2700
   * This may contain both base rels and rels constructed according to
2701
   * sub-joinlists.
2702
   */
2703
70.8k
  initial_rels = NIL;
2704
70.8k
  foreach(jl, joinlist)
2705
75.6k
  {
2706
75.6k
    Node     *jlnode = (Node *) lfirst(jl);
2707
75.6k
    RelOptInfo *thisrel;
2708
2709
75.6k
    if (IsA(jlnode, RangeTblRef))
2710
75.3k
    {
2711
75.3k
      int     varno = ((RangeTblRef *) jlnode)->rtindex;
2712
2713
75.3k
      thisrel = find_base_rel(root, varno);
2714
75.3k
    }
2715
218
    else if (IsA(jlnode, List))
2716
219
    {
2717
      /* Recurse to handle subproblem */
2718
219
      thisrel = make_rel_from_joinlist(root, (List *) jlnode);
2719
219
    }
2720
18.4E
    else
2721
18.4E
    {
2722
18.4E
      elog(ERROR, "unrecognized joinlist node type: %d",
2723
18.4E
         (int) nodeTag(jlnode));
2724
18.4E
      thisrel = NULL;   /* keep compiler quiet */
2725
18.4E
    }
2726
2727
75.6k
    initial_rels = lappend(initial_rels, thisrel);
2728
75.6k
  }
2729
2730
70.8k
  if (levels_needed == 1)
2731
66.5k
  {
2732
    /*
2733
     * Single joinlist node, so we're done.
2734
     */
2735
66.5k
    return (RelOptInfo *) linitial(initial_rels);
2736
66.5k
  }
2737
4.33k
  else
2738
4.33k
  {
2739
    /*
2740
     * Consider the different orders in which we could join the rels,
2741
     * using a plugin, GEQO, or the regular join search code.
2742
     *
2743
     * We put the initial_rels list into a PlannerInfo field because
2744
     * has_legal_joinclause() needs to look at it (ugly :-().
2745
     */
2746
4.33k
    root->initial_rels = initial_rels;
2747
2748
4.33k
    if (join_search_hook)
2749
4.32k
      return (*join_search_hook) (root, levels_needed, initial_rels);
2750
3
    else if (enable_geqo && levels_needed >= geqo_threshold)
2751
0
      return geqo(root, levels_needed, initial_rels);
2752
3
    else
2753
3
      return standard_join_search(root, levels_needed, initial_rels);
2754
4.33k
  }
2755
70.8k
}
2756
2757
/*
2758
 * standard_join_search
2759
 *    Find possible joinpaths for a query by successively finding ways
2760
 *    to join component relations into join relations.
2761
 *
2762
 * 'levels_needed' is the number of iterations needed, ie, the number of
2763
 *    independent jointree items in the query.  This is > 1.
2764
 *
2765
 * 'initial_rels' is a list of RelOptInfo nodes for each independent
2766
 *    jointree item.  These are the components to be joined together.
2767
 *    Note that levels_needed == list_length(initial_rels).
2768
 *
2769
 * Returns the final level of join relations, i.e., the relation that is
2770
 * the result of joining all the original relations together.
2771
 * At least one implementation path must be provided for this relation and
2772
 * all required sub-relations.
2773
 *
2774
 * To support loadable plugins that modify planner behavior by changing the
2775
 * join searching algorithm, we provide a hook variable that lets a plugin
2776
 * replace or supplement this function.  Any such hook must return the same
2777
 * final join relation as the standard code would, but it might have a
2778
 * different set of implementation paths attached, and only the sub-joinrels
2779
 * needed for these paths need have been instantiated.
2780
 *
2781
 * Note to plugin authors: the functions invoked during standard_join_search()
2782
 * modify root->join_rel_list and root->join_rel_hash.  If you want to do more
2783
 * than one join-order search, you'll probably need to save and restore the
2784
 * original states of those data structures.  See geqo_eval() for an example.
2785
 */
2786
RelOptInfo *
2787
standard_join_search(PlannerInfo *root, int levels_needed, List *initial_rels)
2788
4.32k
{
2789
4.32k
  int     lev;
2790
4.32k
  RelOptInfo *rel;
2791
2792
  /*
2793
   * This function cannot be invoked recursively within any one planning
2794
   * problem, so join_rel_level[] can't be in use already.
2795
   */
2796
4.32k
  Assert(root->join_rel_level == NULL);
2797
2798
  /*
2799
   * We employ a simple "dynamic programming" algorithm: we first find all
2800
   * ways to build joins of two jointree items, then all ways to build joins
2801
   * of three items (from two-item joins and single items), then four-item
2802
   * joins, and so on until we have considered all ways to join all the
2803
   * items into one rel.
2804
   *
2805
   * root->join_rel_level[j] is a list of all the j-item rels.  Initially we
2806
   * set root->join_rel_level[1] to represent all the single-jointree-item
2807
   * relations.
2808
   */
2809
4.32k
  root->join_rel_level = (List **) palloc0((levels_needed + 1) * sizeof(List *));
2810
2811
4.32k
  root->join_rel_level[1] = initial_rels;
2812
2813
9.08k
  for (lev = 2; lev <= levels_needed; lev++)
2814
4.75k
  {
2815
4.75k
    ListCell   *lc;
2816
2817
    /*
2818
     * Determine all possible pairs of relations to be joined at this
2819
     * level, and build paths for making each one from every available
2820
     * pair of lower-level relations.
2821
     */
2822
4.75k
    join_search_one_level(root, lev);
2823
2824
    /*
2825
     * Run generate_partitionwise_join_paths() and generate_gather_paths()
2826
     * for each just-processed joinrel.  We could not do this earlier
2827
     * because both regular and partial paths can get added to a
2828
     * particular joinrel at multiple times within join_search_one_level.
2829
     *
2830
     * After that, we're done creating paths for the joinrel, so run
2831
     * set_cheapest().
2832
     */
2833
4.75k
    foreach(lc, root->join_rel_level[lev])
2834
5.56k
    {
2835
5.56k
      rel = (RelOptInfo *) lfirst(lc);
2836
2837
      /* Create paths for partitionwise joins. */
2838
5.56k
      generate_partitionwise_join_paths(root, rel);
2839
2840
      /*
2841
       * Except for the topmost scan/join rel, consider gathering
2842
       * partial paths.  We'll do the same for the topmost scan/join rel
2843
       * once we know the final targetlist (see grouping_planner).
2844
       */
2845
5.56k
      if (lev < levels_needed)
2846
1.23k
        generate_gather_paths(root, rel, false);
2847
2848
      /* Find and save the cheapest paths for this rel */
2849
5.56k
      set_cheapest(rel);
2850
2851
#ifdef OPTIMIZER_DEBUG
2852
      debug_print_rel(root, rel);
2853
#endif
2854
5.56k
    }
2855
4.75k
  }
2856
2857
  /*
2858
   * We should have a single rel at the final level.
2859
   */
2860
4.32k
  if (root->join_rel_level[levels_needed] == NIL)
2861
0
    elog(ERROR, "failed to build any %d-way joins", levels_needed);
2862
4.32k
  Assert(list_length(root->join_rel_level[levels_needed]) == 1);
2863
2864
4.32k
  rel = (RelOptInfo *) linitial(root->join_rel_level[levels_needed]);
2865
2866
4.32k
  root->join_rel_level = NULL;
2867
2868
4.32k
  return rel;
2869
4.32k
}
2870
2871
/*****************************************************************************
2872
 *      PUSHING QUALS DOWN INTO SUBQUERIES
2873
 *****************************************************************************/
2874
2875
/*
2876
 * subquery_is_pushdown_safe - is a subquery safe for pushing down quals?
2877
 *
2878
 * subquery is the particular component query being checked.  topquery
2879
 * is the top component of a set-operations tree (the same Query if no
2880
 * set-op is involved).
2881
 *
2882
 * Conditions checked here:
2883
 *
2884
 * 1. If the subquery has a LIMIT clause, we must not push down any quals,
2885
 * since that could change the set of rows returned.
2886
 *
2887
 * 2. If the subquery contains EXCEPT or EXCEPT ALL set ops we cannot push
2888
 * quals into it, because that could change the results.
2889
 *
2890
 * 3. If the subquery uses DISTINCT, we cannot push volatile quals into it.
2891
 * This is because upper-level quals should semantically be evaluated only
2892
 * once per distinct row, not once per original row, and if the qual is
2893
 * volatile then extra evaluations could change the results.  (This issue
2894
 * does not apply to other forms of aggregation such as GROUP BY, because
2895
 * when those are present we push into HAVING not WHERE, so that the quals
2896
 * are still applied after aggregation.)
2897
 *
2898
 * 4. If the subquery contains window functions, we cannot push volatile quals
2899
 * into it.  The issue here is a bit different from DISTINCT: a volatile qual
2900
 * might succeed for some rows of a window partition and fail for others,
2901
 * thereby changing the partition contents and thus the window functions'
2902
 * results for rows that remain.
2903
 *
2904
 * 5. If the subquery contains any set-returning functions in its targetlist,
2905
 * we cannot push volatile quals into it.  That would push them below the SRFs
2906
 * and thereby change the number of times they are evaluated.  Also, a
2907
 * volatile qual could succeed for some SRF output rows and fail for others,
2908
 * a behavior that cannot occur if it's evaluated before SRF expansion.
2909
 *
2910
 * In addition, we make several checks on the subquery's output columns to see
2911
 * if it is safe to reference them in pushed-down quals.  If output column k
2912
 * is found to be unsafe to reference, we set safetyInfo->unsafeColumns[k]
2913
 * to true, but we don't reject the subquery overall since column k might not
2914
 * be referenced by some/all quals.  The unsafeColumns[] array will be
2915
 * consulted later by qual_is_pushdown_safe().  It's better to do it this way
2916
 * than to make the checks directly in qual_is_pushdown_safe(), because when
2917
 * the subquery involves set operations we have to check the output
2918
 * expressions in each arm of the set op.
2919
 *
2920
 * Note: pushing quals into a DISTINCT subquery is theoretically dubious:
2921
 * we're effectively assuming that the quals cannot distinguish values that
2922
 * the DISTINCT's equality operator sees as equal, yet there are many
2923
 * counterexamples to that assumption.  However use of such a qual with a
2924
 * DISTINCT subquery would be unsafe anyway, since there's no guarantee which
2925
 * "equal" value will be chosen as the output value by the DISTINCT operation.
2926
 * So we don't worry too much about that.  Another objection is that if the
2927
 * qual is expensive to evaluate, running it for each original row might cost
2928
 * more than we save by eliminating rows before the DISTINCT step.  But it
2929
 * would be very hard to estimate that at this stage, and in practice pushdown
2930
 * seldom seems to make things worse, so we ignore that problem too.
2931
 *
2932
 * Note: likewise, pushing quals into a subquery with window functions is a
2933
 * bit dubious: the quals might remove some rows of a window partition while
2934
 * leaving others, causing changes in the window functions' results for the
2935
 * surviving rows.  We insist that such a qual reference only partitioning
2936
 * columns, but again that only protects us if the qual does not distinguish
2937
 * values that the partitioning equality operator sees as equal.  The risks
2938
 * here are perhaps larger than for DISTINCT, since no de-duplication of rows
2939
 * occurs and thus there is no theoretical problem with such a qual.  But
2940
 * we'll do this anyway because the potential performance benefits are very
2941
 * large, and we've seen no field complaints about the longstanding comparable
2942
 * behavior with DISTINCT.
2943
 */
2944
static bool
2945
subquery_is_pushdown_safe(Query *subquery, Query *topquery,
2946
              pushdown_safety_info *safetyInfo)
2947
8
{
2948
8
  SetOperationStmt *topop;
2949
2950
  /* Check point 1 */
2951
8
  if (subquery->limitOffset != NULL || subquery->limitCount != NULL)
2952
2
    return false;
2953
2954
  /* Check points 3, 4, and 5 */
2955
6
  if (subquery->distinctClause ||
2956
6
    subquery->hasWindowFuncs ||
2957
6
    subquery->hasTargetSRFs)
2958
0
    safetyInfo->unsafeVolatile = true;
2959
2960
  /*
2961
   * If we're at a leaf query, check for unsafe expressions in its target
2962
   * list, and mark any unsafe ones in unsafeColumns[].  (Non-leaf nodes in
2963
   * setop trees have only simple Vars in their tlists, so no need to check
2964
   * them.)
2965
   */
2966
6
  if (subquery->setOperations == NULL)
2967
6
    check_output_expressions(subquery, safetyInfo);
2968
2969
  /* Are we at top level, or looking at a setop component? */
2970
6
  if (subquery == topquery)
2971
6
  {
2972
    /* Top level, so check any component queries */
2973
6
    if (subquery->setOperations != NULL)
2974
0
      if (!recurse_pushdown_safe(subquery->setOperations, topquery,
2975
0
                     safetyInfo))
2976
0
        return false;
2977
0
  }
2978
0
  else
2979
0
  {
2980
    /* Setop component must not have more components (too weird) */
2981
0
    if (subquery->setOperations != NULL)
2982
0
      return false;
2983
    /* Check whether setop component output types match top level */
2984
0
    topop = castNode(SetOperationStmt, topquery->setOperations);
2985
0
    Assert(topop);
2986
0
    compare_tlist_datatypes(subquery->targetList,
2987
0
                topop->colTypes,
2988
0
                safetyInfo);
2989
0
  }
2990
6
  return true;
2991
6
}
2992
2993
/*
2994
 * Helper routine to recurse through setOperations tree
2995
 */
2996
static bool
2997
recurse_pushdown_safe(Node *setOp, Query *topquery,
2998
            pushdown_safety_info *safetyInfo)
2999
0
{
3000
0
  if (IsA(setOp, RangeTblRef))
3001
0
  {
3002
0
    RangeTblRef *rtr = (RangeTblRef *) setOp;
3003
0
    RangeTblEntry *rte = rt_fetch(rtr->rtindex, topquery->rtable);
3004
0
    Query    *subquery = rte->subquery;
3005
3006
0
    Assert(subquery != NULL);
3007
0
    return subquery_is_pushdown_safe(subquery, topquery, safetyInfo);
3008
0
  }
3009
0
  else if (IsA(setOp, SetOperationStmt))
3010
0
  {
3011
0
    SetOperationStmt *op = (SetOperationStmt *) setOp;
3012
3013
    /* EXCEPT is no good (point 2 for subquery_is_pushdown_safe) */
3014
0
    if (op->op == SETOP_EXCEPT)
3015
0
      return false;
3016
    /* Else recurse */
3017
0
    if (!recurse_pushdown_safe(op->larg, topquery, safetyInfo))
3018
0
      return false;
3019
0
    if (!recurse_pushdown_safe(op->rarg, topquery, safetyInfo))
3020
0
      return false;
3021
0
  }
3022
0
  else
3023
0
  {
3024
0
    elog(ERROR, "unrecognized node type: %d",
3025
0
       (int) nodeTag(setOp));
3026
0
  }
3027
0
  return true;
3028
0
}
3029
3030
/*
3031
 * check_output_expressions - check subquery's output expressions for safety
3032
 *
3033
 * There are several cases in which it's unsafe to push down an upper-level
3034
 * qual if it references a particular output column of a subquery.  We check
3035
 * each output column of the subquery and set unsafeColumns[k] to true if
3036
 * that column is unsafe for a pushed-down qual to reference.  The conditions
3037
 * checked here are:
3038
 *
3039
 * 1. We must not push down any quals that refer to subselect outputs that
3040
 * return sets, else we'd introduce functions-returning-sets into the
3041
 * subquery's WHERE/HAVING quals.
3042
 *
3043
 * 2. We must not push down any quals that refer to subselect outputs that
3044
 * contain volatile functions, for fear of introducing strange results due
3045
 * to multiple evaluation of a volatile function.
3046
 *
3047
 * 3. If the subquery uses DISTINCT ON, we must not push down any quals that
3048
 * refer to non-DISTINCT output columns, because that could change the set
3049
 * of rows returned.  (This condition is vacuous for DISTINCT, because then
3050
 * there are no non-DISTINCT output columns, so we needn't check.  Note that
3051
 * subquery_is_pushdown_safe already reported that we can't use volatile
3052
 * quals if there's DISTINCT or DISTINCT ON.)
3053
 *
3054
 * 4. If the subquery has any window functions, we must not push down quals
3055
 * that reference any output columns that are not listed in all the subquery's
3056
 * window PARTITION BY clauses.  We can push down quals that use only
3057
 * partitioning columns because they should succeed or fail identically for
3058
 * every row of any one window partition, and totally excluding some
3059
 * partitions will not change a window function's results for remaining
3060
 * partitions.  (Again, this also requires nonvolatile quals, but
3061
 * subquery_is_pushdown_safe handles that.)
3062
 */
3063
static void
3064
check_output_expressions(Query *subquery, pushdown_safety_info *safetyInfo)
3065
6
{
3066
6
  ListCell   *lc;
3067
3068
6
  foreach(lc, subquery->targetList)
3069
36
  {
3070
36
    TargetEntry *tle = (TargetEntry *) lfirst(lc);
3071
3072
36
    if (tle->resjunk)
3073
0
      continue;     /* ignore resjunk columns */
3074
3075
    /* We need not check further if output col is already known unsafe */
3076
36
    if (safetyInfo->unsafeColumns[tle->resno])
3077
0
      continue;
3078
3079
    /* Functions returning sets are unsafe (point 1) */
3080
36
    if (subquery->hasTargetSRFs &&
3081
0
      expression_returns_set((Node *) tle->expr))
3082
0
    {
3083
0
      safetyInfo->unsafeColumns[tle->resno] = true;
3084
0
      continue;
3085
0
    }
3086
3087
    /* Volatile functions are unsafe (point 2) */
3088
36
    if (contain_volatile_functions((Node *) tle->expr))
3089
2
    {
3090
2
      safetyInfo->unsafeColumns[tle->resno] = true;
3091
2
      continue;
3092
2
    }
3093
3094
    /* If subquery uses DISTINCT ON, check point 3 */
3095
34
    if (subquery->hasDistinctOn &&
3096
0
      !targetIsInSortList(tle, InvalidOid, subquery->distinctClause))
3097
0
    {
3098
      /* non-DISTINCT column, so mark it unsafe */
3099
0
      safetyInfo->unsafeColumns[tle->resno] = true;
3100
0
      continue;
3101
0
    }
3102
3103
    /* If subquery uses window functions, check point 4 */
3104
34
    if (subquery->hasWindowFuncs &&
3105
0
      !targetIsInAllPartitionLists(tle, subquery))
3106
0
    {
3107
      /* not present in all PARTITION BY clauses, so mark it unsafe */
3108
0
      safetyInfo->unsafeColumns[tle->resno] = true;
3109
0
      continue;
3110
0
    }
3111
34
  }
3112
6
}
3113
3114
/*
3115
 * For subqueries using UNION/UNION ALL/INTERSECT/INTERSECT ALL, we can
3116
 * push quals into each component query, but the quals can only reference
3117
 * subquery columns that suffer no type coercions in the set operation.
3118
 * Otherwise there are possible semantic gotchas.  So, we check the
3119
 * component queries to see if any of them have output types different from
3120
 * the top-level setop outputs.  unsafeColumns[k] is set true if column k
3121
 * has different type in any component.
3122
 *
3123
 * We don't have to care about typmods here: the only allowed difference
3124
 * between set-op input and output typmods is input is a specific typmod
3125
 * and output is -1, and that does not require a coercion.
3126
 *
3127
 * tlist is a subquery tlist.
3128
 * colTypes is an OID list of the top-level setop's output column types.
3129
 * safetyInfo->unsafeColumns[] is the result array.
3130
 */
3131
static void
3132
compare_tlist_datatypes(List *tlist, List *colTypes,
3133
            pushdown_safety_info *safetyInfo)
3134
0
{
3135
0
  ListCell   *l;
3136
0
  ListCell   *colType = list_head(colTypes);
3137
3138
0
  foreach(l, tlist)
3139
0
  {
3140
0
    TargetEntry *tle = (TargetEntry *) lfirst(l);
3141
3142
0
    if (tle->resjunk)
3143
0
      continue;     /* ignore resjunk columns */
3144
0
    if (colType == NULL)
3145
0
      elog(ERROR, "wrong number of tlist entries");
3146
0
    if (exprType((Node *) tle->expr) != lfirst_oid(colType))
3147
0
      safetyInfo->unsafeColumns[tle->resno] = true;
3148
0
    colType = lnext(colType);
3149
0
  }
3150
0
  if (colType != NULL)
3151
0
    elog(ERROR, "wrong number of tlist entries");
3152
0
}
3153
3154
/*
3155
 * targetIsInAllPartitionLists
3156
 *    True if the TargetEntry is listed in the PARTITION BY clause
3157
 *    of every window defined in the query.
3158
 *
3159
 * It would be safe to ignore windows not actually used by any window
3160
 * function, but it's not easy to get that info at this stage; and it's
3161
 * unlikely to be useful to spend any extra cycles getting it, since
3162
 * unreferenced window definitions are probably infrequent in practice.
3163
 */
3164
static bool
3165
targetIsInAllPartitionLists(TargetEntry *tle, Query *query)
3166
0
{
3167
0
  ListCell   *lc;
3168
3169
0
  foreach(lc, query->windowClause)
3170
0
  {
3171
0
    WindowClause *wc = (WindowClause *) lfirst(lc);
3172
3173
0
    if (!targetIsInSortList(tle, InvalidOid, wc->partitionClause))
3174
0
      return false;
3175
0
  }
3176
0
  return true;
3177
0
}
3178
3179
/*
3180
 * qual_is_pushdown_safe - is a particular qual safe to push down?
3181
 *
3182
 * qual is a restriction clause applying to the given subquery (whose RTE
3183
 * has index rti in the parent query).
3184
 *
3185
 * Conditions checked here:
3186
 *
3187
 * 1. The qual must not contain any SubPlans (mainly because I'm not sure
3188
 * it will work correctly: SubLinks will already have been transformed into
3189
 * SubPlans in the qual, but not in the subquery).  Note that SubLinks that
3190
 * transform to initplans are safe, and will be accepted here because what
3191
 * we'll see in the qual is just a Param referencing the initplan output.
3192
 *
3193
 * 2. If unsafeVolatile is set, the qual must not contain any volatile
3194
 * functions.
3195
 *
3196
 * 3. If unsafeLeaky is set, the qual must not contain any leaky functions
3197
 * that are passed Var nodes, and therefore might reveal values from the
3198
 * subquery as side effects.
3199
 *
3200
 * 4. The qual must not refer to the whole-row output of the subquery
3201
 * (since there is no easy way to name that within the subquery itself).
3202
 *
3203
 * 5. The qual must not refer to any subquery output columns that were
3204
 * found to be unsafe to reference by subquery_is_pushdown_safe().
3205
 */
3206
static bool
3207
qual_is_pushdown_safe(Query *subquery, Index rti, Node *qual,
3208
            pushdown_safety_info *safetyInfo)
3209
6
{
3210
6
  bool    safe = true;
3211
6
  List     *vars;
3212
6
  ListCell   *vl;
3213
3214
  /* Refuse subselects (point 1) */
3215
6
  if (contain_subplans(qual))
3216
0
    return false;
3217
3218
  /* Refuse volatile quals if we found they'd be unsafe (point 2) */
3219
6
  if (safetyInfo->unsafeVolatile &&
3220
0
    contain_volatile_functions(qual))
3221
0
    return false;
3222
3223
  /* Refuse leaky quals if told to (point 3) */
3224
6
  if (safetyInfo->unsafeLeaky &&
3225
4
    contain_leaked_vars(qual))
3226
0
    return false;
3227
3228
  /*
3229
   * It would be unsafe to push down window function calls, but at least for
3230
   * the moment we could never see any in a qual anyhow.  (The same applies
3231
   * to aggregates, which we check for in pull_var_clause below.)
3232
   */
3233
6
  Assert(!contain_window_function(qual));
3234
3235
  /*
3236
   * Examine all Vars used in clause; since it's a restriction clause, all
3237
   * such Vars must refer to subselect output columns.
3238
   */
3239
6
  vars = pull_var_clause(qual, PVC_INCLUDE_PLACEHOLDERS);
3240
6
  foreach(vl, vars)
3241
6
  {
3242
6
    Var      *var = (Var *) lfirst(vl);
3243
3244
    /*
3245
     * XXX Punt if we find any PlaceHolderVars in the restriction clause.
3246
     * It's not clear whether a PHV could safely be pushed down, and even
3247
     * less clear whether such a situation could arise in any cases of
3248
     * practical interest anyway.  So for the moment, just refuse to push
3249
     * down.
3250
     */
3251
6
    if (!IsA(var, Var))
3252
0
    {
3253
0
      safe = false;
3254
0
      break;
3255
0
    }
3256
3257
6
    Assert(var->varno == rti);
3258
6
    Assert(var->varattno >= 0);
3259
3260
    /* Check point 4 */
3261
6
    if (var->varattno == 0)
3262
0
    {
3263
0
      safe = false;
3264
0
      break;
3265
0
    }
3266
3267
    /* Check point 5 */
3268
6
    if (safetyInfo->unsafeColumns[var->varattno])
3269
0
    {
3270
0
      safe = false;
3271
0
      break;
3272
0
    }
3273
6
  }
3274
3275
6
  list_free(vars);
3276
3277
6
  return safe;
3278
6
}
3279
3280
/*
3281
 * subquery_push_qual - push down a qual that we have determined is safe
3282
 */
3283
static void
3284
subquery_push_qual(Query *subquery, RangeTblEntry *rte, Index rti, Node *qual)
3285
6
{
3286
6
  if (subquery->setOperations != NULL)
3287
0
  {
3288
    /* Recurse to push it separately to each component query */
3289
0
    recurse_push_qual(subquery->setOperations, subquery,
3290
0
              rte, rti, qual);
3291
0
  }
3292
6
  else
3293
6
  {
3294
    /*
3295
     * We need to replace Vars in the qual (which must refer to outputs of
3296
     * the subquery) with copies of the subquery's targetlist expressions.
3297
     * Note that at this point, any uplevel Vars in the qual should have
3298
     * been replaced with Params, so they need no work.
3299
     *
3300
     * This step also ensures that when we are pushing into a setop tree,
3301
     * each component query gets its own copy of the qual.
3302
     */
3303
6
    qual = ReplaceVarsFromTargetList(qual, rti, 0, rte,
3304
6
                     subquery->targetList,
3305
6
                     REPLACEVARS_REPORT_ERROR, 0,
3306
6
                     &subquery->hasSubLinks);
3307
3308
    /*
3309
     * Now attach the qual to the proper place: normally WHERE, but if the
3310
     * subquery uses grouping or aggregation, put it in HAVING (since the
3311
     * qual really refers to the group-result rows).
3312
     */
3313
6
    if (subquery->hasAggs || subquery->groupClause || subquery->groupingSets || subquery->havingQual)
3314
0
      subquery->havingQual = make_and_qual(subquery->havingQual, qual);
3315
6
    else
3316
6
      subquery->jointree->quals =
3317
6
        make_and_qual(subquery->jointree->quals, qual);
3318
3319
    /*
3320
     * We need not change the subquery's hasAggs or hasSubLinks flags,
3321
     * since we can't be pushing down any aggregates that weren't there
3322
     * before, and we don't push down subselects at all.
3323
     */
3324
6
  }
3325
6
}
3326
3327
/*
3328
 * Helper routine to recurse through setOperations tree
3329
 */
3330
static void
3331
recurse_push_qual(Node *setOp, Query *topquery,
3332
          RangeTblEntry *rte, Index rti, Node *qual)
3333
0
{
3334
0
  if (IsA(setOp, RangeTblRef))
3335
0
  {
3336
0
    RangeTblRef *rtr = (RangeTblRef *) setOp;
3337
0
    RangeTblEntry *subrte = rt_fetch(rtr->rtindex, topquery->rtable);
3338
0
    Query    *subquery = subrte->subquery;
3339
3340
0
    Assert(subquery != NULL);
3341
0
    subquery_push_qual(subquery, rte, rti, qual);
3342
0
  }
3343
0
  else if (IsA(setOp, SetOperationStmt))
3344
0
  {
3345
0
    SetOperationStmt *op = (SetOperationStmt *) setOp;
3346
3347
0
    recurse_push_qual(op->larg, topquery, rte, rti, qual);
3348
0
    recurse_push_qual(op->rarg, topquery, rte, rti, qual);
3349
0
  }
3350
0
  else
3351
0
  {
3352
0
    elog(ERROR, "unrecognized node type: %d",
3353
0
       (int) nodeTag(setOp));
3354
0
  }
3355
0
}
3356
3357
/*****************************************************************************
3358
 *      SIMPLIFYING SUBQUERY TARGETLISTS
3359
 *****************************************************************************/
3360
3361
/*
3362
 * remove_unused_subquery_outputs
3363
 *    Remove subquery targetlist items we don't need
3364
 *
3365
 * It's possible, even likely, that the upper query does not read all the
3366
 * output columns of the subquery.  We can remove any such outputs that are
3367
 * not needed by the subquery itself (e.g., as sort/group columns) and do not
3368
 * affect semantics otherwise (e.g., volatile functions can't be removed).
3369
 * This is useful not only because we might be able to remove expensive-to-
3370
 * compute expressions, but because deletion of output columns might allow
3371
 * optimizations such as join removal to occur within the subquery.
3372
 *
3373
 * To avoid affecting column numbering in the targetlist, we don't physically
3374
 * remove unused tlist entries, but rather replace their expressions with NULL
3375
 * constants.  This is implemented by modifying subquery->targetList.
3376
 */
3377
static void
3378
remove_unused_subquery_outputs(Query *subquery, RelOptInfo *rel)
3379
1.60k
{
3380
1.60k
  Bitmapset  *attrs_used = NULL;
3381
1.60k
  ListCell   *lc;
3382
3383
  /*
3384
   * Do nothing if subquery has UNION/INTERSECT/EXCEPT: in principle we
3385
   * could update all the child SELECTs' tlists, but it seems not worth the
3386
   * trouble presently.
3387
   */
3388
1.60k
  if (subquery->setOperations)
3389
1
    return;
3390
3391
  /*
3392
   * If subquery has regular DISTINCT (not DISTINCT ON), we're wasting our
3393
   * time: all its output columns must be used in the distinctClause.
3394
   */
3395
1.60k
  if (subquery->distinctClause && !subquery->hasDistinctOn)
3396
1
    return;
3397
3398
  /*
3399
   * Collect a bitmap of all the output column numbers used by the upper
3400
   * query.
3401
   *
3402
   * Add all the attributes needed for joins or final output.  Note: we must
3403
   * look at rel's targetlist, not the attr_needed data, because attr_needed
3404
   * isn't computed for inheritance child rels, cf set_append_rel_size().
3405
   * (XXX might be worth changing that sometime.)
3406
   */
3407
1.60k
  pull_varattnos((Node *) rel->reltarget->exprs, rel->relid, &attrs_used);
3408
3409
  /* Add all the attributes used by un-pushed-down restriction clauses. */
3410
1.60k
  foreach(lc, rel->baserestrictinfo)
3411
2
  {
3412
2
    RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
3413
3414
2
    pull_varattnos((Node *) rinfo->clause, rel->relid, &attrs_used);
3415
2
  }
3416
3417
  /*
3418
   * If there's a whole-row reference to the subquery, we can't remove
3419
   * anything.
3420
   */
3421
1.60k
  if (bms_is_member(0 - FirstLowInvalidHeapAttributeNumber, attrs_used))
3422
1
    return;
3423
3424
  /*
3425
   * Run through the tlist and zap entries we don't need.  It's okay to
3426
   * modify the tlist items in-place because set_subquery_pathlist made a
3427
   * copy of the subquery.
3428
   */
3429
1.60k
  foreach(lc, subquery->targetList)
3430
3.23k
  {
3431
3.23k
    TargetEntry *tle = (TargetEntry *) lfirst(lc);
3432
3.23k
    Node     *texpr = (Node *) tle->expr;
3433
3434
    /*
3435
     * If it has a sortgroupref number, it's used in some sort/group
3436
     * clause so we'd better not remove it.  Also, don't remove any
3437
     * resjunk columns, since their reason for being has nothing to do
3438
     * with anybody reading the subquery's output.  (It's likely that
3439
     * resjunk columns in a sub-SELECT would always have ressortgroupref
3440
     * set, but even if they don't, it seems imprudent to remove them.)
3441
     */
3442
3.23k
    if (tle->ressortgroupref || tle->resjunk)
3443
19
      continue;
3444
3445
    /*
3446
     * If it's used by the upper query, we can't remove it.
3447
     */
3448
3.21k
    if (bms_is_member(tle->resno - FirstLowInvalidHeapAttributeNumber,
3449
3.21k
              attrs_used))
3450
1.68k
      continue;
3451
3452
    /*
3453
     * If it contains a set-returning function, we can't remove it since
3454
     * that could change the number of rows returned by the subquery.
3455
     */
3456
1.52k
    if (subquery->hasTargetSRFs &&
3457
1
      expression_returns_set(texpr))
3458
1
      continue;
3459
3460
    /*
3461
     * If it contains volatile functions, we daren't remove it for fear
3462
     * that the user is expecting their side-effects to happen.
3463
     */
3464
1.52k
    if (contain_volatile_functions(texpr))
3465
0
      continue;
3466
3467
    /*
3468
     * OK, we don't need it.  Replace the expression with a NULL constant.
3469
     * Preserve the exposed type of the expression, in case something
3470
     * looks at the rowtype of the subquery's result.
3471
     */
3472
1.52k
    tle->expr = (Expr *) makeNullConst(exprType(texpr),
3473
1.52k
                       exprTypmod(texpr),
3474
1.52k
                       exprCollation(texpr));
3475
1.52k
  }
3476
1.60k
}
3477
3478
/*
3479
 * create_partial_bitmap_paths
3480
 *    Build partial bitmap heap path for the relation
3481
 */
3482
void
3483
create_partial_bitmap_paths(PlannerInfo *root, RelOptInfo *rel,
3484
              Path *bitmapqual)
3485
0
{
3486
0
  int     parallel_workers;
3487
0
  double    pages_fetched;
3488
3489
  /* Compute heap pages for bitmap heap scan */
3490
0
  pages_fetched = compute_bitmap_pages(root, rel, bitmapqual, 1.0,
3491
0
                     NULL, NULL);
3492
3493
0
  parallel_workers = compute_parallel_worker(rel, pages_fetched, -1,
3494
0
                         max_parallel_workers_per_gather);
3495
3496
0
  if (parallel_workers <= 0)
3497
0
    return;
3498
3499
0
  add_partial_path(rel, (Path *) create_bitmap_heap_path(root, rel,
3500
0
                               bitmapqual, rel->lateral_relids, 1.0, parallel_workers));
3501
0
}
3502
3503
/*
3504
 * Compute the number of parallel workers that should be used to scan a
3505
 * relation.  We compute the parallel workers based on the size of the heap to
3506
 * be scanned and the size of the index to be scanned, then choose a minimum
3507
 * of those.
3508
 *
3509
 * "heap_pages" is the number of pages from the table that we expect to scan, or
3510
 * -1 if we don't expect to scan any.
3511
 *
3512
 * "index_pages" is the number of pages from the index that we expect to scan, or
3513
 * -1 if we don't expect to scan any.
3514
 *
3515
 * "max_workers" is caller's limit on the number of workers.  This typically
3516
 * comes from a GUC.
3517
 */
3518
int
3519
compute_parallel_worker(RelOptInfo *rel, double heap_pages, double index_pages,
3520
            int max_workers)
3521
0
{
3522
0
  int     parallel_workers = 0;
3523
3524
  /*
3525
   * If the user has set the parallel_workers reloption, use that; otherwise
3526
   * select a default number of workers.
3527
   */
3528
0
  if (rel->rel_parallel_workers != -1)
3529
0
    parallel_workers = rel->rel_parallel_workers;
3530
0
  else
3531
0
  {
3532
    /*
3533
     * If the number of pages being scanned is insufficient to justify a
3534
     * parallel scan, just return zero ... unless it's an inheritance
3535
     * child. In that case, we want to generate a parallel path here
3536
     * anyway.  It might not be worthwhile just for this relation, but
3537
     * when combined with all of its inheritance siblings it may well pay
3538
     * off.
3539
     */
3540
0
    if (rel->reloptkind == RELOPT_BASEREL &&
3541
0
      ((heap_pages >= 0 && heap_pages < min_parallel_table_scan_size) ||
3542
0
       (index_pages >= 0 && index_pages < min_parallel_index_scan_size)))
3543
0
      return 0;
3544
3545
0
    if (heap_pages >= 0)
3546
0
    {
3547
0
      int     heap_parallel_threshold;
3548
0
      int     heap_parallel_workers = 1;
3549
3550
      /*
3551
       * Select the number of workers based on the log of the size of
3552
       * the relation.  This probably needs to be a good deal more
3553
       * sophisticated, but we need something here for now.  Note that
3554
       * the upper limit of the min_parallel_table_scan_size GUC is
3555
       * chosen to prevent overflow here.
3556
       */
3557
0
      heap_parallel_threshold = Max(min_parallel_table_scan_size, 1);
3558
0
      while (heap_pages >= (BlockNumber) (heap_parallel_threshold * 3))
3559
0
      {
3560
0
        heap_parallel_workers++;
3561
0
        heap_parallel_threshold *= 3;
3562
0
        if (heap_parallel_threshold > INT_MAX / 3)
3563
0
          break;   /* avoid overflow */
3564
0
      }
3565
3566
0
      parallel_workers = heap_parallel_workers;
3567
0
    }
3568
3569
0
    if (index_pages >= 0)
3570
0
    {
3571
0
      int     index_parallel_workers = 1;
3572
0
      int     index_parallel_threshold;
3573
3574
      /* same calculation as for heap_pages above */
3575
0
      index_parallel_threshold = Max(min_parallel_index_scan_size, 1);
3576
0
      while (index_pages >= (BlockNumber) (index_parallel_threshold * 3))
3577
0
      {
3578
0
        index_parallel_workers++;
3579
0
        index_parallel_threshold *= 3;
3580
0
        if (index_parallel_threshold > INT_MAX / 3)
3581
0
          break;   /* avoid overflow */
3582
0
      }
3583
3584
0
      if (parallel_workers > 0)
3585
0
        parallel_workers = Min(parallel_workers, index_parallel_workers);
3586
0
      else
3587
0
        parallel_workers = index_parallel_workers;
3588
0
    }
3589
0
  }
3590
3591
  /* In no case use more than caller supplied maximum number of workers */
3592
0
  parallel_workers = Min(parallel_workers, max_workers);
3593
3594
0
  return parallel_workers;
3595
0
}
3596
3597
/*
3598
 * generate_partitionwise_join_paths
3599
 *    Create paths representing partitionwise join for given partitioned
3600
 *    join relation.
3601
 *
3602
 * This must not be called until after we are done adding paths for all
3603
 * child-joins. Otherwise, add_path might delete a path to which some path
3604
 * generated here has a reference.
3605
 */
3606
void
3607
generate_partitionwise_join_paths(PlannerInfo *root, RelOptInfo *rel)
3608
5.58k
{
3609
5.58k
  List     *live_children = NIL;
3610
5.58k
  int     cnt_parts;
3611
5.58k
  int     num_parts;
3612
5.58k
  RelOptInfo **part_rels;
3613
3614
  /* Handle only join relations here. */
3615
5.58k
  if (!IS_JOIN_REL(rel))
3616
0
    return;
3617
3618
  /* We've nothing to do if the relation is not partitioned. */
3619
5.58k
  if (!IS_PARTITIONED_REL(rel))
3620
5.57k
    return;
3621
3622
  /* The relation should have consider_partitionwise_join set. */
3623
8
  Assert(rel->consider_partitionwise_join);
3624
3625
  /* Guard against stack overflow due to overly deep partition hierarchy. */
3626
8
  check_stack_depth();
3627
3628
8
  num_parts = rel->nparts;
3629
8
  part_rels = rel->part_rels;
3630
3631
  /* Collect non-dummy child-joins. */
3632
24
  for (cnt_parts = 0; cnt_parts < num_parts; cnt_parts++)
3633
16
  {
3634
16
    RelOptInfo *child_rel = part_rels[cnt_parts];
3635
3636
16
    Assert(child_rel != NULL);
3637
3638
    /* Add partitionwise join paths for partitioned child-joins. */
3639
16
    generate_partitionwise_join_paths(root, child_rel);
3640
3641
    /* Dummy children will not be scanned, so ignore those. */
3642
16
    if (IS_DUMMY_REL(child_rel))
3643
1
      continue;
3644
3645
15
    set_cheapest(child_rel);
3646
3647
#ifdef OPTIMIZER_DEBUG
3648
    debug_print_rel(root, child_rel);
3649
#endif
3650
3651
15
    live_children = lappend(live_children, child_rel);
3652
15
  }
3653
3654
  /* If all child-joins are dummy, parent join is also dummy. */
3655
8
  if (!live_children)
3656
0
  {
3657
0
    mark_dummy_rel(rel);
3658
0
    return;
3659
0
  }
3660
3661
  /* Build additional paths for this rel from child-join paths. */
3662
8
  add_paths_to_append_rel(root, rel, live_children);
3663
8
  list_free(live_children);
3664
8
}
3665
3666
3667
/*****************************************************************************
3668
 *      DEBUG SUPPORT
3669
 *****************************************************************************/
3670
3671
#ifdef OPTIMIZER_DEBUG
3672
3673
static void
3674
print_relids(PlannerInfo *root, Relids relids)
3675
{
3676
  int     x;
3677
  bool    first = true;
3678
3679
  x = -1;
3680
  while ((x = bms_next_member(relids, x)) >= 0)
3681
  {
3682
    if (!first)
3683
      printf(" ");
3684
    if (x < root->simple_rel_array_size &&
3685
      root->simple_rte_array[x])
3686
      printf("%s", root->simple_rte_array[x]->eref->aliasname);
3687
    else
3688
      printf("%d", x);
3689
    first = false;
3690
  }
3691
}
3692
3693
static void
3694
print_restrictclauses(PlannerInfo *root, List *clauses)
3695
{
3696
  ListCell   *l;
3697
3698
  foreach(l, clauses)
3699
  {
3700
    RestrictInfo *c = lfirst(l);
3701
3702
    print_expr((Node *) c->clause, root->parse->rtable);
3703
    if (lnext(l))
3704
      printf(", ");
3705
  }
3706
}
3707
3708
static void
3709
print_path(PlannerInfo *root, Path *path, int indent)
3710
{
3711
  const char *ptype;
3712
  bool    join = false;
3713
  Path     *subpath = NULL;
3714
  int     i;
3715
3716
  switch (nodeTag(path))
3717
  {
3718
    case T_Path:
3719
      switch (path->pathtype)
3720
      {
3721
        case T_SeqScan:
3722
          ptype = "SeqScan";
3723
          break;
3724
        case T_SampleScan:
3725
          ptype = "SampleScan";
3726
          break;
3727
        case T_SubqueryScan:
3728
          ptype = "SubqueryScan";
3729
          break;
3730
        case T_FunctionScan:
3731
          ptype = "FunctionScan";
3732
          break;
3733
        case T_TableFuncScan:
3734
          ptype = "TableFuncScan";
3735
          break;
3736
        case T_ValuesScan:
3737
          ptype = "ValuesScan";
3738
          break;
3739
        case T_CteScan:
3740
          ptype = "CteScan";
3741
          break;
3742
        case T_WorkTableScan:
3743
          ptype = "WorkTableScan";
3744
          break;
3745
        default:
3746
          ptype = "???Path";
3747
          break;
3748
      }
3749
      break;
3750
    case T_IndexPath:
3751
      ptype = "IdxScan";
3752
      break;
3753
    case T_BitmapHeapPath:
3754
      ptype = "BitmapHeapScan";
3755
      break;
3756
    case T_BitmapAndPath:
3757
      ptype = "BitmapAndPath";
3758
      break;
3759
    case T_BitmapOrPath:
3760
      ptype = "BitmapOrPath";
3761
      break;
3762
    case T_TidPath:
3763
      ptype = "TidScan";
3764
      break;
3765
    case T_SubqueryScanPath:
3766
      ptype = "SubqueryScanScan";
3767
      break;
3768
    case T_ForeignPath:
3769
      ptype = "ForeignScan";
3770
      break;
3771
    case T_CustomPath:
3772
      ptype = "CustomScan";
3773
      break;
3774
    case T_NestPath:
3775
      ptype = "NestLoop";
3776
      join = true;
3777
      break;
3778
    case T_MergePath:
3779
      ptype = "MergeJoin";
3780
      join = true;
3781
      break;
3782
    case T_HashPath:
3783
      ptype = "HashJoin";
3784
      join = true;
3785
      break;
3786
    case T_AppendPath:
3787
      ptype = "Append";
3788
      break;
3789
    case T_MergeAppendPath:
3790
      ptype = "MergeAppend";
3791
      break;
3792
    case T_ResultPath:
3793
      ptype = "Result";
3794
      break;
3795
    case T_MaterialPath:
3796
      ptype = "Material";
3797
      subpath = ((MaterialPath *) path)->subpath;
3798
      break;
3799
    case T_UniquePath:
3800
      ptype = "Unique";
3801
      subpath = ((UniquePath *) path)->subpath;
3802
      break;
3803
    case T_GatherPath:
3804
      ptype = "Gather";
3805
      subpath = ((GatherPath *) path)->subpath;
3806
      break;
3807
    case T_GatherMergePath:
3808
      ptype = "GatherMerge";
3809
      subpath = ((GatherMergePath *) path)->subpath;
3810
      break;
3811
    case T_ProjectionPath:
3812
      ptype = "Projection";
3813
      subpath = ((ProjectionPath *) path)->subpath;
3814
      break;
3815
    case T_ProjectSetPath:
3816
      ptype = "ProjectSet";
3817
      subpath = ((ProjectSetPath *) path)->subpath;
3818
      break;
3819
    case T_SortPath:
3820
      ptype = "Sort";
3821
      subpath = ((SortPath *) path)->subpath;
3822
      break;
3823
    case T_GroupPath:
3824
      ptype = "Group";
3825
      subpath = ((GroupPath *) path)->subpath;
3826
      break;
3827
    case T_UpperUniquePath:
3828
      ptype = "UpperUnique";
3829
      subpath = ((UpperUniquePath *) path)->subpath;
3830
      break;
3831
    case T_AggPath:
3832
      ptype = "Agg";
3833
      subpath = ((AggPath *) path)->subpath;
3834
      break;
3835
    case T_GroupingSetsPath:
3836
      ptype = "GroupingSets";
3837
      subpath = ((GroupingSetsPath *) path)->subpath;
3838
      break;
3839
    case T_MinMaxAggPath:
3840
      ptype = "MinMaxAgg";
3841
      break;
3842
    case T_WindowAggPath:
3843
      ptype = "WindowAgg";
3844
      subpath = ((WindowAggPath *) path)->subpath;
3845
      break;
3846
    case T_SetOpPath:
3847
      ptype = "SetOp";
3848
      subpath = ((SetOpPath *) path)->subpath;
3849
      break;
3850
    case T_RecursiveUnionPath:
3851
      ptype = "RecursiveUnion";
3852
      break;
3853
    case T_LockRowsPath:
3854
      ptype = "LockRows";
3855
      subpath = ((LockRowsPath *) path)->subpath;
3856
      break;
3857
    case T_ModifyTablePath:
3858
      ptype = "ModifyTable";
3859
      break;
3860
    case T_LimitPath:
3861
      ptype = "Limit";
3862
      subpath = ((LimitPath *) path)->subpath;
3863
      break;
3864
    default:
3865
      ptype = "???Path";
3866
      break;
3867
  }
3868
3869
  for (i = 0; i < indent; i++)
3870
    printf("\t");
3871
  printf("%s", ptype);
3872
3873
  if (path->parent)
3874
  {
3875
    printf("(");
3876
    print_relids(root, path->parent->relids);
3877
    printf(")");
3878
  }
3879
  if (path->param_info)
3880
  {
3881
    printf(" required_outer (");
3882
    print_relids(root, path->param_info->ppi_req_outer);
3883
    printf(")");
3884
  }
3885
  printf(" rows=%.0f cost=%.2f..%.2f\n",
3886
       path->rows, path->startup_cost, path->total_cost);
3887
3888
  if (path->pathkeys)
3889
  {
3890
    for (i = 0; i < indent; i++)
3891
      printf("\t");
3892
    printf("  pathkeys: ");
3893
    print_pathkeys(path->pathkeys, root->parse->rtable);
3894
  }
3895
3896
  if (join)
3897
  {
3898
    JoinPath   *jp = (JoinPath *) path;
3899
3900
    for (i = 0; i < indent; i++)
3901
      printf("\t");
3902
    printf("  clauses: ");
3903
    print_restrictclauses(root, jp->joinrestrictinfo);
3904
    printf("\n");
3905
3906
    if (IsA(path, MergePath))
3907
    {
3908
      MergePath  *mp = (MergePath *) path;
3909
3910
      for (i = 0; i < indent; i++)
3911
        printf("\t");
3912
      printf("  sortouter=%d sortinner=%d materializeinner=%d\n",
3913
           ((mp->outersortkeys) ? 1 : 0),
3914
           ((mp->innersortkeys) ? 1 : 0),
3915
           ((mp->materialize_inner) ? 1 : 0));
3916
    }
3917
3918
    print_path(root, jp->outerjoinpath, indent + 1);
3919
    print_path(root, jp->innerjoinpath, indent + 1);
3920
  }
3921
3922
  if (subpath)
3923
    print_path(root, subpath, indent + 1);
3924
}
3925
3926
void
3927
debug_print_rel(PlannerInfo *root, RelOptInfo *rel)
3928
{
3929
  ListCell   *l;
3930
3931
  printf("RELOPTINFO (");
3932
  print_relids(root, rel->relids);
3933
  printf("): rows=%.0f width=%d\n", rel->rows, rel->reltarget->width);
3934
3935
  if (rel->baserestrictinfo)
3936
  {
3937
    printf("\tbaserestrictinfo: ");
3938
    print_restrictclauses(root, rel->baserestrictinfo);
3939
    printf("\n");
3940
  }
3941
3942
  if (rel->joininfo)
3943
  {
3944
    printf("\tjoininfo: ");
3945
    print_restrictclauses(root, rel->joininfo);
3946
    printf("\n");
3947
  }
3948
3949
  printf("\tpath list:\n");
3950
  foreach(l, rel->pathlist)
3951
    print_path(root, lfirst(l), 1);
3952
  if (rel->cheapest_parameterized_paths)
3953
  {
3954
    printf("\n\tcheapest parameterized paths:\n");
3955
    foreach(l, rel->cheapest_parameterized_paths)
3956
      print_path(root, lfirst(l), 1);
3957
  }
3958
  if (rel->cheapest_startup_path)
3959
  {
3960
    printf("\n\tcheapest startup path:\n");
3961
    print_path(root, rel->cheapest_startup_path, 1);
3962
  }
3963
  if (rel->cheapest_total_path)
3964
  {
3965
    printf("\n\tcheapest total path:\n");
3966
    print_path(root, rel->cheapest_total_path, 1);
3967
  }
3968
  printf("\n");
3969
  fflush(stdout);
3970
}
3971
3972
#endif              /* OPTIMIZER_DEBUG */