4646static HTAB * prepared_queries = NULL ;
4747
4848static void InitQueryHashTable (void );
49- static ParamListInfo EvaluateParams (PreparedStatement * pstmt , List * params ,
50- const char * queryString , EState * estate );
49+ static ParamListInfo EvaluateParams (ParseState * pstate ,
50+ PreparedStatement * pstmt , List * params ,
51+ EState * estate );
5152static Datum build_regtype_array (Oid * param_types , int num_params );
5253
5354/*
5455 * Implements the 'PREPARE' utility statement.
5556 */
5657void
57- PrepareQuery (PrepareStmt * stmt , const char * queryString ,
58+ PrepareQuery (ParseState * pstate , PrepareStmt * stmt ,
5859 int stmt_location , int stmt_len )
5960{
6061 RawStmt * rawstmt ;
@@ -90,24 +91,16 @@ PrepareQuery(PrepareStmt *stmt, const char *queryString,
9091 * Create the CachedPlanSource before we do parse analysis, since it needs
9192 * to see the unmodified raw parse tree.
9293 */
93- plansource = CreateCachedPlan (rawstmt , queryString ,
94+ plansource = CreateCachedPlan (rawstmt , pstate -> p_sourcetext ,
9495 CreateCommandTag (stmt -> query ));
9596
9697 /* Transform list of TypeNames to array of type OIDs */
9798 nargs = list_length (stmt -> argtypes );
9899
99100 if (nargs )
100101 {
101- ParseState * pstate ;
102102 ListCell * l ;
103103
104- /*
105- * typenameTypeId wants a ParseState to carry the source query string.
106- * Is it worth refactoring its API to avoid this?
107- */
108- pstate = make_parsestate (NULL );
109- pstate -> p_sourcetext = queryString ;
110-
111104 argtypes = (Oid * ) palloc (nargs * sizeof (Oid ));
112105 i = 0 ;
113106
@@ -125,7 +118,7 @@ PrepareQuery(PrepareStmt *stmt, const char *queryString,
125118 * passed in from above us will not be visible to it), allowing
126119 * information about unknown parameters to be deduced from context.
127120 */
128- query = parse_analyze_varparams (rawstmt , queryString ,
121+ query = parse_analyze_varparams (rawstmt , pstate -> p_sourcetext ,
129122 & argtypes , & nargs );
130123
131124 /*
@@ -189,16 +182,11 @@ PrepareQuery(PrepareStmt *stmt, const char *queryString,
189182 * indicated by passing a non-null intoClause. The DestReceiver is already
190183 * set up correctly for CREATE TABLE AS, but we still have to make a few
191184 * other adjustments here.
192- *
193- * Note: this is one of very few places in the code that needs to deal with
194- * two query strings at once. The passed-in queryString is that of the
195- * EXECUTE, which we might need for error reporting while processing the
196- * parameter expressions. The query_string that we copy from the plan
197- * source is that of the original PREPARE.
198185 */
199186void
200- ExecuteQuery (ExecuteStmt * stmt , IntoClause * intoClause ,
201- const char * queryString , ParamListInfo params ,
187+ ExecuteQuery (ParseState * pstate ,
188+ ExecuteStmt * stmt , IntoClause * intoClause ,
189+ ParamListInfo params ,
202190 DestReceiver * dest , char * completionTag )
203191{
204192 PreparedStatement * entry ;
@@ -229,8 +217,7 @@ ExecuteQuery(ExecuteStmt *stmt, IntoClause *intoClause,
229217 */
230218 estate = CreateExecutorState ();
231219 estate -> es_param_list_info = params ;
232- paramLI = EvaluateParams (entry , stmt -> params ,
233- queryString , estate );
220+ paramLI = EvaluateParams (pstate , entry , stmt -> params , estate );
234221 }
235222
236223 /* Create a new portal to run the query in */
@@ -314,23 +301,22 @@ ExecuteQuery(ExecuteStmt *stmt, IntoClause *intoClause,
314301/*
315302 * EvaluateParams: evaluate a list of parameters.
316303 *
304+ * pstate: parse state
317305 * pstmt: statement we are getting parameters for.
318306 * params: list of given parameter expressions (raw parser output!)
319- * queryString: source text for error messages.
320307 * estate: executor state to use.
321308 *
322309 * Returns a filled-in ParamListInfo -- this can later be passed to
323310 * CreateQueryDesc(), which allows the executor to make use of the parameters
324311 * during query execution.
325312 */
326313static ParamListInfo
327- EvaluateParams (PreparedStatement * pstmt , List * params ,
328- const char * queryString , EState * estate )
314+ EvaluateParams (ParseState * pstate , PreparedStatement * pstmt , List * params ,
315+ EState * estate )
329316{
330317 Oid * param_types = pstmt -> plansource -> param_types ;
331318 int num_params = pstmt -> plansource -> num_params ;
332319 int nparams = list_length (params );
333- ParseState * pstate ;
334320 ParamListInfo paramLI ;
335321 List * exprstates ;
336322 ListCell * l ;
@@ -354,9 +340,6 @@ EvaluateParams(PreparedStatement *pstmt, List *params,
354340 */
355341 params = copyObject (params );
356342
357- pstate = make_parsestate (NULL );
358- pstate -> p_sourcetext = queryString ;
359-
360343 i = 0 ;
361344 foreach (l , params )
362345 {
@@ -648,6 +631,11 @@ ExplainExecuteQuery(ExecuteStmt *execstmt, IntoClause *into, ExplainState *es,
648631 /* Evaluate parameters, if any */
649632 if (entry -> plansource -> num_params )
650633 {
634+ ParseState * pstate ;
635+
636+ pstate = make_parsestate (NULL );
637+ pstate -> p_sourcetext = queryString ;
638+
651639 /*
652640 * Need an EState to evaluate parameters; must not delete it till end
653641 * of query, in case parameters are pass-by-reference. Note that the
@@ -656,8 +644,8 @@ ExplainExecuteQuery(ExecuteStmt *execstmt, IntoClause *into, ExplainState *es,
656644 */
657645 estate = CreateExecutorState ();
658646 estate -> es_param_list_info = params ;
659- paramLI = EvaluateParams ( entry , execstmt -> params ,
660- queryString , estate );
647+
648+ paramLI = EvaluateParams ( pstate , entry , execstmt -> params , estate );
661649 }
662650
663651 /* Replan if needed, and acquire a transient refcount */
0 commit comments