Coverage Report

Created: 2026-08-14 06:37

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/postgres/src/backend/optimizer/plan/planner.c
Line
Count
Source
1
/*-------------------------------------------------------------------------
2
 *
3
 * planner.c
4
 *    The query optimizer external interface.
5
 *
6
 * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
7
 * Portions Copyright (c) 1994, Regents of the University of California
8
 *
9
 *
10
 * IDENTIFICATION
11
 *    src/backend/optimizer/plan/planner.c
12
 *
13
 *-------------------------------------------------------------------------
14
 */
15
16
#include "postgres.h"
17
18
#include <limits.h>
19
#include <math.h>
20
21
#include "access/genam.h"
22
#include "access/parallel.h"
23
#include "access/sysattr.h"
24
#include "access/table.h"
25
#include "catalog/pg_aggregate.h"
26
#include "catalog/pg_inherits.h"
27
#include "catalog/pg_proc.h"
28
#include "catalog/pg_type.h"
29
#include "executor/executor.h"
30
#include "foreign/fdwapi.h"
31
#include "jit/jit.h"
32
#include "lib/bipartite_match.h"
33
#include "lib/knapsack.h"
34
#include "miscadmin.h"
35
#include "nodes/makefuncs.h"
36
#include "nodes/nodeFuncs.h"
37
#ifdef OPTIMIZER_DEBUG
38
#include "nodes/print.h"
39
#endif
40
#include "nodes/supportnodes.h"
41
#include "optimizer/appendinfo.h"
42
#include "optimizer/clauses.h"
43
#include "optimizer/cost.h"
44
#include "optimizer/optimizer.h"
45
#include "optimizer/paramassign.h"
46
#include "optimizer/pathnode.h"
47
#include "optimizer/paths.h"
48
#include "optimizer/plancat.h"
49
#include "optimizer/planmain.h"
50
#include "optimizer/planner.h"
51
#include "optimizer/prep.h"
52
#include "optimizer/subselect.h"
53
#include "optimizer/tlist.h"
54
#include "parser/analyze.h"
55
#include "parser/parse_agg.h"
56
#include "parser/parse_clause.h"
57
#include "parser/parse_relation.h"
58
#include "parser/parsetree.h"
59
#include "partitioning/partdesc.h"
60
#include "rewrite/rewriteManip.h"
61
#include "utils/acl.h"
62
#include "utils/backend_status.h"
63
#include "utils/lsyscache.h"
64
#include "utils/rel.h"
65
#include "utils/selfuncs.h"
66
67
/* GUC parameters */
68
double    cursor_tuple_fraction = DEFAULT_CURSOR_TUPLE_FRACTION;
69
int     debug_parallel_query = DEBUG_PARALLEL_OFF;
70
bool    parallel_leader_participation = true;
71
bool    enable_distinct_reordering = true;
72
73
/* Hook for plugins to get control in planner() */
74
planner_hook_type planner_hook = NULL;
75
76
/* Hook for plugins to get control after PlannerGlobal is initialized */
77
planner_setup_hook_type planner_setup_hook = NULL;
78
79
/* Hook for plugins to get control before PlannerGlobal is discarded */
80
planner_shutdown_hook_type planner_shutdown_hook = NULL;
81
82
/* Hook for plugins to get control when grouping_planner() plans upper rels */
83
create_upper_paths_hook_type create_upper_paths_hook = NULL;
84
85
86
/* Expression kind codes for preprocess_expression */
87
0
#define EXPRKIND_QUAL       0
88
0
#define EXPRKIND_TARGET       1
89
0
#define EXPRKIND_RTFUNC       2
90
0
#define EXPRKIND_RTFUNC_LATERAL   3
91
0
#define EXPRKIND_VALUES       4
92
0
#define EXPRKIND_VALUES_LATERAL   5
93
0
#define EXPRKIND_LIMIT        6
94
0
#define EXPRKIND_APPINFO      7
95
0
#define EXPRKIND_PHV        8
96
0
#define EXPRKIND_TABLESAMPLE    9
97
0
#define EXPRKIND_ARBITER_ELEM   10
98
0
#define EXPRKIND_TABLEFUNC      11
99
0
#define EXPRKIND_TABLEFUNC_LATERAL  12
100
0
#define EXPRKIND_GROUPEXPR      13
101
102
/*
103
 * Data specific to grouping sets
104
 */
105
typedef struct
106
{
107
  List     *rollups;
108
  List     *hash_sets_idx;
109
  double    dNumHashGroups;
110
  bool    any_hashable;
111
  Bitmapset  *unsortable_refs;
112
  Bitmapset  *unhashable_refs;
113
  List     *unsortable_sets;
114
  int      *tleref_to_colnum_map;
115
} grouping_sets_data;
116
117
/*
118
 * Temporary structure for use during WindowClause reordering in order to be
119
 * able to sort WindowClauses on partitioning/ordering prefix.
120
 */
121
typedef struct
122
{
123
  WindowClause *wc;
124
  List     *uniqueOrder;  /* A List of unique ordering/partitioning
125
                 * clauses per Window */
126
} WindowClauseSortData;
127
128
/* Passthrough data for standard_qp_callback */
129
typedef struct
130
{
131
  List     *activeWindows;  /* active windows, if any */
132
  grouping_sets_data *gset_data;  /* grouping sets data, if any */
133
  SetOperationStmt *setop;  /* parent set operation or NULL if not a
134
                 * subquery belonging to a set operation */
135
} standard_qp_extra;
136
137
/*
138
 * Context for find_having_conflicts.  This is the callback context passed to
139
 * expression_has_grouping_conflict in clauses.c.
140
 */
141
typedef struct
142
{
143
  Query    *parse;
144
  Index   group_rtindex;
145
} having_grouping_ctx;
146
147
/* Local functions */
148
static Node *preprocess_expression(PlannerInfo *root, Node *expr, int kind);
149
static void preprocess_qual_conditions(PlannerInfo *root, Node *jtnode);
150
static Bitmapset *find_having_conflicts(Query *parse, Index group_rtindex);
151
static Oid  having_var_grouping_eqop(Var *var, void *context);
152
static Oid  group_var_eqop(Query *parse, Var *var);
153
static void grouping_planner(PlannerInfo *root, double tuple_fraction,
154
               SetOperationStmt *setops);
155
static grouping_sets_data *preprocess_grouping_sets(PlannerInfo *root);
156
static List *remap_to_groupclause_idx(List *groupClause, List *gsets,
157
                    int *tleref_to_colnum_map);
158
static void preprocess_rowmarks(PlannerInfo *root);
159
static double preprocess_limit(PlannerInfo *root,
160
                 double tuple_fraction,
161
                 int64 *offset_est, int64 *count_est);
162
static List *preprocess_groupclause(PlannerInfo *root, List *force);
163
static List *extract_rollup_sets(List *groupingSets);
164
static List *reorder_grouping_sets(List *groupingSets, List *sortclause);
165
static void standard_qp_callback(PlannerInfo *root, void *extra);
166
static double get_number_of_groups(PlannerInfo *root,
167
                   double path_rows,
168
                   grouping_sets_data *gd,
169
                   List *target_list);
170
static RelOptInfo *create_grouping_paths(PlannerInfo *root,
171
                     RelOptInfo *input_rel,
172
                     PathTarget *target,
173
                     bool target_parallel_safe,
174
                     grouping_sets_data *gd);
175
static bool is_degenerate_grouping(PlannerInfo *root);
176
static void create_degenerate_grouping_paths(PlannerInfo *root,
177
                       RelOptInfo *input_rel,
178
                       RelOptInfo *grouped_rel);
179
static RelOptInfo *make_grouping_rel(PlannerInfo *root, RelOptInfo *input_rel,
180
                   PathTarget *target, bool target_parallel_safe,
181
                   Node *havingQual);
182
static void create_ordinary_grouping_paths(PlannerInfo *root,
183
                       RelOptInfo *input_rel,
184
                       RelOptInfo *grouped_rel,
185
                       const AggClauseCosts *agg_costs,
186
                       grouping_sets_data *gd,
187
                       GroupPathExtraData *extra,
188
                       RelOptInfo **partially_grouped_rel_p);
189
static void consider_groupingsets_paths(PlannerInfo *root,
190
                    RelOptInfo *grouped_rel,
191
                    Path *path,
192
                    bool is_sorted,
193
                    bool can_hash,
194
                    grouping_sets_data *gd,
195
                    const AggClauseCosts *agg_costs,
196
                    double dNumGroups);
197
static RelOptInfo *create_window_paths(PlannerInfo *root,
198
                     RelOptInfo *input_rel,
199
                     PathTarget *input_target,
200
                     PathTarget *output_target,
201
                     bool output_target_parallel_safe,
202
                     WindowFuncLists *wflists,
203
                     List *activeWindows);
204
static void create_one_window_path(PlannerInfo *root,
205
                   RelOptInfo *window_rel,
206
                   Path *path,
207
                   PathTarget *input_target,
208
                   PathTarget *output_target,
209
                   WindowFuncLists *wflists,
210
                   List *activeWindows);
211
static RelOptInfo *create_distinct_paths(PlannerInfo *root,
212
                     RelOptInfo *input_rel,
213
                     PathTarget *target);
214
static void create_partial_distinct_paths(PlannerInfo *root,
215
                      RelOptInfo *input_rel,
216
                      RelOptInfo *final_distinct_rel,
217
                      PathTarget *target);
218
static RelOptInfo *create_final_distinct_paths(PlannerInfo *root,
219
                         RelOptInfo *input_rel,
220
                         RelOptInfo *distinct_rel);
221
static List *get_useful_pathkeys_for_distinct(PlannerInfo *root,
222
                        List *needed_pathkeys,
223
                        List *path_pathkeys);
224
static RelOptInfo *create_ordered_paths(PlannerInfo *root,
225
                    RelOptInfo *input_rel,
226
                    PathTarget *target,
227
                    bool target_parallel_safe,
228
                    double limit_tuples);
229
static PathTarget *make_group_input_target(PlannerInfo *root,
230
                       PathTarget *final_target);
231
static PathTarget *make_partial_grouping_target(PlannerInfo *root,
232
                        PathTarget *grouping_target,
233
                        Node *havingQual);
234
static List *postprocess_setop_tlist(List *new_tlist, List *orig_tlist);
235
static void optimize_window_clauses(PlannerInfo *root,
236
                  WindowFuncLists *wflists);
237
static List *select_active_windows(PlannerInfo *root, WindowFuncLists *wflists);
238
static void name_active_windows(List *activeWindows);
239
static PathTarget *make_window_input_target(PlannerInfo *root,
240
                      PathTarget *final_target,
241
                      List *activeWindows);
242
static List *make_pathkeys_for_window(PlannerInfo *root, WindowClause *wc,
243
                    List *tlist);
244
static PathTarget *make_sort_input_target(PlannerInfo *root,
245
                      PathTarget *final_target,
246
                      bool *have_postponed_srfs);
247
static void adjust_paths_for_srfs(PlannerInfo *root, RelOptInfo *rel,
248
                  List *targets, List *targets_contain_srfs);
249
static void add_paths_to_grouping_rel(PlannerInfo *root, RelOptInfo *input_rel,
250
                    RelOptInfo *grouped_rel,
251
                    RelOptInfo *partially_grouped_rel,
252
                    const AggClauseCosts *agg_costs,
253
                    grouping_sets_data *gd,
254
                    GroupPathExtraData *extra);
255
static RelOptInfo *create_partial_grouping_paths(PlannerInfo *root,
256
                         RelOptInfo *grouped_rel,
257
                         RelOptInfo *input_rel,
258
                         grouping_sets_data *gd,
259
                         GroupPathExtraData *extra,
260
                         bool force_rel_creation);
261
static Path *make_ordered_path(PlannerInfo *root,
262
                 RelOptInfo *rel,
263
                 Path *path,
264
                 Path *cheapest_path,
265
                 List *pathkeys,
266
                 double limit_tuples);
267
static void gather_grouping_paths(PlannerInfo *root, RelOptInfo *rel);
268
static bool can_partial_agg(PlannerInfo *root);
269
static void apply_scanjoin_target_to_paths(PlannerInfo *root,
270
                       RelOptInfo *rel,
271
                       List *scanjoin_targets,
272
                       List *scanjoin_targets_contain_srfs,
273
                       bool scanjoin_target_parallel_safe,
274
                       bool tlist_same_exprs);
275
static void create_partitionwise_grouping_paths(PlannerInfo *root,
276
                        RelOptInfo *input_rel,
277
                        RelOptInfo *grouped_rel,
278
                        RelOptInfo *partially_grouped_rel,
279
                        const AggClauseCosts *agg_costs,
280
                        grouping_sets_data *gd,
281
                        PartitionwiseAggregateType patype,
282
                        GroupPathExtraData *extra);
283
static bool group_by_has_partkey(RelOptInfo *input_rel,
284
                 List *targetList,
285
                 List *groupClause);
286
static int  common_prefix_cmp(const void *a, const void *b);
287
static List *generate_setop_child_grouplist(SetOperationStmt *op,
288
                      List *targetlist);
289
static void create_final_unique_paths(PlannerInfo *root, RelOptInfo *input_rel,
290
                    List *sortPathkeys, List *groupClause,
291
                    SpecialJoinInfo *sjinfo, RelOptInfo *unique_rel);
292
static void create_partial_unique_paths(PlannerInfo *root, RelOptInfo *input_rel,
293
                    List *sortPathkeys, List *groupClause,
294
                    SpecialJoinInfo *sjinfo, RelOptInfo *unique_rel);
295
296
297
/*****************************************************************************
298
 *
299
 *     Query optimizer entry point
300
 *
301
 * Inputs:
302
 *  parse: an analyzed-and-rewritten query tree for an optimizable statement
303
 *  query_string: source text for the query tree (used for error reports)
304
 *  cursorOptions: bitmask of CURSOR_OPT_XXX flags, see parsenodes.h
305
 *  boundParams: passed-in parameter values, or NULL if none
306
 *  es: ExplainState if being called from EXPLAIN, else NULL
307
 *
308
 * The result is a PlannedStmt tree.
309
 *
310
 * PARAM_EXTERN Param nodes within the parse tree can be replaced by Consts
311
 * using values from boundParams, if those values are marked PARAM_FLAG_CONST.
312
 * Parameter values not so marked are still relied on for estimation purposes.
313
 *
314
 * The ExplainState pointer is not currently used by the core planner, but it
315
 * is passed through to some planner hooks so that they can report information
316
 * back to EXPLAIN extension hooks.
317
 *
318
 * To support loadable plugins that monitor or modify planner behavior,
319
 * we provide a hook variable that lets a plugin get control before and
320
 * after the standard planning process.  The plugin would normally call
321
 * standard_planner().
322
 *
323
 * Note to plugin authors: standard_planner() scribbles on its Query input,
324
 * so you'd better copy that data structure if you want to plan more than once.
325
 *
326
 *****************************************************************************/
327
PlannedStmt *
328
planner(Query *parse, const char *query_string, int cursorOptions,
329
    ParamListInfo boundParams, ExplainState *es)
330
0
{
331
0
  PlannedStmt *result;
332
333
0
  if (planner_hook)
334
0
    result = (*planner_hook) (parse, query_string, cursorOptions,
335
0
                  boundParams, es);
336
0
  else
337
0
    result = standard_planner(parse, query_string, cursorOptions,
338
0
                  boundParams, es);
339
340
0
  pgstat_report_plan_id(result->planId, false);
341
342
0
  return result;
343
0
}
344
345
PlannedStmt *
346
standard_planner(Query *parse, const char *query_string, int cursorOptions,
347
         ParamListInfo boundParams, ExplainState *es)
348
0
{
349
0
  PlannedStmt *result;
350
0
  PlannerGlobal *glob;
351
0
  double    tuple_fraction;
352
0
  PlannerInfo *root;
353
0
  RelOptInfo *final_rel;
354
0
  Path     *best_path;
355
0
  Plan     *top_plan;
356
0
  ListCell   *lp,
357
0
         *lr,
358
0
         *lc;
359
360
  /*
361
   * Set up global state for this planner invocation.  This data is needed
362
   * across all levels of sub-Query that might exist in the given command,
363
   * so we keep it in a separate struct that's linked to by each per-Query
364
   * PlannerInfo.
365
   */
366
0
  glob = makeNode(PlannerGlobal);
367
368
0
  glob->boundParams = boundParams;
369
0
  glob->subplans = NIL;
370
0
  glob->subpaths = NIL;
371
0
  glob->subroots = NIL;
372
0
  glob->rewindPlanIDs = NULL;
373
0
  glob->finalrtable = NIL;
374
0
  glob->allRelids = NULL;
375
0
  glob->prunableRelids = NULL;
376
0
  glob->finalrteperminfos = NIL;
377
0
  glob->finalrowmarks = NIL;
378
0
  glob->resultRelations = NIL;
379
0
  glob->appendRelations = NIL;
380
0
  glob->partPruneInfos = NIL;
381
0
  glob->relationOids = NIL;
382
0
  glob->invalItems = NIL;
383
0
  glob->paramExecTypes = NIL;
384
0
  glob->lastPHId = 0;
385
0
  glob->lastRowMarkId = 0;
386
0
  glob->lastPlanNodeId = 0;
387
0
  glob->transientPlan = false;
388
0
  glob->dependsOnRole = false;
389
0
  glob->partition_directory = NULL;
390
0
  glob->rel_notnullatts_hash = NULL;
391
392
  /*
393
   * Assess whether it's feasible to use parallel mode for this query. We
394
   * can't do this in a standalone backend, or if the command will try to
395
   * modify any data, or if this is a cursor operation, or if GUCs are set
396
   * to values that don't permit parallelism, or if parallel-unsafe
397
   * functions are present in the query tree.
398
   *
399
   * (Note that we do allow CREATE TABLE AS, SELECT INTO, and CREATE
400
   * MATERIALIZED VIEW to use parallel plans, but this is safe only because
401
   * the command is writing into a completely new table which workers won't
402
   * be able to see.  If the workers could see the table, the fact that
403
   * group locking would cause them to ignore the leader's heavyweight GIN
404
   * page locks would make this unsafe.  We'll have to fix that somehow if
405
   * we want to allow parallel inserts in general; updates and deletes have
406
   * additional problems especially around combo CIDs.)
407
   *
408
   * For now, we don't try to use parallel mode if we're running inside a
409
   * parallel worker.  We might eventually be able to relax this
410
   * restriction, but for now it seems best not to have parallel workers
411
   * trying to create their own parallel workers.
412
   */
413
0
  if ((cursorOptions & CURSOR_OPT_PARALLEL_OK) != 0 &&
414
0
    IsUnderPostmaster &&
415
0
    parse->commandType == CMD_SELECT &&
416
0
    !parse->hasModifyingCTE &&
417
0
    max_parallel_workers_per_gather > 0 &&
418
0
    !IsParallelWorker())
419
0
  {
420
    /* all the cheap tests pass, so scan the query tree */
421
0
    glob->maxParallelHazard = max_parallel_hazard(parse);
422
0
    glob->parallelModeOK = (glob->maxParallelHazard != PROPARALLEL_UNSAFE);
423
0
  }
424
0
  else
425
0
  {
426
    /* skip the query tree scan, just assume it's unsafe */
427
0
    glob->maxParallelHazard = PROPARALLEL_UNSAFE;
428
0
    glob->parallelModeOK = false;
429
0
  }
430
431
  /*
432
   * glob->parallelModeNeeded is normally set to false here and changed to
433
   * true during plan creation if a Gather or Gather Merge plan is actually
434
   * created (cf. create_gather_plan, create_gather_merge_plan).
435
   *
436
   * However, if debug_parallel_query = on or debug_parallel_query =
437
   * regress, then we impose parallel mode whenever it's safe to do so, even
438
   * if the final plan doesn't use parallelism.  It's not safe to do so if
439
   * the query contains anything parallel-unsafe; parallelModeOK will be
440
   * false in that case.  Note that parallelModeOK can't change after this
441
   * point. Otherwise, everything in the query is either parallel-safe or
442
   * parallel-restricted, and in either case it should be OK to impose
443
   * parallel-mode restrictions.  If that ends up breaking something, then
444
   * either some function the user included in the query is incorrectly
445
   * labeled as parallel-safe or parallel-restricted when in reality it's
446
   * parallel-unsafe, or else the query planner itself has a bug.
447
   */
448
0
  glob->parallelModeNeeded = glob->parallelModeOK &&
449
0
    (debug_parallel_query != DEBUG_PARALLEL_OFF);
450
451
  /* Determine what fraction of the plan is likely to be scanned */
452
0
  if (cursorOptions & CURSOR_OPT_FAST_PLAN)
453
0
  {
454
    /*
455
     * We have no real idea how many tuples the user will ultimately FETCH
456
     * from a cursor, but it is often the case that he doesn't want 'em
457
     * all, or would prefer a fast-start plan anyway so that he can
458
     * process some of the tuples sooner.  Use a GUC parameter to decide
459
     * what fraction to optimize for.
460
     */
461
0
    tuple_fraction = cursor_tuple_fraction;
462
463
    /*
464
     * We document cursor_tuple_fraction as simply being a fraction, which
465
     * means the edge cases 0 and 1 have to be treated specially here.  We
466
     * convert 1 to 0 ("all the tuples") and 0 to a very small fraction.
467
     */
468
0
    if (tuple_fraction >= 1.0)
469
0
      tuple_fraction = 0.0;
470
0
    else if (tuple_fraction <= 0.0)
471
0
      tuple_fraction = 1e-10;
472
0
  }
473
0
  else
474
0
  {
475
    /* Default assumption is we need all the tuples */
476
0
    tuple_fraction = 0.0;
477
0
  }
478
479
  /*
480
   * Compute the initial path generation strategy mask.
481
   *
482
   * Some strategies, such as PGS_FOREIGNJOIN, have no corresponding enable_*
483
   * GUC, and so the corresponding bits are always set in the default
484
   * strategy mask.
485
   *
486
   * It may seem surprising that enable_indexscan sets both PGS_INDEXSCAN
487
   * and PGS_INDEXONLYSCAN. However, the historical behavior of this GUC
488
   * corresponds to this exactly: enable_indexscan=off disables both
489
   * index-scan and index-only scan paths, whereas enable_indexonlyscan=off
490
   * converts the index-only scan paths that we would have considered into
491
   * index scan paths.
492
   */
493
0
  glob->default_pgs_mask = PGS_APPEND | PGS_MERGE_APPEND | PGS_FOREIGNJOIN |
494
0
    PGS_GATHER | PGS_CONSIDER_NONPARTIAL;
495
0
  if (enable_tidscan)
496
0
    glob->default_pgs_mask |= PGS_TIDSCAN;
497
0
  if (enable_seqscan)
498
0
    glob->default_pgs_mask |= PGS_SEQSCAN;
499
0
  if (enable_indexscan)
500
0
    glob->default_pgs_mask |= PGS_INDEXSCAN | PGS_INDEXONLYSCAN;
501
0
  if (enable_indexonlyscan)
502
0
    glob->default_pgs_mask |= PGS_CONSIDER_INDEXONLY;
503
0
  if (enable_bitmapscan)
504
0
    glob->default_pgs_mask |= PGS_BITMAPSCAN;
505
0
  if (enable_mergejoin)
506
0
  {
507
0
    glob->default_pgs_mask |= PGS_MERGEJOIN_PLAIN;
508
0
    if (enable_material)
509
0
      glob->default_pgs_mask |= PGS_MERGEJOIN_MATERIALIZE;
510
0
  }
511
0
  if (enable_nestloop)
512
0
  {
513
0
    glob->default_pgs_mask |= PGS_NESTLOOP_PLAIN;
514
0
    if (enable_material)
515
0
      glob->default_pgs_mask |= PGS_NESTLOOP_MATERIALIZE;
516
0
    if (enable_memoize)
517
0
      glob->default_pgs_mask |= PGS_NESTLOOP_MEMOIZE;
518
0
  }
519
0
  if (enable_hashjoin)
520
0
    glob->default_pgs_mask |= PGS_HASHJOIN;
521
0
  if (enable_gathermerge)
522
0
    glob->default_pgs_mask |= PGS_GATHER_MERGE;
523
0
  if (enable_partitionwise_join)
524
0
    glob->default_pgs_mask |= PGS_CONSIDER_PARTITIONWISE;
525
526
  /* Allow plugins to take control after we've initialized "glob" */
527
0
  if (planner_setup_hook)
528
0
    (*planner_setup_hook) (glob, parse, query_string, cursorOptions,
529
0
                 &tuple_fraction, es);
530
531
  /* primary planning entry point (may recurse for subqueries) */
532
0
  root = subquery_planner(glob, parse, NULL, NULL, NULL, false,
533
0
              tuple_fraction, NULL);
534
535
  /* Select best Path and turn it into a Plan */
536
0
  final_rel = fetch_upper_rel(root, UPPERREL_FINAL, NULL);
537
0
  best_path = get_cheapest_fractional_path(final_rel, tuple_fraction);
538
539
0
  top_plan = create_plan(root, best_path);
540
541
  /*
542
   * If creating a plan for a scrollable cursor, make sure it can run
543
   * backwards on demand.  Add a Material node at the top at need.
544
   */
545
0
  if (cursorOptions & CURSOR_OPT_SCROLL)
546
0
  {
547
0
    if (!ExecSupportsBackwardScan(top_plan))
548
0
      top_plan = materialize_finished_plan(top_plan);
549
0
  }
550
551
  /*
552
   * Optionally add a Gather node for testing purposes, provided this is
553
   * actually a safe thing to do.
554
   *
555
   * We can add Gather even when top_plan has parallel-safe initPlans, but
556
   * then we have to move the initPlans to the Gather node because of
557
   * SS_finalize_plan's limitations.  That would cause cosmetic breakage of
558
   * regression tests when debug_parallel_query = regress, because initPlans
559
   * that would normally appear on the top_plan move to the Gather, causing
560
   * them to disappear from EXPLAIN output.  That doesn't seem worth kluging
561
   * EXPLAIN to hide, so skip it when debug_parallel_query = regress.
562
   */
563
0
  if (debug_parallel_query != DEBUG_PARALLEL_OFF &&
564
0
    top_plan->parallel_safe &&
565
0
    (top_plan->initPlan == NIL ||
566
0
     debug_parallel_query != DEBUG_PARALLEL_REGRESS))
567
0
  {
568
0
    Gather     *gather = makeNode(Gather);
569
0
    Cost    initplan_cost;
570
0
    bool    unsafe_initplans;
571
572
0
    gather->plan.targetlist = top_plan->targetlist;
573
0
    gather->plan.qual = NIL;
574
0
    gather->plan.lefttree = top_plan;
575
0
    gather->plan.righttree = NULL;
576
0
    gather->num_workers = 1;
577
0
    gather->single_copy = true;
578
0
    gather->invisible = (debug_parallel_query == DEBUG_PARALLEL_REGRESS);
579
580
    /* Transfer any initPlans to the new top node */
581
0
    gather->plan.initPlan = top_plan->initPlan;
582
0
    top_plan->initPlan = NIL;
583
584
    /*
585
     * Since this Gather has no parallel-aware descendants to signal to,
586
     * we don't need a rescan Param.
587
     */
588
0
    gather->rescan_param = -1;
589
590
    /*
591
     * Ideally we'd use cost_gather here, but setting up dummy path data
592
     * to satisfy it doesn't seem much cleaner than knowing what it does.
593
     */
594
0
    gather->plan.startup_cost = top_plan->startup_cost +
595
0
      parallel_setup_cost;
596
0
    gather->plan.total_cost = top_plan->total_cost +
597
0
      parallel_setup_cost + parallel_tuple_cost * top_plan->plan_rows;
598
0
    gather->plan.plan_rows = top_plan->plan_rows;
599
0
    gather->plan.plan_width = top_plan->plan_width;
600
0
    gather->plan.parallel_aware = false;
601
0
    gather->plan.parallel_safe = false;
602
603
    /*
604
     * Delete the initplans' cost from top_plan.  We needn't add it to the
605
     * Gather node, since the above coding already included it there.
606
     */
607
0
    SS_compute_initplan_cost(gather->plan.initPlan,
608
0
                 &initplan_cost, &unsafe_initplans);
609
0
    top_plan->startup_cost -= initplan_cost;
610
0
    top_plan->total_cost -= initplan_cost;
611
612
    /* use parallel mode for parallel plans. */
613
0
    root->glob->parallelModeNeeded = true;
614
615
0
    top_plan = &gather->plan;
616
0
  }
617
618
  /*
619
   * If any Params were generated, run through the plan tree and compute
620
   * each plan node's extParam/allParam sets.  Ideally we'd merge this into
621
   * set_plan_references' tree traversal, but for now it has to be separate
622
   * because we need to visit subplans before not after main plan.
623
   */
624
0
  if (glob->paramExecTypes != NIL)
625
0
  {
626
0
    Assert(list_length(glob->subplans) == list_length(glob->subroots));
627
0
    forboth(lp, glob->subplans, lr, glob->subroots)
628
0
    {
629
0
      Plan     *subplan = (Plan *) lfirst(lp);
630
0
      PlannerInfo *subroot = lfirst_node(PlannerInfo, lr);
631
632
0
      SS_finalize_plan(subroot, subplan);
633
0
    }
634
0
    SS_finalize_plan(root, top_plan);
635
0
  }
636
637
  /* final cleanup of the plan */
638
0
  Assert(glob->finalrtable == NIL);
639
0
  Assert(glob->finalrteperminfos == NIL);
640
0
  Assert(glob->finalrowmarks == NIL);
641
0
  Assert(glob->resultRelations == NIL);
642
0
  Assert(glob->appendRelations == NIL);
643
0
  top_plan = set_plan_references(root, top_plan);
644
  /* ... and the subplans (both regular subplans and initplans) */
645
0
  Assert(list_length(glob->subplans) == list_length(glob->subroots));
646
0
  forboth(lp, glob->subplans, lr, glob->subroots)
647
0
  {
648
0
    Plan     *subplan = (Plan *) lfirst(lp);
649
0
    PlannerInfo *subroot = lfirst_node(PlannerInfo, lr);
650
651
0
    lfirst(lp) = set_plan_references(subroot, subplan);
652
0
  }
653
654
  /* build the PlannedStmt result */
655
0
  result = makeNode(PlannedStmt);
656
657
0
  result->commandType = parse->commandType;
658
0
  result->queryId = parse->queryId;
659
0
  result->planOrigin = PLAN_STMT_STANDARD;
660
0
  result->hasReturning = (parse->returningList != NIL);
661
0
  result->hasModifyingCTE = parse->hasModifyingCTE;
662
0
  result->canSetTag = parse->canSetTag;
663
0
  result->transientPlan = glob->transientPlan;
664
0
  result->dependsOnRole = glob->dependsOnRole;
665
0
  result->parallelModeNeeded = glob->parallelModeNeeded;
666
0
  result->planTree = top_plan;
667
0
  result->partPruneInfos = glob->partPruneInfos;
668
0
  result->rtable = glob->finalrtable;
669
0
  result->unprunableRelids = bms_difference(glob->allRelids,
670
0
                        glob->prunableRelids);
671
0
  result->permInfos = glob->finalrteperminfos;
672
0
  result->subrtinfos = glob->subrtinfos;
673
0
  result->appendRelations = glob->appendRelations;
674
0
  result->subplans = glob->subplans;
675
0
  result->rewindPlanIDs = glob->rewindPlanIDs;
676
0
  result->rowMarks = glob->finalrowmarks;
677
678
  /*
679
   * Compute resultRelationRelids and rowMarkRelids from resultRelations and
680
   * rowMarks. These can be used for cheap membership checks.
681
   */
682
0
  foreach(lc, glob->resultRelations)
683
0
    result->resultRelationRelids = bms_add_member(result->resultRelationRelids,
684
0
                            lfirst_int(lc));
685
0
  foreach(lc, glob->finalrowmarks)
686
0
    result->rowMarkRelids = bms_add_member(result->rowMarkRelids,
687
0
                         ((PlanRowMark *) lfirst(lc))->rti);
688
689
0
  result->relationOids = glob->relationOids;
690
0
  result->invalItems = glob->invalItems;
691
0
  result->paramExecTypes = glob->paramExecTypes;
692
  /* utilityStmt should be null, but we might as well copy it */
693
0
  result->utilityStmt = parse->utilityStmt;
694
0
  result->elidedNodes = glob->elidedNodes;
695
0
  result->stmt_location = parse->stmt_location;
696
0
  result->stmt_len = parse->stmt_len;
697
698
0
  result->jitFlags = PGJIT_NONE;
699
0
  if (jit_enabled && jit_above_cost >= 0 &&
700
0
    top_plan->total_cost > jit_above_cost)
701
0
  {
702
0
    result->jitFlags |= PGJIT_PERFORM;
703
704
    /*
705
     * Decide how much effort should be put into generating better code.
706
     */
707
0
    if (jit_optimize_above_cost >= 0 &&
708
0
      top_plan->total_cost > jit_optimize_above_cost)
709
0
      result->jitFlags |= PGJIT_OPT3;
710
0
    if (jit_inline_above_cost >= 0 &&
711
0
      top_plan->total_cost > jit_inline_above_cost)
712
0
      result->jitFlags |= PGJIT_INLINE;
713
714
    /*
715
     * Decide which operations should be JITed.
716
     */
717
0
    if (jit_expressions)
718
0
      result->jitFlags |= PGJIT_EXPR;
719
0
    if (jit_tuple_deforming)
720
0
      result->jitFlags |= PGJIT_DEFORM;
721
0
  }
722
723
  /* Allow plugins to take control before we discard "glob" */
724
0
  if (planner_shutdown_hook)
725
0
    (*planner_shutdown_hook) (glob, parse, query_string, result);
726
727
0
  if (glob->partition_directory != NULL)
728
0
    DestroyPartitionDirectory(glob->partition_directory);
729
730
0
  return result;
731
0
}
732
733
734
/*--------------------
735
 * subquery_planner
736
 *    Invokes the planner on a subquery.  We recurse to here for each
737
 *    sub-SELECT found in the query tree.
738
 *
739
 * glob is the global state for the current planner run.
740
 * parse is the querytree produced by the parser & rewriter.
741
 * plan_name is the name to assign to this subplan (NULL at the top level).
742
 * parent_root is the immediate parent Query's info (NULL at the top level).
743
 * alternative_root is a previously created PlannerInfo for which this query
744
 * level is an alternative implementation, or else NULL.
745
 * hasRecursion is true if this is a recursive WITH query.
746
 * tuple_fraction is the fraction of tuples we expect will be retrieved.
747
 * tuple_fraction is interpreted as explained for grouping_planner, below.
748
 * setops is used for set operation subqueries to provide the subquery with
749
 * the context in which it's being used so that Paths correctly sorted for the
750
 * set operation can be generated.  NULL when not planning a set operation
751
 * child, or when a child of a set op that isn't interested in sorted input.
752
 *
753
 * Basically, this routine does the stuff that should only be done once
754
 * per Query object.  It then calls grouping_planner.  At one time,
755
 * grouping_planner could be invoked recursively on the same Query object;
756
 * that's not currently true, but we keep the separation between the two
757
 * routines anyway, in case we need it again someday.
758
 *
759
 * subquery_planner will be called recursively to handle sub-Query nodes
760
 * found within the query's expressions and rangetable.
761
 *
762
 * Returns the PlannerInfo struct ("root") that contains all data generated
763
 * while planning the subquery.  In particular, the Path(s) attached to
764
 * the (UPPERREL_FINAL, NULL) upperrel represent our conclusions about the
765
 * cheapest way(s) to implement the query.  The top level will select the
766
 * best Path and pass it through createplan.c to produce a finished Plan.
767
 *--------------------
768
 */
769
PlannerInfo *
770
subquery_planner(PlannerGlobal *glob, Query *parse, char *plan_name,
771
         PlannerInfo *parent_root, PlannerInfo *alternative_root,
772
         bool hasRecursion, double tuple_fraction,
773
         SetOperationStmt *setops)
774
0
{
775
0
  PlannerInfo *root;
776
0
  List     *newWithCheckOptions;
777
0
  List     *newHaving;
778
0
  Bitmapset  *havingPushdownConflicts;
779
0
  int     havingIdx;
780
0
  bool    hasOuterJoins;
781
0
  bool    hasResultRTEs;
782
0
  RelOptInfo *final_rel;
783
0
  ListCell   *l;
784
785
  /* Create a PlannerInfo data structure for this subquery */
786
0
  root = makeNode(PlannerInfo);
787
0
  root->parse = parse;
788
0
  root->glob = glob;
789
0
  root->query_level = parent_root ? parent_root->query_level + 1 : 1;
790
0
  root->plan_name = plan_name;
791
0
  if (alternative_root != NULL)
792
0
    root->alternative_plan_name = alternative_root->plan_name;
793
0
  else
794
0
    root->alternative_plan_name = plan_name;
795
0
  root->parent_root = parent_root;
796
0
  root->plan_params = NIL;
797
0
  root->outer_params = NULL;
798
0
  root->planner_cxt = CurrentMemoryContext;
799
0
  root->init_plans = NIL;
800
0
  root->cte_plan_ids = NIL;
801
0
  root->multiexpr_params = NIL;
802
0
  root->join_domains = NIL;
803
0
  root->eq_classes = NIL;
804
0
  root->ec_merging_done = false;
805
0
  root->last_rinfo_serial = 0;
806
0
  root->all_result_relids =
807
0
    parse->resultRelation ? bms_make_singleton(parse->resultRelation) : NULL;
808
0
  root->leaf_result_relids = NULL; /* we'll find out leaf-ness later */
809
0
  root->append_rel_list = NIL;
810
0
  root->row_identity_vars = NIL;
811
0
  root->rowMarks = NIL;
812
0
  memset(root->upper_rels, 0, sizeof(root->upper_rels));
813
0
  memset(root->upper_targets, 0, sizeof(root->upper_targets));
814
0
  root->processed_groupClause = NIL;
815
0
  root->processed_distinctClause = NIL;
816
0
  root->processed_tlist = NIL;
817
0
  root->update_colnos = NIL;
818
0
  root->grouping_map = NULL;
819
0
  root->minmax_aggs = NIL;
820
0
  root->qual_security_level = 0;
821
0
  root->hasPseudoConstantQuals = false;
822
0
  root->hasAlternativeSubPlans = false;
823
0
  root->placeholdersFrozen = false;
824
0
  root->hasRecursion = hasRecursion;
825
0
  root->assumeReplanning = false;
826
0
  if (hasRecursion)
827
0
    root->wt_param_id = assign_special_exec_param(root);
828
0
  else
829
0
    root->wt_param_id = -1;
830
0
  root->non_recursive_path = NULL;
831
832
  /*
833
   * Create the top-level join domain.  This won't have valid contents until
834
   * deconstruct_jointree fills it in, but the node needs to exist before
835
   * that so we can build EquivalenceClasses referencing it.
836
   */
837
0
  root->join_domains = list_make1(makeNode(JoinDomain));
838
839
  /*
840
   * If there is a WITH list, process each WITH query and either convert it
841
   * to RTE_SUBQUERY RTE(s) or build an initplan SubPlan structure for it.
842
   */
843
0
  if (parse->cteList)
844
0
    SS_process_ctes(root);
845
846
  /*
847
   * If it's a MERGE command, transform the joinlist as appropriate.
848
   */
849
0
  transform_MERGE_to_join(parse);
850
851
  /*
852
   * Reject FOR PORTION OF on a generated column.  We can't write to a
853
   * virtual generated column, and a stored generated column should be
854
   * written by its own expression.
855
   *
856
   * We do this in the planner rather than parse analysis so that updatable
857
   * views have been rewritten; otherwise they would mask which columns are
858
   * generated.  We need to check before preprocess_relation_rtes(), so that
859
   * for virtual generated columns we still have the rangeVar.  After that
860
   * it is replaced by the column's expression.
861
   *
862
   * XXX: We plan to implement PERIODs as stored generated columns, so later
863
   * we will loosen this restriction if the column belongs to a PERIOD.
864
   */
865
0
  if (parse->forPortionOf)
866
0
  {
867
0
    ForPortionOfExpr *forPortionOf = parse->forPortionOf;
868
0
    RangeTblEntry *rte = rt_fetch(parse->resultRelation, parse->rtable);
869
870
0
    if (get_attgenerated(rte->relid, forPortionOf->rangeVar->varattno))
871
0
      ereport(ERROR,
872
0
          (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
873
0
           errmsg("cannot use generated column \"%s\" in FOR PORTION OF",
874
0
              get_attname(rte->relid,
875
0
                    forPortionOf->rangeVar->varattno,
876
0
                    false))));
877
0
  }
878
879
  /*
880
   * Scan the rangetable for relation RTEs and retrieve the necessary
881
   * catalog information for each relation.  Using this information, clear
882
   * the inh flag for any relation that has no children, collect not-null
883
   * attribute numbers for any relation that has column not-null
884
   * constraints, and expand virtual generated columns for any relation that
885
   * contains them.  Note that this step does not descend into sublinks and
886
   * subqueries; if we pull up any sublinks or subqueries below, their
887
   * relation RTEs are processed just before pulling them up.
888
   */
889
0
  parse = root->parse = preprocess_relation_rtes(root);
890
891
  /*
892
   * If the FROM clause is empty, replace it with a dummy RTE_RESULT RTE, so
893
   * that we don't need so many special cases to deal with that situation.
894
   */
895
0
  replace_empty_jointree(parse);
896
897
  /*
898
   * Look for ANY and EXISTS SubLinks in WHERE and JOIN/ON clauses, and try
899
   * to transform them into joins.  Note that this step does not descend
900
   * into subqueries; if we pull up any subqueries below, their SubLinks are
901
   * processed just before pulling them up.
902
   */
903
0
  if (parse->hasSubLinks)
904
0
    pull_up_sublinks(root);
905
906
  /*
907
   * Scan the rangetable for function RTEs, do const-simplification on them,
908
   * and then inline them if possible (producing subqueries that might get
909
   * pulled up next).  Recursion issues here are handled in the same way as
910
   * for SubLinks.
911
   */
912
0
  preprocess_function_rtes(root);
913
914
  /*
915
   * Check to see if any subqueries in the jointree can be merged into this
916
   * query.
917
   */
918
0
  pull_up_subqueries(root);
919
920
  /*
921
   * If this is a simple UNION ALL query, flatten it into an appendrel. We
922
   * do this now because it requires applying pull_up_subqueries to the leaf
923
   * queries of the UNION ALL, which weren't touched above because they
924
   * weren't referenced by the jointree (they will be after we do this).
925
   */
926
0
  if (parse->setOperations)
927
0
    flatten_simple_union_all(root);
928
929
  /*
930
   * Survey the rangetable to see what kinds of entries are present.  We can
931
   * skip some later processing if relevant SQL features are not used; for
932
   * example if there are no JOIN RTEs we can avoid the expense of doing
933
   * flatten_join_alias_vars().  This must be done after we have finished
934
   * adding rangetable entries, of course.  (Note: actually, processing of
935
   * inherited or partitioned rels can cause RTEs for their child tables to
936
   * get added later; but those must all be RTE_RELATION entries, so they
937
   * don't invalidate the conclusions drawn here.)
938
   */
939
0
  root->hasJoinRTEs = false;
940
0
  root->hasLateralRTEs = false;
941
0
  root->group_rtindex = 0;
942
0
  hasOuterJoins = false;
943
0
  hasResultRTEs = false;
944
0
  foreach(l, parse->rtable)
945
0
  {
946
0
    RangeTblEntry *rte = lfirst_node(RangeTblEntry, l);
947
948
0
    switch (rte->rtekind)
949
0
    {
950
0
      case RTE_JOIN:
951
0
        root->hasJoinRTEs = true;
952
0
        if (IS_OUTER_JOIN(rte->jointype))
953
0
          hasOuterJoins = true;
954
0
        break;
955
0
      case RTE_RESULT:
956
0
        hasResultRTEs = true;
957
0
        break;
958
0
      case RTE_GROUP:
959
0
        Assert(parse->hasGroupRTE);
960
0
        root->group_rtindex = list_cell_number(parse->rtable, l) + 1;
961
0
        break;
962
0
      default:
963
        /* No work here for other RTE types */
964
0
        break;
965
0
    }
966
967
0
    if (rte->lateral)
968
0
      root->hasLateralRTEs = true;
969
970
    /*
971
     * We can also determine the maximum security level required for any
972
     * securityQuals now.  Addition of inheritance-child RTEs won't affect
973
     * this, because child tables don't have their own securityQuals; see
974
     * expand_single_inheritance_child().
975
     */
976
0
    if (rte->securityQuals)
977
0
      root->qual_security_level = Max(root->qual_security_level,
978
0
                      list_length(rte->securityQuals));
979
0
  }
980
981
  /*
982
   * If we have now verified that the query target relation is
983
   * non-inheriting, mark it as a leaf target.
984
   */
985
0
  if (parse->resultRelation)
986
0
  {
987
0
    RangeTblEntry *rte = rt_fetch(parse->resultRelation, parse->rtable);
988
989
0
    if (!rte->inh)
990
0
      root->leaf_result_relids =
991
0
        bms_make_singleton(parse->resultRelation);
992
0
  }
993
994
  /*
995
   * This would be a convenient time to check access permissions for all
996
   * relations mentioned in the query, since it would be better to fail now,
997
   * before doing any detailed planning.  However, for historical reasons,
998
   * we leave this to be done at executor startup.
999
   *
1000
   * Note, however, that we do need to check access permissions for any view
1001
   * relations mentioned in the query, in order to prevent information being
1002
   * leaked by selectivity estimation functions, which only check view owner
1003
   * permissions on underlying tables (see all_rows_selectable() and its
1004
   * callers).  This is a little ugly, because it means that access
1005
   * permissions for views will be checked twice, which is another reason
1006
   * why it would be better to do all the ACL checks here.
1007
   */
1008
0
  foreach(l, parse->rtable)
1009
0
  {
1010
0
    RangeTblEntry *rte = lfirst_node(RangeTblEntry, l);
1011
1012
0
    if (rte->perminfoindex != 0 &&
1013
0
      rte->relkind == RELKIND_VIEW)
1014
0
    {
1015
0
      RTEPermissionInfo *perminfo;
1016
0
      bool    result;
1017
1018
0
      perminfo = getRTEPermissionInfo(parse->rteperminfos, rte);
1019
0
      result = ExecCheckOneRelPerms(perminfo);
1020
0
      if (!result)
1021
0
        aclcheck_error(ACLCHECK_NO_PRIV, OBJECT_VIEW,
1022
0
                 get_rel_name(perminfo->relid));
1023
0
    }
1024
0
  }
1025
1026
  /*
1027
   * Preprocess RowMark information.  We need to do this after subquery
1028
   * pullup, so that all base relations are present.
1029
   */
1030
0
  preprocess_rowmarks(root);
1031
1032
  /*
1033
   * Set hasHavingQual to remember if HAVING clause is present.  Needed
1034
   * because preprocess_expression will reduce a constant-true condition to
1035
   * an empty qual list ... but "HAVING TRUE" is not a semantic no-op.
1036
   */
1037
0
  root->hasHavingQual = (parse->havingQual != NULL);
1038
1039
  /*
1040
   * Do expression preprocessing on targetlist and quals, as well as other
1041
   * random expressions in the querytree.  Note that we do not need to
1042
   * handle sort/group expressions explicitly, because they are actually
1043
   * part of the targetlist.
1044
   */
1045
0
  parse->targetList = (List *)
1046
0
    preprocess_expression(root, (Node *) parse->targetList,
1047
0
                EXPRKIND_TARGET);
1048
1049
0
  newWithCheckOptions = NIL;
1050
0
  foreach(l, parse->withCheckOptions)
1051
0
  {
1052
0
    WithCheckOption *wco = lfirst_node(WithCheckOption, l);
1053
1054
0
    wco->qual = preprocess_expression(root, wco->qual,
1055
0
                      EXPRKIND_QUAL);
1056
0
    if (wco->qual != NULL)
1057
0
      newWithCheckOptions = lappend(newWithCheckOptions, wco);
1058
0
  }
1059
0
  parse->withCheckOptions = newWithCheckOptions;
1060
1061
0
  parse->returningList = (List *)
1062
0
    preprocess_expression(root, (Node *) parse->returningList,
1063
0
                EXPRKIND_TARGET);
1064
1065
0
  preprocess_qual_conditions(root, (Node *) parse->jointree);
1066
1067
0
  parse->havingQual = preprocess_expression(root, parse->havingQual,
1068
0
                        EXPRKIND_QUAL);
1069
1070
0
  foreach(l, parse->windowClause)
1071
0
  {
1072
0
    WindowClause *wc = lfirst_node(WindowClause, l);
1073
1074
    /* partitionClause/orderClause are sort/group expressions */
1075
0
    wc->startOffset = preprocess_expression(root, wc->startOffset,
1076
0
                        EXPRKIND_LIMIT);
1077
0
    wc->endOffset = preprocess_expression(root, wc->endOffset,
1078
0
                        EXPRKIND_LIMIT);
1079
0
  }
1080
1081
0
  parse->limitOffset = preprocess_expression(root, parse->limitOffset,
1082
0
                         EXPRKIND_LIMIT);
1083
0
  parse->limitCount = preprocess_expression(root, parse->limitCount,
1084
0
                        EXPRKIND_LIMIT);
1085
1086
0
  if (parse->onConflict)
1087
0
  {
1088
0
    parse->onConflict->arbiterElems = (List *)
1089
0
      preprocess_expression(root,
1090
0
                  (Node *) parse->onConflict->arbiterElems,
1091
0
                  EXPRKIND_ARBITER_ELEM);
1092
0
    parse->onConflict->arbiterWhere =
1093
0
      preprocess_expression(root,
1094
0
                  parse->onConflict->arbiterWhere,
1095
0
                  EXPRKIND_QUAL);
1096
0
    parse->onConflict->onConflictSet = (List *)
1097
0
      preprocess_expression(root,
1098
0
                  (Node *) parse->onConflict->onConflictSet,
1099
0
                  EXPRKIND_TARGET);
1100
0
    parse->onConflict->onConflictWhere =
1101
0
      preprocess_expression(root,
1102
0
                  parse->onConflict->onConflictWhere,
1103
0
                  EXPRKIND_QUAL);
1104
    /* exclRelTlist contains only Vars, so no preprocessing needed */
1105
0
  }
1106
1107
0
  if (parse->forPortionOf)
1108
0
  {
1109
0
    parse->forPortionOf->targetRange =
1110
0
      preprocess_expression(root,
1111
0
                  parse->forPortionOf->targetRange,
1112
0
                  EXPRKIND_TARGET);
1113
0
    if (contain_volatile_functions(parse->forPortionOf->targetRange))
1114
0
      ereport(ERROR,
1115
0
          (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1116
0
           errmsg("FOR PORTION OF bounds cannot contain volatile functions")));
1117
0
  }
1118
1119
0
  foreach(l, parse->mergeActionList)
1120
0
  {
1121
0
    MergeAction *action = (MergeAction *) lfirst(l);
1122
1123
0
    action->targetList = (List *)
1124
0
      preprocess_expression(root,
1125
0
                  (Node *) action->targetList,
1126
0
                  EXPRKIND_TARGET);
1127
0
    action->qual =
1128
0
      preprocess_expression(root,
1129
0
                  (Node *) action->qual,
1130
0
                  EXPRKIND_QUAL);
1131
0
  }
1132
1133
0
  parse->mergeJoinCondition =
1134
0
    preprocess_expression(root, parse->mergeJoinCondition, EXPRKIND_QUAL);
1135
1136
0
  root->append_rel_list = (List *)
1137
0
    preprocess_expression(root, (Node *) root->append_rel_list,
1138
0
                EXPRKIND_APPINFO);
1139
1140
  /* Also need to preprocess expressions within RTEs */
1141
0
  foreach(l, parse->rtable)
1142
0
  {
1143
0
    RangeTblEntry *rte = lfirst_node(RangeTblEntry, l);
1144
0
    int     kind;
1145
0
    ListCell   *lcsq;
1146
1147
0
    if (rte->rtekind == RTE_RELATION)
1148
0
    {
1149
0
      if (rte->tablesample)
1150
0
        rte->tablesample = (TableSampleClause *)
1151
0
          preprocess_expression(root,
1152
0
                      (Node *) rte->tablesample,
1153
0
                      EXPRKIND_TABLESAMPLE);
1154
0
    }
1155
0
    else if (rte->rtekind == RTE_SUBQUERY)
1156
0
    {
1157
      /*
1158
       * We don't want to do all preprocessing yet on the subquery's
1159
       * expressions, since that will happen when we plan it.  But if it
1160
       * contains any join aliases of our level, those have to get
1161
       * expanded now, because planning of the subquery won't do it.
1162
       * That's only possible if the subquery is LATERAL.
1163
       */
1164
0
      if (rte->lateral && root->hasJoinRTEs)
1165
0
        rte->subquery = (Query *)
1166
0
          flatten_join_alias_vars(root, root->parse,
1167
0
                      (Node *) rte->subquery);
1168
0
    }
1169
0
    else if (rte->rtekind == RTE_FUNCTION)
1170
0
    {
1171
      /* Preprocess the function expression(s) fully */
1172
0
      kind = rte->lateral ? EXPRKIND_RTFUNC_LATERAL : EXPRKIND_RTFUNC;
1173
0
      rte->functions = (List *)
1174
0
        preprocess_expression(root, (Node *) rte->functions, kind);
1175
0
    }
1176
0
    else if (rte->rtekind == RTE_TABLEFUNC)
1177
0
    {
1178
      /* Preprocess the function expression(s) fully */
1179
0
      kind = rte->lateral ? EXPRKIND_TABLEFUNC_LATERAL : EXPRKIND_TABLEFUNC;
1180
0
      rte->tablefunc = (TableFunc *)
1181
0
        preprocess_expression(root, (Node *) rte->tablefunc, kind);
1182
0
    }
1183
0
    else if (rte->rtekind == RTE_VALUES)
1184
0
    {
1185
      /* Preprocess the values lists fully */
1186
0
      kind = rte->lateral ? EXPRKIND_VALUES_LATERAL : EXPRKIND_VALUES;
1187
0
      rte->values_lists = (List *)
1188
0
        preprocess_expression(root, (Node *) rte->values_lists, kind);
1189
0
    }
1190
0
    else if (rte->rtekind == RTE_GROUP)
1191
0
    {
1192
      /* Preprocess the groupexprs list fully */
1193
0
      rte->groupexprs = (List *)
1194
0
        preprocess_expression(root, (Node *) rte->groupexprs,
1195
0
                    EXPRKIND_GROUPEXPR);
1196
0
    }
1197
1198
    /*
1199
     * Process each element of the securityQuals list as if it were a
1200
     * separate qual expression (as indeed it is).  We need to do it this
1201
     * way to get proper canonicalization of AND/OR structure.  Note that
1202
     * this converts each element into an implicit-AND sublist.
1203
     */
1204
0
    foreach(lcsq, rte->securityQuals)
1205
0
    {
1206
0
      lfirst(lcsq) = preprocess_expression(root,
1207
0
                         (Node *) lfirst(lcsq),
1208
0
                         EXPRKIND_QUAL);
1209
0
    }
1210
0
  }
1211
1212
  /*
1213
   * Now that we are done preprocessing expressions, and in particular done
1214
   * flattening join alias variables, get rid of the joinaliasvars lists.
1215
   * They no longer match what expressions in the rest of the tree look
1216
   * like, because we have not preprocessed expressions in those lists (and
1217
   * do not want to; for example, expanding a SubLink there would result in
1218
   * a useless unreferenced subplan).  Leaving them in place simply creates
1219
   * a hazard for later scans of the tree.  We could try to prevent that by
1220
   * using QTW_IGNORE_JOINALIASES in every tree scan done after this point,
1221
   * but that doesn't sound very reliable.
1222
   */
1223
0
  if (root->hasJoinRTEs)
1224
0
  {
1225
0
    foreach(l, parse->rtable)
1226
0
    {
1227
0
      RangeTblEntry *rte = lfirst_node(RangeTblEntry, l);
1228
1229
0
      rte->joinaliasvars = NIL;
1230
0
    }
1231
0
  }
1232
1233
  /*
1234
   * Before we flatten GROUP Vars, identify HAVING clauses whose equality
1235
   * semantics disagree with the GROUP BY's.  See find_having_conflicts.
1236
   */
1237
0
  if (parse->hasGroupRTE)
1238
0
    havingPushdownConflicts = find_having_conflicts(parse,
1239
0
                            root->group_rtindex);
1240
0
  else
1241
0
    havingPushdownConflicts = NULL;
1242
1243
  /*
1244
   * Replace any Vars in the subquery's targetlist and havingQual that
1245
   * reference GROUP outputs with the underlying grouping expressions.
1246
   *
1247
   * Note that we need to perform this replacement after we've preprocessed
1248
   * the grouping expressions.  This is to ensure that there is only one
1249
   * instance of SubPlan for each SubLink contained within the grouping
1250
   * expressions.
1251
   */
1252
0
  if (parse->hasGroupRTE)
1253
0
  {
1254
0
    parse->targetList = (List *)
1255
0
      flatten_group_exprs(root, root->parse, (Node *) parse->targetList);
1256
0
    parse->havingQual =
1257
0
      flatten_group_exprs(root, root->parse, parse->havingQual);
1258
0
  }
1259
1260
  /* Constant-folding might have removed all set-returning functions */
1261
0
  if (parse->hasTargetSRFs)
1262
0
    parse->hasTargetSRFs = expression_returns_set((Node *) parse->targetList);
1263
1264
  /*
1265
   * If we have grouping sets, expand the groupingSets tree of this query to
1266
   * a flat list of grouping sets.  We need to do this before optimizing
1267
   * HAVING, since we can't easily tell if there's an empty grouping set
1268
   * until we have this representation.
1269
   */
1270
0
  if (parse->groupingSets)
1271
0
  {
1272
0
    parse->groupingSets =
1273
0
      expand_grouping_sets(parse->groupingSets, parse->groupDistinct, -1);
1274
0
  }
1275
1276
  /*
1277
   * In some cases we may want to transfer a HAVING clause into WHERE. We
1278
   * cannot do so if the HAVING clause contains aggregates (obviously) or
1279
   * volatile functions (since a HAVING clause is supposed to be executed
1280
   * only once per group).  We also can't do this if there are any grouping
1281
   * sets and the clause references any columns that are nullable by the
1282
   * grouping sets; the nulled values of those columns are not available
1283
   * before the grouping step.  (The test on groupClause might seem wrong,
1284
   * but it's okay: it's just an optimization to avoid running pull_varnos
1285
   * when there cannot be any Vars in the HAVING clause.)
1286
   *
1287
   * We also cannot do this for HAVING clauses that conflict with GROUP BY
1288
   * on collation or operator family.  Both kinds of conflict are detected
1289
   * before flatten_group_exprs (see find_having_conflicts above) and
1290
   * recorded in the havingPushdownConflicts bitmapset.  The bitmapset
1291
   * indexes remain valid here because flatten_group_exprs uses
1292
   * expression_tree_mutator, which preserves the list length and ordering
1293
   * of havingQual.
1294
   *
1295
   * Also, it may be that the clause is so expensive to execute that we're
1296
   * better off doing it only once per group, despite the loss of
1297
   * selectivity.  This is hard to estimate short of doing the entire
1298
   * planning process twice, so we use a heuristic: clauses containing
1299
   * subplans are left in HAVING.  Otherwise, we move or copy the HAVING
1300
   * clause into WHERE, in hopes of eliminating tuples before aggregation
1301
   * instead of after.
1302
   *
1303
   * If the query has no empty grouping set then we can simply move such a
1304
   * clause into WHERE; any group that fails the clause will not be in the
1305
   * output because none of its tuples will reach the grouping or
1306
   * aggregation stage.  Otherwise we have to keep the clause in HAVING to
1307
   * ensure that we don't emit a bogus aggregated row.  But then the HAVING
1308
   * clause must be degenerate (variable-free), so we can copy it into WHERE
1309
   * so that query_planner() can use it in a gating Result node. (This could
1310
   * be done better, but it seems not worth optimizing.)
1311
   *
1312
   * Note that a HAVING clause may contain expressions that are not fully
1313
   * preprocessed.  This can happen if these expressions are part of
1314
   * grouping items.  In such cases, they are replaced with GROUP Vars in
1315
   * the parser and then replaced back after we're done with expression
1316
   * preprocessing on havingQual.  This is not an issue if the clause
1317
   * remains in HAVING, because these expressions will be matched to lower
1318
   * target items in setrefs.c.  However, if the clause is moved or copied
1319
   * into WHERE, we need to ensure that these expressions are fully
1320
   * preprocessed.
1321
   *
1322
   * Note that both havingQual and parse->jointree->quals are in
1323
   * implicitly-ANDed-list form at this point, even though they are declared
1324
   * as Node *.
1325
   */
1326
0
  newHaving = NIL;
1327
0
  havingIdx = 0;
1328
0
  foreach(l, (List *) parse->havingQual)
1329
0
  {
1330
0
    Node     *havingclause = (Node *) lfirst(l);
1331
1332
0
    if (contain_agg_clause(havingclause) ||
1333
0
      contain_volatile_functions(havingclause) ||
1334
0
      contain_subplans(havingclause) ||
1335
0
      bms_is_member(havingIdx, havingPushdownConflicts) ||
1336
0
      (parse->groupClause && parse->groupingSets &&
1337
0
       bms_is_member(root->group_rtindex, pull_varnos(root, havingclause))))
1338
0
    {
1339
      /* keep it in HAVING */
1340
0
      newHaving = lappend(newHaving, havingclause);
1341
0
    }
1342
0
    else if (parse->groupClause &&
1343
0
         (parse->groupingSets == NIL ||
1344
0
          (List *) linitial(parse->groupingSets) != NIL))
1345
0
    {
1346
      /* There is GROUP BY, but no empty grouping set */
1347
0
      Node     *whereclause;
1348
1349
      /* Preprocess the HAVING clause fully */
1350
0
      whereclause = preprocess_expression(root, havingclause,
1351
0
                        EXPRKIND_QUAL);
1352
      /* ... and move it to WHERE */
1353
0
      parse->jointree->quals = (Node *)
1354
0
        list_concat((List *) parse->jointree->quals,
1355
0
              (List *) whereclause);
1356
0
    }
1357
0
    else
1358
0
    {
1359
      /* There is an empty grouping set (perhaps implicitly) */
1360
0
      Node     *whereclause;
1361
1362
      /* Preprocess the HAVING clause fully */
1363
0
      whereclause = preprocess_expression(root, copyObject(havingclause),
1364
0
                        EXPRKIND_QUAL);
1365
      /* ... and put a copy in WHERE */
1366
0
      parse->jointree->quals = (Node *)
1367
0
        list_concat((List *) parse->jointree->quals,
1368
0
              (List *) whereclause);
1369
      /* ... and also keep it in HAVING */
1370
0
      newHaving = lappend(newHaving, havingclause);
1371
0
    }
1372
1373
0
    havingIdx++;
1374
0
  }
1375
0
  parse->havingQual = (Node *) newHaving;
1376
1377
  /*
1378
   * If we have any outer joins, try to reduce them to plain inner joins.
1379
   * This step is most easily done after we've done expression
1380
   * preprocessing.
1381
   */
1382
0
  if (hasOuterJoins)
1383
0
    reduce_outer_joins(root);
1384
1385
  /*
1386
   * If we have any RTE_RESULT relations, see if they can be deleted from
1387
   * the jointree.  We also rely on this processing to flatten single-child
1388
   * FromExprs underneath outer joins.  This step is most effectively done
1389
   * after we've done expression preprocessing and outer join reduction.
1390
   */
1391
0
  if (hasResultRTEs || hasOuterJoins)
1392
0
    remove_useless_result_rtes(root);
1393
1394
  /*
1395
   * Do the main planning.
1396
   */
1397
0
  grouping_planner(root, tuple_fraction, setops);
1398
1399
  /*
1400
   * Capture the set of outer-level param IDs we have access to, for use in
1401
   * extParam/allParam calculations later.
1402
   */
1403
0
  SS_identify_outer_params(root);
1404
1405
  /*
1406
   * If any initPlans were created in this query level, adjust the surviving
1407
   * Paths' costs and parallel-safety flags to account for them.  The
1408
   * initPlans won't actually get attached to the plan tree till
1409
   * create_plan() runs, but we must include their effects now.
1410
   */
1411
0
  final_rel = fetch_upper_rel(root, UPPERREL_FINAL, NULL);
1412
0
  SS_charge_for_initplans(root, final_rel);
1413
1414
  /*
1415
   * Make sure we've identified the cheapest Path for the final rel.  (By
1416
   * doing this here not in grouping_planner, we include initPlan costs in
1417
   * the decision, though it's unlikely that will change anything.)
1418
   */
1419
0
  set_cheapest(final_rel);
1420
1421
0
  return root;
1422
0
}
1423
1424
/*
1425
 * preprocess_expression
1426
 *    Do subquery_planner's preprocessing work for an expression,
1427
 *    which can be a targetlist, a WHERE clause (including JOIN/ON
1428
 *    conditions), a HAVING clause, or a few other things.
1429
 */
1430
static Node *
1431
preprocess_expression(PlannerInfo *root, Node *expr, int kind)
1432
0
{
1433
  /*
1434
   * Fall out quickly if expression is empty.  This occurs often enough to
1435
   * be worth checking.  Note that null->null is the correct conversion for
1436
   * implicit-AND result format, too.
1437
   */
1438
0
  if (expr == NULL)
1439
0
    return NULL;
1440
1441
  /*
1442
   * If the query has any join RTEs, replace join alias variables with
1443
   * base-relation variables.  We must do this first, since any expressions
1444
   * we may extract from the joinaliasvars lists have not been preprocessed.
1445
   * For example, if we did this after sublink processing, sublinks expanded
1446
   * out from join aliases would not get processed.  But we can skip this in
1447
   * non-lateral RTE functions, VALUES lists, and TABLESAMPLE clauses, since
1448
   * they can't contain any Vars of the current query level.
1449
   */
1450
0
  if (root->hasJoinRTEs &&
1451
0
    !(kind == EXPRKIND_RTFUNC ||
1452
0
      kind == EXPRKIND_VALUES ||
1453
0
      kind == EXPRKIND_TABLESAMPLE ||
1454
0
      kind == EXPRKIND_TABLEFUNC))
1455
0
    expr = flatten_join_alias_vars(root, root->parse, expr);
1456
1457
  /*
1458
   * Simplify constant expressions.  For function RTEs, this was already
1459
   * done by preprocess_function_rtes.  (But note we must do it again for
1460
   * EXPRKIND_RTFUNC_LATERAL, because those might by now contain
1461
   * un-simplified subexpressions inserted by flattening of subqueries or
1462
   * join alias variables.)
1463
   *
1464
   * Note: an essential effect of this is to convert named-argument function
1465
   * calls to positional notation and insert the current actual values of
1466
   * any default arguments for functions.  To ensure that happens, we *must*
1467
   * process all expressions here.  Previous PG versions sometimes skipped
1468
   * const-simplification if it didn't seem worth the trouble, but we can't
1469
   * do that anymore.
1470
   *
1471
   * Note: this also flattens nested AND and OR expressions into N-argument
1472
   * form.  All processing of a qual expression after this point must be
1473
   * careful to maintain AND/OR flatness --- that is, do not generate a tree
1474
   * with AND directly under AND, nor OR directly under OR.
1475
   */
1476
0
  if (kind != EXPRKIND_RTFUNC)
1477
0
    expr = eval_const_expressions(root, expr);
1478
1479
  /*
1480
   * If it's a qual or havingQual, canonicalize it.
1481
   */
1482
0
  if (kind == EXPRKIND_QUAL)
1483
0
  {
1484
0
    expr = (Node *) canonicalize_qual((Expr *) expr, false);
1485
1486
#ifdef OPTIMIZER_DEBUG
1487
    printf("After canonicalize_qual()\n");
1488
    pprint(expr);
1489
#endif
1490
0
  }
1491
1492
  /*
1493
   * Check for ANY ScalarArrayOpExpr with Const arrays and set the
1494
   * hashfuncid of any that might execute more quickly by using hash lookups
1495
   * instead of a linear search.
1496
   */
1497
0
  if (kind == EXPRKIND_QUAL || kind == EXPRKIND_TARGET)
1498
0
  {
1499
0
    convert_saop_to_hashed_saop(expr);
1500
0
  }
1501
1502
  /* Expand SubLinks to SubPlans */
1503
0
  if (root->parse->hasSubLinks)
1504
0
    expr = SS_process_sublinks(root, expr, (kind == EXPRKIND_QUAL));
1505
1506
  /*
1507
   * XXX do not insert anything here unless you have grokked the comments in
1508
   * SS_replace_correlation_vars ...
1509
   */
1510
1511
  /* Replace uplevel vars with Param nodes (this IS possible in VALUES) */
1512
0
  if (root->query_level > 1)
1513
0
    expr = SS_replace_correlation_vars(root, expr);
1514
1515
  /*
1516
   * If it's a qual or havingQual, convert it to implicit-AND format. (We
1517
   * don't want to do this before eval_const_expressions, since the latter
1518
   * would be unable to simplify a top-level AND correctly. Also,
1519
   * SS_process_sublinks expects explicit-AND format.)
1520
   */
1521
0
  if (kind == EXPRKIND_QUAL)
1522
0
    expr = (Node *) make_ands_implicit((Expr *) expr);
1523
1524
0
  return expr;
1525
0
}
1526
1527
/*
1528
 * preprocess_qual_conditions
1529
 *    Recursively scan the query's jointree and do subquery_planner's
1530
 *    preprocessing work on each qual condition found therein.
1531
 */
1532
static void
1533
preprocess_qual_conditions(PlannerInfo *root, Node *jtnode)
1534
0
{
1535
0
  if (jtnode == NULL)
1536
0
    return;
1537
0
  if (IsA(jtnode, RangeTblRef))
1538
0
  {
1539
    /* nothing to do here */
1540
0
  }
1541
0
  else if (IsA(jtnode, FromExpr))
1542
0
  {
1543
0
    FromExpr   *f = (FromExpr *) jtnode;
1544
0
    ListCell   *l;
1545
1546
0
    foreach(l, f->fromlist)
1547
0
      preprocess_qual_conditions(root, lfirst(l));
1548
1549
0
    f->quals = preprocess_expression(root, f->quals, EXPRKIND_QUAL);
1550
0
  }
1551
0
  else if (IsA(jtnode, JoinExpr))
1552
0
  {
1553
0
    JoinExpr   *j = (JoinExpr *) jtnode;
1554
1555
0
    preprocess_qual_conditions(root, j->larg);
1556
0
    preprocess_qual_conditions(root, j->rarg);
1557
1558
0
    j->quals = preprocess_expression(root, j->quals, EXPRKIND_QUAL);
1559
0
  }
1560
0
  else
1561
0
    elog(ERROR, "unrecognized node type: %d",
1562
0
       (int) nodeTag(jtnode));
1563
0
}
1564
1565
/*
1566
 * find_having_conflicts
1567
 *    Identify HAVING clauses that must not be moved to WHERE because they
1568
 *    apply a different equivalence relation than GROUP BY.  Pushing such a
1569
 *    clause to WHERE would filter individual rows before grouping happens,
1570
 *    eliminating rows that GROUP BY would have merged into a single group
1571
 *    and thereby changing aggregate results.
1572
 *
1573
 * The actual walking is done by expression_has_grouping_conflict; see that
1574
 * function for the kinds of conflict it looks for.  We just iterate over
1575
 * havingQual and supply a HAVING-specific callback that identifies GROUP
1576
 * Vars.
1577
 *
1578
 * This must be called before flatten_group_exprs, while the HAVING clause
1579
 * still contains GROUP Vars (Vars referencing RTE_GROUP).  These GROUP Vars
1580
 * carry the GROUP BY collation as their varcollid and let us recover the
1581
 * grouping eqop via varattno.  After flattening, those Vars are replaced by
1582
 * the underlying expressions, and matching back to grouping expressions is
1583
 * much harder.
1584
 *
1585
 * Returns a Bitmapset of zero-based indexes into the havingQual list for
1586
 * clauses that conflict and must stay in HAVING.
1587
 */
1588
static Bitmapset *
1589
find_having_conflicts(Query *parse, Index group_rtindex)
1590
0
{
1591
0
  Bitmapset  *result = NULL;
1592
0
  having_grouping_ctx ctx;
1593
0
  int     idx;
1594
1595
0
  if (parse->havingQual == NULL)
1596
0
    return NULL;
1597
1598
0
  ctx.parse = parse;
1599
0
  ctx.group_rtindex = group_rtindex;
1600
1601
0
  idx = 0;
1602
0
  foreach_ptr(Node, clause, (List *) parse->havingQual)
1603
0
  {
1604
0
    if (expression_has_grouping_conflict(clause, having_var_grouping_eqop,
1605
0
                       &ctx))
1606
0
      result = bms_add_member(result, idx);
1607
0
    idx++;
1608
0
  }
1609
1610
0
  return result;
1611
0
}
1612
1613
/*
1614
 * having_var_grouping_eqop
1615
 *    grouping_eqop_callback for find_having_conflicts.
1616
 *
1617
 * Returns the GROUP BY equality operator for 'var' if it references the
1618
 * query's RTE_GROUP, or InvalidOid otherwise.
1619
 */
1620
static Oid
1621
having_var_grouping_eqop(Var *var, void *context)
1622
0
{
1623
0
  having_grouping_ctx *ctx = (having_grouping_ctx *) context;
1624
1625
0
  if (var->varno != ctx->group_rtindex || var->varlevelsup != 0)
1626
0
    return InvalidOid;
1627
1628
0
  return group_var_eqop(ctx->parse, var);
1629
0
}
1630
1631
/*
1632
 * group_var_eqop
1633
 *    Return the equality operator that GROUP BY uses for the given GROUP Var.
1634
 *
1635
 * A GROUP Var's varattno is its 1-based position in the RTE_GROUP's groupexprs
1636
 * list, which addRangeTableEntryForGroup built by iterating parse->groupClause
1637
 * and including every SortGroupClause whose TLE was present in the targetlist.
1638
 * Replay that traversal here to recover the SortGroupClause for the given
1639
 * varattno.
1640
 */
1641
static Oid
1642
group_var_eqop(Query *parse, Var *var)
1643
0
{
1644
0
  int     counter = 0;
1645
1646
0
  Assert(var->varlevelsup == 0);
1647
1648
0
  foreach_node(SortGroupClause, sgc, parse->groupClause)
1649
0
  {
1650
0
    if (get_sortgroupclause_tle(sgc, parse->targetList) == NULL)
1651
0
      continue;
1652
0
    if (++counter == var->varattno)
1653
0
      return sgc->eqop;
1654
0
  }
1655
1656
0
  elog(ERROR, "could not find GROUP clause for GROUP Var attno %d",
1657
0
     var->varattno);
1658
0
  return InvalidOid;     /* keep compiler quiet */
1659
0
}
1660
1661
/*
1662
 * preprocess_phv_expression
1663
 *    Do preprocessing on a PlaceHolderVar expression that's been pulled up.
1664
 *
1665
 * If a LATERAL subquery references an output of another subquery, and that
1666
 * output must be wrapped in a PlaceHolderVar because of an intermediate outer
1667
 * join, then we'll push the PlaceHolderVar expression down into the subquery
1668
 * and later pull it back up during find_lateral_references, which runs after
1669
 * subquery_planner has preprocessed all the expressions that were in the
1670
 * current query level to start with.  So we need to preprocess it then.
1671
 */
1672
Expr *
1673
preprocess_phv_expression(PlannerInfo *root, Expr *expr)
1674
0
{
1675
0
  return (Expr *) preprocess_expression(root, (Node *) expr, EXPRKIND_PHV);
1676
0
}
1677
1678
/*--------------------
1679
 * grouping_planner
1680
 *    Perform planning steps related to grouping, aggregation, etc.
1681
 *
1682
 * This function adds all required top-level processing to the scan/join
1683
 * Path(s) produced by query_planner.
1684
 *
1685
 * tuple_fraction is the fraction of tuples we expect will be retrieved.
1686
 * tuple_fraction is interpreted as follows:
1687
 *    0: expect all tuples to be retrieved (normal case)
1688
 *    0 < tuple_fraction < 1: expect the given fraction of tuples available
1689
 *    from the plan to be retrieved
1690
 *    tuple_fraction >= 1: tuple_fraction is the absolute number of tuples
1691
 *    expected to be retrieved (ie, a LIMIT specification).
1692
 * setops is used for set operation subqueries to provide the subquery with
1693
 * the context in which it's being used so that Paths correctly sorted for the
1694
 * set operation can be generated.  NULL when not planning a set operation
1695
 * child, or when a child of a set op that isn't interested in sorted input.
1696
 *
1697
 * Returns nothing; the useful output is in the Paths we attach to the
1698
 * (UPPERREL_FINAL, NULL) upperrel in *root.  In addition,
1699
 * root->processed_tlist contains the final processed targetlist.
1700
 *
1701
 * Note that we have not done set_cheapest() on the final rel; it's convenient
1702
 * to leave this to the caller.
1703
 *--------------------
1704
 */
1705
static void
1706
grouping_planner(PlannerInfo *root, double tuple_fraction,
1707
         SetOperationStmt *setops)
1708
0
{
1709
0
  Query    *parse = root->parse;
1710
0
  int64   offset_est = 0;
1711
0
  int64   count_est = 0;
1712
0
  double    limit_tuples = -1.0;
1713
0
  bool    have_postponed_srfs = false;
1714
0
  PathTarget *final_target;
1715
0
  List     *final_targets;
1716
0
  List     *final_targets_contain_srfs;
1717
0
  bool    final_target_parallel_safe;
1718
0
  RelOptInfo *current_rel;
1719
0
  RelOptInfo *final_rel;
1720
0
  FinalPathExtraData extra;
1721
0
  ListCell   *lc;
1722
1723
  /* Tweak caller-supplied tuple_fraction if have LIMIT/OFFSET */
1724
0
  if (parse->limitCount || parse->limitOffset)
1725
0
  {
1726
0
    tuple_fraction = preprocess_limit(root, tuple_fraction,
1727
0
                      &offset_est, &count_est);
1728
1729
    /*
1730
     * If we have a known LIMIT, and don't have an unknown OFFSET, we can
1731
     * estimate the effects of using a bounded sort.
1732
     */
1733
0
    if (count_est > 0 && offset_est >= 0)
1734
0
      limit_tuples = (double) count_est + (double) offset_est;
1735
0
  }
1736
1737
  /* Make tuple_fraction accessible to lower-level routines */
1738
0
  root->tuple_fraction = tuple_fraction;
1739
1740
0
  if (parse->setOperations)
1741
0
  {
1742
    /*
1743
     * Construct Paths for set operations.  The results will not need any
1744
     * work except perhaps a top-level sort and/or LIMIT.  Note that any
1745
     * special work for recursive unions is the responsibility of
1746
     * plan_set_operations.
1747
     */
1748
0
    current_rel = plan_set_operations(root);
1749
1750
    /*
1751
     * We should not need to call preprocess_targetlist, since we must be
1752
     * in a SELECT query node.  Instead, use the processed_tlist returned
1753
     * by plan_set_operations (since this tells whether it returned any
1754
     * resjunk columns!), and transfer any sort key information from the
1755
     * original tlist.
1756
     */
1757
0
    Assert(parse->commandType == CMD_SELECT);
1758
1759
    /* for safety, copy processed_tlist instead of modifying in-place */
1760
0
    root->processed_tlist =
1761
0
      postprocess_setop_tlist(copyObject(root->processed_tlist),
1762
0
                  parse->targetList);
1763
1764
    /* Also extract the PathTarget form of the setop result tlist */
1765
0
    final_target = current_rel->cheapest_total_path->pathtarget;
1766
1767
    /* And check whether it's parallel safe */
1768
0
    final_target_parallel_safe =
1769
0
      is_parallel_safe(root, (Node *) final_target->exprs);
1770
1771
    /* The setop result tlist couldn't contain any SRFs */
1772
0
    Assert(!parse->hasTargetSRFs);
1773
0
    final_targets = final_targets_contain_srfs = NIL;
1774
1775
    /*
1776
     * Can't handle FOR [KEY] UPDATE/SHARE here (parser should have
1777
     * checked already, but let's make sure).
1778
     */
1779
0
    if (parse->rowMarks)
1780
0
      ereport(ERROR,
1781
0
          (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1782
      /*------
1783
        translator: %s is a SQL row locking clause such as FOR UPDATE */
1784
0
           errmsg("%s is not allowed with UNION/INTERSECT/EXCEPT",
1785
0
              LCS_asString(linitial_node(RowMarkClause,
1786
0
                             parse->rowMarks)->strength))));
1787
1788
    /*
1789
     * Calculate pathkeys that represent result ordering requirements
1790
     */
1791
0
    Assert(parse->distinctClause == NIL);
1792
0
    root->sort_pathkeys = make_pathkeys_for_sortclauses(root,
1793
0
                              parse->sortClause,
1794
0
                              root->processed_tlist);
1795
0
  }
1796
0
  else
1797
0
  {
1798
    /* No set operations, do regular planning */
1799
0
    PathTarget *sort_input_target;
1800
0
    List     *sort_input_targets;
1801
0
    List     *sort_input_targets_contain_srfs;
1802
0
    bool    sort_input_target_parallel_safe;
1803
0
    PathTarget *grouping_target;
1804
0
    List     *grouping_targets;
1805
0
    List     *grouping_targets_contain_srfs;
1806
0
    bool    grouping_target_parallel_safe;
1807
0
    PathTarget *scanjoin_target;
1808
0
    List     *scanjoin_targets;
1809
0
    List     *scanjoin_targets_contain_srfs;
1810
0
    bool    scanjoin_target_parallel_safe;
1811
0
    bool    scanjoin_target_same_exprs;
1812
0
    bool    have_grouping;
1813
0
    WindowFuncLists *wflists = NULL;
1814
0
    List     *activeWindows = NIL;
1815
0
    grouping_sets_data *gset_data = NULL;
1816
0
    standard_qp_extra qp_extra;
1817
1818
    /* A recursive query should always have setOperations */
1819
0
    Assert(!root->hasRecursion);
1820
1821
    /* Preprocess grouping sets and GROUP BY clause, if any */
1822
0
    if (parse->groupingSets)
1823
0
    {
1824
0
      gset_data = preprocess_grouping_sets(root);
1825
0
    }
1826
0
    else if (parse->groupClause)
1827
0
    {
1828
      /* Preprocess regular GROUP BY clause, if any */
1829
0
      root->processed_groupClause = preprocess_groupclause(root, NIL);
1830
0
    }
1831
1832
    /*
1833
     * Preprocess targetlist.  Note that much of the remaining planning
1834
     * work will be done with the PathTarget representation of tlists, but
1835
     * we must also maintain the full representation of the final tlist so
1836
     * that we can transfer its decoration (resnames etc) to the topmost
1837
     * tlist of the finished Plan.  This is kept in processed_tlist.
1838
     */
1839
0
    preprocess_targetlist(root);
1840
1841
    /*
1842
     * Mark all the aggregates with resolved aggtranstypes, and detect
1843
     * aggregates that are duplicates or can share transition state.  We
1844
     * must do this before slicing and dicing the tlist into various
1845
     * pathtargets, else some copies of the Aggref nodes might escape
1846
     * being marked.
1847
     */
1848
0
    if (parse->hasAggs)
1849
0
    {
1850
0
      preprocess_aggrefs(root, (Node *) root->processed_tlist);
1851
0
      preprocess_aggrefs(root, (Node *) parse->havingQual);
1852
0
    }
1853
1854
    /*
1855
     * Locate any window functions in the tlist.  (We don't need to look
1856
     * anywhere else, since expressions used in ORDER BY will be in there
1857
     * too.)  Note that they could all have been eliminated by constant
1858
     * folding, in which case we don't need to do any more work.
1859
     */
1860
0
    if (parse->hasWindowFuncs)
1861
0
    {
1862
0
      wflists = find_window_functions((Node *) root->processed_tlist,
1863
0
                      list_length(parse->windowClause));
1864
0
      if (wflists->numWindowFuncs > 0)
1865
0
      {
1866
        /*
1867
         * See if any modifications can be made to each WindowClause
1868
         * to allow the executor to execute the WindowFuncs more
1869
         * quickly.
1870
         */
1871
0
        optimize_window_clauses(root, wflists);
1872
1873
        /* Extract the list of windows actually in use. */
1874
0
        activeWindows = select_active_windows(root, wflists);
1875
1876
        /* Make sure they all have names, for EXPLAIN's use. */
1877
0
        name_active_windows(activeWindows);
1878
0
      }
1879
0
      else
1880
0
        parse->hasWindowFuncs = false;
1881
0
    }
1882
1883
    /*
1884
     * Preprocess MIN/MAX aggregates, if any.  Note: be careful about
1885
     * adding logic between here and the query_planner() call.  Anything
1886
     * that is needed in MIN/MAX-optimizable cases will have to be
1887
     * duplicated in planagg.c.
1888
     */
1889
0
    if (parse->hasAggs)
1890
0
      preprocess_minmax_aggregates(root);
1891
1892
    /*
1893
     * Figure out whether there's a hard limit on the number of rows that
1894
     * query_planner's result subplan needs to return.  Even if we know a
1895
     * hard limit overall, it doesn't apply if the query has any
1896
     * grouping/aggregation operations, or SRFs in the tlist.
1897
     */
1898
0
    if (parse->groupClause ||
1899
0
      parse->groupingSets ||
1900
0
      parse->distinctClause ||
1901
0
      parse->hasAggs ||
1902
0
      parse->hasWindowFuncs ||
1903
0
      parse->hasTargetSRFs ||
1904
0
      root->hasHavingQual)
1905
0
      root->limit_tuples = -1.0;
1906
0
    else
1907
0
      root->limit_tuples = limit_tuples;
1908
1909
    /* Set up data needed by standard_qp_callback */
1910
0
    qp_extra.activeWindows = activeWindows;
1911
0
    qp_extra.gset_data = gset_data;
1912
1913
    /*
1914
     * If we're a subquery for a set operation, store the SetOperationStmt
1915
     * in qp_extra.
1916
     */
1917
0
    qp_extra.setop = setops;
1918
1919
    /*
1920
     * Generate the best unsorted and presorted paths for the scan/join
1921
     * portion of this Query, ie the processing represented by the
1922
     * FROM/WHERE clauses.  (Note there may not be any presorted paths.)
1923
     * We also generate (in standard_qp_callback) pathkey representations
1924
     * of the query's sort clause, distinct clause, etc.
1925
     */
1926
0
    current_rel = query_planner(root, standard_qp_callback, &qp_extra);
1927
1928
    /*
1929
     * Convert the query's result tlist into PathTarget format.
1930
     *
1931
     * Note: this cannot be done before query_planner() has performed
1932
     * appendrel expansion, because that might add resjunk entries to
1933
     * root->processed_tlist.  Waiting till afterwards is also helpful
1934
     * because the target width estimates can use per-Var width numbers
1935
     * that were obtained within query_planner().
1936
     */
1937
0
    final_target = create_pathtarget(root, root->processed_tlist);
1938
0
    final_target_parallel_safe =
1939
0
      is_parallel_safe(root, (Node *) final_target->exprs);
1940
1941
    /*
1942
     * If ORDER BY was given, consider whether we should use a post-sort
1943
     * projection, and compute the adjusted target for preceding steps if
1944
     * so.
1945
     */
1946
0
    if (parse->sortClause)
1947
0
    {
1948
0
      sort_input_target = make_sort_input_target(root,
1949
0
                             final_target,
1950
0
                             &have_postponed_srfs);
1951
0
      sort_input_target_parallel_safe =
1952
0
        is_parallel_safe(root, (Node *) sort_input_target->exprs);
1953
0
    }
1954
0
    else
1955
0
    {
1956
0
      sort_input_target = final_target;
1957
0
      sort_input_target_parallel_safe = final_target_parallel_safe;
1958
0
    }
1959
1960
    /*
1961
     * If we have window functions to deal with, the output from any
1962
     * grouping step needs to be what the window functions want;
1963
     * otherwise, it should be sort_input_target.
1964
     */
1965
0
    if (activeWindows)
1966
0
    {
1967
0
      grouping_target = make_window_input_target(root,
1968
0
                             final_target,
1969
0
                             activeWindows);
1970
0
      grouping_target_parallel_safe =
1971
0
        is_parallel_safe(root, (Node *) grouping_target->exprs);
1972
0
    }
1973
0
    else
1974
0
    {
1975
0
      grouping_target = sort_input_target;
1976
0
      grouping_target_parallel_safe = sort_input_target_parallel_safe;
1977
0
    }
1978
1979
    /*
1980
     * If we have grouping or aggregation to do, the topmost scan/join
1981
     * plan node must emit what the grouping step wants; otherwise, it
1982
     * should emit grouping_target.
1983
     */
1984
0
    have_grouping = (parse->groupClause || parse->groupingSets ||
1985
0
             parse->hasAggs || root->hasHavingQual);
1986
0
    if (have_grouping)
1987
0
    {
1988
0
      scanjoin_target = make_group_input_target(root, final_target);
1989
0
      scanjoin_target_parallel_safe =
1990
0
        is_parallel_safe(root, (Node *) scanjoin_target->exprs);
1991
0
    }
1992
0
    else
1993
0
    {
1994
0
      scanjoin_target = grouping_target;
1995
0
      scanjoin_target_parallel_safe = grouping_target_parallel_safe;
1996
0
    }
1997
1998
    /*
1999
     * If there are any SRFs in the targetlist, we must separate each of
2000
     * these PathTargets into SRF-computing and SRF-free targets.  Replace
2001
     * each of the named targets with a SRF-free version, and remember the
2002
     * list of additional projection steps we need to add afterwards.
2003
     */
2004
0
    if (parse->hasTargetSRFs)
2005
0
    {
2006
      /* final_target doesn't recompute any SRFs in sort_input_target */
2007
0
      split_pathtarget_at_srfs(root, final_target, sort_input_target,
2008
0
                   &final_targets,
2009
0
                   &final_targets_contain_srfs);
2010
0
      final_target = linitial_node(PathTarget, final_targets);
2011
0
      Assert(!linitial_int(final_targets_contain_srfs));
2012
      /* likewise for sort_input_target vs. grouping_target */
2013
0
      split_pathtarget_at_srfs(root, sort_input_target, grouping_target,
2014
0
                   &sort_input_targets,
2015
0
                   &sort_input_targets_contain_srfs);
2016
0
      sort_input_target = linitial_node(PathTarget, sort_input_targets);
2017
0
      Assert(!linitial_int(sort_input_targets_contain_srfs));
2018
      /* likewise for grouping_target vs. scanjoin_target */
2019
0
      split_pathtarget_at_srfs_grouping(root,
2020
0
                        grouping_target, scanjoin_target,
2021
0
                        &grouping_targets,
2022
0
                        &grouping_targets_contain_srfs);
2023
0
      grouping_target = linitial_node(PathTarget, grouping_targets);
2024
0
      Assert(!linitial_int(grouping_targets_contain_srfs));
2025
      /* scanjoin_target will not have any SRFs precomputed for it */
2026
0
      split_pathtarget_at_srfs(root, scanjoin_target, NULL,
2027
0
                   &scanjoin_targets,
2028
0
                   &scanjoin_targets_contain_srfs);
2029
0
      scanjoin_target = linitial_node(PathTarget, scanjoin_targets);
2030
0
      Assert(!linitial_int(scanjoin_targets_contain_srfs));
2031
0
    }
2032
0
    else
2033
0
    {
2034
      /* initialize lists; for most of these, dummy values are OK */
2035
0
      final_targets = final_targets_contain_srfs = NIL;
2036
0
      sort_input_targets = sort_input_targets_contain_srfs = NIL;
2037
0
      grouping_targets = grouping_targets_contain_srfs = NIL;
2038
0
      scanjoin_targets = list_make1(scanjoin_target);
2039
0
      scanjoin_targets_contain_srfs = NIL;
2040
0
    }
2041
2042
    /* Apply scan/join target. */
2043
0
    scanjoin_target_same_exprs = list_length(scanjoin_targets) == 1
2044
0
      && equal(scanjoin_target->exprs, current_rel->reltarget->exprs);
2045
0
    apply_scanjoin_target_to_paths(root, current_rel, scanjoin_targets,
2046
0
                     scanjoin_targets_contain_srfs,
2047
0
                     scanjoin_target_parallel_safe,
2048
0
                     scanjoin_target_same_exprs);
2049
2050
    /*
2051
     * Save the various upper-rel PathTargets we just computed into
2052
     * root->upper_targets[].  The core code doesn't use this, but it
2053
     * provides a convenient place for extensions to get at the info.  For
2054
     * consistency, we save all the intermediate targets, even though some
2055
     * of the corresponding upperrels might not be needed for this query.
2056
     */
2057
0
    root->upper_targets[UPPERREL_FINAL] = final_target;
2058
0
    root->upper_targets[UPPERREL_ORDERED] = final_target;
2059
0
    root->upper_targets[UPPERREL_DISTINCT] = sort_input_target;
2060
0
    root->upper_targets[UPPERREL_PARTIAL_DISTINCT] = sort_input_target;
2061
0
    root->upper_targets[UPPERREL_WINDOW] = sort_input_target;
2062
0
    root->upper_targets[UPPERREL_GROUP_AGG] = grouping_target;
2063
2064
    /*
2065
     * If we have grouping and/or aggregation, consider ways to implement
2066
     * that.  We build a new upperrel representing the output of this
2067
     * phase.
2068
     */
2069
0
    if (have_grouping)
2070
0
    {
2071
0
      current_rel = create_grouping_paths(root,
2072
0
                        current_rel,
2073
0
                        grouping_target,
2074
0
                        grouping_target_parallel_safe,
2075
0
                        gset_data);
2076
      /* Fix things up if grouping_target contains SRFs */
2077
0
      if (parse->hasTargetSRFs)
2078
0
        adjust_paths_for_srfs(root, current_rel,
2079
0
                    grouping_targets,
2080
0
                    grouping_targets_contain_srfs);
2081
0
    }
2082
2083
    /*
2084
     * If we have window functions, consider ways to implement those.  We
2085
     * build a new upperrel representing the output of this phase.
2086
     */
2087
0
    if (activeWindows)
2088
0
    {
2089
0
      current_rel = create_window_paths(root,
2090
0
                        current_rel,
2091
0
                        grouping_target,
2092
0
                        sort_input_target,
2093
0
                        sort_input_target_parallel_safe,
2094
0
                        wflists,
2095
0
                        activeWindows);
2096
      /* Fix things up if sort_input_target contains SRFs */
2097
0
      if (parse->hasTargetSRFs)
2098
0
        adjust_paths_for_srfs(root, current_rel,
2099
0
                    sort_input_targets,
2100
0
                    sort_input_targets_contain_srfs);
2101
0
    }
2102
2103
    /*
2104
     * If there is a DISTINCT clause, consider ways to implement that. We
2105
     * build a new upperrel representing the output of this phase.
2106
     */
2107
0
    if (parse->distinctClause)
2108
0
    {
2109
0
      current_rel = create_distinct_paths(root,
2110
0
                        current_rel,
2111
0
                        sort_input_target);
2112
0
    }
2113
0
  }             /* end of if (setOperations) */
2114
2115
  /*
2116
   * If ORDER BY was given, consider ways to implement that, and generate a
2117
   * new upperrel containing only paths that emit the correct ordering and
2118
   * project the correct final_target.  We can apply the original
2119
   * limit_tuples limit in sort costing here, but only if there are no
2120
   * postponed SRFs.
2121
   */
2122
0
  if (parse->sortClause)
2123
0
  {
2124
0
    current_rel = create_ordered_paths(root,
2125
0
                       current_rel,
2126
0
                       final_target,
2127
0
                       final_target_parallel_safe,
2128
0
                       have_postponed_srfs ? -1.0 :
2129
0
                       limit_tuples);
2130
    /* Fix things up if final_target contains SRFs */
2131
0
    if (parse->hasTargetSRFs)
2132
0
      adjust_paths_for_srfs(root, current_rel,
2133
0
                  final_targets,
2134
0
                  final_targets_contain_srfs);
2135
0
  }
2136
2137
  /*
2138
   * Now we are prepared to build the final-output upperrel.
2139
   */
2140
0
  final_rel = fetch_upper_rel(root, UPPERREL_FINAL, NULL);
2141
2142
  /*
2143
   * If the input rel is marked consider_parallel and there's nothing that's
2144
   * not parallel-safe in the LIMIT clause, then the final_rel can be marked
2145
   * consider_parallel as well.  Note that if the query has rowMarks or is
2146
   * not a SELECT, consider_parallel will be false for every relation in the
2147
   * query.
2148
   */
2149
0
  if (current_rel->consider_parallel &&
2150
0
    is_parallel_safe(root, parse->limitOffset) &&
2151
0
    is_parallel_safe(root, parse->limitCount))
2152
0
    final_rel->consider_parallel = true;
2153
2154
  /*
2155
   * If the current_rel belongs to a single FDW, so does the final_rel.
2156
   */
2157
0
  final_rel->serverid = current_rel->serverid;
2158
0
  final_rel->userid = current_rel->userid;
2159
0
  final_rel->useridiscurrent = current_rel->useridiscurrent;
2160
0
  final_rel->fdwroutine = current_rel->fdwroutine;
2161
2162
  /*
2163
   * Generate paths for the final_rel.  Insert all surviving paths, with
2164
   * LockRows, Limit, and/or ModifyTable steps added if needed.
2165
   */
2166
0
  foreach(lc, current_rel->pathlist)
2167
0
  {
2168
0
    Path     *path = (Path *) lfirst(lc);
2169
2170
    /*
2171
     * If there is a FOR [KEY] UPDATE/SHARE clause, add the LockRows node.
2172
     * (Note: we intentionally test parse->rowMarks not root->rowMarks
2173
     * here.  If there are only non-locking rowmarks, they should be
2174
     * handled by the ModifyTable node instead.  However, root->rowMarks
2175
     * is what goes into the LockRows node.)
2176
     */
2177
0
    if (parse->rowMarks)
2178
0
    {
2179
0
      path = (Path *) create_lockrows_path(root, final_rel, path,
2180
0
                         root->rowMarks,
2181
0
                         assign_special_exec_param(root));
2182
0
    }
2183
2184
    /*
2185
     * If there is a LIMIT/OFFSET clause, add the LIMIT node.
2186
     */
2187
0
    if (limit_needed(parse))
2188
0
    {
2189
0
      path = (Path *) create_limit_path(root, final_rel, path,
2190
0
                        parse->limitOffset,
2191
0
                        parse->limitCount,
2192
0
                        parse->limitOption,
2193
0
                        offset_est, count_est);
2194
0
    }
2195
2196
    /*
2197
     * If this is an INSERT/UPDATE/DELETE/MERGE, add the ModifyTable node.
2198
     */
2199
0
    if (parse->commandType != CMD_SELECT)
2200
0
    {
2201
0
      Index   rootRelation;
2202
0
      List     *resultRelations = NIL;
2203
0
      List     *updateColnosLists = NIL;
2204
0
      List     *withCheckOptionLists = NIL;
2205
0
      List     *returningLists = NIL;
2206
0
      List     *mergeActionLists = NIL;
2207
0
      List     *mergeJoinConditions = NIL;
2208
0
      List     *rowMarks;
2209
2210
0
      if (bms_membership(root->all_result_relids) == BMS_MULTIPLE)
2211
0
      {
2212
        /* Inherited UPDATE/DELETE/MERGE */
2213
0
        RelOptInfo *top_result_rel = find_base_rel(root,
2214
0
                               parse->resultRelation);
2215
0
        int     resultRelation = -1;
2216
2217
        /* Pass the root result rel forward to the executor. */
2218
0
        rootRelation = parse->resultRelation;
2219
2220
        /* Add only leaf children to ModifyTable. */
2221
0
        while ((resultRelation = bms_next_member(root->leaf_result_relids,
2222
0
                             resultRelation)) >= 0)
2223
0
        {
2224
0
          RelOptInfo *this_result_rel = find_base_rel(root,
2225
0
                                resultRelation);
2226
2227
          /*
2228
           * Also exclude any leaf rels that have turned dummy since
2229
           * being added to the list, for example, by being excluded
2230
           * by constraint exclusion.
2231
           */
2232
0
          if (IS_DUMMY_REL(this_result_rel))
2233
0
            continue;
2234
2235
          /* Build per-target-rel lists needed by ModifyTable */
2236
0
          resultRelations = lappend_int(resultRelations,
2237
0
                          resultRelation);
2238
0
          if (parse->commandType == CMD_UPDATE)
2239
0
          {
2240
0
            List     *update_colnos = root->update_colnos;
2241
2242
0
            if (this_result_rel != top_result_rel)
2243
0
              update_colnos =
2244
0
                adjust_inherited_attnums_multilevel(root,
2245
0
                                  update_colnos,
2246
0
                                  this_result_rel->relid,
2247
0
                                  top_result_rel->relid);
2248
0
            updateColnosLists = lappend(updateColnosLists,
2249
0
                          update_colnos);
2250
0
          }
2251
0
          if (parse->withCheckOptions)
2252
0
          {
2253
0
            List     *withCheckOptions = parse->withCheckOptions;
2254
2255
0
            if (this_result_rel != top_result_rel)
2256
0
              withCheckOptions = (List *)
2257
0
                adjust_appendrel_attrs_multilevel(root,
2258
0
                                  (Node *) withCheckOptions,
2259
0
                                  this_result_rel,
2260
0
                                  top_result_rel);
2261
0
            withCheckOptionLists = lappend(withCheckOptionLists,
2262
0
                             withCheckOptions);
2263
0
          }
2264
0
          if (parse->returningList)
2265
0
          {
2266
0
            List     *returningList = parse->returningList;
2267
2268
0
            if (this_result_rel != top_result_rel)
2269
0
              returningList = (List *)
2270
0
                adjust_appendrel_attrs_multilevel(root,
2271
0
                                  (Node *) returningList,
2272
0
                                  this_result_rel,
2273
0
                                  top_result_rel);
2274
0
            returningLists = lappend(returningLists,
2275
0
                         returningList);
2276
0
          }
2277
0
          if (parse->mergeActionList)
2278
0
          {
2279
0
            ListCell   *l;
2280
0
            List     *mergeActionList = NIL;
2281
2282
            /*
2283
             * Copy MergeActions and translate stuff that
2284
             * references attribute numbers.
2285
             */
2286
0
            foreach(l, parse->mergeActionList)
2287
0
            {
2288
0
              MergeAction *action = lfirst(l),
2289
0
                     *leaf_action = copyObject(action);
2290
2291
0
              leaf_action->qual =
2292
0
                adjust_appendrel_attrs_multilevel(root,
2293
0
                                  (Node *) action->qual,
2294
0
                                  this_result_rel,
2295
0
                                  top_result_rel);
2296
0
              leaf_action->targetList = (List *)
2297
0
                adjust_appendrel_attrs_multilevel(root,
2298
0
                                  (Node *) action->targetList,
2299
0
                                  this_result_rel,
2300
0
                                  top_result_rel);
2301
0
              if (leaf_action->commandType == CMD_UPDATE)
2302
0
                leaf_action->updateColnos =
2303
0
                  adjust_inherited_attnums_multilevel(root,
2304
0
                                    action->updateColnos,
2305
0
                                    this_result_rel->relid,
2306
0
                                    top_result_rel->relid);
2307
0
              mergeActionList = lappend(mergeActionList,
2308
0
                            leaf_action);
2309
0
            }
2310
2311
0
            mergeActionLists = lappend(mergeActionLists,
2312
0
                           mergeActionList);
2313
0
          }
2314
0
          if (parse->commandType == CMD_MERGE)
2315
0
          {
2316
0
            Node     *mergeJoinCondition = parse->mergeJoinCondition;
2317
2318
0
            if (this_result_rel != top_result_rel)
2319
0
              mergeJoinCondition =
2320
0
                adjust_appendrel_attrs_multilevel(root,
2321
0
                                  mergeJoinCondition,
2322
0
                                  this_result_rel,
2323
0
                                  top_result_rel);
2324
0
            mergeJoinConditions = lappend(mergeJoinConditions,
2325
0
                            mergeJoinCondition);
2326
0
          }
2327
0
        }
2328
2329
0
        if (resultRelations == NIL)
2330
0
        {
2331
          /*
2332
           * We managed to exclude every child rel, so generate a
2333
           * dummy one-relation plan using info for the top target
2334
           * rel (even though that may not be a leaf target).
2335
           * Although it's clear that no data will be updated or
2336
           * deleted, we still need to have a ModifyTable node so
2337
           * that any statement triggers will be executed.  (This
2338
           * could be cleaner if we fixed nodeModifyTable.c to allow
2339
           * zero target relations, but that probably wouldn't be a
2340
           * net win.)
2341
           */
2342
0
          resultRelations = list_make1_int(parse->resultRelation);
2343
0
          if (parse->commandType == CMD_UPDATE)
2344
0
            updateColnosLists = list_make1(root->update_colnos);
2345
0
          if (parse->withCheckOptions)
2346
0
            withCheckOptionLists = list_make1(parse->withCheckOptions);
2347
0
          if (parse->returningList)
2348
0
            returningLists = list_make1(parse->returningList);
2349
0
          if (parse->mergeActionList)
2350
0
            mergeActionLists = list_make1(parse->mergeActionList);
2351
0
          if (parse->commandType == CMD_MERGE)
2352
0
            mergeJoinConditions = list_make1(parse->mergeJoinCondition);
2353
0
        }
2354
0
      }
2355
0
      else
2356
0
      {
2357
        /* Single-relation INSERT/UPDATE/DELETE/MERGE. */
2358
0
        rootRelation = 0; /* there's no separate root rel */
2359
0
        resultRelations = list_make1_int(parse->resultRelation);
2360
0
        if (parse->commandType == CMD_UPDATE)
2361
0
          updateColnosLists = list_make1(root->update_colnos);
2362
0
        if (parse->withCheckOptions)
2363
0
          withCheckOptionLists = list_make1(parse->withCheckOptions);
2364
0
        if (parse->returningList)
2365
0
          returningLists = list_make1(parse->returningList);
2366
0
        if (parse->mergeActionList)
2367
0
          mergeActionLists = list_make1(parse->mergeActionList);
2368
0
        if (parse->commandType == CMD_MERGE)
2369
0
          mergeJoinConditions = list_make1(parse->mergeJoinCondition);
2370
0
      }
2371
2372
      /*
2373
       * If there was a FOR [KEY] UPDATE/SHARE clause, the LockRows node
2374
       * will have dealt with fetching non-locked marked rows, else we
2375
       * need to have ModifyTable do that.
2376
       */
2377
0
      if (parse->rowMarks)
2378
0
        rowMarks = NIL;
2379
0
      else
2380
0
        rowMarks = root->rowMarks;
2381
2382
0
      path = (Path *)
2383
0
        create_modifytable_path(root, final_rel,
2384
0
                    path,
2385
0
                    parse->commandType,
2386
0
                    parse->canSetTag,
2387
0
                    parse->resultRelation,
2388
0
                    rootRelation,
2389
0
                    resultRelations,
2390
0
                    updateColnosLists,
2391
0
                    withCheckOptionLists,
2392
0
                    returningLists,
2393
0
                    rowMarks,
2394
0
                    parse->onConflict,
2395
0
                    mergeActionLists,
2396
0
                    mergeJoinConditions,
2397
0
                    parse->forPortionOf,
2398
0
                    assign_special_exec_param(root));
2399
0
    }
2400
2401
    /* And shove it into final_rel */
2402
0
    add_path(final_rel, path);
2403
0
  }
2404
2405
  /*
2406
   * Generate partial paths for final_rel, too, if outer query levels might
2407
   * be able to make use of them.
2408
   */
2409
0
  if (final_rel->consider_parallel && root->query_level > 1 &&
2410
0
    !limit_needed(parse))
2411
0
  {
2412
0
    Assert(!parse->rowMarks && parse->commandType == CMD_SELECT);
2413
0
    foreach(lc, current_rel->partial_pathlist)
2414
0
    {
2415
0
      Path     *partial_path = (Path *) lfirst(lc);
2416
2417
0
      add_partial_path(final_rel, partial_path);
2418
0
    }
2419
0
  }
2420
2421
0
  extra.limit_needed = limit_needed(parse);
2422
0
  extra.limit_tuples = limit_tuples;
2423
0
  extra.count_est = count_est;
2424
0
  extra.offset_est = offset_est;
2425
2426
  /*
2427
   * If there is an FDW that's responsible for all baserels of the query,
2428
   * let it consider adding ForeignPaths.
2429
   */
2430
0
  if (final_rel->fdwroutine &&
2431
0
    final_rel->fdwroutine->GetForeignUpperPaths)
2432
0
    final_rel->fdwroutine->GetForeignUpperPaths(root, UPPERREL_FINAL,
2433
0
                          current_rel, final_rel,
2434
0
                          &extra);
2435
2436
  /* Let extensions possibly add some more paths */
2437
0
  if (create_upper_paths_hook)
2438
0
    (*create_upper_paths_hook) (root, UPPERREL_FINAL,
2439
0
                  current_rel, final_rel, &extra);
2440
2441
  /* Note: currently, we leave it to callers to do set_cheapest() */
2442
0
}
2443
2444
/*
2445
 * Do preprocessing for groupingSets clause and related data.
2446
 *
2447
 * We expect that parse->groupingSets has already been expanded into a flat
2448
 * list of grouping sets (that is, just integer Lists of ressortgroupref
2449
 * numbers) by expand_grouping_sets().  This function handles the preliminary
2450
 * steps of organizing the grouping sets into lists of rollups, and preparing
2451
 * annotations which will later be filled in with size estimates.
2452
 */
2453
static grouping_sets_data *
2454
preprocess_grouping_sets(PlannerInfo *root)
2455
0
{
2456
0
  Query    *parse = root->parse;
2457
0
  List     *sets;
2458
0
  int     maxref = 0;
2459
0
  ListCell   *lc_set;
2460
0
  grouping_sets_data *gd = palloc0_object(grouping_sets_data);
2461
2462
  /*
2463
   * We don't currently make any attempt to optimize the groupClause when
2464
   * there are grouping sets, so just duplicate it in processed_groupClause.
2465
   */
2466
0
  root->processed_groupClause = parse->groupClause;
2467
2468
  /* Detect unhashable and unsortable grouping expressions */
2469
0
  gd->any_hashable = false;
2470
0
  gd->unhashable_refs = NULL;
2471
0
  gd->unsortable_refs = NULL;
2472
0
  gd->unsortable_sets = NIL;
2473
2474
0
  if (parse->groupClause)
2475
0
  {
2476
0
    ListCell   *lc;
2477
2478
0
    foreach(lc, parse->groupClause)
2479
0
    {
2480
0
      SortGroupClause *gc = lfirst_node(SortGroupClause, lc);
2481
0
      Index   ref = gc->tleSortGroupRef;
2482
2483
0
      if (ref > maxref)
2484
0
        maxref = ref;
2485
2486
0
      if (!gc->hashable)
2487
0
        gd->unhashable_refs = bms_add_member(gd->unhashable_refs, ref);
2488
2489
0
      if (!OidIsValid(gc->sortop))
2490
0
        gd->unsortable_refs = bms_add_member(gd->unsortable_refs, ref);
2491
0
    }
2492
0
  }
2493
2494
  /* Allocate workspace array for remapping */
2495
0
  gd->tleref_to_colnum_map = (int *) palloc((maxref + 1) * sizeof(int));
2496
2497
  /*
2498
   * If we have any unsortable sets, we must extract them before trying to
2499
   * prepare rollups. Unsortable sets don't go through
2500
   * reorder_grouping_sets, so we must apply the GroupingSetData annotation
2501
   * here.
2502
   */
2503
0
  if (!bms_is_empty(gd->unsortable_refs))
2504
0
  {
2505
0
    List     *sortable_sets = NIL;
2506
0
    ListCell   *lc;
2507
2508
0
    foreach(lc, parse->groupingSets)
2509
0
    {
2510
0
      List     *gset = (List *) lfirst(lc);
2511
2512
0
      if (bms_overlap_list(gd->unsortable_refs, gset))
2513
0
      {
2514
0
        GroupingSetData *gs = makeNode(GroupingSetData);
2515
2516
0
        gs->set = gset;
2517
0
        gd->unsortable_sets = lappend(gd->unsortable_sets, gs);
2518
2519
        /*
2520
         * We must enforce here that an unsortable set is hashable;
2521
         * later code assumes this.  Parse analysis only checks that
2522
         * every individual column is either hashable or sortable.
2523
         *
2524
         * Note that passing this test doesn't guarantee we can
2525
         * generate a plan; there might be other showstoppers.
2526
         */
2527
0
        if (bms_overlap_list(gd->unhashable_refs, gset))
2528
0
          ereport(ERROR,
2529
0
              (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2530
0
               errmsg("could not implement GROUP BY"),
2531
0
               errdetail("Some of the datatypes only support hashing, while others only support sorting.")));
2532
0
      }
2533
0
      else
2534
0
        sortable_sets = lappend(sortable_sets, gset);
2535
0
    }
2536
2537
0
    if (sortable_sets)
2538
0
      sets = extract_rollup_sets(sortable_sets);
2539
0
    else
2540
0
      sets = NIL;
2541
0
  }
2542
0
  else
2543
0
    sets = extract_rollup_sets(parse->groupingSets);
2544
2545
0
  foreach(lc_set, sets)
2546
0
  {
2547
0
    List     *current_sets = (List *) lfirst(lc_set);
2548
0
    RollupData *rollup = makeNode(RollupData);
2549
0
    GroupingSetData *gs;
2550
2551
    /*
2552
     * Reorder the current list of grouping sets into correct prefix
2553
     * order.  If only one aggregation pass is needed, try to make the
2554
     * list match the ORDER BY clause; if more than one pass is needed, we
2555
     * don't bother with that.
2556
     *
2557
     * Note that this reorders the sets from smallest-member-first to
2558
     * largest-member-first, and applies the GroupingSetData annotations,
2559
     * though the data will be filled in later.
2560
     */
2561
0
    current_sets = reorder_grouping_sets(current_sets,
2562
0
                       (list_length(sets) == 1
2563
0
                        ? parse->sortClause
2564
0
                        : NIL));
2565
2566
    /*
2567
     * Get the initial (and therefore largest) grouping set.
2568
     */
2569
0
    gs = linitial_node(GroupingSetData, current_sets);
2570
2571
    /*
2572
     * Order the groupClause appropriately.  If the first grouping set is
2573
     * empty, then the groupClause must also be empty; otherwise we have
2574
     * to force the groupClause to match that grouping set's order.
2575
     *
2576
     * (The first grouping set can be empty even though parse->groupClause
2577
     * is not empty only if all non-empty grouping sets are unsortable.
2578
     * The groupClauses for hashed grouping sets are built later on.)
2579
     */
2580
0
    if (gs->set)
2581
0
      rollup->groupClause = preprocess_groupclause(root, gs->set);
2582
0
    else
2583
0
      rollup->groupClause = NIL;
2584
2585
    /*
2586
     * Is it hashable? We pretend empty sets are hashable even though we
2587
     * actually force them not to be hashed later. But don't bother if
2588
     * there's nothing but empty sets (since in that case we can't hash
2589
     * anything).
2590
     */
2591
0
    if (gs->set &&
2592
0
      !bms_overlap_list(gd->unhashable_refs, gs->set))
2593
0
    {
2594
0
      rollup->hashable = true;
2595
0
      gd->any_hashable = true;
2596
0
    }
2597
2598
    /*
2599
     * Now that we've pinned down an order for the groupClause for this
2600
     * list of grouping sets, we need to remap the entries in the grouping
2601
     * sets from sortgrouprefs to plain indices (0-based) into the
2602
     * groupClause for this collection of grouping sets. We keep the
2603
     * original form for later use, though.
2604
     */
2605
0
    rollup->gsets = remap_to_groupclause_idx(rollup->groupClause,
2606
0
                         current_sets,
2607
0
                         gd->tleref_to_colnum_map);
2608
0
    rollup->gsets_data = current_sets;
2609
2610
0
    gd->rollups = lappend(gd->rollups, rollup);
2611
0
  }
2612
2613
0
  if (gd->unsortable_sets)
2614
0
  {
2615
    /*
2616
     * We have not yet pinned down a groupclause for this, but we will
2617
     * need index-based lists for estimation purposes. Construct
2618
     * hash_sets_idx based on the entire original groupclause for now.
2619
     */
2620
0
    gd->hash_sets_idx = remap_to_groupclause_idx(parse->groupClause,
2621
0
                           gd->unsortable_sets,
2622
0
                           gd->tleref_to_colnum_map);
2623
0
    gd->any_hashable = true;
2624
0
  }
2625
2626
0
  return gd;
2627
0
}
2628
2629
/*
2630
 * Given a groupclause and a list of GroupingSetData, return equivalent sets
2631
 * (without annotation) mapped to indexes into the given groupclause.
2632
 */
2633
static List *
2634
remap_to_groupclause_idx(List *groupClause,
2635
             List *gsets,
2636
             int *tleref_to_colnum_map)
2637
0
{
2638
0
  int     ref = 0;
2639
0
  List     *result = NIL;
2640
0
  ListCell   *lc;
2641
2642
0
  foreach(lc, groupClause)
2643
0
  {
2644
0
    SortGroupClause *gc = lfirst_node(SortGroupClause, lc);
2645
2646
0
    tleref_to_colnum_map[gc->tleSortGroupRef] = ref++;
2647
0
  }
2648
2649
0
  foreach(lc, gsets)
2650
0
  {
2651
0
    List     *set = NIL;
2652
0
    ListCell   *lc2;
2653
0
    GroupingSetData *gs = lfirst_node(GroupingSetData, lc);
2654
2655
0
    foreach(lc2, gs->set)
2656
0
    {
2657
0
      set = lappend_int(set, tleref_to_colnum_map[lfirst_int(lc2)]);
2658
0
    }
2659
2660
0
    result = lappend(result, set);
2661
0
  }
2662
2663
0
  return result;
2664
0
}
2665
2666
2667
/*
2668
 * preprocess_rowmarks - set up PlanRowMarks if needed
2669
 */
2670
static void
2671
preprocess_rowmarks(PlannerInfo *root)
2672
0
{
2673
0
  Query    *parse = root->parse;
2674
0
  Bitmapset  *rels;
2675
0
  List     *prowmarks;
2676
0
  ListCell   *l;
2677
0
  int     i;
2678
2679
0
  if (parse->rowMarks)
2680
0
  {
2681
    /*
2682
     * We've got trouble if FOR [KEY] UPDATE/SHARE appears inside
2683
     * grouping, since grouping renders a reference to individual tuple
2684
     * CTIDs invalid.  This is also checked at parse time, but that's
2685
     * insufficient because of rule substitution, query pullup, etc.
2686
     */
2687
0
    CheckSelectLocking(parse, linitial_node(RowMarkClause,
2688
0
                        parse->rowMarks)->strength);
2689
0
  }
2690
0
  else
2691
0
  {
2692
    /*
2693
     * We only need rowmarks for UPDATE, DELETE, MERGE, or FOR [KEY]
2694
     * UPDATE/SHARE.
2695
     */
2696
0
    if (parse->commandType != CMD_UPDATE &&
2697
0
      parse->commandType != CMD_DELETE &&
2698
0
      parse->commandType != CMD_MERGE)
2699
0
      return;
2700
0
  }
2701
2702
  /*
2703
   * We need to have rowmarks for all base relations except the target. We
2704
   * make a bitmapset of all base rels and then remove the items we don't
2705
   * need or have FOR [KEY] UPDATE/SHARE marks for.
2706
   */
2707
0
  rels = get_relids_in_jointree((Node *) parse->jointree, false, false);
2708
0
  if (parse->resultRelation)
2709
0
    rels = bms_del_member(rels, parse->resultRelation);
2710
2711
  /*
2712
   * Convert RowMarkClauses to PlanRowMark representation.
2713
   */
2714
0
  prowmarks = NIL;
2715
0
  foreach(l, parse->rowMarks)
2716
0
  {
2717
0
    RowMarkClause *rc = lfirst_node(RowMarkClause, l);
2718
0
    RangeTblEntry *rte = rt_fetch(rc->rti, parse->rtable);
2719
0
    PlanRowMark *newrc;
2720
2721
    /*
2722
     * Currently, it is syntactically impossible to have FOR UPDATE et al
2723
     * applied to an update/delete target rel.  If that ever becomes
2724
     * possible, we should drop the target from the PlanRowMark list.
2725
     */
2726
0
    Assert(rc->rti != parse->resultRelation);
2727
2728
    /*
2729
     * Ignore RowMarkClauses for subqueries; they aren't real tables and
2730
     * can't support true locking.  Subqueries that got flattened into the
2731
     * main query should be ignored completely.  Any that didn't will get
2732
     * ROW_MARK_COPY items in the next loop.
2733
     */
2734
0
    if (rte->rtekind != RTE_RELATION)
2735
0
      continue;
2736
2737
0
    rels = bms_del_member(rels, rc->rti);
2738
2739
0
    newrc = makeNode(PlanRowMark);
2740
0
    newrc->rti = newrc->prti = rc->rti;
2741
0
    newrc->rowmarkId = ++(root->glob->lastRowMarkId);
2742
0
    newrc->markType = select_rowmark_type(rte, rc->strength);
2743
0
    newrc->allMarkTypes = (1 << newrc->markType);
2744
0
    newrc->strength = rc->strength;
2745
0
    newrc->waitPolicy = rc->waitPolicy;
2746
0
    newrc->isParent = false;
2747
2748
0
    prowmarks = lappend(prowmarks, newrc);
2749
0
  }
2750
2751
  /*
2752
   * Now, add rowmarks for any non-target, non-locked base relations.
2753
   */
2754
0
  i = 0;
2755
0
  foreach(l, parse->rtable)
2756
0
  {
2757
0
    RangeTblEntry *rte = lfirst_node(RangeTblEntry, l);
2758
0
    PlanRowMark *newrc;
2759
2760
0
    i++;
2761
0
    if (!bms_is_member(i, rels))
2762
0
      continue;
2763
2764
0
    newrc = makeNode(PlanRowMark);
2765
0
    newrc->rti = newrc->prti = i;
2766
0
    newrc->rowmarkId = ++(root->glob->lastRowMarkId);
2767
0
    newrc->markType = select_rowmark_type(rte, LCS_NONE);
2768
0
    newrc->allMarkTypes = (1 << newrc->markType);
2769
0
    newrc->strength = LCS_NONE;
2770
0
    newrc->waitPolicy = LockWaitBlock;  /* doesn't matter */
2771
0
    newrc->isParent = false;
2772
2773
0
    prowmarks = lappend(prowmarks, newrc);
2774
0
  }
2775
2776
0
  root->rowMarks = prowmarks;
2777
0
}
2778
2779
/*
2780
 * Select RowMarkType to use for a given table
2781
 */
2782
RowMarkType
2783
select_rowmark_type(RangeTblEntry *rte, LockClauseStrength strength)
2784
0
{
2785
0
  if (rte->rtekind != RTE_RELATION)
2786
0
  {
2787
    /* If it's not a table at all, use ROW_MARK_COPY */
2788
0
    return ROW_MARK_COPY;
2789
0
  }
2790
0
  else if (rte->relkind == RELKIND_FOREIGN_TABLE)
2791
0
  {
2792
    /* Let the FDW select the rowmark type, if it wants to */
2793
0
    FdwRoutine *fdwroutine = GetFdwRoutineByRelId(rte->relid);
2794
2795
0
    if (fdwroutine->GetForeignRowMarkType != NULL)
2796
0
      return fdwroutine->GetForeignRowMarkType(rte, strength);
2797
    /* Otherwise, use ROW_MARK_COPY by default */
2798
0
    return ROW_MARK_COPY;
2799
0
  }
2800
0
  else
2801
0
  {
2802
    /* Regular table, apply the appropriate lock type */
2803
0
    switch (strength)
2804
0
    {
2805
0
      case LCS_NONE:
2806
2807
        /*
2808
         * We don't need a tuple lock, only the ability to re-fetch
2809
         * the row.
2810
         */
2811
0
        return ROW_MARK_REFERENCE;
2812
0
        break;
2813
0
      case LCS_FORKEYSHARE:
2814
0
        return ROW_MARK_KEYSHARE;
2815
0
        break;
2816
0
      case LCS_FORSHARE:
2817
0
        return ROW_MARK_SHARE;
2818
0
        break;
2819
0
      case LCS_FORNOKEYUPDATE:
2820
0
        return ROW_MARK_NOKEYEXCLUSIVE;
2821
0
        break;
2822
0
      case LCS_FORUPDATE:
2823
0
        return ROW_MARK_EXCLUSIVE;
2824
0
        break;
2825
0
    }
2826
0
    elog(ERROR, "unrecognized LockClauseStrength %d", (int) strength);
2827
0
    return ROW_MARK_EXCLUSIVE; /* keep compiler quiet */
2828
0
  }
2829
0
}
2830
2831
/*
2832
 * preprocess_limit - do pre-estimation for LIMIT and/or OFFSET clauses
2833
 *
2834
 * We try to estimate the values of the LIMIT/OFFSET clauses, and pass the
2835
 * results back in *count_est and *offset_est.  These variables are set to
2836
 * 0 if the corresponding clause is not present, and -1 if it's present
2837
 * but we couldn't estimate the value for it.  (The "0" convention is OK
2838
 * for OFFSET but a little bit bogus for LIMIT: effectively we estimate
2839
 * LIMIT 0 as though it were LIMIT 1.  But this is in line with the planner's
2840
 * usual practice of never estimating less than one row.)  These values will
2841
 * be passed to create_limit_path, which see if you change this code.
2842
 *
2843
 * The return value is the suitably adjusted tuple_fraction to use for
2844
 * planning the query.  This adjustment is not overridable, since it reflects
2845
 * plan actions that grouping_planner() will certainly take, not assumptions
2846
 * about context.
2847
 */
2848
static double
2849
preprocess_limit(PlannerInfo *root, double tuple_fraction,
2850
         int64 *offset_est, int64 *count_est)
2851
0
{
2852
0
  Query    *parse = root->parse;
2853
0
  Node     *est;
2854
0
  double    limit_fraction;
2855
2856
  /* Should not be called unless LIMIT or OFFSET */
2857
0
  Assert(parse->limitCount || parse->limitOffset);
2858
2859
  /*
2860
   * Try to obtain the clause values.  We use estimate_expression_value
2861
   * primarily because it can sometimes do something useful with Params.
2862
   */
2863
0
  if (parse->limitCount)
2864
0
  {
2865
0
    est = estimate_expression_value(root, parse->limitCount);
2866
0
    if (est && IsA(est, Const))
2867
0
    {
2868
0
      if (((Const *) est)->constisnull)
2869
0
      {
2870
        /* NULL indicates LIMIT ALL, ie, no limit */
2871
0
        *count_est = 0; /* treat as not present */
2872
0
      }
2873
0
      else
2874
0
      {
2875
0
        *count_est = DatumGetInt64(((Const *) est)->constvalue);
2876
0
        if (*count_est <= 0)
2877
0
          *count_est = 1; /* force to at least 1 */
2878
0
      }
2879
0
    }
2880
0
    else
2881
0
      *count_est = -1; /* can't estimate */
2882
0
  }
2883
0
  else
2884
0
    *count_est = 0;     /* not present */
2885
2886
0
  if (parse->limitOffset)
2887
0
  {
2888
0
    est = estimate_expression_value(root, parse->limitOffset);
2889
0
    if (est && IsA(est, Const))
2890
0
    {
2891
0
      if (((Const *) est)->constisnull)
2892
0
      {
2893
        /* Treat NULL as no offset; the executor will too */
2894
0
        *offset_est = 0;  /* treat as not present */
2895
0
      }
2896
0
      else
2897
0
      {
2898
0
        *offset_est = DatumGetInt64(((Const *) est)->constvalue);
2899
0
        if (*offset_est < 0)
2900
0
          *offset_est = 0; /* treat as not present */
2901
0
      }
2902
0
    }
2903
0
    else
2904
0
      *offset_est = -1; /* can't estimate */
2905
0
  }
2906
0
  else
2907
0
    *offset_est = 0;   /* not present */
2908
2909
0
  if (*count_est != 0)
2910
0
  {
2911
    /*
2912
     * A LIMIT clause limits the absolute number of tuples returned.
2913
     * However, if it's not a constant LIMIT then we have to guess; for
2914
     * lack of a better idea, assume 10% of the plan's result is wanted.
2915
     */
2916
0
    if (*count_est < 0 || *offset_est < 0)
2917
0
    {
2918
      /* LIMIT or OFFSET is an expression ... punt ... */
2919
0
      limit_fraction = 0.10;
2920
0
    }
2921
0
    else
2922
0
    {
2923
      /* LIMIT (plus OFFSET, if any) is max number of tuples needed */
2924
0
      limit_fraction = (double) *count_est + (double) *offset_est;
2925
0
    }
2926
2927
    /*
2928
     * If we have absolute limits from both caller and LIMIT, use the
2929
     * smaller value; likewise if they are both fractional.  If one is
2930
     * fractional and the other absolute, we can't easily determine which
2931
     * is smaller, but we use the heuristic that the absolute will usually
2932
     * be smaller.
2933
     */
2934
0
    if (tuple_fraction >= 1.0)
2935
0
    {
2936
0
      if (limit_fraction >= 1.0)
2937
0
      {
2938
        /* both absolute */
2939
0
        tuple_fraction = Min(tuple_fraction, limit_fraction);
2940
0
      }
2941
0
      else
2942
0
      {
2943
        /* caller absolute, limit fractional; use caller's value */
2944
0
      }
2945
0
    }
2946
0
    else if (tuple_fraction > 0.0)
2947
0
    {
2948
0
      if (limit_fraction >= 1.0)
2949
0
      {
2950
        /* caller fractional, limit absolute; use limit */
2951
0
        tuple_fraction = limit_fraction;
2952
0
      }
2953
0
      else
2954
0
      {
2955
        /* both fractional */
2956
0
        tuple_fraction = Min(tuple_fraction, limit_fraction);
2957
0
      }
2958
0
    }
2959
0
    else
2960
0
    {
2961
      /* no info from caller, just use limit */
2962
0
      tuple_fraction = limit_fraction;
2963
0
    }
2964
0
  }
2965
0
  else if (*offset_est != 0 && tuple_fraction > 0.0)
2966
0
  {
2967
    /*
2968
     * We have an OFFSET but no LIMIT.  This acts entirely differently
2969
     * from the LIMIT case: here, we need to increase rather than decrease
2970
     * the caller's tuple_fraction, because the OFFSET acts to cause more
2971
     * tuples to be fetched instead of fewer.  This only matters if we got
2972
     * a tuple_fraction > 0, however.
2973
     *
2974
     * As above, use 10% if OFFSET is present but unestimatable.
2975
     */
2976
0
    if (*offset_est < 0)
2977
0
      limit_fraction = 0.10;
2978
0
    else
2979
0
      limit_fraction = (double) *offset_est;
2980
2981
    /*
2982
     * If we have absolute counts from both caller and OFFSET, add them
2983
     * together; likewise if they are both fractional.  If one is
2984
     * fractional and the other absolute, we want to take the larger, and
2985
     * we heuristically assume that's the fractional one.
2986
     */
2987
0
    if (tuple_fraction >= 1.0)
2988
0
    {
2989
0
      if (limit_fraction >= 1.0)
2990
0
      {
2991
        /* both absolute, so add them together */
2992
0
        tuple_fraction += limit_fraction;
2993
0
      }
2994
0
      else
2995
0
      {
2996
        /* caller absolute, limit fractional; use limit */
2997
0
        tuple_fraction = limit_fraction;
2998
0
      }
2999
0
    }
3000
0
    else
3001
0
    {
3002
0
      if (limit_fraction >= 1.0)
3003
0
      {
3004
        /* caller fractional, limit absolute; use caller's value */
3005
0
      }
3006
0
      else
3007
0
      {
3008
        /* both fractional, so add them together */
3009
0
        tuple_fraction += limit_fraction;
3010
0
        if (tuple_fraction >= 1.0)
3011
0
          tuple_fraction = 0.0; /* assume fetch all */
3012
0
      }
3013
0
    }
3014
0
  }
3015
3016
0
  return tuple_fraction;
3017
0
}
3018
3019
/*
3020
 * limit_needed - do we actually need a Limit plan node?
3021
 *
3022
 * If we have constant-zero OFFSET and constant-null LIMIT, we can skip adding
3023
 * a Limit node.  This is worth checking for because "OFFSET 0" is a common
3024
 * locution for an optimization fence.  (Because other places in the planner
3025
 * merely check whether parse->limitOffset isn't NULL, it will still work as
3026
 * an optimization fence --- we're just suppressing unnecessary run-time
3027
 * overhead.)
3028
 *
3029
 * This might look like it could be merged into preprocess_limit, but there's
3030
 * a key distinction: here we need hard constants in OFFSET/LIMIT, whereas
3031
 * in preprocess_limit it's good enough to consider estimated values.
3032
 */
3033
bool
3034
limit_needed(Query *parse)
3035
0
{
3036
0
  Node     *node;
3037
3038
0
  node = parse->limitCount;
3039
0
  if (node)
3040
0
  {
3041
0
    if (IsA(node, Const))
3042
0
    {
3043
      /* NULL indicates LIMIT ALL, ie, no limit */
3044
0
      if (!((Const *) node)->constisnull)
3045
0
        return true; /* LIMIT with a constant value */
3046
0
    }
3047
0
    else
3048
0
      return true;   /* non-constant LIMIT */
3049
0
  }
3050
3051
0
  node = parse->limitOffset;
3052
0
  if (node)
3053
0
  {
3054
0
    if (IsA(node, Const))
3055
0
    {
3056
      /* Treat NULL as no offset; the executor would too */
3057
0
      if (!((Const *) node)->constisnull)
3058
0
      {
3059
0
        int64   offset = DatumGetInt64(((Const *) node)->constvalue);
3060
3061
0
        if (offset != 0)
3062
0
          return true; /* OFFSET with a nonzero value */
3063
0
      }
3064
0
    }
3065
0
    else
3066
0
      return true;   /* non-constant OFFSET */
3067
0
  }
3068
3069
0
  return false;       /* don't need a Limit plan node */
3070
0
}
3071
3072
/*
3073
 * preprocess_groupclause - do preparatory work on GROUP BY clause
3074
 *
3075
 * The idea here is to adjust the ordering of the GROUP BY elements
3076
 * (which in itself is semantically insignificant) to match ORDER BY,
3077
 * thereby allowing a single sort operation to both implement the ORDER BY
3078
 * requirement and set up for a Unique step that implements GROUP BY.
3079
 * We also consider partial match between GROUP BY and ORDER BY elements,
3080
 * which could allow to implement ORDER BY using the incremental sort.
3081
 *
3082
 * We also consider other orderings of the GROUP BY elements, which could
3083
 * match the sort ordering of other possible plans (eg an indexscan) and
3084
 * thereby reduce cost.  This is implemented during the generation of grouping
3085
 * paths.  See get_useful_group_keys_orderings() for details.
3086
 *
3087
 * Note: we need no comparable processing of the distinctClause because
3088
 * the parser already enforced that that matches ORDER BY.
3089
 *
3090
 * Note: we return a fresh List, but its elements are the same
3091
 * SortGroupClauses appearing in parse->groupClause.  This is important
3092
 * because later processing may modify the processed_groupClause list.
3093
 *
3094
 * For grouping sets, the order of items is instead forced to agree with that
3095
 * of the grouping set (and items not in the grouping set are skipped). The
3096
 * work of sorting the order of grouping set elements to match the ORDER BY if
3097
 * possible is done elsewhere.
3098
 */
3099
static List *
3100
preprocess_groupclause(PlannerInfo *root, List *force)
3101
0
{
3102
0
  Query    *parse = root->parse;
3103
0
  List     *new_groupclause = NIL;
3104
0
  ListCell   *sl;
3105
0
  ListCell   *gl;
3106
3107
  /* For grouping sets, we need to force the ordering */
3108
0
  if (force)
3109
0
  {
3110
0
    foreach(sl, force)
3111
0
    {
3112
0
      Index   ref = lfirst_int(sl);
3113
0
      SortGroupClause *cl = get_sortgroupref_clause(ref, parse->groupClause);
3114
3115
0
      new_groupclause = lappend(new_groupclause, cl);
3116
0
    }
3117
3118
0
    return new_groupclause;
3119
0
  }
3120
3121
  /* If no ORDER BY, nothing useful to do here */
3122
0
  if (parse->sortClause == NIL)
3123
0
    return list_copy(parse->groupClause);
3124
3125
  /*
3126
   * Scan the ORDER BY clause and construct a list of matching GROUP BY
3127
   * items, but only as far as we can make a matching prefix.
3128
   *
3129
   * This code assumes that the sortClause contains no duplicate items.
3130
   */
3131
0
  foreach(sl, parse->sortClause)
3132
0
  {
3133
0
    SortGroupClause *sc = lfirst_node(SortGroupClause, sl);
3134
3135
0
    foreach(gl, parse->groupClause)
3136
0
    {
3137
0
      SortGroupClause *gc = lfirst_node(SortGroupClause, gl);
3138
3139
0
      if (equal(gc, sc))
3140
0
      {
3141
0
        new_groupclause = lappend(new_groupclause, gc);
3142
0
        break;
3143
0
      }
3144
0
    }
3145
0
    if (gl == NULL)
3146
0
      break;       /* no match, so stop scanning */
3147
0
  }
3148
3149
3150
  /* If no match at all, no point in reordering GROUP BY */
3151
0
  if (new_groupclause == NIL)
3152
0
    return list_copy(parse->groupClause);
3153
3154
  /*
3155
   * Add any remaining GROUP BY items to the new list.  We don't require a
3156
   * complete match, because even partial match allows ORDER BY to be
3157
   * implemented using incremental sort.  Also, give up if there are any
3158
   * non-sortable GROUP BY items, since then there's no hope anyway.
3159
   */
3160
0
  foreach(gl, parse->groupClause)
3161
0
  {
3162
0
    SortGroupClause *gc = lfirst_node(SortGroupClause, gl);
3163
3164
0
    if (list_member_ptr(new_groupclause, gc))
3165
0
      continue;     /* it matched an ORDER BY item */
3166
0
    if (!OidIsValid(gc->sortop)) /* give up, GROUP BY can't be sorted */
3167
0
      return list_copy(parse->groupClause);
3168
0
    new_groupclause = lappend(new_groupclause, gc);
3169
0
  }
3170
3171
  /* Success --- install the rearranged GROUP BY list */
3172
0
  Assert(list_length(parse->groupClause) == list_length(new_groupclause));
3173
0
  return new_groupclause;
3174
0
}
3175
3176
/*
3177
 * Extract lists of grouping sets that can be implemented using a single
3178
 * rollup-type aggregate pass each. Returns a list of lists of grouping sets.
3179
 *
3180
 * Input must be sorted with smallest sets first. Result has each sublist
3181
 * sorted with smallest sets first.
3182
 *
3183
 * We want to produce the absolute minimum possible number of lists here to
3184
 * avoid excess sorts. Fortunately, there is an algorithm for this; the problem
3185
 * of finding the minimal partition of a partially-ordered set into chains
3186
 * (which is what we need, taking the list of grouping sets as a poset ordered
3187
 * by set inclusion) can be mapped to the problem of finding the maximum
3188
 * cardinality matching on a bipartite graph, which is solvable in polynomial
3189
 * time with a worst case of no worse than O(n^2.5) and usually much
3190
 * better. Since our N is at most 4096, we don't need to consider fallbacks to
3191
 * heuristic or approximate methods.  (Planning time for a 12-d cube is under
3192
 * half a second on my modest system even with optimization off and assertions
3193
 * on.)
3194
 */
3195
static List *
3196
extract_rollup_sets(List *groupingSets)
3197
0
{
3198
0
  int     num_sets_raw = list_length(groupingSets);
3199
0
  int     num_empty = 0;
3200
0
  int     num_sets = 0; /* distinct sets */
3201
0
  int     num_chains = 0;
3202
0
  List     *result = NIL;
3203
0
  List    **results;
3204
0
  List    **orig_sets;
3205
0
  Bitmapset **set_masks;
3206
0
  int      *chains;
3207
0
  short   **adjacency;
3208
0
  short    *adjacency_buf;
3209
0
  BipartiteMatchState *state;
3210
0
  int     i;
3211
0
  int     j;
3212
0
  int     j_size;
3213
0
  ListCell   *lc1 = list_head(groupingSets);
3214
0
  ListCell   *lc;
3215
3216
  /*
3217
   * Start by stripping out empty sets.  The algorithm doesn't require this,
3218
   * but the planner currently needs all empty sets to be returned in the
3219
   * first list, so we strip them here and add them back after.
3220
   */
3221
0
  while (lc1 && lfirst(lc1) == NIL)
3222
0
  {
3223
0
    ++num_empty;
3224
0
    lc1 = lnext(groupingSets, lc1);
3225
0
  }
3226
3227
  /* bail out now if it turns out that all we had were empty sets. */
3228
0
  if (!lc1)
3229
0
    return list_make1(groupingSets);
3230
3231
  /*----------
3232
   * We don't strictly need to remove duplicate sets here, but if we don't,
3233
   * they tend to become scattered through the result, which is a bit
3234
   * confusing (and irritating if we ever decide to optimize them out).
3235
   * So we remove them here and add them back after.
3236
   *
3237
   * For each non-duplicate set, we fill in the following:
3238
   *
3239
   * orig_sets[i] = list of the original set lists
3240
   * set_masks[i] = bitmapset for testing inclusion
3241
   * adjacency[i] = array [n, v1, v2, ... vn] of adjacency indices
3242
   *
3243
   * chains[i] will be the result group this set is assigned to.
3244
   *
3245
   * We index all of these from 1 rather than 0 because it is convenient
3246
   * to leave 0 free for the NIL node in the graph algorithm.
3247
   *----------
3248
   */
3249
0
  orig_sets = palloc0((num_sets_raw + 1) * sizeof(List *));
3250
0
  set_masks = palloc0((num_sets_raw + 1) * sizeof(Bitmapset *));
3251
0
  adjacency = palloc0((num_sets_raw + 1) * sizeof(short *));
3252
0
  adjacency_buf = palloc((num_sets_raw + 1) * sizeof(short));
3253
3254
0
  j_size = 0;
3255
0
  j = 0;
3256
0
  i = 1;
3257
3258
0
  for_each_cell(lc, groupingSets, lc1)
3259
0
  {
3260
0
    List     *candidate = (List *) lfirst(lc);
3261
0
    Bitmapset  *candidate_set = NULL;
3262
0
    ListCell   *lc2;
3263
0
    int     dup_of = 0;
3264
3265
0
    foreach(lc2, candidate)
3266
0
    {
3267
0
      candidate_set = bms_add_member(candidate_set, lfirst_int(lc2));
3268
0
    }
3269
3270
    /* we can only be a dup if we're the same length as a previous set */
3271
0
    if (j_size == list_length(candidate))
3272
0
    {
3273
0
      int     k;
3274
3275
0
      for (k = j; k < i; ++k)
3276
0
      {
3277
0
        if (bms_equal(set_masks[k], candidate_set))
3278
0
        {
3279
0
          dup_of = k;
3280
0
          break;
3281
0
        }
3282
0
      }
3283
0
    }
3284
0
    else if (j_size < list_length(candidate))
3285
0
    {
3286
0
      j_size = list_length(candidate);
3287
0
      j = i;
3288
0
    }
3289
3290
0
    if (dup_of > 0)
3291
0
    {
3292
0
      orig_sets[dup_of] = lappend(orig_sets[dup_of], candidate);
3293
0
      bms_free(candidate_set);
3294
0
    }
3295
0
    else
3296
0
    {
3297
0
      int     k;
3298
0
      int     n_adj = 0;
3299
3300
0
      orig_sets[i] = list_make1(candidate);
3301
0
      set_masks[i] = candidate_set;
3302
3303
      /* fill in adjacency list; no need to compare equal-size sets */
3304
3305
0
      for (k = j - 1; k > 0; --k)
3306
0
      {
3307
0
        if (bms_is_subset(set_masks[k], candidate_set))
3308
0
          adjacency_buf[++n_adj] = k;
3309
0
      }
3310
3311
0
      if (n_adj > 0)
3312
0
      {
3313
0
        adjacency_buf[0] = n_adj;
3314
0
        adjacency[i] = palloc((n_adj + 1) * sizeof(short));
3315
0
        memcpy(adjacency[i], adjacency_buf, (n_adj + 1) * sizeof(short));
3316
0
      }
3317
0
      else
3318
0
        adjacency[i] = NULL;
3319
3320
0
      ++i;
3321
0
    }
3322
0
  }
3323
3324
0
  num_sets = i - 1;
3325
3326
  /*
3327
   * Apply the graph matching algorithm to do the work.
3328
   */
3329
0
  state = BipartiteMatch(num_sets, num_sets, adjacency);
3330
3331
  /*
3332
   * Now, the state->pair* fields have the info we need to assign sets to
3333
   * chains. Two sets (u,v) belong to the same chain if pair_uv[u] = v or
3334
   * pair_vu[v] = u (both will be true, but we check both so that we can do
3335
   * it in one pass)
3336
   */
3337
0
  chains = palloc0((num_sets + 1) * sizeof(int));
3338
3339
0
  for (i = 1; i <= num_sets; ++i)
3340
0
  {
3341
0
    int     u = state->pair_vu[i];
3342
0
    int     v = state->pair_uv[i];
3343
3344
0
    if (u > 0 && u < i)
3345
0
      chains[i] = chains[u];
3346
0
    else if (v > 0 && v < i)
3347
0
      chains[i] = chains[v];
3348
0
    else
3349
0
      chains[i] = ++num_chains;
3350
0
  }
3351
3352
  /* build result lists. */
3353
0
  results = palloc0((num_chains + 1) * sizeof(List *));
3354
3355
0
  for (i = 1; i <= num_sets; ++i)
3356
0
  {
3357
0
    int     c = chains[i];
3358
3359
0
    Assert(c > 0);
3360
3361
0
    results[c] = list_concat(results[c], orig_sets[i]);
3362
0
  }
3363
3364
  /* push any empty sets back on the first list. */
3365
0
  while (num_empty-- > 0)
3366
0
    results[1] = lcons(NIL, results[1]);
3367
3368
  /* make result list */
3369
0
  for (i = 1; i <= num_chains; ++i)
3370
0
    result = lappend(result, results[i]);
3371
3372
  /*
3373
   * Free all the things.
3374
   *
3375
   * (This is over-fussy for small sets but for large sets we could have
3376
   * tied up a nontrivial amount of memory.)
3377
   */
3378
0
  BipartiteMatchFree(state);
3379
0
  pfree(results);
3380
0
  pfree(chains);
3381
0
  for (i = 1; i <= num_sets; ++i)
3382
0
    if (adjacency[i])
3383
0
      pfree(adjacency[i]);
3384
0
  pfree(adjacency);
3385
0
  pfree(adjacency_buf);
3386
0
  pfree(orig_sets);
3387
0
  for (i = 1; i <= num_sets; ++i)
3388
0
    bms_free(set_masks[i]);
3389
0
  pfree(set_masks);
3390
3391
0
  return result;
3392
0
}
3393
3394
/*
3395
 * Reorder the elements of a list of grouping sets such that they have correct
3396
 * prefix relationships. Also inserts the GroupingSetData annotations.
3397
 *
3398
 * The input must be ordered with smallest sets first; the result is returned
3399
 * with largest sets first.  Note that the result shares no list substructure
3400
 * with the input, so it's safe for the caller to modify it later.
3401
 *
3402
 * If we're passed in a sortclause, we follow its order of columns to the
3403
 * extent possible, to minimize the chance that we add unnecessary sorts.
3404
 * (We're trying here to ensure that GROUPING SETS ((a,b,c),(c)) ORDER BY c,b,a
3405
 * gets implemented in one pass.)
3406
 */
3407
static List *
3408
reorder_grouping_sets(List *groupingSets, List *sortclause)
3409
0
{
3410
0
  ListCell   *lc;
3411
0
  List     *previous = NIL;
3412
0
  List     *result = NIL;
3413
3414
0
  foreach(lc, groupingSets)
3415
0
  {
3416
0
    List     *candidate = (List *) lfirst(lc);
3417
0
    List     *new_elems = list_difference_int(candidate, previous);
3418
0
    GroupingSetData *gs = makeNode(GroupingSetData);
3419
3420
0
    while (list_length(sortclause) > list_length(previous) &&
3421
0
         new_elems != NIL)
3422
0
    {
3423
0
      SortGroupClause *sc = list_nth(sortclause, list_length(previous));
3424
0
      int     ref = sc->tleSortGroupRef;
3425
3426
0
      if (list_member_int(new_elems, ref))
3427
0
      {
3428
0
        previous = lappend_int(previous, ref);
3429
0
        new_elems = list_delete_int(new_elems, ref);
3430
0
      }
3431
0
      else
3432
0
      {
3433
        /* diverged from the sortclause; give up on it */
3434
0
        sortclause = NIL;
3435
0
        break;
3436
0
      }
3437
0
    }
3438
3439
0
    previous = list_concat(previous, new_elems);
3440
3441
0
    gs->set = list_copy(previous);
3442
0
    result = lcons(gs, result);
3443
0
  }
3444
3445
0
  list_free(previous);
3446
3447
0
  return result;
3448
0
}
3449
3450
/*
3451
 * has_volatile_pathkey
3452
 *    Returns true if any PathKey in 'keys' has an EquivalenceClass
3453
 *    containing a volatile function.  Otherwise returns false.
3454
 */
3455
static bool
3456
has_volatile_pathkey(List *keys)
3457
0
{
3458
0
  ListCell   *lc;
3459
3460
0
  foreach(lc, keys)
3461
0
  {
3462
0
    PathKey    *pathkey = lfirst_node(PathKey, lc);
3463
3464
0
    if (pathkey->pk_eclass->ec_has_volatile)
3465
0
      return true;
3466
0
  }
3467
3468
0
  return false;
3469
0
}
3470
3471
/*
3472
 * adjust_group_pathkeys_for_groupagg
3473
 *    Add pathkeys to root->group_pathkeys to reflect the best set of
3474
 *    pre-ordered input for ordered aggregates.
3475
 *
3476
 * We define "best" as the pathkeys that suit the largest number of
3477
 * aggregate functions.  We find these by looking at the first ORDER BY /
3478
 * DISTINCT aggregate and take the pathkeys for that before searching for
3479
 * other aggregates that require the same or a more strict variation of the
3480
 * same pathkeys.  We then repeat that process for any remaining aggregates
3481
 * with different pathkeys and if we find another set of pathkeys that suits a
3482
 * larger number of aggregates then we select those pathkeys instead.
3483
 *
3484
 * When the best pathkeys are found we also mark each Aggref that can use
3485
 * those pathkeys as aggpresorted = true.
3486
 *
3487
 * Note: When an aggregate function's ORDER BY / DISTINCT clause contains any
3488
 * volatile functions, we never make use of these pathkeys.  We want to ensure
3489
 * that sorts using volatile functions are done independently in each Aggref
3490
 * rather than once at the query level.  If we were to allow this then Aggrefs
3491
 * with compatible sort orders would all transition their rows in the same
3492
 * order if those pathkeys were deemed to be the best pathkeys to sort on.
3493
 * Whereas, if some other set of Aggref's pathkeys happened to be deemed
3494
 * better pathkeys to sort on, then the volatile function Aggrefs would be
3495
 * left to perform their sorts individually.  To avoid this inconsistent
3496
 * behavior which could make Aggref results depend on what other Aggrefs the
3497
 * query contains, we always force Aggrefs with volatile functions to perform
3498
 * their own sorts.
3499
 */
3500
static void
3501
adjust_group_pathkeys_for_groupagg(PlannerInfo *root)
3502
0
{
3503
0
  List     *grouppathkeys = root->group_pathkeys;
3504
0
  List     *bestpathkeys;
3505
0
  Bitmapset  *bestaggs;
3506
0
  Bitmapset  *unprocessed_aggs;
3507
0
  ListCell   *lc;
3508
0
  int     i;
3509
3510
  /* Shouldn't be here if there are grouping sets */
3511
0
  Assert(root->parse->groupingSets == NIL);
3512
  /* Shouldn't be here unless there are some ordered aggregates */
3513
0
  Assert(root->numOrderedAggs > 0);
3514
3515
  /* Do nothing if disabled */
3516
0
  if (!enable_presorted_aggregate)
3517
0
    return;
3518
3519
  /*
3520
   * Make a first pass over all AggInfos to collect a Bitmapset containing
3521
   * the indexes of all AggInfos to be processed below.
3522
   */
3523
0
  unprocessed_aggs = NULL;
3524
0
  foreach(lc, root->agginfos)
3525
0
  {
3526
0
    AggInfo    *agginfo = lfirst_node(AggInfo, lc);
3527
0
    Aggref     *aggref = linitial_node(Aggref, agginfo->aggrefs);
3528
3529
0
    if (AGGKIND_IS_ORDERED_SET(aggref->aggkind))
3530
0
      continue;
3531
3532
    /* Skip unless there's a DISTINCT or ORDER BY clause */
3533
0
    if (aggref->aggdistinct == NIL && aggref->aggorder == NIL)
3534
0
      continue;
3535
3536
    /* Additional safety checks are needed if there's a FILTER clause */
3537
0
    if (aggref->aggfilter != NULL)
3538
0
    {
3539
0
      ListCell   *lc2;
3540
0
      bool    allow_presort = true;
3541
3542
      /*
3543
       * When the Aggref has a FILTER clause, it's possible that the
3544
       * filter removes rows that cannot be sorted because the
3545
       * expression to sort by results in an error during its
3546
       * evaluation.  This is a problem for presorting as that happens
3547
       * before the FILTER, whereas without presorting, the Aggregate
3548
       * node will apply the FILTER *before* sorting.  So that we never
3549
       * try to sort anything that might error, here we aim to skip over
3550
       * any Aggrefs with arguments with expressions which, when
3551
       * evaluated, could cause an ERROR.  Vars and Consts are ok. There
3552
       * may be more cases that should be allowed, but more thought
3553
       * needs to be given.  Err on the side of caution.
3554
       */
3555
0
      foreach(lc2, aggref->args)
3556
0
      {
3557
0
        TargetEntry *tle = (TargetEntry *) lfirst(lc2);
3558
0
        Expr     *expr = tle->expr;
3559
3560
0
        while (IsA(expr, RelabelType))
3561
0
          expr = (Expr *) (castNode(RelabelType, expr))->arg;
3562
3563
        /* Common case, Vars and Consts are ok */
3564
0
        if (IsA(expr, Var) || IsA(expr, Const))
3565
0
          continue;
3566
3567
        /* Unsupported.  Don't try to presort for this Aggref */
3568
0
        allow_presort = false;
3569
0
        break;
3570
0
      }
3571
3572
      /* Skip unsupported Aggrefs */
3573
0
      if (!allow_presort)
3574
0
        continue;
3575
0
    }
3576
3577
0
    unprocessed_aggs = bms_add_member(unprocessed_aggs,
3578
0
                      foreach_current_index(lc));
3579
0
  }
3580
3581
  /*
3582
   * Now process all the unprocessed_aggs to find the best set of pathkeys
3583
   * for the given set of aggregates.
3584
   *
3585
   * On the first outer loop here 'bestaggs' will be empty.   We'll populate
3586
   * this during the first loop using the pathkeys for the very first
3587
   * AggInfo then taking any stronger pathkeys from any other AggInfos with
3588
   * a more strict set of compatible pathkeys.  Once the outer loop is
3589
   * complete, we mark off all the aggregates with compatible pathkeys then
3590
   * remove those from the unprocessed_aggs and repeat the process to try to
3591
   * find another set of pathkeys that are suitable for a larger number of
3592
   * aggregates.  The outer loop will stop when there are not enough
3593
   * unprocessed aggregates for it to be possible to find a set of pathkeys
3594
   * to suit a larger number of aggregates.
3595
   */
3596
0
  bestpathkeys = NIL;
3597
0
  bestaggs = NULL;
3598
0
  while (bms_num_members(unprocessed_aggs) > bms_num_members(bestaggs))
3599
0
  {
3600
0
    Bitmapset  *aggindexes = NULL;
3601
0
    List     *currpathkeys = NIL;
3602
3603
0
    i = -1;
3604
0
    while ((i = bms_next_member(unprocessed_aggs, i)) >= 0)
3605
0
    {
3606
0
      AggInfo    *agginfo = list_nth_node(AggInfo, root->agginfos, i);
3607
0
      Aggref     *aggref = linitial_node(Aggref, agginfo->aggrefs);
3608
0
      List     *sortlist;
3609
0
      List     *pathkeys;
3610
3611
0
      if (aggref->aggdistinct != NIL)
3612
0
        sortlist = aggref->aggdistinct;
3613
0
      else
3614
0
        sortlist = aggref->aggorder;
3615
3616
0
      pathkeys = make_pathkeys_for_sortclauses(root, sortlist,
3617
0
                           aggref->args);
3618
3619
      /*
3620
       * Ignore Aggrefs which have volatile functions in their ORDER BY
3621
       * or DISTINCT clause.
3622
       */
3623
0
      if (has_volatile_pathkey(pathkeys))
3624
0
      {
3625
0
        unprocessed_aggs = bms_del_member(unprocessed_aggs, i);
3626
0
        continue;
3627
0
      }
3628
3629
      /*
3630
       * When not set yet, take the pathkeys from the first unprocessed
3631
       * aggregate.
3632
       */
3633
0
      if (currpathkeys == NIL)
3634
0
      {
3635
0
        currpathkeys = pathkeys;
3636
3637
        /* include the GROUP BY pathkeys, if they exist */
3638
0
        if (grouppathkeys != NIL)
3639
0
          currpathkeys = append_pathkeys(list_copy(grouppathkeys),
3640
0
                           currpathkeys);
3641
3642
        /* record that we found pathkeys for this aggregate */
3643
0
        aggindexes = bms_add_member(aggindexes, i);
3644
0
      }
3645
0
      else
3646
0
      {
3647
        /* now look for a stronger set of matching pathkeys */
3648
3649
        /* include the GROUP BY pathkeys, if they exist */
3650
0
        if (grouppathkeys != NIL)
3651
0
          pathkeys = append_pathkeys(list_copy(grouppathkeys),
3652
0
                         pathkeys);
3653
3654
        /* are 'pathkeys' compatible or better than 'currpathkeys'? */
3655
0
        switch (compare_pathkeys(currpathkeys, pathkeys))
3656
0
        {
3657
0
          case PATHKEYS_BETTER2:
3658
            /* 'pathkeys' are stronger, use these ones instead */
3659
0
            currpathkeys = pathkeys;
3660
0
            pg_fallthrough;
3661
3662
0
          case PATHKEYS_BETTER1:
3663
            /* 'pathkeys' are less strict */
3664
0
            pg_fallthrough;
3665
3666
0
          case PATHKEYS_EQUAL:
3667
            /* mark this aggregate as covered by 'currpathkeys' */
3668
0
            aggindexes = bms_add_member(aggindexes, i);
3669
0
            break;
3670
3671
0
          case PATHKEYS_DIFFERENT:
3672
0
            break;
3673
0
        }
3674
0
      }
3675
0
    }
3676
3677
    /* remove the aggregates that we've just processed */
3678
0
    unprocessed_aggs = bms_del_members(unprocessed_aggs, aggindexes);
3679
3680
    /*
3681
     * If this pass included more aggregates than the previous best then
3682
     * use these ones as the best set.
3683
     */
3684
0
    if (bms_num_members(aggindexes) > bms_num_members(bestaggs))
3685
0
    {
3686
0
      bestaggs = aggindexes;
3687
0
      bestpathkeys = currpathkeys;
3688
0
    }
3689
0
  }
3690
3691
  /*
3692
   * If we found any ordered aggregates, update root->group_pathkeys to add
3693
   * the best set of aggregate pathkeys.  Note that bestpathkeys includes
3694
   * the original GROUP BY pathkeys already.
3695
   */
3696
0
  if (bestpathkeys != NIL)
3697
0
    root->group_pathkeys = bestpathkeys;
3698
3699
  /*
3700
   * Now that we've found the best set of aggregates we can set the
3701
   * presorted flag to indicate to the executor that it needn't bother
3702
   * performing a sort for these Aggrefs.  We're able to do this now as
3703
   * there's no chance of a Hash Aggregate plan as create_grouping_paths
3704
   * will not mark the GROUP BY as GROUPING_CAN_USE_HASH due to the presence
3705
   * of ordered aggregates.
3706
   */
3707
0
  i = -1;
3708
0
  while ((i = bms_next_member(bestaggs, i)) >= 0)
3709
0
  {
3710
0
    AggInfo    *agginfo = list_nth_node(AggInfo, root->agginfos, i);
3711
3712
0
    foreach(lc, agginfo->aggrefs)
3713
0
    {
3714
0
      Aggref     *aggref = lfirst_node(Aggref, lc);
3715
3716
0
      aggref->aggpresorted = true;
3717
0
    }
3718
0
  }
3719
0
}
3720
3721
/*
3722
 * Compute query_pathkeys and other pathkeys during plan generation
3723
 */
3724
static void
3725
standard_qp_callback(PlannerInfo *root, void *extra)
3726
0
{
3727
0
  Query    *parse = root->parse;
3728
0
  standard_qp_extra *qp_extra = (standard_qp_extra *) extra;
3729
0
  List     *tlist = root->processed_tlist;
3730
0
  List     *activeWindows = qp_extra->activeWindows;
3731
3732
  /*
3733
   * Calculate pathkeys that represent grouping/ordering and/or ordered
3734
   * aggregate requirements.
3735
   */
3736
0
  if (qp_extra->gset_data)
3737
0
  {
3738
    /*
3739
     * With grouping sets, just use the first RollupData's groupClause. We
3740
     * don't make any effort to optimize grouping clauses when there are
3741
     * grouping sets, nor can we combine aggregate ordering keys with
3742
     * grouping.
3743
     */
3744
0
    List     *rollups = qp_extra->gset_data->rollups;
3745
0
    List     *groupClause = (rollups ? linitial_node(RollupData, rollups)->groupClause : NIL);
3746
3747
0
    if (grouping_is_sortable(groupClause))
3748
0
    {
3749
0
      bool    sortable;
3750
3751
      /*
3752
       * The groupClause is logically below the grouping step.  So if
3753
       * there is an RTE entry for the grouping step, we need to remove
3754
       * its RT index from the sort expressions before we make PathKeys
3755
       * for them.
3756
       */
3757
0
      root->group_pathkeys =
3758
0
        make_pathkeys_for_sortclauses_extended(root,
3759
0
                             &groupClause,
3760
0
                             tlist,
3761
0
                             false,
3762
0
                             parse->hasGroupRTE,
3763
0
                             &sortable,
3764
0
                             false);
3765
0
      Assert(sortable);
3766
0
      root->num_groupby_pathkeys = list_length(root->group_pathkeys);
3767
0
    }
3768
0
    else
3769
0
    {
3770
0
      root->group_pathkeys = NIL;
3771
0
      root->num_groupby_pathkeys = 0;
3772
0
    }
3773
0
  }
3774
0
  else if (parse->groupClause || root->numOrderedAggs > 0)
3775
0
  {
3776
    /*
3777
     * With a plain GROUP BY list, we can remove any grouping items that
3778
     * are proven redundant by EquivalenceClass processing.  For example,
3779
     * we can remove y given "WHERE x = y GROUP BY x, y".  These aren't
3780
     * especially common cases, but they're nearly free to detect.  Note
3781
     * that we remove redundant items from processed_groupClause but not
3782
     * the original parse->groupClause.
3783
     */
3784
0
    bool    sortable;
3785
3786
    /*
3787
     * Convert group clauses into pathkeys.  Set the ec_sortref field of
3788
     * EquivalenceClass'es if it's not set yet.
3789
     */
3790
0
    root->group_pathkeys =
3791
0
      make_pathkeys_for_sortclauses_extended(root,
3792
0
                           &root->processed_groupClause,
3793
0
                           tlist,
3794
0
                           true,
3795
0
                           false,
3796
0
                           &sortable,
3797
0
                           true);
3798
0
    if (!sortable)
3799
0
    {
3800
      /* Can't sort; no point in considering aggregate ordering either */
3801
0
      root->group_pathkeys = NIL;
3802
0
      root->num_groupby_pathkeys = 0;
3803
0
    }
3804
0
    else
3805
0
    {
3806
0
      root->num_groupby_pathkeys = list_length(root->group_pathkeys);
3807
      /* If we have ordered aggs, consider adding onto group_pathkeys */
3808
0
      if (root->numOrderedAggs > 0)
3809
0
        adjust_group_pathkeys_for_groupagg(root);
3810
0
    }
3811
0
  }
3812
0
  else
3813
0
  {
3814
0
    root->group_pathkeys = NIL;
3815
0
    root->num_groupby_pathkeys = 0;
3816
0
  }
3817
3818
  /* We consider only the first (bottom) window in pathkeys logic */
3819
0
  if (activeWindows != NIL)
3820
0
  {
3821
0
    WindowClause *wc = linitial_node(WindowClause, activeWindows);
3822
3823
0
    root->window_pathkeys = make_pathkeys_for_window(root,
3824
0
                             wc,
3825
0
                             tlist);
3826
0
  }
3827
0
  else
3828
0
    root->window_pathkeys = NIL;
3829
3830
  /*
3831
   * As with GROUP BY, we can discard any DISTINCT items that are proven
3832
   * redundant by EquivalenceClass processing.  The non-redundant list is
3833
   * kept in root->processed_distinctClause, leaving the original
3834
   * parse->distinctClause alone.
3835
   */
3836
0
  if (parse->distinctClause)
3837
0
  {
3838
0
    bool    sortable;
3839
3840
    /* Make a copy since pathkey processing can modify the list */
3841
0
    root->processed_distinctClause = list_copy(parse->distinctClause);
3842
0
    root->distinct_pathkeys =
3843
0
      make_pathkeys_for_sortclauses_extended(root,
3844
0
                           &root->processed_distinctClause,
3845
0
                           tlist,
3846
0
                           true,
3847
0
                           false,
3848
0
                           &sortable,
3849
0
                           false);
3850
0
    if (!sortable)
3851
0
      root->distinct_pathkeys = NIL;
3852
0
  }
3853
0
  else
3854
0
    root->distinct_pathkeys = NIL;
3855
3856
0
  root->sort_pathkeys =
3857
0
    make_pathkeys_for_sortclauses(root,
3858
0
                    parse->sortClause,
3859
0
                    tlist);
3860
3861
  /* setting setop_pathkeys might be useful to the union planner */
3862
0
  if (qp_extra->setop != NULL)
3863
0
  {
3864
0
    List     *groupClauses;
3865
0
    bool    sortable;
3866
3867
0
    groupClauses = generate_setop_child_grouplist(qp_extra->setop, tlist);
3868
3869
0
    root->setop_pathkeys =
3870
0
      make_pathkeys_for_sortclauses_extended(root,
3871
0
                           &groupClauses,
3872
0
                           tlist,
3873
0
                           false,
3874
0
                           false,
3875
0
                           &sortable,
3876
0
                           false);
3877
0
    if (!sortable)
3878
0
      root->setop_pathkeys = NIL;
3879
0
  }
3880
0
  else
3881
0
    root->setop_pathkeys = NIL;
3882
3883
  /*
3884
   * Figure out whether we want a sorted result from query_planner.
3885
   *
3886
   * If we have a sortable GROUP BY clause, then we want a result sorted
3887
   * properly for grouping.  Otherwise, if we have window functions to
3888
   * evaluate, we try to sort for the first window.  Otherwise, if there's a
3889
   * sortable DISTINCT clause that's more rigorous than the ORDER BY clause,
3890
   * we try to produce output that's sufficiently well sorted for the
3891
   * DISTINCT.  Otherwise, if there is an ORDER BY clause, we want to sort
3892
   * by the ORDER BY clause.  Otherwise, if we're a subquery being planned
3893
   * for a set operation which can benefit from presorted results and have a
3894
   * sortable targetlist, we want to sort by the target list.
3895
   *
3896
   * Note: if we have both ORDER BY and GROUP BY, and ORDER BY is a superset
3897
   * of GROUP BY, it would be tempting to request sort by ORDER BY --- but
3898
   * that might just leave us failing to exploit an available sort order at
3899
   * all.  Needs more thought.  The choice for DISTINCT versus ORDER BY is
3900
   * much easier, since we know that the parser ensured that one is a
3901
   * superset of the other.
3902
   */
3903
0
  if (root->group_pathkeys)
3904
0
    root->query_pathkeys = root->group_pathkeys;
3905
0
  else if (root->window_pathkeys)
3906
0
    root->query_pathkeys = root->window_pathkeys;
3907
0
  else if (list_length(root->distinct_pathkeys) >
3908
0
       list_length(root->sort_pathkeys))
3909
0
    root->query_pathkeys = root->distinct_pathkeys;
3910
0
  else if (root->sort_pathkeys)
3911
0
    root->query_pathkeys = root->sort_pathkeys;
3912
0
  else if (root->setop_pathkeys != NIL)
3913
0
    root->query_pathkeys = root->setop_pathkeys;
3914
0
  else
3915
0
    root->query_pathkeys = NIL;
3916
0
}
3917
3918
/*
3919
 * Estimate number of groups produced by grouping clauses (1 if not grouping)
3920
 *
3921
 * path_rows: number of output rows from scan/join step
3922
 * gd: grouping sets data including list of grouping sets and their clauses
3923
 * target_list: target list containing group clause references
3924
 *
3925
 * If doing grouping sets, we also annotate the gsets data with the estimates
3926
 * for each set and each individual rollup list, with a view to later
3927
 * determining whether some combination of them could be hashed instead.
3928
 */
3929
static double
3930
get_number_of_groups(PlannerInfo *root,
3931
           double path_rows,
3932
           grouping_sets_data *gd,
3933
           List *target_list)
3934
0
{
3935
0
  Query    *parse = root->parse;
3936
0
  double    dNumGroups;
3937
3938
0
  if (parse->groupClause)
3939
0
  {
3940
0
    List     *groupExprs;
3941
3942
0
    if (parse->groupingSets)
3943
0
    {
3944
      /* Add up the estimates for each grouping set */
3945
0
      ListCell   *lc;
3946
3947
0
      Assert(gd);     /* keep Coverity happy */
3948
3949
0
      dNumGroups = 0;
3950
3951
0
      foreach(lc, gd->rollups)
3952
0
      {
3953
0
        RollupData *rollup = lfirst_node(RollupData, lc);
3954
0
        ListCell   *lc2;
3955
0
        ListCell   *lc3;
3956
3957
0
        groupExprs = get_sortgrouplist_exprs(rollup->groupClause,
3958
0
                           target_list);
3959
3960
0
        rollup->numGroups = 0.0;
3961
3962
0
        forboth(lc2, rollup->gsets, lc3, rollup->gsets_data)
3963
0
        {
3964
0
          List     *gset = (List *) lfirst(lc2);
3965
0
          GroupingSetData *gs = lfirst_node(GroupingSetData, lc3);
3966
0
          double    numGroups = estimate_num_groups(root,
3967
0
                                groupExprs,
3968
0
                                path_rows,
3969
0
                                &gset,
3970
0
                                NULL);
3971
3972
0
          gs->numGroups = numGroups;
3973
0
          rollup->numGroups += numGroups;
3974
0
        }
3975
3976
0
        dNumGroups += rollup->numGroups;
3977
0
      }
3978
3979
0
      if (gd->hash_sets_idx)
3980
0
      {
3981
0
        ListCell   *lc2;
3982
3983
0
        gd->dNumHashGroups = 0;
3984
3985
0
        groupExprs = get_sortgrouplist_exprs(parse->groupClause,
3986
0
                           target_list);
3987
3988
0
        forboth(lc, gd->hash_sets_idx, lc2, gd->unsortable_sets)
3989
0
        {
3990
0
          List     *gset = (List *) lfirst(lc);
3991
0
          GroupingSetData *gs = lfirst_node(GroupingSetData, lc2);
3992
0
          double    numGroups = estimate_num_groups(root,
3993
0
                                groupExprs,
3994
0
                                path_rows,
3995
0
                                &gset,
3996
0
                                NULL);
3997
3998
0
          gs->numGroups = numGroups;
3999
0
          gd->dNumHashGroups += numGroups;
4000
0
        }
4001
4002
0
        dNumGroups += gd->dNumHashGroups;
4003
0
      }
4004
0
    }
4005
0
    else
4006
0
    {
4007
      /* Plain GROUP BY -- estimate based on optimized groupClause */
4008
0
      groupExprs = get_sortgrouplist_exprs(root->processed_groupClause,
4009
0
                         target_list);
4010
4011
0
      dNumGroups = estimate_num_groups(root, groupExprs, path_rows,
4012
0
                       NULL, NULL);
4013
0
    }
4014
0
  }
4015
0
  else if (parse->groupingSets)
4016
0
  {
4017
    /* Empty grouping sets ... one result row for each one */
4018
0
    dNumGroups = list_length(parse->groupingSets);
4019
0
  }
4020
0
  else if (parse->hasAggs || root->hasHavingQual)
4021
0
  {
4022
    /* Plain aggregation, one result row */
4023
0
    dNumGroups = 1;
4024
0
  }
4025
0
  else
4026
0
  {
4027
    /* Not grouping */
4028
0
    dNumGroups = 1;
4029
0
  }
4030
4031
0
  return dNumGroups;
4032
0
}
4033
4034
/*
4035
 * create_grouping_paths
4036
 *
4037
 * Build a new upperrel containing Paths for grouping and/or aggregation.
4038
 * Along the way, we also build an upperrel for Paths which are partially
4039
 * grouped and/or aggregated.  A partially grouped and/or aggregated path
4040
 * needs a FinalizeAggregate node to complete the aggregation.  Currently,
4041
 * the only partially grouped paths we build are also partial paths; that
4042
 * is, they need a Gather and then a FinalizeAggregate.
4043
 *
4044
 * input_rel: contains the source-data Paths
4045
 * target: the pathtarget for the result Paths to compute
4046
 * gd: grouping sets data including list of grouping sets and their clauses
4047
 *
4048
 * Note: all Paths in input_rel are expected to return the target computed
4049
 * by make_group_input_target.
4050
 */
4051
static RelOptInfo *
4052
create_grouping_paths(PlannerInfo *root,
4053
            RelOptInfo *input_rel,
4054
            PathTarget *target,
4055
            bool target_parallel_safe,
4056
            grouping_sets_data *gd)
4057
0
{
4058
0
  Query    *parse = root->parse;
4059
0
  RelOptInfo *grouped_rel;
4060
0
  RelOptInfo *partially_grouped_rel;
4061
0
  AggClauseCosts agg_costs;
4062
4063
0
  MemSet(&agg_costs, 0, sizeof(AggClauseCosts));
4064
0
  get_agg_clause_costs(root, AGGSPLIT_SIMPLE, &agg_costs);
4065
4066
  /*
4067
   * Create grouping relation to hold fully aggregated grouping and/or
4068
   * aggregation paths.
4069
   */
4070
0
  grouped_rel = make_grouping_rel(root, input_rel, target,
4071
0
                  target_parallel_safe, parse->havingQual);
4072
4073
  /*
4074
   * Create either paths for a degenerate grouping or paths for ordinary
4075
   * grouping, as appropriate.
4076
   */
4077
0
  if (is_degenerate_grouping(root))
4078
0
    create_degenerate_grouping_paths(root, input_rel, grouped_rel);
4079
0
  else
4080
0
  {
4081
0
    int     flags = 0;
4082
0
    GroupPathExtraData extra;
4083
4084
    /*
4085
     * Determine whether it's possible to perform sort-based
4086
     * implementations of grouping.  (Note that if processed_groupClause
4087
     * is empty, grouping_is_sortable() is trivially true, and all the
4088
     * pathkeys_contained_in() tests will succeed too, so that we'll
4089
     * consider every surviving input path.)
4090
     *
4091
     * If we have grouping sets, we might be able to sort some but not all
4092
     * of them; in this case, we need can_sort to be true as long as we
4093
     * must consider any sorted-input plan.
4094
     */
4095
0
    if ((gd && gd->rollups != NIL)
4096
0
      || grouping_is_sortable(root->processed_groupClause))
4097
0
      flags |= GROUPING_CAN_USE_SORT;
4098
4099
    /*
4100
     * Determine whether we should consider hash-based implementations of
4101
     * grouping.
4102
     *
4103
     * Hashed aggregation only applies if we're grouping. If we have
4104
     * grouping sets, some groups might be hashable but others not; in
4105
     * this case we set can_hash true as long as there is nothing globally
4106
     * preventing us from hashing (and we should therefore consider plans
4107
     * with hashes).
4108
     *
4109
     * Executor doesn't support hashed aggregation with DISTINCT or ORDER
4110
     * BY aggregates.  (Doing so would imply storing *all* the input
4111
     * values in the hash table, and/or running many sorts in parallel,
4112
     * either of which seems like a certain loser.)  We similarly don't
4113
     * support ordered-set aggregates in hashed aggregation, but that case
4114
     * is also included in the numOrderedAggs count.
4115
     *
4116
     * Note: grouping_is_hashable() is much more expensive to check than
4117
     * the other gating conditions, so we want to do it last.
4118
     */
4119
0
    if ((parse->groupClause != NIL &&
4120
0
       root->numOrderedAggs == 0 &&
4121
0
       (gd ? gd->any_hashable : grouping_is_hashable(root->processed_groupClause))))
4122
0
      flags |= GROUPING_CAN_USE_HASH;
4123
4124
    /*
4125
     * Determine whether partial aggregation is possible.
4126
     */
4127
0
    if (can_partial_agg(root))
4128
0
      flags |= GROUPING_CAN_PARTIAL_AGG;
4129
4130
0
    extra.flags = flags;
4131
0
    extra.target_parallel_safe = target_parallel_safe;
4132
0
    extra.havingQual = parse->havingQual;
4133
0
    extra.targetList = parse->targetList;
4134
0
    extra.partial_costs_set = false;
4135
4136
    /*
4137
     * Determine whether partitionwise aggregation is in theory possible.
4138
     * It can be disabled by the user, and for now, we don't try to
4139
     * support grouping sets.  create_ordinary_grouping_paths() will check
4140
     * additional conditions, such as whether input_rel is partitioned.
4141
     */
4142
0
    if (enable_partitionwise_aggregate && !parse->groupingSets)
4143
0
      extra.patype = PARTITIONWISE_AGGREGATE_FULL;
4144
0
    else
4145
0
      extra.patype = PARTITIONWISE_AGGREGATE_NONE;
4146
4147
0
    create_ordinary_grouping_paths(root, input_rel, grouped_rel,
4148
0
                     &agg_costs, gd, &extra,
4149
0
                     &partially_grouped_rel);
4150
0
  }
4151
4152
0
  set_cheapest(grouped_rel);
4153
0
  return grouped_rel;
4154
0
}
4155
4156
/*
4157
 * make_grouping_rel
4158
 *
4159
 * Create a new grouping rel and set basic properties.
4160
 *
4161
 * input_rel represents the underlying scan/join relation.
4162
 * target is the output expected from the grouping relation.
4163
 */
4164
static RelOptInfo *
4165
make_grouping_rel(PlannerInfo *root, RelOptInfo *input_rel,
4166
          PathTarget *target, bool target_parallel_safe,
4167
          Node *havingQual)
4168
0
{
4169
0
  RelOptInfo *grouped_rel;
4170
4171
0
  if (IS_OTHER_REL(input_rel))
4172
0
  {
4173
0
    grouped_rel = fetch_upper_rel(root, UPPERREL_GROUP_AGG,
4174
0
                    input_rel->relids);
4175
0
    grouped_rel->reloptkind = RELOPT_OTHER_UPPER_REL;
4176
0
  }
4177
0
  else
4178
0
  {
4179
    /*
4180
     * By tradition, the relids set for the main grouping relation is
4181
     * NULL.  (This could be changed, but might require adjustments
4182
     * elsewhere.)
4183
     */
4184
0
    grouped_rel = fetch_upper_rel(root, UPPERREL_GROUP_AGG, NULL);
4185
0
  }
4186
4187
  /* Set target. */
4188
0
  grouped_rel->reltarget = target;
4189
4190
  /*
4191
   * If the input relation is not parallel-safe, then the grouped relation
4192
   * can't be parallel-safe, either.  Otherwise, it's parallel-safe if the
4193
   * target list and HAVING quals are parallel-safe.
4194
   */
4195
0
  if (input_rel->consider_parallel && target_parallel_safe &&
4196
0
    is_parallel_safe(root, havingQual))
4197
0
    grouped_rel->consider_parallel = true;
4198
4199
  /* Assume that the same path generation strategies are allowed */
4200
0
  grouped_rel->pgs_mask = input_rel->pgs_mask;
4201
4202
  /*
4203
   * If the input rel belongs to a single FDW, so does the grouped rel.
4204
   */
4205
0
  grouped_rel->serverid = input_rel->serverid;
4206
0
  grouped_rel->userid = input_rel->userid;
4207
0
  grouped_rel->useridiscurrent = input_rel->useridiscurrent;
4208
0
  grouped_rel->fdwroutine = input_rel->fdwroutine;
4209
4210
0
  return grouped_rel;
4211
0
}
4212
4213
/*
4214
 * is_degenerate_grouping
4215
 *
4216
 * A degenerate grouping is one in which the query has a HAVING qual and/or
4217
 * grouping sets, but no aggregates and no GROUP BY (which implies that the
4218
 * grouping sets are all empty).
4219
 */
4220
static bool
4221
is_degenerate_grouping(PlannerInfo *root)
4222
0
{
4223
0
  Query    *parse = root->parse;
4224
4225
0
  return (root->hasHavingQual || parse->groupingSets) &&
4226
0
    !parse->hasAggs && parse->groupClause == NIL;
4227
0
}
4228
4229
/*
4230
 * create_degenerate_grouping_paths
4231
 *
4232
 * When the grouping is degenerate (see is_degenerate_grouping), we are
4233
 * supposed to emit either zero or one row for each grouping set depending on
4234
 * whether HAVING succeeds.  Furthermore, there cannot be any variables in
4235
 * either HAVING or the targetlist, so we actually do not need the FROM table
4236
 * at all! We can just throw away the plan-so-far and generate a Result node.
4237
 * This is a sufficiently unusual corner case that it's not worth contorting
4238
 * the structure of this module to avoid having to generate the earlier paths
4239
 * in the first place.
4240
 */
4241
static void
4242
create_degenerate_grouping_paths(PlannerInfo *root, RelOptInfo *input_rel,
4243
                 RelOptInfo *grouped_rel)
4244
0
{
4245
0
  Query    *parse = root->parse;
4246
0
  int     nrows;
4247
0
  Path     *path;
4248
4249
0
  nrows = list_length(parse->groupingSets);
4250
0
  if (nrows > 1)
4251
0
  {
4252
    /*
4253
     * Doesn't seem worthwhile writing code to cons up a generate_series
4254
     * or a values scan to emit multiple rows. Instead just make N clones
4255
     * and append them.  (With a volatile HAVING clause, this means you
4256
     * might get between 0 and N output rows. Offhand I think that's
4257
     * desired.)
4258
     */
4259
0
    AppendPathInput append = {0};
4260
4261
0
    while (--nrows >= 0)
4262
0
    {
4263
0
      path = (Path *)
4264
0
        create_group_result_path(root, grouped_rel,
4265
0
                     grouped_rel->reltarget,
4266
0
                     (List *) parse->havingQual);
4267
0
      append.subpaths = lappend(append.subpaths, path);
4268
0
    }
4269
0
    path = (Path *)
4270
0
      create_append_path(root,
4271
0
                 grouped_rel,
4272
0
                 append,
4273
0
                 NIL,
4274
0
                 NULL,
4275
0
                 0,
4276
0
                 false,
4277
0
                 -1);
4278
0
  }
4279
0
  else
4280
0
  {
4281
    /* No grouping sets, or just one, so one output row */
4282
0
    path = (Path *)
4283
0
      create_group_result_path(root, grouped_rel,
4284
0
                   grouped_rel->reltarget,
4285
0
                   (List *) parse->havingQual);
4286
0
  }
4287
4288
0
  add_path(grouped_rel, path);
4289
0
}
4290
4291
/*
4292
 * create_ordinary_grouping_paths
4293
 *
4294
 * Create grouping paths for the ordinary (that is, non-degenerate) case.
4295
 *
4296
 * We need to consider sorted and hashed aggregation in the same function,
4297
 * because otherwise (1) it would be harder to throw an appropriate error
4298
 * message if neither way works, and (2) we should not allow hashtable size
4299
 * considerations to dissuade us from using hashing if sorting is not possible.
4300
 *
4301
 * *partially_grouped_rel_p will be set to the partially grouped rel which this
4302
 * function creates, or to NULL if it doesn't create one.
4303
 */
4304
static void
4305
create_ordinary_grouping_paths(PlannerInfo *root, RelOptInfo *input_rel,
4306
                 RelOptInfo *grouped_rel,
4307
                 const AggClauseCosts *agg_costs,
4308
                 grouping_sets_data *gd,
4309
                 GroupPathExtraData *extra,
4310
                 RelOptInfo **partially_grouped_rel_p)
4311
0
{
4312
0
  RelOptInfo *partially_grouped_rel = NULL;
4313
0
  PartitionwiseAggregateType patype = PARTITIONWISE_AGGREGATE_NONE;
4314
4315
  /*
4316
   * If this is the topmost grouping relation or if the parent relation is
4317
   * doing some form of partitionwise aggregation, then we may be able to do
4318
   * it at this level also.  However, if the input relation is not
4319
   * partitioned, partitionwise aggregate is impossible.
4320
   */
4321
0
  if (extra->patype != PARTITIONWISE_AGGREGATE_NONE &&
4322
0
    IS_PARTITIONED_REL(input_rel))
4323
0
  {
4324
    /*
4325
     * If this is the topmost relation or if the parent relation is doing
4326
     * full partitionwise aggregation, then we can do full partitionwise
4327
     * aggregation provided that the GROUP BY clause contains all of the
4328
     * partitioning columns at this level and the collation used by GROUP
4329
     * BY matches the partitioning collation.  Otherwise, we can do at
4330
     * most partial partitionwise aggregation.  But if partial aggregation
4331
     * is not supported in general then we can't use it for partitionwise
4332
     * aggregation either.
4333
     *
4334
     * Check parse->groupClause not processed_groupClause, because it's
4335
     * okay if some of the partitioning columns were proved redundant.
4336
     */
4337
0
    if (extra->patype == PARTITIONWISE_AGGREGATE_FULL &&
4338
0
      group_by_has_partkey(input_rel, extra->targetList,
4339
0
                 root->parse->groupClause))
4340
0
      patype = PARTITIONWISE_AGGREGATE_FULL;
4341
0
    else if ((extra->flags & GROUPING_CAN_PARTIAL_AGG) != 0)
4342
0
      patype = PARTITIONWISE_AGGREGATE_PARTIAL;
4343
0
    else
4344
0
      patype = PARTITIONWISE_AGGREGATE_NONE;
4345
0
  }
4346
4347
  /*
4348
   * Before generating paths for grouped_rel, we first generate any possible
4349
   * partially grouped paths; that way, later code can easily consider both
4350
   * parallel and non-parallel approaches to grouping.
4351
   */
4352
0
  if ((extra->flags & GROUPING_CAN_PARTIAL_AGG) != 0)
4353
0
  {
4354
0
    bool    force_rel_creation;
4355
4356
    /*
4357
     * If we're doing partitionwise aggregation at this level, force
4358
     * creation of a partially_grouped_rel so we can add partitionwise
4359
     * paths to it.
4360
     */
4361
0
    force_rel_creation = (patype == PARTITIONWISE_AGGREGATE_PARTIAL);
4362
4363
0
    partially_grouped_rel =
4364
0
      create_partial_grouping_paths(root,
4365
0
                      grouped_rel,
4366
0
                      input_rel,
4367
0
                      gd,
4368
0
                      extra,
4369
0
                      force_rel_creation);
4370
0
  }
4371
4372
  /* Set out parameter. */
4373
0
  *partially_grouped_rel_p = partially_grouped_rel;
4374
4375
  /* Apply partitionwise aggregation technique, if possible. */
4376
0
  if (patype != PARTITIONWISE_AGGREGATE_NONE)
4377
0
    create_partitionwise_grouping_paths(root, input_rel, grouped_rel,
4378
0
                      partially_grouped_rel, agg_costs,
4379
0
                      gd, patype, extra);
4380
4381
  /* If we are doing partial aggregation only, return. */
4382
0
  if (extra->patype == PARTITIONWISE_AGGREGATE_PARTIAL)
4383
0
  {
4384
0
    Assert(partially_grouped_rel);
4385
4386
0
    if (partially_grouped_rel->pathlist)
4387
0
      set_cheapest(partially_grouped_rel);
4388
4389
0
    return;
4390
0
  }
4391
4392
  /* Gather any partially grouped partial paths. */
4393
0
  if (partially_grouped_rel && partially_grouped_rel->partial_pathlist)
4394
0
    gather_grouping_paths(root, partially_grouped_rel);
4395
4396
  /* Now choose the best path(s) for partially_grouped_rel. */
4397
0
  if (partially_grouped_rel && partially_grouped_rel->pathlist)
4398
0
    set_cheapest(partially_grouped_rel);
4399
4400
  /* Build final grouping paths */
4401
0
  add_paths_to_grouping_rel(root, input_rel, grouped_rel,
4402
0
                partially_grouped_rel, agg_costs, gd,
4403
0
                extra);
4404
4405
  /* Give a helpful error if we failed to find any implementation */
4406
0
  if (grouped_rel->pathlist == NIL)
4407
0
    ereport(ERROR,
4408
0
        (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4409
0
         errmsg("could not implement GROUP BY"),
4410
0
         errdetail("Some of the datatypes only support hashing, while others only support sorting.")));
4411
4412
  /*
4413
   * If there is an FDW that's responsible for all baserels of the query,
4414
   * let it consider adding ForeignPaths.
4415
   */
4416
0
  if (grouped_rel->fdwroutine &&
4417
0
    grouped_rel->fdwroutine->GetForeignUpperPaths)
4418
0
    grouped_rel->fdwroutine->GetForeignUpperPaths(root, UPPERREL_GROUP_AGG,
4419
0
                            input_rel, grouped_rel,
4420
0
                            extra);
4421
4422
  /* Let extensions possibly add some more paths */
4423
0
  if (create_upper_paths_hook)
4424
0
    (*create_upper_paths_hook) (root, UPPERREL_GROUP_AGG,
4425
0
                  input_rel, grouped_rel,
4426
0
                  extra);
4427
0
}
4428
4429
/*
4430
 * For a given input path, consider the possible ways of doing grouping sets on
4431
 * it, by combinations of hashing and sorting.  This can be called multiple
4432
 * times, so it's important that it not scribble on input.  No result is
4433
 * returned, but any generated paths are added to grouped_rel.
4434
 */
4435
static void
4436
consider_groupingsets_paths(PlannerInfo *root,
4437
              RelOptInfo *grouped_rel,
4438
              Path *path,
4439
              bool is_sorted,
4440
              bool can_hash,
4441
              grouping_sets_data *gd,
4442
              const AggClauseCosts *agg_costs,
4443
              double dNumGroups)
4444
0
{
4445
0
  Query    *parse = root->parse;
4446
0
  Size    hash_mem_limit = get_hash_memory_limit();
4447
4448
  /*
4449
   * If we're not being offered sorted input, then only consider plans that
4450
   * can be done entirely by hashing.
4451
   *
4452
   * We can hash everything if it looks like it'll fit in hash_mem. But if
4453
   * the input is actually sorted despite not being advertised as such, we
4454
   * prefer to make use of that in order to use less memory.
4455
   *
4456
   * If none of the grouping sets are sortable, then ignore the hash_mem
4457
   * limit and generate a path anyway, since otherwise we'll just fail.
4458
   */
4459
0
  if (!is_sorted)
4460
0
  {
4461
0
    List     *new_rollups = NIL;
4462
0
    RollupData *unhashed_rollup = NULL;
4463
0
    List     *sets_data;
4464
0
    List     *empty_sets_data = NIL;
4465
0
    List     *empty_sets = NIL;
4466
0
    ListCell   *lc;
4467
0
    ListCell   *l_start = list_head(gd->rollups);
4468
0
    AggStrategy strat = AGG_HASHED;
4469
0
    double    hashsize;
4470
0
    double    exclude_groups = 0.0;
4471
4472
0
    Assert(can_hash);
4473
4474
    /*
4475
     * If the input is coincidentally sorted usefully (which can happen
4476
     * even if is_sorted is false, since that only means that our caller
4477
     * has set up the sorting for us), then save some hashtable space by
4478
     * making use of that. But we need to watch out for degenerate cases:
4479
     *
4480
     * 1) If there are any empty grouping sets, then group_pathkeys might
4481
     * be NIL if all non-empty grouping sets are unsortable. In this case,
4482
     * there will be a rollup containing only empty groups, and the
4483
     * pathkeys_contained_in test is vacuously true; this is ok.
4484
     *
4485
     * XXX: the above relies on the fact that group_pathkeys is generated
4486
     * from the first rollup. If we add the ability to consider multiple
4487
     * sort orders for grouping input, this assumption might fail.
4488
     *
4489
     * 2) If there are no empty sets and only unsortable sets, then the
4490
     * rollups list will be empty (and thus l_start == NULL), and
4491
     * group_pathkeys will be NIL; we must ensure that the vacuously-true
4492
     * pathkeys_contained_in test doesn't cause us to crash.
4493
     */
4494
0
    if (l_start != NULL &&
4495
0
      pathkeys_contained_in(root->group_pathkeys, path->pathkeys))
4496
0
    {
4497
0
      unhashed_rollup = lfirst_node(RollupData, l_start);
4498
0
      exclude_groups = unhashed_rollup->numGroups;
4499
0
      l_start = lnext(gd->rollups, l_start);
4500
0
    }
4501
4502
0
    hashsize = estimate_hashagg_tablesize(root,
4503
0
                        path,
4504
0
                        agg_costs,
4505
0
                        dNumGroups - exclude_groups);
4506
4507
    /*
4508
     * gd->rollups is empty if we have only unsortable columns to work
4509
     * with.  Override hash_mem in that case; otherwise, we'll rely on the
4510
     * sorted-input case to generate usable mixed paths.
4511
     */
4512
0
    if (hashsize > hash_mem_limit && gd->rollups)
4513
0
      return;       /* nope, won't fit */
4514
4515
    /*
4516
     * We need to burst the existing rollups list into individual grouping
4517
     * sets and recompute a groupClause for each set.
4518
     */
4519
0
    sets_data = list_copy(gd->unsortable_sets);
4520
4521
0
    for_each_cell(lc, gd->rollups, l_start)
4522
0
    {
4523
0
      RollupData *rollup = lfirst_node(RollupData, lc);
4524
4525
      /*
4526
       * If we find an unhashable rollup that's not been skipped by the
4527
       * "actually sorted" check above, we can't cope; we'd need sorted
4528
       * input (with a different sort order) but we can't get that here.
4529
       * So bail out; we'll get a valid path from the is_sorted case
4530
       * instead.
4531
       *
4532
       * The mere presence of empty grouping sets doesn't make a rollup
4533
       * unhashable (see preprocess_grouping_sets), we handle those
4534
       * specially below.
4535
       */
4536
0
      if (!rollup->hashable)
4537
0
        return;
4538
4539
0
      sets_data = list_concat(sets_data, rollup->gsets_data);
4540
0
    }
4541
0
    foreach(lc, sets_data)
4542
0
    {
4543
0
      GroupingSetData *gs = lfirst_node(GroupingSetData, lc);
4544
0
      List     *gset = gs->set;
4545
0
      RollupData *rollup;
4546
4547
0
      if (gset == NIL)
4548
0
      {
4549
        /* Empty grouping sets can't be hashed. */
4550
0
        empty_sets_data = lappend(empty_sets_data, gs);
4551
0
        empty_sets = lappend(empty_sets, NIL);
4552
0
      }
4553
0
      else
4554
0
      {
4555
0
        rollup = makeNode(RollupData);
4556
4557
0
        rollup->groupClause = preprocess_groupclause(root, gset);
4558
0
        rollup->gsets_data = list_make1(gs);
4559
0
        rollup->gsets = remap_to_groupclause_idx(rollup->groupClause,
4560
0
                             rollup->gsets_data,
4561
0
                             gd->tleref_to_colnum_map);
4562
0
        rollup->numGroups = gs->numGroups;
4563
0
        rollup->hashable = true;
4564
0
        rollup->is_hashed = true;
4565
0
        new_rollups = lappend(new_rollups, rollup);
4566
0
      }
4567
0
    }
4568
4569
    /*
4570
     * If we didn't find anything nonempty to hash, then bail.  We'll
4571
     * generate a path from the is_sorted case.
4572
     */
4573
0
    if (new_rollups == NIL)
4574
0
      return;
4575
4576
    /*
4577
     * If there were empty grouping sets they should have been in the
4578
     * first rollup.
4579
     */
4580
0
    Assert(!unhashed_rollup || !empty_sets);
4581
4582
0
    if (unhashed_rollup)
4583
0
    {
4584
0
      new_rollups = lappend(new_rollups, unhashed_rollup);
4585
0
      strat = AGG_MIXED;
4586
0
    }
4587
0
    else if (empty_sets)
4588
0
    {
4589
0
      RollupData *rollup = makeNode(RollupData);
4590
4591
0
      rollup->groupClause = NIL;
4592
0
      rollup->gsets_data = empty_sets_data;
4593
0
      rollup->gsets = empty_sets;
4594
0
      rollup->numGroups = list_length(empty_sets);
4595
0
      rollup->hashable = false;
4596
0
      rollup->is_hashed = false;
4597
0
      new_rollups = lappend(new_rollups, rollup);
4598
0
      strat = AGG_MIXED;
4599
0
    }
4600
4601
0
    add_path(grouped_rel, (Path *)
4602
0
         create_groupingsets_path(root,
4603
0
                      grouped_rel,
4604
0
                      path,
4605
0
                      (List *) parse->havingQual,
4606
0
                      strat,
4607
0
                      new_rollups,
4608
0
                      agg_costs));
4609
0
    return;
4610
0
  }
4611
4612
  /*
4613
   * If we have sorted input but nothing we can do with it, bail.
4614
   */
4615
0
  if (gd->rollups == NIL)
4616
0
    return;
4617
4618
  /*
4619
   * Given sorted input, we try and make two paths: one sorted and one mixed
4620
   * sort/hash. (We need to try both because hashagg might be disabled, or
4621
   * some columns might not be sortable.)
4622
   *
4623
   * can_hash is passed in as false if some obstacle elsewhere (such as
4624
   * ordered aggs) means that we shouldn't consider hashing at all.
4625
   */
4626
0
  if (can_hash && gd->any_hashable)
4627
0
  {
4628
0
    List     *rollups = NIL;
4629
0
    List     *hash_sets = list_copy(gd->unsortable_sets);
4630
0
    double    availspace = hash_mem_limit;
4631
0
    ListCell   *lc;
4632
4633
    /*
4634
     * Account first for space needed for groups we can't sort at all.
4635
     */
4636
0
    availspace -= estimate_hashagg_tablesize(root,
4637
0
                         path,
4638
0
                         agg_costs,
4639
0
                         gd->dNumHashGroups);
4640
4641
0
    if (availspace > 0 && list_length(gd->rollups) > 1)
4642
0
    {
4643
0
      double    scale;
4644
0
      int     num_rollups = list_length(gd->rollups);
4645
0
      int     k_capacity;
4646
0
      int      *k_weights = palloc(num_rollups * sizeof(int));
4647
0
      Bitmapset  *hash_items = NULL;
4648
0
      int     i;
4649
4650
      /*
4651
       * We treat this as a knapsack problem: the knapsack capacity
4652
       * represents hash_mem, the item weights are the estimated memory
4653
       * usage of the hashtables needed to implement a single rollup,
4654
       * and we really ought to use the cost saving as the item value;
4655
       * however, currently the costs assigned to sort nodes don't
4656
       * reflect the comparison costs well, and so we treat all items as
4657
       * of equal value (each rollup we hash instead saves us one sort).
4658
       *
4659
       * To use the discrete knapsack, we need to scale the values to a
4660
       * reasonably small bounded range.  We choose to allow a 5% error
4661
       * margin; we have no more than 4096 rollups in the worst possible
4662
       * case, which with a 5% error margin will require a bit over 42MB
4663
       * of workspace. (Anyone wanting to plan queries that complex had
4664
       * better have the memory for it.  In more reasonable cases, with
4665
       * no more than a couple of dozen rollups, the memory usage will
4666
       * be negligible.)
4667
       *
4668
       * k_capacity is naturally bounded, but we clamp the values for
4669
       * scale and weight (below) to avoid overflows or underflows (or
4670
       * uselessly trying to use a scale factor less than 1 byte).
4671
       */
4672
0
      scale = Max(availspace / (20.0 * num_rollups), 1.0);
4673
0
      k_capacity = (int) floor(availspace / scale);
4674
4675
      /*
4676
       * We leave the first rollup out of consideration since it's the
4677
       * one that matches the input sort order.  We assign indexes "i"
4678
       * to only those entries considered for hashing; the second loop,
4679
       * below, must use the same condition.
4680
       */
4681
0
      i = 0;
4682
0
      for_each_from(lc, gd->rollups, 1)
4683
0
      {
4684
0
        RollupData *rollup = lfirst_node(RollupData, lc);
4685
4686
0
        if (rollup->hashable)
4687
0
        {
4688
0
          double    sz = estimate_hashagg_tablesize(root,
4689
0
                                path,
4690
0
                                agg_costs,
4691
0
                                rollup->numGroups);
4692
4693
          /*
4694
           * If sz is enormous, but hash_mem (and hence scale) is
4695
           * small, avoid integer overflow here.
4696
           */
4697
0
          k_weights[i] = (int) Min(floor(sz / scale),
4698
0
                       k_capacity + 1.0);
4699
0
          ++i;
4700
0
        }
4701
0
      }
4702
4703
      /*
4704
       * Apply knapsack algorithm; compute the set of items which
4705
       * maximizes the value stored (in this case the number of sorts
4706
       * saved) while keeping the total size (approximately) within
4707
       * capacity.
4708
       */
4709
0
      if (i > 0)
4710
0
        hash_items = DiscreteKnapsack(k_capacity, i, k_weights, NULL);
4711
4712
0
      if (!bms_is_empty(hash_items))
4713
0
      {
4714
0
        rollups = list_make1(linitial(gd->rollups));
4715
4716
0
        i = 0;
4717
0
        for_each_from(lc, gd->rollups, 1)
4718
0
        {
4719
0
          RollupData *rollup = lfirst_node(RollupData, lc);
4720
4721
0
          if (rollup->hashable)
4722
0
          {
4723
0
            if (bms_is_member(i, hash_items))
4724
0
              hash_sets = list_concat(hash_sets,
4725
0
                          rollup->gsets_data);
4726
0
            else
4727
0
              rollups = lappend(rollups, rollup);
4728
0
            ++i;
4729
0
          }
4730
0
          else
4731
0
            rollups = lappend(rollups, rollup);
4732
0
        }
4733
0
      }
4734
0
    }
4735
4736
0
    if (!rollups && hash_sets)
4737
0
      rollups = list_copy(gd->rollups);
4738
4739
0
    foreach(lc, hash_sets)
4740
0
    {
4741
0
      GroupingSetData *gs = lfirst_node(GroupingSetData, lc);
4742
0
      RollupData *rollup = makeNode(RollupData);
4743
4744
0
      Assert(gs->set != NIL);
4745
4746
0
      rollup->groupClause = preprocess_groupclause(root, gs->set);
4747
0
      rollup->gsets_data = list_make1(gs);
4748
0
      rollup->gsets = remap_to_groupclause_idx(rollup->groupClause,
4749
0
                           rollup->gsets_data,
4750
0
                           gd->tleref_to_colnum_map);
4751
0
      rollup->numGroups = gs->numGroups;
4752
0
      rollup->hashable = true;
4753
0
      rollup->is_hashed = true;
4754
0
      rollups = lcons(rollup, rollups);
4755
0
    }
4756
4757
0
    if (rollups)
4758
0
    {
4759
0
      add_path(grouped_rel, (Path *)
4760
0
           create_groupingsets_path(root,
4761
0
                        grouped_rel,
4762
0
                        path,
4763
0
                        (List *) parse->havingQual,
4764
0
                        AGG_MIXED,
4765
0
                        rollups,
4766
0
                        agg_costs));
4767
0
    }
4768
0
  }
4769
4770
  /*
4771
   * Now try the simple sorted case.
4772
   */
4773
0
  if (!gd->unsortable_sets)
4774
0
    add_path(grouped_rel, (Path *)
4775
0
         create_groupingsets_path(root,
4776
0
                      grouped_rel,
4777
0
                      path,
4778
0
                      (List *) parse->havingQual,
4779
0
                      AGG_SORTED,
4780
0
                      gd->rollups,
4781
0
                      agg_costs));
4782
0
}
4783
4784
/*
4785
 * create_window_paths
4786
 *
4787
 * Build a new upperrel containing Paths for window-function evaluation.
4788
 *
4789
 * input_rel: contains the source-data Paths
4790
 * input_target: result of make_window_input_target
4791
 * output_target: what the topmost WindowAggPath should return
4792
 * wflists: result of find_window_functions
4793
 * activeWindows: result of select_active_windows
4794
 *
4795
 * Note: all Paths in input_rel are expected to return input_target.
4796
 */
4797
static RelOptInfo *
4798
create_window_paths(PlannerInfo *root,
4799
          RelOptInfo *input_rel,
4800
          PathTarget *input_target,
4801
          PathTarget *output_target,
4802
          bool output_target_parallel_safe,
4803
          WindowFuncLists *wflists,
4804
          List *activeWindows)
4805
0
{
4806
0
  RelOptInfo *window_rel;
4807
0
  ListCell   *lc;
4808
4809
  /* For now, do all work in the (WINDOW, NULL) upperrel */
4810
0
  window_rel = fetch_upper_rel(root, UPPERREL_WINDOW, NULL);
4811
4812
  /*
4813
   * If the input relation is not parallel-safe, then the window relation
4814
   * can't be parallel-safe, either.  Otherwise, we need to examine the
4815
   * target list and active windows for non-parallel-safe constructs.
4816
   */
4817
0
  if (input_rel->consider_parallel && output_target_parallel_safe &&
4818
0
    is_parallel_safe(root, (Node *) activeWindows))
4819
0
    window_rel->consider_parallel = true;
4820
4821
  /*
4822
   * If the input rel belongs to a single FDW, so does the window rel.
4823
   */
4824
0
  window_rel->serverid = input_rel->serverid;
4825
0
  window_rel->userid = input_rel->userid;
4826
0
  window_rel->useridiscurrent = input_rel->useridiscurrent;
4827
0
  window_rel->fdwroutine = input_rel->fdwroutine;
4828
4829
  /*
4830
   * Consider computing window functions starting from the existing
4831
   * cheapest-total path (which will likely require a sort) as well as any
4832
   * existing paths that satisfy or partially satisfy root->window_pathkeys.
4833
   */
4834
0
  foreach(lc, input_rel->pathlist)
4835
0
  {
4836
0
    Path     *path = (Path *) lfirst(lc);
4837
0
    int     presorted_keys;
4838
4839
0
    if (path == input_rel->cheapest_total_path ||
4840
0
      pathkeys_count_contained_in(root->window_pathkeys, path->pathkeys,
4841
0
                    &presorted_keys) ||
4842
0
      presorted_keys > 0)
4843
0
      create_one_window_path(root,
4844
0
                   window_rel,
4845
0
                   path,
4846
0
                   input_target,
4847
0
                   output_target,
4848
0
                   wflists,
4849
0
                   activeWindows);
4850
0
  }
4851
4852
  /*
4853
   * If there is an FDW that's responsible for all baserels of the query,
4854
   * let it consider adding ForeignPaths.
4855
   */
4856
0
  if (window_rel->fdwroutine &&
4857
0
    window_rel->fdwroutine->GetForeignUpperPaths)
4858
0
    window_rel->fdwroutine->GetForeignUpperPaths(root, UPPERREL_WINDOW,
4859
0
                           input_rel, window_rel,
4860
0
                           NULL);
4861
4862
  /* Let extensions possibly add some more paths */
4863
0
  if (create_upper_paths_hook)
4864
0
    (*create_upper_paths_hook) (root, UPPERREL_WINDOW,
4865
0
                  input_rel, window_rel, NULL);
4866
4867
  /* Now choose the best path(s) */
4868
0
  set_cheapest(window_rel);
4869
4870
0
  return window_rel;
4871
0
}
4872
4873
/*
4874
 * Stack window-function implementation steps atop the given Path, and
4875
 * add the result to window_rel.
4876
 *
4877
 * window_rel: upperrel to contain result
4878
 * path: input Path to use (must return input_target)
4879
 * input_target: result of make_window_input_target
4880
 * output_target: what the topmost WindowAggPath should return
4881
 * wflists: result of find_window_functions
4882
 * activeWindows: result of select_active_windows
4883
 */
4884
static void
4885
create_one_window_path(PlannerInfo *root,
4886
             RelOptInfo *window_rel,
4887
             Path *path,
4888
             PathTarget *input_target,
4889
             PathTarget *output_target,
4890
             WindowFuncLists *wflists,
4891
             List *activeWindows)
4892
0
{
4893
0
  PathTarget *window_target;
4894
0
  ListCell   *l;
4895
0
  List     *topqual = NIL;
4896
4897
  /*
4898
   * Since each window clause could require a different sort order, we stack
4899
   * up a WindowAgg node for each clause, with sort steps between them as
4900
   * needed.  (We assume that select_active_windows chose a good order for
4901
   * executing the clauses in.)
4902
   *
4903
   * input_target should contain all Vars and Aggs needed for the result.
4904
   * (In some cases we wouldn't need to propagate all of these all the way
4905
   * to the top, since they might only be needed as inputs to WindowFuncs.
4906
   * It's probably not worth trying to optimize that though.)  It must also
4907
   * contain all window partitioning and sorting expressions, to ensure
4908
   * they're computed only once at the bottom of the stack (that's critical
4909
   * for volatile functions).  As we climb up the stack, we'll add outputs
4910
   * for the WindowFuncs computed at each level.
4911
   */
4912
0
  window_target = input_target;
4913
4914
0
  foreach(l, activeWindows)
4915
0
  {
4916
0
    WindowClause *wc = lfirst_node(WindowClause, l);
4917
0
    List     *window_pathkeys;
4918
0
    List     *runcondition = NIL;
4919
0
    int     presorted_keys;
4920
0
    bool    is_sorted;
4921
0
    bool    topwindow;
4922
0
    ListCell   *lc2;
4923
4924
0
    window_pathkeys = make_pathkeys_for_window(root,
4925
0
                           wc,
4926
0
                           root->processed_tlist);
4927
4928
0
    is_sorted = pathkeys_count_contained_in(window_pathkeys,
4929
0
                        path->pathkeys,
4930
0
                        &presorted_keys);
4931
4932
    /* Sort if necessary */
4933
0
    if (!is_sorted)
4934
0
    {
4935
      /*
4936
       * No presorted keys or incremental sort disabled, just perform a
4937
       * complete sort.
4938
       */
4939
0
      if (presorted_keys == 0 || !enable_incremental_sort)
4940
0
        path = (Path *) create_sort_path(root, window_rel,
4941
0
                         path,
4942
0
                         window_pathkeys,
4943
0
                         -1.0);
4944
0
      else
4945
0
      {
4946
        /*
4947
         * Since we have presorted keys and incremental sort is
4948
         * enabled, just use incremental sort.
4949
         */
4950
0
        path = (Path *) create_incremental_sort_path(root,
4951
0
                               window_rel,
4952
0
                               path,
4953
0
                               window_pathkeys,
4954
0
                               presorted_keys,
4955
0
                               -1.0);
4956
0
      }
4957
0
    }
4958
4959
0
    if (lnext(activeWindows, l))
4960
0
    {
4961
      /*
4962
       * Add the current WindowFuncs to the output target for this
4963
       * intermediate WindowAggPath.  We must copy window_target to
4964
       * avoid changing the previous path's target.
4965
       *
4966
       * Note: a WindowFunc adds nothing to the target's eval costs; but
4967
       * we do need to account for the increase in tlist width.
4968
       */
4969
0
      int64   tuple_width = window_target->width;
4970
4971
0
      window_target = copy_pathtarget(window_target);
4972
0
      foreach(lc2, wflists->windowFuncs[wc->winref])
4973
0
      {
4974
0
        WindowFunc *wfunc = lfirst_node(WindowFunc, lc2);
4975
4976
0
        add_column_to_pathtarget(window_target, (Expr *) wfunc, 0);
4977
0
        tuple_width += get_typavgwidth(wfunc->wintype, -1);
4978
0
      }
4979
0
      window_target->width = clamp_width_est(tuple_width);
4980
0
    }
4981
0
    else
4982
0
    {
4983
      /* Install the goal target in the topmost WindowAgg */
4984
0
      window_target = output_target;
4985
0
    }
4986
4987
    /* mark the final item in the list as the top-level window */
4988
0
    topwindow = foreach_current_index(l) == list_length(activeWindows) - 1;
4989
4990
    /*
4991
     * Collect the WindowFuncRunConditions from each WindowFunc and
4992
     * convert them into OpExprs
4993
     */
4994
0
    foreach(lc2, wflists->windowFuncs[wc->winref])
4995
0
    {
4996
0
      ListCell   *lc3;
4997
0
      WindowFunc *wfunc = lfirst_node(WindowFunc, lc2);
4998
4999
0
      foreach(lc3, wfunc->runCondition)
5000
0
      {
5001
0
        WindowFuncRunCondition *wfuncrc =
5002
0
          lfirst_node(WindowFuncRunCondition, lc3);
5003
0
        Expr     *opexpr;
5004
0
        Expr     *leftop;
5005
0
        Expr     *rightop;
5006
5007
0
        if (wfuncrc->wfunc_left)
5008
0
        {
5009
0
          leftop = (Expr *) copyObject(wfunc);
5010
0
          rightop = copyObject(wfuncrc->arg);
5011
0
        }
5012
0
        else
5013
0
        {
5014
0
          leftop = copyObject(wfuncrc->arg);
5015
0
          rightop = (Expr *) copyObject(wfunc);
5016
0
        }
5017
5018
0
        opexpr = make_opclause(wfuncrc->opno,
5019
0
                     BOOLOID,
5020
0
                     false,
5021
0
                     leftop,
5022
0
                     rightop,
5023
0
                     InvalidOid,
5024
0
                     wfuncrc->inputcollid);
5025
5026
0
        runcondition = lappend(runcondition, opexpr);
5027
5028
0
        if (!topwindow)
5029
0
          topqual = lappend(topqual, opexpr);
5030
0
      }
5031
0
    }
5032
5033
0
    path = (Path *)
5034
0
      create_windowagg_path(root, window_rel, path, window_target,
5035
0
                  wflists->windowFuncs[wc->winref],
5036
0
                  runcondition, wc,
5037
0
                  topwindow ? topqual : NIL, topwindow);
5038
0
  }
5039
5040
0
  add_path(window_rel, path);
5041
0
}
5042
5043
/*
5044
 * create_distinct_paths
5045
 *
5046
 * Build a new upperrel containing Paths for SELECT DISTINCT evaluation.
5047
 *
5048
 * input_rel: contains the source-data Paths
5049
 * target: the pathtarget for the result Paths to compute
5050
 *
5051
 * Note: input paths should already compute the desired pathtarget, since
5052
 * Sort/Unique won't project anything.
5053
 */
5054
static RelOptInfo *
5055
create_distinct_paths(PlannerInfo *root, RelOptInfo *input_rel,
5056
            PathTarget *target)
5057
0
{
5058
0
  RelOptInfo *distinct_rel;
5059
5060
  /* For now, do all work in the (DISTINCT, NULL) upperrel */
5061
0
  distinct_rel = fetch_upper_rel(root, UPPERREL_DISTINCT, NULL);
5062
5063
  /*
5064
   * We don't compute anything at this level, so distinct_rel will be
5065
   * parallel-safe if the input rel is parallel-safe.  In particular, if
5066
   * there is a DISTINCT ON (...) clause, any path for the input_rel will
5067
   * output those expressions, and will not be parallel-safe unless those
5068
   * expressions are parallel-safe.
5069
   */
5070
0
  distinct_rel->consider_parallel = input_rel->consider_parallel;
5071
5072
  /*
5073
   * If the input rel belongs to a single FDW, so does the distinct_rel.
5074
   */
5075
0
  distinct_rel->serverid = input_rel->serverid;
5076
0
  distinct_rel->userid = input_rel->userid;
5077
0
  distinct_rel->useridiscurrent = input_rel->useridiscurrent;
5078
0
  distinct_rel->fdwroutine = input_rel->fdwroutine;
5079
5080
  /* build distinct paths based on input_rel's pathlist */
5081
0
  create_final_distinct_paths(root, input_rel, distinct_rel);
5082
5083
  /* now build distinct paths based on input_rel's partial_pathlist */
5084
0
  create_partial_distinct_paths(root, input_rel, distinct_rel, target);
5085
5086
  /* Give a helpful error if we failed to create any paths */
5087
0
  if (distinct_rel->pathlist == NIL)
5088
0
    ereport(ERROR,
5089
0
        (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5090
0
         errmsg("could not implement DISTINCT"),
5091
0
         errdetail("Some of the datatypes only support hashing, while others only support sorting.")));
5092
5093
  /*
5094
   * If there is an FDW that's responsible for all baserels of the query,
5095
   * let it consider adding ForeignPaths.
5096
   */
5097
0
  if (distinct_rel->fdwroutine &&
5098
0
    distinct_rel->fdwroutine->GetForeignUpperPaths)
5099
0
    distinct_rel->fdwroutine->GetForeignUpperPaths(root,
5100
0
                             UPPERREL_DISTINCT,
5101
0
                             input_rel,
5102
0
                             distinct_rel,
5103
0
                             NULL);
5104
5105
  /* Let extensions possibly add some more paths */
5106
0
  if (create_upper_paths_hook)
5107
0
    (*create_upper_paths_hook) (root, UPPERREL_DISTINCT, input_rel,
5108
0
                  distinct_rel, NULL);
5109
5110
  /* Now choose the best path(s) */
5111
0
  set_cheapest(distinct_rel);
5112
5113
0
  return distinct_rel;
5114
0
}
5115
5116
/*
5117
 * create_partial_distinct_paths
5118
 *
5119
 * Process 'input_rel' partial paths and add unique/aggregate paths to the
5120
 * UPPERREL_PARTIAL_DISTINCT rel.  For paths created, add Gather/GatherMerge
5121
 * paths on top and add a final unique/aggregate path to remove any duplicate
5122
 * produced from combining rows from parallel workers.
5123
 */
5124
static void
5125
create_partial_distinct_paths(PlannerInfo *root, RelOptInfo *input_rel,
5126
                RelOptInfo *final_distinct_rel,
5127
                PathTarget *target)
5128
0
{
5129
0
  RelOptInfo *partial_distinct_rel;
5130
0
  Query    *parse;
5131
0
  List     *distinctExprs;
5132
0
  double    numDistinctRows;
5133
0
  Path     *cheapest_partial_path;
5134
0
  ListCell   *lc;
5135
5136
  /* nothing to do when there are no partial paths in the input rel */
5137
0
  if (!input_rel->consider_parallel || input_rel->partial_pathlist == NIL)
5138
0
    return;
5139
5140
0
  parse = root->parse;
5141
5142
  /* can't do parallel DISTINCT ON */
5143
0
  if (parse->hasDistinctOn)
5144
0
    return;
5145
5146
0
  partial_distinct_rel = fetch_upper_rel(root, UPPERREL_PARTIAL_DISTINCT,
5147
0
                       NULL);
5148
0
  partial_distinct_rel->reltarget = target;
5149
0
  partial_distinct_rel->consider_parallel = input_rel->consider_parallel;
5150
5151
  /*
5152
   * If input_rel belongs to a single FDW, so does the partial_distinct_rel.
5153
   */
5154
0
  partial_distinct_rel->serverid = input_rel->serverid;
5155
0
  partial_distinct_rel->userid = input_rel->userid;
5156
0
  partial_distinct_rel->useridiscurrent = input_rel->useridiscurrent;
5157
0
  partial_distinct_rel->fdwroutine = input_rel->fdwroutine;
5158
5159
0
  cheapest_partial_path = linitial(input_rel->partial_pathlist);
5160
5161
0
  distinctExprs = get_sortgrouplist_exprs(root->processed_distinctClause,
5162
0
                      parse->targetList);
5163
5164
  /* estimate how many distinct rows we'll get from each worker */
5165
0
  numDistinctRows = estimate_num_groups(root, distinctExprs,
5166
0
                      cheapest_partial_path->rows,
5167
0
                      NULL, NULL);
5168
5169
  /*
5170
   * Try sorting the cheapest path and incrementally sorting any paths with
5171
   * presorted keys and put a unique paths atop of those.  We'll also
5172
   * attempt to reorder the required pathkeys to match the input path's
5173
   * pathkeys as much as possible, in hopes of avoiding a possible need to
5174
   * re-sort.
5175
   */
5176
0
  if (grouping_is_sortable(root->processed_distinctClause))
5177
0
  {
5178
0
    foreach(lc, input_rel->partial_pathlist)
5179
0
    {
5180
0
      Path     *input_path = (Path *) lfirst(lc);
5181
0
      Path     *sorted_path;
5182
0
      List     *useful_pathkeys_list = NIL;
5183
5184
0
      useful_pathkeys_list =
5185
0
        get_useful_pathkeys_for_distinct(root,
5186
0
                         root->distinct_pathkeys,
5187
0
                         input_path->pathkeys);
5188
0
      Assert(list_length(useful_pathkeys_list) > 0);
5189
5190
0
      foreach_node(List, useful_pathkeys, useful_pathkeys_list)
5191
0
      {
5192
0
        sorted_path = make_ordered_path(root,
5193
0
                        partial_distinct_rel,
5194
0
                        input_path,
5195
0
                        cheapest_partial_path,
5196
0
                        useful_pathkeys,
5197
0
                        -1.0);
5198
5199
0
        if (sorted_path == NULL)
5200
0
          continue;
5201
5202
        /*
5203
         * An empty distinct_pathkeys means all tuples have the same
5204
         * value for the DISTINCT clause.  See
5205
         * create_final_distinct_paths()
5206
         */
5207
0
        if (root->distinct_pathkeys == NIL)
5208
0
        {
5209
0
          Node     *limitCount;
5210
5211
0
          limitCount = (Node *) makeConst(INT8OID, -1, InvalidOid,
5212
0
                          sizeof(int64),
5213
0
                          Int64GetDatum(1), false,
5214
0
                          true);
5215
5216
          /*
5217
           * Apply a LimitPath onto the partial path to restrict the
5218
           * tuples from each worker to 1.
5219
           * create_final_distinct_paths will need to apply an
5220
           * additional LimitPath to restrict this to a single row
5221
           * after the Gather node.  If the query already has a
5222
           * LIMIT clause, then we could end up with three Limit
5223
           * nodes in the final plan.  Consolidating the top two of
5224
           * these could be done, but does not seem worth troubling
5225
           * over.
5226
           */
5227
0
          add_partial_path(partial_distinct_rel, (Path *)
5228
0
                   create_limit_path(root, partial_distinct_rel,
5229
0
                             sorted_path,
5230
0
                             NULL,
5231
0
                             limitCount,
5232
0
                             LIMIT_OPTION_COUNT,
5233
0
                             0, 1));
5234
0
        }
5235
0
        else
5236
0
        {
5237
0
          add_partial_path(partial_distinct_rel, (Path *)
5238
0
                   create_unique_path(root, partial_distinct_rel,
5239
0
                            sorted_path,
5240
0
                            list_length(root->distinct_pathkeys),
5241
0
                            numDistinctRows));
5242
0
        }
5243
0
      }
5244
0
    }
5245
0
  }
5246
5247
  /*
5248
   * Now try hash aggregate paths, if enabled and hashing is possible. Since
5249
   * we're not on the hook to ensure we do our best to create at least one
5250
   * path here, we treat enable_hashagg as a hard off-switch rather than the
5251
   * slightly softer variant in create_final_distinct_paths.
5252
   */
5253
0
  if (enable_hashagg && grouping_is_hashable(root->processed_distinctClause))
5254
0
  {
5255
0
    add_partial_path(partial_distinct_rel, (Path *)
5256
0
             create_agg_path(root,
5257
0
                     partial_distinct_rel,
5258
0
                     cheapest_partial_path,
5259
0
                     cheapest_partial_path->pathtarget,
5260
0
                     AGG_HASHED,
5261
0
                     AGGSPLIT_SIMPLE,
5262
0
                     root->processed_distinctClause,
5263
0
                     NIL,
5264
0
                     NULL,
5265
0
                     numDistinctRows));
5266
0
  }
5267
5268
  /*
5269
   * If there is an FDW that's responsible for all baserels of the query,
5270
   * let it consider adding ForeignPaths.
5271
   */
5272
0
  if (partial_distinct_rel->fdwroutine &&
5273
0
    partial_distinct_rel->fdwroutine->GetForeignUpperPaths)
5274
0
    partial_distinct_rel->fdwroutine->GetForeignUpperPaths(root,
5275
0
                                 UPPERREL_PARTIAL_DISTINCT,
5276
0
                                 input_rel,
5277
0
                                 partial_distinct_rel,
5278
0
                                 NULL);
5279
5280
  /* Let extensions possibly add some more partial paths */
5281
0
  if (create_upper_paths_hook)
5282
0
    (*create_upper_paths_hook) (root, UPPERREL_PARTIAL_DISTINCT,
5283
0
                  input_rel, partial_distinct_rel, NULL);
5284
5285
0
  if (partial_distinct_rel->partial_pathlist != NIL)
5286
0
  {
5287
0
    generate_useful_gather_paths(root, partial_distinct_rel, true);
5288
0
    set_cheapest(partial_distinct_rel);
5289
5290
    /*
5291
     * Finally, create paths to distinctify the final result.  This step
5292
     * is needed to remove any duplicates due to combining rows from
5293
     * parallel workers.
5294
     */
5295
0
    create_final_distinct_paths(root, partial_distinct_rel,
5296
0
                  final_distinct_rel);
5297
0
  }
5298
0
}
5299
5300
/*
5301
 * create_final_distinct_paths
5302
 *    Create distinct paths in 'distinct_rel' based on 'input_rel' pathlist
5303
 *
5304
 * input_rel: contains the source-data paths
5305
 * distinct_rel: destination relation for storing created paths
5306
 */
5307
static RelOptInfo *
5308
create_final_distinct_paths(PlannerInfo *root, RelOptInfo *input_rel,
5309
              RelOptInfo *distinct_rel)
5310
0
{
5311
0
  Query    *parse = root->parse;
5312
0
  Path     *cheapest_input_path = input_rel->cheapest_total_path;
5313
0
  double    numDistinctRows;
5314
0
  bool    allow_hash;
5315
5316
  /* Estimate number of distinct rows there will be */
5317
0
  if (parse->groupClause || parse->groupingSets || parse->hasAggs ||
5318
0
    root->hasHavingQual)
5319
0
  {
5320
    /*
5321
     * If there was grouping or aggregation, use the number of input rows
5322
     * as the estimated number of DISTINCT rows (ie, assume the input is
5323
     * already mostly unique).
5324
     */
5325
0
    numDistinctRows = cheapest_input_path->rows;
5326
0
  }
5327
0
  else
5328
0
  {
5329
    /*
5330
     * Otherwise, the UNIQUE filter has effects comparable to GROUP BY.
5331
     */
5332
0
    List     *distinctExprs;
5333
5334
0
    distinctExprs = get_sortgrouplist_exprs(root->processed_distinctClause,
5335
0
                        parse->targetList);
5336
0
    numDistinctRows = estimate_num_groups(root, distinctExprs,
5337
0
                        cheapest_input_path->rows,
5338
0
                        NULL, NULL);
5339
0
  }
5340
5341
  /*
5342
   * Consider sort-based implementations of DISTINCT, if possible.
5343
   */
5344
0
  if (grouping_is_sortable(root->processed_distinctClause))
5345
0
  {
5346
    /*
5347
     * Firstly, if we have any adequately-presorted paths, just stick a
5348
     * Unique node on those.  We also, consider doing an explicit sort of
5349
     * the cheapest input path and Unique'ing that.  If any paths have
5350
     * presorted keys then we'll create an incremental sort atop of those
5351
     * before adding a unique node on the top.  We'll also attempt to
5352
     * reorder the required pathkeys to match the input path's pathkeys as
5353
     * much as possible, in hopes of avoiding a possible need to re-sort.
5354
     *
5355
     * When we have DISTINCT ON, we must sort by the more rigorous of
5356
     * DISTINCT and ORDER BY, else it won't have the desired behavior.
5357
     * Also, if we do have to do an explicit sort, we might as well use
5358
     * the more rigorous ordering to avoid a second sort later.  (Note
5359
     * that the parser will have ensured that one clause is a prefix of
5360
     * the other.)
5361
     */
5362
0
    List     *needed_pathkeys;
5363
0
    ListCell   *lc;
5364
0
    double    limittuples = root->distinct_pathkeys == NIL ? 1.0 : -1.0;
5365
5366
0
    if (parse->hasDistinctOn &&
5367
0
      list_length(root->distinct_pathkeys) <
5368
0
      list_length(root->sort_pathkeys))
5369
0
      needed_pathkeys = root->sort_pathkeys;
5370
0
    else
5371
0
      needed_pathkeys = root->distinct_pathkeys;
5372
5373
0
    foreach(lc, input_rel->pathlist)
5374
0
    {
5375
0
      Path     *input_path = (Path *) lfirst(lc);
5376
0
      Path     *sorted_path;
5377
0
      List     *useful_pathkeys_list = NIL;
5378
5379
0
      useful_pathkeys_list =
5380
0
        get_useful_pathkeys_for_distinct(root,
5381
0
                         needed_pathkeys,
5382
0
                         input_path->pathkeys);
5383
0
      Assert(list_length(useful_pathkeys_list) > 0);
5384
5385
0
      foreach_node(List, useful_pathkeys, useful_pathkeys_list)
5386
0
      {
5387
0
        sorted_path = make_ordered_path(root,
5388
0
                        distinct_rel,
5389
0
                        input_path,
5390
0
                        cheapest_input_path,
5391
0
                        useful_pathkeys,
5392
0
                        limittuples);
5393
5394
0
        if (sorted_path == NULL)
5395
0
          continue;
5396
5397
        /*
5398
         * distinct_pathkeys may have become empty if all of the
5399
         * pathkeys were determined to be redundant.  If all of the
5400
         * pathkeys are redundant then each DISTINCT target must only
5401
         * allow a single value, therefore all resulting tuples must
5402
         * be identical (or at least indistinguishable by an equality
5403
         * check).  We can uniquify these tuples simply by just taking
5404
         * the first tuple.  All we do here is add a path to do "LIMIT
5405
         * 1" atop of 'sorted_path'.  When doing a DISTINCT ON we may
5406
         * still have a non-NIL sort_pathkeys list, so we must still
5407
         * only do this with paths which are correctly sorted by
5408
         * sort_pathkeys.
5409
         */
5410
0
        if (root->distinct_pathkeys == NIL)
5411
0
        {
5412
0
          Node     *limitCount;
5413
5414
0
          limitCount = (Node *) makeConst(INT8OID, -1, InvalidOid,
5415
0
                          sizeof(int64),
5416
0
                          Int64GetDatum(1), false,
5417
0
                          true);
5418
5419
          /*
5420
           * If the query already has a LIMIT clause, then we could
5421
           * end up with a duplicate LimitPath in the final plan.
5422
           * That does not seem worth troubling over too much.
5423
           */
5424
0
          add_path(distinct_rel, (Path *)
5425
0
               create_limit_path(root, distinct_rel, sorted_path,
5426
0
                         NULL, limitCount,
5427
0
                         LIMIT_OPTION_COUNT, 0, 1));
5428
0
        }
5429
0
        else
5430
0
        {
5431
0
          add_path(distinct_rel, (Path *)
5432
0
               create_unique_path(root, distinct_rel,
5433
0
                        sorted_path,
5434
0
                        list_length(root->distinct_pathkeys),
5435
0
                        numDistinctRows));
5436
0
        }
5437
0
      }
5438
0
    }
5439
0
  }
5440
5441
  /*
5442
   * Consider hash-based implementations of DISTINCT, if possible.
5443
   *
5444
   * If we were not able to make any other types of path, we *must* hash or
5445
   * die trying.  If we do have other choices, there are two things that
5446
   * should prevent selection of hashing: if the query uses DISTINCT ON
5447
   * (because it won't really have the expected behavior if we hash), or if
5448
   * enable_hashagg is off.
5449
   *
5450
   * Note: grouping_is_hashable() is much more expensive to check than the
5451
   * other gating conditions, so we want to do it last.
5452
   */
5453
0
  if (distinct_rel->pathlist == NIL)
5454
0
    allow_hash = true;   /* we have no alternatives */
5455
0
  else if (parse->hasDistinctOn || !enable_hashagg)
5456
0
    allow_hash = false;   /* policy-based decision not to hash */
5457
0
  else
5458
0
    allow_hash = true;   /* default */
5459
5460
0
  if (allow_hash && grouping_is_hashable(root->processed_distinctClause))
5461
0
  {
5462
    /* Generate hashed aggregate path --- no sort needed */
5463
0
    add_path(distinct_rel, (Path *)
5464
0
         create_agg_path(root,
5465
0
                 distinct_rel,
5466
0
                 cheapest_input_path,
5467
0
                 cheapest_input_path->pathtarget,
5468
0
                 AGG_HASHED,
5469
0
                 AGGSPLIT_SIMPLE,
5470
0
                 root->processed_distinctClause,
5471
0
                 NIL,
5472
0
                 NULL,
5473
0
                 numDistinctRows));
5474
0
  }
5475
5476
0
  return distinct_rel;
5477
0
}
5478
5479
/*
5480
 * get_useful_pathkeys_for_distinct
5481
 *    Get useful orderings of pathkeys for distinctClause by reordering
5482
 *    'needed_pathkeys' to match the given 'path_pathkeys' as much as possible.
5483
 *
5484
 * This returns a list of pathkeys that can be useful for DISTINCT or DISTINCT
5485
 * ON clause.  For convenience, it always includes the given 'needed_pathkeys'.
5486
 */
5487
static List *
5488
get_useful_pathkeys_for_distinct(PlannerInfo *root, List *needed_pathkeys,
5489
                 List *path_pathkeys)
5490
0
{
5491
0
  List     *useful_pathkeys_list = NIL;
5492
0
  List     *useful_pathkeys = NIL;
5493
5494
  /* always include the given 'needed_pathkeys' */
5495
0
  useful_pathkeys_list = lappend(useful_pathkeys_list,
5496
0
                   needed_pathkeys);
5497
5498
0
  if (!enable_distinct_reordering)
5499
0
    return useful_pathkeys_list;
5500
5501
  /*
5502
   * Scan the given 'path_pathkeys' and construct a list of PathKey nodes
5503
   * that match 'needed_pathkeys', but only up to the longest matching
5504
   * prefix.
5505
   *
5506
   * When we have DISTINCT ON, we must ensure that the resulting pathkey
5507
   * list matches initial distinctClause pathkeys; otherwise, it won't have
5508
   * the desired behavior.
5509
   */
5510
0
  foreach_node(PathKey, pathkey, path_pathkeys)
5511
0
  {
5512
    /*
5513
     * The PathKey nodes are canonical, so they can be checked for
5514
     * equality by simple pointer comparison.
5515
     */
5516
0
    if (!list_member_ptr(needed_pathkeys, pathkey))
5517
0
      break;
5518
0
    if (root->parse->hasDistinctOn &&
5519
0
      !list_member_ptr(root->distinct_pathkeys, pathkey))
5520
0
      break;
5521
5522
0
    useful_pathkeys = lappend(useful_pathkeys, pathkey);
5523
0
  }
5524
5525
  /* If no match at all, no point in reordering needed_pathkeys */
5526
0
  if (useful_pathkeys == NIL)
5527
0
    return useful_pathkeys_list;
5528
5529
  /*
5530
   * If not full match, the resulting pathkey list is not useful without
5531
   * incremental sort.
5532
   */
5533
0
  if (list_length(useful_pathkeys) < list_length(needed_pathkeys) &&
5534
0
    !enable_incremental_sort)
5535
0
    return useful_pathkeys_list;
5536
5537
  /* Append the remaining PathKey nodes in needed_pathkeys */
5538
0
  useful_pathkeys = list_concat_unique_ptr(useful_pathkeys,
5539
0
                       needed_pathkeys);
5540
5541
  /*
5542
   * If the resulting pathkey list is the same as the 'needed_pathkeys',
5543
   * just drop it.
5544
   */
5545
0
  if (compare_pathkeys(needed_pathkeys,
5546
0
             useful_pathkeys) == PATHKEYS_EQUAL)
5547
0
    return useful_pathkeys_list;
5548
5549
0
  useful_pathkeys_list = lappend(useful_pathkeys_list,
5550
0
                   useful_pathkeys);
5551
5552
0
  return useful_pathkeys_list;
5553
0
}
5554
5555
/*
5556
 * create_ordered_paths
5557
 *
5558
 * Build a new upperrel containing Paths for ORDER BY evaluation.
5559
 *
5560
 * All paths in the result must satisfy the ORDER BY ordering.
5561
 * The only new paths we need consider are an explicit full sort
5562
 * and incremental sort on the cheapest-total existing path.
5563
 *
5564
 * input_rel: contains the source-data Paths
5565
 * target: the output tlist the result Paths must emit
5566
 * limit_tuples: estimated bound on the number of output tuples,
5567
 *    or -1 if no LIMIT or couldn't estimate
5568
 *
5569
 * XXX This only looks at sort_pathkeys. I wonder if it needs to look at the
5570
 * other pathkeys (grouping, ...) like generate_useful_gather_paths.
5571
 */
5572
static RelOptInfo *
5573
create_ordered_paths(PlannerInfo *root,
5574
           RelOptInfo *input_rel,
5575
           PathTarget *target,
5576
           bool target_parallel_safe,
5577
           double limit_tuples)
5578
0
{
5579
0
  Path     *cheapest_input_path = input_rel->cheapest_total_path;
5580
0
  RelOptInfo *ordered_rel;
5581
0
  ListCell   *lc;
5582
5583
  /* For now, do all work in the (ORDERED, NULL) upperrel */
5584
0
  ordered_rel = fetch_upper_rel(root, UPPERREL_ORDERED, NULL);
5585
5586
  /*
5587
   * If the input relation is not parallel-safe, then the ordered relation
5588
   * can't be parallel-safe, either.  Otherwise, it's parallel-safe if the
5589
   * target list is parallel-safe.
5590
   */
5591
0
  if (input_rel->consider_parallel && target_parallel_safe)
5592
0
    ordered_rel->consider_parallel = true;
5593
5594
  /* Assume that the same path generation strategies are allowed. */
5595
0
  ordered_rel->pgs_mask = input_rel->pgs_mask;
5596
5597
  /*
5598
   * If the input rel belongs to a single FDW, so does the ordered_rel.
5599
   */
5600
0
  ordered_rel->serverid = input_rel->serverid;
5601
0
  ordered_rel->userid = input_rel->userid;
5602
0
  ordered_rel->useridiscurrent = input_rel->useridiscurrent;
5603
0
  ordered_rel->fdwroutine = input_rel->fdwroutine;
5604
5605
0
  foreach(lc, input_rel->pathlist)
5606
0
  {
5607
0
    Path     *input_path = (Path *) lfirst(lc);
5608
0
    Path     *sorted_path;
5609
0
    bool    is_sorted;
5610
0
    int     presorted_keys;
5611
5612
0
    is_sorted = pathkeys_count_contained_in(root->sort_pathkeys,
5613
0
                        input_path->pathkeys, &presorted_keys);
5614
5615
0
    if (is_sorted)
5616
0
      sorted_path = input_path;
5617
0
    else
5618
0
    {
5619
      /*
5620
       * Try at least sorting the cheapest path and also try
5621
       * incrementally sorting any path which is partially sorted
5622
       * already (no need to deal with paths which have presorted keys
5623
       * when incremental sort is disabled unless it's the cheapest
5624
       * input path).
5625
       */
5626
0
      if (input_path != cheapest_input_path &&
5627
0
        (presorted_keys == 0 || !enable_incremental_sort))
5628
0
        continue;
5629
5630
      /*
5631
       * We've no need to consider both a sort and incremental sort.
5632
       * We'll just do a sort if there are no presorted keys and an
5633
       * incremental sort when there are presorted keys.
5634
       */
5635
0
      if (presorted_keys == 0 || !enable_incremental_sort)
5636
0
        sorted_path = (Path *) create_sort_path(root,
5637
0
                            ordered_rel,
5638
0
                            input_path,
5639
0
                            root->sort_pathkeys,
5640
0
                            limit_tuples);
5641
0
      else
5642
0
        sorted_path = (Path *) create_incremental_sort_path(root,
5643
0
                                  ordered_rel,
5644
0
                                  input_path,
5645
0
                                  root->sort_pathkeys,
5646
0
                                  presorted_keys,
5647
0
                                  limit_tuples);
5648
0
    }
5649
5650
    /*
5651
     * If the pathtarget of the result path has different expressions from
5652
     * the target to be applied, a projection step is needed.
5653
     */
5654
0
    if (!equal(sorted_path->pathtarget->exprs, target->exprs))
5655
0
      sorted_path = apply_projection_to_path(root, ordered_rel,
5656
0
                           sorted_path, target);
5657
5658
0
    add_path(ordered_rel, sorted_path);
5659
0
  }
5660
5661
  /*
5662
   * generate_gather_paths() will have already generated a simple Gather
5663
   * path for the best parallel path, if any, and the loop above will have
5664
   * considered sorting it.  Similarly, generate_gather_paths() will also
5665
   * have generated order-preserving Gather Merge plans which can be used
5666
   * without sorting if they happen to match the sort_pathkeys, and the loop
5667
   * above will have handled those as well.  However, there's one more
5668
   * possibility: it may make sense to sort the cheapest partial path or
5669
   * incrementally sort any partial path that is partially sorted according
5670
   * to the required output order and then use Gather Merge.
5671
   */
5672
0
  if (ordered_rel->consider_parallel && root->sort_pathkeys != NIL &&
5673
0
    input_rel->partial_pathlist != NIL)
5674
0
  {
5675
0
    Path     *cheapest_partial_path;
5676
5677
0
    cheapest_partial_path = linitial(input_rel->partial_pathlist);
5678
5679
0
    foreach(lc, input_rel->partial_pathlist)
5680
0
    {
5681
0
      Path     *input_path = (Path *) lfirst(lc);
5682
0
      Path     *sorted_path;
5683
0
      bool    is_sorted;
5684
0
      int     presorted_keys;
5685
0
      double    total_groups;
5686
5687
0
      is_sorted = pathkeys_count_contained_in(root->sort_pathkeys,
5688
0
                          input_path->pathkeys,
5689
0
                          &presorted_keys);
5690
5691
0
      if (is_sorted)
5692
0
        continue;
5693
5694
      /*
5695
       * Try at least sorting the cheapest path and also try
5696
       * incrementally sorting any path which is partially sorted
5697
       * already (no need to deal with paths which have presorted keys
5698
       * when incremental sort is disabled unless it's the cheapest
5699
       * partial path).
5700
       */
5701
0
      if (input_path != cheapest_partial_path &&
5702
0
        (presorted_keys == 0 || !enable_incremental_sort))
5703
0
        continue;
5704
5705
      /*
5706
       * We've no need to consider both a sort and incremental sort.
5707
       * We'll just do a sort if there are no presorted keys and an
5708
       * incremental sort when there are presorted keys.
5709
       */
5710
0
      if (presorted_keys == 0 || !enable_incremental_sort)
5711
0
        sorted_path = (Path *) create_sort_path(root,
5712
0
                            ordered_rel,
5713
0
                            input_path,
5714
0
                            root->sort_pathkeys,
5715
0
                            limit_tuples);
5716
0
      else
5717
0
        sorted_path = (Path *) create_incremental_sort_path(root,
5718
0
                                  ordered_rel,
5719
0
                                  input_path,
5720
0
                                  root->sort_pathkeys,
5721
0
                                  presorted_keys,
5722
0
                                  limit_tuples);
5723
0
      total_groups = compute_gather_rows(sorted_path);
5724
0
      sorted_path = (Path *)
5725
0
        create_gather_merge_path(root, ordered_rel,
5726
0
                     sorted_path,
5727
0
                     sorted_path->pathtarget,
5728
0
                     root->sort_pathkeys, NULL,
5729
0
                     &total_groups);
5730
5731
      /*
5732
       * If the pathtarget of the result path has different expressions
5733
       * from the target to be applied, a projection step is needed.
5734
       */
5735
0
      if (!equal(sorted_path->pathtarget->exprs, target->exprs))
5736
0
        sorted_path = apply_projection_to_path(root, ordered_rel,
5737
0
                             sorted_path, target);
5738
5739
0
      add_path(ordered_rel, sorted_path);
5740
0
    }
5741
0
  }
5742
5743
  /*
5744
   * If there is an FDW that's responsible for all baserels of the query,
5745
   * let it consider adding ForeignPaths.
5746
   */
5747
0
  if (ordered_rel->fdwroutine &&
5748
0
    ordered_rel->fdwroutine->GetForeignUpperPaths)
5749
0
    ordered_rel->fdwroutine->GetForeignUpperPaths(root, UPPERREL_ORDERED,
5750
0
                            input_rel, ordered_rel,
5751
0
                            NULL);
5752
5753
  /* Let extensions possibly add some more paths */
5754
0
  if (create_upper_paths_hook)
5755
0
    (*create_upper_paths_hook) (root, UPPERREL_ORDERED,
5756
0
                  input_rel, ordered_rel, NULL);
5757
5758
  /*
5759
   * No need to bother with set_cheapest here; grouping_planner does not
5760
   * need us to do it.
5761
   */
5762
0
  Assert(ordered_rel->pathlist != NIL);
5763
5764
0
  return ordered_rel;
5765
0
}
5766
5767
5768
/*
5769
 * make_group_input_target
5770
 *    Generate appropriate PathTarget for initial input to grouping nodes.
5771
 *
5772
 * If there is grouping or aggregation, the scan/join subplan cannot emit
5773
 * the query's final targetlist; for example, it certainly can't emit any
5774
 * aggregate function calls.  This routine generates the correct target
5775
 * for the scan/join subplan.
5776
 *
5777
 * The query target list passed from the parser already contains entries
5778
 * for all ORDER BY and GROUP BY expressions, but it will not have entries
5779
 * for variables used only in HAVING clauses; so we need to add those
5780
 * variables to the subplan target list.  Also, we flatten all expressions
5781
 * except GROUP BY items into their component variables; other expressions
5782
 * will be computed by the upper plan nodes rather than by the subplan.
5783
 * For example, given a query like
5784
 *    SELECT a+b,SUM(c+d) FROM table GROUP BY a+b;
5785
 * we want to pass this targetlist to the subplan:
5786
 *    a+b,c,d
5787
 * where the a+b target will be used by the Sort/Group steps, and the
5788
 * other targets will be used for computing the final results.
5789
 *
5790
 * 'final_target' is the query's final target list (in PathTarget form)
5791
 *
5792
 * The result is the PathTarget to be computed by the Paths returned from
5793
 * query_planner().
5794
 */
5795
static PathTarget *
5796
make_group_input_target(PlannerInfo *root, PathTarget *final_target)
5797
0
{
5798
0
  Query    *parse = root->parse;
5799
0
  PathTarget *input_target;
5800
0
  List     *non_group_cols;
5801
0
  List     *non_group_vars;
5802
0
  int     i;
5803
0
  ListCell   *lc;
5804
5805
  /*
5806
   * We must build a target containing all grouping columns, plus any other
5807
   * Vars mentioned in the query's targetlist and HAVING qual.
5808
   */
5809
0
  input_target = create_empty_pathtarget();
5810
0
  non_group_cols = NIL;
5811
5812
0
  i = 0;
5813
0
  foreach(lc, final_target->exprs)
5814
0
  {
5815
0
    Expr     *expr = (Expr *) lfirst(lc);
5816
0
    Index   sgref = get_pathtarget_sortgroupref(final_target, i);
5817
5818
0
    if (sgref && root->processed_groupClause &&
5819
0
      get_sortgroupref_clause_noerr(sgref,
5820
0
                      root->processed_groupClause) != NULL)
5821
0
    {
5822
      /*
5823
       * It's a grouping column, so add it to the input target as-is.
5824
       *
5825
       * Note that the target is logically below the grouping step.  So
5826
       * with grouping sets we need to remove the RT index of the
5827
       * grouping step if there is any from the target expression.
5828
       */
5829
0
      if (parse->hasGroupRTE && parse->groupingSets != NIL)
5830
0
      {
5831
0
        Assert(root->group_rtindex > 0);
5832
0
        expr = (Expr *)
5833
0
          remove_nulling_relids((Node *) expr,
5834
0
                      bms_make_singleton(root->group_rtindex),
5835
0
                      NULL);
5836
0
      }
5837
0
      add_column_to_pathtarget(input_target, expr, sgref);
5838
0
    }
5839
0
    else
5840
0
    {
5841
      /*
5842
       * Non-grouping column, so just remember the expression for later
5843
       * call to pull_var_clause.
5844
       */
5845
0
      non_group_cols = lappend(non_group_cols, expr);
5846
0
    }
5847
5848
0
    i++;
5849
0
  }
5850
5851
  /*
5852
   * If there's a HAVING clause, we'll need the Vars it uses, too.
5853
   */
5854
0
  if (parse->havingQual)
5855
0
    non_group_cols = lappend(non_group_cols, parse->havingQual);
5856
5857
  /*
5858
   * Pull out all the Vars mentioned in non-group cols (plus HAVING), and
5859
   * add them to the input target if not already present.  (A Var used
5860
   * directly as a GROUP BY item will be present already.)  Note this
5861
   * includes Vars used in resjunk items, so we are covering the needs of
5862
   * ORDER BY and window specifications.  Vars used within Aggrefs and
5863
   * WindowFuncs will be pulled out here, too.
5864
   *
5865
   * Note that the target is logically below the grouping step.  So with
5866
   * grouping sets we need to remove the RT index of the grouping step if
5867
   * there is any from the non-group Vars.
5868
   */
5869
0
  non_group_vars = pull_var_clause((Node *) non_group_cols,
5870
0
                   PVC_RECURSE_AGGREGATES |
5871
0
                   PVC_RECURSE_WINDOWFUNCS |
5872
0
                   PVC_INCLUDE_PLACEHOLDERS);
5873
0
  if (parse->hasGroupRTE && parse->groupingSets != NIL)
5874
0
  {
5875
0
    Assert(root->group_rtindex > 0);
5876
0
    non_group_vars = (List *)
5877
0
      remove_nulling_relids((Node *) non_group_vars,
5878
0
                  bms_make_singleton(root->group_rtindex),
5879
0
                  NULL);
5880
0
  }
5881
0
  add_new_columns_to_pathtarget(input_target, non_group_vars);
5882
5883
  /* clean up cruft */
5884
0
  list_free(non_group_vars);
5885
0
  list_free(non_group_cols);
5886
5887
  /* XXX this causes some redundant cost calculation ... */
5888
0
  return set_pathtarget_cost_width(root, input_target);
5889
0
}
5890
5891
/*
5892
 * make_partial_grouping_target
5893
 *    Generate appropriate PathTarget for output of partial aggregate
5894
 *    (or partial grouping, if there are no aggregates) nodes.
5895
 *
5896
 * A partial aggregation node needs to emit all the same aggregates that
5897
 * a regular aggregation node would, plus any aggregates used in HAVING;
5898
 * except that the Aggref nodes should be marked as partial aggregates.
5899
 *
5900
 * In addition, we'd better emit any Vars and PlaceHolderVars that are
5901
 * used outside of Aggrefs in the aggregation tlist and HAVING.  (Presumably,
5902
 * these would be Vars that are grouped by or used in grouping expressions.)
5903
 *
5904
 * grouping_target is the tlist to be emitted by the topmost aggregation step.
5905
 * havingQual represents the HAVING clause.
5906
 */
5907
static PathTarget *
5908
make_partial_grouping_target(PlannerInfo *root,
5909
               PathTarget *grouping_target,
5910
               Node *havingQual)
5911
0
{
5912
0
  PathTarget *partial_target;
5913
0
  List     *non_group_cols;
5914
0
  List     *non_group_exprs;
5915
0
  int     i;
5916
0
  ListCell   *lc;
5917
5918
0
  partial_target = create_empty_pathtarget();
5919
0
  non_group_cols = NIL;
5920
5921
0
  i = 0;
5922
0
  foreach(lc, grouping_target->exprs)
5923
0
  {
5924
0
    Expr     *expr = (Expr *) lfirst(lc);
5925
0
    Index   sgref = get_pathtarget_sortgroupref(grouping_target, i);
5926
5927
0
    if (sgref && root->processed_groupClause &&
5928
0
      get_sortgroupref_clause_noerr(sgref,
5929
0
                      root->processed_groupClause) != NULL)
5930
0
    {
5931
      /*
5932
       * It's a grouping column, so add it to the partial_target as-is.
5933
       * (This allows the upper agg step to repeat the grouping calcs.)
5934
       */
5935
0
      add_column_to_pathtarget(partial_target, expr, sgref);
5936
0
    }
5937
0
    else
5938
0
    {
5939
      /*
5940
       * Non-grouping column, so just remember the expression for later
5941
       * call to pull_var_clause.
5942
       */
5943
0
      non_group_cols = lappend(non_group_cols, expr);
5944
0
    }
5945
5946
0
    i++;
5947
0
  }
5948
5949
  /*
5950
   * If there's a HAVING clause, we'll need the Vars/Aggrefs it uses, too.
5951
   */
5952
0
  if (havingQual)
5953
0
    non_group_cols = lappend(non_group_cols, havingQual);
5954
5955
  /*
5956
   * Pull out all the Vars, PlaceHolderVars, and Aggrefs mentioned in
5957
   * non-group cols (plus HAVING), and add them to the partial_target if not
5958
   * already present.  (An expression used directly as a GROUP BY item will
5959
   * be present already.)  Note this includes Vars used in resjunk items, so
5960
   * we are covering the needs of ORDER BY and window specifications.
5961
   */
5962
0
  non_group_exprs = pull_var_clause((Node *) non_group_cols,
5963
0
                    PVC_INCLUDE_AGGREGATES |
5964
0
                    PVC_RECURSE_WINDOWFUNCS |
5965
0
                    PVC_INCLUDE_PLACEHOLDERS);
5966
5967
0
  add_new_columns_to_pathtarget(partial_target, non_group_exprs);
5968
5969
  /*
5970
   * Adjust Aggrefs to put them in partial mode.  At this point all Aggrefs
5971
   * are at the top level of the target list, so we can just scan the list
5972
   * rather than recursing through the expression trees.
5973
   */
5974
0
  foreach(lc, partial_target->exprs)
5975
0
  {
5976
0
    Aggref     *aggref = (Aggref *) lfirst(lc);
5977
5978
0
    if (IsA(aggref, Aggref))
5979
0
    {
5980
0
      Aggref     *newaggref;
5981
5982
      /*
5983
       * We shouldn't need to copy the substructure of the Aggref node,
5984
       * but flat-copy the node itself to avoid damaging other trees.
5985
       */
5986
0
      newaggref = makeNode(Aggref);
5987
0
      memcpy(newaggref, aggref, sizeof(Aggref));
5988
5989
      /* For now, assume serialization is required */
5990
0
      mark_partial_aggref(newaggref, AGGSPLIT_INITIAL_SERIAL);
5991
5992
0
      lfirst(lc) = newaggref;
5993
0
    }
5994
0
  }
5995
5996
  /* clean up cruft */
5997
0
  list_free(non_group_exprs);
5998
0
  list_free(non_group_cols);
5999
6000
  /* XXX this causes some redundant cost calculation ... */
6001
0
  return set_pathtarget_cost_width(root, partial_target);
6002
0
}
6003
6004
/*
6005
 * mark_partial_aggref
6006
 *    Adjust an Aggref to make it represent a partial-aggregation step.
6007
 *
6008
 * The Aggref node is modified in-place; caller must do any copying required.
6009
 */
6010
void
6011
mark_partial_aggref(Aggref *agg, AggSplit aggsplit)
6012
0
{
6013
  /* aggtranstype should be computed by this point */
6014
0
  Assert(OidIsValid(agg->aggtranstype));
6015
  /* ... but aggsplit should still be as the parser left it */
6016
0
  Assert(agg->aggsplit == AGGSPLIT_SIMPLE);
6017
6018
  /* Mark the Aggref with the intended partial-aggregation mode */
6019
0
  agg->aggsplit = aggsplit;
6020
6021
  /*
6022
   * Adjust result type if needed.  Normally, a partial aggregate returns
6023
   * the aggregate's transition type; but if that's INTERNAL and we're
6024
   * serializing, it returns BYTEA instead.
6025
   */
6026
0
  if (DO_AGGSPLIT_SKIPFINAL(aggsplit))
6027
0
  {
6028
0
    if (agg->aggtranstype == INTERNALOID && DO_AGGSPLIT_SERIALIZE(aggsplit))
6029
0
      agg->aggtype = BYTEAOID;
6030
0
    else
6031
0
      agg->aggtype = agg->aggtranstype;
6032
0
  }
6033
0
}
6034
6035
/*
6036
 * postprocess_setop_tlist
6037
 *    Fix up targetlist returned by plan_set_operations().
6038
 *
6039
 * We need to transpose sort key info from the orig_tlist into new_tlist.
6040
 * NOTE: this would not be good enough if we supported resjunk sort keys
6041
 * for results of set operations --- then, we'd need to project a whole
6042
 * new tlist to evaluate the resjunk columns.  For now, just ereport if we
6043
 * find any resjunk columns in orig_tlist.
6044
 */
6045
static List *
6046
postprocess_setop_tlist(List *new_tlist, List *orig_tlist)
6047
0
{
6048
0
  ListCell   *l;
6049
0
  ListCell   *orig_tlist_item = list_head(orig_tlist);
6050
6051
0
  foreach(l, new_tlist)
6052
0
  {
6053
0
    TargetEntry *new_tle = lfirst_node(TargetEntry, l);
6054
0
    TargetEntry *orig_tle;
6055
6056
    /* ignore resjunk columns in setop result */
6057
0
    if (new_tle->resjunk)
6058
0
      continue;
6059
6060
0
    Assert(orig_tlist_item != NULL);
6061
0
    orig_tle = lfirst_node(TargetEntry, orig_tlist_item);
6062
0
    orig_tlist_item = lnext(orig_tlist, orig_tlist_item);
6063
0
    if (orig_tle->resjunk) /* should not happen */
6064
0
      elog(ERROR, "resjunk output columns are not implemented");
6065
0
    Assert(new_tle->resno == orig_tle->resno);
6066
0
    new_tle->ressortgroupref = orig_tle->ressortgroupref;
6067
0
  }
6068
0
  if (orig_tlist_item != NULL)
6069
0
    elog(ERROR, "resjunk output columns are not implemented");
6070
0
  return new_tlist;
6071
0
}
6072
6073
/*
6074
 * optimize_window_clauses
6075
 *    Call each WindowFunc's prosupport function to see if we're able to
6076
 *    make any adjustments to any of the WindowClause's so that the executor
6077
 *    can execute the window functions in a more optimal way.
6078
 *
6079
 * Currently we only allow adjustments to the WindowClause's frameOptions.  We
6080
 * may allow more things to be done here in the future.
6081
 */
6082
static void
6083
optimize_window_clauses(PlannerInfo *root, WindowFuncLists *wflists)
6084
0
{
6085
0
  List     *windowClause = root->parse->windowClause;
6086
0
  ListCell   *lc;
6087
6088
0
  foreach(lc, windowClause)
6089
0
  {
6090
0
    WindowClause *wc = lfirst_node(WindowClause, lc);
6091
0
    ListCell   *lc2;
6092
0
    int     optimizedFrameOptions = 0;
6093
6094
0
    Assert(wc->winref <= wflists->maxWinRef);
6095
6096
    /* skip any WindowClauses that have no WindowFuncs */
6097
0
    if (wflists->windowFuncs[wc->winref] == NIL)
6098
0
      continue;
6099
6100
0
    foreach(lc2, wflists->windowFuncs[wc->winref])
6101
0
    {
6102
0
      SupportRequestOptimizeWindowClause req;
6103
0
      SupportRequestOptimizeWindowClause *res;
6104
0
      WindowFunc *wfunc = lfirst_node(WindowFunc, lc2);
6105
0
      Oid     prosupport;
6106
6107
0
      prosupport = get_func_support(wfunc->winfnoid);
6108
6109
      /* Check if there's a support function for 'wfunc' */
6110
0
      if (!OidIsValid(prosupport))
6111
0
        break;     /* can't optimize this WindowClause */
6112
6113
0
      req.type = T_SupportRequestOptimizeWindowClause;
6114
0
      req.window_clause = wc;
6115
0
      req.window_func = wfunc;
6116
0
      req.frameOptions = wc->frameOptions;
6117
6118
      /* call the support function */
6119
0
      res = (SupportRequestOptimizeWindowClause *)
6120
0
        DatumGetPointer(OidFunctionCall1(prosupport,
6121
0
                         PointerGetDatum(&req)));
6122
6123
      /*
6124
       * Skip to next WindowClause if the support function does not
6125
       * support this request type.
6126
       */
6127
0
      if (res == NULL)
6128
0
        break;
6129
6130
      /*
6131
       * Save these frameOptions for the first WindowFunc for this
6132
       * WindowClause.
6133
       */
6134
0
      if (foreach_current_index(lc2) == 0)
6135
0
        optimizedFrameOptions = res->frameOptions;
6136
6137
      /*
6138
       * On subsequent WindowFuncs, if the frameOptions are not the same
6139
       * then we're unable to optimize the frameOptions for this
6140
       * WindowClause.
6141
       */
6142
0
      else if (optimizedFrameOptions != res->frameOptions)
6143
0
        break;     /* skip to the next WindowClause, if any */
6144
0
    }
6145
6146
    /* adjust the frameOptions if all WindowFunc's agree that it's ok */
6147
0
    if (lc2 == NULL && wc->frameOptions != optimizedFrameOptions)
6148
0
    {
6149
0
      ListCell   *lc3;
6150
6151
      /* apply the new frame options */
6152
0
      wc->frameOptions = optimizedFrameOptions;
6153
6154
      /*
6155
       * We now check to see if changing the frameOptions has caused
6156
       * this WindowClause to be a duplicate of some other WindowClause.
6157
       * This can only happen if we have multiple WindowClauses, so
6158
       * don't bother if there's only 1.
6159
       */
6160
0
      if (list_length(windowClause) == 1)
6161
0
        continue;
6162
6163
      /*
6164
       * Do the duplicate check and reuse the existing WindowClause if
6165
       * we find a duplicate.
6166
       */
6167
0
      foreach(lc3, windowClause)
6168
0
      {
6169
0
        WindowClause *existing_wc = lfirst_node(WindowClause, lc3);
6170
6171
        /* skip over the WindowClause we're currently editing */
6172
0
        if (existing_wc == wc)
6173
0
          continue;
6174
6175
        /*
6176
         * Perform the same duplicate check that is done in
6177
         * transformWindowFuncCall.
6178
         */
6179
0
        if (equal(wc->partitionClause, existing_wc->partitionClause) &&
6180
0
          equal(wc->orderClause, existing_wc->orderClause) &&
6181
0
          wc->frameOptions == existing_wc->frameOptions &&
6182
0
          equal(wc->startOffset, existing_wc->startOffset) &&
6183
0
          equal(wc->endOffset, existing_wc->endOffset))
6184
0
        {
6185
0
          ListCell   *lc4;
6186
6187
          /*
6188
           * Now move each WindowFunc in 'wc' into 'existing_wc'.
6189
           * This required adjusting each WindowFunc's winref and
6190
           * moving the WindowFuncs in 'wc' to the list of
6191
           * WindowFuncs in 'existing_wc'.
6192
           */
6193
0
          foreach(lc4, wflists->windowFuncs[wc->winref])
6194
0
          {
6195
0
            WindowFunc *wfunc = lfirst_node(WindowFunc, lc4);
6196
6197
0
            wfunc->winref = existing_wc->winref;
6198
0
          }
6199
6200
          /* move list items */
6201
0
          wflists->windowFuncs[existing_wc->winref] = list_concat(wflists->windowFuncs[existing_wc->winref],
6202
0
                                      wflists->windowFuncs[wc->winref]);
6203
0
          wflists->windowFuncs[wc->winref] = NIL;
6204
6205
          /*
6206
           * transformWindowFuncCall() should have made sure there
6207
           * are no other duplicates, so we needn't bother looking
6208
           * any further.
6209
           */
6210
0
          break;
6211
0
        }
6212
0
      }
6213
0
    }
6214
0
  }
6215
0
}
6216
6217
/*
6218
 * select_active_windows
6219
 *    Create a list of the "active" window clauses (ie, those referenced
6220
 *    by non-deleted WindowFuncs) in the order they are to be executed.
6221
 */
6222
static List *
6223
select_active_windows(PlannerInfo *root, WindowFuncLists *wflists)
6224
0
{
6225
0
  List     *windowClause = root->parse->windowClause;
6226
0
  List     *result = NIL;
6227
0
  ListCell   *lc;
6228
0
  int     nActive = 0;
6229
0
  WindowClauseSortData *actives = palloc_array(WindowClauseSortData,
6230
0
                         list_length(windowClause));
6231
6232
  /* First, construct an array of the active windows */
6233
0
  foreach(lc, windowClause)
6234
0
  {
6235
0
    WindowClause *wc = lfirst_node(WindowClause, lc);
6236
6237
    /* It's only active if wflists shows some related WindowFuncs */
6238
0
    Assert(wc->winref <= wflists->maxWinRef);
6239
0
    if (wflists->windowFuncs[wc->winref] == NIL)
6240
0
      continue;
6241
6242
0
    actives[nActive].wc = wc; /* original clause */
6243
6244
    /*
6245
     * For sorting, we want the list of partition keys followed by the
6246
     * list of sort keys. But pathkeys construction will remove duplicates
6247
     * between the two, so we can as well (even though we can't detect all
6248
     * of the duplicates, since some may come from ECs - that might mean
6249
     * we miss optimization chances here). We must, however, ensure that
6250
     * the order of entries is preserved with respect to the ones we do
6251
     * keep.
6252
     *
6253
     * partitionClause and orderClause had their own duplicates removed in
6254
     * parse analysis, so we're only concerned here with removing
6255
     * orderClause entries that also appear in partitionClause.
6256
     */
6257
0
    actives[nActive].uniqueOrder =
6258
0
      list_concat_unique(list_copy(wc->partitionClause),
6259
0
                 wc->orderClause);
6260
0
    nActive++;
6261
0
  }
6262
6263
  /*
6264
   * Sort active windows by their partitioning/ordering clauses, ignoring
6265
   * any framing clauses, so that the windows that need the same sorting are
6266
   * adjacent in the list. When we come to generate paths, this will avoid
6267
   * inserting additional Sort nodes.
6268
   *
6269
   * This is how we implement a specific requirement from the SQL standard,
6270
   * which says that when two or more windows are order-equivalent (i.e.
6271
   * have matching partition and order clauses, even if their names or
6272
   * framing clauses differ), then all peer rows must be presented in the
6273
   * same order in all of them. If we allowed multiple sort nodes for such
6274
   * cases, we'd risk having the peer rows end up in different orders in
6275
   * equivalent windows due to sort instability. (See General Rule 4 of
6276
   * <window clause> in SQL2008 - SQL2016.)
6277
   *
6278
   * Additionally, if the entire list of clauses of one window is a prefix
6279
   * of another, put first the window with stronger sorting requirements.
6280
   * This way we will first sort for stronger window, and won't have to sort
6281
   * again for the weaker one.
6282
   */
6283
0
  qsort(actives, nActive, sizeof(WindowClauseSortData), common_prefix_cmp);
6284
6285
  /* build ordered list of the original WindowClause nodes */
6286
0
  for (int i = 0; i < nActive; i++)
6287
0
    result = lappend(result, actives[i].wc);
6288
6289
0
  pfree(actives);
6290
6291
0
  return result;
6292
0
}
6293
6294
/*
6295
 * name_active_windows
6296
 *    Ensure all active windows have unique names.
6297
 *
6298
 * The parser will have checked that user-assigned window names are unique
6299
 * within the Query.  Here we assign made-up names to any unnamed
6300
 * WindowClauses for the benefit of EXPLAIN.  (We don't want to do this
6301
 * at parse time, because it'd mess up decompilation of views.)
6302
 *
6303
 * activeWindows: result of select_active_windows
6304
 */
6305
static void
6306
name_active_windows(List *activeWindows)
6307
0
{
6308
0
  int     next_n = 1;
6309
0
  char    newname[16];
6310
0
  ListCell   *lc;
6311
6312
0
  foreach(lc, activeWindows)
6313
0
  {
6314
0
    WindowClause *wc = lfirst_node(WindowClause, lc);
6315
6316
    /* Nothing to do if it has a name already. */
6317
0
    if (wc->name)
6318
0
      continue;
6319
6320
    /* Select a name not currently present in the list. */
6321
0
    for (;;)
6322
0
    {
6323
0
      ListCell   *lc2;
6324
6325
0
      snprintf(newname, sizeof(newname), "w%d", next_n++);
6326
0
      foreach(lc2, activeWindows)
6327
0
      {
6328
0
        WindowClause *wc2 = lfirst_node(WindowClause, lc2);
6329
6330
0
        if (wc2->name && strcmp(wc2->name, newname) == 0)
6331
0
          break;   /* matched */
6332
0
      }
6333
0
      if (lc2 == NULL)
6334
0
        break;     /* reached the end with no match */
6335
0
    }
6336
0
    wc->name = pstrdup(newname);
6337
0
  }
6338
0
}
6339
6340
/*
6341
 * common_prefix_cmp
6342
 *    QSort comparison function for WindowClauseSortData
6343
 *
6344
 * Sort the windows by the required sorting clauses. First, compare the sort
6345
 * clauses themselves. Second, if one window's clauses are a prefix of another
6346
 * one's clauses, put the window with more sort clauses first.
6347
 *
6348
 * We purposefully sort by the highest tleSortGroupRef first.  Since
6349
 * tleSortGroupRefs are assigned for the query's DISTINCT and ORDER BY first
6350
 * and because here we sort the lowest tleSortGroupRefs last, if a
6351
 * WindowClause is sharing a tleSortGroupRef with the query's DISTINCT or
6352
 * ORDER BY clause, this makes it more likely that the final WindowAgg will
6353
 * provide presorted input for the query's DISTINCT or ORDER BY clause, thus
6354
 * reducing the total number of sorts required for the query.
6355
 */
6356
static int
6357
common_prefix_cmp(const void *a, const void *b)
6358
0
{
6359
0
  const WindowClauseSortData *wcsa = a;
6360
0
  const WindowClauseSortData *wcsb = b;
6361
0
  ListCell   *item_a;
6362
0
  ListCell   *item_b;
6363
6364
0
  forboth(item_a, wcsa->uniqueOrder, item_b, wcsb->uniqueOrder)
6365
0
  {
6366
0
    SortGroupClause *sca = lfirst_node(SortGroupClause, item_a);
6367
0
    SortGroupClause *scb = lfirst_node(SortGroupClause, item_b);
6368
6369
0
    if (sca->tleSortGroupRef > scb->tleSortGroupRef)
6370
0
      return -1;
6371
0
    else if (sca->tleSortGroupRef < scb->tleSortGroupRef)
6372
0
      return 1;
6373
0
    else if (sca->sortop > scb->sortop)
6374
0
      return -1;
6375
0
    else if (sca->sortop < scb->sortop)
6376
0
      return 1;
6377
0
    else if (sca->nulls_first && !scb->nulls_first)
6378
0
      return -1;
6379
0
    else if (!sca->nulls_first && scb->nulls_first)
6380
0
      return 1;
6381
    /* no need to compare eqop, since it is fully determined by sortop */
6382
0
  }
6383
6384
0
  if (list_length(wcsa->uniqueOrder) > list_length(wcsb->uniqueOrder))
6385
0
    return -1;
6386
0
  else if (list_length(wcsa->uniqueOrder) < list_length(wcsb->uniqueOrder))
6387
0
    return 1;
6388
6389
0
  return 0;
6390
0
}
6391
6392
/*
6393
 * make_window_input_target
6394
 *    Generate appropriate PathTarget for initial input to WindowAgg nodes.
6395
 *
6396
 * When the query has window functions, this function computes the desired
6397
 * target to be computed by the node just below the first WindowAgg.
6398
 * This tlist must contain all values needed to evaluate the window functions,
6399
 * compute the final target list, and perform any required final sort step.
6400
 * If multiple WindowAggs are needed, each intermediate one adds its window
6401
 * function results onto this base tlist; only the topmost WindowAgg computes
6402
 * the actual desired target list.
6403
 *
6404
 * This function is much like make_group_input_target, though not quite enough
6405
 * like it to share code.  As in that function, we flatten most expressions
6406
 * into their component variables.  But we do not want to flatten window
6407
 * PARTITION BY/ORDER BY clauses, since that might result in multiple
6408
 * evaluations of them, which would be bad (possibly even resulting in
6409
 * inconsistent answers, if they contain volatile functions).
6410
 * Also, we must not flatten GROUP BY clauses that were left unflattened by
6411
 * make_group_input_target, because we may no longer have access to the
6412
 * individual Vars in them.
6413
 *
6414
 * Another key difference from make_group_input_target is that we don't
6415
 * flatten Aggref expressions, since those are to be computed below the
6416
 * window functions and just referenced like Vars above that.
6417
 *
6418
 * 'final_target' is the query's final target list (in PathTarget form)
6419
 * 'activeWindows' is the list of active windows previously identified by
6420
 *      select_active_windows.
6421
 *
6422
 * The result is the PathTarget to be computed by the plan node immediately
6423
 * below the first WindowAgg node.
6424
 */
6425
static PathTarget *
6426
make_window_input_target(PlannerInfo *root,
6427
             PathTarget *final_target,
6428
             List *activeWindows)
6429
0
{
6430
0
  PathTarget *input_target;
6431
0
  Bitmapset  *sgrefs;
6432
0
  List     *flattenable_cols;
6433
0
  List     *flattenable_vars;
6434
0
  int     i;
6435
0
  ListCell   *lc;
6436
6437
0
  Assert(root->parse->hasWindowFuncs);
6438
6439
  /*
6440
   * Collect the sortgroupref numbers of window PARTITION/ORDER BY clauses
6441
   * into a bitmapset for convenient reference below.
6442
   */
6443
0
  sgrefs = NULL;
6444
0
  foreach(lc, activeWindows)
6445
0
  {
6446
0
    WindowClause *wc = lfirst_node(WindowClause, lc);
6447
0
    ListCell   *lc2;
6448
6449
0
    foreach(lc2, wc->partitionClause)
6450
0
    {
6451
0
      SortGroupClause *sortcl = lfirst_node(SortGroupClause, lc2);
6452
6453
0
      sgrefs = bms_add_member(sgrefs, sortcl->tleSortGroupRef);
6454
0
    }
6455
0
    foreach(lc2, wc->orderClause)
6456
0
    {
6457
0
      SortGroupClause *sortcl = lfirst_node(SortGroupClause, lc2);
6458
6459
0
      sgrefs = bms_add_member(sgrefs, sortcl->tleSortGroupRef);
6460
0
    }
6461
0
  }
6462
6463
  /* Add in sortgroupref numbers of GROUP BY clauses, too */
6464
0
  foreach(lc, root->processed_groupClause)
6465
0
  {
6466
0
    SortGroupClause *grpcl = lfirst_node(SortGroupClause, lc);
6467
6468
0
    sgrefs = bms_add_member(sgrefs, grpcl->tleSortGroupRef);
6469
0
  }
6470
6471
  /*
6472
   * Construct a target containing all the non-flattenable targetlist items,
6473
   * and save aside the others for a moment.
6474
   */
6475
0
  input_target = create_empty_pathtarget();
6476
0
  flattenable_cols = NIL;
6477
6478
0
  i = 0;
6479
0
  foreach(lc, final_target->exprs)
6480
0
  {
6481
0
    Expr     *expr = (Expr *) lfirst(lc);
6482
0
    Index   sgref = get_pathtarget_sortgroupref(final_target, i);
6483
6484
    /*
6485
     * Don't want to deconstruct window clauses or GROUP BY items.  (Note
6486
     * that such items can't contain window functions, so it's okay to
6487
     * compute them below the WindowAgg nodes.)
6488
     */
6489
0
    if (sgref != 0 && bms_is_member(sgref, sgrefs))
6490
0
    {
6491
      /*
6492
       * Don't want to deconstruct this value, so add it to the input
6493
       * target as-is.
6494
       */
6495
0
      add_column_to_pathtarget(input_target, expr, sgref);
6496
0
    }
6497
0
    else
6498
0
    {
6499
      /*
6500
       * Column is to be flattened, so just remember the expression for
6501
       * later call to pull_var_clause.
6502
       */
6503
0
      flattenable_cols = lappend(flattenable_cols, expr);
6504
0
    }
6505
6506
0
    i++;
6507
0
  }
6508
6509
  /*
6510
   * Pull out all the Vars and Aggrefs mentioned in flattenable columns, and
6511
   * add them to the input target if not already present.  (Some might be
6512
   * there already because they're used directly as window/group clauses.)
6513
   *
6514
   * Note: it's essential to use PVC_INCLUDE_AGGREGATES here, so that any
6515
   * Aggrefs are placed in the Agg node's tlist and not left to be computed
6516
   * at higher levels.  On the other hand, we should recurse into
6517
   * WindowFuncs to make sure their input expressions are available.
6518
   */
6519
0
  flattenable_vars = pull_var_clause((Node *) flattenable_cols,
6520
0
                     PVC_INCLUDE_AGGREGATES |
6521
0
                     PVC_RECURSE_WINDOWFUNCS |
6522
0
                     PVC_INCLUDE_PLACEHOLDERS);
6523
0
  add_new_columns_to_pathtarget(input_target, flattenable_vars);
6524
6525
  /* clean up cruft */
6526
0
  list_free(flattenable_vars);
6527
0
  list_free(flattenable_cols);
6528
6529
  /* XXX this causes some redundant cost calculation ... */
6530
0
  return set_pathtarget_cost_width(root, input_target);
6531
0
}
6532
6533
/*
6534
 * make_pathkeys_for_window
6535
 *    Create a pathkeys list describing the required input ordering
6536
 *    for the given WindowClause.
6537
 *
6538
 * Modifies wc's partitionClause to remove any clauses which are deemed
6539
 * redundant by the pathkey logic.
6540
 *
6541
 * The required ordering is first the PARTITION keys, then the ORDER keys.
6542
 * In the future we might try to implement windowing using hashing, in which
6543
 * case the ordering could be relaxed, but for now we always sort.
6544
 */
6545
static List *
6546
make_pathkeys_for_window(PlannerInfo *root, WindowClause *wc,
6547
             List *tlist)
6548
0
{
6549
0
  List     *window_pathkeys = NIL;
6550
6551
  /* Throw error if can't sort */
6552
0
  if (!grouping_is_sortable(wc->partitionClause))
6553
0
    ereport(ERROR,
6554
0
        (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
6555
0
         errmsg("could not implement window PARTITION BY"),
6556
0
         errdetail("Window partitioning columns must be of sortable datatypes.")));
6557
0
  if (!grouping_is_sortable(wc->orderClause))
6558
0
    ereport(ERROR,
6559
0
        (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
6560
0
         errmsg("could not implement window ORDER BY"),
6561
0
         errdetail("Window ordering columns must be of sortable datatypes.")));
6562
6563
  /*
6564
   * First fetch the pathkeys for the PARTITION BY clause.  We can safely
6565
   * remove any clauses from the wc->partitionClause for redundant pathkeys.
6566
   */
6567
0
  if (wc->partitionClause != NIL)
6568
0
  {
6569
0
    bool    sortable;
6570
6571
0
    window_pathkeys = make_pathkeys_for_sortclauses_extended(root,
6572
0
                                 &wc->partitionClause,
6573
0
                                 tlist,
6574
0
                                 true,
6575
0
                                 false,
6576
0
                                 &sortable,
6577
0
                                 false);
6578
6579
0
    Assert(sortable);
6580
0
  }
6581
6582
  /*
6583
   * In principle, we could also consider removing redundant ORDER BY items
6584
   * too as doing so does not alter the result of peer row checks done by
6585
   * the executor.  However, we must *not* remove the ordering column for
6586
   * RANGE OFFSET cases, as the executor needs that for in_range tests even
6587
   * if it's known to be equal to some partitioning column.
6588
   */
6589
0
  if (wc->orderClause != NIL)
6590
0
  {
6591
0
    List     *orderby_pathkeys;
6592
6593
0
    orderby_pathkeys = make_pathkeys_for_sortclauses(root,
6594
0
                             wc->orderClause,
6595
0
                             tlist);
6596
6597
    /* Okay, make the combined pathkeys */
6598
0
    if (window_pathkeys != NIL)
6599
0
      window_pathkeys = append_pathkeys(window_pathkeys, orderby_pathkeys);
6600
0
    else
6601
0
      window_pathkeys = orderby_pathkeys;
6602
0
  }
6603
6604
0
  return window_pathkeys;
6605
0
}
6606
6607
/*
6608
 * make_sort_input_target
6609
 *    Generate appropriate PathTarget for initial input to Sort step.
6610
 *
6611
 * If the query has ORDER BY, this function chooses the target to be computed
6612
 * by the node just below the Sort (and DISTINCT, if any, since Unique can't
6613
 * project) steps.  This might or might not be identical to the query's final
6614
 * output target.
6615
 *
6616
 * The main argument for keeping the sort-input tlist the same as the final
6617
 * is that we avoid a separate projection node (which will be needed if
6618
 * they're different, because Sort can't project).  However, there are also
6619
 * advantages to postponing tlist evaluation till after the Sort: it ensures
6620
 * a consistent order of evaluation for any volatile functions in the tlist,
6621
 * and if there's also a LIMIT, we can stop the query without ever computing
6622
 * tlist functions for later rows, which is beneficial for both volatile and
6623
 * expensive functions.
6624
 *
6625
 * Our current policy is to postpone volatile expressions till after the sort
6626
 * unconditionally (assuming that that's possible, ie they are in plain tlist
6627
 * columns and not ORDER BY/GROUP BY/DISTINCT columns).  We also prefer to
6628
 * postpone set-returning expressions, because running them beforehand would
6629
 * bloat the sort dataset, and because it might cause unexpected output order
6630
 * if the sort isn't stable.  However there's a constraint on that: all SRFs
6631
 * in the tlist should be evaluated at the same plan step, so that they can
6632
 * run in sync in nodeProjectSet.  So if any SRFs are in sort columns, we
6633
 * mustn't postpone any SRFs.  (Note that in principle that policy should
6634
 * probably get applied to the group/window input targetlists too, but we
6635
 * have not done that historically.)  Lastly, expensive expressions are
6636
 * postponed if there is a LIMIT, or if root->tuple_fraction shows that
6637
 * partial evaluation of the query is possible (if neither is true, we expect
6638
 * to have to evaluate the expressions for every row anyway), or if there are
6639
 * any volatile or set-returning expressions (since once we've put in a
6640
 * projection at all, it won't cost any more to postpone more stuff).
6641
 *
6642
 * Another issue that could potentially be considered here is that
6643
 * evaluating tlist expressions could result in data that's either wider
6644
 * or narrower than the input Vars, thus changing the volume of data that
6645
 * has to go through the Sort.  However, we usually have only a very bad
6646
 * idea of the output width of any expression more complex than a Var,
6647
 * so for now it seems too risky to try to optimize on that basis.
6648
 *
6649
 * Note that if we do produce a modified sort-input target, and then the
6650
 * query ends up not using an explicit Sort, no particular harm is done:
6651
 * we'll initially use the modified target for the preceding path nodes,
6652
 * but then change them to the final target with apply_projection_to_path.
6653
 * Moreover, in such a case the guarantees about evaluation order of
6654
 * volatile functions still hold, since the rows are sorted already.
6655
 *
6656
 * This function has some things in common with make_group_input_target and
6657
 * make_window_input_target, though the detailed rules for what to do are
6658
 * different.  We never flatten/postpone any grouping or ordering columns;
6659
 * those are needed before the sort.  If we do flatten a particular
6660
 * expression, we leave Aggref and WindowFunc nodes alone, since those were
6661
 * computed earlier.
6662
 *
6663
 * 'final_target' is the query's final target list (in PathTarget form)
6664
 * 'have_postponed_srfs' is an output argument, see below
6665
 *
6666
 * The result is the PathTarget to be computed by the plan node immediately
6667
 * below the Sort step (and the Distinct step, if any).  This will be
6668
 * exactly final_target if we decide a projection step wouldn't be helpful.
6669
 *
6670
 * In addition, *have_postponed_srfs is set to true if we choose to postpone
6671
 * any set-returning functions to after the Sort.
6672
 */
6673
static PathTarget *
6674
make_sort_input_target(PlannerInfo *root,
6675
             PathTarget *final_target,
6676
             bool *have_postponed_srfs)
6677
0
{
6678
0
  Query    *parse = root->parse;
6679
0
  PathTarget *input_target;
6680
0
  int     ncols;
6681
0
  bool     *col_is_srf;
6682
0
  bool     *postpone_col;
6683
0
  bool    have_srf;
6684
0
  bool    have_volatile;
6685
0
  bool    have_expensive;
6686
0
  bool    have_srf_sortcols;
6687
0
  bool    postpone_srfs;
6688
0
  List     *postponable_cols;
6689
0
  List     *postponable_vars;
6690
0
  int     i;
6691
0
  ListCell   *lc;
6692
6693
  /* Shouldn't get here unless query has ORDER BY */
6694
0
  Assert(parse->sortClause);
6695
6696
0
  *have_postponed_srfs = false;  /* default result */
6697
6698
  /* Inspect tlist and collect per-column information */
6699
0
  ncols = list_length(final_target->exprs);
6700
0
  col_is_srf = (bool *) palloc0(ncols * sizeof(bool));
6701
0
  postpone_col = (bool *) palloc0(ncols * sizeof(bool));
6702
0
  have_srf = have_volatile = have_expensive = have_srf_sortcols = false;
6703
6704
0
  i = 0;
6705
0
  foreach(lc, final_target->exprs)
6706
0
  {
6707
0
    Expr     *expr = (Expr *) lfirst(lc);
6708
6709
    /*
6710
     * If the column has a sortgroupref, assume it has to be evaluated
6711
     * before sorting.  Generally such columns would be ORDER BY, GROUP
6712
     * BY, etc targets.  One exception is columns that were removed from
6713
     * GROUP BY by remove_useless_groupby_columns() ... but those would
6714
     * only be Vars anyway.  There don't seem to be any cases where it
6715
     * would be worth the trouble to double-check.
6716
     */
6717
0
    if (get_pathtarget_sortgroupref(final_target, i) == 0)
6718
0
    {
6719
      /*
6720
       * Check for SRF or volatile functions.  Check the SRF case first
6721
       * because we must know whether we have any postponed SRFs.
6722
       */
6723
0
      if (parse->hasTargetSRFs &&
6724
0
        expression_returns_set((Node *) expr))
6725
0
      {
6726
        /* We'll decide below whether these are postponable */
6727
0
        col_is_srf[i] = true;
6728
0
        have_srf = true;
6729
0
      }
6730
0
      else if (contain_volatile_functions((Node *) expr))
6731
0
      {
6732
        /* Unconditionally postpone */
6733
0
        postpone_col[i] = true;
6734
0
        have_volatile = true;
6735
0
      }
6736
0
      else
6737
0
      {
6738
        /*
6739
         * Else check the cost.  XXX it's annoying to have to do this
6740
         * when set_pathtarget_cost_width() just did it.  Refactor to
6741
         * allow sharing the work?
6742
         */
6743
0
        QualCost  cost;
6744
6745
0
        cost_qual_eval_node(&cost, (Node *) expr, root);
6746
6747
        /*
6748
         * We arbitrarily define "expensive" as "more than 10X
6749
         * cpu_operator_cost".  Note this will take in any PL function
6750
         * with default cost.
6751
         */
6752
0
        if (cost.per_tuple > 10 * cpu_operator_cost)
6753
0
        {
6754
0
          postpone_col[i] = true;
6755
0
          have_expensive = true;
6756
0
        }
6757
0
      }
6758
0
    }
6759
0
    else
6760
0
    {
6761
      /* For sortgroupref cols, just check if any contain SRFs */
6762
0
      if (!have_srf_sortcols &&
6763
0
        parse->hasTargetSRFs &&
6764
0
        expression_returns_set((Node *) expr))
6765
0
        have_srf_sortcols = true;
6766
0
    }
6767
6768
0
    i++;
6769
0
  }
6770
6771
  /*
6772
   * We can postpone SRFs if we have some but none are in sortgroupref cols.
6773
   */
6774
0
  postpone_srfs = (have_srf && !have_srf_sortcols);
6775
6776
  /*
6777
   * If we don't need a post-sort projection, just return final_target.
6778
   */
6779
0
  if (!(postpone_srfs || have_volatile ||
6780
0
      (have_expensive &&
6781
0
       (parse->limitCount || root->tuple_fraction > 0))))
6782
0
    return final_target;
6783
6784
  /*
6785
   * Report whether the post-sort projection will contain set-returning
6786
   * functions.  This is important because it affects whether the Sort can
6787
   * rely on the query's LIMIT (if any) to bound the number of rows it needs
6788
   * to return.
6789
   */
6790
0
  *have_postponed_srfs = postpone_srfs;
6791
6792
  /*
6793
   * Construct the sort-input target, taking all non-postponable columns and
6794
   * then adding Vars, PlaceHolderVars, Aggrefs, and WindowFuncs found in
6795
   * the postponable ones.
6796
   */
6797
0
  input_target = create_empty_pathtarget();
6798
0
  postponable_cols = NIL;
6799
6800
0
  i = 0;
6801
0
  foreach(lc, final_target->exprs)
6802
0
  {
6803
0
    Expr     *expr = (Expr *) lfirst(lc);
6804
6805
0
    if (postpone_col[i] || (postpone_srfs && col_is_srf[i]))
6806
0
      postponable_cols = lappend(postponable_cols, expr);
6807
0
    else
6808
0
      add_column_to_pathtarget(input_target, expr,
6809
0
                   get_pathtarget_sortgroupref(final_target, i));
6810
6811
0
    i++;
6812
0
  }
6813
6814
  /*
6815
   * Pull out all the Vars, Aggrefs, and WindowFuncs mentioned in
6816
   * postponable columns, and add them to the sort-input target if not
6817
   * already present.  (Some might be there already.)  We mustn't
6818
   * deconstruct Aggrefs or WindowFuncs here, since the projection node
6819
   * would be unable to recompute them.
6820
   */
6821
0
  postponable_vars = pull_var_clause((Node *) postponable_cols,
6822
0
                     PVC_INCLUDE_AGGREGATES |
6823
0
                     PVC_INCLUDE_WINDOWFUNCS |
6824
0
                     PVC_INCLUDE_PLACEHOLDERS);
6825
0
  add_new_columns_to_pathtarget(input_target, postponable_vars);
6826
6827
  /* clean up cruft */
6828
0
  list_free(postponable_vars);
6829
0
  list_free(postponable_cols);
6830
6831
  /* XXX this represents even more redundant cost calculation ... */
6832
0
  return set_pathtarget_cost_width(root, input_target);
6833
0
}
6834
6835
/*
6836
 * get_cheapest_fractional_path
6837
 *    Find the cheapest path for retrieving a specified fraction of all
6838
 *    the tuples expected to be returned by the given relation.
6839
 *
6840
 * Do not consider parameterized paths.  If the caller needs a path for upper
6841
 * rel, it can't have parameterized paths.  If the caller needs an append
6842
 * subpath, it could become limited by the treatment of similar
6843
 * parameterization of all the subpaths.
6844
 *
6845
 * We interpret tuple_fraction the same way as grouping_planner.
6846
 *
6847
 * We assume set_cheapest() has been run on the given rel.
6848
 */
6849
Path *
6850
get_cheapest_fractional_path(RelOptInfo *rel, double tuple_fraction)
6851
0
{
6852
0
  Path     *best_path = rel->cheapest_total_path;
6853
0
  ListCell   *l;
6854
6855
  /* If all tuples will be retrieved, just return the cheapest-total path */
6856
0
  if (tuple_fraction <= 0.0)
6857
0
    return best_path;
6858
6859
  /* Convert absolute # of tuples to a fraction; no need to clamp to 0..1 */
6860
0
  if (tuple_fraction >= 1.0 && best_path->rows > 0)
6861
0
    tuple_fraction /= best_path->rows;
6862
6863
0
  foreach(l, rel->pathlist)
6864
0
  {
6865
0
    Path     *path = (Path *) lfirst(l);
6866
6867
0
    if (path->param_info)
6868
0
      continue;
6869
6870
0
    if (path == rel->cheapest_total_path ||
6871
0
      compare_fractional_path_costs(best_path, path, tuple_fraction) <= 0)
6872
0
      continue;
6873
6874
0
    best_path = path;
6875
0
  }
6876
6877
0
  return best_path;
6878
0
}
6879
6880
/*
6881
 * adjust_paths_for_srfs
6882
 *    Fix up the Paths of the given upperrel to handle tSRFs properly.
6883
 *
6884
 * The executor can only handle set-returning functions that appear at the
6885
 * top level of the targetlist of a ProjectSet plan node.  If we have any SRFs
6886
 * that are not at top level, we need to split up the evaluation into multiple
6887
 * plan levels in which each level satisfies this constraint.  This function
6888
 * modifies each Path of an upperrel that (might) compute any SRFs in its
6889
 * output tlist to insert appropriate projection steps.
6890
 *
6891
 * The given targets and targets_contain_srfs lists are from
6892
 * split_pathtarget_at_srfs().  We assume the existing Paths emit the first
6893
 * target in targets.
6894
 */
6895
static void
6896
adjust_paths_for_srfs(PlannerInfo *root, RelOptInfo *rel,
6897
            List *targets, List *targets_contain_srfs)
6898
0
{
6899
0
  ListCell   *lc;
6900
6901
0
  Assert(list_length(targets) == list_length(targets_contain_srfs));
6902
0
  Assert(!linitial_int(targets_contain_srfs));
6903
6904
  /* If no SRFs appear at this plan level, nothing to do */
6905
0
  if (list_length(targets) == 1)
6906
0
    return;
6907
6908
  /*
6909
   * Stack SRF-evaluation nodes atop each path for the rel.
6910
   *
6911
   * In principle we should re-run set_cheapest() here to identify the
6912
   * cheapest path, but it seems unlikely that adding the same tlist eval
6913
   * costs to all the paths would change that, so we don't bother. Instead,
6914
   * just assume that the cheapest-startup and cheapest-total paths remain
6915
   * so.  (There should be no parameterized paths anymore, so we needn't
6916
   * worry about updating cheapest_parameterized_paths.)
6917
   */
6918
0
  foreach(lc, rel->pathlist)
6919
0
  {
6920
0
    Path     *subpath = (Path *) lfirst(lc);
6921
0
    Path     *newpath = subpath;
6922
0
    ListCell   *lc1,
6923
0
           *lc2;
6924
6925
0
    Assert(subpath->param_info == NULL);
6926
0
    forboth(lc1, targets, lc2, targets_contain_srfs)
6927
0
    {
6928
0
      PathTarget *thistarget = lfirst_node(PathTarget, lc1);
6929
0
      bool    contains_srfs = (bool) lfirst_int(lc2);
6930
6931
      /* If this level doesn't contain SRFs, do regular projection */
6932
0
      if (contains_srfs)
6933
0
        newpath = (Path *) create_set_projection_path(root,
6934
0
                                rel,
6935
0
                                newpath,
6936
0
                                thistarget);
6937
0
      else
6938
0
        newpath = (Path *) apply_projection_to_path(root,
6939
0
                              rel,
6940
0
                              newpath,
6941
0
                              thistarget);
6942
0
    }
6943
0
    lfirst(lc) = newpath;
6944
0
    if (subpath == rel->cheapest_startup_path)
6945
0
      rel->cheapest_startup_path = newpath;
6946
0
    if (subpath == rel->cheapest_total_path)
6947
0
      rel->cheapest_total_path = newpath;
6948
0
  }
6949
6950
  /* Likewise for partial paths, if any */
6951
0
  foreach(lc, rel->partial_pathlist)
6952
0
  {
6953
0
    Path     *subpath = (Path *) lfirst(lc);
6954
0
    Path     *newpath = subpath;
6955
0
    ListCell   *lc1,
6956
0
           *lc2;
6957
6958
0
    Assert(subpath->param_info == NULL);
6959
0
    forboth(lc1, targets, lc2, targets_contain_srfs)
6960
0
    {
6961
0
      PathTarget *thistarget = lfirst_node(PathTarget, lc1);
6962
0
      bool    contains_srfs = (bool) lfirst_int(lc2);
6963
6964
      /* If this level doesn't contain SRFs, do regular projection */
6965
0
      if (contains_srfs)
6966
0
        newpath = (Path *) create_set_projection_path(root,
6967
0
                                rel,
6968
0
                                newpath,
6969
0
                                thistarget);
6970
0
      else
6971
0
      {
6972
        /* avoid apply_projection_to_path, in case of multiple refs */
6973
0
        newpath = (Path *) create_projection_path(root,
6974
0
                              rel,
6975
0
                              newpath,
6976
0
                              thistarget);
6977
0
      }
6978
0
    }
6979
0
    lfirst(lc) = newpath;
6980
0
  }
6981
0
}
6982
6983
/*
6984
 * expression_planner
6985
 *    Perform planner's transformations on a standalone expression.
6986
 *
6987
 * Various utility commands need to evaluate expressions that are not part
6988
 * of a plannable query.  They can do so using the executor's regular
6989
 * expression-execution machinery, but first the expression has to be fed
6990
 * through here to transform it from parser output to something executable.
6991
 *
6992
 * Currently, we disallow sublinks in standalone expressions, so there's no
6993
 * real "planning" involved here.  (That might not always be true though.)
6994
 * What we must do is run eval_const_expressions to ensure that any function
6995
 * calls are converted to positional notation and function default arguments
6996
 * get inserted.  The fact that constant subexpressions get simplified is a
6997
 * side-effect that is useful when the expression will get evaluated more than
6998
 * once.  Also, we must fix operator function IDs.
6999
 *
7000
 * This does not return any information about dependencies of the expression.
7001
 * Hence callers should use the results only for the duration of the current
7002
 * query.  Callers that would like to cache the results for longer should use
7003
 * expression_planner_with_deps, probably via the plancache.
7004
 *
7005
 * Note: this must not make any damaging changes to the passed-in expression
7006
 * tree.  (It would actually be okay to apply fix_opfuncids to it, but since
7007
 * we first do an expression_tree_mutator-based walk, what is returned will
7008
 * be a new node tree.)  The result is constructed in the current memory
7009
 * context; beware that this can leak a lot of additional stuff there, too.
7010
 */
7011
Expr *
7012
expression_planner(Expr *expr)
7013
0
{
7014
0
  Node     *result;
7015
7016
  /*
7017
   * Convert named-argument function calls, insert default arguments and
7018
   * simplify constant subexprs
7019
   */
7020
0
  result = eval_const_expressions(NULL, (Node *) expr);
7021
7022
  /* Fill in opfuncid values if missing */
7023
0
  fix_opfuncids(result);
7024
7025
0
  return (Expr *) result;
7026
0
}
7027
7028
/*
7029
 * expression_planner_with_deps
7030
 *    Perform planner's transformations on a standalone expression,
7031
 *    returning expression dependency information along with the result.
7032
 *
7033
 * This is identical to expression_planner() except that it also returns
7034
 * information about possible dependencies of the expression, ie identities of
7035
 * objects whose definitions affect the result.  As in a PlannedStmt, these
7036
 * are expressed as a list of relation Oids and a list of PlanInvalItems.
7037
 */
7038
Expr *
7039
expression_planner_with_deps(Expr *expr,
7040
               List **relationOids,
7041
               List **invalItems)
7042
0
{
7043
0
  Node     *result;
7044
0
  PlannerGlobal glob;
7045
0
  PlannerInfo root;
7046
7047
  /* Make up dummy planner state so we can use setrefs machinery */
7048
0
  MemSet(&glob, 0, sizeof(glob));
7049
0
  glob.type = T_PlannerGlobal;
7050
0
  glob.relationOids = NIL;
7051
0
  glob.invalItems = NIL;
7052
7053
0
  MemSet(&root, 0, sizeof(root));
7054
0
  root.type = T_PlannerInfo;
7055
0
  root.glob = &glob;
7056
7057
  /*
7058
   * Convert named-argument function calls, insert default arguments and
7059
   * simplify constant subexprs.  Collect identities of inlined functions
7060
   * and elided domains, too.
7061
   */
7062
0
  result = eval_const_expressions(&root, (Node *) expr);
7063
7064
  /* Fill in opfuncid values if missing */
7065
0
  fix_opfuncids(result);
7066
7067
  /*
7068
   * Now walk the finished expression to find anything else we ought to
7069
   * record as an expression dependency.
7070
   */
7071
0
  (void) extract_query_dependencies_walker(result, &root);
7072
7073
0
  *relationOids = glob.relationOids;
7074
0
  *invalItems = glob.invalItems;
7075
7076
0
  return (Expr *) result;
7077
0
}
7078
7079
7080
/*
7081
 * plan_cluster_use_sort
7082
 *    Use the planner to decide how CLUSTER should implement sorting
7083
 *
7084
 * tableOid is the OID of a table to be clustered on its index indexOid
7085
 * (which is already known to be a btree index).  Decide whether it's
7086
 * cheaper to do an indexscan or a seqscan-plus-sort to execute the CLUSTER.
7087
 * Return true to use sorting, false to use an indexscan.
7088
 *
7089
 * Note: caller had better already hold some type of lock on the table.
7090
 */
7091
bool
7092
plan_cluster_use_sort(Oid tableOid, Oid indexOid)
7093
0
{
7094
0
  PlannerInfo *root;
7095
0
  Query    *query;
7096
0
  PlannerGlobal *glob;
7097
0
  RangeTblEntry *rte;
7098
0
  RelOptInfo *rel;
7099
0
  IndexOptInfo *indexInfo;
7100
0
  QualCost  indexExprCost;
7101
0
  Cost    comparisonCost;
7102
0
  Path     *seqScanPath;
7103
0
  Path    seqScanAndSortPath;
7104
0
  IndexPath  *indexScanPath;
7105
0
  ListCell   *lc;
7106
7107
  /* We can short-circuit the cost comparison if indexscans are disabled */
7108
0
  if (!enable_indexscan)
7109
0
    return true;     /* use sort */
7110
7111
  /* Set up mostly-dummy planner state */
7112
0
  query = makeNode(Query);
7113
0
  query->commandType = CMD_SELECT;
7114
7115
0
  glob = makeNode(PlannerGlobal);
7116
7117
0
  root = makeNode(PlannerInfo);
7118
0
  root->parse = query;
7119
0
  root->glob = glob;
7120
0
  root->query_level = 1;
7121
0
  root->planner_cxt = CurrentMemoryContext;
7122
0
  root->wt_param_id = -1;
7123
0
  root->join_domains = list_make1(makeNode(JoinDomain));
7124
7125
  /* Build a minimal RTE for the rel */
7126
0
  rte = makeNode(RangeTblEntry);
7127
0
  rte->rtekind = RTE_RELATION;
7128
0
  rte->relid = tableOid;
7129
0
  rte->relkind = RELKIND_RELATION; /* Don't be too picky. */
7130
0
  rte->rellockmode = AccessShareLock;
7131
0
  rte->lateral = false;
7132
0
  rte->inh = false;
7133
0
  rte->inFromCl = true;
7134
0
  query->rtable = list_make1(rte);
7135
0
  addRTEPermissionInfo(&query->rteperminfos, rte);
7136
7137
  /* Set up RTE/RelOptInfo arrays */
7138
0
  setup_simple_rel_arrays(root);
7139
7140
  /* Build RelOptInfo */
7141
0
  rel = build_simple_rel(root, 1, NULL);
7142
7143
  /* Locate IndexOptInfo for the target index */
7144
0
  indexInfo = NULL;
7145
0
  foreach(lc, rel->indexlist)
7146
0
  {
7147
0
    indexInfo = lfirst_node(IndexOptInfo, lc);
7148
0
    if (indexInfo->indexoid == indexOid)
7149
0
      break;
7150
0
  }
7151
7152
  /*
7153
   * It's possible that get_relation_info did not generate an IndexOptInfo
7154
   * for the desired index; this could happen if it's not yet reached its
7155
   * indcheckxmin usability horizon, or if it's a system index and we're
7156
   * ignoring system indexes.  In such cases we should tell CLUSTER to not
7157
   * trust the index contents but use seqscan-and-sort.
7158
   */
7159
0
  if (lc == NULL)       /* not in the list? */
7160
0
    return true;     /* use sort */
7161
7162
  /*
7163
   * Rather than doing all the pushups that would be needed to use
7164
   * set_baserel_size_estimates, just do a quick hack for rows and width.
7165
   */
7166
0
  rel->rows = rel->tuples;
7167
0
  rel->reltarget->width = get_relation_data_width(tableOid, NULL);
7168
7169
0
  root->total_table_pages = rel->pages;
7170
7171
  /*
7172
   * Determine eval cost of the index expressions, if any.  We need to
7173
   * charge twice that amount for each tuple comparison that happens during
7174
   * the sort, since tuplesort.c will have to re-evaluate the index
7175
   * expressions each time.  (XXX that's pretty inefficient...)
7176
   */
7177
0
  cost_qual_eval(&indexExprCost, indexInfo->indexprs, root);
7178
0
  comparisonCost = 2.0 * (indexExprCost.startup + indexExprCost.per_tuple);
7179
7180
  /* Estimate the cost of seq scan + sort */
7181
0
  seqScanPath = create_seqscan_path(root, rel, NULL, 0);
7182
0
  cost_sort(&seqScanAndSortPath, root, NIL,
7183
0
        seqScanPath->disabled_nodes,
7184
0
        seqScanPath->total_cost, rel->tuples, rel->reltarget->width,
7185
0
        comparisonCost, maintenance_work_mem, -1.0);
7186
7187
  /* Estimate the cost of index scan */
7188
0
  indexScanPath = create_index_path(root, indexInfo,
7189
0
                    NIL, NIL, NIL, NIL,
7190
0
                    ForwardScanDirection, false,
7191
0
                    NULL, 1.0, false);
7192
7193
0
  return (seqScanAndSortPath.total_cost < indexScanPath->path.total_cost);
7194
0
}
7195
7196
/*
7197
 * plan_create_index_workers
7198
 *    Use the planner to decide how many parallel worker processes
7199
 *    CREATE INDEX should request for use
7200
 *
7201
 * tableOid is the table on which the index is to be built.  indexOid is the
7202
 * OID of an index to be created or reindexed (which must be an index with
7203
 * support for parallel builds - currently btree, GIN, or BRIN).
7204
 *
7205
 * Return value is the number of parallel worker processes to request.  It
7206
 * may be unsafe to proceed if this is 0.  Note that this does not include the
7207
 * leader participating as a worker (value is always a number of parallel
7208
 * worker processes).
7209
 *
7210
 * Note: caller had better already hold some type of lock on the table and
7211
 * index.
7212
 */
7213
int
7214
plan_create_index_workers(Oid tableOid, Oid indexOid)
7215
0
{
7216
0
  PlannerInfo *root;
7217
0
  Query    *query;
7218
0
  PlannerGlobal *glob;
7219
0
  RangeTblEntry *rte;
7220
0
  Relation  heap;
7221
0
  Relation  index;
7222
0
  RelOptInfo *rel;
7223
0
  int     parallel_workers;
7224
0
  BlockNumber heap_blocks;
7225
0
  double    reltuples;
7226
0
  double    allvisfrac;
7227
7228
  /*
7229
   * We don't allow performing parallel operation in standalone backend or
7230
   * when parallelism is disabled.
7231
   */
7232
0
  if (!IsUnderPostmaster || max_parallel_maintenance_workers == 0)
7233
0
    return 0;
7234
7235
  /* Set up largely-dummy planner state */
7236
0
  query = makeNode(Query);
7237
0
  query->commandType = CMD_SELECT;
7238
7239
0
  glob = makeNode(PlannerGlobal);
7240
7241
0
  root = makeNode(PlannerInfo);
7242
0
  root->parse = query;
7243
0
  root->glob = glob;
7244
0
  root->query_level = 1;
7245
0
  root->planner_cxt = CurrentMemoryContext;
7246
0
  root->wt_param_id = -1;
7247
0
  root->join_domains = list_make1(makeNode(JoinDomain));
7248
7249
  /*
7250
   * Build a minimal RTE.
7251
   *
7252
   * Mark the RTE with inh = true.  This is a kludge to prevent
7253
   * get_relation_info() from fetching index info, which is necessary
7254
   * because it does not expect that any IndexOptInfo is currently
7255
   * undergoing REINDEX.
7256
   */
7257
0
  rte = makeNode(RangeTblEntry);
7258
0
  rte->rtekind = RTE_RELATION;
7259
0
  rte->relid = tableOid;
7260
0
  rte->relkind = RELKIND_RELATION; /* Don't be too picky. */
7261
0
  rte->rellockmode = AccessShareLock;
7262
0
  rte->lateral = false;
7263
0
  rte->inh = true;
7264
0
  rte->inFromCl = true;
7265
0
  query->rtable = list_make1(rte);
7266
0
  addRTEPermissionInfo(&query->rteperminfos, rte);
7267
7268
  /* Set up RTE/RelOptInfo arrays */
7269
0
  setup_simple_rel_arrays(root);
7270
7271
  /* Build RelOptInfo */
7272
0
  rel = build_simple_rel(root, 1, NULL);
7273
7274
  /* Rels are assumed already locked by the caller */
7275
0
  heap = table_open(tableOid, NoLock);
7276
0
  index = index_open(indexOid, NoLock);
7277
7278
  /*
7279
   * Determine if it's safe to proceed.
7280
   *
7281
   * Currently, parallel workers can't access the leader's temporary tables.
7282
   * Furthermore, any index predicate or index expressions must be parallel
7283
   * safe.
7284
   */
7285
0
  if (heap->rd_rel->relpersistence == RELPERSISTENCE_TEMP ||
7286
0
    !is_parallel_safe(root, (Node *) RelationGetIndexExpressions(index)) ||
7287
0
    !is_parallel_safe(root, (Node *) RelationGetIndexPredicate(index)))
7288
0
  {
7289
0
    parallel_workers = 0;
7290
0
    goto done;
7291
0
  }
7292
7293
  /*
7294
   * If parallel_workers storage parameter is set for the table, accept that
7295
   * as the number of parallel worker processes to launch (though still cap
7296
   * at max_parallel_maintenance_workers).  Note that we deliberately do not
7297
   * consider any other factor when parallel_workers is set. (e.g., memory
7298
   * use by workers.)
7299
   */
7300
0
  if (rel->rel_parallel_workers != -1)
7301
0
  {
7302
0
    parallel_workers = Min(rel->rel_parallel_workers,
7303
0
                 max_parallel_maintenance_workers);
7304
0
    goto done;
7305
0
  }
7306
7307
  /*
7308
   * Estimate heap relation size ourselves, since rel->pages cannot be
7309
   * trusted (heap RTE was marked as inheritance parent)
7310
   */
7311
0
  estimate_rel_size(heap, NULL, &heap_blocks, &reltuples, &allvisfrac);
7312
7313
  /*
7314
   * Determine number of workers to scan the heap relation using generic
7315
   * model
7316
   */
7317
0
  parallel_workers = compute_parallel_worker(rel, heap_blocks, -1,
7318
0
                         max_parallel_maintenance_workers);
7319
7320
  /*
7321
   * Cap workers based on available maintenance_work_mem as needed.
7322
   *
7323
   * Note that each tuplesort participant receives an even share of the
7324
   * total maintenance_work_mem budget.  Aim to leave participants
7325
   * (including the leader as a participant) with no less than 32MB of
7326
   * memory.  This leaves cases where maintenance_work_mem is set to 64MB
7327
   * immediately past the threshold of being capable of launching a single
7328
   * parallel worker to sort.
7329
   */
7330
0
  while (parallel_workers > 0 &&
7331
0
       maintenance_work_mem / (parallel_workers + 1) < 32 * 1024)
7332
0
    parallel_workers--;
7333
7334
0
done:
7335
0
  index_close(index, NoLock);
7336
0
  table_close(heap, NoLock);
7337
7338
0
  return parallel_workers;
7339
0
}
7340
7341
/*
7342
 * add_paths_to_grouping_rel
7343
 *
7344
 * Add non-partial paths to grouping relation.
7345
 */
7346
static void
7347
add_paths_to_grouping_rel(PlannerInfo *root, RelOptInfo *input_rel,
7348
              RelOptInfo *grouped_rel,
7349
              RelOptInfo *partially_grouped_rel,
7350
              const AggClauseCosts *agg_costs,
7351
              grouping_sets_data *gd,
7352
              GroupPathExtraData *extra)
7353
0
{
7354
0
  Query    *parse = root->parse;
7355
0
  Path     *cheapest_path = input_rel->cheapest_total_path;
7356
0
  Path     *cheapest_partially_grouped_path = NULL;
7357
0
  ListCell   *lc;
7358
0
  bool    can_hash = (extra->flags & GROUPING_CAN_USE_HASH) != 0;
7359
0
  bool    can_sort = (extra->flags & GROUPING_CAN_USE_SORT) != 0;
7360
0
  List     *havingQual = (List *) extra->havingQual;
7361
0
  AggClauseCosts *agg_final_costs = &extra->agg_final_costs;
7362
0
  double    dNumGroups = 0;
7363
0
  double    dNumFinalGroups = 0;
7364
7365
  /*
7366
   * Estimate number of groups for non-split aggregation.
7367
   */
7368
0
  dNumGroups = get_number_of_groups(root,
7369
0
                    cheapest_path->rows,
7370
0
                    gd,
7371
0
                    extra->targetList);
7372
7373
0
  if (partially_grouped_rel && partially_grouped_rel->pathlist)
7374
0
  {
7375
0
    cheapest_partially_grouped_path =
7376
0
      partially_grouped_rel->cheapest_total_path;
7377
7378
    /*
7379
     * Estimate number of groups for final phase of partial aggregation.
7380
     */
7381
0
    dNumFinalGroups =
7382
0
      get_number_of_groups(root,
7383
0
                 cheapest_partially_grouped_path->rows,
7384
0
                 gd,
7385
0
                 extra->targetList);
7386
0
  }
7387
7388
0
  if (can_sort)
7389
0
  {
7390
    /*
7391
     * Use any available suitably-sorted path as input, and also consider
7392
     * sorting the cheapest-total path and incremental sort on any paths
7393
     * with presorted keys.
7394
     */
7395
0
    foreach(lc, input_rel->pathlist)
7396
0
    {
7397
0
      ListCell   *lc2;
7398
0
      Path     *path = (Path *) lfirst(lc);
7399
0
      Path     *path_save = path;
7400
0
      List     *pathkey_orderings = NIL;
7401
7402
      /* generate alternative group orderings that might be useful */
7403
0
      pathkey_orderings = get_useful_group_keys_orderings(root, path);
7404
7405
0
      Assert(list_length(pathkey_orderings) > 0);
7406
7407
0
      foreach(lc2, pathkey_orderings)
7408
0
      {
7409
0
        GroupByOrdering *info = (GroupByOrdering *) lfirst(lc2);
7410
7411
        /* restore the path (we replace it in the loop) */
7412
0
        path = path_save;
7413
7414
0
        path = make_ordered_path(root,
7415
0
                     grouped_rel,
7416
0
                     path,
7417
0
                     cheapest_path,
7418
0
                     info->pathkeys,
7419
0
                     -1.0);
7420
0
        if (path == NULL)
7421
0
          continue;
7422
7423
        /* Now decide what to stick atop it */
7424
0
        if (parse->groupingSets)
7425
0
        {
7426
0
          consider_groupingsets_paths(root, grouped_rel,
7427
0
                        path, true, can_hash,
7428
0
                        gd, agg_costs, dNumGroups);
7429
0
        }
7430
0
        else if (parse->hasAggs)
7431
0
        {
7432
          /*
7433
           * We have aggregation, possibly with plain GROUP BY. Make
7434
           * an AggPath.
7435
           */
7436
0
          add_path(grouped_rel, (Path *)
7437
0
               create_agg_path(root,
7438
0
                       grouped_rel,
7439
0
                       path,
7440
0
                       grouped_rel->reltarget,
7441
0
                       parse->groupClause ? AGG_SORTED : AGG_PLAIN,
7442
0
                       AGGSPLIT_SIMPLE,
7443
0
                       info->clauses,
7444
0
                       havingQual,
7445
0
                       agg_costs,
7446
0
                       dNumGroups));
7447
0
        }
7448
0
        else if (parse->groupClause)
7449
0
        {
7450
          /*
7451
           * We have GROUP BY without aggregation or grouping sets.
7452
           * Make a GroupPath.
7453
           */
7454
0
          add_path(grouped_rel, (Path *)
7455
0
               create_group_path(root,
7456
0
                         grouped_rel,
7457
0
                         path,
7458
0
                         info->clauses,
7459
0
                         havingQual,
7460
0
                         dNumGroups));
7461
0
        }
7462
0
        else
7463
0
        {
7464
          /* Other cases should have been handled above */
7465
0
          Assert(false);
7466
0
        }
7467
0
      }
7468
0
    }
7469
7470
    /*
7471
     * Instead of operating directly on the input relation, we can
7472
     * consider finalizing a partially aggregated path.
7473
     */
7474
0
    if (partially_grouped_rel != NULL)
7475
0
    {
7476
0
      foreach(lc, partially_grouped_rel->pathlist)
7477
0
      {
7478
0
        ListCell   *lc2;
7479
0
        Path     *path = (Path *) lfirst(lc);
7480
0
        Path     *path_save = path;
7481
0
        List     *pathkey_orderings = NIL;
7482
7483
        /* generate alternative group orderings that might be useful */
7484
0
        pathkey_orderings = get_useful_group_keys_orderings(root, path);
7485
7486
0
        Assert(list_length(pathkey_orderings) > 0);
7487
7488
        /* process all potentially interesting grouping reorderings */
7489
0
        foreach(lc2, pathkey_orderings)
7490
0
        {
7491
0
          GroupByOrdering *info = (GroupByOrdering *) lfirst(lc2);
7492
7493
          /* restore the path (we replace it in the loop) */
7494
0
          path = path_save;
7495
7496
0
          path = make_ordered_path(root,
7497
0
                       grouped_rel,
7498
0
                       path,
7499
0
                       cheapest_partially_grouped_path,
7500
0
                       info->pathkeys,
7501
0
                       -1.0);
7502
7503
0
          if (path == NULL)
7504
0
            continue;
7505
7506
0
          if (parse->hasAggs)
7507
0
            add_path(grouped_rel, (Path *)
7508
0
                 create_agg_path(root,
7509
0
                         grouped_rel,
7510
0
                         path,
7511
0
                         grouped_rel->reltarget,
7512
0
                         parse->groupClause ? AGG_SORTED : AGG_PLAIN,
7513
0
                         AGGSPLIT_FINAL_DESERIAL,
7514
0
                         info->clauses,
7515
0
                         havingQual,
7516
0
                         agg_final_costs,
7517
0
                         dNumFinalGroups));
7518
0
          else
7519
0
            add_path(grouped_rel, (Path *)
7520
0
                 create_group_path(root,
7521
0
                           grouped_rel,
7522
0
                           path,
7523
0
                           info->clauses,
7524
0
                           havingQual,
7525
0
                           dNumFinalGroups));
7526
7527
0
        }
7528
0
      }
7529
0
    }
7530
0
  }
7531
7532
0
  if (can_hash)
7533
0
  {
7534
0
    if (parse->groupingSets)
7535
0
    {
7536
      /*
7537
       * Try for a hash-only groupingsets path over unsorted input.
7538
       */
7539
0
      consider_groupingsets_paths(root, grouped_rel,
7540
0
                    cheapest_path, false, true,
7541
0
                    gd, agg_costs, dNumGroups);
7542
0
    }
7543
0
    else
7544
0
    {
7545
      /*
7546
       * Generate a HashAgg Path.  We just need an Agg over the
7547
       * cheapest-total input path, since input order won't matter.
7548
       */
7549
0
      add_path(grouped_rel, (Path *)
7550
0
           create_agg_path(root, grouped_rel,
7551
0
                   cheapest_path,
7552
0
                   grouped_rel->reltarget,
7553
0
                   AGG_HASHED,
7554
0
                   AGGSPLIT_SIMPLE,
7555
0
                   root->processed_groupClause,
7556
0
                   havingQual,
7557
0
                   agg_costs,
7558
0
                   dNumGroups));
7559
0
    }
7560
7561
    /*
7562
     * Generate a Finalize HashAgg Path atop of the cheapest partially
7563
     * grouped path, assuming there is one
7564
     */
7565
0
    if (partially_grouped_rel && partially_grouped_rel->pathlist)
7566
0
    {
7567
0
      add_path(grouped_rel, (Path *)
7568
0
           create_agg_path(root,
7569
0
                   grouped_rel,
7570
0
                   cheapest_partially_grouped_path,
7571
0
                   grouped_rel->reltarget,
7572
0
                   AGG_HASHED,
7573
0
                   AGGSPLIT_FINAL_DESERIAL,
7574
0
                   root->processed_groupClause,
7575
0
                   havingQual,
7576
0
                   agg_final_costs,
7577
0
                   dNumFinalGroups));
7578
0
    }
7579
0
  }
7580
7581
  /*
7582
   * When partitionwise aggregate is used, we might have fully aggregated
7583
   * paths in the partial pathlist, because add_paths_to_append_rel() will
7584
   * consider a path for grouped_rel consisting of a Parallel Append of
7585
   * non-partial paths from each child.
7586
   */
7587
0
  if (grouped_rel->partial_pathlist != NIL)
7588
0
    gather_grouping_paths(root, grouped_rel);
7589
0
}
7590
7591
/*
7592
 * create_partial_grouping_paths
7593
 *
7594
 * Create a new upper relation representing the result of partial aggregation
7595
 * and populate it with appropriate paths.  Note that we don't finalize the
7596
 * lists of paths here, so the caller can add additional partial or non-partial
7597
 * paths and must afterward call gather_grouping_paths and set_cheapest on
7598
 * the returned upper relation.
7599
 *
7600
 * All paths for this new upper relation -- both partial and non-partial --
7601
 * have been partially aggregated but require a subsequent FinalizeAggregate
7602
 * step.
7603
 *
7604
 * NB: This function is allowed to return NULL if it determines that there is
7605
 * no real need to create a new RelOptInfo.
7606
 */
7607
static RelOptInfo *
7608
create_partial_grouping_paths(PlannerInfo *root,
7609
                RelOptInfo *grouped_rel,
7610
                RelOptInfo *input_rel,
7611
                grouping_sets_data *gd,
7612
                GroupPathExtraData *extra,
7613
                bool force_rel_creation)
7614
0
{
7615
0
  Query    *parse = root->parse;
7616
0
  RelOptInfo *partially_grouped_rel;
7617
0
  RelOptInfo *eager_agg_rel = NULL;
7618
0
  AggClauseCosts *agg_partial_costs = &extra->agg_partial_costs;
7619
0
  AggClauseCosts *agg_final_costs = &extra->agg_final_costs;
7620
0
  Path     *cheapest_partial_path = NULL;
7621
0
  Path     *cheapest_total_path = NULL;
7622
0
  double    dNumPartialGroups = 0;
7623
0
  double    dNumPartialPartialGroups = 0;
7624
0
  ListCell   *lc;
7625
0
  bool    can_hash = (extra->flags & GROUPING_CAN_USE_HASH) != 0;
7626
0
  bool    can_sort = (extra->flags & GROUPING_CAN_USE_SORT) != 0;
7627
7628
  /*
7629
   * Check whether any partially aggregated paths have been generated
7630
   * through eager aggregation.
7631
   */
7632
0
  if (input_rel->grouped_rel &&
7633
0
    !IS_DUMMY_REL(input_rel->grouped_rel) &&
7634
0
    input_rel->grouped_rel->pathlist != NIL)
7635
0
    eager_agg_rel = input_rel->grouped_rel;
7636
7637
  /*
7638
   * Consider whether we should generate partially aggregated non-partial
7639
   * paths.  We can only do this if we have a non-partial path, and only if
7640
   * the parent of the input rel is performing partial partitionwise
7641
   * aggregation.  (Note that extra->patype is the type of partitionwise
7642
   * aggregation being used at the parent level, not this level.)
7643
   */
7644
0
  if (input_rel->pathlist != NIL &&
7645
0
    extra->patype == PARTITIONWISE_AGGREGATE_PARTIAL)
7646
0
    cheapest_total_path = input_rel->cheapest_total_path;
7647
7648
  /*
7649
   * If parallelism is possible for grouped_rel, then we should consider
7650
   * generating partially-grouped partial paths.  However, if the input rel
7651
   * has no partial paths, then we can't.
7652
   */
7653
0
  if (grouped_rel->consider_parallel && input_rel->partial_pathlist != NIL)
7654
0
    cheapest_partial_path = linitial(input_rel->partial_pathlist);
7655
7656
  /*
7657
   * If we can't partially aggregate partial paths, and we can't partially
7658
   * aggregate non-partial paths, and no partially aggregated paths were
7659
   * generated by eager aggregation, then don't bother creating the new
7660
   * RelOptInfo at all, unless the caller specified force_rel_creation.
7661
   */
7662
0
  if (cheapest_total_path == NULL &&
7663
0
    cheapest_partial_path == NULL &&
7664
0
    eager_agg_rel == NULL &&
7665
0
    !force_rel_creation)
7666
0
    return NULL;
7667
7668
  /*
7669
   * Build a new upper relation to represent the result of partially
7670
   * aggregating the rows from the input relation.
7671
   */
7672
0
  partially_grouped_rel = fetch_upper_rel(root,
7673
0
                      UPPERREL_PARTIAL_GROUP_AGG,
7674
0
                      grouped_rel->relids);
7675
0
  partially_grouped_rel->consider_parallel =
7676
0
    grouped_rel->consider_parallel;
7677
0
  partially_grouped_rel->pgs_mask = grouped_rel->pgs_mask;
7678
0
  partially_grouped_rel->reloptkind = grouped_rel->reloptkind;
7679
0
  partially_grouped_rel->serverid = grouped_rel->serverid;
7680
0
  partially_grouped_rel->userid = grouped_rel->userid;
7681
0
  partially_grouped_rel->useridiscurrent = grouped_rel->useridiscurrent;
7682
0
  partially_grouped_rel->fdwroutine = grouped_rel->fdwroutine;
7683
7684
  /*
7685
   * Build target list for partial aggregate paths.  These paths cannot just
7686
   * emit the same tlist as regular aggregate paths, because (1) we must
7687
   * include Vars and Aggrefs needed in HAVING, which might not appear in
7688
   * the result tlist, and (2) the Aggrefs must be set in partial mode.
7689
   */
7690
0
  partially_grouped_rel->reltarget =
7691
0
    make_partial_grouping_target(root, grouped_rel->reltarget,
7692
0
                   extra->havingQual);
7693
7694
0
  if (!extra->partial_costs_set)
7695
0
  {
7696
    /*
7697
     * Collect statistics about aggregates for estimating costs of
7698
     * performing aggregation in parallel.
7699
     */
7700
0
    MemSet(agg_partial_costs, 0, sizeof(AggClauseCosts));
7701
0
    MemSet(agg_final_costs, 0, sizeof(AggClauseCosts));
7702
0
    if (parse->hasAggs)
7703
0
    {
7704
      /* partial phase */
7705
0
      get_agg_clause_costs(root, AGGSPLIT_INITIAL_SERIAL,
7706
0
                 agg_partial_costs);
7707
7708
      /* final phase */
7709
0
      get_agg_clause_costs(root, AGGSPLIT_FINAL_DESERIAL,
7710
0
                 agg_final_costs);
7711
0
    }
7712
7713
0
    extra->partial_costs_set = true;
7714
0
  }
7715
7716
  /* Estimate number of partial groups. */
7717
0
  if (cheapest_total_path != NULL)
7718
0
    dNumPartialGroups =
7719
0
      get_number_of_groups(root,
7720
0
                 cheapest_total_path->rows,
7721
0
                 gd,
7722
0
                 extra->targetList);
7723
0
  if (cheapest_partial_path != NULL)
7724
0
    dNumPartialPartialGroups =
7725
0
      get_number_of_groups(root,
7726
0
                 cheapest_partial_path->rows,
7727
0
                 gd,
7728
0
                 extra->targetList);
7729
7730
0
  if (can_sort && cheapest_total_path != NULL)
7731
0
  {
7732
    /* This should have been checked previously */
7733
0
    Assert(parse->hasAggs || parse->groupClause);
7734
7735
    /*
7736
     * Use any available suitably-sorted path as input, and also consider
7737
     * sorting the cheapest partial path.
7738
     */
7739
0
    foreach(lc, input_rel->pathlist)
7740
0
    {
7741
0
      ListCell   *lc2;
7742
0
      Path     *path = (Path *) lfirst(lc);
7743
0
      Path     *path_save = path;
7744
0
      List     *pathkey_orderings = NIL;
7745
7746
      /* generate alternative group orderings that might be useful */
7747
0
      pathkey_orderings = get_useful_group_keys_orderings(root, path);
7748
7749
0
      Assert(list_length(pathkey_orderings) > 0);
7750
7751
      /* process all potentially interesting grouping reorderings */
7752
0
      foreach(lc2, pathkey_orderings)
7753
0
      {
7754
0
        GroupByOrdering *info = (GroupByOrdering *) lfirst(lc2);
7755
7756
        /* restore the path (we replace it in the loop) */
7757
0
        path = path_save;
7758
7759
0
        path = make_ordered_path(root,
7760
0
                     partially_grouped_rel,
7761
0
                     path,
7762
0
                     cheapest_total_path,
7763
0
                     info->pathkeys,
7764
0
                     -1.0);
7765
7766
0
        if (path == NULL)
7767
0
          continue;
7768
7769
0
        if (parse->hasAggs)
7770
0
          add_path(partially_grouped_rel, (Path *)
7771
0
               create_agg_path(root,
7772
0
                       partially_grouped_rel,
7773
0
                       path,
7774
0
                       partially_grouped_rel->reltarget,
7775
0
                       parse->groupClause ? AGG_SORTED : AGG_PLAIN,
7776
0
                       AGGSPLIT_INITIAL_SERIAL,
7777
0
                       info->clauses,
7778
0
                       NIL,
7779
0
                       agg_partial_costs,
7780
0
                       dNumPartialGroups));
7781
0
        else
7782
0
          add_path(partially_grouped_rel, (Path *)
7783
0
               create_group_path(root,
7784
0
                         partially_grouped_rel,
7785
0
                         path,
7786
0
                         info->clauses,
7787
0
                         NIL,
7788
0
                         dNumPartialGroups));
7789
0
      }
7790
0
    }
7791
0
  }
7792
7793
0
  if (can_sort && cheapest_partial_path != NULL)
7794
0
  {
7795
    /* Similar to above logic, but for partial paths. */
7796
0
    foreach(lc, input_rel->partial_pathlist)
7797
0
    {
7798
0
      ListCell   *lc2;
7799
0
      Path     *path = (Path *) lfirst(lc);
7800
0
      Path     *path_save = path;
7801
0
      List     *pathkey_orderings = NIL;
7802
7803
      /* generate alternative group orderings that might be useful */
7804
0
      pathkey_orderings = get_useful_group_keys_orderings(root, path);
7805
7806
0
      Assert(list_length(pathkey_orderings) > 0);
7807
7808
      /* process all potentially interesting grouping reorderings */
7809
0
      foreach(lc2, pathkey_orderings)
7810
0
      {
7811
0
        GroupByOrdering *info = (GroupByOrdering *) lfirst(lc2);
7812
7813
7814
        /* restore the path (we replace it in the loop) */
7815
0
        path = path_save;
7816
7817
0
        path = make_ordered_path(root,
7818
0
                     partially_grouped_rel,
7819
0
                     path,
7820
0
                     cheapest_partial_path,
7821
0
                     info->pathkeys,
7822
0
                     -1.0);
7823
7824
0
        if (path == NULL)
7825
0
          continue;
7826
7827
0
        if (parse->hasAggs)
7828
0
          add_partial_path(partially_grouped_rel, (Path *)
7829
0
                   create_agg_path(root,
7830
0
                           partially_grouped_rel,
7831
0
                           path,
7832
0
                           partially_grouped_rel->reltarget,
7833
0
                           parse->groupClause ? AGG_SORTED : AGG_PLAIN,
7834
0
                           AGGSPLIT_INITIAL_SERIAL,
7835
0
                           info->clauses,
7836
0
                           NIL,
7837
0
                           agg_partial_costs,
7838
0
                           dNumPartialPartialGroups));
7839
0
        else
7840
0
          add_partial_path(partially_grouped_rel, (Path *)
7841
0
                   create_group_path(root,
7842
0
                             partially_grouped_rel,
7843
0
                             path,
7844
0
                             info->clauses,
7845
0
                             NIL,
7846
0
                             dNumPartialPartialGroups));
7847
0
      }
7848
0
    }
7849
0
  }
7850
7851
  /*
7852
   * Add a partially-grouped HashAgg Path where possible
7853
   */
7854
0
  if (can_hash && cheapest_total_path != NULL)
7855
0
  {
7856
    /* Checked above */
7857
0
    Assert(parse->hasAggs || parse->groupClause);
7858
7859
0
    add_path(partially_grouped_rel, (Path *)
7860
0
         create_agg_path(root,
7861
0
                 partially_grouped_rel,
7862
0
                 cheapest_total_path,
7863
0
                 partially_grouped_rel->reltarget,
7864
0
                 AGG_HASHED,
7865
0
                 AGGSPLIT_INITIAL_SERIAL,
7866
0
                 root->processed_groupClause,
7867
0
                 NIL,
7868
0
                 agg_partial_costs,
7869
0
                 dNumPartialGroups));
7870
0
  }
7871
7872
  /*
7873
   * Now add a partially-grouped HashAgg partial Path where possible
7874
   */
7875
0
  if (can_hash && cheapest_partial_path != NULL)
7876
0
  {
7877
0
    add_partial_path(partially_grouped_rel, (Path *)
7878
0
             create_agg_path(root,
7879
0
                     partially_grouped_rel,
7880
0
                     cheapest_partial_path,
7881
0
                     partially_grouped_rel->reltarget,
7882
0
                     AGG_HASHED,
7883
0
                     AGGSPLIT_INITIAL_SERIAL,
7884
0
                     root->processed_groupClause,
7885
0
                     NIL,
7886
0
                     agg_partial_costs,
7887
0
                     dNumPartialPartialGroups));
7888
0
  }
7889
7890
  /*
7891
   * Add any partially aggregated paths generated by eager aggregation to
7892
   * the new upper relation after applying projection steps as needed.
7893
   */
7894
0
  if (eager_agg_rel)
7895
0
  {
7896
    /* Add the paths */
7897
0
    foreach(lc, eager_agg_rel->pathlist)
7898
0
    {
7899
0
      Path     *path = (Path *) lfirst(lc);
7900
7901
      /* Shouldn't have any parameterized paths anymore */
7902
0
      Assert(path->param_info == NULL);
7903
7904
0
      path = (Path *) create_projection_path(root,
7905
0
                           partially_grouped_rel,
7906
0
                           path,
7907
0
                           partially_grouped_rel->reltarget);
7908
7909
0
      add_path(partially_grouped_rel, path);
7910
0
    }
7911
7912
    /*
7913
     * Likewise add the partial paths, but only if parallelism is possible
7914
     * for partially_grouped_rel.
7915
     */
7916
0
    if (partially_grouped_rel->consider_parallel)
7917
0
    {
7918
0
      foreach(lc, eager_agg_rel->partial_pathlist)
7919
0
      {
7920
0
        Path     *path = (Path *) lfirst(lc);
7921
7922
        /* Shouldn't have any parameterized paths anymore */
7923
0
        Assert(path->param_info == NULL);
7924
7925
0
        path = (Path *) create_projection_path(root,
7926
0
                             partially_grouped_rel,
7927
0
                             path,
7928
0
                             partially_grouped_rel->reltarget);
7929
7930
0
        add_partial_path(partially_grouped_rel, path);
7931
0
      }
7932
0
    }
7933
0
  }
7934
7935
  /*
7936
   * If there is an FDW that's responsible for all baserels of the query,
7937
   * let it consider adding partially grouped ForeignPaths.
7938
   */
7939
0
  if (partially_grouped_rel->fdwroutine &&
7940
0
    partially_grouped_rel->fdwroutine->GetForeignUpperPaths)
7941
0
  {
7942
0
    FdwRoutine *fdwroutine = partially_grouped_rel->fdwroutine;
7943
7944
0
    fdwroutine->GetForeignUpperPaths(root,
7945
0
                     UPPERREL_PARTIAL_GROUP_AGG,
7946
0
                     input_rel, partially_grouped_rel,
7947
0
                     extra);
7948
0
  }
7949
7950
0
  return partially_grouped_rel;
7951
0
}
7952
7953
/*
7954
 * make_ordered_path
7955
 *    Return a path ordered by 'pathkeys' based on the given 'path'.  May
7956
 *    return NULL if it doesn't make sense to generate an ordered path in
7957
 *    this case.
7958
 */
7959
static Path *
7960
make_ordered_path(PlannerInfo *root, RelOptInfo *rel, Path *path,
7961
          Path *cheapest_path, List *pathkeys, double limit_tuples)
7962
0
{
7963
0
  bool    is_sorted;
7964
0
  int     presorted_keys;
7965
7966
0
  is_sorted = pathkeys_count_contained_in(pathkeys,
7967
0
                      path->pathkeys,
7968
0
                      &presorted_keys);
7969
7970
0
  if (!is_sorted)
7971
0
  {
7972
    /*
7973
     * Try at least sorting the cheapest path and also try incrementally
7974
     * sorting any path which is partially sorted already (no need to deal
7975
     * with paths which have presorted keys when incremental sort is
7976
     * disabled unless it's the cheapest input path).
7977
     */
7978
0
    if (path != cheapest_path &&
7979
0
      (presorted_keys == 0 || !enable_incremental_sort))
7980
0
      return NULL;
7981
7982
    /*
7983
     * We've no need to consider both a sort and incremental sort. We'll
7984
     * just do a sort if there are no presorted keys and an incremental
7985
     * sort when there are presorted keys.
7986
     */
7987
0
    if (presorted_keys == 0 || !enable_incremental_sort)
7988
0
      path = (Path *) create_sort_path(root,
7989
0
                       rel,
7990
0
                       path,
7991
0
                       pathkeys,
7992
0
                       limit_tuples);
7993
0
    else
7994
0
      path = (Path *) create_incremental_sort_path(root,
7995
0
                             rel,
7996
0
                             path,
7997
0
                             pathkeys,
7998
0
                             presorted_keys,
7999
0
                             limit_tuples);
8000
0
  }
8001
8002
0
  return path;
8003
0
}
8004
8005
/*
8006
 * Generate Gather and Gather Merge paths for a grouping relation or partial
8007
 * grouping relation.
8008
 *
8009
 * generate_useful_gather_paths does most of the work, but we also consider a
8010
 * special case: we could try sorting the data by the group_pathkeys and then
8011
 * applying Gather Merge.
8012
 *
8013
 * NB: This function shouldn't be used for anything other than a grouped or
8014
 * partially grouped relation not only because of the fact that it explicitly
8015
 * references group_pathkeys but we pass "true" as the third argument to
8016
 * generate_useful_gather_paths().
8017
 */
8018
static void
8019
gather_grouping_paths(PlannerInfo *root, RelOptInfo *rel)
8020
0
{
8021
0
  ListCell   *lc;
8022
0
  Path     *cheapest_partial_path;
8023
0
  List     *groupby_pathkeys;
8024
8025
  /*
8026
   * This occurs after any partial aggregation has taken place, so trim off
8027
   * any pathkeys added for ORDER BY / DISTINCT aggregates.
8028
   */
8029
0
  if (list_length(root->group_pathkeys) > root->num_groupby_pathkeys)
8030
0
    groupby_pathkeys = list_copy_head(root->group_pathkeys,
8031
0
                      root->num_groupby_pathkeys);
8032
0
  else
8033
0
    groupby_pathkeys = root->group_pathkeys;
8034
8035
  /* Try Gather for unordered paths and Gather Merge for ordered ones. */
8036
0
  generate_useful_gather_paths(root, rel, true);
8037
8038
0
  cheapest_partial_path = linitial(rel->partial_pathlist);
8039
8040
  /* XXX Shouldn't this also consider the group-key-reordering? */
8041
0
  foreach(lc, rel->partial_pathlist)
8042
0
  {
8043
0
    Path     *path = (Path *) lfirst(lc);
8044
0
    bool    is_sorted;
8045
0
    int     presorted_keys;
8046
0
    double    total_groups;
8047
8048
0
    is_sorted = pathkeys_count_contained_in(groupby_pathkeys,
8049
0
                        path->pathkeys,
8050
0
                        &presorted_keys);
8051
8052
0
    if (is_sorted)
8053
0
      continue;
8054
8055
    /*
8056
     * Try at least sorting the cheapest path and also try incrementally
8057
     * sorting any path which is partially sorted already (no need to deal
8058
     * with paths which have presorted keys when incremental sort is
8059
     * disabled unless it's the cheapest input path).
8060
     */
8061
0
    if (path != cheapest_partial_path &&
8062
0
      (presorted_keys == 0 || !enable_incremental_sort))
8063
0
      continue;
8064
8065
    /*
8066
     * We've no need to consider both a sort and incremental sort. We'll
8067
     * just do a sort if there are no presorted keys and an incremental
8068
     * sort when there are presorted keys.
8069
     */
8070
0
    if (presorted_keys == 0 || !enable_incremental_sort)
8071
0
      path = (Path *) create_sort_path(root, rel, path,
8072
0
                       groupby_pathkeys,
8073
0
                       -1.0);
8074
0
    else
8075
0
      path = (Path *) create_incremental_sort_path(root,
8076
0
                             rel,
8077
0
                             path,
8078
0
                             groupby_pathkeys,
8079
0
                             presorted_keys,
8080
0
                             -1.0);
8081
0
    total_groups = compute_gather_rows(path);
8082
0
    path = (Path *)
8083
0
      create_gather_merge_path(root,
8084
0
                   rel,
8085
0
                   path,
8086
0
                   rel->reltarget,
8087
0
                   groupby_pathkeys,
8088
0
                   NULL,
8089
0
                   &total_groups);
8090
8091
0
    add_path(rel, path);
8092
0
  }
8093
0
}
8094
8095
/*
8096
 * can_partial_agg
8097
 *
8098
 * Determines whether or not partial grouping and/or aggregation is possible.
8099
 * Returns true when possible, false otherwise.
8100
 */
8101
static bool
8102
can_partial_agg(PlannerInfo *root)
8103
0
{
8104
0
  Query    *parse = root->parse;
8105
8106
0
  if (!parse->hasAggs && parse->groupClause == NIL)
8107
0
  {
8108
    /*
8109
     * We don't know how to do parallel aggregation unless we have either
8110
     * some aggregates or a grouping clause.
8111
     */
8112
0
    return false;
8113
0
  }
8114
0
  else if (parse->groupingSets)
8115
0
  {
8116
    /* We don't know how to do grouping sets in parallel. */
8117
0
    return false;
8118
0
  }
8119
0
  else if (root->hasNonPartialAggs || root->hasNonSerialAggs)
8120
0
  {
8121
    /* Insufficient support for partial mode. */
8122
0
    return false;
8123
0
  }
8124
8125
  /* Everything looks good. */
8126
0
  return true;
8127
0
}
8128
8129
/*
8130
 * apply_scanjoin_target_to_paths
8131
 *
8132
 * Adjust the final scan/join relation, and recursively all of its children,
8133
 * to generate the final scan/join target.  It would be more correct to model
8134
 * this as a separate planning step with a new RelOptInfo at the toplevel and
8135
 * for each child relation, but doing it this way is noticeably cheaper.
8136
 * Maybe that problem can be solved at some point, but for now we do this.
8137
 *
8138
 * If tlist_same_exprs is true, then the scan/join target to be applied has
8139
 * the same expressions as the existing reltarget, so we need only insert the
8140
 * appropriate sortgroupref information.  By avoiding the creation of
8141
 * projection paths we save effort both immediately and at plan creation time.
8142
 */
8143
static void
8144
apply_scanjoin_target_to_paths(PlannerInfo *root,
8145
                 RelOptInfo *rel,
8146
                 List *scanjoin_targets,
8147
                 List *scanjoin_targets_contain_srfs,
8148
                 bool scanjoin_target_parallel_safe,
8149
                 bool tlist_same_exprs)
8150
0
{
8151
0
  bool    rel_is_partitioned = IS_PARTITIONED_REL(rel);
8152
0
  PathTarget *scanjoin_target;
8153
0
  ListCell   *lc;
8154
8155
  /* This recurses, so be paranoid. */
8156
0
  check_stack_depth();
8157
8158
  /*
8159
   * If the rel only has Append and MergeAppend paths, we want to drop its
8160
   * existing paths and generate new ones.  This function would still be
8161
   * correct if we kept the existing paths: we'd modify them to generate the
8162
   * correct target above the partitioning Append, and then they'd compete
8163
   * on cost with paths generating the target below the Append.  However, in
8164
   * our current cost model the latter way is always the same or cheaper
8165
   * cost, so modifying the existing paths would just be useless work.
8166
   * Moreover, when the cost is the same, varying roundoff errors might
8167
   * sometimes allow an existing path to be picked, resulting in undesirable
8168
   * cross-platform plan variations.  So we drop old paths and thereby force
8169
   * the work to be done below the Append.
8170
   *
8171
   * However, there are several cases when this optimization is not safe. If
8172
   * the rel isn't partitioned, then none of the paths will be Append or
8173
   * MergeAppend paths, so we should definitely not do this. If it is
8174
   * partitioned but is a joinrel, it may have Append and MergeAppend paths,
8175
   * but it can also have join paths that we can't afford to discard.
8176
   *
8177
   * Some care is needed, because we have to allow
8178
   * generate_useful_gather_paths to see the old partial paths in the next
8179
   * stanza.  Hence, zap the main pathlist here, then allow
8180
   * generate_useful_gather_paths to add path(s) to the main list, and
8181
   * finally zap the partial pathlist.
8182
   */
8183
0
  if (rel_is_partitioned && IS_SIMPLE_REL(rel))
8184
0
    rel->pathlist = NIL;
8185
8186
  /*
8187
   * If the scan/join target is not parallel-safe, partial paths cannot
8188
   * generate it.
8189
   */
8190
0
  if (!scanjoin_target_parallel_safe)
8191
0
  {
8192
    /*
8193
     * Since we can't generate the final scan/join target in parallel
8194
     * workers, this is our last opportunity to use any partial paths that
8195
     * exist; so build Gather path(s) that use them and emit whatever the
8196
     * current reltarget is.  We don't do this in the case where the
8197
     * target is parallel-safe, since we will be able to generate superior
8198
     * paths by doing it after the final scan/join target has been
8199
     * applied.
8200
     */
8201
0
    generate_useful_gather_paths(root, rel, false);
8202
8203
    /* Can't use parallel query above this level. */
8204
0
    rel->partial_pathlist = NIL;
8205
0
    rel->consider_parallel = false;
8206
0
  }
8207
8208
  /* Finish dropping old paths for a partitioned rel, per comment above */
8209
0
  if (rel_is_partitioned && IS_SIMPLE_REL(rel))
8210
0
    rel->partial_pathlist = NIL;
8211
8212
  /* Extract SRF-free scan/join target. */
8213
0
  scanjoin_target = linitial_node(PathTarget, scanjoin_targets);
8214
8215
  /*
8216
   * Apply the SRF-free scan/join target to each existing path.
8217
   *
8218
   * If the tlist exprs are the same, we can just inject the sortgroupref
8219
   * information into the existing pathtargets.  Otherwise, replace each
8220
   * path with a projection path that generates the SRF-free scan/join
8221
   * target.  This can't change the ordering of paths within rel->pathlist,
8222
   * so we just modify the list in place.
8223
   */
8224
0
  foreach(lc, rel->pathlist)
8225
0
  {
8226
0
    Path     *subpath = (Path *) lfirst(lc);
8227
8228
    /* Shouldn't have any parameterized paths anymore */
8229
0
    Assert(subpath->param_info == NULL);
8230
8231
0
    if (tlist_same_exprs)
8232
0
      subpath->pathtarget->sortgrouprefs =
8233
0
        scanjoin_target->sortgrouprefs;
8234
0
    else
8235
0
    {
8236
0
      Path     *newpath;
8237
8238
0
      newpath = (Path *) create_projection_path(root, rel, subpath,
8239
0
                            scanjoin_target);
8240
0
      lfirst(lc) = newpath;
8241
0
    }
8242
0
  }
8243
8244
  /* Likewise adjust the targets for any partial paths. */
8245
0
  foreach(lc, rel->partial_pathlist)
8246
0
  {
8247
0
    Path     *subpath = (Path *) lfirst(lc);
8248
8249
    /* Shouldn't have any parameterized paths anymore */
8250
0
    Assert(subpath->param_info == NULL);
8251
8252
0
    if (tlist_same_exprs)
8253
0
      subpath->pathtarget->sortgrouprefs =
8254
0
        scanjoin_target->sortgrouprefs;
8255
0
    else
8256
0
    {
8257
0
      Path     *newpath;
8258
8259
0
      newpath = (Path *) create_projection_path(root, rel, subpath,
8260
0
                            scanjoin_target);
8261
0
      lfirst(lc) = newpath;
8262
0
    }
8263
0
  }
8264
8265
  /*
8266
   * Now, if final scan/join target contains SRFs, insert ProjectSetPath(s)
8267
   * atop each existing path.  (Note that this function doesn't look at the
8268
   * cheapest-path fields, which is a good thing because they're bogus right
8269
   * now.)
8270
   */
8271
0
  if (root->parse->hasTargetSRFs)
8272
0
    adjust_paths_for_srfs(root, rel,
8273
0
                scanjoin_targets,
8274
0
                scanjoin_targets_contain_srfs);
8275
8276
  /*
8277
   * Update the rel's target to be the final (with SRFs) scan/join target.
8278
   * This now matches the actual output of all the paths, and we might get
8279
   * confused in createplan.c if they don't agree.  We must do this now so
8280
   * that any append paths made in the next part will use the correct
8281
   * pathtarget (cf. create_append_path).
8282
   *
8283
   * Note that this is also necessary if GetForeignUpperPaths() gets called
8284
   * on the final scan/join relation or on any of its children, since the
8285
   * FDW might look at the rel's target to create ForeignPaths.
8286
   */
8287
0
  rel->reltarget = llast_node(PathTarget, scanjoin_targets);
8288
8289
  /*
8290
   * If the relation is partitioned, recursively apply the scan/join target
8291
   * to all partitions, and generate brand-new Append paths in which the
8292
   * scan/join target is computed below the Append rather than above it.
8293
   * Since Append is not projection-capable, that might save a separate
8294
   * Result node, and it also is important for partitionwise aggregate.
8295
   */
8296
0
  if (rel_is_partitioned)
8297
0
  {
8298
0
    List     *live_children = NIL;
8299
0
    int     i;
8300
8301
    /* Adjust each partition. */
8302
0
    i = -1;
8303
0
    while ((i = bms_next_member(rel->live_parts, i)) >= 0)
8304
0
    {
8305
0
      RelOptInfo *child_rel = rel->part_rels[i];
8306
0
      AppendRelInfo **appinfos;
8307
0
      int     nappinfos;
8308
0
      List     *child_scanjoin_targets = NIL;
8309
8310
0
      Assert(child_rel != NULL);
8311
8312
      /* Dummy children can be ignored. */
8313
0
      if (IS_DUMMY_REL(child_rel))
8314
0
        continue;
8315
8316
      /* Translate scan/join targets for this child. */
8317
0
      appinfos = find_appinfos_by_relids(root, child_rel->relids,
8318
0
                         &nappinfos);
8319
0
      foreach(lc, scanjoin_targets)
8320
0
      {
8321
0
        PathTarget *target = lfirst_node(PathTarget, lc);
8322
8323
0
        target = copy_pathtarget(target);
8324
0
        target->exprs = (List *)
8325
0
          adjust_appendrel_attrs(root,
8326
0
                       (Node *) target->exprs,
8327
0
                       nappinfos, appinfos);
8328
0
        child_scanjoin_targets = lappend(child_scanjoin_targets,
8329
0
                         target);
8330
0
      }
8331
0
      pfree(appinfos);
8332
8333
      /* Recursion does the real work. */
8334
0
      apply_scanjoin_target_to_paths(root, child_rel,
8335
0
                       child_scanjoin_targets,
8336
0
                       scanjoin_targets_contain_srfs,
8337
0
                       scanjoin_target_parallel_safe,
8338
0
                       tlist_same_exprs);
8339
8340
      /* Save non-dummy children for Append paths. */
8341
0
      if (!IS_DUMMY_REL(child_rel))
8342
0
        live_children = lappend(live_children, child_rel);
8343
0
    }
8344
8345
    /* Build new paths for this relation by appending child paths. */
8346
0
    add_paths_to_append_rel(root, rel, live_children);
8347
0
  }
8348
8349
  /*
8350
   * Consider generating Gather or Gather Merge paths.  We must only do this
8351
   * if the relation is parallel safe, and we don't do it for child rels to
8352
   * avoid creating multiple Gather nodes within the same plan. We must do
8353
   * this after all paths have been generated and before set_cheapest, since
8354
   * one of the generated paths may turn out to be the cheapest one.
8355
   */
8356
0
  if (rel->consider_parallel && !IS_OTHER_REL(rel))
8357
0
    generate_useful_gather_paths(root, rel, false);
8358
8359
  /*
8360
   * Reassess which paths are the cheapest, now that we've potentially added
8361
   * new Gather (or Gather Merge) and/or Append (or MergeAppend) paths to
8362
   * this relation.
8363
   */
8364
0
  set_cheapest(rel);
8365
0
}
8366
8367
/*
8368
 * create_partitionwise_grouping_paths
8369
 *
8370
 * If the partition keys of input relation are part of the GROUP BY clause, all
8371
 * the rows belonging to a given group come from a single partition.  This
8372
 * allows aggregation/grouping over a partitioned relation to be broken down
8373
 * into aggregation/grouping on each partition.  This should be no worse, and
8374
 * often better, than the normal approach.
8375
 *
8376
 * However, if the GROUP BY clause does not contain all the partition keys,
8377
 * rows from a given group may be spread across multiple partitions. In that
8378
 * case, we perform partial aggregation for each group, append the results,
8379
 * and then finalize aggregation.  This is less certain to win than the
8380
 * previous case.  It may win if the PartialAggregate stage greatly reduces
8381
 * the number of groups, because fewer rows will pass through the Append node.
8382
 * It may lose if we have lots of small groups.
8383
 */
8384
static void
8385
create_partitionwise_grouping_paths(PlannerInfo *root,
8386
                  RelOptInfo *input_rel,
8387
                  RelOptInfo *grouped_rel,
8388
                  RelOptInfo *partially_grouped_rel,
8389
                  const AggClauseCosts *agg_costs,
8390
                  grouping_sets_data *gd,
8391
                  PartitionwiseAggregateType patype,
8392
                  GroupPathExtraData *extra)
8393
0
{
8394
0
  List     *grouped_live_children = NIL;
8395
0
  List     *partially_grouped_live_children = NIL;
8396
0
  PathTarget *target = grouped_rel->reltarget;
8397
0
  bool    partial_grouping_valid = true;
8398
0
  int     i;
8399
8400
0
  Assert(patype != PARTITIONWISE_AGGREGATE_NONE);
8401
0
  Assert(patype != PARTITIONWISE_AGGREGATE_PARTIAL ||
8402
0
       partially_grouped_rel != NULL);
8403
8404
  /* Add paths for partitionwise aggregation/grouping. */
8405
0
  i = -1;
8406
0
  while ((i = bms_next_member(input_rel->live_parts, i)) >= 0)
8407
0
  {
8408
0
    RelOptInfo *child_input_rel = input_rel->part_rels[i];
8409
0
    PathTarget *child_target;
8410
0
    AppendRelInfo **appinfos;
8411
0
    int     nappinfos;
8412
0
    GroupPathExtraData child_extra;
8413
0
    RelOptInfo *child_grouped_rel;
8414
0
    RelOptInfo *child_partially_grouped_rel;
8415
8416
0
    Assert(child_input_rel != NULL);
8417
8418
    /* Dummy children can be ignored. */
8419
0
    if (IS_DUMMY_REL(child_input_rel))
8420
0
      continue;
8421
8422
0
    child_target = copy_pathtarget(target);
8423
8424
    /*
8425
     * Copy the given "extra" structure as is and then override the
8426
     * members specific to this child.
8427
     */
8428
0
    memcpy(&child_extra, extra, sizeof(child_extra));
8429
8430
0
    appinfos = find_appinfos_by_relids(root, child_input_rel->relids,
8431
0
                       &nappinfos);
8432
8433
0
    child_target->exprs = (List *)
8434
0
      adjust_appendrel_attrs(root,
8435
0
                   (Node *) target->exprs,
8436
0
                   nappinfos, appinfos);
8437
8438
    /* Translate havingQual and targetList. */
8439
0
    child_extra.havingQual = (Node *)
8440
0
      adjust_appendrel_attrs(root,
8441
0
                   extra->havingQual,
8442
0
                   nappinfos, appinfos);
8443
0
    child_extra.targetList = (List *)
8444
0
      adjust_appendrel_attrs(root,
8445
0
                   (Node *) extra->targetList,
8446
0
                   nappinfos, appinfos);
8447
8448
    /*
8449
     * extra->patype was the value computed for our parent rel; patype is
8450
     * the value for this relation.  For the child, our value is its
8451
     * parent rel's value.
8452
     */
8453
0
    child_extra.patype = patype;
8454
8455
    /*
8456
     * Create grouping relation to hold fully aggregated grouping and/or
8457
     * aggregation paths for the child.
8458
     */
8459
0
    child_grouped_rel = make_grouping_rel(root, child_input_rel,
8460
0
                        child_target,
8461
0
                        extra->target_parallel_safe,
8462
0
                        child_extra.havingQual);
8463
8464
    /* Create grouping paths for this child relation. */
8465
0
    create_ordinary_grouping_paths(root, child_input_rel,
8466
0
                     child_grouped_rel,
8467
0
                     agg_costs, gd, &child_extra,
8468
0
                     &child_partially_grouped_rel);
8469
8470
0
    if (child_partially_grouped_rel)
8471
0
    {
8472
0
      partially_grouped_live_children =
8473
0
        lappend(partially_grouped_live_children,
8474
0
            child_partially_grouped_rel);
8475
0
    }
8476
0
    else
8477
0
      partial_grouping_valid = false;
8478
8479
0
    if (patype == PARTITIONWISE_AGGREGATE_FULL)
8480
0
    {
8481
0
      set_cheapest(child_grouped_rel);
8482
0
      grouped_live_children = lappend(grouped_live_children,
8483
0
                      child_grouped_rel);
8484
0
    }
8485
8486
0
    pfree(appinfos);
8487
0
  }
8488
8489
  /*
8490
   * Try to create append paths for partially grouped children. For full
8491
   * partitionwise aggregation, we might have paths in the partial_pathlist
8492
   * if parallel aggregation is possible.  For partial partitionwise
8493
   * aggregation, we may have paths in both pathlist and partial_pathlist.
8494
   *
8495
   * NB: We must have a partially grouped path for every child in order to
8496
   * generate a partially grouped path for this relation.
8497
   */
8498
0
  if (partially_grouped_rel && partial_grouping_valid)
8499
0
  {
8500
0
    Assert(partially_grouped_live_children != NIL);
8501
8502
0
    add_paths_to_append_rel(root, partially_grouped_rel,
8503
0
                partially_grouped_live_children);
8504
0
  }
8505
8506
  /* If possible, create append paths for fully grouped children. */
8507
0
  if (patype == PARTITIONWISE_AGGREGATE_FULL)
8508
0
  {
8509
0
    Assert(grouped_live_children != NIL);
8510
8511
0
    add_paths_to_append_rel(root, grouped_rel, grouped_live_children);
8512
0
  }
8513
0
}
8514
8515
/*
8516
 * group_by_has_partkey
8517
 *
8518
 * Returns true if all the partition keys of the given relation are part of
8519
 * the GROUP BY clauses, including having matching collation, false otherwise.
8520
 */
8521
static bool
8522
group_by_has_partkey(RelOptInfo *input_rel,
8523
           List *targetList,
8524
           List *groupClause)
8525
0
{
8526
0
  List     *groupexprs = get_sortgrouplist_exprs(groupClause, targetList);
8527
0
  int     cnt = 0;
8528
0
  int     partnatts;
8529
8530
  /* Input relation should be partitioned. */
8531
0
  Assert(input_rel->part_scheme);
8532
8533
  /* Rule out early, if there are no partition keys present. */
8534
0
  if (!input_rel->partexprs)
8535
0
    return false;
8536
8537
0
  partnatts = input_rel->part_scheme->partnatts;
8538
8539
0
  for (cnt = 0; cnt < partnatts; cnt++)
8540
0
  {
8541
0
    List     *partexprs = input_rel->partexprs[cnt];
8542
0
    ListCell   *lc;
8543
0
    bool    found = false;
8544
8545
0
    foreach(lc, partexprs)
8546
0
    {
8547
0
      ListCell   *lg;
8548
0
      Expr     *partexpr = lfirst(lc);
8549
0
      Oid     partcoll = input_rel->part_scheme->partcollation[cnt];
8550
8551
0
      foreach(lg, groupexprs)
8552
0
      {
8553
0
        Expr     *groupexpr = lfirst(lg);
8554
0
        Oid     groupcoll = exprCollation((Node *) groupexpr);
8555
8556
        /*
8557
         * Note: we can assume there is at most one RelabelType node;
8558
         * eval_const_expressions() will have simplified if more than
8559
         * one.
8560
         */
8561
0
        if (IsA(groupexpr, RelabelType))
8562
0
          groupexpr = ((RelabelType *) groupexpr)->arg;
8563
8564
0
        if (equal(groupexpr, partexpr))
8565
0
        {
8566
          /*
8567
           * Reject a match if the grouping collation does not match
8568
           * the partitioning collation.
8569
           */
8570
0
          if (OidIsValid(partcoll) && OidIsValid(groupcoll) &&
8571
0
            partcoll != groupcoll)
8572
0
            return false;
8573
8574
0
          found = true;
8575
0
          break;
8576
0
        }
8577
0
      }
8578
8579
0
      if (found)
8580
0
        break;
8581
0
    }
8582
8583
    /*
8584
     * If none of the partition key expressions match with any of the
8585
     * GROUP BY expression, return false.
8586
     */
8587
0
    if (!found)
8588
0
      return false;
8589
0
  }
8590
8591
0
  return true;
8592
0
}
8593
8594
/*
8595
 * generate_setop_child_grouplist
8596
 *    Build a SortGroupClause list defining the sort/grouping properties
8597
 *    of the child of a set operation.
8598
 *
8599
 * This is similar to generate_setop_grouplist() but differs as the setop
8600
 * child query's targetlist entries may already have a tleSortGroupRef
8601
 * assigned for other purposes, such as GROUP BYs.  Here we keep the
8602
 * SortGroupClause list in the same order as 'op' groupClauses and just adjust
8603
 * the tleSortGroupRef to reference the TargetEntry's 'ressortgroupref'.  If
8604
 * any of the columns in the targetlist don't match to the setop's colTypes
8605
 * then we return an empty list.  This may leave some TLEs with unreferenced
8606
 * ressortgroupref markings, but that's harmless.
8607
 */
8608
static List *
8609
generate_setop_child_grouplist(SetOperationStmt *op, List *targetlist)
8610
0
{
8611
0
  List     *grouplist = copyObject(op->groupClauses);
8612
0
  ListCell   *lg;
8613
0
  ListCell   *lt;
8614
0
  ListCell   *ct;
8615
8616
0
  lg = list_head(grouplist);
8617
0
  ct = list_head(op->colTypes);
8618
0
  foreach(lt, targetlist)
8619
0
  {
8620
0
    TargetEntry *tle = (TargetEntry *) lfirst(lt);
8621
0
    SortGroupClause *sgc;
8622
0
    Oid     coltype;
8623
8624
    /* resjunk columns could have sortgrouprefs.  Leave these alone */
8625
0
    if (tle->resjunk)
8626
0
      continue;
8627
8628
    /*
8629
     * We expect every non-resjunk target to have a SortGroupClause and
8630
     * colTypes.
8631
     */
8632
0
    Assert(lg != NULL);
8633
0
    Assert(ct != NULL);
8634
0
    sgc = (SortGroupClause *) lfirst(lg);
8635
0
    coltype = lfirst_oid(ct);
8636
8637
    /* reject if target type isn't the same as the setop target type */
8638
0
    if (coltype != exprType((Node *) tle->expr))
8639
0
      return NIL;
8640
8641
0
    lg = lnext(grouplist, lg);
8642
0
    ct = lnext(op->colTypes, ct);
8643
8644
    /* assign a tleSortGroupRef, or reuse the existing one */
8645
0
    sgc->tleSortGroupRef = assignSortGroupRef(tle, targetlist);
8646
0
  }
8647
8648
0
  Assert(lg == NULL);
8649
0
  Assert(ct == NULL);
8650
8651
0
  return grouplist;
8652
0
}
8653
8654
/*
8655
 * create_unique_paths
8656
 *    Build a new RelOptInfo containing Paths that represent elimination of
8657
 *    distinct rows from the input data.  Distinct-ness is defined according to
8658
 *    the needs of the semijoin represented by sjinfo.  If it is not possible
8659
 *    to identify how to make the data unique, NULL is returned.
8660
 *
8661
 * If used at all, this is likely to be called repeatedly on the same rel,
8662
 * so we cache the result.
8663
 */
8664
RelOptInfo *
8665
create_unique_paths(PlannerInfo *root, RelOptInfo *rel, SpecialJoinInfo *sjinfo)
8666
0
{
8667
0
  RelOptInfo *unique_rel;
8668
0
  List     *sortPathkeys = NIL;
8669
0
  List     *groupClause = NIL;
8670
0
  MemoryContext oldcontext;
8671
8672
  /* Caller made a mistake if SpecialJoinInfo is the wrong one */
8673
0
  Assert(sjinfo->jointype == JOIN_SEMI);
8674
0
  Assert(bms_equal(rel->relids, sjinfo->syn_righthand));
8675
8676
  /* If result already cached, return it */
8677
0
  if (rel->unique_rel)
8678
0
    return rel->unique_rel;
8679
8680
  /* If it's not possible to unique-ify, return NULL */
8681
0
  if (!(sjinfo->semi_can_btree || sjinfo->semi_can_hash))
8682
0
    return NULL;
8683
8684
  /*
8685
   * Punt if this is a child relation and we failed to build a unique-ified
8686
   * relation for its parent.  This can happen if all the RHS columns were
8687
   * found to be equated to constants when unique-ifying the parent table,
8688
   * leaving no columns to unique-ify.
8689
   */
8690
0
  if (IS_OTHER_REL(rel) && rel->top_parent->unique_rel == NULL)
8691
0
    return NULL;
8692
8693
  /*
8694
   * When called during GEQO join planning, we are in a short-lived memory
8695
   * context.  We must make sure that the unique rel and any subsidiary data
8696
   * structures created for a baserel survive the GEQO cycle, else the
8697
   * baserel is trashed for future GEQO cycles.  On the other hand, when we
8698
   * are creating those for a joinrel during GEQO, we don't want them to
8699
   * clutter the main planning context.  Upshot is that the best solution is
8700
   * to explicitly allocate memory in the same context the given RelOptInfo
8701
   * is in.
8702
   */
8703
0
  oldcontext = MemoryContextSwitchTo(GetMemoryChunkContext(rel));
8704
8705
0
  unique_rel = makeNode(RelOptInfo);
8706
0
  memcpy(unique_rel, rel, sizeof(RelOptInfo));
8707
8708
  /*
8709
   * clear path info
8710
   */
8711
0
  unique_rel->pathlist = NIL;
8712
0
  unique_rel->ppilist = NIL;
8713
0
  unique_rel->partial_pathlist = NIL;
8714
0
  unique_rel->cheapest_startup_path = NULL;
8715
0
  unique_rel->cheapest_total_path = NULL;
8716
0
  unique_rel->cheapest_parameterized_paths = NIL;
8717
8718
  /*
8719
   * Build the target list for the unique rel.  We also build the pathkeys
8720
   * that represent the ordering requirements for the sort-based
8721
   * implementation, and the list of SortGroupClause nodes that represent
8722
   * the columns to be grouped on for the hash-based implementation.
8723
   *
8724
   * For a child rel, we can construct these fields from those of its
8725
   * parent.
8726
   */
8727
0
  if (IS_OTHER_REL(rel))
8728
0
  {
8729
0
    PathTarget *child_unique_target;
8730
0
    PathTarget *parent_unique_target;
8731
8732
0
    parent_unique_target = rel->top_parent->unique_rel->reltarget;
8733
8734
0
    child_unique_target = copy_pathtarget(parent_unique_target);
8735
8736
    /* Translate the target expressions */
8737
0
    child_unique_target->exprs = (List *)
8738
0
      adjust_appendrel_attrs_multilevel(root,
8739
0
                        (Node *) parent_unique_target->exprs,
8740
0
                        rel,
8741
0
                        rel->top_parent);
8742
8743
0
    unique_rel->reltarget = child_unique_target;
8744
8745
0
    sortPathkeys = rel->top_parent->unique_pathkeys;
8746
0
    groupClause = rel->top_parent->unique_groupclause;
8747
0
  }
8748
0
  else
8749
0
  {
8750
0
    List     *newtlist;
8751
0
    int     nextresno;
8752
0
    List     *sortList = NIL;
8753
0
    ListCell   *lc1;
8754
0
    ListCell   *lc2;
8755
8756
    /*
8757
     * The values we are supposed to unique-ify may be expressions in the
8758
     * variables of the input rel's targetlist.  We have to add any such
8759
     * expressions to the unique rel's targetlist.
8760
     *
8761
     * To complicate matters, some of the values to be unique-ified may be
8762
     * known redundant by the EquivalenceClass machinery (e.g., because
8763
     * they have been equated to constants).  There is no need to compare
8764
     * such values during unique-ification, and indeed we had better not
8765
     * try because the Vars involved may not have propagated as high as
8766
     * the semijoin's level.  We use make_pathkeys_for_sortclauses to
8767
     * detect such cases, which is a tad inefficient but it doesn't seem
8768
     * worth building specialized infrastructure for this.
8769
     */
8770
0
    newtlist = make_tlist_from_pathtarget(rel->reltarget);
8771
0
    nextresno = list_length(newtlist) + 1;
8772
8773
0
    forboth(lc1, sjinfo->semi_rhs_exprs, lc2, sjinfo->semi_operators)
8774
0
    {
8775
0
      Expr     *uniqexpr = lfirst(lc1);
8776
0
      Oid     in_oper = lfirst_oid(lc2);
8777
0
      Oid     sortop;
8778
0
      TargetEntry *tle;
8779
0
      bool    made_tle = false;
8780
8781
0
      tle = tlist_member(uniqexpr, newtlist);
8782
0
      if (!tle)
8783
0
      {
8784
0
        tle = makeTargetEntry(uniqexpr,
8785
0
                    nextresno,
8786
0
                    NULL,
8787
0
                    false);
8788
0
        newtlist = lappend(newtlist, tle);
8789
0
        nextresno++;
8790
0
        made_tle = true;
8791
0
      }
8792
8793
      /*
8794
       * Try to build an ORDER BY list to sort the input compatibly.  We
8795
       * do this for each sortable clause even when the clauses are not
8796
       * all sortable, so that we can detect clauses that are redundant
8797
       * according to the pathkey machinery.
8798
       */
8799
0
      sortop = get_ordering_op_for_equality_op(in_oper, false);
8800
0
      if (OidIsValid(sortop))
8801
0
      {
8802
0
        Oid     eqop;
8803
0
        SortGroupClause *sortcl;
8804
8805
        /*
8806
         * The Unique node will need equality operators.  Normally
8807
         * these are the same as the IN clause operators, but if those
8808
         * are cross-type operators then the equality operators are
8809
         * the ones for the IN clause operators' RHS datatype.
8810
         */
8811
0
        eqop = get_equality_op_for_ordering_op(sortop, NULL);
8812
0
        if (!OidIsValid(eqop)) /* shouldn't happen */
8813
0
          elog(ERROR, "could not find equality operator for ordering operator %u",
8814
0
             sortop);
8815
8816
0
        sortcl = makeNode(SortGroupClause);
8817
0
        sortcl->tleSortGroupRef = assignSortGroupRef(tle, newtlist);
8818
0
        sortcl->eqop = eqop;
8819
0
        sortcl->sortop = sortop;
8820
0
        sortcl->reverse_sort = false;
8821
0
        sortcl->nulls_first = false;
8822
0
        sortcl->hashable = false;  /* no need to make this accurate */
8823
0
        sortList = lappend(sortList, sortcl);
8824
8825
        /*
8826
         * At each step, convert the SortGroupClause list to pathkey
8827
         * form.  If the just-added SortGroupClause is redundant, the
8828
         * result will be shorter than the SortGroupClause list.
8829
         */
8830
0
        sortPathkeys = make_pathkeys_for_sortclauses(root, sortList,
8831
0
                               newtlist);
8832
0
        if (list_length(sortPathkeys) != list_length(sortList))
8833
0
        {
8834
          /* Drop the redundant SortGroupClause */
8835
0
          sortList = list_delete_last(sortList);
8836
0
          Assert(list_length(sortPathkeys) == list_length(sortList));
8837
          /* Undo tlist addition, if we made one */
8838
0
          if (made_tle)
8839
0
          {
8840
0
            newtlist = list_delete_last(newtlist);
8841
0
            nextresno--;
8842
0
          }
8843
          /* We need not consider this clause for hashing, either */
8844
0
          continue;
8845
0
        }
8846
0
      }
8847
0
      else if (sjinfo->semi_can_btree) /* shouldn't happen */
8848
0
        elog(ERROR, "could not find ordering operator for equality operator %u",
8849
0
           in_oper);
8850
8851
0
      if (sjinfo->semi_can_hash)
8852
0
      {
8853
        /* Create a GROUP BY list for the Agg node to use */
8854
0
        Oid     eq_oper;
8855
0
        SortGroupClause *groupcl;
8856
8857
        /*
8858
         * Get the hashable equality operators for the Agg node to
8859
         * use. Normally these are the same as the IN clause
8860
         * operators, but if those are cross-type operators then the
8861
         * equality operators are the ones for the IN clause
8862
         * operators' RHS datatype.
8863
         */
8864
0
        if (!get_compatible_hash_operators(in_oper, NULL, &eq_oper))
8865
0
          elog(ERROR, "could not find compatible hash operator for operator %u",
8866
0
             in_oper);
8867
8868
0
        groupcl = makeNode(SortGroupClause);
8869
0
        groupcl->tleSortGroupRef = assignSortGroupRef(tle, newtlist);
8870
0
        groupcl->eqop = eq_oper;
8871
0
        groupcl->sortop = sortop;
8872
0
        groupcl->reverse_sort = false;
8873
0
        groupcl->nulls_first = false;
8874
0
        groupcl->hashable = true;
8875
0
        groupClause = lappend(groupClause, groupcl);
8876
0
      }
8877
0
    }
8878
8879
    /*
8880
     * Done building the sortPathkeys and groupClause.  But the
8881
     * sortPathkeys are bogus if not all the clauses were sortable.
8882
     */
8883
0
    if (!sjinfo->semi_can_btree)
8884
0
      sortPathkeys = NIL;
8885
8886
    /*
8887
     * It can happen that all the RHS columns are equated to constants.
8888
     * We'd have to do something special to unique-ify in that case, and
8889
     * it's such an unlikely-in-the-real-world case that it's not worth
8890
     * the effort.  So just punt if we found no columns to unique-ify.
8891
     */
8892
0
    if (sortPathkeys == NIL && groupClause == NIL)
8893
0
    {
8894
0
      MemoryContextSwitchTo(oldcontext);
8895
0
      return NULL;
8896
0
    }
8897
8898
    /* Convert the required targetlist back to PathTarget form */
8899
0
    unique_rel->reltarget = create_pathtarget(root, newtlist);
8900
0
  }
8901
8902
  /* build unique paths based on input rel's pathlist */
8903
0
  create_final_unique_paths(root, rel, sortPathkeys, groupClause,
8904
0
                sjinfo, unique_rel);
8905
8906
  /* build unique paths based on input rel's partial_pathlist */
8907
0
  create_partial_unique_paths(root, rel, sortPathkeys, groupClause,
8908
0
                sjinfo, unique_rel);
8909
8910
  /* Now choose the best path(s) */
8911
0
  set_cheapest(unique_rel);
8912
8913
  /*
8914
   * There shouldn't be any partial paths for the unique relation;
8915
   * otherwise, we won't be able to properly guarantee uniqueness.
8916
   */
8917
0
  Assert(unique_rel->partial_pathlist == NIL);
8918
8919
  /* Cache the result */
8920
0
  rel->unique_rel = unique_rel;
8921
0
  rel->unique_pathkeys = sortPathkeys;
8922
0
  rel->unique_groupclause = groupClause;
8923
8924
0
  MemoryContextSwitchTo(oldcontext);
8925
8926
0
  return unique_rel;
8927
0
}
8928
8929
/*
8930
 * create_final_unique_paths
8931
 *    Create unique paths in 'unique_rel' based on 'input_rel' pathlist
8932
 */
8933
static void
8934
create_final_unique_paths(PlannerInfo *root, RelOptInfo *input_rel,
8935
              List *sortPathkeys, List *groupClause,
8936
              SpecialJoinInfo *sjinfo, RelOptInfo *unique_rel)
8937
0
{
8938
0
  Path     *cheapest_input_path = input_rel->cheapest_total_path;
8939
8940
  /* Estimate number of output rows */
8941
0
  unique_rel->rows = estimate_num_groups(root,
8942
0
                       sjinfo->semi_rhs_exprs,
8943
0
                       cheapest_input_path->rows,
8944
0
                       NULL,
8945
0
                       NULL);
8946
8947
  /* Consider sort-based implementations, if possible. */
8948
0
  if (sjinfo->semi_can_btree)
8949
0
  {
8950
0
    ListCell   *lc;
8951
8952
    /*
8953
     * Use any available suitably-sorted path as input, and also consider
8954
     * sorting the cheapest-total path and incremental sort on any paths
8955
     * with presorted keys.
8956
     *
8957
     * To save planning time, we ignore parameterized input paths unless
8958
     * they are the cheapest-total path.
8959
     */
8960
0
    foreach(lc, input_rel->pathlist)
8961
0
    {
8962
0
      Path     *input_path = (Path *) lfirst(lc);
8963
0
      Path     *path;
8964
0
      bool    is_sorted;
8965
0
      int     presorted_keys;
8966
8967
      /*
8968
       * Ignore parameterized paths that are not the cheapest-total
8969
       * path.
8970
       */
8971
0
      if (input_path->param_info &&
8972
0
        input_path != cheapest_input_path)
8973
0
        continue;
8974
8975
0
      is_sorted = pathkeys_count_contained_in(sortPathkeys,
8976
0
                          input_path->pathkeys,
8977
0
                          &presorted_keys);
8978
8979
      /*
8980
       * Ignore paths that are not suitably or partially sorted, unless
8981
       * they are the cheapest total path (no need to deal with paths
8982
       * which have presorted keys when incremental sort is disabled).
8983
       */
8984
0
      if (!is_sorted && input_path != cheapest_input_path &&
8985
0
        (presorted_keys == 0 || !enable_incremental_sort))
8986
0
        continue;
8987
8988
      /*
8989
       * Make a separate ProjectionPath in case we need a Result node.
8990
       */
8991
0
      path = (Path *) create_projection_path(root,
8992
0
                           unique_rel,
8993
0
                           input_path,
8994
0
                           unique_rel->reltarget);
8995
8996
0
      if (!is_sorted)
8997
0
      {
8998
        /*
8999
         * We've no need to consider both a sort and incremental sort.
9000
         * We'll just do a sort if there are no presorted keys and an
9001
         * incremental sort when there are presorted keys.
9002
         */
9003
0
        if (presorted_keys == 0 || !enable_incremental_sort)
9004
0
          path = (Path *) create_sort_path(root,
9005
0
                           unique_rel,
9006
0
                           path,
9007
0
                           sortPathkeys,
9008
0
                           -1.0);
9009
0
        else
9010
0
          path = (Path *) create_incremental_sort_path(root,
9011
0
                                 unique_rel,
9012
0
                                 path,
9013
0
                                 sortPathkeys,
9014
0
                                 presorted_keys,
9015
0
                                 -1.0);
9016
0
      }
9017
9018
0
      path = (Path *) create_unique_path(root, unique_rel, path,
9019
0
                         list_length(sortPathkeys),
9020
0
                         unique_rel->rows);
9021
9022
0
      add_path(unique_rel, path);
9023
0
    }
9024
0
  }
9025
9026
  /* Consider hash-based implementation, if possible. */
9027
0
  if (sjinfo->semi_can_hash)
9028
0
  {
9029
0
    Path     *path;
9030
9031
    /*
9032
     * Make a separate ProjectionPath in case we need a Result node.
9033
     */
9034
0
    path = (Path *) create_projection_path(root,
9035
0
                         unique_rel,
9036
0
                         cheapest_input_path,
9037
0
                         unique_rel->reltarget);
9038
9039
0
    path = (Path *) create_agg_path(root,
9040
0
                    unique_rel,
9041
0
                    path,
9042
0
                    cheapest_input_path->pathtarget,
9043
0
                    AGG_HASHED,
9044
0
                    AGGSPLIT_SIMPLE,
9045
0
                    groupClause,
9046
0
                    NIL,
9047
0
                    NULL,
9048
0
                    unique_rel->rows);
9049
9050
0
    add_path(unique_rel, path);
9051
0
  }
9052
0
}
9053
9054
/*
9055
 * create_partial_unique_paths
9056
 *    Create unique paths in 'unique_rel' based on 'input_rel' partial_pathlist
9057
 */
9058
static void
9059
create_partial_unique_paths(PlannerInfo *root, RelOptInfo *input_rel,
9060
              List *sortPathkeys, List *groupClause,
9061
              SpecialJoinInfo *sjinfo, RelOptInfo *unique_rel)
9062
0
{
9063
0
  RelOptInfo *partial_unique_rel;
9064
0
  Path     *cheapest_partial_path;
9065
9066
  /* nothing to do when there are no partial paths in the input rel */
9067
0
  if (!input_rel->consider_parallel || input_rel->partial_pathlist == NIL)
9068
0
    return;
9069
9070
  /*
9071
   * nothing to do if there's anything in the targetlist that's
9072
   * parallel-restricted.
9073
   */
9074
0
  if (!is_parallel_safe(root, (Node *) unique_rel->reltarget->exprs))
9075
0
    return;
9076
9077
0
  cheapest_partial_path = linitial(input_rel->partial_pathlist);
9078
9079
0
  partial_unique_rel = makeNode(RelOptInfo);
9080
0
  memcpy(partial_unique_rel, input_rel, sizeof(RelOptInfo));
9081
9082
  /*
9083
   * clear path info
9084
   */
9085
0
  partial_unique_rel->pathlist = NIL;
9086
0
  partial_unique_rel->ppilist = NIL;
9087
0
  partial_unique_rel->partial_pathlist = NIL;
9088
0
  partial_unique_rel->cheapest_startup_path = NULL;
9089
0
  partial_unique_rel->cheapest_total_path = NULL;
9090
0
  partial_unique_rel->cheapest_parameterized_paths = NIL;
9091
9092
  /* Estimate number of output rows */
9093
0
  partial_unique_rel->rows = estimate_num_groups(root,
9094
0
                           sjinfo->semi_rhs_exprs,
9095
0
                           cheapest_partial_path->rows,
9096
0
                           NULL,
9097
0
                           NULL);
9098
0
  partial_unique_rel->reltarget = unique_rel->reltarget;
9099
9100
  /* Consider sort-based implementations, if possible. */
9101
0
  if (sjinfo->semi_can_btree)
9102
0
  {
9103
0
    ListCell   *lc;
9104
9105
    /*
9106
     * Use any available suitably-sorted path as input, and also consider
9107
     * sorting the cheapest partial path and incremental sort on any paths
9108
     * with presorted keys.
9109
     */
9110
0
    foreach(lc, input_rel->partial_pathlist)
9111
0
    {
9112
0
      Path     *input_path = (Path *) lfirst(lc);
9113
0
      Path     *path;
9114
0
      bool    is_sorted;
9115
0
      int     presorted_keys;
9116
9117
0
      is_sorted = pathkeys_count_contained_in(sortPathkeys,
9118
0
                          input_path->pathkeys,
9119
0
                          &presorted_keys);
9120
9121
      /*
9122
       * Ignore paths that are not suitably or partially sorted, unless
9123
       * they are the cheapest partial path (no need to deal with paths
9124
       * which have presorted keys when incremental sort is disabled).
9125
       */
9126
0
      if (!is_sorted && input_path != cheapest_partial_path &&
9127
0
        (presorted_keys == 0 || !enable_incremental_sort))
9128
0
        continue;
9129
9130
      /*
9131
       * Make a separate ProjectionPath in case we need a Result node.
9132
       */
9133
0
      path = (Path *) create_projection_path(root,
9134
0
                           partial_unique_rel,
9135
0
                           input_path,
9136
0
                           partial_unique_rel->reltarget);
9137
9138
0
      if (!is_sorted)
9139
0
      {
9140
        /*
9141
         * We've no need to consider both a sort and incremental sort.
9142
         * We'll just do a sort if there are no presorted keys and an
9143
         * incremental sort when there are presorted keys.
9144
         */
9145
0
        if (presorted_keys == 0 || !enable_incremental_sort)
9146
0
          path = (Path *) create_sort_path(root,
9147
0
                           partial_unique_rel,
9148
0
                           path,
9149
0
                           sortPathkeys,
9150
0
                           -1.0);
9151
0
        else
9152
0
          path = (Path *) create_incremental_sort_path(root,
9153
0
                                 partial_unique_rel,
9154
0
                                 path,
9155
0
                                 sortPathkeys,
9156
0
                                 presorted_keys,
9157
0
                                 -1.0);
9158
0
      }
9159
9160
0
      path = (Path *) create_unique_path(root, partial_unique_rel, path,
9161
0
                         list_length(sortPathkeys),
9162
0
                         partial_unique_rel->rows);
9163
9164
0
      add_partial_path(partial_unique_rel, path);
9165
0
    }
9166
0
  }
9167
9168
  /* Consider hash-based implementation, if possible. */
9169
0
  if (sjinfo->semi_can_hash)
9170
0
  {
9171
0
    Path     *path;
9172
9173
    /*
9174
     * Make a separate ProjectionPath in case we need a Result node.
9175
     */
9176
0
    path = (Path *) create_projection_path(root,
9177
0
                         partial_unique_rel,
9178
0
                         cheapest_partial_path,
9179
0
                         partial_unique_rel->reltarget);
9180
9181
0
    path = (Path *) create_agg_path(root,
9182
0
                    partial_unique_rel,
9183
0
                    path,
9184
0
                    cheapest_partial_path->pathtarget,
9185
0
                    AGG_HASHED,
9186
0
                    AGGSPLIT_SIMPLE,
9187
0
                    groupClause,
9188
0
                    NIL,
9189
0
                    NULL,
9190
0
                    partial_unique_rel->rows);
9191
9192
0
    add_partial_path(partial_unique_rel, path);
9193
0
  }
9194
9195
0
  if (partial_unique_rel->partial_pathlist != NIL)
9196
0
  {
9197
0
    generate_useful_gather_paths(root, partial_unique_rel, true);
9198
0
    set_cheapest(partial_unique_rel);
9199
9200
    /*
9201
     * Finally, create paths to unique-ify the final result.  This step is
9202
     * needed to remove any duplicates due to combining rows from parallel
9203
     * workers.
9204
     */
9205
0
    create_final_unique_paths(root, partial_unique_rel,
9206
0
                  sortPathkeys, groupClause,
9207
0
                  sjinfo, unique_rel);
9208
0
  }
9209
0
}
9210
9211
/*
9212
 * Choose a unique name for some subroot.
9213
 *
9214
 * Modifies glob->subplanNames to track names already used.
9215
 */
9216
char *
9217
choose_plan_name(PlannerGlobal *glob, const char *name, bool always_number)
9218
0
{
9219
0
  unsigned  n;
9220
9221
  /*
9222
   * If a numeric suffix is not required, then search the list of
9223
   * previously-assigned names for a match. If none is found, then we can
9224
   * use the provided name without modification.
9225
   */
9226
0
  if (!always_number)
9227
0
  {
9228
0
    bool    found = false;
9229
9230
0
    foreach_ptr(char, subplan_name, glob->subplanNames)
9231
0
    {
9232
0
      if (strcmp(subplan_name, name) == 0)
9233
0
      {
9234
0
        found = true;
9235
0
        break;
9236
0
      }
9237
0
    }
9238
9239
0
    if (!found)
9240
0
    {
9241
      /* pstrdup here is just to avoid cast-away-const */
9242
0
      char     *chosen_name = pstrdup(name);
9243
9244
0
      glob->subplanNames = lappend(glob->subplanNames, chosen_name);
9245
0
      return chosen_name;
9246
0
    }
9247
0
  }
9248
9249
  /*
9250
   * If a numeric suffix is required or if the un-suffixed name is already
9251
   * in use, then loop until we find a positive integer that produces a
9252
   * novel name.
9253
   */
9254
0
  for (n = 1; true; ++n)
9255
0
  {
9256
0
    char     *proposed_name = psprintf("%s_%u", name, n);
9257
0
    bool    found = false;
9258
9259
0
    foreach_ptr(char, subplan_name, glob->subplanNames)
9260
0
    {
9261
0
      if (strcmp(subplan_name, proposed_name) == 0)
9262
0
      {
9263
0
        found = true;
9264
0
        break;
9265
0
      }
9266
0
    }
9267
9268
0
    if (!found)
9269
0
    {
9270
0
      glob->subplanNames = lappend(glob->subplanNames, proposed_name);
9271
0
      return proposed_name;
9272
0
    }
9273
9274
0
    pfree(proposed_name);
9275
0
  }
9276
0
}