1 /*-------------------------------------------------------------------------
4 * Prepareable SQL statements via PREPARE, EXECUTE and DEALLOCATE
6 * This module also implements storage of prepared statements that are
7 * accessed via the extended FE/BE query protocol.
10 * Copyright (c) 2002-2009, PostgreSQL Global Development Group
13 * $PostgreSQL: pgsql/src/backend/commands/prepare.c,v 1.100 2009/11/04 22:26:05 tgl Exp $
15 *-------------------------------------------------------------------------
19 #include "access/xact.h"
20 #include "catalog/pg_type.h"
21 #include "commands/prepare.h"
22 #include "miscadmin.h"
23 #include "nodes/nodeFuncs.h"
24 #include "parser/analyze.h"
25 #include "parser/parse_coerce.h"
26 #include "parser/parse_expr.h"
27 #include "parser/parse_type.h"
28 #include "rewrite/rewriteHandler.h"
29 #include "tcop/pquery.h"
30 #include "tcop/tcopprot.h"
31 #include "tcop/utility.h"
32 #include "utils/builtins.h"
33 #include "utils/memutils.h"
34 #include "utils/snapmgr.h"
38 * The hash table in which prepared queries are stored. This is
39 * per-backend: query plans are not shared between backends.
40 * The keys for this hash table are the arguments to PREPARE and EXECUTE
41 * (statement names); the entries are PreparedStatement structs.
43 static HTAB *prepared_queries = NULL;
45 static void InitQueryHashTable(void);
46 static ParamListInfo EvaluateParams(PreparedStatement *pstmt, List *params,
47 const char *queryString, EState *estate);
48 static Datum build_regtype_array(Oid *param_types, int num_params);
51 * Implements the 'PREPARE' utility statement.
54 PrepareQuery(PrepareStmt *stmt, const char *queryString)
64 * Disallow empty-string statement name (conflicts with protocol-level
67 if (!stmt->name || stmt->name[0] == '\0')
69 (errcode(ERRCODE_INVALID_PSTATEMENT_DEFINITION),
70 errmsg("invalid statement name: must not be empty")));
72 /* Transform list of TypeNames to array of type OIDs */
73 nargs = list_length(stmt->argtypes);
81 * typenameTypeId wants a ParseState to carry the source query string.
82 * Is it worth refactoring its API to avoid this?
84 pstate = make_parsestate(NULL);
85 pstate->p_sourcetext = queryString;
87 argtypes = (Oid *) palloc(nargs * sizeof(Oid));
90 foreach(l, stmt->argtypes)
92 TypeName *tn = lfirst(l);
93 Oid toid = typenameTypeId(pstate, tn, NULL);
100 * Analyze the statement using these parameter types (any parameters
101 * passed in from above us will not be visible to it), allowing
102 * information about unknown parameters to be deduced from context.
104 * Because parse analysis scribbles on the raw querytree, we must make a
105 * copy to ensure we have a pristine raw tree to cache. FIXME someday.
107 query = parse_analyze_varparams((Node *) copyObject(stmt->query),
112 * Check that all parameter types were determined.
114 for (i = 0; i < nargs; i++)
116 Oid argtype = argtypes[i];
118 if (argtype == InvalidOid || argtype == UNKNOWNOID)
120 (errcode(ERRCODE_INDETERMINATE_DATATYPE),
121 errmsg("could not determine data type of parameter $%d",
126 * grammar only allows OptimizableStmt, so this check should be redundant
128 switch (query->commandType)
138 (errcode(ERRCODE_INVALID_PSTATEMENT_DEFINITION),
139 errmsg("utility statements cannot be prepared")));
143 /* Rewrite the query. The result could be 0, 1, or many queries. */
144 query_list = QueryRewrite(query);
146 /* Generate plans for queries. */
147 plan_list = pg_plan_queries(query_list, 0, NULL);
152 StorePreparedStatement(stmt->name,
155 CreateCommandTag((Node *) query),
158 0, /* default cursor options */
164 * Implements the 'EXECUTE' utility statement.
166 * Note: this is one of very few places in the code that needs to deal with
167 * two query strings at once. The passed-in queryString is that of the
168 * EXECUTE, which we might need for error reporting while processing the
169 * parameter expressions. The query_string that we copy from the plan
170 * source is that of the original PREPARE.
173 ExecuteQuery(ExecuteStmt *stmt, const char *queryString,
174 ParamListInfo params,
175 DestReceiver *dest, char *completionTag)
177 PreparedStatement *entry;
180 ParamListInfo paramLI = NULL;
181 EState *estate = NULL;
185 /* Look it up in the hash table */
186 entry = FetchPreparedStatement(stmt->name, true);
188 /* Shouldn't have a non-fully-planned plancache entry */
189 if (!entry->plansource->fully_planned)
190 elog(ERROR, "EXECUTE does not support unplanned prepared statements");
191 /* Shouldn't get any non-fixed-result cached plan, either */
192 if (!entry->plansource->fixed_result)
193 elog(ERROR, "EXECUTE does not support variable-result cached plans");
195 /* Evaluate parameters, if any */
196 if (entry->plansource->num_params > 0)
199 * Need an EState to evaluate parameters; must not delete it till end
200 * of query, in case parameters are pass-by-reference.
202 estate = CreateExecutorState();
203 estate->es_param_list_info = params;
204 paramLI = EvaluateParams(entry, stmt->params,
205 queryString, estate);
208 /* Create a new portal to run the query in */
209 portal = CreateNewPortal();
210 /* Don't display the portal in pg_cursors, it is for internal use only */
211 portal->visible = false;
213 /* Copy the plan's saved query string into the portal's memory */
214 query_string = MemoryContextStrdup(PortalGetHeapMemory(portal),
215 entry->plansource->query_string);
218 * For CREATE TABLE / AS EXECUTE, we must make a copy of the stored query
219 * so that we can modify its destination (yech, but this has always been
220 * ugly). For regular EXECUTE we can just use the cached query, since the
221 * executor is read-only.
225 MemoryContext oldContext;
228 /* Replan if needed, and increment plan refcount transiently */
229 cplan = RevalidateCachedPlan(entry->plansource, true);
231 /* Copy plan into portal's context, and modify */
232 oldContext = MemoryContextSwitchTo(PortalGetHeapMemory(portal));
234 plan_list = copyObject(cplan->stmt_list);
236 if (list_length(plan_list) != 1)
238 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
239 errmsg("prepared statement is not a SELECT")));
240 pstmt = (PlannedStmt *) linitial(plan_list);
241 if (!IsA(pstmt, PlannedStmt) ||
242 pstmt->commandType != CMD_SELECT ||
243 pstmt->utilityStmt != NULL)
245 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
246 errmsg("prepared statement is not a SELECT")));
247 pstmt->intoClause = copyObject(stmt->into);
249 MemoryContextSwitchTo(oldContext);
251 /* We no longer need the cached plan refcount ... */
252 ReleaseCachedPlan(cplan, true);
253 /* ... and we don't want the portal to depend on it, either */
258 /* Replan if needed, and increment plan refcount for portal */
259 cplan = RevalidateCachedPlan(entry->plansource, false);
260 plan_list = cplan->stmt_list;
263 PortalDefineQuery(portal,
266 entry->plansource->commandTag,
271 * Run the portal to completion.
273 PortalStart(portal, paramLI, GetActiveSnapshot());
275 (void) PortalRun(portal, FETCH_ALL, false, dest, dest, completionTag);
277 PortalDrop(portal, false);
280 FreeExecutorState(estate);
282 /* No need to pfree other memory, MemoryContext will be reset */
286 * EvaluateParams: evaluate a list of parameters.
288 * pstmt: statement we are getting parameters for.
289 * params: list of given parameter expressions (raw parser output!)
290 * queryString: source text for error messages.
291 * estate: executor state to use.
293 * Returns a filled-in ParamListInfo -- this can later be passed to
294 * CreateQueryDesc(), which allows the executor to make use of the parameters
295 * during query execution.
298 EvaluateParams(PreparedStatement *pstmt, List *params,
299 const char *queryString, EState *estate)
301 Oid *param_types = pstmt->plansource->param_types;
302 int num_params = pstmt->plansource->num_params;
303 int nparams = list_length(params);
305 ParamListInfo paramLI;
310 if (nparams != num_params)
312 (errcode(ERRCODE_SYNTAX_ERROR),
313 errmsg("wrong number of parameters for prepared statement \"%s\"",
315 errdetail("Expected %d parameters but got %d.",
316 num_params, nparams)));
318 /* Quick exit if no parameters */
323 * We have to run parse analysis for the expressions. Since the parser is
324 * not cool about scribbling on its input, copy first.
326 params = (List *) copyObject(params);
328 pstate = make_parsestate(NULL);
329 pstate->p_sourcetext = queryString;
334 Node *expr = lfirst(l);
335 Oid expected_type_id = param_types[i];
338 expr = transformExpr(pstate, expr);
340 /* Cannot contain subselects or aggregates */
341 if (pstate->p_hasSubLinks)
343 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
344 errmsg("cannot use subquery in EXECUTE parameter")));
345 if (pstate->p_hasAggs)
347 (errcode(ERRCODE_GROUPING_ERROR),
348 errmsg("cannot use aggregate function in EXECUTE parameter")));
349 if (pstate->p_hasWindowFuncs)
351 (errcode(ERRCODE_WINDOWING_ERROR),
352 errmsg("cannot use window function in EXECUTE parameter")));
354 given_type_id = exprType(expr);
356 expr = coerce_to_target_type(pstate, expr, given_type_id,
357 expected_type_id, -1,
359 COERCE_IMPLICIT_CAST,
364 (errcode(ERRCODE_DATATYPE_MISMATCH),
365 errmsg("parameter $%d of type %s cannot be coerced to the expected type %s",
367 format_type_be(given_type_id),
368 format_type_be(expected_type_id)),
369 errhint("You will need to rewrite or cast the expression.")));
375 /* Prepare the expressions for execution */
376 exprstates = (List *) ExecPrepareExpr((Expr *) params, estate);
378 /* sizeof(ParamListInfoData) includes the first array element */
379 paramLI = (ParamListInfo)
380 palloc(sizeof(ParamListInfoData) +
381 (num_params - 1) *sizeof(ParamExternData));
382 /* we have static list of params, so no hooks needed */
383 paramLI->paramFetch = NULL;
384 paramLI->paramFetchArg = NULL;
385 paramLI->parserSetup = NULL;
386 paramLI->parserSetupArg = NULL;
387 paramLI->numParams = num_params;
390 foreach(l, exprstates)
392 ExprState *n = lfirst(l);
393 ParamExternData *prm = ¶mLI->params[i];
395 prm->ptype = param_types[i];
397 prm->value = ExecEvalExprSwitchContext(n,
398 GetPerTupleExprContext(estate),
410 * Initialize query hash table upon first use.
413 InitQueryHashTable(void)
417 MemSet(&hash_ctl, 0, sizeof(hash_ctl));
419 hash_ctl.keysize = NAMEDATALEN;
420 hash_ctl.entrysize = sizeof(PreparedStatement);
422 prepared_queries = hash_create("Prepared Queries",
429 * Store all the data pertaining to a query in the hash table using
430 * the specified key. All the given data is copied into either the hashtable
431 * entry or the underlying plancache entry, so the caller can dispose of its
434 * Exception: commandTag is presumed to be a pointer to a constant string,
435 * or possibly NULL, so it need not be copied. Note that commandTag should
436 * be NULL only if the original query (before rewriting) was empty.
439 StorePreparedStatement(const char *stmt_name,
440 Node *raw_parse_tree,
441 const char *query_string,
442 const char *commandTag,
449 PreparedStatement *entry;
450 CachedPlanSource *plansource;
453 /* Initialize the hash table, if necessary */
454 if (!prepared_queries)
455 InitQueryHashTable();
457 /* Check for pre-existing entry of same name */
458 hash_search(prepared_queries, stmt_name, HASH_FIND, &found);
462 (errcode(ERRCODE_DUPLICATE_PSTATEMENT),
463 errmsg("prepared statement \"%s\" already exists",
466 /* Create a plancache entry */
467 plansource = CreateCachedPlan(raw_parse_tree,
477 /* Now we can add entry to hash table */
478 entry = (PreparedStatement *) hash_search(prepared_queries,
483 /* Shouldn't get a duplicate entry */
485 elog(ERROR, "duplicate prepared statement \"%s\"",
488 /* Fill in the hash table entry */
489 entry->plansource = plansource;
490 entry->from_sql = from_sql;
491 entry->prepare_time = GetCurrentStatementStartTimestamp();
495 * Lookup an existing query in the hash table. If the query does not
496 * actually exist, throw ereport(ERROR) or return NULL per second parameter.
498 * Note: this does not force the referenced plancache entry to be valid,
499 * since not all callers care.
502 FetchPreparedStatement(const char *stmt_name, bool throwError)
504 PreparedStatement *entry;
507 * If the hash table hasn't been initialized, it can't be storing
508 * anything, therefore it couldn't possibly store our plan.
510 if (prepared_queries)
511 entry = (PreparedStatement *) hash_search(prepared_queries,
518 if (!entry && throwError)
520 (errcode(ERRCODE_UNDEFINED_PSTATEMENT),
521 errmsg("prepared statement \"%s\" does not exist",
528 * Given a prepared statement, determine the result tupledesc it will
529 * produce. Returns NULL if the execution will not return tuples.
531 * Note: the result is created or copied into current memory context.
534 FetchPreparedStatementResultDesc(PreparedStatement *stmt)
537 * Since we don't allow prepared statements' result tupdescs to change,
538 * there's no need for a revalidate call here.
540 Assert(stmt->plansource->fixed_result);
541 if (stmt->plansource->resultDesc)
542 return CreateTupleDescCopy(stmt->plansource->resultDesc);
548 * Given a prepared statement that returns tuples, extract the query
549 * targetlist. Returns NIL if the statement doesn't have a determinable
552 * Note: this is pretty ugly, but since it's only used in corner cases like
553 * Describe Statement on an EXECUTE command, we don't worry too much about
557 FetchPreparedStatementTargetList(PreparedStatement *stmt)
562 /* No point in looking if it doesn't return tuples */
563 if (stmt->plansource->resultDesc == NULL)
566 /* Make sure the plan is up to date */
567 cplan = RevalidateCachedPlan(stmt->plansource, true);
569 /* Get the primary statement and find out what it returns */
570 tlist = FetchStatementTargetList(PortalListGetPrimaryStmt(cplan->stmt_list));
572 /* Copy into caller's context so we can release the plancache entry */
573 tlist = (List *) copyObject(tlist);
575 ReleaseCachedPlan(cplan, true);
581 * Implements the 'DEALLOCATE' utility statement: deletes the
582 * specified plan from storage.
585 DeallocateQuery(DeallocateStmt *stmt)
588 DropPreparedStatement(stmt->name, true);
590 DropAllPreparedStatements();
594 * Internal version of DEALLOCATE
596 * If showError is false, dropping a nonexistent statement is a no-op.
599 DropPreparedStatement(const char *stmt_name, bool showError)
601 PreparedStatement *entry;
603 /* Find the query's hash table entry; raise error if wanted */
604 entry = FetchPreparedStatement(stmt_name, showError);
608 /* Release the plancache entry */
609 DropCachedPlan(entry->plansource);
611 /* Now we can remove the hash table entry */
612 hash_search(prepared_queries, entry->stmt_name, HASH_REMOVE, NULL);
617 * Drop all cached statements.
620 DropAllPreparedStatements(void)
623 PreparedStatement *entry;
626 if (!prepared_queries)
629 /* walk over cache */
630 hash_seq_init(&seq, prepared_queries);
631 while ((entry = hash_seq_search(&seq)) != NULL)
633 /* Release the plancache entry */
634 DropCachedPlan(entry->plansource);
636 /* Now we can remove the hash table entry */
637 hash_search(prepared_queries, entry->stmt_name, HASH_REMOVE, NULL);
642 * Implements the 'EXPLAIN EXECUTE' utility statement.
644 * Note: the passed-in queryString is that of the EXPLAIN EXECUTE,
645 * not the original PREPARE; we get the latter string from the plancache.
648 ExplainExecuteQuery(ExecuteStmt *execstmt, ExplainState *es,
649 const char *queryString, ParamListInfo params)
651 PreparedStatement *entry;
652 const char *query_string;
656 ParamListInfo paramLI = NULL;
657 EState *estate = NULL;
659 /* Look it up in the hash table */
660 entry = FetchPreparedStatement(execstmt->name, true);
662 /* Shouldn't have a non-fully-planned plancache entry */
663 if (!entry->plansource->fully_planned)
664 elog(ERROR, "EXPLAIN EXECUTE does not support unplanned prepared statements");
665 /* Shouldn't get any non-fixed-result cached plan, either */
666 if (!entry->plansource->fixed_result)
667 elog(ERROR, "EXPLAIN EXECUTE does not support variable-result cached plans");
669 query_string = entry->plansource->query_string;
671 /* Replan if needed, and acquire a transient refcount */
672 cplan = RevalidateCachedPlan(entry->plansource, true);
674 plan_list = cplan->stmt_list;
676 /* Evaluate parameters, if any */
677 if (entry->plansource->num_params)
680 * Need an EState to evaluate parameters; must not delete it till end
681 * of query, in case parameters are pass-by-reference.
683 estate = CreateExecutorState();
684 estate->es_param_list_info = params;
685 paramLI = EvaluateParams(entry, execstmt->params,
686 queryString, estate);
689 /* Explain each query */
690 foreach(p, plan_list)
692 PlannedStmt *pstmt = (PlannedStmt *) lfirst(p);
694 if (IsA(pstmt, PlannedStmt))
698 if (pstmt->commandType != CMD_SELECT ||
699 pstmt->utilityStmt != NULL)
701 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
702 errmsg("prepared statement is not a SELECT")));
704 /* Copy the stmt so we can modify it */
705 pstmt = copyObject(pstmt);
707 pstmt->intoClause = execstmt->into;
710 ExplainOnePlan(pstmt, es, query_string, paramLI);
714 ExplainOneUtility((Node *) pstmt, es, query_string, params);
717 /* No need for CommandCounterIncrement, as ExplainOnePlan did it */
719 /* Separate plans with an appropriate separator */
720 if (lnext(p) != NULL)
721 ExplainSeparatePlans(es);
725 FreeExecutorState(estate);
727 ReleaseCachedPlan(cplan, true);
731 * This set returning function reads all the prepared statements and
732 * returns a set of (name, statement, prepare_time, param_types, from_sql).
735 pg_prepared_statement(PG_FUNCTION_ARGS)
737 ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
739 Tuplestorestate *tupstore;
740 MemoryContext per_query_ctx;
741 MemoryContext oldcontext;
743 /* check to see if caller supports us returning a tuplestore */
744 if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
746 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
747 errmsg("set-valued function called in context that cannot accept a set")));
748 if (!(rsinfo->allowedModes & SFRM_Materialize))
750 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
751 errmsg("materialize mode required, but it is not " \
752 "allowed in this context")));
754 /* need to build tuplestore in query context */
755 per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
756 oldcontext = MemoryContextSwitchTo(per_query_ctx);
759 * build tupdesc for result tuples. This must match the definition of the
760 * pg_prepared_statements view in system_views.sql
762 tupdesc = CreateTemplateTupleDesc(5, false);
763 TupleDescInitEntry(tupdesc, (AttrNumber) 1, "name",
765 TupleDescInitEntry(tupdesc, (AttrNumber) 2, "statement",
767 TupleDescInitEntry(tupdesc, (AttrNumber) 3, "prepare_time",
768 TIMESTAMPTZOID, -1, 0);
769 TupleDescInitEntry(tupdesc, (AttrNumber) 4, "parameter_types",
770 REGTYPEARRAYOID, -1, 0);
771 TupleDescInitEntry(tupdesc, (AttrNumber) 5, "from_sql",
775 * We put all the tuples into a tuplestore in one scan of the hashtable.
776 * This avoids any issue of the hashtable possibly changing between calls.
779 tuplestore_begin_heap(rsinfo->allowedModes & SFRM_Materialize_Random,
782 /* hash table might be uninitialized */
783 if (prepared_queries)
785 HASH_SEQ_STATUS hash_seq;
786 PreparedStatement *prep_stmt;
788 hash_seq_init(&hash_seq, prepared_queries);
789 while ((prep_stmt = hash_seq_search(&hash_seq)) != NULL)
794 /* generate junk in short-term context */
795 MemoryContextSwitchTo(oldcontext);
797 MemSet(nulls, 0, sizeof(nulls));
799 values[0] = CStringGetTextDatum(prep_stmt->stmt_name);
800 values[1] = CStringGetTextDatum(prep_stmt->plansource->query_string);
801 values[2] = TimestampTzGetDatum(prep_stmt->prepare_time);
802 values[3] = build_regtype_array(prep_stmt->plansource->param_types,
803 prep_stmt->plansource->num_params);
804 values[4] = BoolGetDatum(prep_stmt->from_sql);
806 /* switch to appropriate context while storing the tuple */
807 MemoryContextSwitchTo(per_query_ctx);
808 tuplestore_putvalues(tupstore, tupdesc, values, nulls);
812 /* clean up and return the tuplestore */
813 tuplestore_donestoring(tupstore);
815 MemoryContextSwitchTo(oldcontext);
817 rsinfo->returnMode = SFRM_Materialize;
818 rsinfo->setResult = tupstore;
819 rsinfo->setDesc = tupdesc;
825 * This utility function takes a C array of Oids, and returns a Datum
826 * pointing to a one-dimensional Postgres array of regtypes. An empty
827 * array is returned as a zero-element array, not NULL.
830 build_regtype_array(Oid *param_types, int num_params)
836 tmp_ary = (Datum *) palloc(num_params * sizeof(Datum));
838 for (i = 0; i < num_params; i++)
839 tmp_ary[i] = ObjectIdGetDatum(param_types[i]);
841 /* XXX: this hardcodes assumptions about the regtype type */
842 result = construct_array(tmp_ary, num_params, REGTYPEOID, 4, true, 'i');
843 return PointerGetDatum(result);