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-2008, PostgreSQL Global Development Group
13 * $PostgreSQL: pgsql/src/backend/commands/prepare.c,v 1.90 2008/08/25 22:42:32 tgl Exp $
15 *-------------------------------------------------------------------------
19 #include "access/xact.h"
20 #include "catalog/pg_type.h"
21 #include "commands/explain.h"
22 #include "commands/prepare.h"
23 #include "miscadmin.h"
24 #include "nodes/nodeFuncs.h"
25 #include "parser/analyze.h"
26 #include "parser/parse_coerce.h"
27 #include "parser/parse_expr.h"
28 #include "parser/parse_type.h"
29 #include "rewrite/rewriteHandler.h"
30 #include "tcop/pquery.h"
31 #include "tcop/tcopprot.h"
32 #include "tcop/utility.h"
33 #include "utils/builtins.h"
34 #include "utils/memutils.h"
35 #include "utils/snapmgr.h"
39 * The hash table in which prepared queries are stored. This is
40 * per-backend: query plans are not shared between backends.
41 * The keys for this hash table are the arguments to PREPARE and EXECUTE
42 * (statement names); the entries are PreparedStatement structs.
44 static HTAB *prepared_queries = NULL;
46 static void InitQueryHashTable(void);
47 static ParamListInfo EvaluateParams(PreparedStatement *pstmt, List *params,
48 const char *queryString, EState *estate);
49 static Datum build_regtype_array(Oid *param_types, int num_params);
52 * Implements the 'PREPARE' utility statement.
55 PrepareQuery(PrepareStmt *stmt, const char *queryString)
65 * Disallow empty-string statement name (conflicts with protocol-level
68 if (!stmt->name || stmt->name[0] == '\0')
70 (errcode(ERRCODE_INVALID_PSTATEMENT_DEFINITION),
71 errmsg("invalid statement name: must not be empty")));
73 /* Transform list of TypeNames to array of type OIDs */
74 nargs = list_length(stmt->argtypes);
82 * typenameTypeId wants a ParseState to carry the source query string.
83 * Is it worth refactoring its API to avoid this?
85 pstate = make_parsestate(NULL);
86 pstate->p_sourcetext = queryString;
88 argtypes = (Oid *) palloc(nargs * sizeof(Oid));
91 foreach(l, stmt->argtypes)
93 TypeName *tn = lfirst(l);
94 Oid toid = typenameTypeId(pstate, tn, NULL);
101 * Analyze the statement using these parameter types (any parameters
102 * passed in from above us will not be visible to it), allowing
103 * information about unknown parameters to be deduced from context.
105 * Because parse analysis scribbles on the raw querytree, we must make a
106 * copy to ensure we have a pristine raw tree to cache. FIXME someday.
108 query = parse_analyze_varparams((Node *) copyObject(stmt->query),
113 * Check that all parameter types were determined.
115 for (i = 0; i < nargs; i++)
117 Oid argtype = argtypes[i];
119 if (argtype == InvalidOid || argtype == UNKNOWNOID)
121 (errcode(ERRCODE_INDETERMINATE_DATATYPE),
122 errmsg("could not determine data type of parameter $%d",
127 * grammar only allows OptimizableStmt, so this check should be redundant
129 switch (query->commandType)
139 (errcode(ERRCODE_INVALID_PSTATEMENT_DEFINITION),
140 errmsg("utility statements cannot be prepared")));
144 /* Rewrite the query. The result could be 0, 1, or many queries. */
145 query_list = QueryRewrite(query);
147 /* Generate plans for queries. Snapshot is already set. */
148 plan_list = pg_plan_queries(query_list, 0, NULL, false);
153 StorePreparedStatement(stmt->name,
156 CreateCommandTag((Node *) query),
159 0, /* default cursor options */
165 * Implements the 'EXECUTE' utility statement.
167 * Note: this is one of very few places in the code that needs to deal with
168 * two query strings at once. The passed-in queryString is that of the
169 * EXECUTE, which we might need for error reporting while processing the
170 * parameter expressions. The query_string that we copy from the plan
171 * source is that of the original PREPARE.
174 ExecuteQuery(ExecuteStmt *stmt, const char *queryString,
175 ParamListInfo params,
176 DestReceiver *dest, char *completionTag)
178 PreparedStatement *entry;
181 ParamListInfo paramLI = NULL;
182 EState *estate = NULL;
186 /* Look it up in the hash table */
187 entry = FetchPreparedStatement(stmt->name, true);
189 /* Shouldn't have a non-fully-planned plancache entry */
190 if (!entry->plansource->fully_planned)
191 elog(ERROR, "EXECUTE does not support unplanned prepared statements");
192 /* Shouldn't get any non-fixed-result cached plan, either */
193 if (!entry->plansource->fixed_result)
194 elog(ERROR, "EXECUTE does not support variable-result cached plans");
196 /* Evaluate parameters, if any */
197 if (entry->plansource->num_params > 0)
200 * Need an EState to evaluate parameters; must not delete it till end
201 * of query, in case parameters are pass-by-reference.
203 estate = CreateExecutorState();
204 estate->es_param_list_info = params;
205 paramLI = EvaluateParams(entry, stmt->params,
206 queryString, estate);
209 /* Create a new portal to run the query in */
210 portal = CreateNewPortal();
211 /* Don't display the portal in pg_cursors, it is for internal use only */
212 portal->visible = false;
214 /* Copy the plan's saved query string into the portal's memory */
215 query_string = MemoryContextStrdup(PortalGetHeapMemory(portal),
216 entry->plansource->query_string);
219 * For CREATE TABLE / AS EXECUTE, we must make a copy of the stored query
220 * so that we can modify its destination (yech, but this has always been
221 * ugly). For regular EXECUTE we can just use the cached query, since the
222 * executor is read-only.
226 MemoryContext oldContext;
229 /* Replan if needed, and increment plan refcount transiently */
230 cplan = RevalidateCachedPlan(entry->plansource, true);
232 /* Copy plan into portal's context, and modify */
233 oldContext = MemoryContextSwitchTo(PortalGetHeapMemory(portal));
235 plan_list = copyObject(cplan->stmt_list);
237 if (list_length(plan_list) != 1)
239 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
240 errmsg("prepared statement is not a SELECT")));
241 pstmt = (PlannedStmt *) linitial(plan_list);
242 if (!IsA(pstmt, PlannedStmt) ||
243 pstmt->commandType != CMD_SELECT ||
244 pstmt->utilityStmt != NULL)
246 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
247 errmsg("prepared statement is not a SELECT")));
248 pstmt->intoClause = copyObject(stmt->into);
250 MemoryContextSwitchTo(oldContext);
252 /* We no longer need the cached plan refcount ... */
253 ReleaseCachedPlan(cplan, true);
254 /* ... and we don't want the portal to depend on it, either */
259 /* Replan if needed, and increment plan refcount for portal */
260 cplan = RevalidateCachedPlan(entry->plansource, false);
261 plan_list = cplan->stmt_list;
264 PortalDefineQuery(portal,
267 entry->plansource->commandTag,
272 * Run the portal to completion.
274 PortalStart(portal, paramLI, GetActiveSnapshot());
276 (void) PortalRun(portal, FETCH_ALL, false, dest, dest, completionTag);
278 PortalDrop(portal, false);
281 FreeExecutorState(estate);
283 /* No need to pfree other memory, MemoryContext will be reset */
287 * EvaluateParams: evaluate a list of parameters.
289 * pstmt: statement we are getting parameters for.
290 * params: list of given parameter expressions (raw parser output!)
291 * queryString: source text for error messages.
292 * estate: executor state to use.
294 * Returns a filled-in ParamListInfo -- this can later be passed to
295 * CreateQueryDesc(), which allows the executor to make use of the parameters
296 * during query execution.
299 EvaluateParams(PreparedStatement *pstmt, List *params,
300 const char *queryString, EState *estate)
302 Oid *param_types = pstmt->plansource->param_types;
303 int num_params = pstmt->plansource->num_params;
304 int nparams = list_length(params);
306 ParamListInfo paramLI;
311 if (nparams != num_params)
313 (errcode(ERRCODE_SYNTAX_ERROR),
314 errmsg("wrong number of parameters for prepared statement \"%s\"",
316 errdetail("Expected %d parameters but got %d.",
317 num_params, nparams)));
319 /* Quick exit if no parameters */
324 * We have to run parse analysis for the expressions. Since the parser is
325 * not cool about scribbling on its input, copy first.
327 params = (List *) copyObject(params);
329 pstate = make_parsestate(NULL);
330 pstate->p_sourcetext = queryString;
335 Node *expr = lfirst(l);
336 Oid expected_type_id = param_types[i];
339 expr = transformExpr(pstate, expr);
341 /* Cannot contain subselects or aggregates */
342 if (pstate->p_hasSubLinks)
344 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
345 errmsg("cannot use subquery in EXECUTE parameter")));
346 if (pstate->p_hasAggs)
348 (errcode(ERRCODE_GROUPING_ERROR),
349 errmsg("cannot use aggregate function in EXECUTE parameter")));
351 given_type_id = exprType(expr);
353 expr = coerce_to_target_type(pstate, expr, given_type_id,
354 expected_type_id, -1,
356 COERCE_IMPLICIT_CAST);
360 (errcode(ERRCODE_DATATYPE_MISMATCH),
361 errmsg("parameter $%d of type %s cannot be coerced to the expected type %s",
363 format_type_be(given_type_id),
364 format_type_be(expected_type_id)),
365 errhint("You will need to rewrite or cast the expression.")));
371 /* Prepare the expressions for execution */
372 exprstates = (List *) ExecPrepareExpr((Expr *) params, estate);
374 /* sizeof(ParamListInfoData) includes the first array element */
375 paramLI = (ParamListInfo)
376 palloc(sizeof(ParamListInfoData) +
377 (num_params - 1) *sizeof(ParamExternData));
378 paramLI->numParams = num_params;
381 foreach(l, exprstates)
383 ExprState *n = lfirst(l);
384 ParamExternData *prm = ¶mLI->params[i];
386 prm->ptype = param_types[i];
388 prm->value = ExecEvalExprSwitchContext(n,
389 GetPerTupleExprContext(estate),
401 * Initialize query hash table upon first use.
404 InitQueryHashTable(void)
408 MemSet(&hash_ctl, 0, sizeof(hash_ctl));
410 hash_ctl.keysize = NAMEDATALEN;
411 hash_ctl.entrysize = sizeof(PreparedStatement);
413 prepared_queries = hash_create("Prepared Queries",
420 * Store all the data pertaining to a query in the hash table using
421 * the specified key. All the given data is copied into either the hashtable
422 * entry or the underlying plancache entry, so the caller can dispose of its
425 * Exception: commandTag is presumed to be a pointer to a constant string,
426 * or possibly NULL, so it need not be copied. Note that commandTag should
427 * be NULL only if the original query (before rewriting) was empty.
430 StorePreparedStatement(const char *stmt_name,
431 Node *raw_parse_tree,
432 const char *query_string,
433 const char *commandTag,
440 PreparedStatement *entry;
441 CachedPlanSource *plansource;
444 /* Initialize the hash table, if necessary */
445 if (!prepared_queries)
446 InitQueryHashTable();
448 /* Check for pre-existing entry of same name */
449 hash_search(prepared_queries, stmt_name, HASH_FIND, &found);
453 (errcode(ERRCODE_DUPLICATE_PSTATEMENT),
454 errmsg("prepared statement \"%s\" already exists",
457 /* Create a plancache entry */
458 plansource = CreateCachedPlan(raw_parse_tree,
468 /* Now we can add entry to hash table */
469 entry = (PreparedStatement *) hash_search(prepared_queries,
474 /* Shouldn't get a duplicate entry */
476 elog(ERROR, "duplicate prepared statement \"%s\"",
479 /* Fill in the hash table entry */
480 entry->plansource = plansource;
481 entry->from_sql = from_sql;
482 entry->prepare_time = GetCurrentStatementStartTimestamp();
486 * Lookup an existing query in the hash table. If the query does not
487 * actually exist, throw ereport(ERROR) or return NULL per second parameter.
489 * Note: this does not force the referenced plancache entry to be valid,
490 * since not all callers care.
493 FetchPreparedStatement(const char *stmt_name, bool throwError)
495 PreparedStatement *entry;
498 * If the hash table hasn't been initialized, it can't be storing
499 * anything, therefore it couldn't possibly store our plan.
501 if (prepared_queries)
502 entry = (PreparedStatement *) hash_search(prepared_queries,
509 if (!entry && throwError)
511 (errcode(ERRCODE_UNDEFINED_PSTATEMENT),
512 errmsg("prepared statement \"%s\" does not exist",
519 * Given a prepared statement, determine the result tupledesc it will
520 * produce. Returns NULL if the execution will not return tuples.
522 * Note: the result is created or copied into current memory context.
525 FetchPreparedStatementResultDesc(PreparedStatement *stmt)
528 * Since we don't allow prepared statements' result tupdescs to change,
529 * there's no need for a revalidate call here.
531 Assert(stmt->plansource->fixed_result);
532 if (stmt->plansource->resultDesc)
533 return CreateTupleDescCopy(stmt->plansource->resultDesc);
539 * Given a prepared statement that returns tuples, extract the query
540 * targetlist. Returns NIL if the statement doesn't have a determinable
543 * Note: this is pretty ugly, but since it's only used in corner cases like
544 * Describe Statement on an EXECUTE command, we don't worry too much about
548 FetchPreparedStatementTargetList(PreparedStatement *stmt)
553 /* No point in looking if it doesn't return tuples */
554 if (stmt->plansource->resultDesc == NULL)
557 /* Make sure the plan is up to date */
558 cplan = RevalidateCachedPlan(stmt->plansource, true);
560 /* Get the primary statement and find out what it returns */
561 tlist = FetchStatementTargetList(PortalListGetPrimaryStmt(cplan->stmt_list));
563 /* Copy into caller's context so we can release the plancache entry */
564 tlist = (List *) copyObject(tlist);
566 ReleaseCachedPlan(cplan, true);
572 * Implements the 'DEALLOCATE' utility statement: deletes the
573 * specified plan from storage.
576 DeallocateQuery(DeallocateStmt *stmt)
579 DropPreparedStatement(stmt->name, true);
581 DropAllPreparedStatements();
585 * Internal version of DEALLOCATE
587 * If showError is false, dropping a nonexistent statement is a no-op.
590 DropPreparedStatement(const char *stmt_name, bool showError)
592 PreparedStatement *entry;
594 /* Find the query's hash table entry; raise error if wanted */
595 entry = FetchPreparedStatement(stmt_name, showError);
599 /* Release the plancache entry */
600 DropCachedPlan(entry->plansource);
602 /* Now we can remove the hash table entry */
603 hash_search(prepared_queries, entry->stmt_name, HASH_REMOVE, NULL);
608 * Drop all cached statements.
611 DropAllPreparedStatements(void)
614 PreparedStatement *entry;
617 if (!prepared_queries)
620 /* walk over cache */
621 hash_seq_init(&seq, prepared_queries);
622 while ((entry = hash_seq_search(&seq)) != NULL)
624 /* Release the plancache entry */
625 DropCachedPlan(entry->plansource);
627 /* Now we can remove the hash table entry */
628 hash_search(prepared_queries, entry->stmt_name, HASH_REMOVE, NULL);
633 * Implements the 'EXPLAIN EXECUTE' utility statement.
636 ExplainExecuteQuery(ExecuteStmt *execstmt, ExplainStmt *stmt,
637 const char *queryString,
638 ParamListInfo params, TupOutputState *tstate)
640 PreparedStatement *entry;
644 ParamListInfo paramLI = NULL;
645 EState *estate = NULL;
647 /* Look it up in the hash table */
648 entry = FetchPreparedStatement(execstmt->name, true);
650 /* Shouldn't have a non-fully-planned plancache entry */
651 if (!entry->plansource->fully_planned)
652 elog(ERROR, "EXPLAIN EXECUTE does not support unplanned prepared statements");
653 /* Shouldn't get any non-fixed-result cached plan, either */
654 if (!entry->plansource->fixed_result)
655 elog(ERROR, "EXPLAIN EXECUTE does not support variable-result cached plans");
657 /* Replan if needed, and acquire a transient refcount */
658 cplan = RevalidateCachedPlan(entry->plansource, true);
660 plan_list = cplan->stmt_list;
662 /* Evaluate parameters, if any */
663 if (entry->plansource->num_params)
666 * Need an EState to evaluate parameters; must not delete it till end
667 * of query, in case parameters are pass-by-reference.
669 estate = CreateExecutorState();
670 estate->es_param_list_info = params;
671 paramLI = EvaluateParams(entry, execstmt->params,
672 queryString, estate);
675 /* Explain each query */
676 foreach(p, plan_list)
678 PlannedStmt *pstmt = (PlannedStmt *) lfirst(p);
681 is_last_query = (lnext(p) == NULL);
683 if (IsA(pstmt, PlannedStmt))
687 if (pstmt->commandType != CMD_SELECT ||
688 pstmt->utilityStmt != NULL)
690 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
691 errmsg("prepared statement is not a SELECT")));
693 /* Copy the stmt so we can modify it */
694 pstmt = copyObject(pstmt);
696 pstmt->intoClause = execstmt->into;
699 ExplainOnePlan(pstmt, paramLI, stmt, tstate);
703 ExplainOneUtility((Node *) pstmt, stmt, queryString,
707 /* No need for CommandCounterIncrement, as ExplainOnePlan did it */
709 /* put a blank line between plans */
711 do_text_output_oneline(tstate, "");
715 FreeExecutorState(estate);
717 ReleaseCachedPlan(cplan, true);
721 * This set returning function reads all the prepared statements and
722 * returns a set of (name, statement, prepare_time, param_types, from_sql).
725 pg_prepared_statement(PG_FUNCTION_ARGS)
727 ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
729 Tuplestorestate *tupstore;
730 MemoryContext per_query_ctx;
731 MemoryContext oldcontext;
733 /* check to see if caller supports us returning a tuplestore */
734 if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
736 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
737 errmsg("set-valued function called in context that cannot accept a set")));
738 if (!(rsinfo->allowedModes & SFRM_Materialize))
740 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
741 errmsg("materialize mode required, but it is not " \
742 "allowed in this context")));
744 /* need to build tuplestore in query context */
745 per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
746 oldcontext = MemoryContextSwitchTo(per_query_ctx);
749 * build tupdesc for result tuples. This must match the definition of the
750 * pg_prepared_statements view in system_views.sql
752 tupdesc = CreateTemplateTupleDesc(5, false);
753 TupleDescInitEntry(tupdesc, (AttrNumber) 1, "name",
755 TupleDescInitEntry(tupdesc, (AttrNumber) 2, "statement",
757 TupleDescInitEntry(tupdesc, (AttrNumber) 3, "prepare_time",
758 TIMESTAMPTZOID, -1, 0);
759 TupleDescInitEntry(tupdesc, (AttrNumber) 4, "parameter_types",
760 REGTYPEARRAYOID, -1, 0);
761 TupleDescInitEntry(tupdesc, (AttrNumber) 5, "from_sql",
765 * We put all the tuples into a tuplestore in one scan of the hashtable.
766 * This avoids any issue of the hashtable possibly changing between calls.
768 tupstore = tuplestore_begin_heap(true, false, work_mem);
770 /* hash table might be uninitialized */
771 if (prepared_queries)
773 HASH_SEQ_STATUS hash_seq;
774 PreparedStatement *prep_stmt;
776 hash_seq_init(&hash_seq, prepared_queries);
777 while ((prep_stmt = hash_seq_search(&hash_seq)) != NULL)
782 /* generate junk in short-term context */
783 MemoryContextSwitchTo(oldcontext);
785 MemSet(nulls, 0, sizeof(nulls));
787 values[0] = CStringGetTextDatum(prep_stmt->stmt_name);
788 values[1] = CStringGetTextDatum(prep_stmt->plansource->query_string);
789 values[2] = TimestampTzGetDatum(prep_stmt->prepare_time);
790 values[3] = build_regtype_array(prep_stmt->plansource->param_types,
791 prep_stmt->plansource->num_params);
792 values[4] = BoolGetDatum(prep_stmt->from_sql);
794 /* switch to appropriate context while storing the tuple */
795 MemoryContextSwitchTo(per_query_ctx);
796 tuplestore_putvalues(tupstore, tupdesc, values, nulls);
800 /* clean up and return the tuplestore */
801 tuplestore_donestoring(tupstore);
803 MemoryContextSwitchTo(oldcontext);
805 rsinfo->returnMode = SFRM_Materialize;
806 rsinfo->setResult = tupstore;
807 rsinfo->setDesc = tupdesc;
813 * This utility function takes a C array of Oids, and returns a Datum
814 * pointing to a one-dimensional Postgres array of regtypes. An empty
815 * array is returned as a zero-element array, not NULL.
818 build_regtype_array(Oid *param_types, int num_params)
824 tmp_ary = (Datum *) palloc(num_params * sizeof(Datum));
826 for (i = 0; i < num_params; i++)
827 tmp_ary[i] = ObjectIdGetDatum(param_types[i]);
829 /* XXX: this hardcodes assumptions about the regtype type */
830 result = construct_array(tmp_ary, num_params, REGTYPEOID, 4, true, 'i');
831 return PointerGetDatum(result);