2
0

params.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*-------------------------------------------------------------------------
  2. *
  3. * params.h
  4. * Support for finding the values associated with Param nodes.
  5. *
  6. *
  7. * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
  8. * Portions Copyright (c) 1994, Regents of the University of California
  9. *
  10. * src/include/nodes/params.h
  11. *
  12. *-------------------------------------------------------------------------
  13. */
  14. #ifndef PARAMS_H
  15. #define PARAMS_H
  16. /* Forward declarations, to avoid including other headers */
  17. struct Bitmapset;
  18. struct ExprState;
  19. struct Param;
  20. struct ParseState;
  21. /*
  22. * ParamListInfo
  23. *
  24. * ParamListInfo structures are used to pass parameters into the executor
  25. * for parameterized plans. We support two basic approaches to supplying
  26. * parameter values, the "static" way and the "dynamic" way.
  27. *
  28. * In the static approach, per-parameter data is stored in an array of
  29. * ParamExternData structs appended to the ParamListInfo struct.
  30. * Each entry in the array defines the value to be substituted for a
  31. * PARAM_EXTERN parameter. The "paramid" of a PARAM_EXTERN Param
  32. * can range from 1 to numParams.
  33. *
  34. * Although parameter numbers are normally consecutive, we allow
  35. * ptype == InvalidOid to signal an unused array entry.
  36. *
  37. * pflags is a flags field. Currently the only used bit is:
  38. * PARAM_FLAG_CONST signals the planner that it may treat this parameter
  39. * as a constant (i.e., generate a plan that works only for this value
  40. * of the parameter).
  41. *
  42. * In the dynamic approach, all access to parameter values is done through
  43. * hook functions found in the ParamListInfo struct. In this case,
  44. * the ParamExternData array is typically unused and not allocated;
  45. * but the legal range of paramid is still 1 to numParams.
  46. *
  47. * Although the data structure is really an array, not a list, we keep
  48. * the old typedef name to avoid unnecessary code changes.
  49. *
  50. * There are 3 hook functions that can be associated with a ParamListInfo
  51. * structure:
  52. *
  53. * If paramFetch isn't null, it is called to fetch the ParamExternData
  54. * for a particular param ID, rather than accessing the relevant element
  55. * of the ParamExternData array. This supports the case where the array
  56. * isn't there at all, as well as cases where the data in the array
  57. * might be obsolete or lazily evaluated. paramFetch must return the
  58. * address of a ParamExternData struct describing the specified param ID;
  59. * the convention above about ptype == InvalidOid signaling an invalid
  60. * param ID still applies. The returned struct can either be placed in
  61. * the "workspace" supplied by the caller, or it can be in storage
  62. * controlled by the paramFetch hook if that's more convenient.
  63. * (In either case, the struct is not expected to be long-lived.)
  64. * If "speculative" is true, the paramFetch hook should not risk errors
  65. * in trying to fetch the parameter value, and should report an invalid
  66. * parameter instead.
  67. *
  68. * If paramCompile isn't null, then it controls what execExpr.c compiles
  69. * for PARAM_EXTERN Param nodes --- typically, this hook would emit a
  70. * EEOP_PARAM_CALLBACK step. This allows unnecessary work to be
  71. * optimized away in compiled expressions.
  72. *
  73. * If parserSetup isn't null, then it is called to re-instantiate the
  74. * original parsing hooks when a query needs to be re-parsed/planned.
  75. * This is especially useful if the types of parameters might change
  76. * from time to time, since it can replace the need to supply a fixed
  77. * list of parameter types to the parser.
  78. *
  79. * Notice that the paramFetch and paramCompile hooks are actually passed
  80. * the ParamListInfo struct's address; they can therefore access all
  81. * three of the "arg" fields, and the distinction between paramFetchArg
  82. * and paramCompileArg is rather arbitrary.
  83. */
  84. #define PARAM_FLAG_CONST 0x0001 /* parameter is constant */
  85. typedef struct ParamExternData
  86. {
  87. Datum value; /* parameter value */
  88. bool isnull; /* is it NULL? */
  89. uint16 pflags; /* flag bits, see above */
  90. Oid ptype; /* parameter's datatype, or 0 */
  91. } ParamExternData;
  92. typedef struct ParamListInfoData *ParamListInfo;
  93. typedef ParamExternData *(*ParamFetchHook) (ParamListInfo params,
  94. int paramid, bool speculative,
  95. ParamExternData *workspace);
  96. typedef void (*ParamCompileHook) (ParamListInfo params, struct Param *param,
  97. struct ExprState *state,
  98. Datum *resv, bool *resnull);
  99. typedef void (*ParserSetupHook) (struct ParseState *pstate, void *arg);
  100. typedef struct ParamListInfoData
  101. {
  102. ParamFetchHook paramFetch; /* parameter fetch hook */
  103. void *paramFetchArg;
  104. ParamCompileHook paramCompile; /* parameter compile hook */
  105. void *paramCompileArg;
  106. ParserSetupHook parserSetup; /* parser setup hook */
  107. void *parserSetupArg;
  108. char *paramValuesStr; /* params as a single string for errors */
  109. int numParams; /* nominal/maximum # of Params represented */
  110. /*
  111. * params[] may be of length zero if paramFetch is supplied; otherwise it
  112. * must be of length numParams.
  113. */
  114. ParamExternData params[FLEXIBLE_ARRAY_MEMBER];
  115. } ParamListInfoData;
  116. /* ----------------
  117. * ParamExecData
  118. *
  119. * ParamExecData entries are used for executor internal parameters
  120. * (that is, values being passed into or out of a sub-query). The
  121. * paramid of a PARAM_EXEC Param is a (zero-based) index into an
  122. * array of ParamExecData records, which is referenced through
  123. * es_param_exec_vals or ecxt_param_exec_vals.
  124. *
  125. * If execPlan is not NULL, it points to a SubPlanState node that needs
  126. * to be executed to produce the value. (This is done so that we can have
  127. * lazy evaluation of InitPlans: they aren't executed until/unless a
  128. * result value is needed.) Otherwise the value is assumed to be valid
  129. * when needed.
  130. * ----------------
  131. */
  132. typedef struct ParamExecData
  133. {
  134. void *execPlan; /* should be "SubPlanState *" */
  135. Datum value;
  136. bool isnull;
  137. } ParamExecData;
  138. /* type of argument for ParamsErrorCallback */
  139. typedef struct ParamsErrorCbData
  140. {
  141. const char *portalName;
  142. ParamListInfo params;
  143. } ParamsErrorCbData;
  144. /* Functions found in src/backend/nodes/params.c */
  145. extern ParamListInfo makeParamList(int numParams);
  146. extern ParamListInfo copyParamList(ParamListInfo from);
  147. extern Size EstimateParamListSpace(ParamListInfo paramLI);
  148. extern void SerializeParamList(ParamListInfo paramLI, char **start_address);
  149. extern ParamListInfo RestoreParamList(char **start_address);
  150. extern char *BuildParamLogString(ParamListInfo params, char **paramTextValues,
  151. int valueLen);
  152. extern void ParamsErrorCallback(void *arg);
  153. #endif /* PARAMS_H */