OSDN Git Service

pgindent run.
[pg-rex/syncrep.git] / src / backend / commands / prepare.c
1 /*-------------------------------------------------------------------------
2  *
3  * prepare.c
4  *        Prepareable SQL statements via PREPARE, EXECUTE and DEALLOCATE
5  *
6  * Copyright (c) 2002, PostgreSQL Global Development Group
7  *
8  * IDENTIFICATION
9  *        $Header: /cvsroot/pgsql/src/backend/commands/prepare.c,v 1.2 2002/09/04 20:31:15 momjian Exp $
10  *
11  *-------------------------------------------------------------------------
12  */
13 #include "postgres.h"
14
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"
25
26
27 #define HASH_KEY_LEN NAMEDATALEN
28
29 /* All the data we need to remember about a stored query */
30 typedef struct
31 {
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 */
38 } QueryHashEntry;
39
40 /*
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.
45  */
46 static HTAB *prepared_queries = NULL;
47
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);
53
54
55 /*
56  * Implements the 'PREPARE' utility statement.
57  */
58 void
59 PrepareQuery(PrepareStmt *stmt)
60 {
61         List       *plan_list = NIL;
62         List       *query_list,
63                            *query_list_item;
64
65         if (!stmt->name)
66                 elog(ERROR, "No statement name given");
67
68         if (stmt->query->commandType == CMD_UTILITY)
69                 elog(ERROR, "Utility statements cannot be prepared");
70
71         /* Rewrite the query. The result could be 0, 1, or many queries. */
72         query_list = QueryRewrite(stmt->query);
73
74         foreach(query_list_item, query_list)
75         {
76                 Query      *query = (Query *) lfirst(query_list_item);
77                 Plan       *plan;
78
79                 /* We can't generate plans for utility statements. */
80                 if (query->commandType == CMD_UTILITY)
81                         plan = NULL;
82                 else
83                 {
84                         /* Call the query planner to generate a plan. */
85                         plan = planner(query);
86                 }
87
88                 plan_list = lappend(plan_list, plan);
89         }
90
91         StoreQuery(stmt->name, query_list, plan_list, stmt->argtype_oids);
92 }
93
94 /*
95  * Implements the 'EXECUTE' utility statement.
96  */
97 void
98 ExecuteQuery(ExecuteStmt *stmt, CommandDest outputDest)
99 {
100         QueryHashEntry *entry;
101         List       *l,
102                            *query_list,
103                            *plan_list;
104         ParamListInfo paramLI = NULL;
105
106         /* Look it up in the hash table */
107         entry = FetchQuery(stmt->name);
108
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);
112
113         Assert(length(query_list) == length(plan_list));
114
115         /* Evaluate parameters, if any */
116         if (entry->argtype_list != NIL)
117         {
118                 int                     nargs = length(entry->argtype_list);
119                 int                     i = 0;
120                 ExprContext *econtext = MakeExprContext(NULL, CurrentMemoryContext);
121
122                 /* Parser should have caught this error, but check */
123                 if (nargs != length(stmt->params))
124                         elog(ERROR, "ExecuteQuery: wrong number of arguments");
125
126                 paramLI = (ParamListInfo) palloc((nargs + 1) * sizeof(ParamListInfoData));
127                 MemSet(paramLI, 0, (nargs + 1) * sizeof(ParamListInfoData));
128
129                 foreach(l, stmt->params)
130                 {
131                         Node       *n = lfirst(l);
132                         bool            isNull;
133
134                         paramLI[i].value = ExecEvalExprSwitchContext(n,
135                                                                                                                  econtext,
136                                                                                                                  &isNull,
137                                                                                                                  NULL);
138                         paramLI[i].kind = PARAM_NUM;
139                         paramLI[i].id = i + 1;
140                         paramLI[i].isnull = isNull;
141
142                         i++;
143                 }
144                 paramLI[i].kind = PARAM_INVALID;
145         }
146
147         /* Execute each query */
148         foreach(l, query_list)
149         {
150                 Query      *query = lfirst(l);
151                 Plan       *plan = lfirst(plan_list);
152                 bool            is_last_query;
153
154                 plan_list = lnext(plan_list);
155                 is_last_query = (plan_list == NIL);
156
157                 if (query->commandType == CMD_UTILITY)
158                         ProcessUtility(query->utilityStmt, outputDest, NULL);
159                 else
160                 {
161                         QueryDesc  *qdesc;
162                         EState     *state;
163
164                         if (Show_executor_stats)
165                                 ResetUsage();
166
167                         qdesc = CreateQueryDesc(query, plan, outputDest, NULL);
168                         state = CreateExecutorState();
169
170                         state->es_param_list_info = paramLI;
171
172                         if (stmt->into)
173                         {
174                                 if (qdesc->operation != CMD_SELECT)
175                                         elog(ERROR, "INTO clause specified for non-SELECT query");
176
177                                 query->into = stmt->into;
178                                 qdesc->dest = None;
179                         }
180
181                         RunQuery(qdesc, state);
182
183                         if (Show_executor_stats)
184                                 ShowUsage("EXECUTOR STATISTICS");
185                 }
186
187                 /*
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.
191                  */
192                 if (!is_last_query)
193                         CommandCounterIncrement();
194         }
195
196         /* No need to pfree memory, MemoryContext will be reset */
197 }
198
199 /*
200  * Initialize query hash table upon first use.
201  */
202 static void
203 InitQueryHashTable(void)
204 {
205         HASHCTL         hash_ctl;
206
207         MemSet(&hash_ctl, 0, sizeof(hash_ctl));
208
209         hash_ctl.keysize = HASH_KEY_LEN;
210         hash_ctl.entrysize = sizeof(QueryHashEntry);
211
212         prepared_queries = hash_create("Prepared Queries",
213                                                                    32,
214                                                                    &hash_ctl,
215                                                                    HASH_ELEM);
216
217         if (!prepared_queries)
218                 elog(ERROR, "InitQueryHashTable: unable to create hash table");
219 }
220
221 /*
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.
225  */
226 static void
227 StoreQuery(const char *stmt_name, List *query_list, List *plan_list,
228                    List *argtype_list)
229 {
230         QueryHashEntry *entry;
231         MemoryContext oldcxt,
232                                 entrycxt;
233         char            key[HASH_KEY_LEN];
234         bool            found;
235
236         /* Initialize the hash table, if necessary */
237         if (!prepared_queries)
238                 InitQueryHashTable();
239
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));
244
245         hash_search(prepared_queries, key, HASH_FIND, &found);
246
247         if (found)
248                 elog(ERROR, "Prepared statement with name \"%s\" already exists",
249                          stmt_name);
250
251         /* Okay. Make a permanent memory context for the hashtable entry */
252         entrycxt = AllocSetContextCreate(TopMemoryContext,
253                                                                          stmt_name,
254                                                                          1024,
255                                                                          1024,
256                                                                          ALLOCSET_DEFAULT_MAXSIZE);
257
258         oldcxt = MemoryContextSwitchTo(entrycxt);
259
260         /*
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.
265          */
266         query_list = (List *) copyObject(query_list);
267         plan_list = (List *) copyObject(plan_list);
268         argtype_list = listCopy(argtype_list);
269
270         /* Now we can add entry to hash table */
271         entry = (QueryHashEntry *) hash_search(prepared_queries,
272                                                                                    key,
273                                                                                    HASH_ENTER,
274                                                                                    &found);
275
276         /* Shouldn't get a failure, nor duplicate entry */
277         if (!entry || found)
278                 elog(ERROR, "Unable to store prepared statement \"%s\"!",
279                          stmt_name);
280
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;
286
287         MemoryContextSwitchTo(oldcxt);
288 }
289
290 /*
291  * Lookup an existing query in the hash table.
292  */
293 static QueryHashEntry *
294 FetchQuery(const char *plan_name)
295 {
296         char            key[HASH_KEY_LEN];
297         QueryHashEntry *entry;
298
299         /*
300          * If the hash table hasn't been initialized, it can't be storing
301          * anything, therefore it couldn't possibly store our plan.
302          */
303         if (!prepared_queries)
304                 elog(ERROR, "Prepared statement with name \"%s\" does not exist",
305                          plan_name);
306
307         /*
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.
311          */
312         MemSet(key, 0, sizeof(key));
313         strncpy(key, plan_name, sizeof(key));
314
315         entry = (QueryHashEntry *) hash_search(prepared_queries,
316                                                                                    key,
317                                                                                    HASH_FIND,
318                                                                                    NULL);
319
320         if (!entry)
321                 elog(ERROR, "Prepared statement with name \"%s\" does not exist",
322                          plan_name);
323
324         return entry;
325 }
326
327 /*
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.
330  */
331 List *
332 FetchQueryParams(const char *plan_name)
333 {
334         QueryHashEntry *entry;
335
336         entry = FetchQuery(plan_name);
337
338         return entry->argtype_list;
339 }
340
341 /*
342  * Actually execute a prepared query.
343  */
344 static void
345 RunQuery(QueryDesc *qdesc, EState *state)
346 {
347         TupleDesc       tupdesc;
348
349         tupdesc = ExecutorStart(qdesc, state);
350
351         ExecutorRun(qdesc, state, state->es_direction, 0L);
352
353         ExecutorEnd(qdesc, state);
354 }
355
356 /*
357  * Implements the 'DEALLOCATE' utility statement: deletes the
358  * specified plan from storage.
359  *
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.
362  */
363 void
364 DeallocateQuery(DeallocateStmt *stmt)
365 {
366         char            key[HASH_KEY_LEN];
367         QueryHashEntry *entry;
368
369         /*
370          * If the hash table hasn't been initialized, it can't be storing
371          * anything, therefore it couldn't possibly store our plan.
372          */
373         if (!prepared_queries)
374                 elog(ERROR, "Prepared statement with name \"%s\" does not exist",
375                          stmt->name);
376
377         /*
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.
381          */
382         MemSet(key, 0, sizeof(key));
383         strncpy(key, stmt->name, sizeof(key));
384
385         /*
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.
391          */
392         entry = (QueryHashEntry *) hash_search(prepared_queries,
393                                                                                    key,
394                                                                                    HASH_FIND,
395                                                                                    NULL);
396
397         if (!entry)
398                 elog(ERROR, "Prepared statement with name \"%s\" does not exist",
399                          stmt->name);
400
401         /* Flush the context holding the subsidiary data */
402         if (MemoryContextIsValid(entry->context))
403                 MemoryContextDelete(entry->context);
404
405         /* Now we can remove the hash table entry */
406         hash_search(prepared_queries, key, HASH_REMOVE, NULL);
407 }