PostgreSQL Source Code  git master
prepagg.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * prepagg.c
4  * Routines to preprocess aggregate function calls
5  *
6  * If there are identical aggregate calls in the query, they only need to
7  * be computed once. Also, some aggregate functions can share the same
8  * transition state, so that we only need to call the final function for
9  * them separately. These optimizations are independent of how the
10  * aggregates are executed.
11  *
12  * preprocess_aggrefs() detects those cases, creates AggInfo and
13  * AggTransInfo structs for each aggregate and transition state that needs
14  * to be computed, and sets the 'aggno' and 'transno' fields in the Aggrefs
15  * accordingly. It also resolves polymorphic transition types, and sets
16  * the 'aggtranstype' fields accordingly.
17  *
18  * XXX: The AggInfo and AggTransInfo structs are thrown away after
19  * planning, so executor startup has to perform some of the same lookups
20  * of transition functions and initial values that we do here. One day, we
21  * might want to carry that information to the Agg nodes to save the effort
22  * at executor startup. The Agg nodes are constructed much later in the
23  * planning, however, so it's not trivial.
24  *
25  * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
26  * Portions Copyright (c) 1994, Regents of the University of California
27  *
28  *
29  * IDENTIFICATION
30  * src/backend/optimizer/prep/prepagg.c
31  *
32  *-------------------------------------------------------------------------
33  */
34 
35 #include "postgres.h"
36 
37 #include "access/htup_details.h"
38 #include "catalog/pg_aggregate.h"
39 #include "catalog/pg_type.h"
40 #include "nodes/nodeFuncs.h"
41 #include "nodes/pathnodes.h"
42 #include "optimizer/clauses.h"
43 #include "optimizer/cost.h"
44 #include "optimizer/optimizer.h"
45 #include "optimizer/plancat.h"
46 #include "optimizer/prep.h"
47 #include "parser/parse_agg.h"
48 #include "utils/builtins.h"
49 #include "utils/datum.h"
50 #include "utils/fmgroids.h"
51 #include "utils/lsyscache.h"
52 #include "utils/memutils.h"
53 #include "utils/syscache.h"
54 
55 static bool preprocess_aggrefs_walker(Node *node, PlannerInfo *root);
56 static int find_compatible_agg(PlannerInfo *root, Aggref *newagg,
57  List **same_input_transnos);
58 static int find_compatible_trans(PlannerInfo *root, Aggref *newagg,
59  bool shareable,
60  Oid aggtransfn, Oid aggtranstype,
61  int transtypeLen, bool transtypeByVal,
62  Oid aggcombinefn,
63  Oid aggserialfn, Oid aggdeserialfn,
64  Datum initValue, bool initValueIsNull,
65  List *transnos);
66 static Datum GetAggInitVal(Datum textInitVal, Oid transtype);
67 
68 /* -----------------
69  * Resolve the transition type of all Aggrefs, and determine which Aggrefs
70  * can share aggregate or transition state.
71  *
72  * Information about the aggregates and transition functions are collected
73  * in the root->agginfos and root->aggtransinfos lists. The 'aggtranstype',
74  * 'aggno', and 'aggtransno' fields of each Aggref are filled in.
75  *
76  * NOTE: This modifies the Aggrefs in the input expression in-place!
77  *
78  * We try to optimize by detecting duplicate aggregate functions so that
79  * their state and final values are re-used, rather than needlessly being
80  * re-calculated independently. We also detect aggregates that are not
81  * the same, but which can share the same transition state.
82  *
83  * Scenarios:
84  *
85  * 1. Identical aggregate function calls appear in the query:
86  *
87  * SELECT SUM(x) FROM ... HAVING SUM(x) > 0
88  *
89  * Since these aggregates are identical, we only need to calculate
90  * the value once. Both aggregates will share the same 'aggno' value.
91  *
92  * 2. Two different aggregate functions appear in the query, but the
93  * aggregates have the same arguments, transition functions and
94  * initial values (and, presumably, different final functions):
95  *
96  * SELECT AVG(x), STDDEV(x) FROM ...
97  *
98  * In this case we must create a new AggInfo for the varying aggregate,
99  * and we need to call the final functions separately, but we need
100  * only run the transition function once. (This requires that the
101  * final functions be nondestructive of the transition state, but
102  * that's required anyway for other reasons.)
103  *
104  * For either of these optimizations to be valid, all aggregate properties
105  * used in the transition phase must be the same, including any modifiers
106  * such as ORDER BY, DISTINCT and FILTER, and the arguments mustn't
107  * contain any volatile functions.
108  * -----------------
109  */
110 void
112 {
113  (void) preprocess_aggrefs_walker(clause, root);
114 }
115 
116 static void
118 {
119  HeapTuple aggTuple;
120  Form_pg_aggregate aggform;
121  Oid aggtransfn;
122  Oid aggfinalfn;
123  Oid aggcombinefn;
124  Oid aggserialfn;
125  Oid aggdeserialfn;
126  Oid aggtranstype;
127  int32 aggtranstypmod;
128  int32 aggtransspace;
129  bool shareable;
130  int aggno;
131  int transno;
132  List *same_input_transnos;
133  int16 resulttypeLen;
134  bool resulttypeByVal;
135  Datum textInitVal;
137  bool initValueIsNull;
138  bool transtypeByVal;
139  int16 transtypeLen;
140  Oid inputTypes[FUNC_MAX_ARGS];
141  int numArguments;
142 
143  Assert(aggref->agglevelsup == 0);
144 
145  /*
146  * Fetch info about the aggregate from pg_aggregate. Note it's correct to
147  * ignore the moving-aggregate variant, since what we're concerned with
148  * here is aggregates not window functions.
149  */
150  aggTuple = SearchSysCache1(AGGFNOID,
151  ObjectIdGetDatum(aggref->aggfnoid));
152  if (!HeapTupleIsValid(aggTuple))
153  elog(ERROR, "cache lookup failed for aggregate %u",
154  aggref->aggfnoid);
155  aggform = (Form_pg_aggregate) GETSTRUCT(aggTuple);
156  aggtransfn = aggform->aggtransfn;
157  aggfinalfn = aggform->aggfinalfn;
158  aggcombinefn = aggform->aggcombinefn;
159  aggserialfn = aggform->aggserialfn;
160  aggdeserialfn = aggform->aggdeserialfn;
161  aggtranstype = aggform->aggtranstype;
162  aggtransspace = aggform->aggtransspace;
163 
164  /*
165  * Resolve the possibly-polymorphic aggregate transition type.
166  */
167 
168  /* extract argument types (ignoring any ORDER BY expressions) */
169  numArguments = get_aggregate_argtypes(aggref, inputTypes);
170 
171  /* resolve actual type of transition state, if polymorphic */
172  aggtranstype = resolve_aggregate_transtype(aggref->aggfnoid,
173  aggtranstype,
174  inputTypes,
175  numArguments);
176  aggref->aggtranstype = aggtranstype;
177 
178  /*
179  * If transition state is of same type as first aggregated input, assume
180  * it's the same typmod (same width) as well. This works for cases like
181  * MAX/MIN and is probably somewhat reasonable otherwise.
182  */
183  aggtranstypmod = -1;
184  if (aggref->args)
185  {
186  TargetEntry *tle = (TargetEntry *) linitial(aggref->args);
187 
188  if (aggtranstype == exprType((Node *) tle->expr))
189  aggtranstypmod = exprTypmod((Node *) tle->expr);
190  }
191 
192  /*
193  * If finalfn is marked read-write, we can't share transition states; but
194  * it is okay to share states for AGGMODIFY_SHAREABLE aggs.
195  *
196  * In principle, in a partial aggregate, we could share the transition
197  * state even if the final function is marked as read-write, because the
198  * partial aggregate doesn't execute the final function. But it's too
199  * early to know whether we're going perform a partial aggregate.
200  */
201  shareable = (aggform->aggfinalmodify != AGGMODIFY_READ_WRITE);
202 
203  /* get info about the output value's datatype */
204  get_typlenbyval(aggref->aggtype,
205  &resulttypeLen,
206  &resulttypeByVal);
207 
208  /* get initial value */
209  textInitVal = SysCacheGetAttr(AGGFNOID, aggTuple,
210  Anum_pg_aggregate_agginitval,
211  &initValueIsNull);
212  if (initValueIsNull)
213  initValue = (Datum) 0;
214  else
215  initValue = GetAggInitVal(textInitVal, aggtranstype);
216 
217  ReleaseSysCache(aggTuple);
218 
219  /*
220  * 1. See if this is identical to another aggregate function call that
221  * we've seen already.
222  */
223  aggno = find_compatible_agg(root, aggref, &same_input_transnos);
224  if (aggno != -1)
225  {
226  AggInfo *agginfo = list_nth_node(AggInfo, root->agginfos, aggno);
227 
228  agginfo->aggrefs = lappend(agginfo->aggrefs, aggref);
229  transno = agginfo->transno;
230  }
231  else
232  {
233  AggInfo *agginfo = makeNode(AggInfo);
234 
235  agginfo->finalfn_oid = aggfinalfn;
236  agginfo->aggrefs = list_make1(aggref);
237  agginfo->shareable = shareable;
238 
239  aggno = list_length(root->agginfos);
240  root->agginfos = lappend(root->agginfos, agginfo);
241 
242  /*
243  * Count it, and check for cases requiring ordered input. Note that
244  * ordered-set aggs always have nonempty aggorder. Any ordered-input
245  * case also defeats partial aggregation.
246  */
247  if (aggref->aggorder != NIL || aggref->aggdistinct != NIL)
248  {
249  root->numOrderedAggs++;
250  root->hasNonPartialAggs = true;
251  }
252 
253  get_typlenbyval(aggtranstype,
254  &transtypeLen,
255  &transtypeByVal);
256 
257  /*
258  * 2. See if this aggregate can share transition state with another
259  * aggregate that we've initialized already.
260  */
261  transno = find_compatible_trans(root, aggref, shareable,
262  aggtransfn, aggtranstype,
263  transtypeLen, transtypeByVal,
264  aggcombinefn,
265  aggserialfn, aggdeserialfn,
266  initValue, initValueIsNull,
267  same_input_transnos);
268  if (transno == -1)
269  {
270  AggTransInfo *transinfo = makeNode(AggTransInfo);
271 
272  transinfo->args = aggref->args;
273  transinfo->aggfilter = aggref->aggfilter;
274  transinfo->transfn_oid = aggtransfn;
275  transinfo->combinefn_oid = aggcombinefn;
276  transinfo->serialfn_oid = aggserialfn;
277  transinfo->deserialfn_oid = aggdeserialfn;
278  transinfo->aggtranstype = aggtranstype;
279  transinfo->aggtranstypmod = aggtranstypmod;
280  transinfo->transtypeLen = transtypeLen;
281  transinfo->transtypeByVal = transtypeByVal;
282  transinfo->aggtransspace = aggtransspace;
283  transinfo->initValue = initValue;
284  transinfo->initValueIsNull = initValueIsNull;
285 
286  transno = list_length(root->aggtransinfos);
287  root->aggtransinfos = lappend(root->aggtransinfos, transinfo);
288 
289  /*
290  * Check whether partial aggregation is feasible, unless we
291  * already found out that we can't do it.
292  */
293  if (!root->hasNonPartialAggs)
294  {
295  /*
296  * If there is no combine function, then partial aggregation
297  * is not possible.
298  */
299  if (!OidIsValid(transinfo->combinefn_oid))
300  root->hasNonPartialAggs = true;
301 
302  /*
303  * If we have any aggs with transtype INTERNAL then we must
304  * check whether they have serialization/deserialization
305  * functions; if not, we can't serialize partial-aggregation
306  * results.
307  */
308  else if (transinfo->aggtranstype == INTERNALOID &&
309  (!OidIsValid(transinfo->serialfn_oid) ||
310  !OidIsValid(transinfo->deserialfn_oid)))
311  root->hasNonSerialAggs = true;
312  }
313  }
314  agginfo->transno = transno;
315  }
316 
317  /*
318  * Fill in the fields in the Aggref (aggtranstype was set above already)
319  */
320  aggref->aggno = aggno;
321  aggref->aggtransno = transno;
322 }
323 
324 static bool
326 {
327  if (node == NULL)
328  return false;
329  if (IsA(node, Aggref))
330  {
331  Aggref *aggref = (Aggref *) node;
332 
333  preprocess_aggref(aggref, root);
334 
335  /*
336  * We assume that the parser checked that there are no aggregates (of
337  * this level anyway) in the aggregated arguments, direct arguments,
338  * or filter clause. Hence, we need not recurse into any of them.
339  */
340  return false;
341  }
342  Assert(!IsA(node, SubLink));
344  (void *) root);
345 }
346 
347 
348 /*
349  * find_compatible_agg - search for a previously initialized per-Agg struct
350  *
351  * Searches the previously looked at aggregates to find one which is compatible
352  * with this one, with the same input parameters. If no compatible aggregate
353  * can be found, returns -1.
354  *
355  * As a side-effect, this also collects a list of existing, shareable per-Trans
356  * structs with matching inputs. If no identical Aggref is found, the list is
357  * passed later to find_compatible_trans, to see if we can at least reuse
358  * the state value of another aggregate.
359  */
360 static int
362  List **same_input_transnos)
363 {
364  ListCell *lc;
365  int aggno;
366 
367  *same_input_transnos = NIL;
368 
369  /* we mustn't reuse the aggref if it contains volatile function calls */
370  if (contain_volatile_functions((Node *) newagg))
371  return -1;
372 
373  /*
374  * Search through the list of already seen aggregates. If we find an
375  * existing identical aggregate call, then we can re-use that one. While
376  * searching, we'll also collect a list of Aggrefs with the same input
377  * parameters. If no matching Aggref is found, the caller can potentially
378  * still re-use the transition state of one of them. (At this stage we
379  * just compare the parsetrees; whether different aggregates share the
380  * same transition function will be checked later.)
381  */
382  aggno = -1;
383  foreach(lc, root->agginfos)
384  {
385  AggInfo *agginfo = lfirst_node(AggInfo, lc);
386  Aggref *existingRef;
387 
388  aggno++;
389 
390  existingRef = linitial_node(Aggref, agginfo->aggrefs);
391 
392  /* all of the following must be the same or it's no match */
393  if (newagg->inputcollid != existingRef->inputcollid ||
394  newagg->aggtranstype != existingRef->aggtranstype ||
395  newagg->aggstar != existingRef->aggstar ||
396  newagg->aggvariadic != existingRef->aggvariadic ||
397  newagg->aggkind != existingRef->aggkind ||
398  !equal(newagg->args, existingRef->args) ||
399  !equal(newagg->aggorder, existingRef->aggorder) ||
400  !equal(newagg->aggdistinct, existingRef->aggdistinct) ||
401  !equal(newagg->aggfilter, existingRef->aggfilter))
402  continue;
403 
404  /* if it's the same aggregate function then report exact match */
405  if (newagg->aggfnoid == existingRef->aggfnoid &&
406  newagg->aggtype == existingRef->aggtype &&
407  newagg->aggcollid == existingRef->aggcollid &&
408  equal(newagg->aggdirectargs, existingRef->aggdirectargs))
409  {
410  list_free(*same_input_transnos);
411  *same_input_transnos = NIL;
412  return aggno;
413  }
414 
415  /*
416  * Not identical, but it had the same inputs. If the final function
417  * permits sharing, return its transno to the caller, in case we can
418  * re-use its per-trans state. (If there's already sharing going on,
419  * we might report a transno more than once. find_compatible_trans is
420  * cheap enough that it's not worth spending cycles to avoid that.)
421  */
422  if (agginfo->shareable)
423  *same_input_transnos = lappend_int(*same_input_transnos,
424  agginfo->transno);
425  }
426 
427  return -1;
428 }
429 
430 /*
431  * find_compatible_trans - search for a previously initialized per-Trans
432  * struct
433  *
434  * Searches the list of transnos for a per-Trans struct with the same
435  * transition function and initial condition. (The inputs have already been
436  * verified to match.)
437  */
438 static int
439 find_compatible_trans(PlannerInfo *root, Aggref *newagg, bool shareable,
440  Oid aggtransfn, Oid aggtranstype,
441  int transtypeLen, bool transtypeByVal,
442  Oid aggcombinefn,
443  Oid aggserialfn, Oid aggdeserialfn,
444  Datum initValue, bool initValueIsNull,
445  List *transnos)
446 {
447  ListCell *lc;
448 
449  /* If this aggregate can't share transition states, give up */
450  if (!shareable)
451  return -1;
452 
453  foreach(lc, transnos)
454  {
455  int transno = lfirst_int(lc);
457  root->aggtransinfos,
458  transno);
459 
460  /*
461  * if the transfns or transition state types are not the same then the
462  * state can't be shared.
463  */
464  if (aggtransfn != pertrans->transfn_oid ||
465  aggtranstype != pertrans->aggtranstype)
466  continue;
467 
468  /*
469  * The serialization and deserialization functions must match, if
470  * present, as we're unable to share the trans state for aggregates
471  * which will serialize or deserialize into different formats.
472  * Remember that these will be InvalidOid if they're not required for
473  * this agg node.
474  */
475  if (aggserialfn != pertrans->serialfn_oid ||
476  aggdeserialfn != pertrans->deserialfn_oid)
477  continue;
478 
479  /*
480  * Combine function must also match. We only care about the combine
481  * function with partial aggregates, but it's too early in the
482  * planning to know if we will do partial aggregation, so be
483  * conservative.
484  */
485  if (aggcombinefn != pertrans->combinefn_oid)
486  continue;
487 
488  /*
489  * Check that the initial condition matches, too.
490  */
491  if (initValueIsNull && pertrans->initValueIsNull)
492  return transno;
493 
494  if (!initValueIsNull && !pertrans->initValueIsNull &&
495  datumIsEqual(initValue, pertrans->initValue,
496  transtypeByVal, transtypeLen))
497  return transno;
498  }
499  return -1;
500 }
501 
502 static Datum
503 GetAggInitVal(Datum textInitVal, Oid transtype)
504 {
505  Oid typinput,
506  typioparam;
507  char *strInitVal;
508  Datum initVal;
509 
510  getTypeInputInfo(transtype, &typinput, &typioparam);
511  strInitVal = TextDatumGetCString(textInitVal);
512  initVal = OidInputFunctionCall(typinput, strInitVal,
513  typioparam, -1);
514  pfree(strInitVal);
515  return initVal;
516 }
517 
518 
519 /*
520  * get_agg_clause_costs
521  * Process the PlannerInfo's 'aggtransinfos' and 'agginfos' lists
522  * accumulating the cost information about them.
523  *
524  * 'aggsplit' tells us the expected partial-aggregation mode, which affects
525  * the cost estimates.
526  *
527  * NOTE that the costs are ADDED to those already in *costs ... so the caller
528  * is responsible for zeroing the struct initially.
529  *
530  * For each AggTransInfo, we add the cost of an aggregate transition using
531  * either the transfn or combinefn depending on the 'aggsplit' value. We also
532  * account for the costs of any aggfilters and any serializations and
533  * deserializations of the transition state and also estimate the total space
534  * needed for the transition states as if each aggregate's state was stored in
535  * memory concurrently (as would be done in a HashAgg plan).
536  *
537  * For each AggInfo in the 'agginfos' list we add the cost of running the
538  * final function and the direct args, if any.
539  */
540 void
542 {
543  ListCell *lc;
544 
545  foreach(lc, root->aggtransinfos)
546  {
547  AggTransInfo *transinfo = lfirst_node(AggTransInfo, lc);
548 
549  /*
550  * Add the appropriate component function execution costs to
551  * appropriate totals.
552  */
553  if (DO_AGGSPLIT_COMBINE(aggsplit))
554  {
555  /* charge for combining previously aggregated states */
556  add_function_cost(root, transinfo->combinefn_oid, NULL,
557  &costs->transCost);
558  }
559  else
560  add_function_cost(root, transinfo->transfn_oid, NULL,
561  &costs->transCost);
562  if (DO_AGGSPLIT_DESERIALIZE(aggsplit) &&
563  OidIsValid(transinfo->deserialfn_oid))
564  add_function_cost(root, transinfo->deserialfn_oid, NULL,
565  &costs->transCost);
566  if (DO_AGGSPLIT_SERIALIZE(aggsplit) &&
567  OidIsValid(transinfo->serialfn_oid))
568  add_function_cost(root, transinfo->serialfn_oid, NULL,
569  &costs->finalCost);
570 
571  /*
572  * These costs are incurred only by the initial aggregate node, so we
573  * mustn't include them again at upper levels.
574  */
575  if (!DO_AGGSPLIT_COMBINE(aggsplit))
576  {
577  /* add the input expressions' cost to per-input-row costs */
578  QualCost argcosts;
579 
580  cost_qual_eval_node(&argcosts, (Node *) transinfo->args, root);
581  costs->transCost.startup += argcosts.startup;
582  costs->transCost.per_tuple += argcosts.per_tuple;
583 
584  /*
585  * Add any filter's cost to per-input-row costs.
586  *
587  * XXX Ideally we should reduce input expression costs according
588  * to filter selectivity, but it's not clear it's worth the
589  * trouble.
590  */
591  if (transinfo->aggfilter)
592  {
593  cost_qual_eval_node(&argcosts, (Node *) transinfo->aggfilter,
594  root);
595  costs->transCost.startup += argcosts.startup;
596  costs->transCost.per_tuple += argcosts.per_tuple;
597  }
598  }
599 
600  /*
601  * If the transition type is pass-by-value then it doesn't add
602  * anything to the required size of the hashtable. If it is
603  * pass-by-reference then we have to add the estimated size of the
604  * value itself, plus palloc overhead.
605  */
606  if (!transinfo->transtypeByVal)
607  {
608  int32 avgwidth;
609 
610  /* Use average width if aggregate definition gave one */
611  if (transinfo->aggtransspace > 0)
612  avgwidth = transinfo->aggtransspace;
613  else if (transinfo->transfn_oid == F_ARRAY_APPEND)
614  {
615  /*
616  * If the transition function is array_append(), it'll use an
617  * expanded array as transvalue, which will occupy at least
618  * ALLOCSET_SMALL_INITSIZE and possibly more. Use that as the
619  * estimate for lack of a better idea.
620  */
621  avgwidth = ALLOCSET_SMALL_INITSIZE;
622  }
623  else
624  {
625  avgwidth = get_typavgwidth(transinfo->aggtranstype, transinfo->aggtranstypmod);
626  }
627 
628  avgwidth = MAXALIGN(avgwidth);
629  costs->transitionSpace += avgwidth + 2 * sizeof(void *);
630  }
631  else if (transinfo->aggtranstype == INTERNALOID)
632  {
633  /*
634  * INTERNAL transition type is a special case: although INTERNAL
635  * is pass-by-value, it's almost certainly being used as a pointer
636  * to some large data structure. The aggregate definition can
637  * provide an estimate of the size. If it doesn't, then we assume
638  * ALLOCSET_DEFAULT_INITSIZE, which is a good guess if the data is
639  * being kept in a private memory context, as is done by
640  * array_agg() for instance.
641  */
642  if (transinfo->aggtransspace > 0)
643  costs->transitionSpace += transinfo->aggtransspace;
644  else
646  }
647  }
648 
649  foreach(lc, root->agginfos)
650  {
651  AggInfo *agginfo = lfirst_node(AggInfo, lc);
652  Aggref *aggref = linitial_node(Aggref, agginfo->aggrefs);
653 
654  /*
655  * Add the appropriate component function execution costs to
656  * appropriate totals.
657  */
658  if (!DO_AGGSPLIT_SKIPFINAL(aggsplit) &&
659  OidIsValid(agginfo->finalfn_oid))
660  add_function_cost(root, agginfo->finalfn_oid, NULL,
661  &costs->finalCost);
662 
663  /*
664  * If there are direct arguments, treat their evaluation cost like the
665  * cost of the finalfn.
666  */
667  if (aggref->aggdirectargs)
668  {
669  QualCost argcosts;
670 
671  cost_qual_eval_node(&argcosts, (Node *) aggref->aggdirectargs,
672  root);
673  costs->finalCost.startup += argcosts.startup;
674  costs->finalCost.per_tuple += argcosts.per_tuple;
675  }
676  }
677 }
#define TextDatumGetCString(d)
Definition: builtins.h:86
signed short int16
Definition: c.h:429
#define MAXALIGN(LEN)
Definition: c.h:747
signed int int32
Definition: c.h:430
#define OidIsValid(objectId)
Definition: c.h:711
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:448
void cost_qual_eval_node(QualCost *cost, Node *qual, PlannerInfo *root)
Definition: costsize.c:4394
bool datumIsEqual(Datum value1, Datum value2, bool typByVal, int typLen)
Definition: datum.c:223
#define ERROR
Definition: elog.h:35
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:225
Datum OidInputFunctionCall(Oid functionId, char *str, Oid typioparam, int32 typmod)
Definition: fmgr.c:1630
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define GETSTRUCT(TUP)
Definition: htup_details.h:649
static int initValue(long lng_val)
Definition: informix.c:677
Assert(fmt[strlen(fmt) - 1] !='\n')
List * lappend(List *list, void *datum)
Definition: list.c:338
List * lappend_int(List *list, int datum)
Definition: list.c:356
void list_free(List *list)
Definition: list.c:1545
void get_typlenbyval(Oid typid, int16 *typlen, bool *typbyval)
Definition: lsyscache.c:2209
void getTypeInputInfo(Oid type, Oid *typInput, Oid *typIOParam)
Definition: lsyscache.c:2832
int32 get_typavgwidth(Oid typid, int32 typmod)
Definition: lsyscache.c:2536
void pfree(void *pointer)
Definition: mcxt.c:1306
#define ALLOCSET_SMALL_INITSIZE
Definition: memutils.h:161
#define ALLOCSET_DEFAULT_INITSIZE
Definition: memutils.h:151
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:43
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:266
#define expression_tree_walker(n, w, c)
Definition: nodeFuncs.h:151
#define DO_AGGSPLIT_SKIPFINAL(as)
Definition: nodes.h:383
#define IsA(nodeptr, _type_)
Definition: nodes.h:168
#define DO_AGGSPLIT_DESERIALIZE(as)
Definition: nodes.h:385
#define DO_AGGSPLIT_COMBINE(as)
Definition: nodes.h:382
#define DO_AGGSPLIT_SERIALIZE(as)
Definition: nodes.h:384
AggSplit
Definition: nodes.h:372
#define makeNode(_type_)
Definition: nodes.h:165
Oid resolve_aggregate_transtype(Oid aggfuncid, Oid aggtranstype, Oid *inputTypes, int numArguments)
Definition: parse_agg.c:1916
int get_aggregate_argtypes(Aggref *aggref, Oid *inputTypes)
Definition: parse_agg.c:1890
FormData_pg_aggregate * Form_pg_aggregate
Definition: pg_aggregate.h:109
#define FUNC_MAX_ARGS
#define lfirst_node(type, lc)
Definition: pg_list.h:174
static int list_length(const List *l)
Definition: pg_list.h:150
#define linitial_node(type, l)
Definition: pg_list.h:179
#define NIL
Definition: pg_list.h:66
#define lfirst_int(lc)
Definition: pg_list.h:171
#define list_make1(x1)
Definition: pg_list.h:210
#define linitial(l)
Definition: pg_list.h:176
#define list_nth_node(type, list, n)
Definition: pg_list.h:325
void add_function_cost(PlannerInfo *root, Oid funcid, Node *node, QualCost *cost)
Definition: plancat.c:2007
uintptr_t Datum
Definition: postgres.h:412
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:600
unsigned int Oid
Definition: postgres_ext.h:31
static bool preprocess_aggrefs_walker(Node *node, PlannerInfo *root)
Definition: prepagg.c:325
static Datum GetAggInitVal(Datum textInitVal, Oid transtype)
Definition: prepagg.c:503
void get_agg_clause_costs(PlannerInfo *root, AggSplit aggsplit, AggClauseCosts *costs)
Definition: prepagg.c:541
static int find_compatible_agg(PlannerInfo *root, Aggref *newagg, List **same_input_transnos)
Definition: prepagg.c:361
static int find_compatible_trans(PlannerInfo *root, Aggref *newagg, bool shareable, Oid aggtransfn, Oid aggtranstype, int transtypeLen, bool transtypeByVal, Oid aggcombinefn, Oid aggserialfn, Oid aggdeserialfn, Datum initValue, bool initValueIsNull, List *transnos)
Definition: prepagg.c:439
static void preprocess_aggref(Aggref *aggref, PlannerInfo *root)
Definition: prepagg.c:117
void preprocess_aggrefs(PlannerInfo *root, Node *clause)
Definition: prepagg.c:111
QualCost finalCost
Definition: pathnodes.h:61
Size transitionSpace
Definition: pathnodes.h:62
QualCost transCost
Definition: pathnodes.h:60
bool shareable
Definition: pathnodes.h:3172
List * aggrefs
Definition: pathnodes.h:3163
int transno
Definition: pathnodes.h:3166
Oid finalfn_oid
Definition: pathnodes.h:3175
List * args
Definition: pathnodes.h:3192
int32 aggtransspace
Definition: pathnodes.h:3216
bool transtypeByVal
Definition: pathnodes.h:3213
Oid combinefn_oid
Definition: pathnodes.h:3205
Oid deserialfn_oid
Definition: pathnodes.h:3202
int32 aggtranstypmod
Definition: pathnodes.h:3211
int transtypeLen
Definition: pathnodes.h:3212
bool initValueIsNull
Definition: pathnodes.h:3220
Oid serialfn_oid
Definition: pathnodes.h:3199
Oid aggtranstype
Definition: pathnodes.h:3208
Expr * aggfilter
Definition: pathnodes.h:3193
bool aggstar
Definition: primnodes.h:409
Oid aggfnoid
Definition: primnodes.h:373
List * aggdistinct
Definition: primnodes.h:403
List * aggdirectargs
Definition: primnodes.h:394
bool aggvariadic
Definition: primnodes.h:415
char aggkind
Definition: primnodes.h:418
int aggtransno
Definition: primnodes.h:433
Index agglevelsup
Definition: primnodes.h:424
List * args
Definition: primnodes.h:397
Expr * aggfilter
Definition: primnodes.h:406
Oid inputcollid
Definition: primnodes.h:382
Oid aggtype
Definition: primnodes.h:376
List * aggorder
Definition: primnodes.h:400
Oid aggcollid
Definition: primnodes.h:379
int aggno
Definition: primnodes.h:430
Definition: pg_list.h:52
Definition: nodes.h:118
List * aggtransinfos
Definition: pathnodes.h:474
int numOrderedAggs
Definition: pathnodes.h:476
bool hasNonPartialAggs
Definition: pathnodes.h:478
List * agginfos
Definition: pathnodes.h:472
bool hasNonSerialAggs
Definition: pathnodes.h:480
Cost per_tuple
Definition: pathnodes.h:48
Cost startup
Definition: pathnodes.h:47
Expr * expr
Definition: primnodes.h:1555
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1221
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1173
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1434
@ AGGFNOID
Definition: syscache.h:34