/* * FindConversion * * Find conversion by namespace and conversion name. * Returns conversion OID. */ Oid FindConversion(const char *conname, Oid connamespace) { HeapTuple tuple; Oid procoid; Oid conoid; AclResult aclresult; /* search pg_conversion by connamespace and conversion name */ tuple = SearchSysCache(CONNAMENSP, PointerGetDatum(conname), ObjectIdGetDatum(connamespace), 0, 0); if (!HeapTupleIsValid(tuple)) return InvalidOid; procoid = ((Form_pg_conversion) GETSTRUCT(tuple))->conproc; conoid = HeapTupleGetOid(tuple); ReleaseSysCache(tuple); /* Check we have execute rights for the function */ aclresult = pg_proc_aclcheck(procoid, GetUserId(), ACL_EXECUTE); if (aclresult != ACLCHECK_OK) return InvalidOid; return conoid; }
/* * FindConversion * * Find conversion by namespace and conversion name. * Returns conversion OID. */ Oid FindConversion(const char *conname, Oid connamespace) { HeapTuple tuple; Oid procoid; Oid conoid; AclResult aclresult; cqContext *pcqCtx; /* search pg_conversion by connamespace and conversion name */ pcqCtx = caql_beginscan( NULL, cql("SELECT * FROM pg_conversion " " WHERE conname = :1 " " AND connamespace = :2 ", CStringGetDatum((char *) conname), ObjectIdGetDatum(connamespace))); tuple = caql_getnext(pcqCtx); if (!HeapTupleIsValid(tuple)) return InvalidOid; procoid = ((Form_pg_conversion) GETSTRUCT(tuple))->conproc; conoid = HeapTupleGetOid(tuple); caql_endscan(pcqCtx); /* Check we have execute rights for the function */ aclresult = pg_proc_aclcheck(procoid, GetUserId(), ACL_EXECUTE); if (aclresult != ACLCHECK_OK) return InvalidOid; return conoid; }
/* * CREATE CONVERSION */ void CreateConversionCommand(CreateConversionStmt *stmt) { Oid namespaceId; char *conversion_name; AclResult aclresult; int from_encoding; int to_encoding; Oid funcoid; const char *from_encoding_name = stmt->for_encoding_name; const char *to_encoding_name = stmt->to_encoding_name; List *func_name = stmt->func_name; static Oid funcargs[] = {INT4OID, INT4OID, CSTRINGOID, INTERNALOID, INT4OID}; /* Convert list of names to a name and namespace */ namespaceId = QualifiedNameGetCreationNamespace(stmt->conversion_name, &conversion_name); /* Check we have creation rights in target namespace */ aclresult = pg_namespace_aclcheck(namespaceId, GetUserId(), ACL_CREATE); if (aclresult != ACLCHECK_OK) aclcheck_error(aclresult, ACL_KIND_NAMESPACE, get_namespace_name(namespaceId)); /* Check the encoding names */ from_encoding = pg_char_to_encoding(from_encoding_name); if (from_encoding < 0) ereport(ERROR, (errcode(ERRCODE_UNDEFINED_OBJECT), errmsg("source encoding \"%s\" does not exist", from_encoding_name))); to_encoding = pg_char_to_encoding(to_encoding_name); if (to_encoding < 0) ereport(ERROR, (errcode(ERRCODE_UNDEFINED_OBJECT), errmsg("destination encoding \"%s\" does not exist", to_encoding_name))); /* * Check the existence of the conversion function. Function name could * be a qualified name. */ funcoid = LookupFuncName(func_name, sizeof(funcargs) / sizeof(Oid), funcargs, false); /* Check we have EXECUTE rights for the function */ aclresult = pg_proc_aclcheck(funcoid, GetUserId(), ACL_EXECUTE); if (aclresult != ACLCHECK_OK) aclcheck_error(aclresult, ACL_KIND_PROC, NameListToString(func_name)); /* * All seem ok, go ahead (possible failure would be a duplicate * conversion name) */ ConversionCreate(conversion_name, namespaceId, GetUserId(), from_encoding, to_encoding, funcoid, stmt->def); }
/* * lookup_agg_function -- common code for finding both transfn and finalfn */ static Oid lookup_agg_function(List *fnName, int nargs, Oid *input_types, Oid *rettype) { Oid fnOid; bool retset; Oid *true_oid_array; FuncDetailCode fdresult; AclResult aclresult; /* * func_get_detail looks up the function in the catalogs, does * disambiguation for polymorphic functions, handles inheritance, and * returns the funcid and type and set or singleton status of the * function's return value. it also returns the true argument types * to the function. */ fdresult = func_get_detail(fnName, NIL, nargs, input_types, &fnOid, rettype, &retset, &true_oid_array); /* only valid case is a normal function not returning a set */ if (fdresult != FUNCDETAIL_NORMAL || !OidIsValid(fnOid)) ereport(ERROR, (errcode(ERRCODE_UNDEFINED_FUNCTION), errmsg("function %s does not exist", func_signature_string(fnName, nargs, input_types)))); if (retset) ereport(ERROR, (errcode(ERRCODE_DATATYPE_MISMATCH), errmsg("function %s returns a set", func_signature_string(fnName, nargs, input_types)))); /* * If the given type(s) are all polymorphic, there's nothing we can * check. Otherwise, enforce consistency, and possibly refine the * result type. */ if ((input_types[0] == ANYARRAYOID || input_types[0] == ANYELEMENTOID) && (nargs == 1 || (input_types[1] == ANYARRAYOID || input_types[1] == ANYELEMENTOID))) { /* nothing to check here */ } else { *rettype = enforce_generic_type_consistency(input_types, true_oid_array, nargs, *rettype); } /* * func_get_detail will find functions requiring run-time argument * type coercion, but nodeAgg.c isn't prepared to deal with that */ if (true_oid_array[0] != ANYARRAYOID && true_oid_array[0] != ANYELEMENTOID && !IsBinaryCoercible(input_types[0], true_oid_array[0])) ereport(ERROR, (errcode(ERRCODE_DATATYPE_MISMATCH), errmsg("function %s requires run-time type coercion", func_signature_string(fnName, nargs, true_oid_array)))); if (nargs == 2 && true_oid_array[1] != ANYARRAYOID && true_oid_array[1] != ANYELEMENTOID && !IsBinaryCoercible(input_types[1], true_oid_array[1])) ereport(ERROR, (errcode(ERRCODE_DATATYPE_MISMATCH), errmsg("function %s requires run-time type coercion", func_signature_string(fnName, nargs, true_oid_array)))); /* Check aggregate creator has permission to call the function */ aclresult = pg_proc_aclcheck(fnOid, GetUserId(), ACL_EXECUTE); if (aclresult != ACLCHECK_OK) aclcheck_error(aclresult, ACL_KIND_PROC, get_func_name(fnOid)); return fnOid; }
/* * lookup_agg_function * common code for finding transfn, invtransfn, finalfn, and combinefn * * Returns OID of function, and stores its return type into *rettype * * NB: must not scribble on input_types[], as we may re-use those */ static Oid lookup_agg_function(List *fnName, int nargs, Oid *input_types, Oid variadicArgType, Oid *rettype) { Oid fnOid; bool retset; int nvargs; Oid vatype; Oid *true_oid_array; FuncDetailCode fdresult; AclResult aclresult; int i; /* * func_get_detail looks up the function in the catalogs, does * disambiguation for polymorphic functions, handles inheritance, and * returns the funcid and type and set or singleton status of the * function's return value. it also returns the true argument types to * the function. */ fdresult = func_get_detail(fnName, NIL, NIL, nargs, input_types, false, false, &fnOid, rettype, &retset, &nvargs, &vatype, &true_oid_array, NULL); /* only valid case is a normal function not returning a set */ if (fdresult != FUNCDETAIL_NORMAL || !OidIsValid(fnOid)) ereport(ERROR, (errcode(ERRCODE_UNDEFINED_FUNCTION), errmsg("function %s does not exist", func_signature_string(fnName, nargs, NIL, input_types)))); if (retset) ereport(ERROR, (errcode(ERRCODE_DATATYPE_MISMATCH), errmsg("function %s returns a set", func_signature_string(fnName, nargs, NIL, input_types)))); /* * If the agg is declared to take VARIADIC ANY, the underlying functions * had better be declared that way too, else they may receive too many * parameters; but func_get_detail would have been happy with plain ANY. * (Probably nothing very bad would happen, but it wouldn't work as the * user expects.) Other combinations should work without any special * pushups, given that we told func_get_detail not to expand VARIADIC. */ if (variadicArgType == ANYOID && vatype != ANYOID) ereport(ERROR, (errcode(ERRCODE_DATATYPE_MISMATCH), errmsg("function %s must accept VARIADIC ANY to be used in this aggregate", func_signature_string(fnName, nargs, NIL, input_types)))); /* * If there are any polymorphic types involved, enforce consistency, and * possibly refine the result type. It's OK if the result is still * polymorphic at this point, though. */ *rettype = enforce_generic_type_consistency(input_types, true_oid_array, nargs, *rettype, true); /* * func_get_detail will find functions requiring run-time argument type * coercion, but nodeAgg.c isn't prepared to deal with that */ for (i = 0; i < nargs; i++) { if (!IsBinaryCoercible(input_types[i], true_oid_array[i])) ereport(ERROR, (errcode(ERRCODE_DATATYPE_MISMATCH), errmsg("function %s requires run-time type coercion", func_signature_string(fnName, nargs, NIL, true_oid_array)))); } /* Check aggregate creator has permission to call the function */ aclresult = pg_proc_aclcheck(fnOid, GetUserId(), ACL_EXECUTE); if (aclresult != ACLCHECK_OK) aclcheck_error(aclresult, ACL_KIND_PROC, get_func_name(fnOid)); return fnOid; }
/* * CREATE CONVERSION */ ObjectAddress CreateConversionCommand(CreateConversionStmt *stmt) { Oid namespaceId; char *conversion_name; AclResult aclresult; int from_encoding; int to_encoding; Oid funcoid; const char *from_encoding_name = stmt->for_encoding_name; const char *to_encoding_name = stmt->to_encoding_name; List *func_name = stmt->func_name; static const Oid funcargs[] = {INT4OID, INT4OID, CSTRINGOID, INTERNALOID, INT4OID}; char result[1]; /* Convert list of names to a name and namespace */ namespaceId = QualifiedNameGetCreationNamespace(stmt->conversion_name, &conversion_name); /* Check we have creation rights in target namespace */ aclresult = pg_namespace_aclcheck(namespaceId, GetUserId(), ACL_CREATE); if (aclresult != ACLCHECK_OK) aclcheck_error(aclresult, ACL_KIND_NAMESPACE, get_namespace_name(namespaceId)); /* Check the encoding names */ from_encoding = pg_char_to_encoding(from_encoding_name); if (from_encoding < 0) ereport(ERROR, (errcode(ERRCODE_UNDEFINED_OBJECT), errmsg("source encoding \"%s\" does not exist", from_encoding_name))); to_encoding = pg_char_to_encoding(to_encoding_name); if (to_encoding < 0) ereport(ERROR, (errcode(ERRCODE_UNDEFINED_OBJECT), errmsg("destination encoding \"%s\" does not exist", to_encoding_name))); /* * Check the existence of the conversion function. Function name could be * a qualified name. */ funcoid = LookupFuncName(func_name, sizeof(funcargs) / sizeof(Oid), funcargs, false); /* Check it returns VOID, else it's probably the wrong function */ if (get_func_rettype(funcoid) != VOIDOID) ereport(ERROR, (errcode(ERRCODE_INVALID_OBJECT_DEFINITION), errmsg("encoding conversion function %s must return type %s", NameListToString(func_name), "void"))); /* Check we have EXECUTE rights for the function */ aclresult = pg_proc_aclcheck(funcoid, GetUserId(), ACL_EXECUTE); if (aclresult != ACLCHECK_OK) aclcheck_error(aclresult, ACL_KIND_PROC, NameListToString(func_name)); /* * Check that the conversion function is suitable for the requested source * and target encodings. We do that by calling the function with an empty * string; the conversion function should throw an error if it can't * perform the requested conversion. */ OidFunctionCall5(funcoid, Int32GetDatum(from_encoding), Int32GetDatum(to_encoding), CStringGetDatum(""), CStringGetDatum(result), Int32GetDatum(0)); /* * All seem ok, go ahead (possible failure would be a duplicate conversion * name) */ return ConversionCreate(conversion_name, namespaceId, GetUserId(), from_encoding, to_encoding, funcoid, stmt->def); }
/* * ValidateProtocolFunction -- common code for finding readfn, writefn or validatorfn */ static Oid ValidateProtocolFunction(List *fnName, ExtPtcFuncType fntype) { Oid fnOid; bool retset; bool retstrict; bool retordered; Oid *true_oid_array; Oid actual_rettype; Oid desired_rettype; FuncDetailCode fdresult; AclResult aclresult; Oid inputTypes[1] = {InvalidOid}; /* dummy */ int nargs = 0; /* true for all 3 function types at the moment */ int nvargs; if (fntype == EXTPTC_FUNC_VALIDATOR) desired_rettype = VOIDOID; else desired_rettype = INT4OID; /* * func_get_detail looks up the function in the catalogs, does * disambiguation for polymorphic functions, handles inheritance, and * returns the funcid and type and set or singleton status of the * function's return value. it also returns the true argument types to * the function. */ fdresult = func_get_detail(fnName, NIL, nargs, inputTypes, false, false, &fnOid, &actual_rettype, &retset, &retstrict, &retordered, &nvargs, &true_oid_array, NULL); /* only valid case is a normal function not returning a set */ if (fdresult != FUNCDETAIL_NORMAL || !OidIsValid(fnOid)) ereport(ERROR, (errcode(ERRCODE_UNDEFINED_FUNCTION), errmsg("function %s does not exist", func_signature_string(fnName, nargs, inputTypes)))); if (retset) ereport(ERROR, (errcode(ERRCODE_DATATYPE_MISMATCH), errmsg("Invalid protocol function"), errdetail("Protocol functions cannot return sets."))); if (actual_rettype != desired_rettype) ereport(ERROR, (errcode(ERRCODE_DATATYPE_MISMATCH), errmsg("%s protocol function %s must return %s", func_type_to_name(fntype), func_signature_string(fnName, nargs, inputTypes), (fntype == EXTPTC_FUNC_VALIDATOR ? "void" : "an integer")))); if (func_volatile(fnOid) == PROVOLATILE_IMMUTABLE) ereport(ERROR, (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION), errmsg("%s protocol function %s is declared IMMUTABLE", func_type_to_name(fntype), func_signature_string(fnName, nargs, inputTypes)), errhint("PROTOCOL functions must be declared STABLE or VOLATILE"))); /* Check protocol creator has permission to call the function */ aclresult = pg_proc_aclcheck(fnOid, GetUserId(), ACL_EXECUTE); if (aclresult != ACLCHECK_OK) aclcheck_error(aclresult, ACL_KIND_PROC, get_func_name(fnOid)); return fnOid; }
/* * lookup_agg_function -- common code for finding both transfn and finalfn */ static Oid lookup_agg_function(List *fnName, int nargs, Oid *input_types, Oid *rettype) { Oid fnOid; bool retset; int nvargs; Oid *true_oid_array; FuncDetailCode fdresult; AclResult aclresult; int i; /* * func_get_detail looks up the function in the catalogs, does * disambiguation for polymorphic functions, handles inheritance, and * returns the funcid and type and set or singleton status of the * function's return value. it also returns the true argument types to * the function. */ fdresult = func_get_detail(fnName, NIL, nargs, input_types, false, false, &fnOid, rettype, &retset, &nvargs, &true_oid_array, NULL); /* only valid case is a normal function not returning a set */ if (fdresult != FUNCDETAIL_NORMAL || !OidIsValid(fnOid)) ereport(ERROR, (errcode(ERRCODE_UNDEFINED_FUNCTION), errmsg("function %s does not exist", func_signature_string(fnName, nargs, input_types)))); if (retset) ereport(ERROR, (errcode(ERRCODE_DATATYPE_MISMATCH), errmsg("function %s returns a set", func_signature_string(fnName, nargs, input_types)))); /* * If there are any polymorphic types involved, enforce consistency, and * possibly refine the result type. It's OK if the result is still * polymorphic at this point, though. */ *rettype = enforce_generic_type_consistency(input_types, true_oid_array, nargs, *rettype, true); /* * func_get_detail will find functions requiring run-time argument type * coercion, but nodeAgg.c isn't prepared to deal with that */ for (i = 0; i < nargs; i++) { if (!IsPolymorphicType(true_oid_array[i]) && !IsBinaryCoercible(input_types[i], true_oid_array[i])) ereport(ERROR, (errcode(ERRCODE_DATATYPE_MISMATCH), errmsg("function %s requires run-time type coercion", func_signature_string(fnName, nargs, true_oid_array)))); } /* Check aggregate creator has permission to call the function */ aclresult = pg_proc_aclcheck(fnOid, GetUserId(), ACL_EXECUTE); if (aclresult != ACLCHECK_OK) aclcheck_error(aclresult, ACL_KIND_PROC, get_func_name(fnOid)); return fnOid; }
/* * init_sexpr - initialize a SetExprState node during first use */ static void init_sexpr(Oid foid, Oid input_collation, Expr *node, SetExprState *sexpr, PlanState *parent, MemoryContext sexprCxt, bool allowSRF, bool needDescForSRF) { AclResult aclresult; /* Check permission to call function */ aclresult = pg_proc_aclcheck(foid, GetUserId(), ACL_EXECUTE); if (aclresult != ACLCHECK_OK) aclcheck_error(aclresult, OBJECT_FUNCTION, get_func_name(foid)); InvokeFunctionExecuteHook(foid); /* * Safety check on nargs. Under normal circumstances this should never * fail, as parser should check sooner. But possibly it might fail if * server has been compiled with FUNC_MAX_ARGS smaller than some functions * declared in pg_proc? */ if (list_length(sexpr->args) > FUNC_MAX_ARGS) ereport(ERROR, (errcode(ERRCODE_TOO_MANY_ARGUMENTS), errmsg_plural("cannot pass more than %d argument to a function", "cannot pass more than %d arguments to a function", FUNC_MAX_ARGS, FUNC_MAX_ARGS))); /* Set up the primary fmgr lookup information */ fmgr_info_cxt(foid, &(sexpr->func), sexprCxt); fmgr_info_set_expr((Node *) sexpr->expr, &(sexpr->func)); /* Initialize the function call parameter struct as well */ InitFunctionCallInfoData(sexpr->fcinfo_data, &(sexpr->func), list_length(sexpr->args), input_collation, NULL, NULL); /* If function returns set, check if that's allowed by caller */ if (sexpr->func.fn_retset && !allowSRF) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("set-valued function called in context that cannot accept a set"), parent ? executor_errposition(parent->state, exprLocation((Node *) node)) : 0)); /* Otherwise, caller should have marked the sexpr correctly */ Assert(sexpr->func.fn_retset == sexpr->funcReturnsSet); /* If function returns set, prepare expected tuple descriptor */ if (sexpr->func.fn_retset && needDescForSRF) { TypeFuncClass functypclass; Oid funcrettype; TupleDesc tupdesc; MemoryContext oldcontext; functypclass = get_expr_result_type(sexpr->func.fn_expr, &funcrettype, &tupdesc); /* Must save tupdesc in sexpr's context */ oldcontext = MemoryContextSwitchTo(sexprCxt); if (functypclass == TYPEFUNC_COMPOSITE || functypclass == TYPEFUNC_COMPOSITE_DOMAIN) { /* Composite data type, e.g. a table's row type */ Assert(tupdesc); /* Must copy it out of typcache for safety */ sexpr->funcResultDesc = CreateTupleDescCopy(tupdesc); sexpr->funcReturnsTuple = true; } else if (functypclass == TYPEFUNC_SCALAR) { /* Base data type, i.e. scalar */ tupdesc = CreateTemplateTupleDesc(1, false); TupleDescInitEntry(tupdesc, (AttrNumber) 1, NULL, funcrettype, -1, 0); sexpr->funcResultDesc = tupdesc; sexpr->funcReturnsTuple = false; } else if (functypclass == TYPEFUNC_RECORD) { /* This will work if function doesn't need an expectedDesc */ sexpr->funcResultDesc = NULL; sexpr->funcReturnsTuple = true; } else { /* Else, we will fail if function needs an expectedDesc */ sexpr->funcResultDesc = NULL; } MemoryContextSwitchTo(oldcontext); } else sexpr->funcResultDesc = NULL; /* Initialize additional state */ sexpr->funcResultStore = NULL; sexpr->funcResultSlot = NULL; sexpr->shutdown_reg = false; }