2
0

spi_priv.h 4.3 KB

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