PostgreSQL Source Code  git master
aggregatecmds.c File Reference
#include "postgres.h"
#include "access/htup_details.h"
#include "catalog/dependency.h"
#include "catalog/pg_aggregate.h"
#include "catalog/pg_namespace.h"
#include "catalog/pg_proc.h"
#include "catalog/pg_type.h"
#include "commands/alter.h"
#include "commands/defrem.h"
#include "miscadmin.h"
#include "parser/parse_func.h"
#include "parser/parse_type.h"
#include "utils/acl.h"
#include "utils/builtins.h"
#include "utils/lsyscache.h"
#include "utils/syscache.h"
Include dependency graph for aggregatecmds.c:

Go to the source code of this file.

Functions

static char extractModify (DefElem *defel)
 
ObjectAddress DefineAggregate (ParseState *pstate, List *name, List *args, bool oldstyle, List *parameters, bool replace)
 

Function Documentation

◆ DefineAggregate()

ObjectAddress DefineAggregate ( ParseState pstate,
List name,
List args,
bool  oldstyle,
List parameters,
bool  replace 
)

Definition at line 57 of file aggregatecmds.c.

63 {
64  char *aggName;
65  Oid aggNamespace;
66  AclResult aclresult;
67  char aggKind = AGGKIND_NORMAL;
68  List *transfuncName = NIL;
69  List *finalfuncName = NIL;
70  List *combinefuncName = NIL;
71  List *serialfuncName = NIL;
72  List *deserialfuncName = NIL;
73  List *mtransfuncName = NIL;
74  List *minvtransfuncName = NIL;
75  List *mfinalfuncName = NIL;
76  bool finalfuncExtraArgs = false;
77  bool mfinalfuncExtraArgs = false;
78  char finalfuncModify = 0;
79  char mfinalfuncModify = 0;
80  List *sortoperatorName = NIL;
81  TypeName *baseType = NULL;
82  TypeName *transType = NULL;
83  TypeName *mtransType = NULL;
84  int32 transSpace = 0;
85  int32 mtransSpace = 0;
86  char *initval = NULL;
87  char *minitval = NULL;
88  char *parallel = NULL;
89  int numArgs;
90  int numDirectArgs = 0;
91  oidvector *parameterTypes;
92  ArrayType *allParameterTypes;
93  ArrayType *parameterModes;
94  ArrayType *parameterNames;
95  List *parameterDefaults;
96  Oid variadicArgType;
97  Oid transTypeId;
98  Oid mtransTypeId = InvalidOid;
99  char transTypeType;
100  char mtransTypeType = 0;
101  char proparallel = PROPARALLEL_UNSAFE;
102  ListCell *pl;
103 
104  /* Convert list of names to a name and namespace */
105  aggNamespace = QualifiedNameGetCreationNamespace(name, &aggName);
106 
107  /* Check we have creation rights in target namespace */
108  aclresult = object_aclcheck(NamespaceRelationId, aggNamespace, GetUserId(), ACL_CREATE);
109  if (aclresult != ACLCHECK_OK)
110  aclcheck_error(aclresult, OBJECT_SCHEMA,
111  get_namespace_name(aggNamespace));
112 
113  /* Deconstruct the output of the aggr_args grammar production */
114  if (!oldstyle)
115  {
116  Assert(list_length(args) == 2);
117  numDirectArgs = intVal(lsecond(args));
118  if (numDirectArgs >= 0)
119  aggKind = AGGKIND_ORDERED_SET;
120  else
121  numDirectArgs = 0;
123  }
124 
125  /* Examine aggregate's definition clauses */
126  foreach(pl, parameters)
127  {
128  DefElem *defel = lfirst_node(DefElem, pl);
129 
130  /*
131  * sfunc1, stype1, and initcond1 are accepted as obsolete spellings
132  * for sfunc, stype, initcond.
133  */
134  if (strcmp(defel->defname, "sfunc") == 0)
135  transfuncName = defGetQualifiedName(defel);
136  else if (strcmp(defel->defname, "sfunc1") == 0)
137  transfuncName = defGetQualifiedName(defel);
138  else if (strcmp(defel->defname, "finalfunc") == 0)
139  finalfuncName = defGetQualifiedName(defel);
140  else if (strcmp(defel->defname, "combinefunc") == 0)
141  combinefuncName = defGetQualifiedName(defel);
142  else if (strcmp(defel->defname, "serialfunc") == 0)
143  serialfuncName = defGetQualifiedName(defel);
144  else if (strcmp(defel->defname, "deserialfunc") == 0)
145  deserialfuncName = defGetQualifiedName(defel);
146  else if (strcmp(defel->defname, "msfunc") == 0)
147  mtransfuncName = defGetQualifiedName(defel);
148  else if (strcmp(defel->defname, "minvfunc") == 0)
149  minvtransfuncName = defGetQualifiedName(defel);
150  else if (strcmp(defel->defname, "mfinalfunc") == 0)
151  mfinalfuncName = defGetQualifiedName(defel);
152  else if (strcmp(defel->defname, "finalfunc_extra") == 0)
153  finalfuncExtraArgs = defGetBoolean(defel);
154  else if (strcmp(defel->defname, "mfinalfunc_extra") == 0)
155  mfinalfuncExtraArgs = defGetBoolean(defel);
156  else if (strcmp(defel->defname, "finalfunc_modify") == 0)
157  finalfuncModify = extractModify(defel);
158  else if (strcmp(defel->defname, "mfinalfunc_modify") == 0)
159  mfinalfuncModify = extractModify(defel);
160  else if (strcmp(defel->defname, "sortop") == 0)
161  sortoperatorName = defGetQualifiedName(defel);
162  else if (strcmp(defel->defname, "basetype") == 0)
163  baseType = defGetTypeName(defel);
164  else if (strcmp(defel->defname, "hypothetical") == 0)
165  {
166  if (defGetBoolean(defel))
167  {
168  if (aggKind == AGGKIND_NORMAL)
169  ereport(ERROR,
170  (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
171  errmsg("only ordered-set aggregates can be hypothetical")));
172  aggKind = AGGKIND_HYPOTHETICAL;
173  }
174  }
175  else if (strcmp(defel->defname, "stype") == 0)
176  transType = defGetTypeName(defel);
177  else if (strcmp(defel->defname, "stype1") == 0)
178  transType = defGetTypeName(defel);
179  else if (strcmp(defel->defname, "sspace") == 0)
180  transSpace = defGetInt32(defel);
181  else if (strcmp(defel->defname, "mstype") == 0)
182  mtransType = defGetTypeName(defel);
183  else if (strcmp(defel->defname, "msspace") == 0)
184  mtransSpace = defGetInt32(defel);
185  else if (strcmp(defel->defname, "initcond") == 0)
186  initval = defGetString(defel);
187  else if (strcmp(defel->defname, "initcond1") == 0)
188  initval = defGetString(defel);
189  else if (strcmp(defel->defname, "minitcond") == 0)
190  minitval = defGetString(defel);
191  else if (strcmp(defel->defname, "parallel") == 0)
192  parallel = defGetString(defel);
193  else
195  (errcode(ERRCODE_SYNTAX_ERROR),
196  errmsg("aggregate attribute \"%s\" not recognized",
197  defel->defname)));
198  }
199 
200  /*
201  * make sure we have our required definitions
202  */
203  if (transType == NULL)
204  ereport(ERROR,
205  (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
206  errmsg("aggregate stype must be specified")));
207  if (transfuncName == NIL)
208  ereport(ERROR,
209  (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
210  errmsg("aggregate sfunc must be specified")));
211 
212  /*
213  * if mtransType is given, mtransfuncName and minvtransfuncName must be as
214  * well; if not, then none of the moving-aggregate options should have
215  * been given.
216  */
217  if (mtransType != NULL)
218  {
219  if (mtransfuncName == NIL)
220  ereport(ERROR,
221  (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
222  errmsg("aggregate msfunc must be specified when mstype is specified")));
223  if (minvtransfuncName == NIL)
224  ereport(ERROR,
225  (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
226  errmsg("aggregate minvfunc must be specified when mstype is specified")));
227  }
228  else
229  {
230  if (mtransfuncName != NIL)
231  ereport(ERROR,
232  (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
233  errmsg("aggregate msfunc must not be specified without mstype")));
234  if (minvtransfuncName != NIL)
235  ereport(ERROR,
236  (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
237  errmsg("aggregate minvfunc must not be specified without mstype")));
238  if (mfinalfuncName != NIL)
239  ereport(ERROR,
240  (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
241  errmsg("aggregate mfinalfunc must not be specified without mstype")));
242  if (mtransSpace != 0)
243  ereport(ERROR,
244  (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
245  errmsg("aggregate msspace must not be specified without mstype")));
246  if (minitval != NULL)
247  ereport(ERROR,
248  (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
249  errmsg("aggregate minitcond must not be specified without mstype")));
250  }
251 
252  /*
253  * Default values for modify flags can only be determined once we know the
254  * aggKind.
255  */
256  if (finalfuncModify == 0)
257  finalfuncModify = (aggKind == AGGKIND_NORMAL) ? AGGMODIFY_READ_ONLY : AGGMODIFY_READ_WRITE;
258  if (mfinalfuncModify == 0)
259  mfinalfuncModify = (aggKind == AGGKIND_NORMAL) ? AGGMODIFY_READ_ONLY : AGGMODIFY_READ_WRITE;
260 
261  /*
262  * look up the aggregate's input datatype(s).
263  */
264  if (oldstyle)
265  {
266  /*
267  * Old style: use basetype parameter. This supports aggregates of
268  * zero or one input, with input type ANY meaning zero inputs.
269  *
270  * Historically we allowed the command to look like basetype = 'ANY'
271  * so we must do a case-insensitive comparison for the name ANY. Ugh.
272  */
273  Oid aggArgTypes[1];
274 
275  if (baseType == NULL)
276  ereport(ERROR,
277  (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
278  errmsg("aggregate input type must be specified")));
279 
280  if (pg_strcasecmp(TypeNameToString(baseType), "ANY") == 0)
281  {
282  numArgs = 0;
283  aggArgTypes[0] = InvalidOid;
284  }
285  else
286  {
287  numArgs = 1;
288  aggArgTypes[0] = typenameTypeId(NULL, baseType);
289  }
290  parameterTypes = buildoidvector(aggArgTypes, numArgs);
291  allParameterTypes = NULL;
292  parameterModes = NULL;
293  parameterNames = NULL;
294  parameterDefaults = NIL;
295  variadicArgType = InvalidOid;
296  }
297  else
298  {
299  /*
300  * New style: args is a list of FunctionParameters (possibly zero of
301  * 'em). We share functioncmds.c's code for processing them.
302  */
303  Oid requiredResultType;
304 
305  if (baseType != NULL)
306  ereport(ERROR,
307  (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
308  errmsg("basetype is redundant with aggregate input type specification")));
309 
310  numArgs = list_length(args);
312  args,
313  InvalidOid,
315  &parameterTypes,
316  NULL,
317  &allParameterTypes,
318  &parameterModes,
319  &parameterNames,
320  NULL,
321  &parameterDefaults,
322  &variadicArgType,
323  &requiredResultType);
324  /* Parameter defaults are not currently allowed by the grammar */
325  Assert(parameterDefaults == NIL);
326  /* There shouldn't have been any OUT parameters, either */
327  Assert(requiredResultType == InvalidOid);
328  }
329 
330  /*
331  * look up the aggregate's transtype.
332  *
333  * transtype can't be a pseudo-type, since we need to be able to store
334  * values of the transtype. However, we can allow polymorphic transtype
335  * in some cases (AggregateCreate will check). Also, we allow "internal"
336  * for functions that want to pass pointers to private data structures;
337  * but allow that only to superusers, since you could crash the system (or
338  * worse) by connecting up incompatible internal-using functions in an
339  * aggregate.
340  */
341  transTypeId = typenameTypeId(NULL, transType);
342  transTypeType = get_typtype(transTypeId);
343  if (transTypeType == TYPTYPE_PSEUDO &&
344  !IsPolymorphicType(transTypeId))
345  {
346  if (transTypeId == INTERNALOID && superuser())
347  /* okay */ ;
348  else
349  ereport(ERROR,
350  (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
351  errmsg("aggregate transition data type cannot be %s",
352  format_type_be(transTypeId))));
353  }
354 
355  if (serialfuncName && deserialfuncName)
356  {
357  /*
358  * Serialization is only needed/allowed for transtype INTERNAL.
359  */
360  if (transTypeId != INTERNALOID)
361  ereport(ERROR,
362  (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
363  errmsg("serialization functions may be specified only when the aggregate transition data type is %s",
364  format_type_be(INTERNALOID))));
365  }
366  else if (serialfuncName || deserialfuncName)
367  {
368  /*
369  * Cannot specify one function without the other.
370  */
371  ereport(ERROR,
372  (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
373  errmsg("must specify both or neither of serialization and deserialization functions")));
374  }
375 
376  /*
377  * If a moving-aggregate transtype is specified, look that up. Same
378  * restrictions as for transtype.
379  */
380  if (mtransType)
381  {
382  mtransTypeId = typenameTypeId(NULL, mtransType);
383  mtransTypeType = get_typtype(mtransTypeId);
384  if (mtransTypeType == TYPTYPE_PSEUDO &&
385  !IsPolymorphicType(mtransTypeId))
386  {
387  if (mtransTypeId == INTERNALOID && superuser())
388  /* okay */ ;
389  else
390  ereport(ERROR,
391  (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
392  errmsg("aggregate transition data type cannot be %s",
393  format_type_be(mtransTypeId))));
394  }
395  }
396 
397  /*
398  * If we have an initval, and it's not for a pseudotype (particularly a
399  * polymorphic type), make sure it's acceptable to the type's input
400  * function. We will store the initval as text, because the input
401  * function isn't necessarily immutable (consider "now" for timestamp),
402  * and we want to use the runtime not creation-time interpretation of the
403  * value. However, if it's an incorrect value it seems much more
404  * user-friendly to complain at CREATE AGGREGATE time.
405  */
406  if (initval && transTypeType != TYPTYPE_PSEUDO)
407  {
408  Oid typinput,
409  typioparam;
410 
411  getTypeInputInfo(transTypeId, &typinput, &typioparam);
412  (void) OidInputFunctionCall(typinput, initval, typioparam, -1);
413  }
414 
415  /*
416  * Likewise for moving-aggregate initval.
417  */
418  if (minitval && mtransTypeType != TYPTYPE_PSEUDO)
419  {
420  Oid typinput,
421  typioparam;
422 
423  getTypeInputInfo(mtransTypeId, &typinput, &typioparam);
424  (void) OidInputFunctionCall(typinput, minitval, typioparam, -1);
425  }
426 
427  if (parallel)
428  {
429  if (strcmp(parallel, "safe") == 0)
430  proparallel = PROPARALLEL_SAFE;
431  else if (strcmp(parallel, "restricted") == 0)
432  proparallel = PROPARALLEL_RESTRICTED;
433  else if (strcmp(parallel, "unsafe") == 0)
434  proparallel = PROPARALLEL_UNSAFE;
435  else
436  ereport(ERROR,
437  (errcode(ERRCODE_SYNTAX_ERROR),
438  errmsg("parameter \"parallel\" must be SAFE, RESTRICTED, or UNSAFE")));
439  }
440 
441  /*
442  * Most of the argument-checking is done inside of AggregateCreate
443  */
444  return AggregateCreate(aggName, /* aggregate name */
445  aggNamespace, /* namespace */
446  replace,
447  aggKind,
448  numArgs,
449  numDirectArgs,
450  parameterTypes,
451  PointerGetDatum(allParameterTypes),
452  PointerGetDatum(parameterModes),
453  PointerGetDatum(parameterNames),
454  parameterDefaults,
455  variadicArgType,
456  transfuncName, /* step function name */
457  finalfuncName, /* final function name */
458  combinefuncName, /* combine function name */
459  serialfuncName, /* serial function name */
460  deserialfuncName, /* deserial function name */
461  mtransfuncName, /* fwd trans function name */
462  minvtransfuncName, /* inv trans function name */
463  mfinalfuncName, /* final function name */
464  finalfuncExtraArgs,
465  mfinalfuncExtraArgs,
466  finalfuncModify,
467  mfinalfuncModify,
468  sortoperatorName, /* sort operator name */
469  transTypeId, /* transition data type */
470  transSpace, /* transition space */
471  mtransTypeId, /* transition data type */
472  mtransSpace, /* transition space */
473  initval, /* initial condition */
474  minitval, /* initial condition */
475  proparallel); /* parallel safe? */
476 }
AclResult
Definition: acl.h:181
@ ACLCHECK_OK
Definition: acl.h:182
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:3477
AclResult object_aclcheck(Oid classid, Oid objectid, Oid roleid, AclMode mode)
Definition: aclchk.c:4570
static char extractModify(DefElem *defel)
signed int int32
Definition: c.h:430
int32 defGetInt32(DefElem *def)
Definition: define.c:163
bool defGetBoolean(DefElem *def)
Definition: define.c:108
List * defGetQualifiedName(DefElem *def)
Definition: define.c:253
char * defGetString(DefElem *def)
Definition: define.c:49
TypeName * defGetTypeName(DefElem *def)
Definition: define.c:285
int errcode(int sqlerrcode)
Definition: elog.c:695
int errmsg(const char *fmt,...)
Definition: elog.c:906
#define WARNING
Definition: elog.h:32
#define ERROR
Definition: elog.h:35
#define ereport(elevel,...)
Definition: elog.h:145
const char * name
Definition: encode.c:561
Datum OidInputFunctionCall(Oid functionId, char *str, Oid typioparam, int32 typmod)
Definition: fmgr.c:1630
char * format_type_be(Oid type_oid)
Definition: format_type.c:339
void interpret_function_parameter_list(ParseState *pstate, List *parameters, Oid languageOid, ObjectType objtype, oidvector **parameterTypes, List **parameterTypes_list, ArrayType **allParameterTypes, ArrayType **parameterModes, ArrayType **parameterNames, List **inParameterNames_list, List **parameterDefaults, Oid *variadicArgType, Oid *requiredResultType)
Definition: functioncmds.c:188
Assert(fmt[strlen(fmt) - 1] !='\n')
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3331
void getTypeInputInfo(Oid type, Oid *typInput, Oid *typIOParam)
Definition: lsyscache.c:2832
char get_typtype(Oid typid)
Definition: lsyscache.c:2587
Oid GetUserId(void)
Definition: miscinit.c:497
Oid QualifiedNameGetCreationNamespace(List *names, char **objname_p)
Definition: namespace.c:3038
oidvector * buildoidvector(const Oid *oids, int n)
Definition: oid.c:167
char * TypeNameToString(const TypeName *typeName)
Definition: parse_type.c:478
Oid typenameTypeId(ParseState *pstate, const TypeName *typeName)
Definition: parse_type.c:291
@ OBJECT_AGGREGATE
Definition: parsenodes.h:1861
@ OBJECT_SCHEMA
Definition: parsenodes.h:1896
#define ACL_CREATE
Definition: parsenodes.h:92
ObjectAddress AggregateCreate(const char *aggName, Oid aggNamespace, bool replace, char aggKind, int numArgs, int numDirectArgs, oidvector *parameterTypes, Datum allParameterTypes, Datum parameterModes, Datum parameterNames, List *parameterDefaults, Oid variadicArgType, List *aggtransfnName, List *aggfinalfnName, List *aggcombinefnName, List *aggserialfnName, List *aggdeserialfnName, List *aggmtransfnName, List *aggminvtransfnName, List *aggmfinalfnName, bool finalfnExtraArgs, bool mfinalfnExtraArgs, char finalfnModify, char mfinalfnModify, List *aggsortopName, Oid aggTransType, int32 aggTransSpace, Oid aggmTransType, int32 aggmTransSpace, const char *agginitval, const char *aggminitval, char proparallel)
Definition: pg_aggregate.c:46
#define lfirst_node(type, lc)
Definition: pg_list.h:174
static int list_length(const List *l)
Definition: pg_list.h:150
#define linitial_node(type, l)
Definition: pg_list.h:179
#define NIL
Definition: pg_list.h:66
#define lsecond(l)
Definition: pg_list.h:181
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:670
#define InvalidOid
Definition: postgres_ext.h:36
unsigned int Oid
Definition: postgres_ext.h:31
char * defname
Definition: parsenodes.h:775
Definition: pg_list.h:52
Definition: c.h:662
bool superuser(void)
Definition: superuser.c:46
#define intVal(v)
Definition: value.h:79

References ACL_CREATE, aclcheck_error(), ACLCHECK_OK, AggregateCreate(), generate_unaccent_rules::args, Assert(), buildoidvector(), defGetBoolean(), defGetInt32(), defGetQualifiedName(), defGetString(), defGetTypeName(), DefElem::defname, ereport, errcode(), errmsg(), ERROR, extractModify(), format_type_be(), get_namespace_name(), get_typtype(), getTypeInputInfo(), GetUserId(), interpret_function_parameter_list(), intVal, InvalidOid, lfirst_node, linitial_node, list_length(), lsecond, name, NIL, object_aclcheck(), OBJECT_AGGREGATE, OBJECT_SCHEMA, OidInputFunctionCall(), pg_strcasecmp(), PointerGetDatum(), QualifiedNameGetCreationNamespace(), superuser(), TypeNameToString(), typenameTypeId(), and WARNING.

Referenced by ProcessUtilitySlow().

◆ extractModify()

static char extractModify ( DefElem defel)
static

Definition at line 482 of file aggregatecmds.c.

483 {
484  char *val = defGetString(defel);
485 
486  if (strcmp(val, "read_only") == 0)
487  return AGGMODIFY_READ_ONLY;
488  if (strcmp(val, "shareable") == 0)
489  return AGGMODIFY_SHAREABLE;
490  if (strcmp(val, "read_write") == 0)
491  return AGGMODIFY_READ_WRITE;
492  ereport(ERROR,
493  (errcode(ERRCODE_SYNTAX_ERROR),
494  errmsg("parameter \"%s\" must be READ_ONLY, SHAREABLE, or READ_WRITE",
495  defel->defname)));
496  return 0; /* keep compiler quiet */
497 }
long val
Definition: informix.c:664

References defGetString(), DefElem::defname, ereport, errcode(), errmsg(), ERROR, and val.

Referenced by DefineAggregate().