53 Datum allParameterTypes,
56 List *parameterDefaults,
60 List *aggcombinefnName,
61 List *aggserialfnName,
62 List *aggdeserialfnName,
63 List *aggmtransfnName,
64 List *aggminvtransfnName,
65 List *aggmfinalfnName,
66 bool finalfnExtraArgs,
67 bool mfinalfnExtraArgs,
75 const char *agginitval,
76 const char *aggminitval,
82 bool nulls[Natts_pg_aggregate];
84 bool replaces[Natts_pg_aggregate];
95 Oid *aggArgTypes = parameterTypes->
values;
96 bool mtransIsStrict =
false;
113 elog(
ERROR,
"no aggregate name supplied");
116 elog(
ERROR,
"aggregate must have a transition function");
118 if (numDirectArgs < 0 || numDirectArgs > numArgs)
119 elog(
ERROR,
"incorrect number of direct arguments for aggregate");
128 (
errcode(ERRCODE_TOO_MANY_ARGUMENTS),
129 errmsg_plural(
"aggregates cannot have more than %d argument",
130 "aggregates cannot have more than %d arguments",
143 (
errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
144 errmsg(
"cannot determine transition data type"),
157 (
errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
158 errmsg(
"cannot determine transition data type"),
169 if (AGGKIND_IS_ORDERED_SET(aggKind) &&
OidIsValid(variadicArgType) &&
170 variadicArgType != ANYOID)
172 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
173 errmsg(
"a variadic ordered-set aggregate must use VARIADIC type ANY")));
187 if (aggKind == AGGKIND_HYPOTHETICAL &&
188 numDirectArgs < numArgs)
190 int numAggregatedArgs = numArgs - numDirectArgs;
193 numDirectArgs < numAggregatedArgs ||
194 memcmp(aggArgTypes + (numDirectArgs - numAggregatedArgs),
195 aggArgTypes + numDirectArgs,
196 numAggregatedArgs *
sizeof(
Oid)) != 0)
198 (
errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
199 errmsg(
"a hypothetical-set aggregate must have direct arguments matching its aggregated arguments")));
209 if (AGGKIND_IS_ORDERED_SET(aggKind))
211 if (numDirectArgs < numArgs)
212 nargs_transfn = numArgs - numDirectArgs + 1;
219 fnArgs[0] = aggTransType;
220 memcpy(fnArgs + 1, aggArgTypes + (numArgs - (nargs_transfn - 1)),
221 (nargs_transfn - 1) *
sizeof(
Oid));
225 nargs_transfn = numArgs + 1;
226 fnArgs[0] = aggTransType;
227 memcpy(fnArgs + 1, aggArgTypes, numArgs *
sizeof(
Oid));
230 fnArgs, variadicArgType,
243 if (rettype != aggTransType)
245 (
errcode(ERRCODE_DATATYPE_MISMATCH),
246 errmsg(
"return type of transition function %s is not %s",
252 elog(
ERROR,
"cache lookup failed for function %u", transfn);
260 if (proc->proisstrict && agginitval == NULL)
265 (
errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
266 errmsg(
"must not omit initial value when transition function is strict and transition type is not compatible with input type")));
280 fnArgs[0] = aggmTransType;
283 fnArgs, variadicArgType,
287 if (rettype != aggmTransType)
289 (
errcode(ERRCODE_DATATYPE_MISMATCH),
290 errmsg(
"return type of transition function %s is not %s",
296 elog(
ERROR,
"cache lookup failed for function %u", mtransfn);
303 if (proc->proisstrict && aggminitval == NULL)
308 (
errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
309 errmsg(
"must not omit initial value when transition function is strict and transition type is not compatible with input type")));
313 mtransIsStrict = proc->proisstrict;
319 if (aggminvtransfnName)
328 fnArgs, variadicArgType,
332 if (rettype != aggmTransType)
334 (
errcode(ERRCODE_DATATYPE_MISMATCH),
335 errmsg(
"return type of inverse transition function %s is not %s",
341 elog(
ERROR,
"cache lookup failed for function %u", minvtransfn);
349 if (proc->proisstrict != mtransIsStrict)
351 (
errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
352 errmsg(
"strictness of aggregate's forward and inverse transition functions must match")));
367 Oid ffnVariadicArgType = variadicArgType;
369 fnArgs[0] = aggTransType;
370 memcpy(fnArgs + 1, aggArgTypes, numArgs *
sizeof(
Oid));
371 if (finalfnExtraArgs)
372 nargs_finalfn = numArgs + 1;
375 nargs_finalfn = numDirectArgs + 1;
376 if (numDirectArgs < numArgs)
384 fnArgs, ffnVariadicArgType,
395 (
errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
396 errmsg(
"final function with extra arguments must not be declared STRICT")));
403 finaltype = aggTransType;
408 if (aggcombinefnName)
416 fnArgs[0] = aggTransType;
417 fnArgs[1] = aggTransType;
424 if (combineType != aggTransType)
426 (
errcode(ERRCODE_DATATYPE_MISMATCH),
427 errmsg(
"return type of combine function %s is not %s",
436 if (aggTransType == INTERNALOID &&
func_strict(combinefn))
438 (
errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
439 errmsg(
"combine function with transition type %s must not be declared STRICT",
449 fnArgs[0] = INTERNALOID;
455 if (rettype != BYTEAOID)
457 (
errcode(ERRCODE_DATATYPE_MISMATCH),
458 errmsg(
"return type of serialization function %s is not %s",
466 if (aggdeserialfnName)
469 fnArgs[0] = BYTEAOID;
470 fnArgs[1] = INTERNALOID;
476 if (rettype != INTERNALOID)
478 (
errcode(ERRCODE_DATATYPE_MISMATCH),
479 errmsg(
"return type of deserialization function %s is not %s",
497 (
errcode(ERRCODE_DATATYPE_MISMATCH),
498 errmsg(
"cannot determine result data type"),
512 (
errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
513 errmsg(
"unsafe use of pseudo-type \"internal\""),
530 Oid ffnVariadicArgType = variadicArgType;
532 fnArgs[0] = aggmTransType;
533 memcpy(fnArgs + 1, aggArgTypes, numArgs *
sizeof(
Oid));
534 if (mfinalfnExtraArgs)
535 nargs_finalfn = numArgs + 1;
538 nargs_finalfn = numDirectArgs + 1;
539 if (numDirectArgs < numArgs)
547 fnArgs, ffnVariadicArgType,
553 (
errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
554 errmsg(
"final function with extra arguments must not be declared STRICT")));
561 rettype = aggmTransType;
564 if (rettype != finaltype)
566 (
errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
567 errmsg(
"moving-aggregate implementation returns type %s, but plain implementation returns type %s",
577 (
errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
578 errmsg(
"sort operator can only be specified for single-argument aggregates")));
580 aggArgTypes[0], aggArgTypes[0],
587 for (
i = 0;
i < numArgs;
i++)
631 PROVOLATILE_IMMUTABLE,
650 tupDesc = aggdesc->
rd_att;
653 for (
i = 0;
i < Natts_pg_aggregate;
i++)
682 nulls[Anum_pg_aggregate_agginitval - 1] =
true;
686 nulls[Anum_pg_aggregate_aggminitval - 1] =
true;
703 if (aggKind != oldagg->aggkind)
705 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
706 errmsg(
"cannot change routine kind"),
707 (oldagg->aggkind == AGGKIND_NORMAL ?
708 errdetail(
"\"%s\" is an ordinary aggregate function.", aggName) :
709 oldagg->aggkind == AGGKIND_ORDERED_SET ?
710 errdetail(
"\"%s\" is an ordered-set aggregate.", aggName) :
711 oldagg->aggkind == AGGKIND_HYPOTHETICAL ?
712 errdetail(
"\"%s\" is a hypothetical-set aggregate.", aggName) :
714 if (numDirectArgs != oldagg->aggnumdirectargs)
716 (
errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
717 errmsg(
"cannot change number of direct arguments of an aggregate function")));
719 replaces[Anum_pg_aggregate_aggfnoid - 1] =
false;
720 replaces[Anum_pg_aggregate_aggkind - 1] =
false;
721 replaces[Anum_pg_aggregate_aggnumdirectargs - 1] =
false;
849 nargs, input_types,
false,
false,
false,
850 &fnOid, rettype, &retset,
852 &true_oid_array, NULL);
857 (
errcode(ERRCODE_UNDEFINED_FUNCTION),
858 errmsg(
"function %s does not exist",
860 NIL, input_types))));
863 (
errcode(ERRCODE_DATATYPE_MISMATCH),
864 errmsg(
"function %s returns a set",
866 NIL, input_types))));
876 if (variadicArgType == ANYOID && vatype != ANYOID)
878 (
errcode(ERRCODE_DATATYPE_MISMATCH),
879 errmsg(
"function %s must accept VARIADIC ANY to be used in this aggregate",
881 NIL, input_types))));
898 for (
i = 0;
i < nargs;
i++)
902 (
errcode(ERRCODE_DATATYPE_MISMATCH),
903 errmsg(
"function %s requires run-time type coercion",
905 NIL, true_oid_array))));
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
AclResult object_aclcheck(Oid classid, Oid objectid, Oid roleid, AclMode mode)
void aclcheck_error_type(AclResult aclerr, Oid typeOid)
static Datum values[MAXATTR]
#define CStringGetTextDatum(s)
#define Assert(condition)
#define OidIsValid(objectId)
void record_object_address_dependencies(const ObjectAddress *depender, ObjectAddresses *referenced, DependencyType behavior)
ObjectAddresses * new_object_addresses(void)
void add_exact_object_address(const ObjectAddress *object, ObjectAddresses *addrs)
void free_object_addresses(ObjectAddresses *addrs)
int errmsg_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...)
int errdetail_internal(const char *fmt,...)
int errdetail(const char *fmt,...)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
HeapTuple heap_modify_tuple(HeapTuple tuple, TupleDesc tupleDesc, const Datum *replValues, const bool *replIsnull, const bool *doReplace)
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, const Datum *values, const bool *isnull)
#define HeapTupleIsValid(tuple)
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
void CatalogTupleInsert(Relation heapRel, HeapTuple tup)
char * get_func_name(Oid funcid)
bool func_strict(Oid funcid)
char * NameListToString(const List *names)
#define ObjectAddressSet(addr, class_id, object_id)
Oid enforce_generic_type_consistency(const Oid *actual_arg_types, Oid *declared_arg_types, int nargs, Oid rettype, bool allow_poly)
char * check_valid_internal_signature(Oid ret_type, const Oid *declared_arg_types, int nargs)
bool IsBinaryCoercible(Oid srctype, Oid targettype)
char * check_valid_polymorphic_signature(Oid ret_type, const Oid *declared_arg_types, int nargs)
FuncDetailCode func_get_detail(List *funcname, List *fargs, List *fargnames, int nargs, Oid *argtypes, bool expand_variadic, bool expand_defaults, bool include_out_arguments, Oid *funcid, Oid *rettype, bool *retset, int *nvargs, Oid *vatype, Oid **true_typeids, List **argdefaults)
const char * func_signature_string(List *funcname, int nargs, List *argnames, const Oid *argtypes)
Oid LookupOperName(ParseState *pstate, List *opername, Oid oprleft, Oid oprright, bool noError, int location)
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)
static Oid lookup_agg_function(List *fnName, int nargs, Oid *input_types, Oid variadicArgType, Oid *rettype)
FormData_pg_aggregate * Form_pg_aggregate
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, Node *prosqlbody, char prokind, 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, Oid prosupport, float4 procost, float4 prorows)
FormData_pg_proc * Form_pg_proc
static Datum PointerGetDatum(const void *X)
static Datum Int16GetDatum(int16 X)
static Datum BoolGetDatum(bool X)
static Datum ObjectIdGetDatum(Oid X)
static Datum Int32GetDatum(int32 X)
static Datum CharGetDatum(char X)
Oid values[FLEXIBLE_ARRAY_MEMBER]
void ReleaseSysCache(HeapTuple tuple)
HeapTuple SearchSysCache1(int cacheId, Datum key1)
void table_close(Relation relation, LOCKMODE lockmode)
Relation table_open(Oid relationId, LOCKMODE lockmode)