portal.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*-------------------------------------------------------------------------
  2. *
  3. * portal.h
  4. * POSTGRES portal definitions.
  5. *
  6. * A portal is an abstraction which represents the execution state of
  7. * a running or runnable query. Portals support both SQL-level CURSORs
  8. * and protocol-level portals.
  9. *
  10. * Scrolling (nonsequential access) and suspension of execution are allowed
  11. * only for portals that contain a single SELECT-type query. We do not want
  12. * to let the client suspend an update-type query partway through! Because
  13. * the query rewriter does not allow arbitrary ON SELECT rewrite rules,
  14. * only queries that were originally update-type could produce multiple
  15. * plan trees; so the restriction to a single query is not a problem
  16. * in practice.
  17. *
  18. * For SQL cursors, we support three kinds of scroll behavior:
  19. *
  20. * (1) Neither NO SCROLL nor SCROLL was specified: to remain backward
  21. * compatible, we allow backward fetches here, unless it would
  22. * impose additional runtime overhead to do so.
  23. *
  24. * (2) NO SCROLL was specified: don't allow any backward fetches.
  25. *
  26. * (3) SCROLL was specified: allow all kinds of backward fetches, even
  27. * if we need to take a performance hit to do so. (The planner sticks
  28. * a Materialize node atop the query plan if needed.)
  29. *
  30. * Case #1 is converted to #2 or #3 by looking at the query itself and
  31. * determining if scrollability can be supported without additional
  32. * overhead.
  33. *
  34. * Protocol-level portals have no nonsequential-fetch API and so the
  35. * distinction doesn't matter for them. They are always initialized
  36. * to look like NO SCROLL cursors.
  37. *
  38. *
  39. * Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group
  40. * Portions Copyright (c) 1994, Regents of the University of California
  41. *
  42. * src/include/utils/portal.h
  43. *
  44. *-------------------------------------------------------------------------
  45. */
  46. #ifndef PORTAL_H
  47. #define PORTAL_H
  48. #include "datatype/timestamp.h"
  49. #include "executor/execdesc.h"
  50. #include "utils/plancache.h"
  51. #include "utils/resowner.h"
  52. /*
  53. * We have several execution strategies for Portals, depending on what
  54. * query or queries are to be executed. (Note: in all cases, a Portal
  55. * executes just a single source-SQL query, and thus produces just a
  56. * single result from the user's viewpoint. However, the rule rewriter
  57. * may expand the single source query to zero or many actual queries.)
  58. *
  59. * PORTAL_ONE_SELECT: the portal contains one single SELECT query. We run
  60. * the Executor incrementally as results are demanded. This strategy also
  61. * supports holdable cursors (the Executor results can be dumped into a
  62. * tuplestore for access after transaction completion).
  63. *
  64. * PORTAL_ONE_RETURNING: the portal contains a single INSERT/UPDATE/DELETE
  65. * query with a RETURNING clause (plus possibly auxiliary queries added by
  66. * rule rewriting). On first execution, we run the portal to completion
  67. * and dump the primary query's results into the portal tuplestore; the
  68. * results are then returned to the client as demanded. (We can't support
  69. * suspension of the query partway through, because the AFTER TRIGGER code
  70. * can't cope, and also because we don't want to risk failing to execute
  71. * all the auxiliary queries.)
  72. *
  73. * PORTAL_ONE_MOD_WITH: the portal contains one single SELECT query, but
  74. * it has data-modifying CTEs. This is currently treated the same as the
  75. * PORTAL_ONE_RETURNING case because of the possibility of needing to fire
  76. * triggers. It may act more like PORTAL_ONE_SELECT in future.
  77. *
  78. * PORTAL_UTIL_SELECT: the portal contains a utility statement that returns
  79. * a SELECT-like result (for example, EXPLAIN or SHOW). On first execution,
  80. * we run the statement and dump its results into the portal tuplestore;
  81. * the results are then returned to the client as demanded.
  82. *
  83. * PORTAL_MULTI_QUERY: all other cases. Here, we do not support partial
  84. * execution: the portal's queries will be run to completion on first call.
  85. */
  86. typedef enum PortalStrategy
  87. {
  88. PORTAL_ONE_SELECT,
  89. PORTAL_ONE_RETURNING,
  90. PORTAL_ONE_MOD_WITH,
  91. PORTAL_UTIL_SELECT,
  92. PORTAL_MULTI_QUERY
  93. } PortalStrategy;
  94. /*
  95. * A portal is always in one of these states. It is possible to transit
  96. * from ACTIVE back to READY if the query is not run to completion;
  97. * otherwise we never back up in status.
  98. */
  99. typedef enum PortalStatus
  100. {
  101. PORTAL_NEW, /* freshly created */
  102. PORTAL_DEFINED, /* PortalDefineQuery done */
  103. PORTAL_READY, /* PortalStart complete, can run it */
  104. PORTAL_ACTIVE, /* portal is running (can't delete it) */
  105. PORTAL_DONE, /* portal is finished (don't re-run it) */
  106. PORTAL_FAILED /* portal got error (can't re-run it) */
  107. } PortalStatus;
  108. typedef struct PortalData *Portal;
  109. typedef struct PortalData
  110. {
  111. /* Bookkeeping data */
  112. const char *name; /* portal's name */
  113. const char *prepStmtName; /* source prepared statement (NULL if none) */
  114. MemoryContext heap; /* subsidiary memory for portal */
  115. ResourceOwner resowner; /* resources owned by portal */
  116. void (*cleanup) (Portal portal); /* cleanup hook */
  117. /*
  118. * State data for remembering which subtransaction(s) the portal was
  119. * created or used in. If the portal is held over from a previous
  120. * transaction, both subxids are InvalidSubTransactionId. Otherwise,
  121. * createSubid is the creating subxact and activeSubid is the last subxact
  122. * in which we ran the portal.
  123. */
  124. SubTransactionId createSubid; /* the creating subxact */
  125. SubTransactionId activeSubid; /* the last subxact with activity */
  126. /* The query or queries the portal will execute */
  127. const char *sourceText; /* text of query (as of 8.4, never NULL) */
  128. const char *commandTag; /* command tag for original query */
  129. List *stmts; /* PlannedStmts and/or utility statements */
  130. CachedPlan *cplan; /* CachedPlan, if stmts are from one */
  131. ParamListInfo portalParams; /* params to pass to query */
  132. /* Features/options */
  133. PortalStrategy strategy; /* see above */
  134. int cursorOptions; /* DECLARE CURSOR option bits */
  135. /* Status data */
  136. PortalStatus status; /* see above */
  137. bool portalPinned; /* a pinned portal can't be dropped */
  138. /* If not NULL, Executor is active; call ExecutorEnd eventually: */
  139. QueryDesc *queryDesc; /* info needed for executor invocation */
  140. /* If portal returns tuples, this is their tupdesc: */
  141. TupleDesc tupDesc; /* descriptor for result tuples */
  142. /* and these are the format codes to use for the columns: */
  143. int16 *formats; /* a format code for each column */
  144. /*
  145. * Where we store tuples for a held cursor or a PORTAL_ONE_RETURNING or
  146. * PORTAL_UTIL_SELECT query. (A cursor held past the end of its
  147. * transaction no longer has any active executor state.)
  148. */
  149. Tuplestorestate *holdStore; /* store for holdable cursors */
  150. MemoryContext holdContext; /* memory containing holdStore */
  151. /*
  152. * Snapshot under which tuples in the holdStore were read. We must keep a
  153. * reference to this snapshot if there is any possibility that the tuples
  154. * contain TOAST references, because releasing the snapshot could allow
  155. * recently-dead rows to be vacuumed away, along with any toast data
  156. * belonging to them. In the case of a held cursor, we avoid needing to
  157. * keep such a snapshot by forcibly detoasting the data.
  158. */
  159. Snapshot holdSnapshot; /* registered snapshot, or NULL if none */
  160. /*
  161. * atStart, atEnd and portalPos indicate the current cursor position.
  162. * portalPos is zero before the first row, N after fetching N'th row of
  163. * query. After we run off the end, portalPos = # of rows in query, and
  164. * atEnd is true. Note that atStart implies portalPos == 0, but not the
  165. * reverse: we might have backed up only as far as the first row, not to
  166. * the start. Also note that various code inspects atStart and atEnd, but
  167. * only the portal movement routines should touch portalPos.
  168. */
  169. bool atStart;
  170. bool atEnd;
  171. uint64 portalPos;
  172. /* Presentation data, primarily used by the pg_cursors system view */
  173. TimestampTz creation_time; /* time at which this portal was defined */
  174. bool visible; /* include this portal in pg_cursors? */
  175. } PortalData;
  176. /*
  177. * PortalIsValid
  178. * True iff portal is valid.
  179. */
  180. #define PortalIsValid(p) PointerIsValid(p)
  181. /*
  182. * Access macros for Portal ... use these in preference to field access.
  183. */
  184. #define PortalGetQueryDesc(portal) ((portal)->queryDesc)
  185. #define PortalGetHeapMemory(portal) ((portal)->heap)
  186. #define PortalGetPrimaryStmt(portal) PortalListGetPrimaryStmt((portal)->stmts)
  187. /* Prototypes for functions in utils/mmgr/portalmem.c */
  188. extern void EnablePortalManager(void);
  189. extern bool PreCommit_Portals(bool isPrepare);
  190. extern void AtAbort_Portals(void);
  191. extern void AtCleanup_Portals(void);
  192. extern void AtSubCommit_Portals(SubTransactionId mySubid,
  193. SubTransactionId parentSubid,
  194. ResourceOwner parentXactOwner);
  195. extern void AtSubAbort_Portals(SubTransactionId mySubid,
  196. SubTransactionId parentSubid,
  197. ResourceOwner myXactOwner,
  198. ResourceOwner parentXactOwner);
  199. extern void AtSubCleanup_Portals(SubTransactionId mySubid);
  200. extern Portal CreatePortal(const char *name, bool allowDup, bool dupSilent);
  201. extern Portal CreateNewPortal(void);
  202. extern void PinPortal(Portal portal);
  203. extern void UnpinPortal(Portal portal);
  204. extern void MarkPortalActive(Portal portal);
  205. extern void MarkPortalDone(Portal portal);
  206. extern void MarkPortalFailed(Portal portal);
  207. extern void PortalDrop(Portal portal, bool isTopCommit);
  208. extern Portal GetPortalByName(const char *name);
  209. extern void PortalDefineQuery(Portal portal,
  210. const char *prepStmtName,
  211. const char *sourceText,
  212. const char *commandTag,
  213. List *stmts,
  214. CachedPlan *cplan);
  215. extern Node *PortalListGetPrimaryStmt(List *stmts);
  216. extern void PortalCreateHoldStore(Portal portal);
  217. extern void PortalHashTableDeleteAll(void);
  218. extern bool ThereAreNoReadyPortals(void);
  219. #endif /* PORTAL_H */