PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
aggregatecmds.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * aggregatecmds.c
4  *
5  * Routines for aggregate-manipulation commands
6  *
7  * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  *
11  * IDENTIFICATION
12  * src/backend/commands/aggregatecmds.c
13  *
14  * DESCRIPTION
15  * The "DefineFoo" routines take the parse tree and pick out the
16  * appropriate arguments/flags, passing the results to the
17  * corresponding "FooDefine" routines (in src/catalog) that do
18  * the actual catalog-munging. These routines also verify permission
19  * of the user to execute the command.
20  *
21  *-------------------------------------------------------------------------
22  */
23 #include "postgres.h"
24 
25 #include "access/htup_details.h"
26 #include "catalog/dependency.h"
27 #include "catalog/indexing.h"
28 #include "catalog/pg_aggregate.h"
30 #include "catalog/pg_proc.h"
31 #include "catalog/pg_type.h"
32 #include "commands/alter.h"
33 #include "commands/defrem.h"
34 #include "miscadmin.h"
35 #include "parser/parse_func.h"
36 #include "parser/parse_type.h"
37 #include "utils/acl.h"
38 #include "utils/builtins.h"
39 #include "utils/lsyscache.h"
40 #include "utils/syscache.h"
41 
42 
43 static char extractModify(DefElem *defel);
44 
45 
46 /*
47  * DefineAggregate
48  *
49  * "oldstyle" signals the old (pre-8.2) style where the aggregate input type
50  * is specified by a BASETYPE element in the parameters. Otherwise,
51  * "args" is a pair, whose first element is a list of FunctionParameter structs
52  * defining the agg's arguments (both direct and aggregated), and whose second
53  * element is an Integer node with the number of direct args, or -1 if this
54  * isn't an ordered-set aggregate.
55  * "parameters" is a list of DefElem representing the agg's definition clauses.
56  */
58 DefineAggregate(ParseState *pstate, List *name, List *args, bool oldstyle, List *parameters)
59 {
60  char *aggName;
61  Oid aggNamespace;
62  AclResult aclresult;
63  char aggKind = AGGKIND_NORMAL;
64  List *transfuncName = NIL;
65  List *finalfuncName = NIL;
66  List *combinefuncName = NIL;
67  List *serialfuncName = NIL;
68  List *deserialfuncName = NIL;
69  List *mtransfuncName = NIL;
70  List *minvtransfuncName = NIL;
71  List *mfinalfuncName = NIL;
72  bool finalfuncExtraArgs = false;
73  bool mfinalfuncExtraArgs = false;
74  char finalfuncModify = 0;
75  char mfinalfuncModify = 0;
76  List *sortoperatorName = NIL;
77  TypeName *baseType = NULL;
78  TypeName *transType = NULL;
79  TypeName *mtransType = NULL;
80  int32 transSpace = 0;
81  int32 mtransSpace = 0;
82  char *initval = NULL;
83  char *minitval = NULL;
84  char *parallel = NULL;
85  int numArgs;
86  int numDirectArgs = 0;
87  oidvector *parameterTypes;
88  ArrayType *allParameterTypes;
89  ArrayType *parameterModes;
90  ArrayType *parameterNames;
91  List *parameterDefaults;
92  Oid variadicArgType;
93  Oid transTypeId;
94  Oid mtransTypeId = InvalidOid;
95  char transTypeType;
96  char mtransTypeType = 0;
97  char proparallel = PROPARALLEL_UNSAFE;
98  ListCell *pl;
99 
100  /* Convert list of names to a name and namespace */
101  aggNamespace = QualifiedNameGetCreationNamespace(name, &aggName);
102 
103  /* Check we have creation rights in target namespace */
104  aclresult = pg_namespace_aclcheck(aggNamespace, GetUserId(), ACL_CREATE);
105  if (aclresult != ACLCHECK_OK)
107  get_namespace_name(aggNamespace));
108 
109  /* Deconstruct the output of the aggr_args grammar production */
110  if (!oldstyle)
111  {
112  Assert(list_length(args) == 2);
113  numDirectArgs = intVal(lsecond(args));
114  if (numDirectArgs >= 0)
115  aggKind = AGGKIND_ORDERED_SET;
116  else
117  numDirectArgs = 0;
118  args = linitial_node(List, args);
119  }
120 
121  /* Examine aggregate's definition clauses */
122  foreach(pl, parameters)
123  {
124  DefElem *defel = lfirst_node(DefElem, pl);
125 
126  /*
127  * sfunc1, stype1, and initcond1 are accepted as obsolete spellings
128  * for sfunc, stype, initcond.
129  */
130  if (pg_strcasecmp(defel->defname, "sfunc") == 0)
131  transfuncName = defGetQualifiedName(defel);
132  else if (pg_strcasecmp(defel->defname, "sfunc1") == 0)
133  transfuncName = defGetQualifiedName(defel);
134  else if (pg_strcasecmp(defel->defname, "finalfunc") == 0)
135  finalfuncName = defGetQualifiedName(defel);
136  else if (pg_strcasecmp(defel->defname, "combinefunc") == 0)
137  combinefuncName = defGetQualifiedName(defel);
138  else if (pg_strcasecmp(defel->defname, "serialfunc") == 0)
139  serialfuncName = defGetQualifiedName(defel);
140  else if (pg_strcasecmp(defel->defname, "deserialfunc") == 0)
141  deserialfuncName = defGetQualifiedName(defel);
142  else if (pg_strcasecmp(defel->defname, "msfunc") == 0)
143  mtransfuncName = defGetQualifiedName(defel);
144  else if (pg_strcasecmp(defel->defname, "minvfunc") == 0)
145  minvtransfuncName = defGetQualifiedName(defel);
146  else if (pg_strcasecmp(defel->defname, "mfinalfunc") == 0)
147  mfinalfuncName = defGetQualifiedName(defel);
148  else if (pg_strcasecmp(defel->defname, "finalfunc_extra") == 0)
149  finalfuncExtraArgs = defGetBoolean(defel);
150  else if (pg_strcasecmp(defel->defname, "mfinalfunc_extra") == 0)
151  mfinalfuncExtraArgs = defGetBoolean(defel);
152  else if (pg_strcasecmp(defel->defname, "finalfunc_modify") == 0)
153  finalfuncModify = extractModify(defel);
154  else if (pg_strcasecmp(defel->defname, "mfinalfunc_modify") == 0)
155  mfinalfuncModify = extractModify(defel);
156  else if (pg_strcasecmp(defel->defname, "sortop") == 0)
157  sortoperatorName = defGetQualifiedName(defel);
158  else if (pg_strcasecmp(defel->defname, "basetype") == 0)
159  baseType = defGetTypeName(defel);
160  else if (pg_strcasecmp(defel->defname, "hypothetical") == 0)
161  {
162  if (defGetBoolean(defel))
163  {
164  if (aggKind == AGGKIND_NORMAL)
165  ereport(ERROR,
166  (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
167  errmsg("only ordered-set aggregates can be hypothetical")));
168  aggKind = AGGKIND_HYPOTHETICAL;
169  }
170  }
171  else if (pg_strcasecmp(defel->defname, "stype") == 0)
172  transType = defGetTypeName(defel);
173  else if (pg_strcasecmp(defel->defname, "stype1") == 0)
174  transType = defGetTypeName(defel);
175  else if (pg_strcasecmp(defel->defname, "sspace") == 0)
176  transSpace = defGetInt32(defel);
177  else if (pg_strcasecmp(defel->defname, "mstype") == 0)
178  mtransType = defGetTypeName(defel);
179  else if (pg_strcasecmp(defel->defname, "msspace") == 0)
180  mtransSpace = defGetInt32(defel);
181  else if (pg_strcasecmp(defel->defname, "initcond") == 0)
182  initval = defGetString(defel);
183  else if (pg_strcasecmp(defel->defname, "initcond1") == 0)
184  initval = defGetString(defel);
185  else if (pg_strcasecmp(defel->defname, "minitcond") == 0)
186  minitval = defGetString(defel);
187  else if (pg_strcasecmp(defel->defname, "parallel") == 0)
188  parallel = defGetString(defel);
189  else
191  (errcode(ERRCODE_SYNTAX_ERROR),
192  errmsg("aggregate attribute \"%s\" not recognized",
193  defel->defname)));
194  }
195 
196  /*
197  * make sure we have our required definitions
198  */
199  if (transType == NULL)
200  ereport(ERROR,
201  (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
202  errmsg("aggregate stype must be specified")));
203  if (transfuncName == NIL)
204  ereport(ERROR,
205  (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
206  errmsg("aggregate sfunc must be specified")));
207 
208  /*
209  * if mtransType is given, mtransfuncName and minvtransfuncName must be as
210  * well; if not, then none of the moving-aggregate options should have
211  * been given.
212  */
213  if (mtransType != NULL)
214  {
215  if (mtransfuncName == NIL)
216  ereport(ERROR,
217  (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
218  errmsg("aggregate msfunc must be specified when mstype is specified")));
219  if (minvtransfuncName == NIL)
220  ereport(ERROR,
221  (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
222  errmsg("aggregate minvfunc must be specified when mstype is specified")));
223  }
224  else
225  {
226  if (mtransfuncName != NIL)
227  ereport(ERROR,
228  (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
229  errmsg("aggregate msfunc must not be specified without mstype")));
230  if (minvtransfuncName != NIL)
231  ereport(ERROR,
232  (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
233  errmsg("aggregate minvfunc must not be specified without mstype")));
234  if (mfinalfuncName != NIL)
235  ereport(ERROR,
236  (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
237  errmsg("aggregate mfinalfunc must not be specified without mstype")));
238  if (mtransSpace != 0)
239  ereport(ERROR,
240  (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
241  errmsg("aggregate msspace must not be specified without mstype")));
242  if (minitval != NULL)
243  ereport(ERROR,
244  (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
245  errmsg("aggregate minitcond must not be specified without mstype")));
246  }
247 
248  /*
249  * Default values for modify flags can only be determined once we know the
250  * aggKind.
251  */
252  if (finalfuncModify == 0)
253  finalfuncModify = (aggKind == AGGKIND_NORMAL) ? AGGMODIFY_READ_ONLY : AGGMODIFY_READ_WRITE;
254  if (mfinalfuncModify == 0)
255  mfinalfuncModify = (aggKind == AGGKIND_NORMAL) ? AGGMODIFY_READ_ONLY : AGGMODIFY_READ_WRITE;
256 
257  /*
258  * look up the aggregate's input datatype(s).
259  */
260  if (oldstyle)
261  {
262  /*
263  * Old style: use basetype parameter. This supports aggregates of
264  * zero or one input, with input type ANY meaning zero inputs.
265  *
266  * Historically we allowed the command to look like basetype = 'ANY'
267  * so we must do a case-insensitive comparison for the name ANY. Ugh.
268  */
269  Oid aggArgTypes[1];
270 
271  if (baseType == NULL)
272  ereport(ERROR,
273  (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
274  errmsg("aggregate input type must be specified")));
275 
276  if (pg_strcasecmp(TypeNameToString(baseType), "ANY") == 0)
277  {
278  numArgs = 0;
279  aggArgTypes[0] = InvalidOid;
280  }
281  else
282  {
283  numArgs = 1;
284  aggArgTypes[0] = typenameTypeId(NULL, baseType);
285  }
286  parameterTypes = buildoidvector(aggArgTypes, numArgs);
287  allParameterTypes = NULL;
288  parameterModes = NULL;
289  parameterNames = NULL;
290  parameterDefaults = NIL;
291  variadicArgType = InvalidOid;
292  }
293  else
294  {
295  /*
296  * New style: args is a list of FunctionParameters (possibly zero of
297  * 'em). We share functioncmds.c's code for processing them.
298  */
299  Oid requiredResultType;
300 
301  if (baseType != NULL)
302  ereport(ERROR,
303  (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
304  errmsg("basetype is redundant with aggregate input type specification")));
305 
306  numArgs = list_length(args);
308  args,
309  InvalidOid,
310  true, /* is an aggregate */
311  &parameterTypes,
312  &allParameterTypes,
313  &parameterModes,
314  &parameterNames,
315  &parameterDefaults,
316  &variadicArgType,
317  &requiredResultType);
318  /* Parameter defaults are not currently allowed by the grammar */
319  Assert(parameterDefaults == NIL);
320  /* There shouldn't have been any OUT parameters, either */
321  Assert(requiredResultType == InvalidOid);
322  }
323 
324  /*
325  * look up the aggregate's transtype.
326  *
327  * transtype can't be a pseudo-type, since we need to be able to store
328  * values of the transtype. However, we can allow polymorphic transtype
329  * in some cases (AggregateCreate will check). Also, we allow "internal"
330  * for functions that want to pass pointers to private data structures;
331  * but allow that only to superusers, since you could crash the system (or
332  * worse) by connecting up incompatible internal-using functions in an
333  * aggregate.
334  */
335  transTypeId = typenameTypeId(NULL, transType);
336  transTypeType = get_typtype(transTypeId);
337  if (transTypeType == TYPTYPE_PSEUDO &&
338  !IsPolymorphicType(transTypeId))
339  {
340  if (transTypeId == INTERNALOID && superuser())
341  /* okay */ ;
342  else
343  ereport(ERROR,
344  (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
345  errmsg("aggregate transition data type cannot be %s",
346  format_type_be(transTypeId))));
347  }
348 
349  if (serialfuncName && deserialfuncName)
350  {
351  /*
352  * Serialization is only needed/allowed for transtype INTERNAL.
353  */
354  if (transTypeId != INTERNALOID)
355  ereport(ERROR,
356  (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
357  errmsg("serialization functions may be specified only when the aggregate transition data type is %s",
359  }
360  else if (serialfuncName || deserialfuncName)
361  {
362  /*
363  * Cannot specify one function without the other.
364  */
365  ereport(ERROR,
366  (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
367  errmsg("must specify both or neither of serialization and deserialization functions")));
368  }
369 
370  /*
371  * If a moving-aggregate transtype is specified, look that up. Same
372  * restrictions as for transtype.
373  */
374  if (mtransType)
375  {
376  mtransTypeId = typenameTypeId(NULL, mtransType);
377  mtransTypeType = get_typtype(mtransTypeId);
378  if (mtransTypeType == TYPTYPE_PSEUDO &&
379  !IsPolymorphicType(mtransTypeId))
380  {
381  if (mtransTypeId == INTERNALOID && superuser())
382  /* okay */ ;
383  else
384  ereport(ERROR,
385  (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
386  errmsg("aggregate transition data type cannot be %s",
387  format_type_be(mtransTypeId))));
388  }
389  }
390 
391  /*
392  * If we have an initval, and it's not for a pseudotype (particularly a
393  * polymorphic type), make sure it's acceptable to the type's input
394  * function. We will store the initval as text, because the input
395  * function isn't necessarily immutable (consider "now" for timestamp),
396  * and we want to use the runtime not creation-time interpretation of the
397  * value. However, if it's an incorrect value it seems much more
398  * user-friendly to complain at CREATE AGGREGATE time.
399  */
400  if (initval && transTypeType != TYPTYPE_PSEUDO)
401  {
402  Oid typinput,
403  typioparam;
404 
405  getTypeInputInfo(transTypeId, &typinput, &typioparam);
406  (void) OidInputFunctionCall(typinput, initval, typioparam, -1);
407  }
408 
409  /*
410  * Likewise for moving-aggregate initval.
411  */
412  if (minitval && mtransTypeType != TYPTYPE_PSEUDO)
413  {
414  Oid typinput,
415  typioparam;
416 
417  getTypeInputInfo(mtransTypeId, &typinput, &typioparam);
418  (void) OidInputFunctionCall(typinput, minitval, typioparam, -1);
419  }
420 
421  if (parallel)
422  {
423  if (pg_strcasecmp(parallel, "safe") == 0)
424  proparallel = PROPARALLEL_SAFE;
425  else if (pg_strcasecmp(parallel, "restricted") == 0)
426  proparallel = PROPARALLEL_RESTRICTED;
427  else if (pg_strcasecmp(parallel, "unsafe") == 0)
428  proparallel = PROPARALLEL_UNSAFE;
429  else
430  ereport(ERROR,
431  (errcode(ERRCODE_SYNTAX_ERROR),
432  errmsg("parameter \"parallel\" must be SAFE, RESTRICTED, or UNSAFE")));
433  }
434 
435  /*
436  * Most of the argument-checking is done inside of AggregateCreate
437  */
438  return AggregateCreate(aggName, /* aggregate name */
439  aggNamespace, /* namespace */
440  aggKind,
441  numArgs,
442  numDirectArgs,
443  parameterTypes,
444  PointerGetDatum(allParameterTypes),
445  PointerGetDatum(parameterModes),
446  PointerGetDatum(parameterNames),
447  parameterDefaults,
448  variadicArgType,
449  transfuncName, /* step function name */
450  finalfuncName, /* final function name */
451  combinefuncName, /* combine function name */
452  serialfuncName, /* serial function name */
453  deserialfuncName, /* deserial function name */
454  mtransfuncName, /* fwd trans function name */
455  minvtransfuncName, /* inv trans function name */
456  mfinalfuncName, /* final function name */
457  finalfuncExtraArgs,
458  mfinalfuncExtraArgs,
459  finalfuncModify,
460  mfinalfuncModify,
461  sortoperatorName, /* sort operator name */
462  transTypeId, /* transition data type */
463  transSpace, /* transition space */
464  mtransTypeId, /* transition data type */
465  mtransSpace, /* transition space */
466  initval, /* initial condition */
467  minitval, /* initial condition */
468  proparallel); /* parallel safe? */
469 }
470 
471 /*
472  * Convert the string form of [m]finalfunc_modify to the catalog representation
473  */
474 static char
476 {
477  char *val = defGetString(defel);
478 
479  if (strcmp(val, "read_only") == 0)
480  return AGGMODIFY_READ_ONLY;
481  if (strcmp(val, "sharable") == 0)
482  return AGGMODIFY_SHARABLE;
483  if (strcmp(val, "read_write") == 0)
484  return AGGMODIFY_READ_WRITE;
485  ereport(ERROR,
486  (errcode(ERRCODE_SYNTAX_ERROR),
487  errmsg("parameter \"%s\" must be READ_ONLY, SHARABLE, or READ_WRITE",
488  defel->defname)));
489  return 0; /* keep compiler quiet */
490 }
#define NIL
Definition: pg_list.h:69
Definition: c.h:472
#define AGGMODIFY_READ_ONLY
Definition: pg_aggregate.h:143
#define AGGMODIFY_READ_WRITE
Definition: pg_aggregate.h:145
ObjectAddress AggregateCreate(const char *aggName, Oid aggNamespace, 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:48
Oid GetUserId(void)
Definition: miscinit.c:284
Oid QualifiedNameGetCreationNamespace(List *names, char **objname_p)
Definition: namespace.c:2956
#define PointerGetDatum(X)
Definition: postgres.h:562
char * TypeNameToString(const TypeName *typeName)
Definition: parse_type.c:459
int32 defGetInt32(DefElem *def)
Definition: define.c:166
int errcode(int sqlerrcode)
Definition: elog.c:575
bool superuser(void)
Definition: superuser.c:47
char get_typtype(Oid typid)
Definition: lsyscache.c:2379
char * format_type_be(Oid type_oid)
Definition: format_type.c:94
#define PROPARALLEL_RESTRICTED
Definition: pg_proc.h:5543
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
unsigned int Oid
Definition: postgres_ext.h:31
#define linitial_node(type, l)
Definition: pg_list.h:114
AclResult pg_namespace_aclcheck(Oid nsp_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:4484
#define lsecond(l)
Definition: pg_list.h:116
signed int int32
Definition: c.h:246
ObjectAddress DefineAggregate(ParseState *pstate, List *name, List *args, bool oldstyle, List *parameters)
Definition: aggregatecmds.c:58
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
bool defGetBoolean(DefElem *def)
Definition: define.c:111
TypeName * defGetTypeName(DefElem *def)
Definition: define.c:255
#define ERROR
Definition: elog.h:43
#define ACL_CREATE
Definition: parsenodes.h:82
#define IsPolymorphicType(typid)
Definition: pg_type.h:745
char * defGetString(DefElem *def)
Definition: define.c:49
static char extractModify(DefElem *defel)
#define lfirst_node(type, lc)
Definition: pg_list.h:109
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3033
void aclcheck_error(AclResult aclerr, AclObjectKind objectkind, const char *objectname)
Definition: aclchk.c:3399
#define AGGKIND_NORMAL
Definition: pg_aggregate.h:128
#define AGGMODIFY_SHARABLE
Definition: pg_aggregate.h:144
#define PROPARALLEL_SAFE
Definition: pg_proc.h:5542
void getTypeInputInfo(Oid type, Oid *typInput, Oid *typIOParam)
Definition: lsyscache.c:2599
#define ereport(elevel, rest)
Definition: elog.h:122
#define PROPARALLEL_UNSAFE
Definition: pg_proc.h:5544
#define WARNING
Definition: elog.h:40
AclResult
Definition: acl.h:178
#define InvalidOid
Definition: postgres_ext.h:36
#define INTERNALOID
Definition: pg_type.h:698
#define Assert(condition)
Definition: c.h:681
oidvector * buildoidvector(const Oid *oids, int n)
Definition: oid.c:167
List * defGetQualifiedName(DefElem *def)
Definition: define.c:223
static int list_length(const List *l)
Definition: pg_list.h:89
const char * name
Definition: encode.c:521
#define TYPTYPE_PSEUDO
Definition: pg_type.h:724
#define intVal(v)
Definition: value.h:52
int errmsg(const char *fmt,...)
Definition: elog.c:797
char * defname
Definition: parsenodes.h:719
#define AGGKIND_HYPOTHETICAL
Definition: pg_aggregate.h:130
Definition: pg_list.h:45
Datum OidInputFunctionCall(Oid functionId, char *str, Oid typioparam, int32 typmod)
Definition: fmgr.c:1733
long val
Definition: informix.c:689
#define AGGKIND_ORDERED_SET
Definition: pg_aggregate.h:129
Oid typenameTypeId(ParseState *pstate, const TypeName *typeName)
Definition: parse_type.c:274