Coverage Report

Created: 2025-06-13 06:06

/src/postgres/src/backend/commands/functioncmds.c
Line
Count
Source (jump to first uncovered line)
1
/*-------------------------------------------------------------------------
2
 *
3
 * functioncmds.c
4
 *
5
 *    Routines for CREATE and DROP FUNCTION commands and CREATE and DROP
6
 *    CAST commands.
7
 *
8
 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
9
 * Portions Copyright (c) 1994, Regents of the University of California
10
 *
11
 *
12
 * IDENTIFICATION
13
 *    src/backend/commands/functioncmds.c
14
 *
15
 * DESCRIPTION
16
 *    These routines take the parse tree and pick out the
17
 *    appropriate arguments/flags, and pass the results to the
18
 *    corresponding "FooCreate" routines (in src/backend/catalog) that do
19
 *    the actual catalog-munging.  These routines also verify permission
20
 *    of the user to execute the command.
21
 *
22
 * NOTES
23
 *    These things must be defined and committed in the following order:
24
 *    "create function":
25
 *        input/output, recv/send procedures
26
 *    "create type":
27
 *        type
28
 *    "create operator":
29
 *        operators
30
 *
31
 *-------------------------------------------------------------------------
32
 */
33
#include "postgres.h"
34
35
#include "access/htup_details.h"
36
#include "access/table.h"
37
#include "catalog/catalog.h"
38
#include "catalog/dependency.h"
39
#include "catalog/indexing.h"
40
#include "catalog/objectaccess.h"
41
#include "catalog/pg_aggregate.h"
42
#include "catalog/pg_cast.h"
43
#include "catalog/pg_language.h"
44
#include "catalog/pg_namespace.h"
45
#include "catalog/pg_proc.h"
46
#include "catalog/pg_transform.h"
47
#include "catalog/pg_type.h"
48
#include "commands/defrem.h"
49
#include "commands/extension.h"
50
#include "commands/proclang.h"
51
#include "executor/executor.h"
52
#include "executor/functions.h"
53
#include "funcapi.h"
54
#include "miscadmin.h"
55
#include "nodes/nodeFuncs.h"
56
#include "optimizer/optimizer.h"
57
#include "parser/analyze.h"
58
#include "parser/parse_coerce.h"
59
#include "parser/parse_collate.h"
60
#include "parser/parse_expr.h"
61
#include "parser/parse_func.h"
62
#include "parser/parse_type.h"
63
#include "pgstat.h"
64
#include "tcop/pquery.h"
65
#include "tcop/utility.h"
66
#include "utils/acl.h"
67
#include "utils/builtins.h"
68
#include "utils/guc.h"
69
#include "utils/lsyscache.h"
70
#include "utils/rel.h"
71
#include "utils/snapmgr.h"
72
#include "utils/syscache.h"
73
#include "utils/typcache.h"
74
75
/*
76
 *   Examine the RETURNS clause of the CREATE FUNCTION statement
77
 *   and return information about it as *prorettype_p and *returnsSet_p.
78
 *
79
 * This is more complex than the average typename lookup because we want to
80
 * allow a shell type to be used, or even created if the specified return type
81
 * doesn't exist yet.  (Without this, there's no way to define the I/O procs
82
 * for a new type.)  But SQL function creation won't cope, so error out if
83
 * the target language is SQL.  (We do this here, not in the SQL-function
84
 * validator, so as not to produce a NOTICE and then an ERROR for the same
85
 * condition.)
86
 */
87
static void
88
compute_return_type(TypeName *returnType, Oid languageOid,
89
          Oid *prorettype_p, bool *returnsSet_p)
90
0
{
91
0
  Oid     rettype;
92
0
  Type    typtup;
93
0
  AclResult aclresult;
94
95
0
  typtup = LookupTypeName(NULL, returnType, NULL, false);
96
97
0
  if (typtup)
98
0
  {
99
0
    if (!((Form_pg_type) GETSTRUCT(typtup))->typisdefined)
100
0
    {
101
0
      if (languageOid == SQLlanguageId)
102
0
        ereport(ERROR,
103
0
            (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
104
0
             errmsg("SQL function cannot return shell type %s",
105
0
                TypeNameToString(returnType))));
106
0
      else
107
0
        ereport(NOTICE,
108
0
            (errcode(ERRCODE_WRONG_OBJECT_TYPE),
109
0
             errmsg("return type %s is only a shell",
110
0
                TypeNameToString(returnType))));
111
0
    }
112
0
    rettype = typeTypeId(typtup);
113
0
    ReleaseSysCache(typtup);
114
0
  }
115
0
  else
116
0
  {
117
0
    char     *typnam = TypeNameToString(returnType);
118
0
    Oid     namespaceId;
119
0
    char     *typname;
120
0
    ObjectAddress address;
121
122
    /*
123
     * Only C-coded functions can be I/O functions.  We enforce this
124
     * restriction here mainly to prevent littering the catalogs with
125
     * shell types due to simple typos in user-defined function
126
     * definitions.
127
     */
128
0
    if (languageOid != INTERNALlanguageId &&
129
0
      languageOid != ClanguageId)
130
0
      ereport(ERROR,
131
0
          (errcode(ERRCODE_UNDEFINED_OBJECT),
132
0
           errmsg("type \"%s\" does not exist", typnam)));
133
134
    /* Reject if there's typmod decoration, too */
135
0
    if (returnType->typmods != NIL)
136
0
      ereport(ERROR,
137
0
          (errcode(ERRCODE_SYNTAX_ERROR),
138
0
           errmsg("type modifier cannot be specified for shell type \"%s\"",
139
0
              typnam)));
140
141
    /* Otherwise, go ahead and make a shell type */
142
0
    ereport(NOTICE,
143
0
        (errcode(ERRCODE_UNDEFINED_OBJECT),
144
0
         errmsg("type \"%s\" is not yet defined", typnam),
145
0
         errdetail("Creating a shell type definition.")));
146
0
    namespaceId = QualifiedNameGetCreationNamespace(returnType->names,
147
0
                            &typname);
148
0
    aclresult = object_aclcheck(NamespaceRelationId, namespaceId, GetUserId(),
149
0
                  ACL_CREATE);
150
0
    if (aclresult != ACLCHECK_OK)
151
0
      aclcheck_error(aclresult, OBJECT_SCHEMA,
152
0
               get_namespace_name(namespaceId));
153
0
    address = TypeShellMake(typname, namespaceId, GetUserId());
154
0
    rettype = address.objectId;
155
0
    Assert(OidIsValid(rettype));
156
0
  }
157
158
0
  aclresult = object_aclcheck(TypeRelationId, rettype, GetUserId(), ACL_USAGE);
159
0
  if (aclresult != ACLCHECK_OK)
160
0
    aclcheck_error_type(aclresult, rettype);
161
162
0
  *prorettype_p = rettype;
163
0
  *returnsSet_p = returnType->setof;
164
0
}
165
166
/*
167
 * Interpret the function parameter list of a CREATE FUNCTION,
168
 * CREATE PROCEDURE, or CREATE AGGREGATE statement.
169
 *
170
 * Input parameters:
171
 * parameters: list of FunctionParameter structs
172
 * languageOid: OID of function language (InvalidOid if it's CREATE AGGREGATE)
173
 * objtype: identifies type of object being created
174
 *
175
 * Results are stored into output parameters.  parameterTypes must always
176
 * be created, but the other arrays/lists can be NULL pointers if not needed.
177
 * variadicArgType is set to the variadic array type if there's a VARIADIC
178
 * parameter (there can be only one); or to InvalidOid if not.
179
 * requiredResultType is set to InvalidOid if there are no OUT parameters,
180
 * else it is set to the OID of the implied result type.
181
 */
182
void
183
interpret_function_parameter_list(ParseState *pstate,
184
                  List *parameters,
185
                  Oid languageOid,
186
                  ObjectType objtype,
187
                  oidvector **parameterTypes,
188
                  List **parameterTypes_list,
189
                  ArrayType **allParameterTypes,
190
                  ArrayType **parameterModes,
191
                  ArrayType **parameterNames,
192
                  List **inParameterNames_list,
193
                  List **parameterDefaults,
194
                  Oid *variadicArgType,
195
                  Oid *requiredResultType)
196
0
{
197
0
  int     parameterCount = list_length(parameters);
198
0
  Oid      *inTypes;
199
0
  int     inCount = 0;
200
0
  Datum    *allTypes;
201
0
  Datum    *paramModes;
202
0
  Datum    *paramNames;
203
0
  int     outCount = 0;
204
0
  int     varCount = 0;
205
0
  bool    have_names = false;
206
0
  bool    have_defaults = false;
207
0
  ListCell   *x;
208
0
  int     i;
209
210
0
  *variadicArgType = InvalidOid; /* default result */
211
0
  *requiredResultType = InvalidOid; /* default result */
212
213
0
  inTypes = (Oid *) palloc(parameterCount * sizeof(Oid));
214
0
  allTypes = (Datum *) palloc(parameterCount * sizeof(Datum));
215
0
  paramModes = (Datum *) palloc(parameterCount * sizeof(Datum));
216
0
  paramNames = (Datum *) palloc0(parameterCount * sizeof(Datum));
217
0
  *parameterDefaults = NIL;
218
219
  /* Scan the list and extract data into work arrays */
220
0
  i = 0;
221
0
  foreach(x, parameters)
222
0
  {
223
0
    FunctionParameter *fp = (FunctionParameter *) lfirst(x);
224
0
    TypeName   *t = fp->argType;
225
0
    FunctionParameterMode fpmode = fp->mode;
226
0
    bool    isinput = false;
227
0
    Oid     toid;
228
0
    Type    typtup;
229
0
    AclResult aclresult;
230
231
    /* For our purposes here, a defaulted mode spec is identical to IN */
232
0
    if (fpmode == FUNC_PARAM_DEFAULT)
233
0
      fpmode = FUNC_PARAM_IN;
234
235
0
    typtup = LookupTypeName(pstate, t, NULL, false);
236
0
    if (typtup)
237
0
    {
238
0
      if (!((Form_pg_type) GETSTRUCT(typtup))->typisdefined)
239
0
      {
240
        /* As above, hard error if language is SQL */
241
0
        if (languageOid == SQLlanguageId)
242
0
          ereport(ERROR,
243
0
              (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
244
0
               errmsg("SQL function cannot accept shell type %s",
245
0
                  TypeNameToString(t)),
246
0
               parser_errposition(pstate, t->location)));
247
        /* We don't allow creating aggregates on shell types either */
248
0
        else if (objtype == OBJECT_AGGREGATE)
249
0
          ereport(ERROR,
250
0
              (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
251
0
               errmsg("aggregate cannot accept shell type %s",
252
0
                  TypeNameToString(t)),
253
0
               parser_errposition(pstate, t->location)));
254
0
        else
255
0
          ereport(NOTICE,
256
0
              (errcode(ERRCODE_WRONG_OBJECT_TYPE),
257
0
               errmsg("argument type %s is only a shell",
258
0
                  TypeNameToString(t)),
259
0
               parser_errposition(pstate, t->location)));
260
0
      }
261
0
      toid = typeTypeId(typtup);
262
0
      ReleaseSysCache(typtup);
263
0
    }
264
0
    else
265
0
    {
266
0
      ereport(ERROR,
267
0
          (errcode(ERRCODE_UNDEFINED_OBJECT),
268
0
           errmsg("type %s does not exist",
269
0
              TypeNameToString(t)),
270
0
           parser_errposition(pstate, t->location)));
271
0
      toid = InvalidOid; /* keep compiler quiet */
272
0
    }
273
274
0
    aclresult = object_aclcheck(TypeRelationId, toid, GetUserId(), ACL_USAGE);
275
0
    if (aclresult != ACLCHECK_OK)
276
0
      aclcheck_error_type(aclresult, toid);
277
278
0
    if (t->setof)
279
0
    {
280
0
      if (objtype == OBJECT_AGGREGATE)
281
0
        ereport(ERROR,
282
0
            (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
283
0
             errmsg("aggregates cannot accept set arguments"),
284
0
             parser_errposition(pstate, fp->location)));
285
0
      else if (objtype == OBJECT_PROCEDURE)
286
0
        ereport(ERROR,
287
0
            (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
288
0
             errmsg("procedures cannot accept set arguments"),
289
0
             parser_errposition(pstate, fp->location)));
290
0
      else
291
0
        ereport(ERROR,
292
0
            (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
293
0
             errmsg("functions cannot accept set arguments"),
294
0
             parser_errposition(pstate, fp->location)));
295
0
    }
296
297
    /* handle input parameters */
298
0
    if (fpmode != FUNC_PARAM_OUT && fpmode != FUNC_PARAM_TABLE)
299
0
    {
300
      /* other input parameters can't follow a VARIADIC parameter */
301
0
      if (varCount > 0)
302
0
        ereport(ERROR,
303
0
            (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
304
0
             errmsg("VARIADIC parameter must be the last input parameter"),
305
0
             parser_errposition(pstate, fp->location)));
306
0
      inTypes[inCount++] = toid;
307
0
      isinput = true;
308
0
      if (parameterTypes_list)
309
0
        *parameterTypes_list = lappend_oid(*parameterTypes_list, toid);
310
0
    }
311
312
    /* handle output parameters */
313
0
    if (fpmode != FUNC_PARAM_IN && fpmode != FUNC_PARAM_VARIADIC)
314
0
    {
315
0
      if (objtype == OBJECT_PROCEDURE)
316
0
      {
317
        /*
318
         * We disallow OUT-after-VARIADIC only for procedures.  While
319
         * such a case causes no confusion in ordinary function calls,
320
         * it would cause confusion in a CALL statement.
321
         */
322
0
        if (varCount > 0)
323
0
          ereport(ERROR,
324
0
              (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
325
0
               errmsg("VARIADIC parameter must be the last parameter"),
326
0
               parser_errposition(pstate, fp->location)));
327
        /* Procedures with output parameters always return RECORD */
328
0
        *requiredResultType = RECORDOID;
329
0
      }
330
0
      else if (outCount == 0) /* save first output param's type */
331
0
        *requiredResultType = toid;
332
0
      outCount++;
333
0
    }
334
335
0
    if (fpmode == FUNC_PARAM_VARIADIC)
336
0
    {
337
0
      *variadicArgType = toid;
338
0
      varCount++;
339
      /* validate variadic parameter type */
340
0
      switch (toid)
341
0
      {
342
0
        case ANYARRAYOID:
343
0
        case ANYCOMPATIBLEARRAYOID:
344
0
        case ANYOID:
345
          /* okay */
346
0
          break;
347
0
        default:
348
0
          if (!OidIsValid(get_element_type(toid)))
349
0
            ereport(ERROR,
350
0
                (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
351
0
                 errmsg("VARIADIC parameter must be an array"),
352
0
                 parser_errposition(pstate, fp->location)));
353
0
          break;
354
0
      }
355
0
    }
356
357
0
    allTypes[i] = ObjectIdGetDatum(toid);
358
359
0
    paramModes[i] = CharGetDatum(fpmode);
360
361
0
    if (fp->name && fp->name[0])
362
0
    {
363
0
      ListCell   *px;
364
365
      /*
366
       * As of Postgres 9.0 we disallow using the same name for two
367
       * input or two output function parameters.  Depending on the
368
       * function's language, conflicting input and output names might
369
       * be bad too, but we leave it to the PL to complain if so.
370
       */
371
0
      foreach(px, parameters)
372
0
      {
373
0
        FunctionParameter *prevfp = (FunctionParameter *) lfirst(px);
374
0
        FunctionParameterMode prevfpmode;
375
376
0
        if (prevfp == fp)
377
0
          break;
378
        /* as above, default mode is IN */
379
0
        prevfpmode = prevfp->mode;
380
0
        if (prevfpmode == FUNC_PARAM_DEFAULT)
381
0
          prevfpmode = FUNC_PARAM_IN;
382
        /* pure in doesn't conflict with pure out */
383
0
        if ((fpmode == FUNC_PARAM_IN ||
384
0
           fpmode == FUNC_PARAM_VARIADIC) &&
385
0
          (prevfpmode == FUNC_PARAM_OUT ||
386
0
           prevfpmode == FUNC_PARAM_TABLE))
387
0
          continue;
388
0
        if ((prevfpmode == FUNC_PARAM_IN ||
389
0
           prevfpmode == FUNC_PARAM_VARIADIC) &&
390
0
          (fpmode == FUNC_PARAM_OUT ||
391
0
           fpmode == FUNC_PARAM_TABLE))
392
0
          continue;
393
0
        if (prevfp->name && prevfp->name[0] &&
394
0
          strcmp(prevfp->name, fp->name) == 0)
395
0
          ereport(ERROR,
396
0
              (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
397
0
               errmsg("parameter name \"%s\" used more than once",
398
0
                  fp->name),
399
0
               parser_errposition(pstate, fp->location)));
400
0
      }
401
402
0
      paramNames[i] = CStringGetTextDatum(fp->name);
403
0
      have_names = true;
404
0
    }
405
406
0
    if (inParameterNames_list)
407
0
      *inParameterNames_list = lappend(*inParameterNames_list, makeString(fp->name ? fp->name : pstrdup("")));
408
409
0
    if (fp->defexpr)
410
0
    {
411
0
      Node     *def;
412
413
0
      if (!isinput)
414
0
        ereport(ERROR,
415
0
            (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
416
0
             errmsg("only input parameters can have default values"),
417
0
             parser_errposition(pstate, fp->location)));
418
419
0
      def = transformExpr(pstate, fp->defexpr,
420
0
                EXPR_KIND_FUNCTION_DEFAULT);
421
0
      def = coerce_to_specific_type(pstate, def, toid, "DEFAULT");
422
0
      assign_expr_collations(pstate, def);
423
424
      /*
425
       * Make sure no variables are referred to (this is probably dead
426
       * code now that add_missing_from is history).
427
       */
428
0
      if (pstate->p_rtable != NIL ||
429
0
        contain_var_clause(def))
430
0
        ereport(ERROR,
431
0
            (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
432
0
             errmsg("cannot use table references in parameter default value"),
433
0
             parser_errposition(pstate, fp->location)));
434
435
      /*
436
       * transformExpr() should have already rejected subqueries,
437
       * aggregates, and window functions, based on the EXPR_KIND_ for a
438
       * default expression.
439
       *
440
       * It can't return a set either --- but coerce_to_specific_type
441
       * already checked that for us.
442
       *
443
       * Note: the point of these restrictions is to ensure that an
444
       * expression that, on its face, hasn't got subplans, aggregates,
445
       * etc cannot suddenly have them after function default arguments
446
       * are inserted.
447
       */
448
449
0
      *parameterDefaults = lappend(*parameterDefaults, def);
450
0
      have_defaults = true;
451
0
    }
452
0
    else
453
0
    {
454
0
      if (isinput && have_defaults)
455
0
        ereport(ERROR,
456
0
            (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
457
0
             errmsg("input parameters after one with a default value must also have defaults"),
458
0
             parser_errposition(pstate, fp->location)));
459
460
      /*
461
       * For procedures, we also can't allow OUT parameters after one
462
       * with a default, because the same sort of confusion arises in a
463
       * CALL statement.
464
       */
465
0
      if (objtype == OBJECT_PROCEDURE && have_defaults)
466
0
        ereport(ERROR,
467
0
            (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
468
0
             errmsg("procedure OUT parameters cannot appear after one with a default value"),
469
0
             parser_errposition(pstate, fp->location)));
470
0
    }
471
472
0
    i++;
473
0
  }
474
475
  /* Now construct the proper outputs as needed */
476
0
  *parameterTypes = buildoidvector(inTypes, inCount);
477
478
0
  if (outCount > 0 || varCount > 0)
479
0
  {
480
0
    *allParameterTypes = construct_array_builtin(allTypes, parameterCount, OIDOID);
481
0
    *parameterModes = construct_array_builtin(paramModes, parameterCount, CHAROID);
482
0
    if (outCount > 1)
483
0
      *requiredResultType = RECORDOID;
484
    /* otherwise we set requiredResultType correctly above */
485
0
  }
486
0
  else
487
0
  {
488
0
    *allParameterTypes = NULL;
489
0
    *parameterModes = NULL;
490
0
  }
491
492
0
  if (have_names)
493
0
  {
494
0
    for (i = 0; i < parameterCount; i++)
495
0
    {
496
0
      if (paramNames[i] == PointerGetDatum(NULL))
497
0
        paramNames[i] = CStringGetTextDatum("");
498
0
    }
499
0
    *parameterNames = construct_array_builtin(paramNames, parameterCount, TEXTOID);
500
0
  }
501
0
  else
502
0
    *parameterNames = NULL;
503
0
}
504
505
506
/*
507
 * Recognize one of the options that can be passed to both CREATE
508
 * FUNCTION and ALTER FUNCTION and return it via one of the out
509
 * parameters. Returns true if the passed option was recognized. If
510
 * the out parameter we were going to assign to points to non-NULL,
511
 * raise a duplicate-clause error.  (We don't try to detect duplicate
512
 * SET parameters though --- if you're redundant, the last one wins.)
513
 */
514
static bool
515
compute_common_attribute(ParseState *pstate,
516
             bool is_procedure,
517
             DefElem *defel,
518
             DefElem **volatility_item,
519
             DefElem **strict_item,
520
             DefElem **security_item,
521
             DefElem **leakproof_item,
522
             List **set_items,
523
             DefElem **cost_item,
524
             DefElem **rows_item,
525
             DefElem **support_item,
526
             DefElem **parallel_item)
527
0
{
528
0
  if (strcmp(defel->defname, "volatility") == 0)
529
0
  {
530
0
    if (is_procedure)
531
0
      goto procedure_error;
532
0
    if (*volatility_item)
533
0
      errorConflictingDefElem(defel, pstate);
534
535
0
    *volatility_item = defel;
536
0
  }
537
0
  else if (strcmp(defel->defname, "strict") == 0)
538
0
  {
539
0
    if (is_procedure)
540
0
      goto procedure_error;
541
0
    if (*strict_item)
542
0
      errorConflictingDefElem(defel, pstate);
543
544
0
    *strict_item = defel;
545
0
  }
546
0
  else if (strcmp(defel->defname, "security") == 0)
547
0
  {
548
0
    if (*security_item)
549
0
      errorConflictingDefElem(defel, pstate);
550
551
0
    *security_item = defel;
552
0
  }
553
0
  else if (strcmp(defel->defname, "leakproof") == 0)
554
0
  {
555
0
    if (is_procedure)
556
0
      goto procedure_error;
557
0
    if (*leakproof_item)
558
0
      errorConflictingDefElem(defel, pstate);
559
560
0
    *leakproof_item = defel;
561
0
  }
562
0
  else if (strcmp(defel->defname, "set") == 0)
563
0
  {
564
0
    *set_items = lappend(*set_items, defel->arg);
565
0
  }
566
0
  else if (strcmp(defel->defname, "cost") == 0)
567
0
  {
568
0
    if (is_procedure)
569
0
      goto procedure_error;
570
0
    if (*cost_item)
571
0
      errorConflictingDefElem(defel, pstate);
572
573
0
    *cost_item = defel;
574
0
  }
575
0
  else if (strcmp(defel->defname, "rows") == 0)
576
0
  {
577
0
    if (is_procedure)
578
0
      goto procedure_error;
579
0
    if (*rows_item)
580
0
      errorConflictingDefElem(defel, pstate);
581
582
0
    *rows_item = defel;
583
0
  }
584
0
  else if (strcmp(defel->defname, "support") == 0)
585
0
  {
586
0
    if (is_procedure)
587
0
      goto procedure_error;
588
0
    if (*support_item)
589
0
      errorConflictingDefElem(defel, pstate);
590
591
0
    *support_item = defel;
592
0
  }
593
0
  else if (strcmp(defel->defname, "parallel") == 0)
594
0
  {
595
0
    if (is_procedure)
596
0
      goto procedure_error;
597
0
    if (*parallel_item)
598
0
      errorConflictingDefElem(defel, pstate);
599
600
0
    *parallel_item = defel;
601
0
  }
602
0
  else
603
0
    return false;
604
605
  /* Recognized an option */
606
0
  return true;
607
608
0
procedure_error:
609
0
  ereport(ERROR,
610
0
      (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
611
0
       errmsg("invalid attribute in procedure definition"),
612
0
       parser_errposition(pstate, defel->location)));
613
0
  return false;
614
0
}
615
616
static char
617
interpret_func_volatility(DefElem *defel)
618
0
{
619
0
  char     *str = strVal(defel->arg);
620
621
0
  if (strcmp(str, "immutable") == 0)
622
0
    return PROVOLATILE_IMMUTABLE;
623
0
  else if (strcmp(str, "stable") == 0)
624
0
    return PROVOLATILE_STABLE;
625
0
  else if (strcmp(str, "volatile") == 0)
626
0
    return PROVOLATILE_VOLATILE;
627
0
  else
628
0
  {
629
0
    elog(ERROR, "invalid volatility \"%s\"", str);
630
0
    return 0;       /* keep compiler quiet */
631
0
  }
632
0
}
633
634
static char
635
interpret_func_parallel(DefElem *defel)
636
0
{
637
0
  char     *str = strVal(defel->arg);
638
639
0
  if (strcmp(str, "safe") == 0)
640
0
    return PROPARALLEL_SAFE;
641
0
  else if (strcmp(str, "unsafe") == 0)
642
0
    return PROPARALLEL_UNSAFE;
643
0
  else if (strcmp(str, "restricted") == 0)
644
0
    return PROPARALLEL_RESTRICTED;
645
0
  else
646
0
  {
647
0
    ereport(ERROR,
648
0
        (errcode(ERRCODE_SYNTAX_ERROR),
649
0
         errmsg("parameter \"parallel\" must be SAFE, RESTRICTED, or UNSAFE")));
650
0
    return PROPARALLEL_UNSAFE; /* keep compiler quiet */
651
0
  }
652
0
}
653
654
/*
655
 * Update a proconfig value according to a list of VariableSetStmt items.
656
 *
657
 * The input and result may be NULL to signify a null entry.
658
 */
659
static ArrayType *
660
update_proconfig_value(ArrayType *a, List *set_items)
661
0
{
662
0
  ListCell   *l;
663
664
0
  foreach(l, set_items)
665
0
  {
666
0
    VariableSetStmt *sstmt = lfirst_node(VariableSetStmt, l);
667
668
0
    if (sstmt->kind == VAR_RESET_ALL)
669
0
      a = NULL;
670
0
    else
671
0
    {
672
0
      char     *valuestr = ExtractSetVariableArgs(sstmt);
673
674
0
      if (valuestr)
675
0
        a = GUCArrayAdd(a, sstmt->name, valuestr);
676
0
      else        /* RESET */
677
0
        a = GUCArrayDelete(a, sstmt->name);
678
0
    }
679
0
  }
680
681
0
  return a;
682
0
}
683
684
static Oid
685
interpret_func_support(DefElem *defel)
686
0
{
687
0
  List     *procName = defGetQualifiedName(defel);
688
0
  Oid     procOid;
689
0
  Oid     argList[1];
690
691
  /*
692
   * Support functions always take one INTERNAL argument and return
693
   * INTERNAL.
694
   */
695
0
  argList[0] = INTERNALOID;
696
697
0
  procOid = LookupFuncName(procName, 1, argList, true);
698
0
  if (!OidIsValid(procOid))
699
0
    ereport(ERROR,
700
0
        (errcode(ERRCODE_UNDEFINED_FUNCTION),
701
0
         errmsg("function %s does not exist",
702
0
            func_signature_string(procName, 1, NIL, argList))));
703
704
0
  if (get_func_rettype(procOid) != INTERNALOID)
705
0
    ereport(ERROR,
706
0
        (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
707
0
         errmsg("support function %s must return type %s",
708
0
            NameListToString(procName), "internal")));
709
710
  /*
711
   * Someday we might want an ACL check here; but for now, we insist that
712
   * you be superuser to specify a support function, so privilege on the
713
   * support function is moot.
714
   */
715
0
  if (!superuser())
716
0
    ereport(ERROR,
717
0
        (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
718
0
         errmsg("must be superuser to specify a support function")));
719
720
0
  return procOid;
721
0
}
722
723
724
/*
725
 * Dissect the list of options assembled in gram.y into function
726
 * attributes.
727
 */
728
static void
729
compute_function_attributes(ParseState *pstate,
730
              bool is_procedure,
731
              List *options,
732
              List **as,
733
              char **language,
734
              Node **transform,
735
              bool *windowfunc_p,
736
              char *volatility_p,
737
              bool *strict_p,
738
              bool *security_definer,
739
              bool *leakproof_p,
740
              ArrayType **proconfig,
741
              float4 *procost,
742
              float4 *prorows,
743
              Oid *prosupport,
744
              char *parallel_p)
745
0
{
746
0
  ListCell   *option;
747
0
  DefElem    *as_item = NULL;
748
0
  DefElem    *language_item = NULL;
749
0
  DefElem    *transform_item = NULL;
750
0
  DefElem    *windowfunc_item = NULL;
751
0
  DefElem    *volatility_item = NULL;
752
0
  DefElem    *strict_item = NULL;
753
0
  DefElem    *security_item = NULL;
754
0
  DefElem    *leakproof_item = NULL;
755
0
  List     *set_items = NIL;
756
0
  DefElem    *cost_item = NULL;
757
0
  DefElem    *rows_item = NULL;
758
0
  DefElem    *support_item = NULL;
759
0
  DefElem    *parallel_item = NULL;
760
761
0
  foreach(option, options)
762
0
  {
763
0
    DefElem    *defel = (DefElem *) lfirst(option);
764
765
0
    if (strcmp(defel->defname, "as") == 0)
766
0
    {
767
0
      if (as_item)
768
0
        errorConflictingDefElem(defel, pstate);
769
0
      as_item = defel;
770
0
    }
771
0
    else if (strcmp(defel->defname, "language") == 0)
772
0
    {
773
0
      if (language_item)
774
0
        errorConflictingDefElem(defel, pstate);
775
0
      language_item = defel;
776
0
    }
777
0
    else if (strcmp(defel->defname, "transform") == 0)
778
0
    {
779
0
      if (transform_item)
780
0
        errorConflictingDefElem(defel, pstate);
781
0
      transform_item = defel;
782
0
    }
783
0
    else if (strcmp(defel->defname, "window") == 0)
784
0
    {
785
0
      if (windowfunc_item)
786
0
        errorConflictingDefElem(defel, pstate);
787
0
      if (is_procedure)
788
0
        ereport(ERROR,
789
0
            (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
790
0
             errmsg("invalid attribute in procedure definition"),
791
0
             parser_errposition(pstate, defel->location)));
792
0
      windowfunc_item = defel;
793
0
    }
794
0
    else if (compute_common_attribute(pstate,
795
0
                      is_procedure,
796
0
                      defel,
797
0
                      &volatility_item,
798
0
                      &strict_item,
799
0
                      &security_item,
800
0
                      &leakproof_item,
801
0
                      &set_items,
802
0
                      &cost_item,
803
0
                      &rows_item,
804
0
                      &support_item,
805
0
                      &parallel_item))
806
0
    {
807
      /* recognized common option */
808
0
      continue;
809
0
    }
810
0
    else
811
0
      elog(ERROR, "option \"%s\" not recognized",
812
0
         defel->defname);
813
0
  }
814
815
0
  if (as_item)
816
0
    *as = (List *) as_item->arg;
817
0
  if (language_item)
818
0
    *language = strVal(language_item->arg);
819
0
  if (transform_item)
820
0
    *transform = transform_item->arg;
821
0
  if (windowfunc_item)
822
0
    *windowfunc_p = boolVal(windowfunc_item->arg);
823
0
  if (volatility_item)
824
0
    *volatility_p = interpret_func_volatility(volatility_item);
825
0
  if (strict_item)
826
0
    *strict_p = boolVal(strict_item->arg);
827
0
  if (security_item)
828
0
    *security_definer = boolVal(security_item->arg);
829
0
  if (leakproof_item)
830
0
    *leakproof_p = boolVal(leakproof_item->arg);
831
0
  if (set_items)
832
0
    *proconfig = update_proconfig_value(NULL, set_items);
833
0
  if (cost_item)
834
0
  {
835
0
    *procost = defGetNumeric(cost_item);
836
0
    if (*procost <= 0)
837
0
      ereport(ERROR,
838
0
          (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
839
0
           errmsg("COST must be positive")));
840
0
  }
841
0
  if (rows_item)
842
0
  {
843
0
    *prorows = defGetNumeric(rows_item);
844
0
    if (*prorows <= 0)
845
0
      ereport(ERROR,
846
0
          (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
847
0
           errmsg("ROWS must be positive")));
848
0
  }
849
0
  if (support_item)
850
0
    *prosupport = interpret_func_support(support_item);
851
0
  if (parallel_item)
852
0
    *parallel_p = interpret_func_parallel(parallel_item);
853
0
}
854
855
856
/*
857
 * For a dynamically linked C language object, the form of the clause is
858
 *
859
 *     AS <object file name> [, <link symbol name> ]
860
 *
861
 * In all other cases
862
 *
863
 *     AS <object reference, or sql code>
864
 */
865
static void
866
interpret_AS_clause(Oid languageOid, const char *languageName,
867
          char *funcname, List *as, Node *sql_body_in,
868
          List *parameterTypes, List *inParameterNames,
869
          char **prosrc_str_p, char **probin_str_p,
870
          Node **sql_body_out,
871
          const char *queryString)
872
0
{
873
0
  if (!sql_body_in && !as)
874
0
    ereport(ERROR,
875
0
        (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
876
0
         errmsg("no function body specified")));
877
878
0
  if (sql_body_in && as)
879
0
    ereport(ERROR,
880
0
        (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
881
0
         errmsg("duplicate function body specified")));
882
883
0
  if (sql_body_in && languageOid != SQLlanguageId)
884
0
    ereport(ERROR,
885
0
        (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
886
0
         errmsg("inline SQL function body only valid for language SQL")));
887
888
0
  *sql_body_out = NULL;
889
890
0
  if (languageOid == ClanguageId)
891
0
  {
892
    /*
893
     * For "C" language, store the file name in probin and, when given,
894
     * the link symbol name in prosrc.  If link symbol is omitted,
895
     * substitute procedure name.  We also allow link symbol to be
896
     * specified as "-", since that was the habit in PG versions before
897
     * 8.4, and there might be dump files out there that don't translate
898
     * that back to "omitted".
899
     */
900
0
    *probin_str_p = strVal(linitial(as));
901
0
    if (list_length(as) == 1)
902
0
      *prosrc_str_p = funcname;
903
0
    else
904
0
    {
905
0
      *prosrc_str_p = strVal(lsecond(as));
906
0
      if (strcmp(*prosrc_str_p, "-") == 0)
907
0
        *prosrc_str_p = funcname;
908
0
    }
909
0
  }
910
0
  else if (sql_body_in)
911
0
  {
912
0
    SQLFunctionParseInfoPtr pinfo;
913
914
0
    pinfo = (SQLFunctionParseInfoPtr) palloc0(sizeof(SQLFunctionParseInfo));
915
916
0
    pinfo->fname = funcname;
917
0
    pinfo->nargs = list_length(parameterTypes);
918
0
    pinfo->argtypes = (Oid *) palloc(pinfo->nargs * sizeof(Oid));
919
0
    pinfo->argnames = (char **) palloc(pinfo->nargs * sizeof(char *));
920
0
    for (int i = 0; i < list_length(parameterTypes); i++)
921
0
    {
922
0
      char     *s = strVal(list_nth(inParameterNames, i));
923
924
0
      pinfo->argtypes[i] = list_nth_oid(parameterTypes, i);
925
0
      if (IsPolymorphicType(pinfo->argtypes[i]))
926
0
        ereport(ERROR,
927
0
            (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
928
0
             errmsg("SQL function with unquoted function body cannot have polymorphic arguments")));
929
930
0
      if (s[0] != '\0')
931
0
        pinfo->argnames[i] = s;
932
0
      else
933
0
        pinfo->argnames[i] = NULL;
934
0
    }
935
936
0
    if (IsA(sql_body_in, List))
937
0
    {
938
0
      List     *stmts = linitial_node(List, castNode(List, sql_body_in));
939
0
      ListCell   *lc;
940
0
      List     *transformed_stmts = NIL;
941
942
0
      foreach(lc, stmts)
943
0
      {
944
0
        Node     *stmt = lfirst(lc);
945
0
        Query    *q;
946
0
        ParseState *pstate = make_parsestate(NULL);
947
948
0
        pstate->p_sourcetext = queryString;
949
0
        sql_fn_parser_setup(pstate, pinfo);
950
0
        q = transformStmt(pstate, stmt);
951
0
        if (q->commandType == CMD_UTILITY)
952
0
          ereport(ERROR,
953
0
              errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
954
0
              errmsg("%s is not yet supported in unquoted SQL function body",
955
0
                   GetCommandTagName(CreateCommandTag(q->utilityStmt))));
956
0
        transformed_stmts = lappend(transformed_stmts, q);
957
0
        free_parsestate(pstate);
958
0
      }
959
960
0
      *sql_body_out = (Node *) list_make1(transformed_stmts);
961
0
    }
962
0
    else
963
0
    {
964
0
      Query    *q;
965
0
      ParseState *pstate = make_parsestate(NULL);
966
967
0
      pstate->p_sourcetext = queryString;
968
0
      sql_fn_parser_setup(pstate, pinfo);
969
0
      q = transformStmt(pstate, sql_body_in);
970
0
      if (q->commandType == CMD_UTILITY)
971
0
        ereport(ERROR,
972
0
            errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
973
0
            errmsg("%s is not yet supported in unquoted SQL function body",
974
0
                 GetCommandTagName(CreateCommandTag(q->utilityStmt))));
975
0
      free_parsestate(pstate);
976
977
0
      *sql_body_out = (Node *) q;
978
0
    }
979
980
    /*
981
     * We must put something in prosrc.  For the moment, just record an
982
     * empty string.  It might be useful to store the original text of the
983
     * CREATE FUNCTION statement --- but to make actual use of that in
984
     * error reports, we'd also have to adjust readfuncs.c to not throw
985
     * away node location fields when reading prosqlbody.
986
     */
987
0
    *prosrc_str_p = pstrdup("");
988
989
    /* But we definitely don't need probin. */
990
0
    *probin_str_p = NULL;
991
0
  }
992
0
  else
993
0
  {
994
    /* Everything else wants the given string in prosrc. */
995
0
    *prosrc_str_p = strVal(linitial(as));
996
0
    *probin_str_p = NULL;
997
998
0
    if (list_length(as) != 1)
999
0
      ereport(ERROR,
1000
0
          (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
1001
0
           errmsg("only one AS item needed for language \"%s\"",
1002
0
              languageName)));
1003
1004
0
    if (languageOid == INTERNALlanguageId)
1005
0
    {
1006
      /*
1007
       * In PostgreSQL versions before 6.5, the SQL name of the created
1008
       * function could not be different from the internal name, and
1009
       * "prosrc" wasn't used.  So there is code out there that does
1010
       * CREATE FUNCTION xyz AS '' LANGUAGE internal. To preserve some
1011
       * modicum of backwards compatibility, accept an empty "prosrc"
1012
       * value as meaning the supplied SQL function name.
1013
       */
1014
0
      if (strlen(*prosrc_str_p) == 0)
1015
0
        *prosrc_str_p = funcname;
1016
0
    }
1017
0
  }
1018
0
}
1019
1020
1021
/*
1022
 * CreateFunction
1023
 *   Execute a CREATE FUNCTION (or CREATE PROCEDURE) utility statement.
1024
 */
1025
ObjectAddress
1026
CreateFunction(ParseState *pstate, CreateFunctionStmt *stmt)
1027
0
{
1028
0
  char     *probin_str;
1029
0
  char     *prosrc_str;
1030
0
  Node     *prosqlbody;
1031
0
  Oid     prorettype;
1032
0
  bool    returnsSet;
1033
0
  char     *language;
1034
0
  Oid     languageOid;
1035
0
  Oid     languageValidator;
1036
0
  Node     *transformDefElem = NULL;
1037
0
  char     *funcname;
1038
0
  Oid     namespaceId;
1039
0
  AclResult aclresult;
1040
0
  oidvector  *parameterTypes;
1041
0
  List     *parameterTypes_list = NIL;
1042
0
  ArrayType  *allParameterTypes;
1043
0
  ArrayType  *parameterModes;
1044
0
  ArrayType  *parameterNames;
1045
0
  List     *inParameterNames_list = NIL;
1046
0
  List     *parameterDefaults;
1047
0
  Oid     variadicArgType;
1048
0
  List     *trftypes_list = NIL;
1049
0
  List     *trfoids_list = NIL;
1050
0
  ArrayType  *trftypes;
1051
0
  Oid     requiredResultType;
1052
0
  bool    isWindowFunc,
1053
0
        isStrict,
1054
0
        security,
1055
0
        isLeakProof;
1056
0
  char    volatility;
1057
0
  ArrayType  *proconfig;
1058
0
  float4    procost;
1059
0
  float4    prorows;
1060
0
  Oid     prosupport;
1061
0
  HeapTuple languageTuple;
1062
0
  Form_pg_language languageStruct;
1063
0
  List     *as_clause;
1064
0
  char    parallel;
1065
1066
  /* Convert list of names to a name and namespace */
1067
0
  namespaceId = QualifiedNameGetCreationNamespace(stmt->funcname,
1068
0
                          &funcname);
1069
1070
  /* Check we have creation rights in target namespace */
1071
0
  aclresult = object_aclcheck(NamespaceRelationId, namespaceId, GetUserId(), ACL_CREATE);
1072
0
  if (aclresult != ACLCHECK_OK)
1073
0
    aclcheck_error(aclresult, OBJECT_SCHEMA,
1074
0
             get_namespace_name(namespaceId));
1075
1076
  /* Set default attributes */
1077
0
  as_clause = NIL;
1078
0
  language = NULL;
1079
0
  isWindowFunc = false;
1080
0
  isStrict = false;
1081
0
  security = false;
1082
0
  isLeakProof = false;
1083
0
  volatility = PROVOLATILE_VOLATILE;
1084
0
  proconfig = NULL;
1085
0
  procost = -1;       /* indicates not set */
1086
0
  prorows = -1;       /* indicates not set */
1087
0
  prosupport = InvalidOid;
1088
0
  parallel = PROPARALLEL_UNSAFE;
1089
1090
  /* Extract non-default attributes from stmt->options list */
1091
0
  compute_function_attributes(pstate,
1092
0
                stmt->is_procedure,
1093
0
                stmt->options,
1094
0
                &as_clause, &language, &transformDefElem,
1095
0
                &isWindowFunc, &volatility,
1096
0
                &isStrict, &security, &isLeakProof,
1097
0
                &proconfig, &procost, &prorows,
1098
0
                &prosupport, &parallel);
1099
1100
0
  if (!language)
1101
0
  {
1102
0
    if (stmt->sql_body)
1103
0
      language = "sql";
1104
0
    else
1105
0
      ereport(ERROR,
1106
0
          (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
1107
0
           errmsg("no language specified")));
1108
0
  }
1109
1110
  /* Look up the language and validate permissions */
1111
0
  languageTuple = SearchSysCache1(LANGNAME, PointerGetDatum(language));
1112
0
  if (!HeapTupleIsValid(languageTuple))
1113
0
    ereport(ERROR,
1114
0
        (errcode(ERRCODE_UNDEFINED_OBJECT),
1115
0
         errmsg("language \"%s\" does not exist", language),
1116
0
         (extension_file_exists(language) ?
1117
0
          errhint("Use CREATE EXTENSION to load the language into the database.") : 0)));
1118
1119
0
  languageStruct = (Form_pg_language) GETSTRUCT(languageTuple);
1120
0
  languageOid = languageStruct->oid;
1121
1122
0
  if (languageStruct->lanpltrusted)
1123
0
  {
1124
    /* if trusted language, need USAGE privilege */
1125
0
    aclresult = object_aclcheck(LanguageRelationId, languageOid, GetUserId(), ACL_USAGE);
1126
0
    if (aclresult != ACLCHECK_OK)
1127
0
      aclcheck_error(aclresult, OBJECT_LANGUAGE,
1128
0
               NameStr(languageStruct->lanname));
1129
0
  }
1130
0
  else
1131
0
  {
1132
    /* if untrusted language, must be superuser */
1133
0
    if (!superuser())
1134
0
      aclcheck_error(ACLCHECK_NO_PRIV, OBJECT_LANGUAGE,
1135
0
               NameStr(languageStruct->lanname));
1136
0
  }
1137
1138
0
  languageValidator = languageStruct->lanvalidator;
1139
1140
0
  ReleaseSysCache(languageTuple);
1141
1142
  /*
1143
   * Only superuser is allowed to create leakproof functions because
1144
   * leakproof functions can see tuples which have not yet been filtered out
1145
   * by security barrier views or row-level security policies.
1146
   */
1147
0
  if (isLeakProof && !superuser())
1148
0
    ereport(ERROR,
1149
0
        (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
1150
0
         errmsg("only superuser can define a leakproof function")));
1151
1152
0
  if (transformDefElem)
1153
0
  {
1154
0
    ListCell   *lc;
1155
1156
0
    foreach(lc, castNode(List, transformDefElem))
1157
0
    {
1158
0
      Oid     typeid = typenameTypeId(NULL,
1159
0
                        lfirst_node(TypeName, lc));
1160
0
      Oid     elt = get_base_element_type(typeid);
1161
0
      Oid     transformid;
1162
1163
0
      typeid = elt ? elt : typeid;
1164
0
      transformid = get_transform_oid(typeid, languageOid, false);
1165
0
      trftypes_list = lappend_oid(trftypes_list, typeid);
1166
0
      trfoids_list = lappend_oid(trfoids_list, transformid);
1167
0
    }
1168
0
  }
1169
1170
  /*
1171
   * Convert remaining parameters of CREATE to form wanted by
1172
   * ProcedureCreate.
1173
   */
1174
0
  interpret_function_parameter_list(pstate,
1175
0
                    stmt->parameters,
1176
0
                    languageOid,
1177
0
                    stmt->is_procedure ? OBJECT_PROCEDURE : OBJECT_FUNCTION,
1178
0
                    &parameterTypes,
1179
0
                    &parameterTypes_list,
1180
0
                    &allParameterTypes,
1181
0
                    &parameterModes,
1182
0
                    &parameterNames,
1183
0
                    &inParameterNames_list,
1184
0
                    &parameterDefaults,
1185
0
                    &variadicArgType,
1186
0
                    &requiredResultType);
1187
1188
0
  if (stmt->is_procedure)
1189
0
  {
1190
0
    Assert(!stmt->returnType);
1191
0
    prorettype = requiredResultType ? requiredResultType : VOIDOID;
1192
0
    returnsSet = false;
1193
0
  }
1194
0
  else if (stmt->returnType)
1195
0
  {
1196
    /* explicit RETURNS clause */
1197
0
    compute_return_type(stmt->returnType, languageOid,
1198
0
              &prorettype, &returnsSet);
1199
0
    if (OidIsValid(requiredResultType) && prorettype != requiredResultType)
1200
0
      ereport(ERROR,
1201
0
          (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
1202
0
           errmsg("function result type must be %s because of OUT parameters",
1203
0
              format_type_be(requiredResultType))));
1204
0
  }
1205
0
  else if (OidIsValid(requiredResultType))
1206
0
  {
1207
    /* default RETURNS clause from OUT parameters */
1208
0
    prorettype = requiredResultType;
1209
0
    returnsSet = false;
1210
0
  }
1211
0
  else
1212
0
  {
1213
0
    ereport(ERROR,
1214
0
        (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
1215
0
         errmsg("function result type must be specified")));
1216
    /* Alternative possibility: default to RETURNS VOID */
1217
0
    prorettype = VOIDOID;
1218
0
    returnsSet = false;
1219
0
  }
1220
1221
0
  if (trftypes_list != NIL)
1222
0
  {
1223
0
    ListCell   *lc;
1224
0
    Datum    *arr;
1225
0
    int     i;
1226
1227
0
    arr = palloc(list_length(trftypes_list) * sizeof(Datum));
1228
0
    i = 0;
1229
0
    foreach(lc, trftypes_list)
1230
0
      arr[i++] = ObjectIdGetDatum(lfirst_oid(lc));
1231
0
    trftypes = construct_array_builtin(arr, list_length(trftypes_list), OIDOID);
1232
0
  }
1233
0
  else
1234
0
  {
1235
    /* store SQL NULL instead of empty array */
1236
0
    trftypes = NULL;
1237
0
  }
1238
1239
0
  interpret_AS_clause(languageOid, language, funcname, as_clause, stmt->sql_body,
1240
0
            parameterTypes_list, inParameterNames_list,
1241
0
            &prosrc_str, &probin_str, &prosqlbody,
1242
0
            pstate->p_sourcetext);
1243
1244
  /*
1245
   * Set default values for COST and ROWS depending on other parameters;
1246
   * reject ROWS if it's not returnsSet.  NB: pg_dump knows these default
1247
   * values, keep it in sync if you change them.
1248
   */
1249
0
  if (procost < 0)
1250
0
  {
1251
    /* SQL and PL-language functions are assumed more expensive */
1252
0
    if (languageOid == INTERNALlanguageId ||
1253
0
      languageOid == ClanguageId)
1254
0
      procost = 1;
1255
0
    else
1256
0
      procost = 100;
1257
0
  }
1258
0
  if (prorows < 0)
1259
0
  {
1260
0
    if (returnsSet)
1261
0
      prorows = 1000;
1262
0
    else
1263
0
      prorows = 0;   /* dummy value if not returnsSet */
1264
0
  }
1265
0
  else if (!returnsSet)
1266
0
    ereport(ERROR,
1267
0
        (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1268
0
         errmsg("ROWS is not applicable when function does not return a set")));
1269
1270
  /*
1271
   * And now that we have all the parameters, and know we're permitted to do
1272
   * so, go ahead and create the function.
1273
   */
1274
0
  return ProcedureCreate(funcname,
1275
0
               namespaceId,
1276
0
               stmt->replace,
1277
0
               returnsSet,
1278
0
               prorettype,
1279
0
               GetUserId(),
1280
0
               languageOid,
1281
0
               languageValidator,
1282
0
               prosrc_str,  /* converted to text later */
1283
0
               probin_str,  /* converted to text later */
1284
0
               prosqlbody,
1285
0
               stmt->is_procedure ? PROKIND_PROCEDURE : (isWindowFunc ? PROKIND_WINDOW : PROKIND_FUNCTION),
1286
0
               security,
1287
0
               isLeakProof,
1288
0
               isStrict,
1289
0
               volatility,
1290
0
               parallel,
1291
0
               parameterTypes,
1292
0
               PointerGetDatum(allParameterTypes),
1293
0
               PointerGetDatum(parameterModes),
1294
0
               PointerGetDatum(parameterNames),
1295
0
               parameterDefaults,
1296
0
               PointerGetDatum(trftypes),
1297
0
               trfoids_list,
1298
0
               PointerGetDatum(proconfig),
1299
0
               prosupport,
1300
0
               procost,
1301
0
               prorows);
1302
0
}
1303
1304
/*
1305
 * Guts of function deletion.
1306
 *
1307
 * Note: this is also used for aggregate deletion, since the OIDs of
1308
 * both functions and aggregates point to pg_proc.
1309
 */
1310
void
1311
RemoveFunctionById(Oid funcOid)
1312
0
{
1313
0
  Relation  relation;
1314
0
  HeapTuple tup;
1315
0
  char    prokind;
1316
1317
  /*
1318
   * Delete the pg_proc tuple.
1319
   */
1320
0
  relation = table_open(ProcedureRelationId, RowExclusiveLock);
1321
1322
0
  tup = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcOid));
1323
0
  if (!HeapTupleIsValid(tup)) /* should not happen */
1324
0
    elog(ERROR, "cache lookup failed for function %u", funcOid);
1325
1326
0
  prokind = ((Form_pg_proc) GETSTRUCT(tup))->prokind;
1327
1328
0
  CatalogTupleDelete(relation, &tup->t_self);
1329
1330
0
  ReleaseSysCache(tup);
1331
1332
0
  table_close(relation, RowExclusiveLock);
1333
1334
0
  pgstat_drop_function(funcOid);
1335
1336
  /*
1337
   * If there's a pg_aggregate tuple, delete that too.
1338
   */
1339
0
  if (prokind == PROKIND_AGGREGATE)
1340
0
  {
1341
0
    relation = table_open(AggregateRelationId, RowExclusiveLock);
1342
1343
0
    tup = SearchSysCache1(AGGFNOID, ObjectIdGetDatum(funcOid));
1344
0
    if (!HeapTupleIsValid(tup)) /* should not happen */
1345
0
      elog(ERROR, "cache lookup failed for pg_aggregate tuple for function %u", funcOid);
1346
1347
0
    CatalogTupleDelete(relation, &tup->t_self);
1348
1349
0
    ReleaseSysCache(tup);
1350
1351
0
    table_close(relation, RowExclusiveLock);
1352
0
  }
1353
0
}
1354
1355
/*
1356
 * Implements the ALTER FUNCTION utility command (except for the
1357
 * RENAME and OWNER clauses, which are handled as part of the generic
1358
 * ALTER framework).
1359
 */
1360
ObjectAddress
1361
AlterFunction(ParseState *pstate, AlterFunctionStmt *stmt)
1362
0
{
1363
0
  HeapTuple tup;
1364
0
  Oid     funcOid;
1365
0
  Form_pg_proc procForm;
1366
0
  bool    is_procedure;
1367
0
  Relation  rel;
1368
0
  ListCell   *l;
1369
0
  DefElem    *volatility_item = NULL;
1370
0
  DefElem    *strict_item = NULL;
1371
0
  DefElem    *security_def_item = NULL;
1372
0
  DefElem    *leakproof_item = NULL;
1373
0
  List     *set_items = NIL;
1374
0
  DefElem    *cost_item = NULL;
1375
0
  DefElem    *rows_item = NULL;
1376
0
  DefElem    *support_item = NULL;
1377
0
  DefElem    *parallel_item = NULL;
1378
0
  ObjectAddress address;
1379
1380
0
  rel = table_open(ProcedureRelationId, RowExclusiveLock);
1381
1382
0
  funcOid = LookupFuncWithArgs(stmt->objtype, stmt->func, false);
1383
1384
0
  ObjectAddressSet(address, ProcedureRelationId, funcOid);
1385
1386
0
  tup = SearchSysCacheCopy1(PROCOID, ObjectIdGetDatum(funcOid));
1387
0
  if (!HeapTupleIsValid(tup)) /* should not happen */
1388
0
    elog(ERROR, "cache lookup failed for function %u", funcOid);
1389
1390
0
  procForm = (Form_pg_proc) GETSTRUCT(tup);
1391
1392
  /* Permission check: must own function */
1393
0
  if (!object_ownercheck(ProcedureRelationId, funcOid, GetUserId()))
1394
0
    aclcheck_error(ACLCHECK_NOT_OWNER, stmt->objtype,
1395
0
             NameListToString(stmt->func->objname));
1396
1397
0
  if (procForm->prokind == PROKIND_AGGREGATE)
1398
0
    ereport(ERROR,
1399
0
        (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1400
0
         errmsg("\"%s\" is an aggregate function",
1401
0
            NameListToString(stmt->func->objname))));
1402
1403
0
  is_procedure = (procForm->prokind == PROKIND_PROCEDURE);
1404
1405
  /* Examine requested actions. */
1406
0
  foreach(l, stmt->actions)
1407
0
  {
1408
0
    DefElem    *defel = (DefElem *) lfirst(l);
1409
1410
0
    if (compute_common_attribute(pstate,
1411
0
                   is_procedure,
1412
0
                   defel,
1413
0
                   &volatility_item,
1414
0
                   &strict_item,
1415
0
                   &security_def_item,
1416
0
                   &leakproof_item,
1417
0
                   &set_items,
1418
0
                   &cost_item,
1419
0
                   &rows_item,
1420
0
                   &support_item,
1421
0
                   &parallel_item) == false)
1422
0
      elog(ERROR, "option \"%s\" not recognized", defel->defname);
1423
0
  }
1424
1425
0
  if (volatility_item)
1426
0
    procForm->provolatile = interpret_func_volatility(volatility_item);
1427
0
  if (strict_item)
1428
0
    procForm->proisstrict = boolVal(strict_item->arg);
1429
0
  if (security_def_item)
1430
0
    procForm->prosecdef = boolVal(security_def_item->arg);
1431
0
  if (leakproof_item)
1432
0
  {
1433
0
    procForm->proleakproof = boolVal(leakproof_item->arg);
1434
0
    if (procForm->proleakproof && !superuser())
1435
0
      ereport(ERROR,
1436
0
          (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
1437
0
           errmsg("only superuser can define a leakproof function")));
1438
0
  }
1439
0
  if (cost_item)
1440
0
  {
1441
0
    procForm->procost = defGetNumeric(cost_item);
1442
0
    if (procForm->procost <= 0)
1443
0
      ereport(ERROR,
1444
0
          (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1445
0
           errmsg("COST must be positive")));
1446
0
  }
1447
0
  if (rows_item)
1448
0
  {
1449
0
    procForm->prorows = defGetNumeric(rows_item);
1450
0
    if (procForm->prorows <= 0)
1451
0
      ereport(ERROR,
1452
0
          (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1453
0
           errmsg("ROWS must be positive")));
1454
0
    if (!procForm->proretset)
1455
0
      ereport(ERROR,
1456
0
          (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1457
0
           errmsg("ROWS is not applicable when function does not return a set")));
1458
0
  }
1459
0
  if (support_item)
1460
0
  {
1461
    /* interpret_func_support handles the privilege check */
1462
0
    Oid     newsupport = interpret_func_support(support_item);
1463
1464
    /* Add or replace dependency on support function */
1465
0
    if (OidIsValid(procForm->prosupport))
1466
0
    {
1467
0
      if (changeDependencyFor(ProcedureRelationId, funcOid,
1468
0
                  ProcedureRelationId, procForm->prosupport,
1469
0
                  newsupport) != 1)
1470
0
        elog(ERROR, "could not change support dependency for function %s",
1471
0
           get_func_name(funcOid));
1472
0
    }
1473
0
    else
1474
0
    {
1475
0
      ObjectAddress referenced;
1476
1477
0
      referenced.classId = ProcedureRelationId;
1478
0
      referenced.objectId = newsupport;
1479
0
      referenced.objectSubId = 0;
1480
0
      recordDependencyOn(&address, &referenced, DEPENDENCY_NORMAL);
1481
0
    }
1482
1483
0
    procForm->prosupport = newsupport;
1484
0
  }
1485
0
  if (parallel_item)
1486
0
    procForm->proparallel = interpret_func_parallel(parallel_item);
1487
0
  if (set_items)
1488
0
  {
1489
0
    Datum   datum;
1490
0
    bool    isnull;
1491
0
    ArrayType  *a;
1492
0
    Datum   repl_val[Natts_pg_proc];
1493
0
    bool    repl_null[Natts_pg_proc];
1494
0
    bool    repl_repl[Natts_pg_proc];
1495
1496
    /* extract existing proconfig setting */
1497
0
    datum = SysCacheGetAttr(PROCOID, tup, Anum_pg_proc_proconfig, &isnull);
1498
0
    a = isnull ? NULL : DatumGetArrayTypeP(datum);
1499
1500
    /* update according to each SET or RESET item, left to right */
1501
0
    a = update_proconfig_value(a, set_items);
1502
1503
    /* update the tuple */
1504
0
    memset(repl_repl, false, sizeof(repl_repl));
1505
0
    repl_repl[Anum_pg_proc_proconfig - 1] = true;
1506
1507
0
    if (a == NULL)
1508
0
    {
1509
0
      repl_val[Anum_pg_proc_proconfig - 1] = (Datum) 0;
1510
0
      repl_null[Anum_pg_proc_proconfig - 1] = true;
1511
0
    }
1512
0
    else
1513
0
    {
1514
0
      repl_val[Anum_pg_proc_proconfig - 1] = PointerGetDatum(a);
1515
0
      repl_null[Anum_pg_proc_proconfig - 1] = false;
1516
0
    }
1517
1518
0
    tup = heap_modify_tuple(tup, RelationGetDescr(rel),
1519
0
                repl_val, repl_null, repl_repl);
1520
0
  }
1521
  /* DO NOT put more touches of procForm below here; it's now dangling. */
1522
1523
  /* Do the update */
1524
0
  CatalogTupleUpdate(rel, &tup->t_self, tup);
1525
1526
0
  InvokeObjectPostAlterHook(ProcedureRelationId, funcOid, 0);
1527
1528
0
  table_close(rel, NoLock);
1529
0
  heap_freetuple(tup);
1530
1531
0
  return address;
1532
0
}
1533
1534
1535
/*
1536
 * CREATE CAST
1537
 */
1538
ObjectAddress
1539
CreateCast(CreateCastStmt *stmt)
1540
0
{
1541
0
  Oid     sourcetypeid;
1542
0
  Oid     targettypeid;
1543
0
  char    sourcetyptype;
1544
0
  char    targettyptype;
1545
0
  Oid     funcid;
1546
0
  Oid     incastid = InvalidOid;
1547
0
  Oid     outcastid = InvalidOid;
1548
0
  int     nargs;
1549
0
  char    castcontext;
1550
0
  char    castmethod;
1551
0
  HeapTuple tuple;
1552
0
  AclResult aclresult;
1553
0
  ObjectAddress myself;
1554
1555
0
  sourcetypeid = typenameTypeId(NULL, stmt->sourcetype);
1556
0
  targettypeid = typenameTypeId(NULL, stmt->targettype);
1557
0
  sourcetyptype = get_typtype(sourcetypeid);
1558
0
  targettyptype = get_typtype(targettypeid);
1559
1560
  /* No pseudo-types allowed */
1561
0
  if (sourcetyptype == TYPTYPE_PSEUDO)
1562
0
    ereport(ERROR,
1563
0
        (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1564
0
         errmsg("source data type %s is a pseudo-type",
1565
0
            TypeNameToString(stmt->sourcetype))));
1566
1567
0
  if (targettyptype == TYPTYPE_PSEUDO)
1568
0
    ereport(ERROR,
1569
0
        (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1570
0
         errmsg("target data type %s is a pseudo-type",
1571
0
            TypeNameToString(stmt->targettype))));
1572
1573
  /* Permission check */
1574
0
  if (!object_ownercheck(TypeRelationId, sourcetypeid, GetUserId())
1575
0
    && !object_ownercheck(TypeRelationId, targettypeid, GetUserId()))
1576
0
    ereport(ERROR,
1577
0
        (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
1578
0
         errmsg("must be owner of type %s or type %s",
1579
0
            format_type_be(sourcetypeid),
1580
0
            format_type_be(targettypeid))));
1581
1582
0
  aclresult = object_aclcheck(TypeRelationId, sourcetypeid, GetUserId(), ACL_USAGE);
1583
0
  if (aclresult != ACLCHECK_OK)
1584
0
    aclcheck_error_type(aclresult, sourcetypeid);
1585
1586
0
  aclresult = object_aclcheck(TypeRelationId, targettypeid, GetUserId(), ACL_USAGE);
1587
0
  if (aclresult != ACLCHECK_OK)
1588
0
    aclcheck_error_type(aclresult, targettypeid);
1589
1590
  /* Domains are allowed for historical reasons, but we warn */
1591
0
  if (sourcetyptype == TYPTYPE_DOMAIN)
1592
0
    ereport(WARNING,
1593
0
        (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1594
0
         errmsg("cast will be ignored because the source data type is a domain")));
1595
1596
0
  else if (targettyptype == TYPTYPE_DOMAIN)
1597
0
    ereport(WARNING,
1598
0
        (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1599
0
         errmsg("cast will be ignored because the target data type is a domain")));
1600
1601
  /* Determine the cast method */
1602
0
  if (stmt->func != NULL)
1603
0
    castmethod = COERCION_METHOD_FUNCTION;
1604
0
  else if (stmt->inout)
1605
0
    castmethod = COERCION_METHOD_INOUT;
1606
0
  else
1607
0
    castmethod = COERCION_METHOD_BINARY;
1608
1609
0
  if (castmethod == COERCION_METHOD_FUNCTION)
1610
0
  {
1611
0
    Form_pg_proc procstruct;
1612
1613
0
    funcid = LookupFuncWithArgs(OBJECT_FUNCTION, stmt->func, false);
1614
1615
0
    tuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
1616
0
    if (!HeapTupleIsValid(tuple))
1617
0
      elog(ERROR, "cache lookup failed for function %u", funcid);
1618
1619
0
    procstruct = (Form_pg_proc) GETSTRUCT(tuple);
1620
0
    nargs = procstruct->pronargs;
1621
0
    if (nargs < 1 || nargs > 3)
1622
0
      ereport(ERROR,
1623
0
          (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1624
0
           errmsg("cast function must take one to three arguments")));
1625
0
    if (!IsBinaryCoercibleWithCast(sourcetypeid,
1626
0
                     procstruct->proargtypes.values[0],
1627
0
                     &incastid))
1628
0
      ereport(ERROR,
1629
0
          (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1630
0
           errmsg("argument of cast function must match or be binary-coercible from source data type")));
1631
0
    if (nargs > 1 && procstruct->proargtypes.values[1] != INT4OID)
1632
0
      ereport(ERROR,
1633
0
          (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1634
0
           errmsg("second argument of cast function must be type %s",
1635
0
              "integer")));
1636
0
    if (nargs > 2 && procstruct->proargtypes.values[2] != BOOLOID)
1637
0
      ereport(ERROR,
1638
0
          (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1639
0
           errmsg("third argument of cast function must be type %s",
1640
0
              "boolean")));
1641
0
    if (!IsBinaryCoercibleWithCast(procstruct->prorettype,
1642
0
                     targettypeid,
1643
0
                     &outcastid))
1644
0
      ereport(ERROR,
1645
0
          (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1646
0
           errmsg("return data type of cast function must match or be binary-coercible to target data type")));
1647
1648
    /*
1649
     * Restricting the volatility of a cast function may or may not be a
1650
     * good idea in the abstract, but it definitely breaks many old
1651
     * user-defined types.  Disable this check --- tgl 2/1/03
1652
     */
1653
#ifdef NOT_USED
1654
    if (procstruct->provolatile == PROVOLATILE_VOLATILE)
1655
      ereport(ERROR,
1656
          (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1657
           errmsg("cast function must not be volatile")));
1658
#endif
1659
0
    if (procstruct->prokind != PROKIND_FUNCTION)
1660
0
      ereport(ERROR,
1661
0
          (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1662
0
           errmsg("cast function must be a normal function")));
1663
0
    if (procstruct->proretset)
1664
0
      ereport(ERROR,
1665
0
          (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1666
0
           errmsg("cast function must not return a set")));
1667
1668
0
    ReleaseSysCache(tuple);
1669
0
  }
1670
0
  else
1671
0
  {
1672
0
    funcid = InvalidOid;
1673
0
    nargs = 0;
1674
0
  }
1675
1676
0
  if (castmethod == COERCION_METHOD_BINARY)
1677
0
  {
1678
0
    int16   typ1len;
1679
0
    int16   typ2len;
1680
0
    bool    typ1byval;
1681
0
    bool    typ2byval;
1682
0
    char    typ1align;
1683
0
    char    typ2align;
1684
1685
    /*
1686
     * Must be superuser to create binary-compatible casts, since
1687
     * erroneous casts can easily crash the backend.
1688
     */
1689
0
    if (!superuser())
1690
0
      ereport(ERROR,
1691
0
          (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
1692
0
           errmsg("must be superuser to create a cast WITHOUT FUNCTION")));
1693
1694
    /*
1695
     * Also, insist that the types match as to size, alignment, and
1696
     * pass-by-value attributes; this provides at least a crude check that
1697
     * they have similar representations.  A pair of types that fail this
1698
     * test should certainly not be equated.
1699
     */
1700
0
    get_typlenbyvalalign(sourcetypeid, &typ1len, &typ1byval, &typ1align);
1701
0
    get_typlenbyvalalign(targettypeid, &typ2len, &typ2byval, &typ2align);
1702
0
    if (typ1len != typ2len ||
1703
0
      typ1byval != typ2byval ||
1704
0
      typ1align != typ2align)
1705
0
      ereport(ERROR,
1706
0
          (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1707
0
           errmsg("source and target data types are not physically compatible")));
1708
1709
    /*
1710
     * We know that composite, array, range and enum types are never
1711
     * binary-compatible with each other.  They all have OIDs embedded in
1712
     * them.
1713
     *
1714
     * Theoretically you could build a user-defined base type that is
1715
     * binary-compatible with such a type.  But we disallow it anyway, as
1716
     * in practice such a cast is surely a mistake.  You can always work
1717
     * around that by writing a cast function.
1718
     *
1719
     * NOTE: if we ever have a kind of container type that doesn't need to
1720
     * be rejected for this reason, we'd likely need to recursively apply
1721
     * all of these same checks to the contained type(s).
1722
     */
1723
0
    if (sourcetyptype == TYPTYPE_COMPOSITE ||
1724
0
      targettyptype == TYPTYPE_COMPOSITE)
1725
0
      ereport(ERROR,
1726
0
          (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1727
0
           errmsg("composite data types are not binary-compatible")));
1728
1729
0
    if (OidIsValid(get_element_type(sourcetypeid)) ||
1730
0
      OidIsValid(get_element_type(targettypeid)))
1731
0
      ereport(ERROR,
1732
0
          (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1733
0
           errmsg("array data types are not binary-compatible")));
1734
1735
0
    if (sourcetyptype == TYPTYPE_RANGE ||
1736
0
      targettyptype == TYPTYPE_RANGE ||
1737
0
      sourcetyptype == TYPTYPE_MULTIRANGE ||
1738
0
      targettyptype == TYPTYPE_MULTIRANGE)
1739
0
      ereport(ERROR,
1740
0
          (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1741
0
           errmsg("range data types are not binary-compatible")));
1742
1743
0
    if (sourcetyptype == TYPTYPE_ENUM ||
1744
0
      targettyptype == TYPTYPE_ENUM)
1745
0
      ereport(ERROR,
1746
0
          (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1747
0
           errmsg("enum data types are not binary-compatible")));
1748
1749
    /*
1750
     * We also disallow creating binary-compatibility casts involving
1751
     * domains.  Casting from a domain to its base type is already
1752
     * allowed, and casting the other way ought to go through domain
1753
     * coercion to permit constraint checking.  Again, if you're intent on
1754
     * having your own semantics for that, create a no-op cast function.
1755
     *
1756
     * NOTE: if we were to relax this, the above checks for composites
1757
     * etc. would have to be modified to look through domains to their
1758
     * base types.
1759
     */
1760
0
    if (sourcetyptype == TYPTYPE_DOMAIN ||
1761
0
      targettyptype == TYPTYPE_DOMAIN)
1762
0
      ereport(ERROR,
1763
0
          (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1764
0
           errmsg("domain data types must not be marked binary-compatible")));
1765
0
  }
1766
1767
  /*
1768
   * Allow source and target types to be same only for length coercion
1769
   * functions.  We assume a multi-arg function does length coercion.
1770
   */
1771
0
  if (sourcetypeid == targettypeid && nargs < 2)
1772
0
    ereport(ERROR,
1773
0
        (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1774
0
         errmsg("source data type and target data type are the same")));
1775
1776
  /* convert CoercionContext enum to char value for castcontext */
1777
0
  switch (stmt->context)
1778
0
  {
1779
0
    case COERCION_IMPLICIT:
1780
0
      castcontext = COERCION_CODE_IMPLICIT;
1781
0
      break;
1782
0
    case COERCION_ASSIGNMENT:
1783
0
      castcontext = COERCION_CODE_ASSIGNMENT;
1784
0
      break;
1785
      /* COERCION_PLPGSQL is intentionally not covered here */
1786
0
    case COERCION_EXPLICIT:
1787
0
      castcontext = COERCION_CODE_EXPLICIT;
1788
0
      break;
1789
0
    default:
1790
0
      elog(ERROR, "unrecognized CoercionContext: %d", stmt->context);
1791
0
      castcontext = 0;  /* keep compiler quiet */
1792
0
      break;
1793
0
  }
1794
1795
0
  myself = CastCreate(sourcetypeid, targettypeid, funcid, incastid, outcastid,
1796
0
            castcontext, castmethod, DEPENDENCY_NORMAL);
1797
0
  return myself;
1798
0
}
1799
1800
1801
static void
1802
check_transform_function(Form_pg_proc procstruct)
1803
0
{
1804
0
  if (procstruct->provolatile == PROVOLATILE_VOLATILE)
1805
0
    ereport(ERROR,
1806
0
        (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1807
0
         errmsg("transform function must not be volatile")));
1808
0
  if (procstruct->prokind != PROKIND_FUNCTION)
1809
0
    ereport(ERROR,
1810
0
        (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1811
0
         errmsg("transform function must be a normal function")));
1812
0
  if (procstruct->proretset)
1813
0
    ereport(ERROR,
1814
0
        (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1815
0
         errmsg("transform function must not return a set")));
1816
0
  if (procstruct->pronargs != 1)
1817
0
    ereport(ERROR,
1818
0
        (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1819
0
         errmsg("transform function must take one argument")));
1820
0
  if (procstruct->proargtypes.values[0] != INTERNALOID)
1821
0
    ereport(ERROR,
1822
0
        (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1823
0
         errmsg("first argument of transform function must be type %s",
1824
0
            "internal")));
1825
0
}
1826
1827
1828
/*
1829
 * CREATE TRANSFORM
1830
 */
1831
ObjectAddress
1832
CreateTransform(CreateTransformStmt *stmt)
1833
0
{
1834
0
  Oid     typeid;
1835
0
  char    typtype;
1836
0
  Oid     langid;
1837
0
  Oid     fromsqlfuncid;
1838
0
  Oid     tosqlfuncid;
1839
0
  AclResult aclresult;
1840
0
  Form_pg_proc procstruct;
1841
0
  Datum   values[Natts_pg_transform];
1842
0
  bool    nulls[Natts_pg_transform] = {0};
1843
0
  bool    replaces[Natts_pg_transform] = {0};
1844
0
  Oid     transformid;
1845
0
  HeapTuple tuple;
1846
0
  HeapTuple newtuple;
1847
0
  Relation  relation;
1848
0
  ObjectAddress myself,
1849
0
        referenced;
1850
0
  ObjectAddresses *addrs;
1851
0
  bool    is_replace;
1852
1853
  /*
1854
   * Get the type
1855
   */
1856
0
  typeid = typenameTypeId(NULL, stmt->type_name);
1857
0
  typtype = get_typtype(typeid);
1858
1859
0
  if (typtype == TYPTYPE_PSEUDO)
1860
0
    ereport(ERROR,
1861
0
        (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1862
0
         errmsg("data type %s is a pseudo-type",
1863
0
            TypeNameToString(stmt->type_name))));
1864
1865
0
  if (typtype == TYPTYPE_DOMAIN)
1866
0
    ereport(ERROR,
1867
0
        (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1868
0
         errmsg("data type %s is a domain",
1869
0
            TypeNameToString(stmt->type_name))));
1870
1871
0
  if (!object_ownercheck(TypeRelationId, typeid, GetUserId()))
1872
0
    aclcheck_error_type(ACLCHECK_NOT_OWNER, typeid);
1873
1874
0
  aclresult = object_aclcheck(TypeRelationId, typeid, GetUserId(), ACL_USAGE);
1875
0
  if (aclresult != ACLCHECK_OK)
1876
0
    aclcheck_error_type(aclresult, typeid);
1877
1878
  /*
1879
   * Get the language
1880
   */
1881
0
  langid = get_language_oid(stmt->lang, false);
1882
1883
0
  aclresult = object_aclcheck(LanguageRelationId, langid, GetUserId(), ACL_USAGE);
1884
0
  if (aclresult != ACLCHECK_OK)
1885
0
    aclcheck_error(aclresult, OBJECT_LANGUAGE, stmt->lang);
1886
1887
  /*
1888
   * Get the functions
1889
   */
1890
0
  if (stmt->fromsql)
1891
0
  {
1892
0
    fromsqlfuncid = LookupFuncWithArgs(OBJECT_FUNCTION, stmt->fromsql, false);
1893
1894
0
    if (!object_ownercheck(ProcedureRelationId, fromsqlfuncid, GetUserId()))
1895
0
      aclcheck_error(ACLCHECK_NOT_OWNER, OBJECT_FUNCTION, NameListToString(stmt->fromsql->objname));
1896
1897
0
    aclresult = object_aclcheck(ProcedureRelationId, fromsqlfuncid, GetUserId(), ACL_EXECUTE);
1898
0
    if (aclresult != ACLCHECK_OK)
1899
0
      aclcheck_error(aclresult, OBJECT_FUNCTION, NameListToString(stmt->fromsql->objname));
1900
1901
0
    tuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(fromsqlfuncid));
1902
0
    if (!HeapTupleIsValid(tuple))
1903
0
      elog(ERROR, "cache lookup failed for function %u", fromsqlfuncid);
1904
0
    procstruct = (Form_pg_proc) GETSTRUCT(tuple);
1905
0
    if (procstruct->prorettype != INTERNALOID)
1906
0
      ereport(ERROR,
1907
0
          (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1908
0
           errmsg("return data type of FROM SQL function must be %s",
1909
0
              "internal")));
1910
0
    check_transform_function(procstruct);
1911
0
    ReleaseSysCache(tuple);
1912
0
  }
1913
0
  else
1914
0
    fromsqlfuncid = InvalidOid;
1915
1916
0
  if (stmt->tosql)
1917
0
  {
1918
0
    tosqlfuncid = LookupFuncWithArgs(OBJECT_FUNCTION, stmt->tosql, false);
1919
1920
0
    if (!object_ownercheck(ProcedureRelationId, tosqlfuncid, GetUserId()))
1921
0
      aclcheck_error(ACLCHECK_NOT_OWNER, OBJECT_FUNCTION, NameListToString(stmt->tosql->objname));
1922
1923
0
    aclresult = object_aclcheck(ProcedureRelationId, tosqlfuncid, GetUserId(), ACL_EXECUTE);
1924
0
    if (aclresult != ACLCHECK_OK)
1925
0
      aclcheck_error(aclresult, OBJECT_FUNCTION, NameListToString(stmt->tosql->objname));
1926
1927
0
    tuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(tosqlfuncid));
1928
0
    if (!HeapTupleIsValid(tuple))
1929
0
      elog(ERROR, "cache lookup failed for function %u", tosqlfuncid);
1930
0
    procstruct = (Form_pg_proc) GETSTRUCT(tuple);
1931
0
    if (procstruct->prorettype != typeid)
1932
0
      ereport(ERROR,
1933
0
          (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1934
0
           errmsg("return data type of TO SQL function must be the transform data type")));
1935
0
    check_transform_function(procstruct);
1936
0
    ReleaseSysCache(tuple);
1937
0
  }
1938
0
  else
1939
0
    tosqlfuncid = InvalidOid;
1940
1941
  /*
1942
   * Ready to go
1943
   */
1944
0
  values[Anum_pg_transform_trftype - 1] = ObjectIdGetDatum(typeid);
1945
0
  values[Anum_pg_transform_trflang - 1] = ObjectIdGetDatum(langid);
1946
0
  values[Anum_pg_transform_trffromsql - 1] = ObjectIdGetDatum(fromsqlfuncid);
1947
0
  values[Anum_pg_transform_trftosql - 1] = ObjectIdGetDatum(tosqlfuncid);
1948
1949
0
  relation = table_open(TransformRelationId, RowExclusiveLock);
1950
1951
0
  tuple = SearchSysCache2(TRFTYPELANG,
1952
0
              ObjectIdGetDatum(typeid),
1953
0
              ObjectIdGetDatum(langid));
1954
0
  if (HeapTupleIsValid(tuple))
1955
0
  {
1956
0
    Form_pg_transform form = (Form_pg_transform) GETSTRUCT(tuple);
1957
1958
0
    if (!stmt->replace)
1959
0
      ereport(ERROR,
1960
0
          (errcode(ERRCODE_DUPLICATE_OBJECT),
1961
0
           errmsg("transform for type %s language \"%s\" already exists",
1962
0
              format_type_be(typeid),
1963
0
              stmt->lang)));
1964
1965
0
    replaces[Anum_pg_transform_trffromsql - 1] = true;
1966
0
    replaces[Anum_pg_transform_trftosql - 1] = true;
1967
1968
0
    newtuple = heap_modify_tuple(tuple, RelationGetDescr(relation), values, nulls, replaces);
1969
0
    CatalogTupleUpdate(relation, &newtuple->t_self, newtuple);
1970
1971
0
    transformid = form->oid;
1972
0
    ReleaseSysCache(tuple);
1973
0
    is_replace = true;
1974
0
  }
1975
0
  else
1976
0
  {
1977
0
    transformid = GetNewOidWithIndex(relation, TransformOidIndexId,
1978
0
                     Anum_pg_transform_oid);
1979
0
    values[Anum_pg_transform_oid - 1] = ObjectIdGetDatum(transformid);
1980
0
    newtuple = heap_form_tuple(RelationGetDescr(relation), values, nulls);
1981
0
    CatalogTupleInsert(relation, newtuple);
1982
0
    is_replace = false;
1983
0
  }
1984
1985
0
  if (is_replace)
1986
0
    deleteDependencyRecordsFor(TransformRelationId, transformid, true);
1987
1988
0
  addrs = new_object_addresses();
1989
1990
  /* make dependency entries */
1991
0
  ObjectAddressSet(myself, TransformRelationId, transformid);
1992
1993
  /* dependency on language */
1994
0
  ObjectAddressSet(referenced, LanguageRelationId, langid);
1995
0
  add_exact_object_address(&referenced, addrs);
1996
1997
  /* dependency on type */
1998
0
  ObjectAddressSet(referenced, TypeRelationId, typeid);
1999
0
  add_exact_object_address(&referenced, addrs);
2000
2001
  /* dependencies on functions */
2002
0
  if (OidIsValid(fromsqlfuncid))
2003
0
  {
2004
0
    ObjectAddressSet(referenced, ProcedureRelationId, fromsqlfuncid);
2005
0
    add_exact_object_address(&referenced, addrs);
2006
0
  }
2007
0
  if (OidIsValid(tosqlfuncid))
2008
0
  {
2009
0
    ObjectAddressSet(referenced, ProcedureRelationId, tosqlfuncid);
2010
0
    add_exact_object_address(&referenced, addrs);
2011
0
  }
2012
2013
0
  record_object_address_dependencies(&myself, addrs, DEPENDENCY_NORMAL);
2014
0
  free_object_addresses(addrs);
2015
2016
  /* dependency on extension */
2017
0
  recordDependencyOnCurrentExtension(&myself, is_replace);
2018
2019
  /* Post creation hook for new transform */
2020
0
  InvokeObjectPostCreateHook(TransformRelationId, transformid, 0);
2021
2022
0
  heap_freetuple(newtuple);
2023
2024
0
  table_close(relation, RowExclusiveLock);
2025
2026
0
  return myself;
2027
0
}
2028
2029
2030
/*
2031
 * get_transform_oid - given type OID and language OID, look up a transform OID
2032
 *
2033
 * If missing_ok is false, throw an error if the transform is not found.  If
2034
 * true, just return InvalidOid.
2035
 */
2036
Oid
2037
get_transform_oid(Oid type_id, Oid lang_id, bool missing_ok)
2038
0
{
2039
0
  Oid     oid;
2040
2041
0
  oid = GetSysCacheOid2(TRFTYPELANG, Anum_pg_transform_oid,
2042
0
              ObjectIdGetDatum(type_id),
2043
0
              ObjectIdGetDatum(lang_id));
2044
0
  if (!OidIsValid(oid) && !missing_ok)
2045
0
    ereport(ERROR,
2046
0
        (errcode(ERRCODE_UNDEFINED_OBJECT),
2047
0
         errmsg("transform for type %s language \"%s\" does not exist",
2048
0
            format_type_be(type_id),
2049
0
            get_language_name(lang_id, false))));
2050
0
  return oid;
2051
0
}
2052
2053
2054
/*
2055
 * Subroutine for ALTER FUNCTION/AGGREGATE SET SCHEMA/RENAME
2056
 *
2057
 * Is there a function with the given name and signature already in the given
2058
 * namespace?  If so, raise an appropriate error message.
2059
 */
2060
void
2061
IsThereFunctionInNamespace(const char *proname, int pronargs,
2062
               oidvector *proargtypes, Oid nspOid)
2063
0
{
2064
  /* check for duplicate name (more friendly than unique-index failure) */
2065
0
  if (SearchSysCacheExists3(PROCNAMEARGSNSP,
2066
0
                CStringGetDatum(proname),
2067
0
                PointerGetDatum(proargtypes),
2068
0
                ObjectIdGetDatum(nspOid)))
2069
0
    ereport(ERROR,
2070
0
        (errcode(ERRCODE_DUPLICATE_FUNCTION),
2071
0
         errmsg("function %s already exists in schema \"%s\"",
2072
0
            funcname_signature_string(proname, pronargs,
2073
0
                          NIL, proargtypes->values),
2074
0
            get_namespace_name(nspOid))));
2075
0
}
2076
2077
/*
2078
 * ExecuteDoStmt
2079
 *    Execute inline procedural-language code
2080
 *
2081
 * See at ExecuteCallStmt() about the atomic argument.
2082
 */
2083
void
2084
ExecuteDoStmt(ParseState *pstate, DoStmt *stmt, bool atomic)
2085
0
{
2086
0
  InlineCodeBlock *codeblock = makeNode(InlineCodeBlock);
2087
0
  ListCell   *arg;
2088
0
  DefElem    *as_item = NULL;
2089
0
  DefElem    *language_item = NULL;
2090
0
  char     *language;
2091
0
  Oid     laninline;
2092
0
  HeapTuple languageTuple;
2093
0
  Form_pg_language languageStruct;
2094
2095
  /* Process options we got from gram.y */
2096
0
  foreach(arg, stmt->args)
2097
0
  {
2098
0
    DefElem    *defel = (DefElem *) lfirst(arg);
2099
2100
0
    if (strcmp(defel->defname, "as") == 0)
2101
0
    {
2102
0
      if (as_item)
2103
0
        errorConflictingDefElem(defel, pstate);
2104
0
      as_item = defel;
2105
0
    }
2106
0
    else if (strcmp(defel->defname, "language") == 0)
2107
0
    {
2108
0
      if (language_item)
2109
0
        errorConflictingDefElem(defel, pstate);
2110
0
      language_item = defel;
2111
0
    }
2112
0
    else
2113
0
      elog(ERROR, "option \"%s\" not recognized",
2114
0
         defel->defname);
2115
0
  }
2116
2117
0
  if (as_item)
2118
0
    codeblock->source_text = strVal(as_item->arg);
2119
0
  else
2120
0
    ereport(ERROR,
2121
0
        (errcode(ERRCODE_SYNTAX_ERROR),
2122
0
         errmsg("no inline code specified")));
2123
2124
  /* if LANGUAGE option wasn't specified, use the default */
2125
0
  if (language_item)
2126
0
    language = strVal(language_item->arg);
2127
0
  else
2128
0
    language = "plpgsql";
2129
2130
  /* Look up the language and validate permissions */
2131
0
  languageTuple = SearchSysCache1(LANGNAME, PointerGetDatum(language));
2132
0
  if (!HeapTupleIsValid(languageTuple))
2133
0
    ereport(ERROR,
2134
0
        (errcode(ERRCODE_UNDEFINED_OBJECT),
2135
0
         errmsg("language \"%s\" does not exist", language),
2136
0
         (extension_file_exists(language) ?
2137
0
          errhint("Use CREATE EXTENSION to load the language into the database.") : 0)));
2138
2139
0
  languageStruct = (Form_pg_language) GETSTRUCT(languageTuple);
2140
0
  codeblock->langOid = languageStruct->oid;
2141
0
  codeblock->langIsTrusted = languageStruct->lanpltrusted;
2142
0
  codeblock->atomic = atomic;
2143
2144
0
  if (languageStruct->lanpltrusted)
2145
0
  {
2146
    /* if trusted language, need USAGE privilege */
2147
0
    AclResult aclresult;
2148
2149
0
    aclresult = object_aclcheck(LanguageRelationId, codeblock->langOid, GetUserId(),
2150
0
                  ACL_USAGE);
2151
0
    if (aclresult != ACLCHECK_OK)
2152
0
      aclcheck_error(aclresult, OBJECT_LANGUAGE,
2153
0
               NameStr(languageStruct->lanname));
2154
0
  }
2155
0
  else
2156
0
  {
2157
    /* if untrusted language, must be superuser */
2158
0
    if (!superuser())
2159
0
      aclcheck_error(ACLCHECK_NO_PRIV, OBJECT_LANGUAGE,
2160
0
               NameStr(languageStruct->lanname));
2161
0
  }
2162
2163
  /* get the handler function's OID */
2164
0
  laninline = languageStruct->laninline;
2165
0
  if (!OidIsValid(laninline))
2166
0
    ereport(ERROR,
2167
0
        (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2168
0
         errmsg("language \"%s\" does not support inline code execution",
2169
0
            NameStr(languageStruct->lanname))));
2170
2171
0
  ReleaseSysCache(languageTuple);
2172
2173
  /* execute the inline handler */
2174
0
  OidFunctionCall1(laninline, PointerGetDatum(codeblock));
2175
0
}
2176
2177
/*
2178
 * Execute CALL statement
2179
 *
2180
 * Inside a top-level CALL statement, transaction-terminating commands such as
2181
 * COMMIT or a PL-specific equivalent are allowed.  The terminology in the SQL
2182
 * standard is that CALL establishes a non-atomic execution context.  Most
2183
 * other commands establish an atomic execution context, in which transaction
2184
 * control actions are not allowed.  If there are nested executions of CALL,
2185
 * we want to track the execution context recursively, so that the nested
2186
 * CALLs can also do transaction control.  Note, however, that for example in
2187
 * CALL -> SELECT -> CALL, the second call cannot do transaction control,
2188
 * because the SELECT in between establishes an atomic execution context.
2189
 *
2190
 * So when ExecuteCallStmt() is called from the top level, we pass in atomic =
2191
 * false (recall that that means transactions = yes).  We then create a
2192
 * CallContext node with content atomic = false, which is passed in the
2193
 * fcinfo->context field to the procedure invocation.  The language
2194
 * implementation should then take appropriate measures to allow or prevent
2195
 * transaction commands based on that information, e.g., call
2196
 * SPI_connect_ext(SPI_OPT_NONATOMIC).  The language should also pass on the
2197
 * atomic flag to any nested invocations to CALL.
2198
 *
2199
 * The expression data structures and execution context that we create
2200
 * within this function are children of the portalContext of the Portal
2201
 * that the CALL utility statement runs in.  Therefore, any pass-by-ref
2202
 * values that we're passing to the procedure will survive transaction
2203
 * commits that might occur inside the procedure.
2204
 */
2205
void
2206
ExecuteCallStmt(CallStmt *stmt, ParamListInfo params, bool atomic, DestReceiver *dest)
2207
0
{
2208
0
  LOCAL_FCINFO(fcinfo, FUNC_MAX_ARGS);
2209
0
  ListCell   *lc;
2210
0
  FuncExpr   *fexpr;
2211
0
  int     nargs;
2212
0
  int     i;
2213
0
  AclResult aclresult;
2214
0
  FmgrInfo  flinfo;
2215
0
  CallContext *callcontext;
2216
0
  EState     *estate;
2217
0
  ExprContext *econtext;
2218
0
  HeapTuple tp;
2219
0
  PgStat_FunctionCallUsage fcusage;
2220
0
  Datum   retval;
2221
2222
0
  fexpr = stmt->funcexpr;
2223
0
  Assert(fexpr);
2224
0
  Assert(IsA(fexpr, FuncExpr));
2225
2226
0
  aclresult = object_aclcheck(ProcedureRelationId, fexpr->funcid, GetUserId(), ACL_EXECUTE);
2227
0
  if (aclresult != ACLCHECK_OK)
2228
0
    aclcheck_error(aclresult, OBJECT_PROCEDURE, get_func_name(fexpr->funcid));
2229
2230
  /* Prep the context object we'll pass to the procedure */
2231
0
  callcontext = makeNode(CallContext);
2232
0
  callcontext->atomic = atomic;
2233
2234
0
  tp = SearchSysCache1(PROCOID, ObjectIdGetDatum(fexpr->funcid));
2235
0
  if (!HeapTupleIsValid(tp))
2236
0
    elog(ERROR, "cache lookup failed for function %u", fexpr->funcid);
2237
2238
  /*
2239
   * If proconfig is set we can't allow transaction commands because of the
2240
   * way the GUC stacking works: The transaction boundary would have to pop
2241
   * the proconfig setting off the stack.  That restriction could be lifted
2242
   * by redesigning the GUC nesting mechanism a bit.
2243
   */
2244
0
  if (!heap_attisnull(tp, Anum_pg_proc_proconfig, NULL))
2245
0
    callcontext->atomic = true;
2246
2247
  /*
2248
   * In security definer procedures, we can't allow transaction commands.
2249
   * StartTransaction() insists that the security context stack is empty,
2250
   * and AbortTransaction() resets the security context.  This could be
2251
   * reorganized, but right now it doesn't work.
2252
   */
2253
0
  if (((Form_pg_proc) GETSTRUCT(tp))->prosecdef)
2254
0
    callcontext->atomic = true;
2255
2256
0
  ReleaseSysCache(tp);
2257
2258
  /* safety check; see ExecInitFunc() */
2259
0
  nargs = list_length(fexpr->args);
2260
0
  if (nargs > FUNC_MAX_ARGS)
2261
0
    ereport(ERROR,
2262
0
        (errcode(ERRCODE_TOO_MANY_ARGUMENTS),
2263
0
         errmsg_plural("cannot pass more than %d argument to a procedure",
2264
0
                 "cannot pass more than %d arguments to a procedure",
2265
0
                 FUNC_MAX_ARGS,
2266
0
                 FUNC_MAX_ARGS)));
2267
2268
  /* Initialize function call structure */
2269
0
  InvokeFunctionExecuteHook(fexpr->funcid);
2270
0
  fmgr_info(fexpr->funcid, &flinfo);
2271
0
  fmgr_info_set_expr((Node *) fexpr, &flinfo);
2272
0
  InitFunctionCallInfoData(*fcinfo, &flinfo, nargs, fexpr->inputcollid,
2273
0
               (Node *) callcontext, NULL);
2274
2275
  /*
2276
   * Evaluate procedure arguments inside a suitable execution context.  Note
2277
   * we can't free this context till the procedure returns.
2278
   */
2279
0
  estate = CreateExecutorState();
2280
0
  estate->es_param_list_info = params;
2281
0
  econtext = CreateExprContext(estate);
2282
2283
  /*
2284
   * If we're called in non-atomic context, we also have to ensure that the
2285
   * argument expressions run with an up-to-date snapshot.  Our caller will
2286
   * have provided a current snapshot in atomic contexts, but not in
2287
   * non-atomic contexts, because the possibility of a COMMIT/ROLLBACK
2288
   * destroying the snapshot makes higher-level management too complicated.
2289
   */
2290
0
  if (!atomic)
2291
0
    PushActiveSnapshot(GetTransactionSnapshot());
2292
2293
0
  i = 0;
2294
0
  foreach(lc, fexpr->args)
2295
0
  {
2296
0
    ExprState  *exprstate;
2297
0
    Datum   val;
2298
0
    bool    isnull;
2299
2300
0
    exprstate = ExecPrepareExpr(lfirst(lc), estate);
2301
2302
0
    val = ExecEvalExprSwitchContext(exprstate, econtext, &isnull);
2303
2304
0
    fcinfo->args[i].value = val;
2305
0
    fcinfo->args[i].isnull = isnull;
2306
2307
0
    i++;
2308
0
  }
2309
2310
  /* Get rid of temporary snapshot for arguments, if we made one */
2311
0
  if (!atomic)
2312
0
    PopActiveSnapshot();
2313
2314
  /* Here we actually call the procedure */
2315
0
  pgstat_init_function_usage(fcinfo, &fcusage);
2316
0
  retval = FunctionCallInvoke(fcinfo);
2317
0
  pgstat_end_function_usage(&fcusage, true);
2318
2319
  /* Handle the procedure's outputs */
2320
0
  if (fexpr->funcresulttype == VOIDOID)
2321
0
  {
2322
    /* do nothing */
2323
0
  }
2324
0
  else if (fexpr->funcresulttype == RECORDOID)
2325
0
  {
2326
    /* send tuple to client */
2327
0
    HeapTupleHeader td;
2328
0
    Oid     tupType;
2329
0
    int32   tupTypmod;
2330
0
    TupleDesc retdesc;
2331
0
    HeapTupleData rettupdata;
2332
0
    TupOutputState *tstate;
2333
0
    TupleTableSlot *slot;
2334
2335
0
    if (fcinfo->isnull)
2336
0
      elog(ERROR, "procedure returned null record");
2337
2338
    /*
2339
     * Ensure there's an active snapshot whilst we execute whatever's
2340
     * involved here.  Note that this is *not* sufficient to make the
2341
     * world safe for TOAST pointers to be included in the returned data:
2342
     * the referenced data could have gone away while we didn't hold a
2343
     * snapshot.  Hence, it's incumbent on PLs that can do COMMIT/ROLLBACK
2344
     * to not return TOAST pointers, unless those pointers were fetched
2345
     * after the last COMMIT/ROLLBACK in the procedure.
2346
     *
2347
     * XXX that is a really nasty, hard-to-test requirement.  Is there a
2348
     * way to remove it?
2349
     */
2350
0
    EnsurePortalSnapshotExists();
2351
2352
0
    td = DatumGetHeapTupleHeader(retval);
2353
0
    tupType = HeapTupleHeaderGetTypeId(td);
2354
0
    tupTypmod = HeapTupleHeaderGetTypMod(td);
2355
0
    retdesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
2356
2357
0
    tstate = begin_tup_output_tupdesc(dest, retdesc,
2358
0
                      &TTSOpsHeapTuple);
2359
2360
0
    rettupdata.t_len = HeapTupleHeaderGetDatumLength(td);
2361
0
    ItemPointerSetInvalid(&(rettupdata.t_self));
2362
0
    rettupdata.t_tableOid = InvalidOid;
2363
0
    rettupdata.t_data = td;
2364
2365
0
    slot = ExecStoreHeapTuple(&rettupdata, tstate->slot, false);
2366
0
    tstate->dest->receiveSlot(slot, tstate->dest);
2367
2368
0
    end_tup_output(tstate);
2369
2370
0
    ReleaseTupleDesc(retdesc);
2371
0
  }
2372
0
  else
2373
0
    elog(ERROR, "unexpected result type for procedure: %u",
2374
0
       fexpr->funcresulttype);
2375
2376
0
  FreeExecutorState(estate);
2377
0
}
2378
2379
/*
2380
 * Construct the tuple descriptor for a CALL statement return
2381
 */
2382
TupleDesc
2383
CallStmtResultDesc(CallStmt *stmt)
2384
0
{
2385
0
  FuncExpr   *fexpr;
2386
0
  HeapTuple tuple;
2387
0
  TupleDesc tupdesc;
2388
2389
0
  fexpr = stmt->funcexpr;
2390
2391
0
  tuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(fexpr->funcid));
2392
0
  if (!HeapTupleIsValid(tuple))
2393
0
    elog(ERROR, "cache lookup failed for procedure %u", fexpr->funcid);
2394
2395
0
  tupdesc = build_function_result_tupdesc_t(tuple);
2396
2397
0
  ReleaseSysCache(tuple);
2398
2399
  /*
2400
   * The result of build_function_result_tupdesc_t has the right column
2401
   * names, but it just has the declared output argument types, which is the
2402
   * wrong thing in polymorphic cases.  Get the correct types by examining
2403
   * stmt->outargs.  We intentionally keep the atttypmod as -1 and the
2404
   * attcollation as the type's default, since that's always the appropriate
2405
   * thing for function outputs; there's no point in considering any
2406
   * additional info available from outargs.  Note that tupdesc is null if
2407
   * there are no outargs.
2408
   */
2409
0
  if (tupdesc)
2410
0
  {
2411
0
    Assert(tupdesc->natts == list_length(stmt->outargs));
2412
0
    for (int i = 0; i < tupdesc->natts; i++)
2413
0
    {
2414
0
      Form_pg_attribute att = TupleDescAttr(tupdesc, i);
2415
0
      Node     *outarg = (Node *) list_nth(stmt->outargs, i);
2416
2417
0
      TupleDescInitEntry(tupdesc,
2418
0
                 i + 1,
2419
0
                 NameStr(att->attname),
2420
0
                 exprType(outarg),
2421
0
                 -1,
2422
0
                 0);
2423
0
    }
2424
0
  }
2425
2426
0
  return tupdesc;
2427
0
}