diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2013-01-04 17:42:19 -0500 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2013-01-04 17:42:19 -0500 |
commit | 94afbd5831fbc1926f1c367ac14a45ccc29d313d (patch) | |
tree | 51cea3e79490853693f795ac03c2438dd9b5522a /src/include | |
parent | 78a5e738e97b4dda89e1bfea60675bcf15f25994 (diff) |
Invent a "one-shot" variant of CachedPlans for better performance.
SPI_execute() and related functions create a CachedPlan, execute it once,
and immediately discard it, so that the functionality offered by
plancache.c is of no value in this code path. And performance measurements
show that the extra data copying and invalidation checking done by
plancache.c slows down simple queries by 10% or more compared to 9.1.
However, enough of the SPI code is shared with functions that do need plan
caching that it seems impractical to bypass plancache.c altogether.
Instead, let's invent a variant version of cached plans that preserves
99% of the API but doesn't offer any of the actual functionality, nor the
overhead. This puts SPI_execute() performance back on par, or maybe even
slightly better, than it was before. This change should resolve recent
complaints of performance degradation from Dong Ye, Pavel Stehule, and
others.
By avoiding data copying, this change also reduces the amount of memory
needed to execute many-statement SPI_execute() strings, as for instance in
a recent complaint from Tomas Vondra.
An additional benefit of this change is that multi-statement SPI_execute()
query strings are now processed fully serially, that is we complete
execution of earlier statements before running parse analysis and planning
on following ones. This eliminates a long-standing POLA violation, in that
DDL that affects the behavior of a later statement will now behave as
expected.
Back-patch to 9.2, since this was a performance regression compared to 9.1.
(In 9.2, place the added struct fields so as to avoid changing the offsets
of existing fields.)
Heikki Linnakangas and Tom Lane
Diffstat (limited to 'src/include')
-rw-r--r-- | src/include/executor/spi_priv.h | 7 | ||||
-rw-r--r-- | src/include/utils/plancache.h | 19 |
2 files changed, 24 insertions, 2 deletions
diff --git a/src/include/executor/spi_priv.h b/src/include/executor/spi_priv.h index faa81dbb7f8..ef7903abd09 100644 --- a/src/include/executor/spi_priv.h +++ b/src/include/executor/spi_priv.h @@ -59,6 +59,12 @@ typedef struct * while additional data such as argtypes and list cells is loose in the SPI * executor context. Such plans can be identified by having plancxt == NULL. * + * We can also have "one-shot" SPI plans (which are typically temporary, + * as described above). These are meant to be executed once and discarded, + * and various optimizations are made on the assumption of single use. + * Note in particular that the CachedPlanSources within such an SPI plan + * are not "complete" until execution. + * * Note: if the original query string contained only whitespace and comments, * the plancache_list will be NIL and so there is no place to store the * query string. We don't care about that, but we do care about the @@ -68,6 +74,7 @@ typedef struct _SPI_plan { int magic; /* should equal _SPI_PLAN_MAGIC */ bool saved; /* saved or unsaved plan? */ + bool oneshot; /* one-shot plan? */ List *plancache_list; /* one CachedPlanSource per parsetree */ MemoryContext plancxt; /* Context containing _SPI_plan and data */ int cursor_options; /* Cursor options used for planning */ diff --git a/src/include/utils/plancache.h b/src/include/utils/plancache.h index a447eaccc58..8185427fc4b 100644 --- a/src/include/utils/plancache.h +++ b/src/include/utils/plancache.h @@ -60,6 +60,14 @@ * context that holds the rewritten query tree and associated data. This * allows the query tree to be discarded easily when it is invalidated. * + * Some callers wish to use the CachedPlan API even with one-shot queries + * that have no reason to be saved at all. We therefore support a "oneshot" + * variant that does no data copying or invalidation checking. In this case + * there are no separate memory contexts: the CachedPlanSource struct and + * all subsidiary data live in the caller's CurrentMemoryContext, and there + * is no way to free memory short of clearing that entire context. A oneshot + * plan is always treated as unsaved. + * * Note: the string referenced by commandTag is not subsidiary storage; * it is assumed to be a compile-time-constant string. As with portals, * commandTag shall be NULL if and only if the original query string (before @@ -69,7 +77,7 @@ typedef struct CachedPlanSource { int magic; /* should equal CACHEDPLANSOURCE_MAGIC */ Node *raw_parse_tree; /* output of raw_parser() */ - char *query_string; /* source text of query */ + const char *query_string; /* source text of query */ const char *commandTag; /* command tag (a constant!), or NULL */ Oid *param_types; /* array of parameter type OIDs, or NULL */ int num_params; /* length of param_types array */ @@ -88,6 +96,7 @@ typedef struct CachedPlanSource /* If we have a generic plan, this is a reference-counted link to it: */ struct CachedPlan *gplan; /* generic plan, or NULL if not valid */ /* Some state flags: */ + bool is_oneshot; /* is it a "oneshot" plan? */ bool is_complete; /* has CompleteCachedPlan been done? */ bool is_saved; /* has CachedPlanSource been "saved"? */ bool is_valid; /* is the query_list currently valid? */ @@ -106,13 +115,16 @@ typedef struct CachedPlanSource * (if any), and any active plan executions, so the plan can be discarded * exactly when refcount goes to zero. Both the struct itself and the * subsidiary data live in the context denoted by the context field. - * This makes it easy to free a no-longer-needed cached plan. + * This makes it easy to free a no-longer-needed cached plan. (However, + * if is_oneshot is true, the context does not belong solely to the CachedPlan + * so no freeing is possible.) */ typedef struct CachedPlan { int magic; /* should equal CACHEDPLAN_MAGIC */ List *stmt_list; /* list of statement nodes (PlannedStmts and * bare utility statements) */ + bool is_oneshot; /* is it a "oneshot" plan? */ bool is_saved; /* is CachedPlan in a long-lived context? */ bool is_valid; /* is the stmt_list currently valid? */ TransactionId saved_xmin; /* if valid, replan when TransactionXmin @@ -129,6 +141,9 @@ extern void ResetPlanCache(void); extern CachedPlanSource *CreateCachedPlan(Node *raw_parse_tree, const char *query_string, const char *commandTag); +extern CachedPlanSource *CreateOneShotCachedPlan(Node *raw_parse_tree, + const char *query_string, + const char *commandTag); extern void CompleteCachedPlan(CachedPlanSource *plansource, List *querytree_list, MemoryContext querytree_context, |