mirror of
				https://github.com/postgres/postgres.git
				synced 2025-11-03 09:13:20 +03:00 
			
		
		
		
	Add pg_analyze_and_rewrite_varparams()
This new function extracts common code from PrepareQuery() and exec_parse_message(). It is then exactly analogous to the existing pg_analyze_and_rewrite_fixedparams() and pg_analyze_and_rewrite_withcb(). To unify these two code paths, this makes PrepareQuery() now subject to log_parser_stats. Also, both paths now invoke TRACE_POSTGRESQL_QUERY_REWRITE_START(). PrepareQuery() no longer checks whether a utility statement was specified. The grammar doesn't allow that anyway, and exec_parse_message() supports it, so restricting it doesn't seem necessary. This also adds QueryEnvironment support to the *varparams functions, for consistency with its cousins, even though it is not used right now. Reviewed-by: Nathan Bossart <bossartn@amazon.com> Discussion: https://www.postgresql.org/message-id/flat/c67ce276-52b4-0239-dc0e-39875bf81840@enterprisedb.com
This commit is contained in:
		@@ -63,9 +63,7 @@ PrepareQuery(ParseState *pstate, PrepareStmt *stmt,
 | 
			
		||||
	CachedPlanSource *plansource;
 | 
			
		||||
	Oid		   *argtypes = NULL;
 | 
			
		||||
	int			nargs;
 | 
			
		||||
	Query	   *query;
 | 
			
		||||
	List	   *query_list;
 | 
			
		||||
	int			i;
 | 
			
		||||
 | 
			
		||||
	/*
 | 
			
		||||
	 * Disallow empty-string statement name (conflicts with protocol-level
 | 
			
		||||
@@ -97,6 +95,7 @@ PrepareQuery(ParseState *pstate, PrepareStmt *stmt,
 | 
			
		||||
 | 
			
		||||
	if (nargs)
 | 
			
		||||
	{
 | 
			
		||||
		int			i;
 | 
			
		||||
		ListCell   *l;
 | 
			
		||||
 | 
			
		||||
		argtypes = (Oid *) palloc(nargs * sizeof(Oid));
 | 
			
		||||
@@ -115,44 +114,10 @@ PrepareQuery(ParseState *pstate, PrepareStmt *stmt,
 | 
			
		||||
	 * Analyze the statement using these parameter types (any parameters
 | 
			
		||||
	 * passed in from above us will not be visible to it), allowing
 | 
			
		||||
	 * information about unknown parameters to be deduced from context.
 | 
			
		||||
	 * Rewrite the query. The result could be 0, 1, or many queries.
 | 
			
		||||
	 */
 | 
			
		||||
	query = parse_analyze_varparams(rawstmt, pstate->p_sourcetext,
 | 
			
		||||
									&argtypes, &nargs);
 | 
			
		||||
 | 
			
		||||
	/*
 | 
			
		||||
	 * Check that all parameter types were determined.
 | 
			
		||||
	 */
 | 
			
		||||
	for (i = 0; i < nargs; i++)
 | 
			
		||||
	{
 | 
			
		||||
		Oid			argtype = argtypes[i];
 | 
			
		||||
 | 
			
		||||
		if (argtype == InvalidOid || argtype == UNKNOWNOID)
 | 
			
		||||
			ereport(ERROR,
 | 
			
		||||
					(errcode(ERRCODE_INDETERMINATE_DATATYPE),
 | 
			
		||||
					 errmsg("could not determine data type of parameter $%d",
 | 
			
		||||
							i + 1)));
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/*
 | 
			
		||||
	 * grammar only allows PreparableStmt, so this check should be redundant
 | 
			
		||||
	 */
 | 
			
		||||
	switch (query->commandType)
 | 
			
		||||
	{
 | 
			
		||||
		case CMD_SELECT:
 | 
			
		||||
		case CMD_INSERT:
 | 
			
		||||
		case CMD_UPDATE:
 | 
			
		||||
		case CMD_DELETE:
 | 
			
		||||
			/* OK */
 | 
			
		||||
			break;
 | 
			
		||||
		default:
 | 
			
		||||
			ereport(ERROR,
 | 
			
		||||
					(errcode(ERRCODE_INVALID_PSTATEMENT_DEFINITION),
 | 
			
		||||
					 errmsg("utility statements cannot be prepared")));
 | 
			
		||||
			break;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/* Rewrite the query. The result could be 0, 1, or many queries. */
 | 
			
		||||
	query_list = QueryRewrite(query);
 | 
			
		||||
	query_list = pg_analyze_and_rewrite_varparams(rawstmt, pstate->p_sourcetext,
 | 
			
		||||
												  &argtypes, &nargs, NULL);
 | 
			
		||||
 | 
			
		||||
	/* Finish filling in the CachedPlanSource */
 | 
			
		||||
	CompleteCachedPlan(plansource,
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user