/* ----------
* Generate a prepared plan
+ *
+ * CAUTION: it is possible for this function to throw an error after it has
+ * built a SPIPlan and saved it in expr->plan. Therefore, be wary of doing
+ * additional things contingent on expr->plan being NULL. That is, given
+ * code like
+ *
+ * if (query->plan == NULL)
+ * {
+ * // okay to put setup code here
+ * exec_prepare_plan(estate, query, ...);
+ * // NOT okay to put more logic here
+ * }
+ *
+ * extra steps at the end are unsafe because they will not be executed when
+ * re-executing the calling statement, if exec_prepare_plan failed the first
+ * time. This is annoyingly error-prone, but the alternatives are worse.
* ----------
*/
static void
SPI_keepplan(plan);
expr->plan = plan;
- /* Check to see if it's a simple expression */
- exec_simple_check_plan(estate, expr);
-
/*
* Mark expression as not using a read-write param. exec_assign_value has
* to take steps to override this if appropriate; that seems cleaner than
* adding parameters to all other callers.
*/
expr->rwparam = -1;
+
+ /* Check to see if it's a simple expression */
+ exec_simple_check_plan(estate, expr);
}
* whether the statement is INSERT/UPDATE/DELETE
*/
if (expr->plan == NULL)
+ exec_prepare_plan(estate, expr, CURSOR_OPT_PARALLEL_OK);
+
+ if (!stmt->mod_stmt_set)
{
ListCell *l;
- exec_prepare_plan(estate, expr, CURSOR_OPT_PARALLEL_OK);
stmt->mod_stmt = false;
foreach(l, SPI_plan_get_plan_sources(expr->plan))
{
break;
}
}
+ stmt->mod_stmt_set = true;
}
/*
* if we can pass the target variable as a read-write parameter to the
* expression. (This is a bit messy, but it seems cleaner than modifying
* the API of exec_eval_expr for the purpose.)
+ *
+ * NOTE: this coding ignores the advice given in exec_prepare_plan's
+ * comments, that one should not do additional setup contingent on
+ * expr->plan being NULL. This means that if we get an error while trying
+ * to check for the expression being simple, we won't check for a
+ * read-write parameter either, so that neither optimization will be
+ * applied in future executions. Nothing will fail though, so we live
+ * with that bit of messiness too.
*/
if (expr->plan == NULL)
{
* exec_simple_check_plan - Check if a plan is simple enough to
* be evaluated by ExecEvalExpr() instead
* of SPI.
+ *
+ * Note: the refcount manipulations in this function assume that expr->plan
+ * is a "saved" SPI plan. That's a bit annoying from the caller's standpoint,
+ * but it's otherwise difficult to avoid leaking the plan on failure.
* ----------
*/
static void
* OK, it seems worth constructing a plan for more careful checking.
*
* Get the generic plan for the query. If replanning is needed, do that
- * work in the eval_mcontext.
+ * work in the eval_mcontext. (Note that replanning could throw an error,
+ * in which case the expr is left marked "not simple", which is fine.)
*/
oldcontext = MemoryContextSwitchTo(get_eval_mcontext(estate));
cplan = SPI_plan_get_cached_plan(expr->plan);