1 /*-------------------------------------------------------------------------
4 * Prepareable SQL statements via PREPARE, EXECUTE and DEALLOCATE
6 * Copyright (c) 2002, PostgreSQL Global Development Group
9 * $Header: /cvsroot/pgsql/src/backend/commands/prepare.c,v 1.2 2002/09/04 20:31:15 momjian Exp $
11 *-------------------------------------------------------------------------
15 #include "commands/prepare.h"
16 #include "executor/executor.h"
17 #include "utils/guc.h"
18 #include "optimizer/planner.h"
19 #include "rewrite/rewriteHandler.h"
20 #include "tcop/pquery.h"
21 #include "tcop/tcopprot.h"
22 #include "tcop/utility.h"
23 #include "utils/hsearch.h"
24 #include "utils/memutils.h"
27 #define HASH_KEY_LEN NAMEDATALEN
29 /* All the data we need to remember about a stored query */
32 /* dynahash.c requires key to be first field */
33 char key[HASH_KEY_LEN];
34 List *query_list; /* list of queries */
35 List *plan_list; /* list of plans */
36 List *argtype_list; /* list of parameter type OIDs */
37 MemoryContext context; /* context containing this query */
41 * The hash table in which prepared queries are stored. This is
42 * per-backend: query plans are not shared between backends.
43 * The keys for this hash table are the arguments to PREPARE
44 * and EXECUTE ("plan names"); the entries are QueryHashEntry structs.
46 static HTAB *prepared_queries = NULL;
48 static void InitQueryHashTable(void);
49 static void StoreQuery(const char *stmt_name, List *query_list,
50 List *plan_list, List *argtype_list);
51 static QueryHashEntry *FetchQuery(const char *plan_name);
52 static void RunQuery(QueryDesc *qdesc, EState *state);
56 * Implements the 'PREPARE' utility statement.
59 PrepareQuery(PrepareStmt *stmt)
61 List *plan_list = NIL;
66 elog(ERROR, "No statement name given");
68 if (stmt->query->commandType == CMD_UTILITY)
69 elog(ERROR, "Utility statements cannot be prepared");
71 /* Rewrite the query. The result could be 0, 1, or many queries. */
72 query_list = QueryRewrite(stmt->query);
74 foreach(query_list_item, query_list)
76 Query *query = (Query *) lfirst(query_list_item);
79 /* We can't generate plans for utility statements. */
80 if (query->commandType == CMD_UTILITY)
84 /* Call the query planner to generate a plan. */
85 plan = planner(query);
88 plan_list = lappend(plan_list, plan);
91 StoreQuery(stmt->name, query_list, plan_list, stmt->argtype_oids);
95 * Implements the 'EXECUTE' utility statement.
98 ExecuteQuery(ExecuteStmt *stmt, CommandDest outputDest)
100 QueryHashEntry *entry;
104 ParamListInfo paramLI = NULL;
106 /* Look it up in the hash table */
107 entry = FetchQuery(stmt->name);
109 /* Make working copies the executor can safely scribble on */
110 query_list = (List *) copyObject(entry->query_list);
111 plan_list = (List *) copyObject(entry->plan_list);
113 Assert(length(query_list) == length(plan_list));
115 /* Evaluate parameters, if any */
116 if (entry->argtype_list != NIL)
118 int nargs = length(entry->argtype_list);
120 ExprContext *econtext = MakeExprContext(NULL, CurrentMemoryContext);
122 /* Parser should have caught this error, but check */
123 if (nargs != length(stmt->params))
124 elog(ERROR, "ExecuteQuery: wrong number of arguments");
126 paramLI = (ParamListInfo) palloc((nargs + 1) * sizeof(ParamListInfoData));
127 MemSet(paramLI, 0, (nargs + 1) * sizeof(ParamListInfoData));
129 foreach(l, stmt->params)
134 paramLI[i].value = ExecEvalExprSwitchContext(n,
138 paramLI[i].kind = PARAM_NUM;
139 paramLI[i].id = i + 1;
140 paramLI[i].isnull = isNull;
144 paramLI[i].kind = PARAM_INVALID;
147 /* Execute each query */
148 foreach(l, query_list)
150 Query *query = lfirst(l);
151 Plan *plan = lfirst(plan_list);
154 plan_list = lnext(plan_list);
155 is_last_query = (plan_list == NIL);
157 if (query->commandType == CMD_UTILITY)
158 ProcessUtility(query->utilityStmt, outputDest, NULL);
164 if (Show_executor_stats)
167 qdesc = CreateQueryDesc(query, plan, outputDest, NULL);
168 state = CreateExecutorState();
170 state->es_param_list_info = paramLI;
174 if (qdesc->operation != CMD_SELECT)
175 elog(ERROR, "INTO clause specified for non-SELECT query");
177 query->into = stmt->into;
181 RunQuery(qdesc, state);
183 if (Show_executor_stats)
184 ShowUsage("EXECUTOR STATISTICS");
188 * If we're processing multiple queries, we need to increment the
189 * command counter between them. For the last query, there's no
190 * need to do this, it's done automatically.
193 CommandCounterIncrement();
196 /* No need to pfree memory, MemoryContext will be reset */
200 * Initialize query hash table upon first use.
203 InitQueryHashTable(void)
207 MemSet(&hash_ctl, 0, sizeof(hash_ctl));
209 hash_ctl.keysize = HASH_KEY_LEN;
210 hash_ctl.entrysize = sizeof(QueryHashEntry);
212 prepared_queries = hash_create("Prepared Queries",
217 if (!prepared_queries)
218 elog(ERROR, "InitQueryHashTable: unable to create hash table");
222 * Store all the data pertaining to a query in the hash table using
223 * the specified key. A copy of the data is made in a memory context belonging
224 * to the hash entry, so the caller can dispose of their copy.
227 StoreQuery(const char *stmt_name, List *query_list, List *plan_list,
230 QueryHashEntry *entry;
231 MemoryContext oldcxt,
233 char key[HASH_KEY_LEN];
236 /* Initialize the hash table, if necessary */
237 if (!prepared_queries)
238 InitQueryHashTable();
240 /* Check for pre-existing entry of same name */
241 /* See notes in FetchQuery */
242 MemSet(key, 0, sizeof(key));
243 strncpy(key, stmt_name, sizeof(key));
245 hash_search(prepared_queries, key, HASH_FIND, &found);
248 elog(ERROR, "Prepared statement with name \"%s\" already exists",
251 /* Okay. Make a permanent memory context for the hashtable entry */
252 entrycxt = AllocSetContextCreate(TopMemoryContext,
256 ALLOCSET_DEFAULT_MAXSIZE);
258 oldcxt = MemoryContextSwitchTo(entrycxt);
261 * We need to copy the data so that it is stored in the correct memory
262 * context. Do this before making hashtable entry, so that an
263 * out-of-memory failure only wastes memory and doesn't leave us with
264 * an incomplete (ie corrupt) hashtable entry.
266 query_list = (List *) copyObject(query_list);
267 plan_list = (List *) copyObject(plan_list);
268 argtype_list = listCopy(argtype_list);
270 /* Now we can add entry to hash table */
271 entry = (QueryHashEntry *) hash_search(prepared_queries,
276 /* Shouldn't get a failure, nor duplicate entry */
278 elog(ERROR, "Unable to store prepared statement \"%s\"!",
281 /* Fill in the hash table entry with copied data */
282 entry->query_list = query_list;
283 entry->plan_list = plan_list;
284 entry->argtype_list = argtype_list;
285 entry->context = entrycxt;
287 MemoryContextSwitchTo(oldcxt);
291 * Lookup an existing query in the hash table.
293 static QueryHashEntry *
294 FetchQuery(const char *plan_name)
296 char key[HASH_KEY_LEN];
297 QueryHashEntry *entry;
300 * If the hash table hasn't been initialized, it can't be storing
301 * anything, therefore it couldn't possibly store our plan.
303 if (!prepared_queries)
304 elog(ERROR, "Prepared statement with name \"%s\" does not exist",
308 * We can't just use the statement name as supplied by the user: the
309 * hash package is picky enough that it needs to be NULL-padded out to
310 * the appropriate length to work correctly.
312 MemSet(key, 0, sizeof(key));
313 strncpy(key, plan_name, sizeof(key));
315 entry = (QueryHashEntry *) hash_search(prepared_queries,
321 elog(ERROR, "Prepared statement with name \"%s\" does not exist",
328 * Given a plan name, look up the stored plan (giving error if not found).
329 * If found, return the list of argument type OIDs.
332 FetchQueryParams(const char *plan_name)
334 QueryHashEntry *entry;
336 entry = FetchQuery(plan_name);
338 return entry->argtype_list;
342 * Actually execute a prepared query.
345 RunQuery(QueryDesc *qdesc, EState *state)
349 tupdesc = ExecutorStart(qdesc, state);
351 ExecutorRun(qdesc, state, state->es_direction, 0L);
353 ExecutorEnd(qdesc, state);
357 * Implements the 'DEALLOCATE' utility statement: deletes the
358 * specified plan from storage.
360 * The initial part of this routine is identical to FetchQuery(),
361 * but we repeat the coding because we need to use the key twice.
364 DeallocateQuery(DeallocateStmt *stmt)
366 char key[HASH_KEY_LEN];
367 QueryHashEntry *entry;
370 * If the hash table hasn't been initialized, it can't be storing
371 * anything, therefore it couldn't possibly store our plan.
373 if (!prepared_queries)
374 elog(ERROR, "Prepared statement with name \"%s\" does not exist",
378 * We can't just use the statement name as supplied by the user: the
379 * hash package is picky enough that it needs to be NULL-padded out to
380 * the appropriate length to work correctly.
382 MemSet(key, 0, sizeof(key));
383 strncpy(key, stmt->name, sizeof(key));
386 * First lookup the entry, so we can release all the subsidiary memory
387 * it has allocated (when it's removed, hash_search() will return a
388 * dangling pointer, so it needs to be done prior to HASH_REMOVE).
389 * This requires an extra hash-table lookup, but DEALLOCATE isn't
390 * exactly a performance bottleneck.
392 entry = (QueryHashEntry *) hash_search(prepared_queries,
398 elog(ERROR, "Prepared statement with name \"%s\" does not exist",
401 /* Flush the context holding the subsidiary data */
402 if (MemoryContextIsValid(entry->context))
403 MemoryContextDelete(entry->context);
405 /* Now we can remove the hash table entry */
406 hash_search(prepared_queries, key, HASH_REMOVE, NULL);