gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

243 line
11KB

  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-2019, 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/primnodes.h"
  36. struct PlannerInfo; /* avoid including pathnodes.h here */
  37. struct IndexOptInfo;
  38. struct SpecialJoinInfo;
  39. /*
  40. * The Simplify request allows the support function to perform plan-time
  41. * simplification of a call to its target function. For example, a varchar
  42. * length coercion that does not decrease the allowed length of its argument
  43. * could be replaced by a RelabelType node, or "x + 0" could be replaced by
  44. * "x". This is invoked during the planner's constant-folding pass, so the
  45. * function's arguments can be presumed already simplified.
  46. *
  47. * The planner's PlannerInfo "root" is typically not needed, but can be
  48. * consulted if it's necessary to obtain info about Vars present in
  49. * the given node tree. Beware that root could be NULL in some usages.
  50. *
  51. * "fcall" will be a FuncExpr invoking the support function's target
  52. * function. (This is true even if the original parsetree node was an
  53. * operator call; a FuncExpr is synthesized for this purpose.)
  54. *
  55. * The result should be a semantically-equivalent transformed node tree,
  56. * or NULL if no simplification could be performed. Do *not* return or
  57. * modify *fcall, as it isn't really a separately allocated Node. But
  58. * it's okay to use fcall->args, or parts of it, in the result tree.
  59. */
  60. typedef struct SupportRequestSimplify
  61. {
  62. NodeTag type;
  63. struct PlannerInfo *root; /* Planner's infrastructure */
  64. FuncExpr *fcall; /* Function call to be simplified */
  65. } SupportRequestSimplify;
  66. /*
  67. * The Selectivity request allows the support function to provide a
  68. * selectivity estimate for a function appearing at top level of a WHERE
  69. * clause (so it applies only to functions returning boolean).
  70. *
  71. * The input arguments are the same as are supplied to operator restriction
  72. * and join estimators, except that we unify those two APIs into just one
  73. * request type. See clause_selectivity() for the details.
  74. *
  75. * If an estimate can be made, store it into the "selectivity" field and
  76. * return the address of the SupportRequestSelectivity node; the estimate
  77. * must be between 0 and 1 inclusive. Return NULL if no estimate can be
  78. * made (in which case the planner will fall back to a default estimate,
  79. * traditionally 1/3).
  80. *
  81. * If the target function is being used as the implementation of an operator,
  82. * the support function will not be used for this purpose; the operator's
  83. * restriction or join estimator is consulted instead.
  84. */
  85. typedef struct SupportRequestSelectivity
  86. {
  87. NodeTag type;
  88. /* Input fields: */
  89. struct PlannerInfo *root; /* Planner's infrastructure */
  90. Oid funcid; /* function we are inquiring about */
  91. List *args; /* pre-simplified arguments to function */
  92. Oid inputcollid; /* function's input collation */
  93. bool is_join; /* is this a join or restriction case? */
  94. int varRelid; /* if restriction, RTI of target relation */
  95. JoinType jointype; /* if join, outer join type */
  96. struct SpecialJoinInfo *sjinfo; /* if outer join, info about join */
  97. /* Output fields: */
  98. Selectivity selectivity; /* returned selectivity estimate */
  99. } SupportRequestSelectivity;
  100. /*
  101. * The Cost request allows the support function to provide an execution
  102. * cost estimate for its target function. The cost estimate can include
  103. * both a one-time (query startup) component and a per-execution component.
  104. * The estimate should *not* include the costs of evaluating the target
  105. * function's arguments, only the target function itself.
  106. *
  107. * The "node" argument is normally the parse node that is invoking the
  108. * target function. This is a FuncExpr in the simplest case, but it could
  109. * also be an OpExpr, DistinctExpr, NullIfExpr, or WindowFunc, or possibly
  110. * other cases in future. NULL is passed if the function cannot presume
  111. * its arguments to be equivalent to what the calling node presents as
  112. * arguments; that happens for, e.g., aggregate support functions and
  113. * per-column comparison operators used by RowExprs.
  114. *
  115. * If an estimate can be made, store it into the cost fields and return the
  116. * address of the SupportRequestCost node. Return NULL if no estimate can be
  117. * made, in which case the planner will rely on the target function's procost
  118. * field. (Note: while procost is automatically scaled by cpu_operator_cost,
  119. * this is not the case for the outputs of the Cost request; the support
  120. * function must scale its results appropriately on its own.)
  121. */
  122. typedef struct SupportRequestCost
  123. {
  124. NodeTag type;
  125. /* Input fields: */
  126. struct PlannerInfo *root; /* Planner's infrastructure (could be NULL) */
  127. Oid funcid; /* function we are inquiring about */
  128. Node *node; /* parse node invoking function, or NULL */
  129. /* Output fields: */
  130. Cost startup; /* one-time cost */
  131. Cost per_tuple; /* per-evaluation cost */
  132. } SupportRequestCost;
  133. /*
  134. * The Rows request allows the support function to provide an output rowcount
  135. * estimate for its target function (so it applies only to set-returning
  136. * functions).
  137. *
  138. * The "node" argument is the parse node that is invoking the target function;
  139. * currently this will always be a FuncExpr or OpExpr.
  140. *
  141. * If an estimate can be made, store it into the rows field and return the
  142. * address of the SupportRequestRows node. Return NULL if no estimate can be
  143. * made, in which case the planner will rely on the target function's prorows
  144. * field.
  145. */
  146. typedef struct SupportRequestRows
  147. {
  148. NodeTag type;
  149. /* Input fields: */
  150. struct PlannerInfo *root; /* Planner's infrastructure (could be NULL) */
  151. Oid funcid; /* function we are inquiring about */
  152. Node *node; /* parse node invoking function */
  153. /* Output fields: */
  154. double rows; /* number of rows expected to be returned */
  155. } SupportRequestRows;
  156. /*
  157. * The IndexCondition request allows the support function to generate
  158. * a directly-indexable condition based on a target function call that is
  159. * not itself indexable. The target function call must appear at the top
  160. * level of WHERE or JOIN/ON, so this applies only to functions returning
  161. * boolean.
  162. *
  163. * The "node" argument is the parse node that is invoking the target function;
  164. * currently this will always be a FuncExpr or OpExpr. The call is made
  165. * only if at least one function argument matches an index column's variable
  166. * or expression. "indexarg" identifies the matching argument (it's the
  167. * argument's zero-based index in the node's args list).
  168. *
  169. * If the transformation is possible, return a List of directly-indexable
  170. * condition expressions, else return NULL. (A List is used because it's
  171. * sometimes useful to generate more than one indexable condition, such as
  172. * when a LIKE with constant prefix gives rise to both >= and < conditions.)
  173. *
  174. * "Directly indexable" means that the condition must be directly executable
  175. * by the index machinery. Typically this means that it is a binary OpExpr
  176. * with the index column value on the left, a pseudo-constant on the right,
  177. * and an operator that is in the index column's operator family. Other
  178. * possibilities include RowCompareExpr, ScalarArrayOpExpr, and NullTest,
  179. * depending on the index type; but those seem less likely to be useful for
  180. * derived index conditions. "Pseudo-constant" means that the right-hand
  181. * expression must not contain any volatile functions, nor any Vars of the
  182. * table the index is for; use is_pseudo_constant_for_index() to check this.
  183. * (Note: if the passed "node" is an OpExpr, the core planner already verified
  184. * that the non-indexkey operand is pseudo-constant; but when the "node"
  185. * is a FuncExpr, it does not check, since it doesn't know which of the
  186. * function's arguments you might need to use in an index comparison value.)
  187. *
  188. * In many cases, an index condition can be generated but it is weaker than
  189. * the function condition itself; for example, a LIKE with a constant prefix
  190. * can produce an index range check based on the prefix, but we still need
  191. * to execute the LIKE operator to verify the rest of the pattern. We say
  192. * that such an index condition is "lossy". When returning an index condition,
  193. * you should set the "lossy" request field to true if the condition is lossy,
  194. * or false if it is an exact equivalent of the function's result. The core
  195. * code will initialize that field to true, which is the common case.
  196. *
  197. * It is important to verify that the index operator family is the correct
  198. * one for the condition you want to generate. Core support functions tend
  199. * to use the known OID of a built-in opfamily for this, but extensions need
  200. * to work harder, since their OIDs aren't fixed. A possibly workable
  201. * answer for an index on an extension datatype is to verify the index AM's
  202. * OID instead, and then assume that there's only one relevant opclass for
  203. * your datatype so the opfamily must be the right one. Generating OpExpr
  204. * nodes may also require knowing extension datatype OIDs (often you can
  205. * find these out by applying exprType() to a function argument) and
  206. * operator OIDs (which you can look up using get_opfamily_member).
  207. */
  208. typedef struct SupportRequestIndexCondition
  209. {
  210. NodeTag type;
  211. /* Input fields: */
  212. struct PlannerInfo *root; /* Planner's infrastructure */
  213. Oid funcid; /* function we are inquiring about */
  214. Node *node; /* parse node invoking function */
  215. int indexarg; /* index of function arg matching indexcol */
  216. struct IndexOptInfo *index; /* planner's info about target index */
  217. int indexcol; /* index of target index column (0-based) */
  218. Oid opfamily; /* index column's operator family */
  219. Oid indexcollation; /* index column's collation */
  220. /* Output fields: */
  221. bool lossy; /* set to false if index condition is an exact
  222. * equivalent of the function call */
  223. } SupportRequestIndexCondition;
  224. #endif /* SUPPORTNODES_H */
上海开阖软件有限公司 沪ICP备12045867号-1