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