PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
functioncmds.c File Reference
#include "postgres.h"
#include "access/genam.h"
#include "access/heapam.h"
#include "access/htup_details.h"
#include "access/sysattr.h"
#include "catalog/dependency.h"
#include "catalog/indexing.h"
#include "catalog/objectaccess.h"
#include "catalog/pg_aggregate.h"
#include "catalog/pg_cast.h"
#include "catalog/pg_language.h"
#include "catalog/pg_namespace.h"
#include "catalog/pg_proc.h"
#include "catalog/pg_proc_fn.h"
#include "catalog/pg_transform.h"
#include "catalog/pg_type.h"
#include "catalog/pg_type_fn.h"
#include "commands/alter.h"
#include "commands/defrem.h"
#include "commands/proclang.h"
#include "miscadmin.h"
#include "optimizer/var.h"
#include "parser/parse_coerce.h"
#include "parser/parse_collate.h"
#include "parser/parse_expr.h"
#include "parser/parse_func.h"
#include "parser/parse_type.h"
#include "utils/acl.h"
#include "utils/builtins.h"
#include "utils/fmgroids.h"
#include "utils/guc.h"
#include "utils/lsyscache.h"
#include "utils/rel.h"
#include "utils/syscache.h"
#include "utils/tqual.h"
Include dependency graph for functioncmds.c:

Go to the source code of this file.

Functions

static void compute_return_type (TypeName *returnType, Oid languageOid, Oid *prorettype_p, bool *returnsSet_p)
 
void interpret_function_parameter_list (ParseState *pstate, List *parameters, Oid languageOid, bool is_aggregate, oidvector **parameterTypes, ArrayType **allParameterTypes, ArrayType **parameterModes, ArrayType **parameterNames, List **parameterDefaults, Oid *variadicArgType, Oid *requiredResultType)
 
static bool compute_common_attribute (ParseState *pstate, DefElem *defel, DefElem **volatility_item, DefElem **strict_item, DefElem **security_item, DefElem **leakproof_item, List **set_items, DefElem **cost_item, DefElem **rows_item, DefElem **parallel_item)
 
static char interpret_func_volatility (DefElem *defel)
 
static char interpret_func_parallel (DefElem *defel)
 
static ArrayTypeupdate_proconfig_value (ArrayType *a, List *set_items)
 
static void compute_attributes_sql_style (ParseState *pstate, List *options, List **as, char **language, Node **transform, bool *windowfunc_p, char *volatility_p, bool *strict_p, bool *security_definer, bool *leakproof_p, ArrayType **proconfig, float4 *procost, float4 *prorows, char *parallel_p)
 
static void compute_attributes_with_style (ParseState *pstate, List *parameters, bool *isStrict_p, char *volatility_p)
 
static void interpret_AS_clause (Oid languageOid, const char *languageName, char *funcname, List *as, char **prosrc_str_p, char **probin_str_p)
 
ObjectAddress CreateFunction (ParseState *pstate, CreateFunctionStmt *stmt)
 
void RemoveFunctionById (Oid funcOid)
 
ObjectAddress AlterFunction (ParseState *pstate, AlterFunctionStmt *stmt)
 
void SetFunctionReturnType (Oid funcOid, Oid newRetType)
 
void SetFunctionArgType (Oid funcOid, int argIndex, Oid newArgType)
 
ObjectAddress CreateCast (CreateCastStmt *stmt)
 
Oid get_cast_oid (Oid sourcetypeid, Oid targettypeid, bool missing_ok)
 
void DropCastById (Oid castOid)
 
static void check_transform_function (Form_pg_proc procstruct)
 
ObjectAddress CreateTransform (CreateTransformStmt *stmt)
 
Oid get_transform_oid (Oid type_id, Oid lang_id, bool missing_ok)
 
void DropTransformById (Oid transformOid)
 
void IsThereFunctionInNamespace (const char *proname, int pronargs, oidvector *proargtypes, Oid nspOid)
 
void ExecuteDoStmt (DoStmt *stmt)
 

Function Documentation

ObjectAddress AlterFunction ( ParseState pstate,
AlterFunctionStmt stmt 
)

Definition at line 1165 of file functioncmds.c.

References ACL_KIND_PROC, aclcheck_error(), ACLCHECK_NOT_OWNER, AlterFunctionStmt::actions, Anum_pg_proc_proconfig, DefElem::arg, CatalogTupleUpdate(), compute_common_attribute(), DatumGetArrayTypeP, defGetNumeric(), DefElem::defname, elog, ereport, errcode(), errmsg(), ERROR, AlterFunctionStmt::func, GETSTRUCT, GetUserId(), heap_close, heap_freetuple(), heap_modify_tuple(), heap_open(), HeapTupleIsValid, interpret_func_parallel(), interpret_func_volatility(), intVal, InvokeObjectPostAlterHook, lfirst, LookupFuncWithArgs(), NameListToString(), Natts_pg_proc, NIL, NoLock, NULL, ObjectAddressSet, ObjectIdGetDatum, ObjectWithArgs::objname, pg_proc_ownercheck(), PointerGetDatum, ProcedureRelationId, PROCOID, RelationGetDescr, RowExclusiveLock, SearchSysCacheCopy1, superuser(), SysCacheGetAttr(), HeapTupleData::t_self, and update_proconfig_value().

Referenced by ProcessUtilitySlow().

1166 {
1167  HeapTuple tup;
1168  Oid funcOid;
1169  Form_pg_proc procForm;
1170  Relation rel;
1171  ListCell *l;
1172  DefElem *volatility_item = NULL;
1173  DefElem *strict_item = NULL;
1174  DefElem *security_def_item = NULL;
1175  DefElem *leakproof_item = NULL;
1176  List *set_items = NIL;
1177  DefElem *cost_item = NULL;
1178  DefElem *rows_item = NULL;
1179  DefElem *parallel_item = NULL;
1180  ObjectAddress address;
1181 
1183 
1184  funcOid = LookupFuncWithArgs(stmt->func, false);
1185 
1186  tup = SearchSysCacheCopy1(PROCOID, ObjectIdGetDatum(funcOid));
1187  if (!HeapTupleIsValid(tup)) /* should not happen */
1188  elog(ERROR, "cache lookup failed for function %u", funcOid);
1189 
1190  procForm = (Form_pg_proc) GETSTRUCT(tup);
1191 
1192  /* Permission check: must own function */
1193  if (!pg_proc_ownercheck(funcOid, GetUserId()))
1195  NameListToString(stmt->func->objname));
1196 
1197  if (procForm->proisagg)
1198  ereport(ERROR,
1199  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1200  errmsg("\"%s\" is an aggregate function",
1201  NameListToString(stmt->func->objname))));
1202 
1203  /* Examine requested actions. */
1204  foreach(l, stmt->actions)
1205  {
1206  DefElem *defel = (DefElem *) lfirst(l);
1207 
1208  if (compute_common_attribute(pstate,
1209  defel,
1210  &volatility_item,
1211  &strict_item,
1212  &security_def_item,
1213  &leakproof_item,
1214  &set_items,
1215  &cost_item,
1216  &rows_item,
1217  &parallel_item) == false)
1218  elog(ERROR, "option \"%s\" not recognized", defel->defname);
1219  }
1220 
1221  if (volatility_item)
1222  procForm->provolatile = interpret_func_volatility(volatility_item);
1223  if (strict_item)
1224  procForm->proisstrict = intVal(strict_item->arg);
1225  if (security_def_item)
1226  procForm->prosecdef = intVal(security_def_item->arg);
1227  if (leakproof_item)
1228  {
1229  procForm->proleakproof = intVal(leakproof_item->arg);
1230  if (procForm->proleakproof && !superuser())
1231  ereport(ERROR,
1232  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
1233  errmsg("only superuser can define a leakproof function")));
1234  }
1235  if (cost_item)
1236  {
1237  procForm->procost = defGetNumeric(cost_item);
1238  if (procForm->procost <= 0)
1239  ereport(ERROR,
1240  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1241  errmsg("COST must be positive")));
1242  }
1243  if (rows_item)
1244  {
1245  procForm->prorows = defGetNumeric(rows_item);
1246  if (procForm->prorows <= 0)
1247  ereport(ERROR,
1248  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1249  errmsg("ROWS must be positive")));
1250  if (!procForm->proretset)
1251  ereport(ERROR,
1252  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1253  errmsg("ROWS is not applicable when function does not return a set")));
1254  }
1255  if (set_items)
1256  {
1257  Datum datum;
1258  bool isnull;
1259  ArrayType *a;
1260  Datum repl_val[Natts_pg_proc];
1261  bool repl_null[Natts_pg_proc];
1262  bool repl_repl[Natts_pg_proc];
1263 
1264  /* extract existing proconfig setting */
1265  datum = SysCacheGetAttr(PROCOID, tup, Anum_pg_proc_proconfig, &isnull);
1266  a = isnull ? NULL : DatumGetArrayTypeP(datum);
1267 
1268  /* update according to each SET or RESET item, left to right */
1269  a = update_proconfig_value(a, set_items);
1270 
1271  /* update the tuple */
1272  memset(repl_repl, false, sizeof(repl_repl));
1273  repl_repl[Anum_pg_proc_proconfig - 1] = true;
1274 
1275  if (a == NULL)
1276  {
1277  repl_val[Anum_pg_proc_proconfig - 1] = (Datum) 0;
1278  repl_null[Anum_pg_proc_proconfig - 1] = true;
1279  }
1280  else
1281  {
1282  repl_val[Anum_pg_proc_proconfig - 1] = PointerGetDatum(a);
1283  repl_null[Anum_pg_proc_proconfig - 1] = false;
1284  }
1285 
1286  tup = heap_modify_tuple(tup, RelationGetDescr(rel),
1287  repl_val, repl_null, repl_repl);
1288  }
1289  if (parallel_item)
1290  procForm->proparallel = interpret_func_parallel(parallel_item);
1291 
1292  /* Do the update */
1293  CatalogTupleUpdate(rel, &tup->t_self, tup);
1294 
1296 
1297  ObjectAddressSet(address, ProcedureRelationId, funcOid);
1298 
1299  heap_close(rel, NoLock);
1300  heap_freetuple(tup);
1301 
1302  return address;
1303 }
#define NIL
Definition: pg_list.h:69
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
#define RelationGetDescr(relation)
Definition: rel.h:429
Oid GetUserId(void)
Definition: miscinit.c:283
#define PointerGetDatum(X)
Definition: postgres.h:562
#define ProcedureRelationId
Definition: pg_proc.h:33
double defGetNumeric(DefElem *def)
Definition: define.c:85
int errcode(int sqlerrcode)
Definition: elog.c:575
bool superuser(void)
Definition: superuser.c:47
#define heap_close(r, l)
Definition: heapam.h:97
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1374
unsigned int Oid
Definition: postgres_ext.h:31
#define Anum_pg_proc_proconfig
Definition: pg_proc.h:117
#define Natts_pg_proc
Definition: pg_proc.h:89
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
ItemPointerData t_self
Definition: htup.h:65
#define NoLock
Definition: lockdefs.h:34
void aclcheck_error(AclResult aclerr, AclObjectKind objectkind, const char *objectname)
Definition: aclchk.c:3382
#define RowExclusiveLock
Definition: lockdefs.h:38
#define ereport(elevel, rest)
Definition: elog.h:122
#define InvokeObjectPostAlterHook(classId, objectId, subId)
Definition: objectaccess.h:163
Oid LookupFuncWithArgs(ObjectWithArgs *func, bool noError)
Definition: parse_func.c:1971
static char interpret_func_volatility(DefElem *defel)
Definition: functioncmds.c:532
Node * arg
Definition: parsenodes.h:709
char * NameListToString(List *names)
Definition: namespace.c:2953
uintptr_t Datum
Definition: postgres.h:372
static ArrayType * update_proconfig_value(ArrayType *a, List *set_items)
Definition: functioncmds.c:575
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1278
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1287
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:83
ObjectWithArgs * func
Definition: parsenodes.h:2711
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define NULL
Definition: c.h:229
#define lfirst(lc)
Definition: pg_list.h:106
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
Definition: indexing.c:210
#define ObjectAddressSet(addr, class_id, object_id)
Definition: objectaddress.h:40
#define SearchSysCacheCopy1(cacheId, key1)
Definition: syscache.h:161
#define intVal(v)
Definition: value.h:52
int errmsg(const char *fmt,...)
Definition: elog.c:797
char * defname
Definition: parsenodes.h:708
#define elog
Definition: elog.h:219
static bool compute_common_attribute(ParseState *pstate, DefElem *defel, DefElem **volatility_item, DefElem **strict_item, DefElem **security_item, DefElem **leakproof_item, List **set_items, DefElem **cost_item, DefElem **rows_item, DefElem **parallel_item)
Definition: functioncmds.c:453
HeapTuple heap_modify_tuple(HeapTuple tuple, TupleDesc tupleDesc, Datum *replValues, bool *replIsnull, bool *doReplace)
Definition: heaptuple.c:793
Definition: pg_list.h:45
bool pg_proc_ownercheck(Oid proc_oid, Oid roleid)
Definition: aclchk.c:4607
static char interpret_func_parallel(DefElem *defel)
Definition: functioncmds.c:550
#define DatumGetArrayTypeP(X)
Definition: array.h:242
static void check_transform_function ( Form_pg_proc  procstruct)
static

Definition at line 1742 of file functioncmds.c.

References ereport, errcode(), errmsg(), ERROR, INTERNALOID, and PROVOLATILE_VOLATILE.

Referenced by CreateTransform().

1743 {
1744  if (procstruct->provolatile == PROVOLATILE_VOLATILE)
1745  ereport(ERROR,
1746  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1747  errmsg("transform function must not be volatile")));
1748  if (procstruct->proisagg)
1749  ereport(ERROR,
1750  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1751  errmsg("transform function must not be an aggregate function")));
1752  if (procstruct->proiswindow)
1753  ereport(ERROR,
1754  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1755  errmsg("transform function must not be a window function")));
1756  if (procstruct->proretset)
1757  ereport(ERROR,
1758  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1759  errmsg("transform function must not return a set")));
1760  if (procstruct->pronargs != 1)
1761  ereport(ERROR,
1762  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1763  errmsg("transform function must take one argument")));
1764  if (procstruct->proargtypes.values[0] != INTERNALOID)
1765  ereport(ERROR,
1766  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1767  errmsg("first argument of transform function must be type %s",
1768  "internal")));
1769 }
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
#define PROVOLATILE_VOLATILE
Definition: pg_proc.h:5436
#define INTERNALOID
Definition: pg_type.h:694
int errmsg(const char *fmt,...)
Definition: elog.c:797
static void compute_attributes_sql_style ( ParseState pstate,
List options,
List **  as,
char **  language,
Node **  transform,
bool windowfunc_p,
char *  volatility_p,
bool strict_p,
bool security_definer,
bool leakproof_p,
ArrayType **  proconfig,
float4 procost,
float4 prorows,
char *  parallel_p 
)
static

Definition at line 605 of file functioncmds.c.

References DefElem::arg, compute_common_attribute(), DefElem::defname, elog, ereport, errcode(), errmsg(), ERROR, lfirst, DefElem::location, NIL, NULL, and parser_errposition().

Referenced by CreateFunction().

619 {
620  ListCell *option;
621  DefElem *as_item = NULL;
622  DefElem *language_item = NULL;
623  DefElem *transform_item = NULL;
624  DefElem *windowfunc_item = NULL;
625  DefElem *volatility_item = NULL;
626  DefElem *strict_item = NULL;
627  DefElem *security_item = NULL;
628  DefElem *leakproof_item = NULL;
629  List *set_items = NIL;
630  DefElem *cost_item = NULL;
631  DefElem *rows_item = NULL;
632  DefElem *parallel_item = NULL;
633 
634  foreach(option, options)
635  {
636  DefElem *defel = (DefElem *) lfirst(option);
637 
638  if (strcmp(defel->defname, "as") == 0)
639  {
640  if (as_item)
641  ereport(ERROR,
642  (errcode(ERRCODE_SYNTAX_ERROR),
643  errmsg("conflicting or redundant options"),
644  parser_errposition(pstate, defel->location)));
645  as_item = defel;
646  }
647  else if (strcmp(defel->defname, "language") == 0)
648  {
649  if (language_item)
650  ereport(ERROR,
651  (errcode(ERRCODE_SYNTAX_ERROR),
652  errmsg("conflicting or redundant options"),
653  parser_errposition(pstate, defel->location)));
654  language_item = defel;
655  }
656  else if (strcmp(defel->defname, "transform") == 0)
657  {
658  if (transform_item)
659  ereport(ERROR,
660  (errcode(ERRCODE_SYNTAX_ERROR),
661  errmsg("conflicting or redundant options"),
662  parser_errposition(pstate, defel->location)));
663  transform_item = defel;
664  }
665  else if (strcmp(defel->defname, "window") == 0)
666  {
667  if (windowfunc_item)
668  ereport(ERROR,
669  (errcode(ERRCODE_SYNTAX_ERROR),
670  errmsg("conflicting or redundant options"),
671  parser_errposition(pstate, defel->location)));
672  windowfunc_item = defel;
673  }
674  else if (compute_common_attribute(pstate,
675  defel,
676  &volatility_item,
677  &strict_item,
678  &security_item,
679  &leakproof_item,
680  &set_items,
681  &cost_item,
682  &rows_item,
683  &parallel_item))
684  {
685  /* recognized common option */
686  continue;
687  }
688  else
689  elog(ERROR, "option \"%s\" not recognized",
690  defel->defname);
691  }
692 
693  /* process required items */
694  if (as_item)
695  *as = (List *) as_item->arg;
696  else
697  {
698  ereport(ERROR,
699  (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
700  errmsg("no function body specified")));
701  *as = NIL; /* keep compiler quiet */
702  }
703 
704  if (language_item)
705  *language = strVal(language_item->arg);
706  else
707  {
708  ereport(ERROR,
709  (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
710  errmsg("no language specified")));
711  *language = NULL; /* keep compiler quiet */
712  }
713 
714  /* process optional items */
715  if (transform_item)
716  *transform = transform_item->arg;
717  if (windowfunc_item)
718  *windowfunc_p = intVal(windowfunc_item->arg);
719  if (volatility_item)
720  *volatility_p = interpret_func_volatility(volatility_item);
721  if (strict_item)
722  *strict_p = intVal(strict_item->arg);
723  if (security_item)
724  *security_definer = intVal(security_item->arg);
725  if (leakproof_item)
726  *leakproof_p = intVal(leakproof_item->arg);
727  if (set_items)
728  *proconfig = update_proconfig_value(NULL, set_items);
729  if (cost_item)
730  {
731  *procost = defGetNumeric(cost_item);
732  if (*procost <= 0)
733  ereport(ERROR,
734  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
735  errmsg("COST must be positive")));
736  }
737  if (rows_item)
738  {
739  *prorows = defGetNumeric(rows_item);
740  if (*prorows <= 0)
741  ereport(ERROR,
742  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
743  errmsg("ROWS must be positive")));
744  }
745  if (parallel_item)
746  *parallel_p = interpret_func_parallel(parallel_item);
747 }
#define NIL
Definition: pg_list.h:69
double defGetNumeric(DefElem *def)
Definition: define.c:85
#define strVal(v)
Definition: value.h:54
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
int location
Definition: parsenodes.h:711
#define ereport(elevel, rest)
Definition: elog.h:122
static char interpret_func_volatility(DefElem *defel)
Definition: functioncmds.c:532
Node * arg
Definition: parsenodes.h:709
static ArrayType * update_proconfig_value(ArrayType *a, List *set_items)
Definition: functioncmds.c:575
#define NULL
Definition: c.h:229
#define lfirst(lc)
Definition: pg_list.h:106
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:109
#define intVal(v)
Definition: value.h:52
int errmsg(const char *fmt,...)
Definition: elog.c:797
char * defname
Definition: parsenodes.h:708
#define elog
Definition: elog.h:219
static bool compute_common_attribute(ParseState *pstate, DefElem *defel, DefElem **volatility_item, DefElem **strict_item, DefElem **security_item, DefElem **leakproof_item, List **set_items, DefElem **cost_item, DefElem **rows_item, DefElem **parallel_item)
Definition: functioncmds.c:453
Definition: pg_list.h:45
static char interpret_func_parallel(DefElem *defel)
Definition: functioncmds.c:550
static void compute_attributes_with_style ( ParseState pstate,
List parameters,
bool isStrict_p,
char *  volatility_p 
)
static

Definition at line 765 of file functioncmds.c.

References defGetBoolean(), DefElem::defname, ereport, errcode(), errmsg(), lfirst, DefElem::location, parser_errposition(), pg_strcasecmp(), PROVOLATILE_IMMUTABLE, and WARNING.

Referenced by CreateFunction().

766 {
767  ListCell *pl;
768 
769  foreach(pl, parameters)
770  {
771  DefElem *param = (DefElem *) lfirst(pl);
772 
773  if (pg_strcasecmp(param->defname, "isstrict") == 0)
774  *isStrict_p = defGetBoolean(param);
775  else if (pg_strcasecmp(param->defname, "iscachable") == 0)
776  {
777  /* obsolete spelling of isImmutable */
778  if (defGetBoolean(param))
779  *volatility_p = PROVOLATILE_IMMUTABLE;
780  }
781  else
783  (errcode(ERRCODE_SYNTAX_ERROR),
784  errmsg("unrecognized function attribute \"%s\" ignored",
785  param->defname),
786  parser_errposition(pstate, param->location)));
787  }
788 }
#define PROVOLATILE_IMMUTABLE
Definition: pg_proc.h:5434
int errcode(int sqlerrcode)
Definition: elog.c:575
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
bool defGetBoolean(DefElem *def)
Definition: define.c:111
int location
Definition: parsenodes.h:711
#define ereport(elevel, rest)
Definition: elog.h:122
#define WARNING
Definition: elog.h:40
#define lfirst(lc)
Definition: pg_list.h:106
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:109
int errmsg(const char *fmt,...)
Definition: elog.c:797
char * defname
Definition: parsenodes.h:708
static bool compute_common_attribute ( ParseState pstate,
DefElem defel,
DefElem **  volatility_item,
DefElem **  strict_item,
DefElem **  security_item,
DefElem **  leakproof_item,
List **  set_items,
DefElem **  cost_item,
DefElem **  rows_item,
DefElem **  parallel_item 
)
static

Definition at line 453 of file functioncmds.c.

References DefElem::arg, DefElem::defname, ereport, errcode(), errmsg(), ERROR, lappend(), DefElem::location, and parser_errposition().

Referenced by AlterFunction(), and compute_attributes_sql_style().

463 {
464  if (strcmp(defel->defname, "volatility") == 0)
465  {
466  if (*volatility_item)
467  goto duplicate_error;
468 
469  *volatility_item = defel;
470  }
471  else if (strcmp(defel->defname, "strict") == 0)
472  {
473  if (*strict_item)
474  goto duplicate_error;
475 
476  *strict_item = defel;
477  }
478  else if (strcmp(defel->defname, "security") == 0)
479  {
480  if (*security_item)
481  goto duplicate_error;
482 
483  *security_item = defel;
484  }
485  else if (strcmp(defel->defname, "leakproof") == 0)
486  {
487  if (*leakproof_item)
488  goto duplicate_error;
489 
490  *leakproof_item = defel;
491  }
492  else if (strcmp(defel->defname, "set") == 0)
493  {
494  *set_items = lappend(*set_items, defel->arg);
495  }
496  else if (strcmp(defel->defname, "cost") == 0)
497  {
498  if (*cost_item)
499  goto duplicate_error;
500 
501  *cost_item = defel;
502  }
503  else if (strcmp(defel->defname, "rows") == 0)
504  {
505  if (*rows_item)
506  goto duplicate_error;
507 
508  *rows_item = defel;
509  }
510  else if (strcmp(defel->defname, "parallel") == 0)
511  {
512  if (*parallel_item)
513  goto duplicate_error;
514 
515  *parallel_item = defel;
516  }
517  else
518  return false;
519 
520  /* Recognized an option */
521  return true;
522 
523 duplicate_error:
524  ereport(ERROR,
525  (errcode(ERRCODE_SYNTAX_ERROR),
526  errmsg("conflicting or redundant options"),
527  parser_errposition(pstate, defel->location)));
528  return false; /* keep compiler quiet */
529 }
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
int location
Definition: parsenodes.h:711
#define ereport(elevel, rest)
Definition: elog.h:122
Node * arg
Definition: parsenodes.h:709
List * lappend(List *list, void *datum)
Definition: list.c:128
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:109
int errmsg(const char *fmt,...)
Definition: elog.c:797
char * defname
Definition: parsenodes.h:708
static void compute_return_type ( TypeName returnType,
Oid  languageOid,
Oid prorettype_p,
bool returnsSet_p 
)
static

Definition at line 83 of file functioncmds.c.

References ACL_CREATE, ACL_KIND_NAMESPACE, ACL_USAGE, aclcheck_error(), aclcheck_error_type(), ACLCHECK_OK, Assert, ClanguageId, ereport, errcode(), errdetail(), errmsg(), ERROR, get_namespace_name(), GETSTRUCT, GetUserId(), INTERNALlanguageId, LookupTypeName(), TypeName::names, NIL, NOTICE, NULL, ObjectAddress::objectId, OidIsValid, pg_namespace_aclcheck(), pg_type_aclcheck(), QualifiedNameGetCreationNamespace(), ReleaseSysCache(), TypeName::setof, SQLlanguageId, TypeNameToString(), TypeShellMake(), typeTypeId(), and TypeName::typmods.

Referenced by CreateFunction().

85 {
86  Oid rettype;
87  Type typtup;
88  AclResult aclresult;
89 
90  typtup = LookupTypeName(NULL, returnType, NULL, false);
91 
92  if (typtup)
93  {
94  if (!((Form_pg_type) GETSTRUCT(typtup))->typisdefined)
95  {
96  if (languageOid == SQLlanguageId)
97  ereport(ERROR,
98  (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
99  errmsg("SQL function cannot return shell type %s",
100  TypeNameToString(returnType))));
101  else
102  ereport(NOTICE,
103  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
104  errmsg("return type %s is only a shell",
105  TypeNameToString(returnType))));
106  }
107  rettype = typeTypeId(typtup);
108  ReleaseSysCache(typtup);
109  }
110  else
111  {
112  char *typnam = TypeNameToString(returnType);
113  Oid namespaceId;
114  AclResult aclresult;
115  char *typname;
116  ObjectAddress address;
117 
118  /*
119  * Only C-coded functions can be I/O functions. We enforce this
120  * restriction here mainly to prevent littering the catalogs with
121  * shell types due to simple typos in user-defined function
122  * definitions.
123  */
124  if (languageOid != INTERNALlanguageId &&
125  languageOid != ClanguageId)
126  ereport(ERROR,
127  (errcode(ERRCODE_UNDEFINED_OBJECT),
128  errmsg("type \"%s\" does not exist", typnam)));
129 
130  /* Reject if there's typmod decoration, too */
131  if (returnType->typmods != NIL)
132  ereport(ERROR,
133  (errcode(ERRCODE_SYNTAX_ERROR),
134  errmsg("type modifier cannot be specified for shell type \"%s\"",
135  typnam)));
136 
137  /* Otherwise, go ahead and make a shell type */
138  ereport(NOTICE,
139  (errcode(ERRCODE_UNDEFINED_OBJECT),
140  errmsg("type \"%s\" is not yet defined", typnam),
141  errdetail("Creating a shell type definition.")));
142  namespaceId = QualifiedNameGetCreationNamespace(returnType->names,
143  &typname);
144  aclresult = pg_namespace_aclcheck(namespaceId, GetUserId(),
145  ACL_CREATE);
146  if (aclresult != ACLCHECK_OK)
148  get_namespace_name(namespaceId));
149  address = TypeShellMake(typname, namespaceId, GetUserId());
150  rettype = address.objectId;
151  Assert(OidIsValid(rettype));
152  }
153 
154  aclresult = pg_type_aclcheck(rettype, GetUserId(), ACL_USAGE);
155  if (aclresult != ACLCHECK_OK)
156  aclcheck_error_type(aclresult, rettype);
157 
158  *prorettype_p = rettype;
159  *returnsSet_p = returnType->setof;
160 }
Type LookupTypeName(ParseState *pstate, const TypeName *typeName, int32 *typmod_p, bool missing_ok)
Definition: parse_type.c:57
#define NIL
Definition: pg_list.h:69
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
List * names
Definition: parsenodes.h:200
#define INTERNALlanguageId
Definition: pg_language.h:74
Oid GetUserId(void)
Definition: miscinit.c:283
Oid QualifiedNameGetCreationNamespace(List *names, char **objname_p)
Definition: namespace.c:2846
char * TypeNameToString(const TypeName *typeName)
Definition: parse_type.c:459
#define ClanguageId
Definition: pg_language.h:77
int errcode(int sqlerrcode)
Definition: elog.c:575
FormData_pg_type * Form_pg_type
Definition: pg_type.h:233
unsigned int Oid
Definition: postgres_ext.h:31
ObjectAddress TypeShellMake(const char *typeName, Oid typeNamespace, Oid ownerId)
Definition: pg_type.c:56
#define OidIsValid(objectId)
Definition: c.h:538
AclResult pg_namespace_aclcheck(Oid nsp_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:4467
void aclcheck_error_type(AclResult aclerr, Oid typeOid)
Definition: aclchk.c:3440
#define ERROR
Definition: elog.h:43
#define ACL_CREATE
Definition: parsenodes.h:75
#define SQLlanguageId
Definition: pg_language.h:80
bool setof
Definition: parsenodes.h:202
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3006
void aclcheck_error(AclResult aclerr, AclObjectKind objectkind, const char *objectname)
Definition: aclchk.c:3382
int errdetail(const char *fmt,...)
Definition: elog.c:873
#define ACL_USAGE
Definition: parsenodes.h:73
#define ereport(elevel, rest)
Definition: elog.h:122
AclResult
Definition: acl.h:170
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1116
List * typmods
Definition: parsenodes.h:204
#define NOTICE
Definition: elog.h:37
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
int errmsg(const char *fmt,...)
Definition: elog.c:797
AclResult pg_type_aclcheck(Oid type_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:4517
Oid typeTypeId(Type tp)
Definition: parse_type.c:571
ObjectAddress CreateCast ( CreateCastStmt stmt)

Definition at line 1377 of file functioncmds.c.

References ACL_USAGE, aclcheck_error_type(), ACLCHECK_OK, Anum_pg_cast_castcontext, Anum_pg_cast_castfunc, Anum_pg_cast_castmethod, Anum_pg_cast_castsource, Anum_pg_cast_casttarget, BOOLOID, CastRelationId, CASTSOURCETARGET, CatalogTupleInsert(), CharGetDatum, ObjectAddress::classId, COERCION_ASSIGNMENT, COERCION_CODE_ASSIGNMENT, COERCION_CODE_EXPLICIT, COERCION_CODE_IMPLICIT, COERCION_EXPLICIT, COERCION_IMPLICIT, COERCION_METHOD_BINARY, COERCION_METHOD_FUNCTION, COERCION_METHOD_INOUT, CreateCastStmt::context, DEPENDENCY_NORMAL, elog, ereport, errcode(), ERRCODE_DUPLICATE_OBJECT, errmsg(), ERROR, format_type_be(), CreateCastStmt::func, get_element_type(), get_typlenbyvalalign(), get_typtype(), GETSTRUCT, GetUserId(), heap_close, heap_form_tuple(), heap_freetuple(), heap_open(), HeapTupleIsValid, CreateCastStmt::inout, INT4OID, InvalidOid, InvokeObjectPostCreateHook, IsBinaryCoercible(), LookupFuncWithArgs(), MemSet, Natts_pg_cast, NULL, ObjectAddress::objectId, ObjectIdGetDatum, ObjectAddress::objectSubId, OidIsValid, pg_type_aclcheck(), pg_type_ownercheck(), ProcedureRelationId, PROCOID, PROVOLATILE_VOLATILE, recordDependencyOn(), recordDependencyOnCurrentExtension(), RelationGetDescr, ReleaseSysCache(), RowExclusiveLock, SearchSysCache1, SearchSysCache2, CreateCastStmt::sourcetype, superuser(), CreateCastStmt::targettype, TypeNameToString(), typenameTypeId(), TypeRelationId, TYPTYPE_COMPOSITE, TYPTYPE_DOMAIN, TYPTYPE_ENUM, TYPTYPE_PSEUDO, values, and WARNING.

Referenced by ProcessUtilitySlow().

1378 {
1379  Oid sourcetypeid;
1380  Oid targettypeid;
1381  char sourcetyptype;
1382  char targettyptype;
1383  Oid funcid;
1384  Oid castid;
1385  int nargs;
1386  char castcontext;
1387  char castmethod;
1388  Relation relation;
1389  HeapTuple tuple;
1391  bool nulls[Natts_pg_cast];
1392  ObjectAddress myself,
1393  referenced;
1394  AclResult aclresult;
1395 
1396  sourcetypeid = typenameTypeId(NULL, stmt->sourcetype);
1397  targettypeid = typenameTypeId(NULL, stmt->targettype);
1398  sourcetyptype = get_typtype(sourcetypeid);
1399  targettyptype = get_typtype(targettypeid);
1400 
1401  /* No pseudo-types allowed */
1402  if (sourcetyptype == TYPTYPE_PSEUDO)
1403  ereport(ERROR,
1404  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1405  errmsg("source data type %s is a pseudo-type",
1406  TypeNameToString(stmt->sourcetype))));
1407 
1408  if (targettyptype == TYPTYPE_PSEUDO)
1409  ereport(ERROR,
1410  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1411  errmsg("target data type %s is a pseudo-type",
1412  TypeNameToString(stmt->targettype))));
1413 
1414  /* Permission check */
1415  if (!pg_type_ownercheck(sourcetypeid, GetUserId())
1416  && !pg_type_ownercheck(targettypeid, GetUserId()))
1417  ereport(ERROR,
1418  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
1419  errmsg("must be owner of type %s or type %s",
1420  format_type_be(sourcetypeid),
1421  format_type_be(targettypeid))));
1422 
1423  aclresult = pg_type_aclcheck(sourcetypeid, GetUserId(), ACL_USAGE);
1424  if (aclresult != ACLCHECK_OK)
1425  aclcheck_error_type(aclresult, sourcetypeid);
1426 
1427  aclresult = pg_type_aclcheck(targettypeid, GetUserId(), ACL_USAGE);
1428  if (aclresult != ACLCHECK_OK)
1429  aclcheck_error_type(aclresult, targettypeid);
1430 
1431  /* Domains are allowed for historical reasons, but we warn */
1432  if (sourcetyptype == TYPTYPE_DOMAIN)
1433  ereport(WARNING,
1434  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1435  errmsg("cast will be ignored because the source data type is a domain")));
1436 
1437  else if (targettyptype == TYPTYPE_DOMAIN)
1438  ereport(WARNING,
1439  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1440  errmsg("cast will be ignored because the target data type is a domain")));
1441 
1442  /* Determine the cast method */
1443  if (stmt->func != NULL)
1444  castmethod = COERCION_METHOD_FUNCTION;
1445  else if (stmt->inout)
1446  castmethod = COERCION_METHOD_INOUT;
1447  else
1448  castmethod = COERCION_METHOD_BINARY;
1449 
1450  if (castmethod == COERCION_METHOD_FUNCTION)
1451  {
1452  Form_pg_proc procstruct;
1453 
1454  funcid = LookupFuncWithArgs(stmt->func, false);
1455 
1456  tuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
1457  if (!HeapTupleIsValid(tuple))
1458  elog(ERROR, "cache lookup failed for function %u", funcid);
1459 
1460  procstruct = (Form_pg_proc) GETSTRUCT(tuple);
1461  nargs = procstruct->pronargs;
1462  if (nargs < 1 || nargs > 3)
1463  ereport(ERROR,
1464  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1465  errmsg("cast function must take one to three arguments")));
1466  if (!IsBinaryCoercible(sourcetypeid, procstruct->proargtypes.values[0]))
1467  ereport(ERROR,
1468  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1469  errmsg("argument of cast function must match or be binary-coercible from source data type")));
1470  if (nargs > 1 && procstruct->proargtypes.values[1] != INT4OID)
1471  ereport(ERROR,
1472  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1473  errmsg("second argument of cast function must be type %s",
1474  "integer")));
1475  if (nargs > 2 && procstruct->proargtypes.values[2] != BOOLOID)
1476  ereport(ERROR,
1477  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1478  errmsg("third argument of cast function must be type %s",
1479  "boolean")));
1480  if (!IsBinaryCoercible(procstruct->prorettype, targettypeid))
1481  ereport(ERROR,
1482  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1483  errmsg("return data type of cast function must match or be binary-coercible to target data type")));
1484 
1485  /*
1486  * Restricting the volatility of a cast function may or may not be a
1487  * good idea in the abstract, but it definitely breaks many old
1488  * user-defined types. Disable this check --- tgl 2/1/03
1489  */
1490 #ifdef NOT_USED
1491  if (procstruct->provolatile == PROVOLATILE_VOLATILE)
1492  ereport(ERROR,
1493  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1494  errmsg("cast function must not be volatile")));
1495 #endif
1496  if (procstruct->proisagg)
1497  ereport(ERROR,
1498  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1499  errmsg("cast function must not be an aggregate function")));
1500  if (procstruct->proiswindow)
1501  ereport(ERROR,
1502  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1503  errmsg("cast function must not be a window function")));
1504  if (procstruct->proretset)
1505  ereport(ERROR,
1506  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1507  errmsg("cast function must not return a set")));
1508 
1509  ReleaseSysCache(tuple);
1510  }
1511  else
1512  {
1513  funcid = InvalidOid;
1514  nargs = 0;
1515  }
1516 
1517  if (castmethod == COERCION_METHOD_BINARY)
1518  {
1519  int16 typ1len;
1520  int16 typ2len;
1521  bool typ1byval;
1522  bool typ2byval;
1523  char typ1align;
1524  char typ2align;
1525 
1526  /*
1527  * Must be superuser to create binary-compatible casts, since
1528  * erroneous casts can easily crash the backend.
1529  */
1530  if (!superuser())
1531  ereport(ERROR,
1532  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
1533  errmsg("must be superuser to create a cast WITHOUT FUNCTION")));
1534 
1535  /*
1536  * Also, insist that the types match as to size, alignment, and
1537  * pass-by-value attributes; this provides at least a crude check that
1538  * they have similar representations. A pair of types that fail this
1539  * test should certainly not be equated.
1540  */
1541  get_typlenbyvalalign(sourcetypeid, &typ1len, &typ1byval, &typ1align);
1542  get_typlenbyvalalign(targettypeid, &typ2len, &typ2byval, &typ2align);
1543  if (typ1len != typ2len ||
1544  typ1byval != typ2byval ||
1545  typ1align != typ2align)
1546  ereport(ERROR,
1547  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1548  errmsg("source and target data types are not physically compatible")));
1549 
1550  /*
1551  * We know that composite, enum and array types are never binary-
1552  * compatible with each other. They all have OIDs embedded in them.
1553  *
1554  * Theoretically you could build a user-defined base type that is
1555  * binary-compatible with a composite, enum, or array type. But we
1556  * disallow that too, as in practice such a cast is surely a mistake.
1557  * You can always work around that by writing a cast function.
1558  */
1559  if (sourcetyptype == TYPTYPE_COMPOSITE ||
1560  targettyptype == TYPTYPE_COMPOSITE)
1561  ereport(ERROR,
1562  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1563  errmsg("composite data types are not binary-compatible")));
1564 
1565  if (sourcetyptype == TYPTYPE_ENUM ||
1566  targettyptype == TYPTYPE_ENUM)
1567  ereport(ERROR,
1568  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1569  errmsg("enum data types are not binary-compatible")));
1570 
1571  if (OidIsValid(get_element_type(sourcetypeid)) ||
1572  OidIsValid(get_element_type(targettypeid)))
1573  ereport(ERROR,
1574  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1575  errmsg("array data types are not binary-compatible")));
1576 
1577  /*
1578  * We also disallow creating binary-compatibility casts involving
1579  * domains. Casting from a domain to its base type is already
1580  * allowed, and casting the other way ought to go through domain
1581  * coercion to permit constraint checking. Again, if you're intent on
1582  * having your own semantics for that, create a no-op cast function.
1583  *
1584  * NOTE: if we were to relax this, the above checks for composites
1585  * etc. would have to be modified to look through domains to their
1586  * base types.
1587  */
1588  if (sourcetyptype == TYPTYPE_DOMAIN ||
1589  targettyptype == TYPTYPE_DOMAIN)
1590  ereport(ERROR,
1591  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1592  errmsg("domain data types must not be marked binary-compatible")));
1593  }
1594 
1595  /*
1596  * Allow source and target types to be same only for length coercion
1597  * functions. We assume a multi-arg function does length coercion.
1598  */
1599  if (sourcetypeid == targettypeid && nargs < 2)
1600  ereport(ERROR,
1601  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1602  errmsg("source data type and target data type are the same")));
1603 
1604  /* convert CoercionContext enum to char value for castcontext */
1605  switch (stmt->context)
1606  {
1607  case COERCION_IMPLICIT:
1608  castcontext = COERCION_CODE_IMPLICIT;
1609  break;
1610  case COERCION_ASSIGNMENT:
1611  castcontext = COERCION_CODE_ASSIGNMENT;
1612  break;
1613  case COERCION_EXPLICIT:
1614  castcontext = COERCION_CODE_EXPLICIT;
1615  break;
1616  default:
1617  elog(ERROR, "unrecognized CoercionContext: %d", stmt->context);
1618  castcontext = 0; /* keep compiler quiet */
1619  break;
1620  }
1621 
1623 
1624  /*
1625  * Check for duplicate. This is just to give a friendly error message,
1626  * the unique index would catch it anyway (so no need to sweat about race
1627  * conditions).
1628  */
1630  ObjectIdGetDatum(sourcetypeid),
1631  ObjectIdGetDatum(targettypeid));
1632  if (HeapTupleIsValid(tuple))
1633  ereport(ERROR,
1635  errmsg("cast from type %s to type %s already exists",
1636  format_type_be(sourcetypeid),
1637  format_type_be(targettypeid))));
1638 
1639  /* ready to go */
1640  values[Anum_pg_cast_castsource - 1] = ObjectIdGetDatum(sourcetypeid);
1641  values[Anum_pg_cast_casttarget - 1] = ObjectIdGetDatum(targettypeid);
1642  values[Anum_pg_cast_castfunc - 1] = ObjectIdGetDatum(funcid);
1643  values[Anum_pg_cast_castcontext - 1] = CharGetDatum(castcontext);
1644  values[Anum_pg_cast_castmethod - 1] = CharGetDatum(castmethod);
1645 
1646  MemSet(nulls, false, sizeof(nulls));
1647 
1648  tuple = heap_form_tuple(RelationGetDescr(relation), values, nulls);
1649 
1650  castid = CatalogTupleInsert(relation, tuple);
1651 
1652  /* make dependency entries */
1653  myself.classId = CastRelationId;
1654  myself.objectId = castid;
1655  myself.objectSubId = 0;
1656 
1657  /* dependency on source type */
1658  referenced.classId = TypeRelationId;
1659  referenced.objectId = sourcetypeid;
1660  referenced.objectSubId = 0;
1661  recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
1662 
1663  /* dependency on target type */
1664  referenced.classId = TypeRelationId;
1665  referenced.objectId = targettypeid;
1666  referenced.objectSubId = 0;
1667  recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
1668 
1669  /* dependency on function */
1670  if (OidIsValid(funcid))
1671  {
1672  referenced.classId = ProcedureRelationId;
1673  referenced.objectId = funcid;
1674  referenced.objectSubId = 0;
1675  recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
1676  }
1677 
1678  /* dependency on extension */
1679  recordDependencyOnCurrentExtension(&myself, false);
1680 
1681  /* Post creation hook for new cast */
1683 
1684  heap_freetuple(tuple);
1685 
1686  heap_close(relation, RowExclusiveLock);
1687 
1688  return myself;
1689 }
signed short int16
Definition: c.h:255
#define TYPTYPE_DOMAIN
Definition: pg_type.h:718
TypeName * sourcetype
Definition: parsenodes.h:3198
#define Anum_pg_cast_castfunc
Definition: pg_cast.h:80
#define Anum_pg_cast_casttarget
Definition: pg_cast.h:79
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
#define InvokeObjectPostCreateHook(classId, objectId, subId)
Definition: objectaccess.h:145
#define RelationGetDescr(relation)
Definition: rel.h:429
Oid GetUserId(void)
Definition: miscinit.c:283
#define TYPTYPE_COMPOSITE
Definition: pg_type.h:717
Oid get_element_type(Oid typid)
Definition: lsyscache.c:2452
void get_typlenbyvalalign(Oid typid, int16 *typlen, bool *typbyval, char *typalign)
Definition: lsyscache.c:1989
char * TypeNameToString(const TypeName *typeName)
Definition: parse_type.c:459
#define ProcedureRelationId
Definition: pg_proc.h:33
#define INT4OID
Definition: pg_type.h:316
int errcode(int sqlerrcode)
Definition: elog.c:575
bool superuser(void)
Definition: superuser.c:47
char get_typtype(Oid typid)
Definition: lsyscache.c:2347
#define MemSet(start, val, len)
Definition: c.h:857
char * format_type_be(Oid type_oid)
Definition: format_type.c:94
void recordDependencyOn(const ObjectAddress *depender, const ObjectAddress *referenced, DependencyType behavior)
Definition: pg_depend.c:44
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:692
#define heap_close(r, l)
Definition: heapam.h:97
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1374
unsigned int Oid
Definition: postgres_ext.h:31
bool pg_type_ownercheck(Oid type_oid, Oid roleid)
Definition: aclchk.c:4555
#define TypeRelationId
Definition: pg_type.h:34
#define OidIsValid(objectId)
Definition: c.h:538
#define Anum_pg_cast_castmethod
Definition: pg_cast.h:82
#define SearchSysCache1(cacheId, key1)
Definition: syscache.h:152
void aclcheck_error_type(AclResult aclerr, Oid typeOid)
Definition: aclchk.c:3440
#define Natts_pg_cast
Definition: pg_cast.h:77
#define Anum_pg_cast_castsource
Definition: pg_cast.h:78
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
Oid CatalogTupleInsert(Relation heapRel, HeapTuple tup)
Definition: indexing.c:162
CoercionContext context
Definition: parsenodes.h:3201
#define RowExclusiveLock
Definition: lockdefs.h:38
#define ACL_USAGE
Definition: parsenodes.h:73
#define ereport(elevel, rest)
Definition: elog.h:122
Oid LookupFuncWithArgs(ObjectWithArgs *func, bool noError)
Definition: parse_func.c:1971
bool IsBinaryCoercible(Oid srctype, Oid targettype)
#define PROVOLATILE_VOLATILE
Definition: pg_proc.h:5436
#define WARNING
Definition: elog.h:40
AclResult
Definition: acl.h:170
uintptr_t Datum
Definition: postgres.h:372
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1116
ObjectWithArgs * func
Definition: parsenodes.h:3200
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1287
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:83
#define InvalidOid
Definition: postgres_ext.h:36
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define NULL
Definition: c.h:229
void recordDependencyOnCurrentExtension(const ObjectAddress *object, bool isReplace)
Definition: pg_depend.c:139
#define CastRelationId
Definition: pg_cast.h:31
#define BOOLOID
Definition: pg_type.h:288
#define CharGetDatum(X)
Definition: postgres.h:422
#define TYPTYPE_PSEUDO
Definition: pg_type.h:720
static Datum values[MAXATTR]
Definition: bootstrap.c:162
int errmsg(const char *fmt,...)
Definition: elog.c:797
TypeName * targettype
Definition: parsenodes.h:3199
#define TYPTYPE_ENUM
Definition: pg_type.h:719
#define Anum_pg_cast_castcontext
Definition: pg_cast.h:81
#define elog
Definition: elog.h:219
AclResult pg_type_aclcheck(Oid type_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:4517
#define ERRCODE_DUPLICATE_OBJECT
Definition: streamutil.c:31
Oid typenameTypeId(ParseState *pstate, const TypeName *typeName)
Definition: parse_type.c:274
#define SearchSysCache2(cacheId, key1, key2)
Definition: syscache.h:154
ObjectAddress CreateFunction ( ParseState pstate,
CreateFunctionStmt stmt 
)

Definition at line 861 of file functioncmds.c.

References ACL_CREATE, ACL_KIND_LANGUAGE, ACL_KIND_NAMESPACE, ACL_USAGE, aclcheck_error(), ACLCHECK_NO_PRIV, ACLCHECK_OK, castNode, ClanguageId, compute_attributes_sql_style(), compute_attributes_with_style(), compute_return_type(), construct_array(), ereport, errcode(), errhint(), errmsg(), ERROR, format_type_be(), CreateFunctionStmt::funcname, get_base_element_type(), get_namespace_name(), get_transform_oid(), GETSTRUCT, GetUserId(), HeapTupleGetOid, HeapTupleIsValid, i, INTERNALlanguageId, interpret_AS_clause(), interpret_function_parameter_list(), LANGNAME, lappend_oid(), lfirst, lfirst_oid, list_length(), NameStr, NIL, NULL, ObjectIdGetDatum, OidIsValid, OIDOID, CreateFunctionStmt::options, palloc(), CreateFunctionStmt::parameters, pg_language_aclcheck(), pg_namespace_aclcheck(), PLTemplateExists(), PointerGetDatum, ProcedureCreate(), PROPARALLEL_UNSAFE, PROVOLATILE_VOLATILE, QualifiedNameGetCreationNamespace(), ReleaseSysCache(), CreateFunctionStmt::replace, CreateFunctionStmt::returnType, SearchSysCache1, superuser(), typenameTypeId(), VOIDOID, and CreateFunctionStmt::withClause.

Referenced by ProcessUtilitySlow().

862 {
863  char *probin_str;
864  char *prosrc_str;
865  Oid prorettype;
866  bool returnsSet;
867  char *language;
868  Oid languageOid;
869  Oid languageValidator;
870  Node *transformDefElem = NULL;
871  char *funcname;
872  Oid namespaceId;
873  AclResult aclresult;
874  oidvector *parameterTypes;
875  ArrayType *allParameterTypes;
876  ArrayType *parameterModes;
877  ArrayType *parameterNames;
878  List *parameterDefaults;
879  Oid variadicArgType;
880  List *trftypes_list = NIL;
881  ArrayType *trftypes;
882  Oid requiredResultType;
883  bool isWindowFunc,
884  isStrict,
885  security,
886  isLeakProof;
887  char volatility;
888  ArrayType *proconfig;
889  float4 procost;
890  float4 prorows;
891  HeapTuple languageTuple;
892  Form_pg_language languageStruct;
893  List *as_clause;
894  char parallel;
895 
896  /* Convert list of names to a name and namespace */
897  namespaceId = QualifiedNameGetCreationNamespace(stmt->funcname,
898  &funcname);
899 
900  /* Check we have creation rights in target namespace */
901  aclresult = pg_namespace_aclcheck(namespaceId, GetUserId(), ACL_CREATE);
902  if (aclresult != ACLCHECK_OK)
904  get_namespace_name(namespaceId));
905 
906  /* default attributes */
907  isWindowFunc = false;
908  isStrict = false;
909  security = false;
910  isLeakProof = false;
911  volatility = PROVOLATILE_VOLATILE;
912  proconfig = NULL;
913  procost = -1; /* indicates not set */
914  prorows = -1; /* indicates not set */
915  parallel = PROPARALLEL_UNSAFE;
916 
917  /* override attributes from explicit list */
919  stmt->options,
920  &as_clause, &language, &transformDefElem,
921  &isWindowFunc, &volatility,
922  &isStrict, &security, &isLeakProof,
923  &proconfig, &procost, &prorows, &parallel);
924 
925  /* Look up the language and validate permissions */
926  languageTuple = SearchSysCache1(LANGNAME, PointerGetDatum(language));
927  if (!HeapTupleIsValid(languageTuple))
928  ereport(ERROR,
929  (errcode(ERRCODE_UNDEFINED_OBJECT),
930  errmsg("language \"%s\" does not exist", language),
931  (PLTemplateExists(language) ?
932  errhint("Use CREATE LANGUAGE to load the language into the database.") : 0)));
933 
934  languageOid = HeapTupleGetOid(languageTuple);
935  languageStruct = (Form_pg_language) GETSTRUCT(languageTuple);
936 
937  if (languageStruct->lanpltrusted)
938  {
939  /* if trusted language, need USAGE privilege */
940  AclResult aclresult;
941 
942  aclresult = pg_language_aclcheck(languageOid, GetUserId(), ACL_USAGE);
943  if (aclresult != ACLCHECK_OK)
945  NameStr(languageStruct->lanname));
946  }
947  else
948  {
949  /* if untrusted language, must be superuser */
950  if (!superuser())
952  NameStr(languageStruct->lanname));
953  }
954 
955  languageValidator = languageStruct->lanvalidator;
956 
957  ReleaseSysCache(languageTuple);
958 
959  /*
960  * Only superuser is allowed to create leakproof functions because
961  * leakproof functions can see tuples which have not yet been filtered out
962  * by security barrier views or row level security policies.
963  */
964  if (isLeakProof && !superuser())
965  ereport(ERROR,
966  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
967  errmsg("only superuser can define a leakproof function")));
968 
969  if (transformDefElem)
970  {
971  ListCell *lc;
972 
973  foreach(lc, castNode(List, transformDefElem))
974  {
975  Oid typeid = typenameTypeId(NULL, lfirst(lc));
976  Oid elt = get_base_element_type(typeid);
977 
978  typeid = elt ? elt : typeid;
979 
980  get_transform_oid(typeid, languageOid, false);
981  trftypes_list = lappend_oid(trftypes_list, typeid);
982  }
983  }
984 
985  /*
986  * Convert remaining parameters of CREATE to form wanted by
987  * ProcedureCreate.
988  */
990  stmt->parameters,
991  languageOid,
992  false, /* not an aggregate */
993  &parameterTypes,
994  &allParameterTypes,
995  &parameterModes,
996  &parameterNames,
997  &parameterDefaults,
998  &variadicArgType,
999  &requiredResultType);
1000 
1001  if (stmt->returnType)
1002  {
1003  /* explicit RETURNS clause */
1004  compute_return_type(stmt->returnType, languageOid,
1005  &prorettype, &returnsSet);
1006  if (OidIsValid(requiredResultType) && prorettype != requiredResultType)
1007  ereport(ERROR,
1008  (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
1009  errmsg("function result type must be %s because of OUT parameters",
1010  format_type_be(requiredResultType))));
1011  }
1012  else if (OidIsValid(requiredResultType))
1013  {
1014  /* default RETURNS clause from OUT parameters */
1015  prorettype = requiredResultType;
1016  returnsSet = false;
1017  }
1018  else
1019  {
1020  ereport(ERROR,
1021  (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
1022  errmsg("function result type must be specified")));
1023  /* Alternative possibility: default to RETURNS VOID */
1024  prorettype = VOIDOID;
1025  returnsSet = false;
1026  }
1027 
1028  if (list_length(trftypes_list) > 0)
1029  {
1030  ListCell *lc;
1031  Datum *arr;
1032  int i;
1033 
1034  arr = palloc(list_length(trftypes_list) * sizeof(Datum));
1035  i = 0;
1036  foreach(lc, trftypes_list)
1037  arr[i++] = ObjectIdGetDatum(lfirst_oid(lc));
1038  trftypes = construct_array(arr, list_length(trftypes_list),
1039  OIDOID, sizeof(Oid), true, 'i');
1040  }
1041  else
1042  {
1043  /* store SQL NULL instead of empty array */
1044  trftypes = NULL;
1045  }
1046 
1047  compute_attributes_with_style(pstate, stmt->withClause, &isStrict, &volatility);
1048 
1049  interpret_AS_clause(languageOid, language, funcname, as_clause,
1050  &prosrc_str, &probin_str);
1051 
1052  /*
1053  * Set default values for COST and ROWS depending on other parameters;
1054  * reject ROWS if it's not returnsSet. NB: pg_dump knows these default
1055  * values, keep it in sync if you change them.
1056  */
1057  if (procost < 0)
1058  {
1059  /* SQL and PL-language functions are assumed more expensive */
1060  if (languageOid == INTERNALlanguageId ||
1061  languageOid == ClanguageId)
1062  procost = 1;
1063  else
1064  procost = 100;
1065  }
1066  if (prorows < 0)
1067  {
1068  if (returnsSet)
1069  prorows = 1000;
1070  else
1071  prorows = 0; /* dummy value if not returnsSet */
1072  }
1073  else if (!returnsSet)
1074  ereport(ERROR,
1075  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1076  errmsg("ROWS is not applicable when function does not return a set")));
1077 
1078  /*
1079  * And now that we have all the parameters, and know we're permitted to do
1080  * so, go ahead and create the function.
1081  */
1082  return ProcedureCreate(funcname,
1083  namespaceId,
1084  stmt->replace,
1085  returnsSet,
1086  prorettype,
1087  GetUserId(),
1088  languageOid,
1089  languageValidator,
1090  prosrc_str, /* converted to text later */
1091  probin_str, /* converted to text later */
1092  false, /* not an aggregate */
1093  isWindowFunc,
1094  security,
1095  isLeakProof,
1096  isStrict,
1097  volatility,
1098  parallel,
1099  parameterTypes,
1100  PointerGetDatum(allParameterTypes),
1101  PointerGetDatum(parameterModes),
1102  PointerGetDatum(parameterNames),
1103  parameterDefaults,
1104  PointerGetDatum(trftypes),
1105  PointerGetDatum(proconfig),
1106  procost,
1107  prorows);
1108 }
#define NIL
Definition: pg_list.h:69
Definition: c.h:478
int errhint(const char *fmt,...)
Definition: elog.c:987
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
static void compute_attributes_sql_style(ParseState *pstate, List *options, List **as, char **language, Node **transform, bool *windowfunc_p, char *volatility_p, bool *strict_p, bool *security_definer, bool *leakproof_p, ArrayType **proconfig, float4 *procost, float4 *prorows, char *parallel_p)
Definition: functioncmds.c:605
#define INTERNALlanguageId
Definition: pg_language.h:74
Oid GetUserId(void)
Definition: miscinit.c:283
#define castNode(_type_, nodeptr)
Definition: nodes.h:575
#define OIDOID
Definition: pg_type.h:328
Oid QualifiedNameGetCreationNamespace(List *names, char **objname_p)
Definition: namespace.c:2846
#define PointerGetDatum(X)
Definition: postgres.h:562
static void compute_return_type(TypeName *returnType, Oid languageOid, Oid *prorettype_p, bool *returnsSet_p)
Definition: functioncmds.c:83
ArrayType * construct_array(Datum *elems, int nelems, Oid elmtype, int elmlen, bool elmbyval, char elmalign)
Definition: arrayfuncs.c:3306
Definition: nodes.h:506
#define ClanguageId
Definition: pg_language.h:77
int errcode(int sqlerrcode)
Definition: elog.c:575
bool superuser(void)
Definition: superuser.c:47
char * format_type_be(Oid type_oid)
Definition: format_type.c:94
static void interpret_AS_clause(Oid languageOid, const char *languageName, char *funcname, List *as, char **prosrc_str_p, char **probin_str_p)
Definition: functioncmds.c:801
unsigned int Oid
Definition: postgres_ext.h:31
List * lappend_oid(List *list, Oid datum)
Definition: list.c:164
#define OidIsValid(objectId)
Definition: c.h:538
AclResult pg_namespace_aclcheck(Oid nsp_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:4467
AclResult pg_language_aclcheck(Oid lang_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:4441
#define SearchSysCache1(cacheId, key1)
Definition: syscache.h:152
void interpret_function_parameter_list(ParseState *pstate, List *parameters, Oid languageOid, bool is_aggregate, oidvector **parameterTypes, ArrayType **allParameterTypes, ArrayType **parameterModes, ArrayType **parameterNames, List **parameterDefaults, Oid *variadicArgType, Oid *requiredResultType)
Definition: functioncmds.c:179
Oid get_transform_oid(Oid type_id, Oid lang_id, bool missing_ok)
#define VOIDOID
Definition: pg_type.h:686
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
#define ACL_CREATE
Definition: parsenodes.h:75
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3006
void aclcheck_error(AclResult aclerr, AclObjectKind objectkind, const char *objectname)
Definition: aclchk.c:3382
#define ACL_USAGE
Definition: parsenodes.h:73
#define ereport(elevel, rest)
Definition: elog.h:122
#define PROPARALLEL_UNSAFE
Definition: pg_proc.h:5445
#define PROVOLATILE_VOLATILE
Definition: pg_proc.h:5436
float float4
Definition: c.h:380
AclResult
Definition: acl.h:170
uintptr_t Datum
Definition: postgres.h:372
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1116
bool PLTemplateExists(const char *languageName)
Definition: proclang.c:519
ObjectAddress ProcedureCreate(const char *procedureName, Oid procNamespace, bool replace, bool returnsSet, Oid returnType, Oid proowner, Oid languageObjectId, Oid languageValidator, const char *prosrc, const char *probin, bool isAgg, bool isWindowFunc, bool security_definer, bool isLeakProof, bool isStrict, char volatility, char parallel, oidvector *parameterTypes, Datum allParameterTypes, Datum parameterModes, Datum parameterNames, List *parameterDefaults, Datum trftypes, Datum proconfig, float4 procost, float4 prorows)
Definition: pg_proc.c:67
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define NULL
Definition: c.h:229
#define lfirst(lc)
Definition: pg_list.h:106
TypeName * returnType
Definition: parsenodes.h:2684
static int list_length(const List *l)
Definition: pg_list.h:89
static void compute_attributes_with_style(ParseState *pstate, List *parameters, bool *isStrict_p, char *volatility_p)
Definition: functioncmds.c:765
Oid get_base_element_type(Oid typid)
Definition: lsyscache.c:2525
FormData_pg_language * Form_pg_language
Definition: pg_language.h:51
void * palloc(Size size)
Definition: mcxt.c:849
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
#define NameStr(name)
Definition: c.h:499
#define HeapTupleGetOid(tuple)
Definition: htup_details.h:695
Definition: pg_list.h:45
#define lfirst_oid(lc)
Definition: pg_list.h:108
Oid typenameTypeId(ParseState *pstate, const TypeName *typeName)
Definition: parse_type.c:274
ObjectAddress CreateTransform ( CreateTransformStmt stmt)

Definition at line 1776 of file functioncmds.c.

References ACL_EXECUTE, ACL_KIND_LANGUAGE, ACL_KIND_PROC, ACL_USAGE, aclcheck_error(), aclcheck_error_type(), ACLCHECK_NOT_OWNER, ACLCHECK_OK, Anum_pg_transform_trffromsql, Anum_pg_transform_trflang, Anum_pg_transform_trftosql, Anum_pg_transform_trftype, CatalogTupleInsert(), CatalogTupleUpdate(), check_transform_function(), ObjectAddress::classId, deleteDependencyRecordsFor(), DEPENDENCY_NORMAL, elog, ereport, errcode(), ERRCODE_DUPLICATE_OBJECT, errmsg(), ERROR, format_type_be(), CreateTransformStmt::fromsql, get_language_oid(), get_typtype(), GETSTRUCT, GetUserId(), heap_close, heap_form_tuple(), heap_freetuple(), heap_modify_tuple(), heap_open(), HeapTupleGetOid, HeapTupleIsValid, INTERNALOID, InvalidOid, InvokeObjectPostCreateHook, CreateTransformStmt::lang, LanguageRelationId, LookupFuncWithArgs(), MemSet, NameListToString(), Natts_pg_transform, NULL, ObjectAddress::objectId, ObjectIdGetDatum, ObjectAddress::objectSubId, ObjectWithArgs::objname, OidIsValid, pg_language_aclcheck(), pg_proc_aclcheck(), pg_proc_ownercheck(), pg_type_aclcheck(), pg_type_ownercheck(), ProcedureRelationId, PROCOID, recordDependencyOn(), recordDependencyOnCurrentExtension(), RelationGetDescr, ReleaseSysCache(), CreateTransformStmt::replace, RowExclusiveLock, SearchSysCache1, SearchSysCache2, HeapTupleData::t_self, CreateTransformStmt::tosql, TransformRelationId, TRFTYPELANG, CreateTransformStmt::type_name, TypeNameToString(), typenameTypeId(), TypeRelationId, TYPTYPE_DOMAIN, TYPTYPE_PSEUDO, and values.

Referenced by ProcessUtilitySlow().

1777 {
1778  Oid typeid;
1779  char typtype;
1780  Oid langid;
1781  Oid fromsqlfuncid;
1782  Oid tosqlfuncid;
1783  AclResult aclresult;
1784  Form_pg_proc procstruct;
1786  bool nulls[Natts_pg_transform];
1787  bool replaces[Natts_pg_transform];
1788  Oid transformid;
1789  HeapTuple tuple;
1790  HeapTuple newtuple;
1791  Relation relation;
1792  ObjectAddress myself,
1793  referenced;
1794  bool is_replace;
1795 
1796  /*
1797  * Get the type
1798  */
1799  typeid = typenameTypeId(NULL, stmt->type_name);
1800  typtype = get_typtype(typeid);
1801 
1802  if (typtype == TYPTYPE_PSEUDO)
1803  ereport(ERROR,
1804  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1805  errmsg("data type %s is a pseudo-type",
1806  TypeNameToString(stmt->type_name))));
1807 
1808  if (typtype == TYPTYPE_DOMAIN)
1809  ereport(ERROR,
1810  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1811  errmsg("data type %s is a domain",
1812  TypeNameToString(stmt->type_name))));
1813 
1814  if (!pg_type_ownercheck(typeid, GetUserId()))
1816 
1817  aclresult = pg_type_aclcheck(typeid, GetUserId(), ACL_USAGE);
1818  if (aclresult != ACLCHECK_OK)
1819  aclcheck_error_type(aclresult, typeid);
1820 
1821  /*
1822  * Get the language
1823  */
1824  langid = get_language_oid(stmt->lang, false);
1825 
1826  aclresult = pg_language_aclcheck(langid, GetUserId(), ACL_USAGE);
1827  if (aclresult != ACLCHECK_OK)
1828  aclcheck_error(aclresult, ACL_KIND_LANGUAGE, stmt->lang);
1829 
1830  /*
1831  * Get the functions
1832  */
1833  if (stmt->fromsql)
1834  {
1835  fromsqlfuncid = LookupFuncWithArgs(stmt->fromsql, false);
1836 
1837  if (!pg_proc_ownercheck(fromsqlfuncid, GetUserId()))
1839 
1840  aclresult = pg_proc_aclcheck(fromsqlfuncid, GetUserId(), ACL_EXECUTE);
1841  if (aclresult != ACLCHECK_OK)
1843 
1844  tuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(fromsqlfuncid));
1845  if (!HeapTupleIsValid(tuple))
1846  elog(ERROR, "cache lookup failed for function %u", fromsqlfuncid);
1847  procstruct = (Form_pg_proc) GETSTRUCT(tuple);
1848  if (procstruct->prorettype != INTERNALOID)
1849  ereport(ERROR,
1850  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1851  errmsg("return data type of FROM SQL function must be %s",
1852  "internal")));
1853  check_transform_function(procstruct);
1854  ReleaseSysCache(tuple);
1855  }
1856  else
1857  fromsqlfuncid = InvalidOid;
1858 
1859  if (stmt->tosql)
1860  {
1861  tosqlfuncid = LookupFuncWithArgs(stmt->tosql, false);
1862 
1863  if (!pg_proc_ownercheck(tosqlfuncid, GetUserId()))
1865 
1866  aclresult = pg_proc_aclcheck(tosqlfuncid, GetUserId(), ACL_EXECUTE);
1867  if (aclresult != ACLCHECK_OK)
1869 
1870  tuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(tosqlfuncid));
1871  if (!HeapTupleIsValid(tuple))
1872  elog(ERROR, "cache lookup failed for function %u", tosqlfuncid);
1873  procstruct = (Form_pg_proc) GETSTRUCT(tuple);
1874  if (procstruct->prorettype != typeid)
1875  ereport(ERROR,
1876  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1877  errmsg("return data type of TO SQL function must be the transform data type")));
1878  check_transform_function(procstruct);
1879  ReleaseSysCache(tuple);
1880  }
1881  else
1882  tosqlfuncid = InvalidOid;
1883 
1884  /*
1885  * Ready to go
1886  */
1887  values[Anum_pg_transform_trftype - 1] = ObjectIdGetDatum(typeid);
1888  values[Anum_pg_transform_trflang - 1] = ObjectIdGetDatum(langid);
1889  values[Anum_pg_transform_trffromsql - 1] = ObjectIdGetDatum(fromsqlfuncid);
1890  values[Anum_pg_transform_trftosql - 1] = ObjectIdGetDatum(tosqlfuncid);
1891 
1892  MemSet(nulls, false, sizeof(nulls));
1893 
1895 
1896  tuple = SearchSysCache2(TRFTYPELANG,
1897  ObjectIdGetDatum(typeid),
1898  ObjectIdGetDatum(langid));
1899  if (HeapTupleIsValid(tuple))
1900  {
1901  if (!stmt->replace)
1902  ereport(ERROR,
1904  errmsg("transform for type %s language \"%s\" already exists",
1905  format_type_be(typeid),
1906  stmt->lang)));
1907 
1908  MemSet(replaces, false, sizeof(replaces));
1909  replaces[Anum_pg_transform_trffromsql - 1] = true;
1910  replaces[Anum_pg_transform_trftosql - 1] = true;
1911 
1912  newtuple = heap_modify_tuple(tuple, RelationGetDescr(relation), values, nulls, replaces);
1913  CatalogTupleUpdate(relation, &newtuple->t_self, newtuple);
1914 
1915  transformid = HeapTupleGetOid(tuple);
1916  ReleaseSysCache(tuple);
1917  is_replace = true;
1918  }
1919  else
1920  {
1921  newtuple = heap_form_tuple(RelationGetDescr(relation), values, nulls);
1922  transformid = CatalogTupleInsert(relation, newtuple);
1923  is_replace = false;
1924  }
1925 
1926  if (is_replace)
1927  deleteDependencyRecordsFor(TransformRelationId, transformid, true);
1928 
1929  /* make dependency entries */
1930  myself.classId = TransformRelationId;
1931  myself.objectId = transformid;
1932  myself.objectSubId = 0;
1933 
1934  /* dependency on language */
1935  referenced.classId = LanguageRelationId;
1936  referenced.objectId = langid;
1937  referenced.objectSubId = 0;
1938  recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
1939 
1940  /* dependency on type */
1941  referenced.classId = TypeRelationId;
1942  referenced.objectId = typeid;
1943  referenced.objectSubId = 0;
1944  recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
1945 
1946  /* dependencies on functions */
1947  if (OidIsValid(fromsqlfuncid))
1948  {
1949  referenced.classId = ProcedureRelationId;
1950  referenced.objectId = fromsqlfuncid;
1951  referenced.objectSubId = 0;
1952  recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
1953  }
1954  if (OidIsValid(tosqlfuncid))
1955  {
1956  referenced.classId = ProcedureRelationId;
1957  referenced.objectId = tosqlfuncid;
1958  referenced.objectSubId = 0;
1959  recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
1960  }
1961 
1962  /* dependency on extension */
1963  recordDependencyOnCurrentExtension(&myself, is_replace);
1964 
1965  /* Post creation hook for new transform */
1967 
1968  heap_freetuple(newtuple);
1969 
1970  heap_close(relation, RowExclusiveLock);
1971 
1972  return myself;
1973 }
#define TYPTYPE_DOMAIN
Definition: pg_type.h:718
ObjectWithArgs * tosql
Definition: parsenodes.h:3216
static void check_transform_function(Form_pg_proc procstruct)
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
#define InvokeObjectPostCreateHook(classId, objectId, subId)
Definition: objectaccess.h:145
#define TransformRelationId
Definition: pg_transform.h:25
#define RelationGetDescr(relation)
Definition: rel.h:429
Oid GetUserId(void)
Definition: miscinit.c:283
char * TypeNameToString(const TypeName *typeName)
Definition: parse_type.c:459
#define ProcedureRelationId
Definition: pg_proc.h:33
long deleteDependencyRecordsFor(Oid classId, Oid objectId, bool skipExtensionDeps)
Definition: pg_depend.c:191
Oid get_language_oid(const char *langname, bool missing_ok)
Definition: proclang.c:553
int errcode(int sqlerrcode)
Definition: elog.c:575
char get_typtype(Oid typid)
Definition: lsyscache.c:2347
#define MemSet(start, val, len)
Definition: c.h:857
char * format_type_be(Oid type_oid)
Definition: format_type.c:94
void recordDependencyOn(const ObjectAddress *depender, const ObjectAddress *referenced, DependencyType behavior)
Definition: pg_depend.c:44
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:692
#define heap_close(r, l)
Definition: heapam.h:97
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1374
unsigned int Oid
Definition: postgres_ext.h:31
bool pg_type_ownercheck(Oid type_oid, Oid roleid)
Definition: aclchk.c:4555
#define TypeRelationId
Definition: pg_type.h:34
#define OidIsValid(objectId)
Definition: c.h:538
#define Anum_pg_transform_trftosql
Definition: pg_transform.h:45
AclResult pg_language_aclcheck(Oid lang_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:4441
#define SearchSysCache1(cacheId, key1)
Definition: syscache.h:152
void aclcheck_error_type(AclResult aclerr, Oid typeOid)
Definition: aclchk.c:3440
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
Oid CatalogTupleInsert(Relation heapRel, HeapTuple tup)
Definition: indexing.c:162
#define Natts_pg_transform
Definition: pg_transform.h:41
#define Anum_pg_transform_trflang
Definition: pg_transform.h:43
ItemPointerData t_self
Definition: htup.h:65
void aclcheck_error(AclResult aclerr, AclObjectKind objectkind, const char *objectname)
Definition: aclchk.c:3382
#define RowExclusiveLock
Definition: lockdefs.h:38
#define ACL_USAGE
Definition: parsenodes.h:73
#define ereport(elevel, rest)
Definition: elog.h:122
Oid LookupFuncWithArgs(ObjectWithArgs *func, bool noError)
Definition: parse_func.c:1971
char * NameListToString(List *names)
Definition: namespace.c:2953
AclResult
Definition: acl.h:170
uintptr_t Datum
Definition: postgres.h:372
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1116
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1287
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:83
#define InvalidOid
Definition: postgres_ext.h:36
ObjectWithArgs * fromsql
Definition: parsenodes.h:3215
#define Anum_pg_transform_trffromsql
Definition: pg_transform.h:44
#define INTERNALOID
Definition: pg_type.h:694
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define NULL
Definition: c.h:229
void recordDependencyOnCurrentExtension(const ObjectAddress *object, bool isReplace)
Definition: pg_depend.c:139
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
Definition: indexing.c:210
#define TYPTYPE_PSEUDO
Definition: pg_type.h:720
#define Anum_pg_transform_trftype
Definition: pg_transform.h:42
TypeName * type_name
Definition: parsenodes.h:3213
static Datum values[MAXATTR]
Definition: bootstrap.c:162
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define ACL_EXECUTE
Definition: parsenodes.h:72
AclResult pg_proc_aclcheck(Oid proc_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:4429
#define LanguageRelationId
Definition: pg_language.h:29
#define elog
Definition: elog.h:219
#define HeapTupleGetOid(tuple)
Definition: htup_details.h:695
AclResult pg_type_aclcheck(Oid type_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:4517
HeapTuple heap_modify_tuple(HeapTuple tuple, TupleDesc tupleDesc, Datum *replValues, bool *replIsnull, bool *doReplace)
Definition: heaptuple.c:793
#define ERRCODE_DUPLICATE_OBJECT
Definition: streamutil.c:31
bool pg_proc_ownercheck(Oid proc_oid, Oid roleid)
Definition: aclchk.c:4607
Oid typenameTypeId(ParseState *pstate, const TypeName *typeName)
Definition: parse_type.c:274
#define SearchSysCache2(cacheId, key1, key2)
Definition: syscache.h:154
void DropCastById ( Oid  castOid)

Definition at line 1715 of file functioncmds.c.

References BTEqualStrategyNumber, CastOidIndexId, CastRelationId, CatalogTupleDelete(), elog, ERROR, heap_close, heap_open(), HeapTupleIsValid, NULL, ObjectIdAttributeNumber, ObjectIdGetDatum, RowExclusiveLock, ScanKeyInit(), systable_beginscan(), systable_endscan(), systable_getnext(), and HeapTupleData::t_self.

Referenced by doDeletion().

1716 {
1717  Relation relation;
1718  ScanKeyData scankey;
1719  SysScanDesc scan;
1720  HeapTuple tuple;
1721 
1723 
1724  ScanKeyInit(&scankey,
1726  BTEqualStrategyNumber, F_OIDEQ,
1727  ObjectIdGetDatum(castOid));
1728  scan = systable_beginscan(relation, CastOidIndexId, true,
1729  NULL, 1, &scankey);
1730 
1731  tuple = systable_getnext(scan);
1732  if (!HeapTupleIsValid(tuple))
1733  elog(ERROR, "could not find tuple for cast %u", castOid);
1734  CatalogTupleDelete(relation, &tuple->t_self);
1735 
1736  systable_endscan(scan);
1737  heap_close(relation, RowExclusiveLock);
1738 }
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:499
#define ObjectIdAttributeNumber
Definition: sysattr.h:22
void CatalogTupleDelete(Relation heapRel, ItemPointer tid)
Definition: indexing.c:255
#define heap_close(r, l)
Definition: heapam.h:97
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:328
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:416
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
ItemPointerData t_self
Definition: htup.h:65
#define RowExclusiveLock
Definition: lockdefs.h:38
#define CastOidIndexId
Definition: indexing.h:107
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1287
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define NULL
Definition: c.h:229
#define CastRelationId
Definition: pg_cast.h:31
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
#define elog
Definition: elog.h:219
#define BTEqualStrategyNumber
Definition: stratnum.h:31
void DropTransformById ( Oid  transformOid)

Definition at line 2001 of file functioncmds.c.

References BTEqualStrategyNumber, CatalogTupleDelete(), elog, ERROR, heap_close, heap_open(), HeapTupleIsValid, NULL, ObjectIdAttributeNumber, ObjectIdGetDatum, RowExclusiveLock, ScanKeyInit(), systable_beginscan(), systable_endscan(), systable_getnext(), HeapTupleData::t_self, TransformOidIndexId, and TransformRelationId.

Referenced by doDeletion().

2002 {
2003  Relation relation;
2004  ScanKeyData scankey;
2005  SysScanDesc scan;
2006  HeapTuple tuple;
2007 
2009 
2010  ScanKeyInit(&scankey,
2012  BTEqualStrategyNumber, F_OIDEQ,
2013  ObjectIdGetDatum(transformOid));
2014  scan = systable_beginscan(relation, TransformOidIndexId, true,
2015  NULL, 1, &scankey);
2016 
2017  tuple = systable_getnext(scan);
2018  if (!HeapTupleIsValid(tuple))
2019  elog(ERROR, "could not find tuple for transform %u", transformOid);
2020  CatalogTupleDelete(relation, &tuple->t_self);
2021 
2022  systable_endscan(scan);
2023  heap_close(relation, RowExclusiveLock);
2024 }
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:499
#define TransformRelationId
Definition: pg_transform.h:25
#define ObjectIdAttributeNumber
Definition: sysattr.h:22
void CatalogTupleDelete(Relation heapRel, ItemPointer tid)
Definition: indexing.c:255
#define heap_close(r, l)
Definition: heapam.h:97
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:328
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:416
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
ItemPointerData t_self
Definition: htup.h:65
#define RowExclusiveLock
Definition: lockdefs.h:38
#define TransformOidIndexId
Definition: indexing.h:242
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1287
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define NULL
Definition: c.h:229
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
#define elog
Definition: elog.h:219
#define BTEqualStrategyNumber
Definition: stratnum.h:31
void ExecuteDoStmt ( DoStmt stmt)

Definition at line 2055 of file functioncmds.c.

References ACL_KIND_LANGUAGE, ACL_USAGE, aclcheck_error(), ACLCHECK_NO_PRIV, ACLCHECK_OK, arg, DefElem::arg, DoStmt::args, DefElem::defname, elog, ereport, errcode(), errhint(), errmsg(), ERROR, GETSTRUCT, GetUserId(), HeapTupleGetOid, HeapTupleIsValid, InlineCodeBlock::langIsTrusted, LANGNAME, InlineCodeBlock::langOid, lfirst, makeNode, NameStr, NULL, OidFunctionCall1, OidIsValid, pg_language_aclcheck(), PLTemplateExists(), PointerGetDatum, ReleaseSysCache(), SearchSysCache1, InlineCodeBlock::source_text, strVal, and superuser().

Referenced by standard_ProcessUtility().

2056 {
2058  ListCell *arg;
2059  DefElem *as_item = NULL;
2060  DefElem *language_item = NULL;
2061  char *language;
2062  Oid laninline;
2063  HeapTuple languageTuple;
2064  Form_pg_language languageStruct;
2065 
2066  /* Process options we got from gram.y */
2067  foreach(arg, stmt->args)
2068  {
2069  DefElem *defel = (DefElem *) lfirst(arg);
2070 
2071  if (strcmp(defel->defname, "as") == 0)
2072  {
2073  if (as_item)
2074  ereport(ERROR,
2075  (errcode(ERRCODE_SYNTAX_ERROR),
2076  errmsg("conflicting or redundant options")));
2077  as_item = defel;
2078  }
2079  else if (strcmp(defel->defname, "language") == 0)
2080  {
2081  if (language_item)
2082  ereport(ERROR,
2083  (errcode(ERRCODE_SYNTAX_ERROR),
2084  errmsg("conflicting or redundant options")));
2085  language_item = defel;
2086  }
2087  else
2088  elog(ERROR, "option \"%s\" not recognized",
2089  defel->defname);
2090  }
2091 
2092  if (as_item)
2093  codeblock->source_text = strVal(as_item->arg);
2094  else
2095  ereport(ERROR,
2096  (errcode(ERRCODE_SYNTAX_ERROR),
2097  errmsg("no inline code specified")));
2098 
2099  /* if LANGUAGE option wasn't specified, use the default */
2100  if (language_item)
2101  language = strVal(language_item->arg);
2102  else
2103  language = "plpgsql";
2104 
2105  /* Look up the language and validate permissions */
2106  languageTuple = SearchSysCache1(LANGNAME, PointerGetDatum(language));
2107  if (!HeapTupleIsValid(languageTuple))
2108  ereport(ERROR,
2109  (errcode(ERRCODE_UNDEFINED_OBJECT),
2110  errmsg("language \"%s\" does not exist", language),
2111  (PLTemplateExists(language) ?
2112  errhint("Use CREATE LANGUAGE to load the language into the database.") : 0)));
2113 
2114  codeblock->langOid = HeapTupleGetOid(languageTuple);
2115  languageStruct = (Form_pg_language) GETSTRUCT(languageTuple);
2116  codeblock->langIsTrusted = languageStruct->lanpltrusted;
2117 
2118  if (languageStruct->lanpltrusted)
2119  {
2120  /* if trusted language, need USAGE privilege */
2121  AclResult aclresult;
2122 
2123  aclresult = pg_language_aclcheck(codeblock->langOid, GetUserId(),
2124  ACL_USAGE);
2125  if (aclresult != ACLCHECK_OK)
2126  aclcheck_error(aclresult, ACL_KIND_LANGUAGE,
2127  NameStr(languageStruct->lanname));
2128  }
2129  else
2130  {
2131  /* if untrusted language, must be superuser */
2132  if (!superuser())
2134  NameStr(languageStruct->lanname));
2135  }
2136 
2137  /* get the handler function's OID */
2138  laninline = languageStruct->laninline;
2139  if (!OidIsValid(laninline))
2140  ereport(ERROR,
2141  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2142  errmsg("language \"%s\" does not support inline code execution",
2143  NameStr(languageStruct->lanname))));
2144 
2145  ReleaseSysCache(languageTuple);
2146 
2147  /* execute the inline handler */
2148  OidFunctionCall1(laninline, PointerGetDatum(codeblock));
2149 }
int errhint(const char *fmt,...)
Definition: elog.c:987
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
Oid GetUserId(void)
Definition: miscinit.c:283
#define PointerGetDatum(X)
Definition: postgres.h:562
#define strVal(v)
Definition: value.h:54
int errcode(int sqlerrcode)
Definition: elog.c:575
bool superuser(void)
Definition: superuser.c:47
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:538
AclResult pg_language_aclcheck(Oid lang_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:4441
#define SearchSysCache1(cacheId, key1)
Definition: syscache.h:152
#define ERROR
Definition: elog.h:43
#define OidFunctionCall1(functionId, arg1)
Definition: fmgr.h:622
void aclcheck_error(AclResult aclerr, AclObjectKind objectkind, const char *objectname)
Definition: aclchk.c:3382
#define ACL_USAGE
Definition: parsenodes.h:73
#define ereport(elevel, rest)
Definition: elog.h:122
Node * arg
Definition: parsenodes.h:709
AclResult
Definition: acl.h:170
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1116
bool PLTemplateExists(const char *languageName)
Definition: proclang.c:519
#define makeNode(_type_)
Definition: nodes.h:554
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define NULL
Definition: c.h:229
#define lfirst(lc)
Definition: pg_list.h:106
List * args
Definition: parsenodes.h:2724
FormData_pg_language * Form_pg_language
Definition: pg_language.h:51
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define NameStr(name)
Definition: c.h:499
void * arg
char * source_text
Definition: parsenodes.h:2730
char * defname
Definition: parsenodes.h:708
#define elog
Definition: elog.h:219
#define HeapTupleGetOid(tuple)
Definition: htup_details.h:695
Oid get_cast_oid ( Oid  sourcetypeid,
Oid  targettypeid,
bool  missing_ok 
)

Definition at line 1698 of file functioncmds.c.

References CASTSOURCETARGET, ereport, errcode(), errmsg(), ERROR, format_type_be(), GetSysCacheOid2, ObjectIdGetDatum, and OidIsValid.

Referenced by get_object_address().

1699 {
1700  Oid oid;
1701 
1703  ObjectIdGetDatum(sourcetypeid),
1704  ObjectIdGetDatum(targettypeid));
1705  if (!OidIsValid(oid) && !missing_ok)
1706  ereport(ERROR,
1707  (errcode(ERRCODE_UNDEFINED_OBJECT),
1708  errmsg("cast from type %s to type %s does not exist",
1709  format_type_be(sourcetypeid),
1710  format_type_be(targettypeid))));
1711  return oid;
1712 }
int errcode(int sqlerrcode)
Definition: elog.c:575
char * format_type_be(Oid type_oid)
Definition: format_type.c:94
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:538
#define GetSysCacheOid2(cacheId, key1, key2)
Definition: syscache.h:181
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
int errmsg(const char *fmt,...)
Definition: elog.c:797
Oid get_transform_oid ( Oid  type_id,
Oid  lang_id,
bool  missing_ok 
)

Definition at line 1983 of file functioncmds.c.

References ereport, errcode(), errmsg(), ERROR, format_type_be(), get_language_name(), GetSysCacheOid2, ObjectIdGetDatum, OidIsValid, and TRFTYPELANG.

Referenced by CreateFunction(), get_object_address(), and ProcedureCreate().

1984 {
1985  Oid oid;
1986 
1988  ObjectIdGetDatum(type_id),
1989  ObjectIdGetDatum(lang_id));
1990  if (!OidIsValid(oid) && !missing_ok)
1991  ereport(ERROR,
1992  (errcode(ERRCODE_UNDEFINED_OBJECT),
1993  errmsg("transform for type %s language \"%s\" does not exist",
1994  format_type_be(type_id),
1995  get_language_name(lang_id, false))));
1996  return oid;
1997 }
char * get_language_name(Oid langoid, bool missing_ok)
Definition: lsyscache.c:986
int errcode(int sqlerrcode)
Definition: elog.c:575
char * format_type_be(Oid type_oid)
Definition: format_type.c:94
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:538
#define GetSysCacheOid2(cacheId, key1, key2)
Definition: syscache.h:181
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
int errmsg(const char *fmt,...)
Definition: elog.c:797
static void interpret_AS_clause ( Oid  languageOid,
const char *  languageName,
char *  funcname,
List as,
char **  prosrc_str_p,
char **  probin_str_p 
)
static

Definition at line 801 of file functioncmds.c.

References Assert, ClanguageId, ereport, errcode(), errmsg(), ERROR, INTERNALlanguageId, linitial, list_length(), lsecond, NIL, NULL, and strVal.

Referenced by CreateFunction().

804 {
805  Assert(as != NIL);
806 
807  if (languageOid == ClanguageId)
808  {
809  /*
810  * For "C" language, store the file name in probin and, when given,
811  * the link symbol name in prosrc. If link symbol is omitted,
812  * substitute procedure name. We also allow link symbol to be
813  * specified as "-", since that was the habit in PG versions before
814  * 8.4, and there might be dump files out there that don't translate
815  * that back to "omitted".
816  */
817  *probin_str_p = strVal(linitial(as));
818  if (list_length(as) == 1)
819  *prosrc_str_p = funcname;
820  else
821  {
822  *prosrc_str_p = strVal(lsecond(as));
823  if (strcmp(*prosrc_str_p, "-") == 0)
824  *prosrc_str_p = funcname;
825  }
826  }
827  else
828  {
829  /* Everything else wants the given string in prosrc. */
830  *prosrc_str_p = strVal(linitial(as));
831  *probin_str_p = NULL;
832 
833  if (list_length(as) != 1)
834  ereport(ERROR,
835  (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
836  errmsg("only one AS item needed for language \"%s\"",
837  languageName)));
838 
839  if (languageOid == INTERNALlanguageId)
840  {
841  /*
842  * In PostgreSQL versions before 6.5, the SQL name of the created
843  * function could not be different from the internal name, and
844  * "prosrc" wasn't used. So there is code out there that does
845  * CREATE FUNCTION xyz AS '' LANGUAGE internal. To preserve some
846  * modicum of backwards compatibility, accept an empty "prosrc"
847  * value as meaning the supplied SQL function name.
848  */
849  if (strlen(*prosrc_str_p) == 0)
850  *prosrc_str_p = funcname;
851  }
852  }
853 }
#define NIL
Definition: pg_list.h:69
#define INTERNALlanguageId
Definition: pg_language.h:74
#define strVal(v)
Definition: value.h:54
#define ClanguageId
Definition: pg_language.h:77
int errcode(int sqlerrcode)
Definition: elog.c:575
#define lsecond(l)
Definition: pg_list.h:114
#define linitial(l)
Definition: pg_list.h:110
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
static int list_length(const List *l)
Definition: pg_list.h:89
int errmsg(const char *fmt,...)
Definition: elog.c:797
static char interpret_func_parallel ( DefElem defel)
static

Definition at line 550 of file functioncmds.c.

References DefElem::arg, ereport, errcode(), errmsg(), ERROR, PROPARALLEL_RESTRICTED, PROPARALLEL_SAFE, PROPARALLEL_UNSAFE, and strVal.

Referenced by AlterFunction().

551 {
552  char *str = strVal(defel->arg);
553 
554  if (strcmp(str, "safe") == 0)
555  return PROPARALLEL_SAFE;
556  else if (strcmp(str, "unsafe") == 0)
557  return PROPARALLEL_UNSAFE;
558  else if (strcmp(str, "restricted") == 0)
559  return PROPARALLEL_RESTRICTED;
560  else
561  {
562  ereport(ERROR,
563  (errcode(ERRCODE_SYNTAX_ERROR),
564  errmsg("parameter \"parallel\" must be SAFE, RESTRICTED, or UNSAFE")));
565  return PROPARALLEL_UNSAFE; /* keep compiler quiet */
566  }
567 }
#define strVal(v)
Definition: value.h:54
int errcode(int sqlerrcode)
Definition: elog.c:575
#define PROPARALLEL_RESTRICTED
Definition: pg_proc.h:5444
#define ERROR
Definition: elog.h:43
#define PROPARALLEL_SAFE
Definition: pg_proc.h:5443
#define ereport(elevel, rest)
Definition: elog.h:122
#define PROPARALLEL_UNSAFE
Definition: pg_proc.h:5445
Node * arg
Definition: parsenodes.h:709
int errmsg(const char *fmt,...)
Definition: elog.c:797
static char interpret_func_volatility ( DefElem defel)
static

Definition at line 532 of file functioncmds.c.

References DefElem::arg, elog, ERROR, PROVOLATILE_IMMUTABLE, PROVOLATILE_STABLE, PROVOLATILE_VOLATILE, and strVal.

Referenced by AlterFunction().

533 {
534  char *str = strVal(defel->arg);
535 
536  if (strcmp(str, "immutable") == 0)
537  return PROVOLATILE_IMMUTABLE;
538  else if (strcmp(str, "stable") == 0)
539  return PROVOLATILE_STABLE;
540  else if (strcmp(str, "volatile") == 0)
541  return PROVOLATILE_VOLATILE;
542  else
543  {
544  elog(ERROR, "invalid volatility \"%s\"", str);
545  return 0; /* keep compiler quiet */
546  }
547 }
#define PROVOLATILE_IMMUTABLE
Definition: pg_proc.h:5434
#define strVal(v)
Definition: value.h:54
#define ERROR
Definition: elog.h:43
#define PROVOLATILE_STABLE
Definition: pg_proc.h:5435
Node * arg
Definition: parsenodes.h:709
#define PROVOLATILE_VOLATILE
Definition: pg_proc.h:5436
#define elog
Definition: elog.h:219
void interpret_function_parameter_list ( ParseState pstate,
List parameters,
Oid  languageOid,
bool  is_aggregate,
oidvector **  parameterTypes,
ArrayType **  allParameterTypes,
ArrayType **  parameterModes,
ArrayType **  parameterNames,
List **  parameterDefaults,
Oid variadicArgType,
Oid requiredResultType 
)

Definition at line 179 of file functioncmds.c.

References ACL_USAGE, aclcheck_error_type(), ACLCHECK_OK, ANYARRAYOID, ANYOID, FunctionParameter::argType, assign_expr_collations(), buildoidvector(), CharGetDatum, CHAROID, coerce_to_specific_type(), construct_array(), contain_var_clause(), CStringGetTextDatum, FunctionParameter::defexpr, ereport, errcode(), errmsg(), ERROR, EXPR_KIND_FUNCTION_DEFAULT, FUNC_PARAM_IN, FUNC_PARAM_OUT, FUNC_PARAM_TABLE, FUNC_PARAM_VARIADIC, get_element_type(), GETSTRUCT, GetUserId(), i, InvalidOid, lappend(), lfirst, list_length(), LookupTypeName(), FunctionParameter::mode, FunctionParameter::name, NIL, NOTICE, NULL, ObjectIdGetDatum, OidIsValid, OIDOID, ParseState::p_rtable, palloc(), palloc0(), pg_type_aclcheck(), PointerGetDatum, px(), RECORDOID, ReleaseSysCache(), TypeName::setof, SQLlanguageId, TEXTOID, transformExpr(), TypeNameToString(), and typeTypeId().

Referenced by CreateFunction(), and DefineAggregate().

190 {
191  int parameterCount = list_length(parameters);
192  Oid *inTypes;
193  int inCount = 0;
194  Datum *allTypes;
195  Datum *paramModes;
196  Datum *paramNames;
197  int outCount = 0;
198  int varCount = 0;
199  bool have_names = false;
200  bool have_defaults = false;
201  ListCell *x;
202  int i;
203 
204  *variadicArgType = InvalidOid; /* default result */
205  *requiredResultType = InvalidOid; /* default result */
206 
207  inTypes = (Oid *) palloc(parameterCount * sizeof(Oid));
208  allTypes = (Datum *) palloc(parameterCount * sizeof(Datum));
209  paramModes = (Datum *) palloc(parameterCount * sizeof(Datum));
210  paramNames = (Datum *) palloc0(parameterCount * sizeof(Datum));
211  *parameterDefaults = NIL;
212 
213  /* Scan the list and extract data into work arrays */
214  i = 0;
215  foreach(x, parameters)
216  {
218  TypeName *t = fp->argType;
219  bool isinput = false;
220  Oid toid;
221  Type typtup;
222  AclResult aclresult;
223 
224  typtup = LookupTypeName(NULL, t, NULL, false);
225  if (typtup)
226  {
227  if (!((Form_pg_type) GETSTRUCT(typtup))->typisdefined)
228  {
229  /* As above, hard error if language is SQL */
230  if (languageOid == SQLlanguageId)
231  ereport(ERROR,
232  (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
233  errmsg("SQL function cannot accept shell type %s",
234  TypeNameToString(t))));
235  /* We don't allow creating aggregates on shell types either */
236  else if (is_aggregate)
237  ereport(ERROR,
238  (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
239  errmsg("aggregate cannot accept shell type %s",
240  TypeNameToString(t))));
241  else
242  ereport(NOTICE,
243  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
244  errmsg("argument type %s is only a shell",
245  TypeNameToString(t))));
246  }
247  toid = typeTypeId(typtup);
248  ReleaseSysCache(typtup);
249  }
250  else
251  {
252  ereport(ERROR,
253  (errcode(ERRCODE_UNDEFINED_OBJECT),
254  errmsg("type %s does not exist",
255  TypeNameToString(t))));
256  toid = InvalidOid; /* keep compiler quiet */
257  }
258 
259  aclresult = pg_type_aclcheck(toid, GetUserId(), ACL_USAGE);
260  if (aclresult != ACLCHECK_OK)
261  aclcheck_error_type(aclresult, toid);
262 
263  if (t->setof)
264  {
265  if (is_aggregate)
266  ereport(ERROR,
267  (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
268  errmsg("aggregates cannot accept set arguments")));
269  else
270  ereport(ERROR,
271  (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
272  errmsg("functions cannot accept set arguments")));
273  }
274 
275  /* handle input parameters */
276  if (fp->mode != FUNC_PARAM_OUT && fp->mode != FUNC_PARAM_TABLE)
277  {
278  /* other input parameters can't follow a VARIADIC parameter */
279  if (varCount > 0)
280  ereport(ERROR,
281  (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
282  errmsg("VARIADIC parameter must be the last input parameter")));
283  inTypes[inCount++] = toid;
284  isinput = true;
285  }
286 
287  /* handle output parameters */
288  if (fp->mode != FUNC_PARAM_IN && fp->mode != FUNC_PARAM_VARIADIC)
289  {
290  if (outCount == 0) /* save first output param's type */
291  *requiredResultType = toid;
292  outCount++;
293  }
294 
295  if (fp->mode == FUNC_PARAM_VARIADIC)
296  {
297  *variadicArgType = toid;
298  varCount++;
299  /* validate variadic parameter type */
300  switch (toid)
301  {
302  case ANYARRAYOID:
303  case ANYOID:
304  /* okay */
305  break;
306  default:
307  if (!OidIsValid(get_element_type(toid)))
308  ereport(ERROR,
309  (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
310  errmsg("VARIADIC parameter must be an array")));
311  break;
312  }
313  }
314 
315  allTypes[i] = ObjectIdGetDatum(toid);
316 
317  paramModes[i] = CharGetDatum(fp->mode);
318 
319  if (fp->name && fp->name[0])
320  {
321  ListCell *px;
322 
323  /*
324  * As of Postgres 9.0 we disallow using the same name for two
325  * input or two output function parameters. Depending on the
326  * function's language, conflicting input and output names might
327  * be bad too, but we leave it to the PL to complain if so.
328  */
329  foreach(px, parameters)
330  {
331  FunctionParameter *prevfp = (FunctionParameter *) lfirst(px);
332 
333  if (prevfp == fp)
334  break;
335  /* pure in doesn't conflict with pure out */
336  if ((fp->mode == FUNC_PARAM_IN ||
337  fp->mode == FUNC_PARAM_VARIADIC) &&
338  (prevfp->mode == FUNC_PARAM_OUT ||
339  prevfp->mode == FUNC_PARAM_TABLE))
340  continue;
341  if ((prevfp->mode == FUNC_PARAM_IN ||
342  prevfp->mode == FUNC_PARAM_VARIADIC) &&
343  (fp->mode == FUNC_PARAM_OUT ||
344  fp->mode == FUNC_PARAM_TABLE))
345  continue;
346  if (prevfp->name && prevfp->name[0] &&
347  strcmp(prevfp->name, fp->name) == 0)
348  ereport(ERROR,
349  (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
350  errmsg("parameter name \"%s\" used more than once",
351  fp->name)));
352  }
353 
354  paramNames[i] = CStringGetTextDatum(fp->name);
355  have_names = true;
356  }
357 
358  if (fp->defexpr)
359  {
360  Node *def;
361 
362  if (!isinput)
363  ereport(ERROR,
364  (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
365  errmsg("only input parameters can have default values")));
366 
367  def = transformExpr(pstate, fp->defexpr,
369  def = coerce_to_specific_type(pstate, def, toid, "DEFAULT");
370  assign_expr_collations(pstate, def);
371 
372  /*
373  * Make sure no variables are referred to (this is probably dead
374  * code now that add_missing_from is history).
375  */
376  if (list_length(pstate->p_rtable) != 0 ||
377  contain_var_clause(def))
378  ereport(ERROR,
379  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
380  errmsg("cannot use table references in parameter default value")));
381 
382  /*
383  * transformExpr() should have already rejected subqueries,
384  * aggregates, and window functions, based on the EXPR_KIND_ for a
385  * default expression.
386  *
387  * It can't return a set either --- but coerce_to_specific_type
388  * already checked that for us.
389  *
390  * Note: the point of these restrictions is to ensure that an
391  * expression that, on its face, hasn't got subplans, aggregates,
392  * etc cannot suddenly have them after function default arguments
393  * are inserted.
394  */
395 
396  *parameterDefaults = lappend(*parameterDefaults, def);
397  have_defaults = true;
398  }
399  else
400  {
401  if (isinput && have_defaults)
402  ereport(ERROR,
403  (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
404  errmsg("input parameters after one with a default value must also have defaults")));
405  }
406 
407  i++;
408  }
409 
410  /* Now construct the proper outputs as needed */
411  *parameterTypes = buildoidvector(inTypes, inCount);
412 
413  if (outCount > 0 || varCount > 0)
414  {
415  *allParameterTypes = construct_array(allTypes, parameterCount, OIDOID,
416  sizeof(Oid), true, 'i');
417  *parameterModes = construct_array(paramModes, parameterCount, CHAROID,
418  1, true, 'c');
419  if (outCount > 1)
420  *requiredResultType = RECORDOID;
421  /* otherwise we set requiredResultType correctly above */
422  }
423  else
424  {
425  *allParameterTypes = NULL;
426  *parameterModes = NULL;
427  }
428 
429  if (have_names)
430  {
431  for (i = 0; i < parameterCount; i++)
432  {
433  if (paramNames[i] == PointerGetDatum(NULL))
434  paramNames[i] = CStringGetTextDatum("");
435  }
436  *parameterNames = construct_array(paramNames, parameterCount, TEXTOID,
437  -1, false, 'i');
438  }
439  else
440  *parameterNames = NULL;
441 }
Type LookupTypeName(ParseState *pstate, const TypeName *typeName, int32 *typmod_p, bool missing_ok)
Definition: parse_type.c:57
#define NIL
Definition: pg_list.h:69
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
Oid GetUserId(void)
Definition: miscinit.c:283
#define OIDOID
Definition: pg_type.h:328
#define TEXTOID
Definition: pg_type.h:324
Oid get_element_type(Oid typid)
Definition: lsyscache.c:2452
#define PointerGetDatum(X)
Definition: postgres.h:562
char * TypeNameToString(const TypeName *typeName)
Definition: parse_type.c:459
Node * transformExpr(ParseState *pstate, Node *expr, ParseExprKind exprKind)
Definition: parse_expr.c:147
ArrayType * construct_array(Datum *elems, int nelems, Oid elmtype, int elmlen, bool elmbyval, char elmalign)
Definition: arrayfuncs.c:3306
Definition: nodes.h:506
int errcode(int sqlerrcode)
Definition: elog.c:575
bool contain_var_clause(Node *node)
Definition: var.c:331
FormData_pg_type * Form_pg_type
Definition: pg_type.h:233
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:538
void aclcheck_error_type(AclResult aclerr, Oid typeOid)
Definition: aclchk.c:3440
void assign_expr_collations(ParseState *pstate, Node *expr)
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
#define SQLlanguageId
Definition: pg_language.h:80
bool setof
Definition: parsenodes.h:202
#define ANYOID
Definition: pg_type.h:682
void px(PlannerInfo *root, Gene *tour1, Gene *tour2, Gene *offspring, int num_gene, City *city_table)
Definition: geqo_px.c:46
#define ACL_USAGE
Definition: parsenodes.h:73
#define RECORDOID
Definition: pg_type.h:676
TypeName * argType
Definition: parsenodes.h:2703
#define ereport(elevel, rest)
Definition: elog.h:122
List * lappend(List *list, void *datum)
Definition: list.c:128
#define ANYARRAYOID
Definition: pg_type.h:684
void * palloc0(Size size)
Definition: mcxt.c:878
AclResult
Definition: acl.h:170
uintptr_t Datum
Definition: postgres.h:372
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1116
#define CHAROID
Definition: pg_type.h:296
#define InvalidOid
Definition: postgres_ext.h:36
#define NOTICE
Definition: elog.h:37
#define NULL
Definition: c.h:229
oidvector * buildoidvector(const Oid *oids, int n)
Definition: oid.c:167
#define lfirst(lc)
Definition: pg_list.h:106
static int list_length(const List *l)
Definition: pg_list.h:89
#define CharGetDatum(X)
Definition: postgres.h:422
void * palloc(Size size)
Definition: mcxt.c:849
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
Node * coerce_to_specific_type(ParseState *pstate, Node *node, Oid targetTypeId, const char *constructName)
#define CStringGetTextDatum(s)
Definition: builtins.h:91
FunctionParameterMode mode
Definition: parsenodes.h:2704
AclResult pg_type_aclcheck(Oid type_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:4517
Oid typeTypeId(Type tp)
Definition: parse_type.c:571
List * p_rtable
Definition: parse_node.h:168
void IsThereFunctionInNamespace ( const char *  proname,
int  pronargs,
oidvector proargtypes,
Oid  nspOid 
)

Definition at line 2034 of file functioncmds.c.

References CStringGetDatum, ereport, errcode(), errmsg(), ERROR, funcname_signature_string(), get_namespace_name(), NIL, ObjectIdGetDatum, PointerGetDatum, PROCNAMEARGSNSP, SearchSysCacheExists3, and oidvector::values.

Referenced by AlterObjectNamespace_internal(), and AlterObjectRename_internal().

2036 {
2037  /* check for duplicate name (more friendly than unique-index failure) */
2039  CStringGetDatum(proname),
2040  PointerGetDatum(proargtypes),
2041  ObjectIdGetDatum(nspOid)))
2042  ereport(ERROR,
2043  (errcode(ERRCODE_DUPLICATE_FUNCTION),
2044  errmsg("function %s already exists in schema \"%s\"",
2045  funcname_signature_string(proname, pronargs,
2046  NIL, proargtypes->values),
2047  get_namespace_name(nspOid))));
2048 }
#define NIL
Definition: pg_list.h:69
#define PointerGetDatum(X)
Definition: postgres.h:562
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
#define SearchSysCacheExists3(cacheId, key1, key2, key3)
Definition: syscache.h:174
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3006
Oid values[FLEXIBLE_ARRAY_MEMBER]
Definition: c.h:486
#define CStringGetDatum(X)
Definition: postgres.h:584
const char * funcname_signature_string(const char *funcname, int nargs, List *argnames, const Oid *argtypes)
Definition: parse_func.c:1852
#define ereport(elevel, rest)
Definition: elog.h:122
int errmsg(const char *fmt,...)
Definition: elog.c:797
void RemoveFunctionById ( Oid  funcOid)

Definition at line 1117 of file functioncmds.c.

References AGGFNOID, AggregateRelationId, CatalogTupleDelete(), elog, ERROR, GETSTRUCT, heap_close, heap_open(), HeapTupleIsValid, ObjectIdGetDatum, ProcedureRelationId, PROCOID, ReleaseSysCache(), RowExclusiveLock, SearchSysCache1, and HeapTupleData::t_self.

Referenced by doDeletion().

1118 {
1119  Relation relation;
1120  HeapTuple tup;
1121  bool isagg;
1122 
1123  /*
1124  * Delete the pg_proc tuple.
1125  */
1127 
1128  tup = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcOid));
1129  if (!HeapTupleIsValid(tup)) /* should not happen */
1130  elog(ERROR, "cache lookup failed for function %u", funcOid);
1131 
1132  isagg = ((Form_pg_proc) GETSTRUCT(tup))->proisagg;
1133 
1134  CatalogTupleDelete(relation, &tup->t_self);
1135 
1136  ReleaseSysCache(tup);
1137 
1138  heap_close(relation, RowExclusiveLock);
1139 
1140  /*
1141  * If there's a pg_aggregate tuple, delete that too.
1142  */
1143  if (isagg)
1144  {
1146 
1147  tup = SearchSysCache1(AGGFNOID, ObjectIdGetDatum(funcOid));
1148  if (!HeapTupleIsValid(tup)) /* should not happen */
1149  elog(ERROR, "cache lookup failed for pg_aggregate tuple for function %u", funcOid);
1150 
1151  CatalogTupleDelete(relation, &tup->t_self);
1152 
1153  ReleaseSysCache(tup);
1154 
1155  heap_close(relation, RowExclusiveLock);
1156  }
1157 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
#define ProcedureRelationId
Definition: pg_proc.h:33
void CatalogTupleDelete(Relation heapRel, ItemPointer tid)
Definition: indexing.c:255
#define heap_close(r, l)
Definition: heapam.h:97
#define SearchSysCache1(cacheId, key1)
Definition: syscache.h:152
#define AggregateRelationId
Definition: pg_aggregate.h:53
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
ItemPointerData t_self
Definition: htup.h:65
#define RowExclusiveLock
Definition: lockdefs.h:38
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1116
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1287
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:83
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define elog
Definition: elog.h:219
void SetFunctionArgType ( Oid  funcOid,
int  argIndex,
Oid  newArgType 
)

Definition at line 1345 of file functioncmds.c.

References CatalogTupleUpdate(), elog, ERROR, GETSTRUCT, heap_close, heap_open(), HeapTupleIsValid, ObjectIdGetDatum, OPAQUEOID, ProcedureRelationId, PROCOID, RowExclusiveLock, SearchSysCacheCopy1, and HeapTupleData::t_self.

Referenced by findTypeInputFunction(), and findTypeOutputFunction().

1346 {
1347  Relation pg_proc_rel;
1348  HeapTuple tup;
1349  Form_pg_proc procForm;
1350 
1352 
1353  tup = SearchSysCacheCopy1(PROCOID, ObjectIdGetDatum(funcOid));
1354  if (!HeapTupleIsValid(tup)) /* should not happen */
1355  elog(ERROR, "cache lookup failed for function %u", funcOid);
1356  procForm = (Form_pg_proc) GETSTRUCT(tup);
1357 
1358  if (argIndex < 0 || argIndex >= procForm->pronargs ||
1359  procForm->proargtypes.values[argIndex] != OPAQUEOID)
1360  elog(ERROR, "function %u doesn't take OPAQUE", funcOid);
1361 
1362  /* okay to overwrite copied tuple */
1363  procForm->proargtypes.values[argIndex] = newArgType;
1364 
1365  /* update the catalog and its indexes */
1366  CatalogTupleUpdate(pg_proc_rel, &tup->t_self, tup);
1367 
1368  heap_close(pg_proc_rel, RowExclusiveLock);
1369 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
#define ProcedureRelationId
Definition: pg_proc.h:33
#define heap_close(r, l)
Definition: heapam.h:97
#define OPAQUEOID
Definition: pg_type.h:696
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
ItemPointerData t_self
Definition: htup.h:65
#define RowExclusiveLock
Definition: lockdefs.h:38
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1287
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:83
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
Definition: indexing.c:210
#define SearchSysCacheCopy1(cacheId, key1)
Definition: syscache.h:161
#define elog
Definition: elog.h:219
void SetFunctionReturnType ( Oid  funcOid,
Oid  newRetType 
)

Definition at line 1313 of file functioncmds.c.

References CatalogTupleUpdate(), elog, ERROR, GETSTRUCT, heap_close, heap_open(), HeapTupleIsValid, ObjectIdGetDatum, OPAQUEOID, ProcedureRelationId, PROCOID, RowExclusiveLock, SearchSysCacheCopy1, and HeapTupleData::t_self.

Referenced by CreateProceduralLanguage(), CreateTrigger(), and DefineType().

1314 {
1315  Relation pg_proc_rel;
1316  HeapTuple tup;
1317  Form_pg_proc procForm;
1318 
1320 
1321  tup = SearchSysCacheCopy1(PROCOID, ObjectIdGetDatum(funcOid));
1322  if (!HeapTupleIsValid(tup)) /* should not happen */
1323  elog(ERROR, "cache lookup failed for function %u", funcOid);
1324  procForm = (Form_pg_proc) GETSTRUCT(tup);
1325 
1326  if (procForm->prorettype != OPAQUEOID) /* caller messed up */
1327  elog(ERROR, "function %u doesn't return OPAQUE", funcOid);
1328 
1329  /* okay to overwrite copied tuple */
1330  procForm->prorettype = newRetType;
1331 
1332  /* update the catalog and its indexes */
1333  CatalogTupleUpdate(pg_proc_rel, &tup->t_self, tup);
1334 
1335  heap_close(pg_proc_rel, RowExclusiveLock);
1336 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
#define ProcedureRelationId
Definition: pg_proc.h:33
#define heap_close(r, l)
Definition: heapam.h:97
#define OPAQUEOID
Definition: pg_type.h:696
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
ItemPointerData t_self
Definition: htup.h:65
#define RowExclusiveLock
Definition: lockdefs.h:38
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1287
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:83
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
Definition: indexing.c:210
#define SearchSysCacheCopy1(cacheId, key1)
Definition: syscache.h:161
#define elog
Definition: elog.h:219
static ArrayType* update_proconfig_value ( ArrayType a,
List set_items 
)
static

Definition at line 575 of file functioncmds.c.

References castNode, ExtractSetVariableArgs(), GUCArrayAdd(), GUCArrayDelete(), VariableSetStmt::kind, lfirst, VariableSetStmt::name, NULL, and VAR_RESET_ALL.

Referenced by AlterFunction().

576 {
577  ListCell *l;
578 
579  foreach(l, set_items)
580  {
582 
583  if (sstmt->kind == VAR_RESET_ALL)
584  a = NULL;
585  else
586  {
587  char *valuestr = ExtractSetVariableArgs(sstmt);
588 
589  if (valuestr)
590  a = GUCArrayAdd(a, sstmt->name, valuestr);
591  else /* RESET */
592  a = GUCArrayDelete(a, sstmt->name);
593  }
594  }
595 
596  return a;
597 }
ArrayType * GUCArrayAdd(ArrayType *array, const char *name, const char *value)
Definition: guc.c:9445
VariableSetKind kind
Definition: parsenodes.h:1918
#define castNode(_type_, nodeptr)
Definition: nodes.h:575
ArrayType * GUCArrayDelete(ArrayType *array, const char *name)
Definition: guc.c:9525
#define NULL
Definition: c.h:229
#define lfirst(lc)
Definition: pg_list.h:106
char * ExtractSetVariableArgs(VariableSetStmt *stmt)
Definition: guc.c:7385