LCOV - code coverage report
Current view: top level - src/backend/parser - parse_param.c (source / functions) Coverage Total Hit
Test: Code coverage Lines: 87.7 % 122 107
Test Date: 2026-01-26 10:56:24 Functions: 100.0 % 9 9
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 46.1 % 76 35

             Branch data     Line data    Source code
       1                 :             : /*-------------------------------------------------------------------------
       2                 :             :  *
       3                 :             :  * parse_param.c
       4                 :             :  *        handle parameters in parser
       5                 :             :  *
       6                 :             :  * This code covers two cases that are used within the core backend:
       7                 :             :  *              * a fixed list of parameters with known types
       8                 :             :  *              * an expandable list of parameters whose types can optionally
       9                 :             :  *                be determined from context
      10                 :             :  * In both cases, only explicit $n references (ParamRef nodes) are supported.
      11                 :             :  *
      12                 :             :  * Note that other approaches to parameters are possible using the parser
      13                 :             :  * hooks defined in ParseState.
      14                 :             :  *
      15                 :             :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
      16                 :             :  * Portions Copyright (c) 1994, Regents of the University of California
      17                 :             :  *
      18                 :             :  *
      19                 :             :  * IDENTIFICATION
      20                 :             :  *        src/backend/parser/parse_param.c
      21                 :             :  *
      22                 :             :  *-------------------------------------------------------------------------
      23                 :             :  */
      24                 :             : 
      25                 :             : #include "postgres.h"
      26                 :             : 
      27                 :             : #include <limits.h>
      28                 :             : 
      29                 :             : #include "catalog/pg_type.h"
      30                 :             : #include "nodes/nodeFuncs.h"
      31                 :             : #include "parser/parse_param.h"
      32                 :             : #include "utils/builtins.h"
      33                 :             : #include "utils/lsyscache.h"
      34                 :             : #include "utils/memutils.h"
      35                 :             : 
      36                 :             : 
      37                 :             : typedef struct FixedParamState
      38                 :             : {
      39                 :             :         const Oid  *paramTypes;         /* array of parameter type OIDs */
      40                 :             :         int                     numParams;              /* number of array entries */
      41                 :             : } FixedParamState;
      42                 :             : 
      43                 :             : /*
      44                 :             :  * In the varparams case, the caller-supplied OID array (if any) can be
      45                 :             :  * re-palloc'd larger at need.  A zero array entry means that parameter number
      46                 :             :  * hasn't been seen, while UNKNOWNOID means the parameter has been used but
      47                 :             :  * its type is not yet known.
      48                 :             :  */
      49                 :             : typedef struct VarParamState
      50                 :             : {
      51                 :             :         Oid               **paramTypes;         /* array of parameter type OIDs */
      52                 :             :         int                *numParams;          /* number of array entries */
      53                 :             : } VarParamState;
      54                 :             : 
      55                 :             : static Node *fixed_paramref_hook(ParseState *pstate, ParamRef *pref);
      56                 :             : static Node *variable_paramref_hook(ParseState *pstate, ParamRef *pref);
      57                 :             : static Node *variable_coerce_param_hook(ParseState *pstate, Param *param,
      58                 :             :                                                                                 Oid targetTypeId, int32 targetTypeMod,
      59                 :             :                                                                                 int location);
      60                 :             : static bool check_parameter_resolution_walker(Node *node, ParseState *pstate);
      61                 :             : static bool query_contains_extern_params_walker(Node *node, void *context);
      62                 :             : 
      63                 :             : 
      64                 :             : /*
      65                 :             :  * Set up to process a query containing references to fixed parameters.
      66                 :             :  */
      67                 :             : void
      68                 :         436 : setup_parse_fixed_parameters(ParseState *pstate,
      69                 :             :                                                          const Oid *paramTypes, int numParams)
      70                 :             : {
      71                 :         436 :         FixedParamState *parstate = palloc_object(FixedParamState);
      72                 :             : 
      73                 :         436 :         parstate->paramTypes = paramTypes;
      74                 :         436 :         parstate->numParams = numParams;
      75                 :         436 :         pstate->p_ref_hook_state = parstate;
      76                 :         436 :         pstate->p_paramref_hook = fixed_paramref_hook;
      77                 :             :         /* no need to use p_coerce_param_hook */
      78                 :         436 : }
      79                 :             : 
      80                 :             : /*
      81                 :             :  * Set up to process a query containing references to variable parameters.
      82                 :             :  */
      83                 :             : void
      84                 :         204 : setup_parse_variable_parameters(ParseState *pstate,
      85                 :             :                                                                 Oid **paramTypes, int *numParams)
      86                 :             : {
      87                 :         204 :         VarParamState *parstate = palloc_object(VarParamState);
      88                 :             : 
      89                 :         204 :         parstate->paramTypes = paramTypes;
      90                 :         204 :         parstate->numParams = numParams;
      91                 :         204 :         pstate->p_ref_hook_state = parstate;
      92                 :         204 :         pstate->p_paramref_hook = variable_paramref_hook;
      93                 :         204 :         pstate->p_coerce_param_hook = variable_coerce_param_hook;
      94                 :         204 : }
      95                 :             : 
      96                 :             : /*
      97                 :             :  * Transform a ParamRef using fixed parameter types.
      98                 :             :  */
      99                 :             : static Node *
     100                 :         728 : fixed_paramref_hook(ParseState *pstate, ParamRef *pref)
     101                 :             : {
     102                 :         728 :         FixedParamState *parstate = (FixedParamState *) pstate->p_ref_hook_state;
     103                 :         728 :         int                     paramno = pref->number;
     104                 :         728 :         Param      *param;
     105                 :             : 
     106                 :             :         /* Check parameter number is valid */
     107         [ +  - ]:         728 :         if (paramno <= 0 || paramno > parstate->numParams ||
     108                 :         728 :                 !OidIsValid(parstate->paramTypes[paramno - 1]))
     109   [ #  #  #  # ]:           0 :                 ereport(ERROR,
     110                 :             :                                 (errcode(ERRCODE_UNDEFINED_PARAMETER),
     111                 :             :                                  errmsg("there is no parameter $%d", paramno),
     112                 :             :                                  parser_errposition(pstate, pref->location)));
     113                 :             : 
     114                 :         728 :         param = makeNode(Param);
     115                 :         728 :         param->paramkind = PARAM_EXTERN;
     116                 :         728 :         param->paramid = paramno;
     117                 :         728 :         param->paramtype = parstate->paramTypes[paramno - 1];
     118                 :         728 :         param->paramtypmod = -1;
     119                 :         728 :         param->paramcollid = get_typcollation(param->paramtype);
     120                 :         728 :         param->location = pref->location;
     121                 :             : 
     122                 :        1456 :         return (Node *) param;
     123                 :         728 : }
     124                 :             : 
     125                 :             : /*
     126                 :             :  * Transform a ParamRef using variable parameter types.
     127                 :             :  *
     128                 :             :  * The only difference here is we must enlarge the parameter type array
     129                 :             :  * as needed.
     130                 :             :  */
     131                 :             : static Node *
     132                 :         149 : variable_paramref_hook(ParseState *pstate, ParamRef *pref)
     133                 :             : {
     134                 :         149 :         VarParamState *parstate = (VarParamState *) pstate->p_ref_hook_state;
     135                 :         149 :         int                     paramno = pref->number;
     136                 :         149 :         Oid                *pptype;
     137                 :         149 :         Param      *param;
     138                 :             : 
     139                 :             :         /* Check parameter number is in range */
     140         [ +  - ]:         149 :         if (paramno <= 0 || paramno > MaxAllocSize / sizeof(Oid))
     141   [ #  #  #  # ]:           0 :                 ereport(ERROR,
     142                 :             :                                 (errcode(ERRCODE_UNDEFINED_PARAMETER),
     143                 :             :                                  errmsg("there is no parameter $%d", paramno),
     144                 :             :                                  parser_errposition(pstate, pref->location)));
     145         [ +  + ]:         149 :         if (paramno > *parstate->numParams)
     146                 :             :         {
     147                 :             :                 /* Need to enlarge param array */
     148         [ +  + ]:          92 :                 if (*parstate->paramTypes)
     149                 :          16 :                         *parstate->paramTypes = repalloc0_array(*parstate->paramTypes, Oid,
     150                 :             :                                                                                                         *parstate->numParams, paramno);
     151                 :             :                 else
     152                 :          76 :                         *parstate->paramTypes = palloc0_array(Oid, paramno);
     153                 :          92 :                 *parstate->numParams = paramno;
     154                 :          92 :         }
     155                 :             : 
     156                 :             :         /* Locate param's slot in array */
     157                 :         149 :         pptype = &(*parstate->paramTypes)[paramno - 1];
     158                 :             : 
     159                 :             :         /* If not seen before, initialize to UNKNOWN type */
     160         [ +  + ]:         149 :         if (*pptype == InvalidOid)
     161                 :          92 :                 *pptype = UNKNOWNOID;
     162                 :             : 
     163                 :             :         /*
     164                 :             :          * If the argument is of type void and it's procedure call, interpret it
     165                 :             :          * as unknown.  This allows the JDBC driver to not have to distinguish
     166                 :             :          * function and procedure calls.  See also another component of this hack
     167                 :             :          * in ParseFuncOrColumn().
     168                 :             :          */
     169   [ -  +  #  # ]:         149 :         if (*pptype == VOIDOID && pstate->p_expr_kind == EXPR_KIND_CALL_ARGUMENT)
     170                 :           0 :                 *pptype = UNKNOWNOID;
     171                 :             : 
     172                 :         149 :         param = makeNode(Param);
     173                 :         149 :         param->paramkind = PARAM_EXTERN;
     174                 :         149 :         param->paramid = paramno;
     175                 :         149 :         param->paramtype = *pptype;
     176                 :         149 :         param->paramtypmod = -1;
     177                 :         149 :         param->paramcollid = get_typcollation(param->paramtype);
     178                 :         149 :         param->location = pref->location;
     179                 :             : 
     180                 :         298 :         return (Node *) param;
     181                 :         149 : }
     182                 :             : 
     183                 :             : /*
     184                 :             :  * Coerce a Param to a query-requested datatype, in the varparams case.
     185                 :             :  */
     186                 :             : static Node *
     187                 :         101 : variable_coerce_param_hook(ParseState *pstate, Param *param,
     188                 :             :                                                    Oid targetTypeId, int32 targetTypeMod,
     189                 :             :                                                    int location)
     190                 :             : {
     191   [ +  -  +  + ]:         101 :         if (param->paramkind == PARAM_EXTERN && param->paramtype == UNKNOWNOID)
     192                 :             :         {
     193                 :             :                 /*
     194                 :             :                  * Input is a Param of previously undetermined type, and we want to
     195                 :             :                  * update our knowledge of the Param's type.
     196                 :             :                  */
     197                 :          93 :                 VarParamState *parstate = (VarParamState *) pstate->p_ref_hook_state;
     198                 :          93 :                 Oid                *paramTypes = *parstate->paramTypes;
     199                 :          93 :                 int                     paramno = param->paramid;
     200                 :             : 
     201         [ +  - ]:          93 :                 if (paramno <= 0 ||          /* shouldn't happen, but... */
     202                 :          93 :                         paramno > *parstate->numParams)
     203   [ #  #  #  # ]:           0 :                         ereport(ERROR,
     204                 :             :                                         (errcode(ERRCODE_UNDEFINED_PARAMETER),
     205                 :             :                                          errmsg("there is no parameter $%d", paramno),
     206                 :             :                                          parser_errposition(pstate, param->location)));
     207                 :             : 
     208         [ +  - ]:          93 :                 if (paramTypes[paramno - 1] == UNKNOWNOID)
     209                 :             :                 {
     210                 :             :                         /* We've successfully resolved the type */
     211                 :          93 :                         paramTypes[paramno - 1] = targetTypeId;
     212                 :          93 :                 }
     213         [ #  # ]:           0 :                 else if (paramTypes[paramno - 1] == targetTypeId)
     214                 :             :                 {
     215                 :             :                         /* We previously resolved the type, and it matches */
     216                 :           0 :                 }
     217                 :             :                 else
     218                 :             :                 {
     219                 :             :                         /* Oops */
     220   [ #  #  #  # ]:           0 :                         ereport(ERROR,
     221                 :             :                                         (errcode(ERRCODE_AMBIGUOUS_PARAMETER),
     222                 :             :                                          errmsg("inconsistent types deduced for parameter $%d",
     223                 :             :                                                         paramno),
     224                 :             :                                          errdetail("%s versus %s",
     225                 :             :                                                            format_type_be(paramTypes[paramno - 1]),
     226                 :             :                                                            format_type_be(targetTypeId)),
     227                 :             :                                          parser_errposition(pstate, param->location)));
     228                 :             :                 }
     229                 :             : 
     230                 :          93 :                 param->paramtype = targetTypeId;
     231                 :             : 
     232                 :             :                 /*
     233                 :             :                  * Note: it is tempting here to set the Param's paramtypmod to
     234                 :             :                  * targetTypeMod, but that is probably unwise because we have no
     235                 :             :                  * infrastructure that enforces that the value delivered for a Param
     236                 :             :                  * will match any particular typmod.  Leaving it -1 ensures that a
     237                 :             :                  * run-time length check/coercion will occur if needed.
     238                 :             :                  */
     239                 :          93 :                 param->paramtypmod = -1;
     240                 :             : 
     241                 :             :                 /*
     242                 :             :                  * This module always sets a Param's collation to be the default for
     243                 :             :                  * its datatype.  If that's not what you want, you should be using the
     244                 :             :                  * more general parser substitution hooks.
     245                 :             :                  */
     246                 :          93 :                 param->paramcollid = get_typcollation(param->paramtype);
     247                 :             : 
     248                 :             :                 /* Use the leftmost of the param's and coercion's locations */
     249   [ +  +  -  + ]:          97 :                 if (location >= 0 &&
     250         [ +  - ]:           4 :                         (param->location < 0 || location < param->location))
     251                 :           0 :                         param->location = location;
     252                 :             : 
     253                 :          93 :                 return (Node *) param;
     254                 :          93 :         }
     255                 :             : 
     256                 :             :         /* Else signal to proceed with normal coercion */
     257                 :           8 :         return NULL;
     258                 :         101 : }
     259                 :             : 
     260                 :             : /*
     261                 :             :  * Check for consistent assignment of variable parameters after completion
     262                 :             :  * of parsing with parse_variable_parameters.
     263                 :             :  *
     264                 :             :  * Note: this code intentionally does not check that all parameter positions
     265                 :             :  * were used, nor that all got non-UNKNOWN types assigned.  Caller of parser
     266                 :             :  * should enforce that if it's important.
     267                 :             :  */
     268                 :             : void
     269                 :         203 : check_variable_parameters(ParseState *pstate, Query *query)
     270                 :             : {
     271                 :         203 :         VarParamState *parstate = (VarParamState *) pstate->p_ref_hook_state;
     272                 :             : 
     273                 :             :         /* If numParams is zero then no Params were generated, so no work */
     274         [ +  + ]:         203 :         if (*parstate->numParams > 0)
     275                 :         108 :                 (void) query_tree_walker(query,
     276                 :             :                                                                  check_parameter_resolution_walker,
     277                 :             :                                                                  pstate, 0);
     278                 :         203 : }
     279                 :             : 
     280                 :             : /*
     281                 :             :  * Traverse a fully-analyzed tree to verify that parameter symbols
     282                 :             :  * match their types.  We need this because some Params might still
     283                 :             :  * be UNKNOWN, if there wasn't anything to force their coercion,
     284                 :             :  * and yet other instances seen later might have gotten coerced.
     285                 :             :  */
     286                 :             : static bool
     287                 :        2628 : check_parameter_resolution_walker(Node *node, ParseState *pstate)
     288                 :             : {
     289         [ +  + ]:        2628 :         if (node == NULL)
     290                 :        1459 :                 return false;
     291         [ +  + ]:        1169 :         if (IsA(node, Param))
     292                 :             :         {
     293                 :         150 :                 Param      *param = (Param *) node;
     294                 :             : 
     295         [ +  + ]:         150 :                 if (param->paramkind == PARAM_EXTERN)
     296                 :             :                 {
     297                 :         149 :                         VarParamState *parstate = (VarParamState *) pstate->p_ref_hook_state;
     298                 :         149 :                         int                     paramno = param->paramid;
     299                 :             : 
     300         [ +  - ]:         149 :                         if (paramno <= 0 || /* shouldn't happen, but... */
     301                 :         149 :                                 paramno > *parstate->numParams)
     302   [ #  #  #  # ]:           0 :                                 ereport(ERROR,
     303                 :             :                                                 (errcode(ERRCODE_UNDEFINED_PARAMETER),
     304                 :             :                                                  errmsg("there is no parameter $%d", paramno),
     305                 :             :                                                  parser_errposition(pstate, param->location)));
     306                 :             : 
     307         [ +  - ]:         149 :                         if (param->paramtype != (*parstate->paramTypes)[paramno - 1])
     308   [ #  #  #  # ]:           0 :                                 ereport(ERROR,
     309                 :             :                                                 (errcode(ERRCODE_AMBIGUOUS_PARAMETER),
     310                 :             :                                                  errmsg("could not determine data type of parameter $%d",
     311                 :             :                                                                 paramno),
     312                 :             :                                                  parser_errposition(pstate, param->location)));
     313                 :         149 :                 }
     314                 :         150 :                 return false;
     315                 :         150 :         }
     316         [ +  + ]:        1019 :         if (IsA(node, Query))
     317                 :             :         {
     318                 :             :                 /* Recurse into RTE subquery or not-yet-planned sublink subquery */
     319                 :          17 :                 return query_tree_walker((Query *) node,
     320                 :             :                                                                  check_parameter_resolution_walker,
     321                 :             :                                                                  pstate, 0);
     322                 :             :         }
     323                 :        1002 :         return expression_tree_walker(node, check_parameter_resolution_walker,
     324                 :             :                                                                   pstate);
     325                 :        2628 : }
     326                 :             : 
     327                 :             : /*
     328                 :             :  * Check to see if a fully-parsed query tree contains any PARAM_EXTERN Params.
     329                 :             :  */
     330                 :             : bool
     331                 :          66 : query_contains_extern_params(Query *query)
     332                 :             : {
     333                 :          66 :         return query_tree_walker(query,
     334                 :             :                                                          query_contains_extern_params_walker,
     335                 :             :                                                          NULL, 0);
     336                 :             : }
     337                 :             : 
     338                 :             : static bool
     339                 :        1324 : query_contains_extern_params_walker(Node *node, void *context)
     340                 :             : {
     341         [ +  + ]:        1324 :         if (node == NULL)
     342                 :         849 :                 return false;
     343         [ -  + ]:         475 :         if (IsA(node, Param))
     344                 :             :         {
     345                 :           0 :                 Param      *param = (Param *) node;
     346                 :             : 
     347         [ #  # ]:           0 :                 if (param->paramkind == PARAM_EXTERN)
     348                 :           0 :                         return true;
     349                 :           0 :                 return false;
     350                 :           0 :         }
     351         [ +  + ]:         475 :         if (IsA(node, Query))
     352                 :             :         {
     353                 :             :                 /* Recurse into RTE subquery or not-yet-planned sublink subquery */
     354                 :           3 :                 return query_tree_walker((Query *) node,
     355                 :             :                                                                  query_contains_extern_params_walker,
     356                 :             :                                                                  context, 0);
     357                 :             :         }
     358                 :         472 :         return expression_tree_walker(node, query_contains_extern_params_walker,
     359                 :             :                                                                   context);
     360                 :        1324 : }
        

Generated by: LCOV version 2.3.2-1