plancache.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*-------------------------------------------------------------------------
  2. *
  3. * plancache.h
  4. * Plan cache definitions.
  5. *
  6. * See plancache.c for comments.
  7. *
  8. * Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group
  9. * Portions Copyright (c) 1994, Regents of the University of California
  10. *
  11. * src/include/utils/plancache.h
  12. *
  13. *-------------------------------------------------------------------------
  14. */
  15. #ifndef PLANCACHE_H
  16. #define PLANCACHE_H
  17. #include "access/tupdesc.h"
  18. #include "nodes/params.h"
  19. #define CACHEDPLANSOURCE_MAGIC 195726186
  20. #define CACHEDPLAN_MAGIC 953717834
  21. /*
  22. * CachedPlanSource (which might better have been called CachedQuery)
  23. * represents a SQL query that we expect to use multiple times. It stores
  24. * the query source text, the raw parse tree, and the analyzed-and-rewritten
  25. * query tree, as well as adjunct data. Cache invalidation can happen as a
  26. * result of DDL affecting objects used by the query. In that case we discard
  27. * the analyzed-and-rewritten query tree, and rebuild it when next needed.
  28. *
  29. * An actual execution plan, represented by CachedPlan, is derived from the
  30. * CachedPlanSource when we need to execute the query. The plan could be
  31. * either generic (usable with any set of plan parameters) or custom (for a
  32. * specific set of parameters). plancache.c contains the logic that decides
  33. * which way to do it for any particular execution. If we are using a generic
  34. * cached plan then it is meant to be re-used across multiple executions, so
  35. * callers must always treat CachedPlans as read-only.
  36. *
  37. * Once successfully built and "saved", CachedPlanSources typically live
  38. * for the life of the backend, although they can be dropped explicitly.
  39. * CachedPlans are reference-counted and go away automatically when the last
  40. * reference is dropped. A CachedPlan can outlive the CachedPlanSource it
  41. * was created from.
  42. *
  43. * An "unsaved" CachedPlanSource can be used for generating plans, but it
  44. * lives in transient storage and will not be updated in response to sinval
  45. * events.
  46. *
  47. * CachedPlans made from saved CachedPlanSources are likewise in permanent
  48. * storage, so to avoid memory leaks, the reference-counted references to them
  49. * must be held in permanent data structures or ResourceOwners. CachedPlans
  50. * made from unsaved CachedPlanSources are in children of the caller's
  51. * memory context, so references to them should not be longer-lived than
  52. * that context. (Reference counting is somewhat pro forma in that case,
  53. * though it may be useful if the CachedPlan can be discarded early.)
  54. *
  55. * A CachedPlanSource has two associated memory contexts: one that holds the
  56. * struct itself, the query source text and the raw parse tree, and another
  57. * context that holds the rewritten query tree and associated data. This
  58. * allows the query tree to be discarded easily when it is invalidated.
  59. *
  60. * Some callers wish to use the CachedPlan API even with one-shot queries
  61. * that have no reason to be saved at all. We therefore support a "oneshot"
  62. * variant that does no data copying or invalidation checking. In this case
  63. * there are no separate memory contexts: the CachedPlanSource struct and
  64. * all subsidiary data live in the caller's CurrentMemoryContext, and there
  65. * is no way to free memory short of clearing that entire context. A oneshot
  66. * plan is always treated as unsaved.
  67. *
  68. * Note: the string referenced by commandTag is not subsidiary storage;
  69. * it is assumed to be a compile-time-constant string. As with portals,
  70. * commandTag shall be NULL if and only if the original query string (before
  71. * rewriting) was an empty string.
  72. */
  73. typedef struct CachedPlanSource
  74. {
  75. int magic; /* should equal CACHEDPLANSOURCE_MAGIC */
  76. Node *raw_parse_tree; /* output of raw_parser(), or NULL */
  77. const char *query_string; /* source text of query */
  78. const char *commandTag; /* command tag (a constant!), or NULL */
  79. Oid *param_types; /* array of parameter type OIDs, or NULL */
  80. int num_params; /* length of param_types array */
  81. ParserSetupHook parserSetup; /* alternative parameter spec method */
  82. void *parserSetupArg;
  83. int cursor_options; /* cursor options used for planning */
  84. bool fixed_result; /* disallow change in result tupdesc? */
  85. TupleDesc resultDesc; /* result type; NULL = doesn't return tuples */
  86. MemoryContext context; /* memory context holding all above */
  87. /* These fields describe the current analyzed-and-rewritten query tree: */
  88. List *query_list; /* list of Query nodes, or NIL if not valid */
  89. List *relationOids; /* OIDs of relations the queries depend on */
  90. List *invalItems; /* other dependencies, as PlanInvalItems */
  91. struct OverrideSearchPath *search_path; /* search_path used for
  92. * parsing and planning */
  93. MemoryContext query_context; /* context holding the above, or NULL */
  94. Oid rewriteRoleId; /* Role ID we did rewriting for */
  95. bool rewriteRowSecurity; /* row_security used during rewrite */
  96. bool dependsOnRLS; /* is rewritten query specific to the above? */
  97. /* If we have a generic plan, this is a reference-counted link to it: */
  98. struct CachedPlan *gplan; /* generic plan, or NULL if not valid */
  99. /* Some state flags: */
  100. bool is_oneshot; /* is it a "oneshot" plan? */
  101. bool is_complete; /* has CompleteCachedPlan been done? */
  102. bool is_saved; /* has CachedPlanSource been "saved"? */
  103. bool is_valid; /* is the query_list currently valid? */
  104. int generation; /* increments each time we create a plan */
  105. /* If CachedPlanSource has been saved, it is a member of a global list */
  106. struct CachedPlanSource *next_saved; /* list link, if so */
  107. /* State kept to help decide whether to use custom or generic plans: */
  108. double generic_cost; /* cost of generic plan, or -1 if not known */
  109. double total_custom_cost; /* total cost of custom plans so far */
  110. int num_custom_plans; /* number of plans included in total */
  111. } CachedPlanSource;
  112. /*
  113. * CachedPlan represents an execution plan derived from a CachedPlanSource.
  114. * The reference count includes both the link from the parent CachedPlanSource
  115. * (if any), and any active plan executions, so the plan can be discarded
  116. * exactly when refcount goes to zero. Both the struct itself and the
  117. * subsidiary data live in the context denoted by the context field.
  118. * This makes it easy to free a no-longer-needed cached plan. (However,
  119. * if is_oneshot is true, the context does not belong solely to the CachedPlan
  120. * so no freeing is possible.)
  121. */
  122. typedef struct CachedPlan
  123. {
  124. int magic; /* should equal CACHEDPLAN_MAGIC */
  125. List *stmt_list; /* list of statement nodes (PlannedStmts and
  126. * bare utility statements) */
  127. bool is_oneshot; /* is it a "oneshot" plan? */
  128. bool is_saved; /* is CachedPlan in a long-lived context? */
  129. bool is_valid; /* is the stmt_list currently valid? */
  130. Oid planRoleId; /* Role ID the plan was created for */
  131. bool dependsOnRole; /* is plan specific to that role? */
  132. TransactionId saved_xmin; /* if valid, replan when TransactionXmin
  133. * changes from this value */
  134. int generation; /* parent's generation number for this plan */
  135. int refcount; /* count of live references to this struct */
  136. MemoryContext context; /* context containing this CachedPlan */
  137. } CachedPlan;
  138. extern void InitPlanCache(void);
  139. extern void ResetPlanCache(void);
  140. extern CachedPlanSource *CreateCachedPlan(Node *raw_parse_tree,
  141. const char *query_string,
  142. const char *commandTag);
  143. extern CachedPlanSource *CreateOneShotCachedPlan(Node *raw_parse_tree,
  144. const char *query_string,
  145. const char *commandTag);
  146. extern void CompleteCachedPlan(CachedPlanSource *plansource,
  147. List *querytree_list,
  148. MemoryContext querytree_context,
  149. Oid *param_types,
  150. int num_params,
  151. ParserSetupHook parserSetup,
  152. void *parserSetupArg,
  153. int cursor_options,
  154. bool fixed_result);
  155. extern void SaveCachedPlan(CachedPlanSource *plansource);
  156. extern void DropCachedPlan(CachedPlanSource *plansource);
  157. extern void CachedPlanSetParentContext(CachedPlanSource *plansource,
  158. MemoryContext newcontext);
  159. extern CachedPlanSource *CopyCachedPlan(CachedPlanSource *plansource);
  160. extern bool CachedPlanIsValid(CachedPlanSource *plansource);
  161. extern List *CachedPlanGetTargetList(CachedPlanSource *plansource);
  162. extern CachedPlan *GetCachedPlan(CachedPlanSource *plansource,
  163. ParamListInfo boundParams,
  164. bool useResOwner);
  165. extern void ReleaseCachedPlan(CachedPlan *plan, bool useResOwner);
  166. #endif /* PLANCACHE_H */