supportnodes.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*-------------------------------------------------------------------------
  2. *
  3. * supportnodes.h
  4. * Definitions for planner support functions.
  5. *
  6. * This file defines the API for "planner support functions", which
  7. * are SQL functions (normally written in C) that can be attached to
  8. * another "target" function to give the system additional knowledge
  9. * about the target function. All the current capabilities have to do
  10. * with planning queries that use the target function, though it is
  11. * possible that future extensions will add functionality to be invoked
  12. * by the parser or executor.
  13. *
  14. * A support function must have the SQL signature
  15. * supportfn(internal) returns internal
  16. * The argument is a pointer to one of the Node types defined in this file.
  17. * The result is usually also a Node pointer, though its type depends on
  18. * which capability is being invoked. In all cases, a NULL pointer result
  19. * (that's PG_RETURN_POINTER(NULL), not PG_RETURN_NULL()) indicates that
  20. * the support function cannot do anything useful for the given request.
  21. * Support functions must return a NULL pointer, not fail, if they do not
  22. * recognize the request node type or cannot handle the given case; this
  23. * allows for future extensions of the set of request cases.
  24. *
  25. *
  26. * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
  27. * Portions Copyright (c) 1994, Regents of the University of California
  28. *
  29. * src/include/nodes/supportnodes.h
  30. *
  31. *-------------------------------------------------------------------------
  32. */
  33. #ifndef SUPPORTNODES_H
  34. #define SUPPORTNODES_H
  35. #include "nodes/plannodes.h"
  36. struct PlannerInfo; /* avoid including pathnodes.h here */
  37. struct IndexOptInfo;
  38. struct SpecialJoinInfo;
  39. struct WindowClause;
  40. /*
  41. * The Simplify request allows the support function to perform plan-time
  42. * simplification of a call to its target function. For example, a varchar
  43. * length coercion that does not decrease the allowed length of its argument
  44. * could be replaced by a RelabelType node, or "x + 0" could be replaced by
  45. * "x". This is invoked during the planner's constant-folding pass, so the
  46. * function's arguments can be presumed already simplified.
  47. *
  48. * The planner's PlannerInfo "root" is typically not needed, but can be
  49. * consulted if it's necessary to obtain info about Vars present in
  50. * the given node tree. Beware that root could be NULL in some usages.
  51. *
  52. * "fcall" will be a FuncExpr invoking the support function's target
  53. * function. (This is true even if the original parsetree node was an
  54. * operator call; a FuncExpr is synthesized for this purpose.)
  55. *
  56. * The result should be a semantically-equivalent transformed node tree,
  57. * or NULL if no simplification could be performed. Do *not* return or
  58. * modify *fcall, as it isn't really a separately allocated Node. But
  59. * it's okay to use fcall->args, or parts of it, in the result tree.
  60. */
  61. typedef struct SupportRequestSimplify
  62. {
  63. NodeTag type;
  64. struct PlannerInfo *root; /* Planner's infrastructure */
  65. FuncExpr *fcall; /* Function call to be simplified */
  66. } SupportRequestSimplify;
  67. /*
  68. * The Selectivity request allows the support function to provide a
  69. * selectivity estimate for a function appearing at top level of a WHERE
  70. * clause (so it applies only to functions returning boolean).
  71. *
  72. * The input arguments are the same as are supplied to operator restriction
  73. * and join estimators, except that we unify those two APIs into just one
  74. * request type. See clause_selectivity() for the details.
  75. *
  76. * If an estimate can be made, store it into the "selectivity" field and
  77. * return the address of the SupportRequestSelectivity node; the estimate
  78. * must be between 0 and 1 inclusive. Return NULL if no estimate can be
  79. * made (in which case the planner will fall back to a default estimate,
  80. * traditionally 1/3).
  81. *
  82. * If the target function is being used as the implementation of an operator,
  83. * the support function will not be used for this purpose; the operator's
  84. * restriction or join estimator is consulted instead.
  85. */
  86. typedef struct SupportRequestSelectivity
  87. {
  88. NodeTag type;
  89. /* Input fields: */
  90. struct PlannerInfo *root; /* Planner's infrastructure */
  91. Oid funcid; /* function we are inquiring about */
  92. List *args; /* pre-simplified arguments to function */
  93. Oid inputcollid; /* function's input collation */
  94. bool is_join; /* is this a join or restriction case? */
  95. int varRelid; /* if restriction, RTI of target relation */
  96. JoinType jointype; /* if join, outer join type */
  97. struct SpecialJoinInfo *sjinfo; /* if outer join, info about join */
  98. /* Output fields: */
  99. Selectivity selectivity; /* returned selectivity estimate */
  100. } SupportRequestSelectivity;
  101. /*
  102. * The Cost request allows the support function to provide an execution
  103. * cost estimate for its target function. The cost estimate can include
  104. * both a one-time (query startup) component and a per-execution component.
  105. * The estimate should *not* include the costs of evaluating the target
  106. * function's arguments, only the target function itself.
  107. *
  108. * The "node" argument is normally the parse node that is invoking the
  109. * target function. This is a FuncExpr in the simplest case, but it could
  110. * also be an OpExpr, DistinctExpr, NullIfExpr, or WindowFunc, or possibly
  111. * other cases in future. NULL is passed if the function cannot presume
  112. * its arguments to be equivalent to what the calling node presents as
  113. * arguments; that happens for, e.g., aggregate support functions and
  114. * per-column comparison operators used by RowExprs.
  115. *
  116. * If an estimate can be made, store it into the cost fields and return the
  117. * address of the SupportRequestCost node. Return NULL if no estimate can be
  118. * made, in which case the planner will rely on the target function's procost
  119. * field. (Note: while procost is automatically scaled by cpu_operator_cost,
  120. * this is not the case for the outputs of the Cost request; the support
  121. * function must scale its results appropriately on its own.)
  122. */
  123. typedef struct SupportRequestCost
  124. {
  125. NodeTag type;
  126. /* Input fields: */
  127. struct PlannerInfo *root; /* Planner's infrastructure (could be NULL) */
  128. Oid funcid; /* function we are inquiring about */
  129. Node *node; /* parse node invoking function, or NULL */
  130. /* Output fields: */
  131. Cost startup; /* one-time cost */
  132. Cost per_tuple; /* per-evaluation cost */
  133. } SupportRequestCost;
  134. /*
  135. * The Rows request allows the support function to provide an output rowcount
  136. * estimate for its target function (so it applies only to set-returning
  137. * functions).
  138. *
  139. * The "node" argument is the parse node that is invoking the target function;
  140. * currently this will always be a FuncExpr or OpExpr.
  141. *
  142. * If an estimate can be made, store it into the rows field and return the
  143. * address of the SupportRequestRows node. Return NULL if no estimate can be
  144. * made, in which case the planner will rely on the target function's prorows
  145. * field.
  146. */
  147. typedef struct SupportRequestRows
  148. {
  149. NodeTag type;
  150. /* Input fields: */
  151. struct PlannerInfo *root; /* Planner's infrastructure (could be NULL) */
  152. Oid funcid; /* function we are inquiring about */
  153. Node *node; /* parse node invoking function */
  154. /* Output fields: */
  155. double rows; /* number of rows expected to be returned */
  156. } SupportRequestRows;
  157. /*
  158. * The IndexCondition request allows the support function to generate
  159. * a directly-indexable condition based on a target function call that is
  160. * not itself indexable. The target function call must appear at the top
  161. * level of WHERE or JOIN/ON, so this applies only to functions returning
  162. * boolean.
  163. *
  164. * The "node" argument is the parse node that is invoking the target function;
  165. * currently this will always be a FuncExpr or OpExpr. The call is made
  166. * only if at least one function argument matches an index column's variable
  167. * or expression. "indexarg" identifies the matching argument (it's the
  168. * argument's zero-based index in the node's args list).
  169. *
  170. * If the transformation is possible, return a List of directly-indexable
  171. * condition expressions, else return NULL. (A List is used because it's
  172. * sometimes useful to generate more than one indexable condition, such as
  173. * when a LIKE with constant prefix gives rise to both >= and < conditions.)
  174. *
  175. * "Directly indexable" means that the condition must be directly executable
  176. * by the index machinery. Typically this means that it is a binary OpExpr
  177. * with the index column value on the left, a pseudo-constant on the right,
  178. * and an operator that is in the index column's operator family. Other
  179. * possibilities include RowCompareExpr, ScalarArrayOpExpr, and NullTest,
  180. * depending on the index type; but those seem less likely to be useful for
  181. * derived index conditions. "Pseudo-constant" means that the right-hand
  182. * expression must not contain any volatile functions, nor any Vars of the
  183. * table the index is for; use is_pseudo_constant_for_index() to check this.
  184. * (Note: if the passed "node" is an OpExpr, the core planner already verified
  185. * that the non-indexkey operand is pseudo-constant; but when the "node"
  186. * is a FuncExpr, it does not check, since it doesn't know which of the
  187. * function's arguments you might need to use in an index comparison value.)
  188. *
  189. * In many cases, an index condition can be generated but it is weaker than
  190. * the function condition itself; for example, a LIKE with a constant prefix
  191. * can produce an index range check based on the prefix, but we still need
  192. * to execute the LIKE operator to verify the rest of the pattern. We say
  193. * that such an index condition is "lossy". When returning an index condition,
  194. * you should set the "lossy" request field to true if the condition is lossy,
  195. * or false if it is an exact equivalent of the function's result. The core
  196. * code will initialize that field to true, which is the common case.
  197. *
  198. * It is important to verify that the index operator family is the correct
  199. * one for the condition you want to generate. Core support functions tend
  200. * to use the known OID of a built-in opfamily for this, but extensions need
  201. * to work harder, since their OIDs aren't fixed. A possibly workable
  202. * answer for an index on an extension datatype is to verify the index AM's
  203. * OID instead, and then assume that there's only one relevant opclass for
  204. * your datatype so the opfamily must be the right one. Generating OpExpr
  205. * nodes may also require knowing extension datatype OIDs (often you can
  206. * find these out by applying exprType() to a function argument) and
  207. * operator OIDs (which you can look up using get_opfamily_member).
  208. */
  209. typedef struct SupportRequestIndexCondition
  210. {
  211. NodeTag type;
  212. /* Input fields: */
  213. struct PlannerInfo *root; /* Planner's infrastructure */
  214. Oid funcid; /* function we are inquiring about */
  215. Node *node; /* parse node invoking function */
  216. int indexarg; /* index of function arg matching indexcol */
  217. struct IndexOptInfo *index; /* planner's info about target index */
  218. int indexcol; /* index of target index column (0-based) */
  219. Oid opfamily; /* index column's operator family */
  220. Oid indexcollation; /* index column's collation */
  221. /* Output fields: */
  222. bool lossy; /* set to false if index condition is an exact
  223. * equivalent of the function call */
  224. } SupportRequestIndexCondition;
  225. /* ----------
  226. * To support more efficient query execution of any monotonically increasing
  227. * and/or monotonically decreasing window functions, we support calling the
  228. * window function's prosupport function passing along this struct whenever
  229. * the planner sees an OpExpr qual directly reference a window function in a
  230. * subquery. When the planner encounters this, we populate this struct and
  231. * pass it along to the window function's prosupport function so that it can
  232. * evaluate if the given WindowFunc is;
  233. *
  234. * a) monotonically increasing, or
  235. * b) monotonically decreasing, or
  236. * c) both monotonically increasing and decreasing, or
  237. * d) none of the above.
  238. *
  239. * A function that is monotonically increasing can never return a value that
  240. * is lower than a value returned in a "previous call". A monotonically
  241. * decreasing function can never return a value higher than a value returned
  242. * in a previous call. A function that is both must return the same value
  243. * each time.
  244. *
  245. * We define "previous call" to mean a previous call to the same WindowFunc
  246. * struct in the same window partition.
  247. *
  248. * row_number() is an example of a monotonically increasing function. The
  249. * return value will be reset back to 1 in each new partition. An example of
  250. * a monotonically increasing and decreasing function is COUNT(*) OVER ().
  251. * Since there is no ORDER BY clause in this example, all rows in the
  252. * partition are peers and all rows within the partition will be within the
  253. * frame bound. Likewise for COUNT(*) OVER(ORDER BY a ROWS BETWEEN UNBOUNDED
  254. * PRECEDING AND UNBOUNDED FOLLOWING).
  255. *
  256. * COUNT(*) OVER (ORDER BY a ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
  257. * is an example of a monotonically decreasing function.
  258. *
  259. * Implementations must only concern themselves with the given WindowFunc
  260. * being monotonic in a single partition.
  261. *
  262. * Inputs:
  263. * 'window_func' is the pointer to the window function being called.
  264. *
  265. * 'window_clause' pointer to the WindowClause data. Support functions can
  266. * use this to check frame bounds, etc.
  267. *
  268. * Outputs:
  269. * 'monotonic' the resulting MonotonicFunction value for the given input
  270. * window function and window clause.
  271. * ----------
  272. */
  273. typedef struct SupportRequestWFuncMonotonic
  274. {
  275. NodeTag type;
  276. /* Input fields: */
  277. WindowFunc *window_func; /* Pointer to the window function data */
  278. struct WindowClause *window_clause; /* Pointer to the window clause data */
  279. /* Output fields: */
  280. MonotonicFunction monotonic;
  281. } SupportRequestWFuncMonotonic;
  282. #endif /* SUPPORTNODES_H */