spi_priv.h 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*-------------------------------------------------------------------------
  2. *
  3. * spi_priv.h
  4. * Server Programming Interface private declarations
  5. *
  6. * Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group
  7. * Portions Copyright (c) 1994, Regents of the University of California
  8. *
  9. * src/include/executor/spi_priv.h
  10. *
  11. *-------------------------------------------------------------------------
  12. */
  13. #ifndef SPI_PRIV_H
  14. #define SPI_PRIV_H
  15. #include "executor/spi.h"
  16. #define _SPI_PLAN_MAGIC 569278163
  17. typedef struct
  18. {
  19. /* current results */
  20. uint64 processed; /* by Executor */
  21. Oid lastoid;
  22. SPITupleTable *tuptable; /* tuptable currently being built */
  23. /* resources of this execution context */
  24. slist_head tuptables; /* list of all live SPITupleTables */
  25. MemoryContext procCxt; /* procedure context */
  26. MemoryContext execCxt; /* executor context */
  27. MemoryContext savedcxt; /* context of SPI_connect's caller */
  28. SubTransactionId connectSubid; /* ID of connecting subtransaction */
  29. } _SPI_connection;
  30. /*
  31. * SPI plans have three states: saved, unsaved, or temporary.
  32. *
  33. * Ordinarily, the _SPI_plan struct itself as well as the argtypes array
  34. * are in a dedicated memory context identified by plancxt (which can be
  35. * really small). All the other subsidiary state is in plancache entries
  36. * identified by plancache_list (note: the list cells themselves are in
  37. * plancxt).
  38. *
  39. * In an unsaved plan, the plancxt as well as the plancache entries' contexts
  40. * are children of the SPI procedure context, so they'll all disappear at
  41. * function exit. plancache.c also knows that the plancache entries are
  42. * "unsaved", so it doesn't link them into its global list; hence they do
  43. * not respond to inval events. This is OK since we are presumably holding
  44. * adequate locks to prevent other backends from messing with the tables.
  45. *
  46. * For a saved plan, the plancxt is made a child of CacheMemoryContext
  47. * since it should persist until explicitly destroyed. Likewise, the
  48. * plancache entries will be under CacheMemoryContext since we tell
  49. * plancache.c to save them. We rely on plancache.c to keep the cache
  50. * entries up-to-date as needed in the face of invalidation events.
  51. *
  52. * There are also "temporary" SPI plans, in which the _SPI_plan struct is
  53. * not even palloc'd but just exists in some function's local variable.
  54. * The plancache entries are unsaved and exist under the SPI executor context,
  55. * while additional data such as argtypes and list cells is loose in the SPI
  56. * executor context. Such plans can be identified by having plancxt == NULL.
  57. *
  58. * We can also have "one-shot" SPI plans (which are typically temporary,
  59. * as described above). These are meant to be executed once and discarded,
  60. * and various optimizations are made on the assumption of single use.
  61. * Note in particular that the CachedPlanSources within such an SPI plan
  62. * are not "complete" until execution.
  63. *
  64. * Note: if the original query string contained only whitespace and comments,
  65. * the plancache_list will be NIL and so there is no place to store the
  66. * query string. We don't care about that, but we do care about the
  67. * argument type array, which is why it's seemingly-redundantly stored.
  68. */
  69. typedef struct _SPI_plan
  70. {
  71. int magic; /* should equal _SPI_PLAN_MAGIC */
  72. bool saved; /* saved or unsaved plan? */
  73. bool oneshot; /* one-shot plan? */
  74. List *plancache_list; /* one CachedPlanSource per parsetree */
  75. MemoryContext plancxt; /* Context containing _SPI_plan and data */
  76. int cursor_options; /* Cursor options used for planning */
  77. int nargs; /* number of plan arguments */
  78. Oid *argtypes; /* Argument types (NULL if nargs is 0) */
  79. ParserSetupHook parserSetup; /* alternative parameter spec method */
  80. void *parserSetupArg;
  81. } _SPI_plan;
  82. #endif /* SPI_PRIV_H */