Datum dbms_assert_object_name(PG_FUNCTION_ARGS) { List *names; text *str; char *object_name; Oid classId; if (PG_ARGISNULL(0)) INVALID_OBJECT_NAME_EXCEPTION(); str = PG_GETARG_TEXT_P(0); if (EMPTY_STR(str)) INVALID_OBJECT_NAME_EXCEPTION(); object_name = text_to_cstring(str); #ifdef GP_VERSION_NUM names = stringToQualifiedNameList(object_name, "dbms"); #else names = stringToQualifiedNameList(object_name); #endif classId = RangeVarGetRelid(makeRangeVarFromNameList(names), true); if (!OidIsValid(classId)) INVALID_OBJECT_NAME_EXCEPTION(); PG_RETURN_TEXT_P(str); }
Datum dbms_assert_schema_name(PG_FUNCTION_ARGS) { Oid namespaceId; AclResult aclresult; text *sname; char *nspname; List *names; if (PG_ARGISNULL(0)) INVALID_SCHEMA_NAME_EXCEPTION(); sname = PG_GETARG_TEXT_P(0); if (EMPTY_STR(sname)) INVALID_SCHEMA_NAME_EXCEPTION(); nspname = text_to_cstring(sname); names = stringToQualifiedNameList(nspname); if (list_length(names) != 1) INVALID_SCHEMA_NAME_EXCEPTION(); namespaceId = GetSysCacheOid(NAMESPACENAME, CStringGetDatum(strVal(linitial(names))), 0, 0, 0); if (!OidIsValid(namespaceId)) INVALID_SCHEMA_NAME_EXCEPTION(); aclresult = pg_namespace_aclcheck(namespaceId, GetUserId(), ACL_USAGE); if (aclresult != ACLCHECK_OK) INVALID_SCHEMA_NAME_EXCEPTION(); PG_RETURN_TEXT_P(sname); }
/* * Get Oid of current parser * * Here, it seems reasonable to select the "default" parser if none has been * set. */ static Oid GetCurrentParser(void) { if (current_parser_oid == InvalidOid) current_parser_oid = TSParserGetPrsid(stringToQualifiedNameList("pg_catalog.default"), false); return current_parser_oid; }
Oid getTSCurrentConfig(bool emitError) { /* if we have a cached value, return it */ if (OidIsValid(TSCurrentConfigCache)) return TSCurrentConfigCache; /* fail if GUC hasn't been set up yet */ if (TSCurrentConfig == NULL || *TSCurrentConfig == '\0') { if (emitError) elog(ERROR, "text search configuration isn't set"); else return InvalidOid; } if (TSConfigCacheHash == NULL) { /* First time through: initialize the tsconfig inval callback */ init_ts_config_cache(); } /* Look up the config */ TSCurrentConfigCache = get_ts_config_oid(stringToQualifiedNameList(TSCurrentConfig), !emitError); return TSCurrentConfigCache; }
/* * regdictionaryin - converts "tsdictionaryname" to tsdictionary OID * * We also accept a numeric OID, for symmetry with the output routine. * * '-' signifies unknown (OID 0). In all other cases, the input must * match an existing pg_ts_dict entry. * * This function is not needed in bootstrap mode, so we don't worry about * making it work then. */ Datum regdictionaryin(PG_FUNCTION_ARGS) { char *dict_name_or_oid = PG_GETARG_CSTRING(0); Oid result; List *names; /* '-' ? */ if (strcmp(dict_name_or_oid, "-") == 0) PG_RETURN_OID(InvalidOid); /* Numeric OID? */ if (dict_name_or_oid[0] >= '0' && dict_name_or_oid[0] <= '9' && strspn(dict_name_or_oid, "0123456789") == strlen(dict_name_or_oid)) { result = DatumGetObjectId(DirectFunctionCall1(oidin, CStringGetDatum(dict_name_or_oid))); PG_RETURN_OID(result); } /* * Normal case: parse the name into components and see if it matches any * pg_ts_dict entries in the current search path. */ names = stringToQualifiedNameList(dict_name_or_oid); result = get_ts_dict_oid(names, false); PG_RETURN_OID(result); }
/* * Get oid_t of current parser * * Here, it seems reasonable to select the "default" parser if none has been * set. */ static oid_t GetCurrentParser(void) { if (current_parser_oid == INVALID_OID) current_parser_oid = get_ts_parser_oid(stringToQualifiedNameList("pg_catalog.default"), false); return current_parser_oid; }
/* GUC check_hook for default_text_search_config */ bool check_TSCurrentConfig(char **newval, void **extra, GucSource source) { /* * If we aren't inside a transaction, we cannot do database access so * cannot verify the config name. Must accept it on faith. */ if (IsTransactionState()) { Oid cfgId; HeapTuple tuple; Form_pg_ts_config cfg; char *buf; cfgId = get_ts_config_oid(stringToQualifiedNameList(*newval), true); /* * When source == PGC_S_TEST, don't throw a hard error for a * nonexistent configuration, only a NOTICE. See comments in guc.h. */ if (!OidIsValid(cfgId)) { if (source == PGC_S_TEST) { ereport(NOTICE, (errcode(ERRCODE_UNDEFINED_OBJECT), errmsg("text search configuration \"%s\" does not exist", *newval))); return true; } else return false; } /* * Modify the actually stored value to be fully qualified, to ensure * later changes of search_path don't affect it. */ tuple = SearchSysCache1(TSCONFIGOID, ObjectIdGetDatum(cfgId)); if (!HeapTupleIsValid(tuple)) elog(ERROR, "cache lookup failed for text search configuration %u", cfgId); cfg = (Form_pg_ts_config) GETSTRUCT(tuple); buf = quote_qualified_identifier(get_namespace_name(cfg->cfgnamespace), NameStr(cfg->cfgname)); ReleaseSysCache(tuple); /* GUC wants it malloc'd not palloc'd */ free(*newval); *newval = strdup(buf); pfree(buf); if (!*newval) return false; } return true; }
/* set_curprs(text) */ Datum tsa_set_curprs_byname(PG_FUNCTION_ARGS) { text *name = PG_GETARG_TEXT_P(0); Oid parser_oid; parser_oid = TSParserGetPrsid(stringToQualifiedNameList(TextPGetCString(name)), false); current_parser_oid = parser_oid; PG_RETURN_VOID(); }
/* set_curprs(text) */ Datum tsa_set_curprs_byname(PG_FUNCTION_ARGS) { text *name = PG_GETARG_TEXT_PP(0); Oid parser_oid; parser_oid = get_ts_parser_oid(stringToQualifiedNameList(text_to_cstring(name)), false); current_parser_oid = parser_oid; PG_RETURN_VOID(); }
/* set_curprs(text) */ datum_t tsa_set_curprs_byname(PG_FUNC_ARGS) { text *name = ARG_TEXT_PP(0); oid_t parser_oid; parser_oid = get_ts_parser_oid(stringToQualifiedNameList(text_to_cstring(name)), false); current_parser_oid = parser_oid; RET_VOID(); }
/* set_curdict(text) */ datum_t tsa_set_curdict_byname(PG_FUNC_ARGS) { text *name = ARG_TEXT_PP(0); oid_t dict_oid; dict_oid = get_ts_dict_oid(stringToQualifiedNameList(text_to_cstring(name)), false); current_dictionary_oid = dict_oid; RET_VOID(); }
/* set_curdict(text) */ Datum tsa_set_curdict_byname(PG_FUNCTION_ARGS) { text *name = PG_GETARG_TEXT_P(0); Oid dict_oid; dict_oid = TSDictionaryGetDictid(stringToQualifiedNameList(TextPGetCString(name)), false); current_dictionary_oid = dict_oid; PG_RETURN_VOID(); }
static Datum assign_callgraph_buffer_id() { List *names; Oid seqoid; names = stringToQualifiedNameList("call_graph.seqCallGraphBuffer"); #if PG_VERSION_NUM >= 90200 seqoid = RangeVarGetRelid(makeRangeVarFromNameList(names), NoLock, false); #else seqoid = RangeVarGetRelid(makeRangeVarFromNameList(names), false); #endif return DirectFunctionCall1(nextval_oid, ObjectIdGetDatum(seqoid)); }
/* * RelationNameGetTupleDesc * * Given a (possibly qualified) relation name, build a TupleDesc. * * Note: while this works as advertised, it's seldom the best way to * build a tupdesc for a function's result type. It's kept around * only for backwards compatibility with existing user-written code. */ TupleDesc RelationNameGetTupleDesc(const char *relname) { RangeVar *relvar; Relation rel; TupleDesc tupdesc; List *relname_list; /* Open relation and copy the tuple description */ relname_list = stringToQualifiedNameList(relname); relvar = makeRangeVarFromNameList(relname_list); rel = relation_openrv(relvar, AccessShareLock); tupdesc = CreateTupleDescCopy(RelationGetDescr(rel)); relation_close(rel, AccessShareLock); return tupdesc; }
static bool BufferedWriterParam(BufferedWriter *self, const char *keyword, char *value) { if (CompareKeyword(keyword, "TABLE") || CompareKeyword(keyword, "OUTPUT")) { ASSERT_ONCE(self->base.output == NULL); self->base.relid = RangeVarGetRelid(makeRangeVarFromNameList( stringToQualifiedNameList(value)), NoLock, false); self->base.output = get_relation_name(self->base.relid); } else if (CompareKeyword(keyword, "DUPLICATE_BADFILE")) { ASSERT_ONCE(self->base.dup_badfile == NULL); self->base.dup_badfile = pstrdup(value); } else if (CompareKeyword(keyword, "DUPLICATE_ERRORS")) { ASSERT_ONCE(self->base.max_dup_errors < -1); self->base.max_dup_errors = ParseInt64(value, -1); if (self->base.max_dup_errors == -1) self->base.max_dup_errors = INT64_MAX; } else if (CompareKeyword(keyword, "ON_DUPLICATE_KEEP")) { const ON_DUPLICATE values[] = { ON_DUPLICATE_KEEP_NEW, ON_DUPLICATE_KEEP_OLD }; self->base.on_duplicate = values[choice(keyword, value, ON_DUPLICATE_NAMES, lengthof(values))]; } else if (CompareKeyword(keyword, "TRUNCATE")) { self->base.truncate = ParseBoolean(value); } else return false; /* unknown parameter */ return true; }
/* * to_regproc - converts "proname" to proc OID * * If the name is not found, we return NULL. */ Datum to_regproc(PG_FUNCTION_ARGS) { char *pro_name = PG_GETARG_CSTRING(0); List *names; FuncCandidateList clist; /* * Parse the name into components and see if it matches any pg_proc * entries in the current search path. */ names = stringToQualifiedNameList(pro_name); clist = FuncnameGetCandidates(names, -1, NIL, false, false, true); if (clist == NULL || clist->next != NULL) PG_RETURN_NULL(); PG_RETURN_OID(clist->oid); }
/* * to_regoper - converts "oprname" to operator OID * * If the name is not found, we return NULL. */ Datum to_regoper(PG_FUNCTION_ARGS) { char *opr_name = PG_GETARG_CSTRING(0); List *names; FuncCandidateList clist; /* * Parse the name into components and see if it matches any pg_operator * entries in the current search path. */ names = stringToQualifiedNameList(opr_name); clist = OpernameGetCandidates(names, '\0', true); if (clist == NULL || clist->next != NULL) PG_RETURN_NULL(); PG_RETURN_OID(clist->oid); }
/* * to_regclass - converts "classname" to class OID * * If the name is not found, we return NULL. */ Datum to_regclass(PG_FUNCTION_ARGS) { char *class_name = PG_GETARG_CSTRING(0); Oid result; List *names; /* * Parse the name into components and see if it matches any pg_class * entries in the current search path. */ names = stringToQualifiedNameList(class_name); /* We might not even have permissions on this relation; don't lock it. */ result = RangeVarGetRelid(makeRangeVarFromNameList(names), NoLock, true); if (OidIsValid(result)) PG_RETURN_OID(result); else PG_RETURN_NULL(); }
StringInfo ddlCommand = (StringInfo) lfirst(ddlCommandCell); Node *ddlCommandNode = ParseTreeNode(ddlCommand->data); ProcessUtility(ddlCommandNode, ddlCommand->data, PROCESS_UTILITY_TOPLEVEL, NULL, None_Receiver, NULL); CommandCounterIncrement(); } SetUserIdAndSecContext(savedUserId, savedSecurityContext); /* * Copy local file into the relation. We call ProcessUtility() instead of * directly calling DoCopy() because some extensions (e.g. cstore_fdw) hook * into process utility to provide their custom COPY behavior. */ tableNameList = stringToQualifiedNameList(tableName); localTable = makeRangeVarFromNameList(tableNameList); localCopyCommand = CopyStatement(localTable, localFilePath->data); queryString = makeStringInfo(); appendStringInfo(queryString, COPY_IN_COMMAND, tableName, localFilePath->data); ProcessUtility((Node *) localCopyCommand, queryString->data, PROCESS_UTILITY_TOPLEVEL, NULL, None_Receiver, NULL); /* finally delete the temporary file we created */ DeleteFile(localFilePath->data); return true; }
/* * regclassin - converts "classname" to class OID * * We also accept a numeric OID, for symmetry with the output routine. * * '-' signifies unknown (OID 0). In all other cases, the input must * match an existing pg_class entry. */ Datum regclassin(PG_FUNCTION_ARGS) { char *class_name_or_oid = PG_GETARG_CSTRING(0); Oid result = InvalidOid; List *names; /* '-' ? */ if (strcmp(class_name_or_oid, "-") == 0) PG_RETURN_OID(InvalidOid); /* Numeric OID? */ if (class_name_or_oid[0] >= '0' && class_name_or_oid[0] <= '9' && strspn(class_name_or_oid, "0123456789") == strlen(class_name_or_oid)) { result = DatumGetObjectId(DirectFunctionCall1(oidin, CStringGetDatum(class_name_or_oid))); PG_RETURN_OID(result); } /* Else it's a name, possibly schema-qualified */ /* * In bootstrap mode we assume the given name is not schema-qualified, and * just search pg_class for a match. This is needed for initializing * other system catalogs (pg_namespace may not exist yet, and certainly * there are no schemas other than pg_catalog). */ if (IsBootstrapProcessingMode()) { int matches = 0; result = caql_getoid_plus( NULL, &matches, NULL, cql("SELECT oid FROM pg_class " " WHERE relname = :1 ", CStringGetDatum(class_name_or_oid))); if (0 == matches) { ereport(ERROR, (errcode(ERRCODE_UNDEFINED_TABLE), errmsg("relation \"%s\" does not exist", class_name_or_oid))); } /* We assume there can be only one match */ PG_RETURN_OID(result); } /* * Normal case: parse the name into components and see if it matches any * pg_class entries in the current search path. */ names = stringToQualifiedNameList(class_name_or_oid, "regclassin"); result = RangeVarGetRelid(makeRangeVarFromNameList(names), false, true /*allowHcatalog*/); PG_RETURN_OID(result); }
/* * regprocin - converts "proname" to proc OID * * We also accept a numeric OID, for symmetry with the output routine. * * '-' signifies unknown (OID 0). In all other cases, the input must * match an existing pg_proc entry. */ Datum regprocin(PG_FUNCTION_ARGS) { char *pro_name_or_oid = PG_GETARG_CSTRING(0); RegProcedure result = InvalidOid; List *names; FuncCandidateList clist; /* '-' ? */ if (strcmp(pro_name_or_oid, "-") == 0) PG_RETURN_OID(InvalidOid); /* Numeric OID? */ if (pro_name_or_oid[0] >= '0' && pro_name_or_oid[0] <= '9' && strspn(pro_name_or_oid, "0123456789") == strlen(pro_name_or_oid)) { result = DatumGetObjectId(DirectFunctionCall1(oidin, CStringGetDatum(pro_name_or_oid))); PG_RETURN_OID(result); } /* Else it's a name, possibly schema-qualified */ /* * In bootstrap mode we assume the given name is not schema-qualified, and * just search pg_proc for a unique match. This is needed for * initializing other system catalogs (pg_namespace may not exist yet, and * certainly there are no schemas other than pg_catalog). */ if (IsBootstrapProcessingMode()) { int matches = 0; result = (RegProcedure) caql_getoid_plus( NULL, &matches, NULL, cql("SELECT oid FROM pg_proc " " WHERE proname = :1 ", CStringGetDatum(pro_name_or_oid))); if (matches == 0) ereport(ERROR, (errcode(ERRCODE_UNDEFINED_FUNCTION), errmsg("function \"%s\" does not exist", pro_name_or_oid))); else if (matches > 1) ereport(ERROR, (errcode(ERRCODE_AMBIGUOUS_FUNCTION), errmsg("more than one function named \"%s\"", pro_name_or_oid))); PG_RETURN_OID(result); } /* * Normal case: parse the name into components and see if it matches any * pg_proc entries in the current search path. */ names = stringToQualifiedNameList(pro_name_or_oid, "regprocin"); clist = FuncnameGetCandidates(names, -1); if (clist == NULL) ereport(ERROR, (errcode(ERRCODE_UNDEFINED_FUNCTION), errmsg("function \"%s\" does not exist", pro_name_or_oid))); else if (clist->next != NULL) ereport(ERROR, (errcode(ERRCODE_AMBIGUOUS_FUNCTION), errmsg("more than one function named \"%s\"", pro_name_or_oid))); result = clist->oid; PG_RETURN_OID(result); }
Datum spgstat(PG_FUNCTION_ARGS) { text *name=PG_GETARG_TEXT_P(0); char *relname=text_to_cstring(name); RangeVar *relvar; Relation index; List *relname_list; Oid relOid; BlockNumber blkno = SPGIST_HEAD_BLKNO; BlockNumber totalPages = 0, innerPages = 0, emptyPages = 0; double usedSpace = 0.0; char res[1024]; int bufferSize = -1; int64 innerTuples = 0, leafTuples = 0; relname_list = stringToQualifiedNameList(relname); relvar = makeRangeVarFromNameList(relname_list); relOid = RangeVarGetRelid(relvar, false); index = index_open(relOid, AccessExclusiveLock); if ( index->rd_am == NULL ) elog(ERROR, "Relation %s.%s is not an index", get_namespace_name(RelationGetNamespace(index)), RelationGetRelationName(index) ); totalPages = RelationGetNumberOfBlocks(index); for(blkno=SPGIST_HEAD_BLKNO; blkno<totalPages; blkno++) { Buffer buffer; Page page; buffer = ReadBuffer(index, blkno); LockBuffer(buffer, BUFFER_LOCK_SHARE); page = BufferGetPage(buffer); if (SpGistPageIsLeaf(page)) { leafTuples += SpGistPageGetMaxOffset(page); } else { innerPages++; innerTuples += SpGistPageGetMaxOffset(page); } if (bufferSize < 0) bufferSize = BufferGetPageSize(buffer) - MAXALIGN(sizeof(SpGistPageOpaqueData)) - SizeOfPageHeaderData; usedSpace += bufferSize - (PageGetFreeSpace(page) + sizeof(ItemIdData)); if (PageGetFreeSpace(page) + sizeof(ItemIdData) == bufferSize) emptyPages++; UnlockReleaseBuffer(buffer); } index_close(index, AccessExclusiveLock); totalPages--; /* metapage */ snprintf(res, sizeof(res), "totalPages: %u\n" "innerPages: %u\n" "leafPages: %u\n" "emptyPages: %u\n" "usedSpace: %.2f kbytes\n" "freeSpace: %.2f kbytes\n" "fillRatio: %.2f%c\n" "leafTuples: %lld\n" "innerTuples: %lld", totalPages, innerPages, totalPages - innerPages, emptyPages, usedSpace / 1024.0, (( (double) bufferSize ) * ( (double) totalPages ) - usedSpace) / 1024, 100.0 * ( usedSpace / (( (double) bufferSize ) * ( (double) totalPages )) ), '%', leafTuples, innerTuples ); PG_RETURN_TEXT_P(CStringGetTextDatum(res)); }
static Datum tsvector_update_trigger(PG_FUNCTION_ARGS, bool config_column) { TriggerData *trigdata; Trigger *trigger; Relation rel; HeapTuple rettuple = NULL; int tsvector_attr_num, i; ParsedText prs; Datum datum; bool isnull; text *txt; Oid cfgId; /* Check call context */ if (!CALLED_AS_TRIGGER(fcinfo)) /* internal error */ elog(ERROR, "tsvector_update_trigger: not fired by trigger manager"); trigdata = (TriggerData *) fcinfo->context; if (!TRIGGER_FIRED_FOR_ROW(trigdata->tg_event)) elog(ERROR, "tsvector_update_trigger: must be fired for row"); if (!TRIGGER_FIRED_BEFORE(trigdata->tg_event)) elog(ERROR, "tsvector_update_trigger: must be fired BEFORE event"); if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event)) rettuple = trigdata->tg_trigtuple; else if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event)) rettuple = trigdata->tg_newtuple; else elog(ERROR, "tsvector_update_trigger: must be fired for INSERT or UPDATE"); trigger = trigdata->tg_trigger; rel = trigdata->tg_relation; if (trigger->tgnargs < 3) elog(ERROR, "tsvector_update_trigger: arguments must be tsvector_field, ts_config, text_field1, ...)"); /* Find the target tsvector column */ tsvector_attr_num = SPI_fnumber(rel->rd_att, trigger->tgargs[0]); if (tsvector_attr_num == SPI_ERROR_NOATTRIBUTE) ereport(ERROR, (errcode(ERRCODE_UNDEFINED_COLUMN), errmsg("tsvector column \"%s\" does not exist", trigger->tgargs[0]))); if (!IsBinaryCoercible(SPI_gettypeid(rel->rd_att, tsvector_attr_num), TSVECTOROID)) ereport(ERROR, (errcode(ERRCODE_DATATYPE_MISMATCH), errmsg("column \"%s\" is not of tsvector type", trigger->tgargs[0]))); /* Find the configuration to use */ if (config_column) { int config_attr_num; config_attr_num = SPI_fnumber(rel->rd_att, trigger->tgargs[1]); if (config_attr_num == SPI_ERROR_NOATTRIBUTE) ereport(ERROR, (errcode(ERRCODE_UNDEFINED_COLUMN), errmsg("configuration column \"%s\" does not exist", trigger->tgargs[1]))); if (!IsBinaryCoercible(SPI_gettypeid(rel->rd_att, config_attr_num), REGCONFIGOID)) ereport(ERROR, (errcode(ERRCODE_DATATYPE_MISMATCH), errmsg("column \"%s\" is not of regconfig type", trigger->tgargs[1]))); datum = SPI_getbinval(rettuple, rel->rd_att, config_attr_num, &isnull); if (isnull) ereport(ERROR, (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED), errmsg("configuration column \"%s\" must not be null", trigger->tgargs[1]))); cfgId = DatumGetObjectId(datum); } else { List *names; names = stringToQualifiedNameList(trigger->tgargs[1]); /* require a schema so that results are not search path dependent */ if (list_length(names) < 2) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("text search configuration name \"%s\" must be schema-qualified", trigger->tgargs[1]))); cfgId = get_ts_config_oid(names, false); } /* initialize parse state */ prs.lenwords = 32; prs.curwords = 0; prs.pos = 0; prs.words = (ParsedWord *) palloc(sizeof(ParsedWord) * prs.lenwords); /* find all words in indexable column(s) */ for (i = 2; i < trigger->tgnargs; i++) { int numattr; numattr = SPI_fnumber(rel->rd_att, trigger->tgargs[i]); if (numattr == SPI_ERROR_NOATTRIBUTE) ereport(ERROR, (errcode(ERRCODE_UNDEFINED_COLUMN), errmsg("column \"%s\" does not exist", trigger->tgargs[i]))); if (!IsBinaryCoercible(SPI_gettypeid(rel->rd_att, numattr), TEXTOID)) ereport(ERROR, (errcode(ERRCODE_DATATYPE_MISMATCH), errmsg("column \"%s\" is not of a character type", trigger->tgargs[i]))); datum = SPI_getbinval(rettuple, rel->rd_att, numattr, &isnull); if (isnull) continue; txt = DatumGetTextP(datum); parsetext(cfgId, &prs, VARDATA(txt), VARSIZE(txt) - VARHDRSZ); if (txt != (text *) DatumGetPointer(datum)) pfree(txt); } /* make tsvector value */ if (prs.curwords) { datum = PointerGetDatum(make_tsvector(&prs)); rettuple = SPI_modifytuple(rel, rettuple, 1, &tsvector_attr_num, &datum, NULL); pfree(DatumGetPointer(datum)); } else { TSVector out = palloc(CALCDATASIZE(0, 0)); SET_VARSIZE(out, CALCDATASIZE(0, 0)); out->size = 0; datum = PointerGetDatum(out); rettuple = SPI_modifytuple(rel, rettuple, 1, &tsvector_attr_num, &datum, NULL); pfree(prs.words); } if (rettuple == NULL) /* internal error */ elog(ERROR, "tsvector_update_trigger: %d returned by SPI_modifytuple", SPI_result); return PointerGetDatum(rettuple); }
/* * Python only supports prefix unary operators. */ static PyObj unary_operate(const char *op, PyObj right) { PyObj rob = NULL; Datum dright = PyPgObject_GetDatum(right); Oid right_oid = PyPgType_GetOid(Py_TYPE(right)); Py_ALLOCATE_OWNER(); { PyObj rtype; Operator opt; volatile Datum rd = 0; List * volatile namelist = NULL; PG_TRY(); { struct FmgrInfo flinfo = {0,}; struct FunctionCallInfoData fcinfo = {0,}; Form_pg_operator ops; Oid declared, result_type, fn_oid; namelist = stringToQualifiedNameList(op); opt = oper(NULL, (List *) namelist, InvalidOid, right_oid, false, 1); ops = (Form_pg_operator) GETSTRUCT(opt); fn_oid = ops->oprcode; declared = ops->oprright; result_type = ops->oprresult; ReleaseSysCache((HeapTuple) opt); result_type = enforce_generic_type_consistency( &right_oid, &declared, 1, result_type, true); rtype = PyPgType_FromOid(result_type); Py_XACQUIRE(rtype); list_free((List *) namelist); namelist = NULL; if (rtype == NULL) elog(ERROR, "operator result type could not be created"); fmgr_info(fn_oid, &flinfo); fcinfo.flinfo = &flinfo; fcinfo.nargs = 1; fcinfo.arg[0] = dright; fcinfo.argnull[0] = false; rd = FunctionCallInvoke(&fcinfo); if (fcinfo.isnull) { rob = Py_None; Py_INCREF(rob); Py_ACQUIRE(rob); } else { rob = PyPgObject_New(rtype, rd); Py_XACQUIRE(rob); if (PyPgType_ShouldFree(rtype)) pfree(DatumGetPointer(rd)); } } PG_CATCH(); { PyErr_SetPgError(false); rob = NULL; } PG_END_TRY(); Py_XINCREF(rob); } Py_DEALLOCATE_OWNER(); return(rob); }
/* * XXX: If type methods ever come along, hopefully this will be implemented * using a more generalized function. */ static PyObj obj_absolute(PyObj self) { MemoryContext former; Oid typoid; volatile PyObj rob = NULL; if (DB_IS_NOT_READY() || PyPgObjectType_Require(Py_TYPE(self))) return(NULL); typoid = PyPgType_GetOid(Py_TYPE(self)); former = CurrentMemoryContext; PG_TRY(); { HeapTuple procTuple; Datum rd = 0; Oid procoid, roid; List *qnl; qnl = stringToQualifiedNameList("abs"); procoid = LookupFuncName(qnl, 1, &(typoid), true); list_free(qnl); if (procoid == InvalidOid) { PyErr_Format(PyExc_LookupError, "no such function named 'abs' for type %u", typoid); return(NULL); } procTuple = SearchSysCache(PROCOID, procoid, 0, 0, 0); if (procTuple == NULL) { PyErr_Format(PyExc_LookupError, "no procedure with Oid %u", procoid); return(NULL); } roid = ((Form_pg_proc) GETSTRUCT(procTuple))->prorettype; ReleaseSysCache(procTuple); rd = OidFunctionCall1(procoid, PyPgObject_GetDatum(self)); rob = PyPgObject_FromTypeOidAndDatum(roid, rd); if (PyPgType_ShouldFree(Py_TYPE(rob))) { /* * That's our datum... */ if (PyPgObject_GetDatum(self) != rd) pfree(DatumGetPointer(rd)); } } PG_CATCH(); { Py_XDECREF(rob); PyErr_SetPgError(false); return(NULL); } PG_END_TRY(); return(rob); }
static PyObj binary_operate(const char *op, PyObj left, PyObj right) { PyObj base = PyPgObject_Check(left) ? left : right; PyObj rob = NULL; Datum dleft, dright; Datum dcoerce; bool lisnull = false, risnull = false, coerce_isnull = true; Oid left_oid, right_oid; Py_ALLOCATE_OWNER(); { volatile Datum rd = 0; List * volatile namelist = NULL; PyObj rtype; PyObj coerce = NULL; PG_TRY(); { struct FmgrInfo flinfo = {0,}; struct FunctionCallInfoData fcinfo = {0,}; Operator opt; Form_pg_operator ops; Oid actual[2]; Oid declared[2]; Oid result_type, fn_oid; /* * base and coerce are used to manage preliminary coercion. * If either side of the operator is not a PyPgObject, convert the * object to the type of the other side. */ if (base == left) { if (!PyPgObject_Check(right)) coerce = right; } else coerce = left; if (coerce != NULL) { PyPgType_DatumNew((PyObj) Py_TYPE(base), coerce, -1, &dcoerce, &coerce_isnull); if (base == left) { dleft = PyPgObject_GetDatum(left); lisnull = false; dright = dcoerce; risnull = coerce_isnull; } else { dleft = dcoerce; lisnull = coerce_isnull; dright = PyPgObject_GetDatum(right); risnull = false; } /* * Both are the same type as base due to coercion. */ left_oid = right_oid = PyPgType_GetOid(Py_TYPE(base)); } else { /* * Both objects are PyPgObjects. */ dleft = PyPgObject_GetDatum(left); left_oid = PyPgType_GetOid(Py_TYPE(left)); dright = PyPgObject_GetDatum(right); right_oid = PyPgType_GetOid(Py_TYPE(right)); } namelist = stringToQualifiedNameList(op); opt = oper(NULL, (List *) namelist, left_oid, right_oid, false, 1); ops = (Form_pg_operator) GETSTRUCT(opt); fn_oid = ops->oprcode; declared[0] = ops->oprleft; declared[1] = ops->oprright; actual[0] = left_oid; actual[1] = right_oid; result_type = ops->oprresult; ReleaseSysCache((HeapTuple) opt); result_type = enforce_generic_type_consistency( actual, declared, 2, result_type, true); rtype = PyPgType_FromOid(result_type); rtype = Py_XACQUIRE(rtype); list_free((List *) namelist); namelist = NULL; if (rtype == NULL) PyErr_RelayException(); fmgr_info(fn_oid, &flinfo); fcinfo.flinfo = &flinfo; fcinfo.nargs = 2; fcinfo.arg[0] = dleft; fcinfo.argnull[0] = lisnull; fcinfo.arg[1] = dright; fcinfo.argnull[1] = risnull; rd = FunctionCallInvoke(&fcinfo); if (fcinfo.isnull) rob = Py_None; else { rob = PyPgObject_New(rtype, rd); Py_XACQUIRE(rob); if (PyPgType_ShouldFree(rtype)) pfree(DatumGetPointer(rd)); } if (!coerce_isnull && PyPgType_ShouldFree(Py_TYPE(base))) pfree(DatumGetPointer(dcoerce)); } PG_CATCH(); { PyErr_SetPgError(false); rob = NULL; } PG_END_TRY(); Py_XINCREF(rob); } Py_DEALLOCATE_OWNER(); return(rob); }
/* * regoperin - converts "oprname" to operator OID * * We also accept a numeric OID, for symmetry with the output routine. * * '0' signifies unknown (OID 0). In all other cases, the input must * match an existing pg_operator entry. */ Datum regoperin(PG_FUNCTION_ARGS) { char *opr_name_or_oid = PG_GETARG_CSTRING(0); Oid result = InvalidOid; List *names; FuncCandidateList clist; /* '0' ? */ if (strcmp(opr_name_or_oid, "0") == 0) PG_RETURN_OID(InvalidOid); /* Numeric OID? */ if (opr_name_or_oid[0] >= '0' && opr_name_or_oid[0] <= '9' && strspn(opr_name_or_oid, "0123456789") == strlen(opr_name_or_oid)) { result = DatumGetObjectId(DirectFunctionCall1(oidin, CStringGetDatum(opr_name_or_oid))); PG_RETURN_OID(result); } /* Else it's a name, possibly schema-qualified */ /* * In bootstrap mode we assume the given name is not schema-qualified, * and just search pg_operator for a unique match. This is needed for * initializing other system catalogs (pg_namespace may not exist yet, * and certainly there are no schemas other than pg_catalog). */ if (IsBootstrapProcessingMode()) { int matches = 0; Relation hdesc; ScanKeyData skey[1]; SysScanDesc sysscan; HeapTuple tuple; ScanKeyEntryInitialize(&skey[0], 0x0, (AttrNumber) Anum_pg_operator_oprname, (RegProcedure) F_NAMEEQ, CStringGetDatum(opr_name_or_oid)); hdesc = heap_openr(OperatorRelationName, AccessShareLock); sysscan = systable_beginscan(hdesc, OperatorNameNspIndex, true, SnapshotNow, 1, skey); while (HeapTupleIsValid(tuple = systable_getnext(sysscan))) { result = HeapTupleGetOid(tuple); if (++matches > 1) break; } systable_endscan(sysscan); heap_close(hdesc, AccessShareLock); if (matches == 0) ereport(ERROR, (errcode(ERRCODE_UNDEFINED_FUNCTION), errmsg("operator does not exist: %s", opr_name_or_oid))); else if (matches > 1) ereport(ERROR, (errcode(ERRCODE_AMBIGUOUS_FUNCTION), errmsg("more than one operator named %s", opr_name_or_oid))); PG_RETURN_OID(result); } /* * Normal case: parse the name into components and see if it matches * any pg_operator entries in the current search path. */ names = stringToQualifiedNameList(opr_name_or_oid, "regoperin"); clist = OpernameGetCandidates(names, '\0'); if (clist == NULL) ereport(ERROR, (errcode(ERRCODE_UNDEFINED_FUNCTION), errmsg("operator does not exist: %s", opr_name_or_oid))); else if (clist->next != NULL) ereport(ERROR, (errcode(ERRCODE_AMBIGUOUS_FUNCTION), errmsg("more than one operator named %s", opr_name_or_oid))); result = clist->oid; PG_RETURN_OID(result); }
/* * regprocin - converts "proname" to proc OID * * We also accept a numeric OID, for symmetry with the output routine. * * '-' signifies unknown (OID 0). In all other cases, the input must * match an existing pg_proc entry. */ Datum regprocin(PG_FUNCTION_ARGS) { char *pro_name_or_oid = PG_GETARG_CSTRING(0); RegProcedure result = InvalidOid; List *names; FuncCandidateList clist; /* '-' ? */ if (strcmp(pro_name_or_oid, "-") == 0) PG_RETURN_OID(InvalidOid); /* Numeric OID? */ if (pro_name_or_oid[0] >= '0' && pro_name_or_oid[0] <= '9' && strspn(pro_name_or_oid, "0123456789") == strlen(pro_name_or_oid)) { result = DatumGetObjectId(DirectFunctionCall1(oidin, CStringGetDatum(pro_name_or_oid))); PG_RETURN_OID(result); } /* Else it's a name, possibly schema-qualified */ /* * In bootstrap mode we assume the given name is not schema-qualified, and * just search pg_proc for a unique match. This is needed for * initializing other system catalogs (pg_namespace may not exist yet, and * certainly there are no schemas other than pg_catalog). */ if (IsBootstrapProcessingMode()) { int matches = 0; Relation hdesc; ScanKeyData skey[1]; SysScanDesc sysscan; HeapTuple tuple; ScanKeyInit(&skey[0], Anum_pg_proc_proname, BTEqualStrategyNumber, F_NAMEEQ, CStringGetDatum(pro_name_or_oid)); hdesc = heap_open(ProcedureRelationId, AccessShareLock); sysscan = systable_beginscan(hdesc, ProcedureNameArgsNspIndexId, true, NULL, 1, skey); while (HeapTupleIsValid(tuple = systable_getnext(sysscan))) { result = (RegProcedure) HeapTupleGetOid(tuple); if (++matches > 1) break; } systable_endscan(sysscan); heap_close(hdesc, AccessShareLock); if (matches == 0) ereport(ERROR, (errcode(ERRCODE_UNDEFINED_FUNCTION), errmsg("function \"%s\" does not exist", pro_name_or_oid))); else if (matches > 1) ereport(ERROR, (errcode(ERRCODE_AMBIGUOUS_FUNCTION), errmsg("more than one function named \"%s\"", pro_name_or_oid))); PG_RETURN_OID(result); } /* * Normal case: parse the name into components and see if it matches any * pg_proc entries in the current search path. */ names = stringToQualifiedNameList(pro_name_or_oid); clist = FuncnameGetCandidates(names, -1, NIL, false, false, false); if (clist == NULL) ereport(ERROR, (errcode(ERRCODE_UNDEFINED_FUNCTION), errmsg("function \"%s\" does not exist", pro_name_or_oid))); else if (clist->next != NULL) ereport(ERROR, (errcode(ERRCODE_AMBIGUOUS_FUNCTION), errmsg("more than one function named \"%s\"", pro_name_or_oid))); result = clist->oid; PG_RETURN_OID(result); }
/* * regclassin - converts "classname" to class OID * * We also accept a numeric OID, for symmetry with the output routine. * * '-' signifies unknown (OID 0). In all other cases, the input must * match an existing pg_class entry. */ Datum regclassin(PG_FUNCTION_ARGS) { char *class_name_or_oid = PG_GETARG_CSTRING(0); Oid result = InvalidOid; List *names; /* '-' ? */ if (strcmp(class_name_or_oid, "-") == 0) PG_RETURN_OID(InvalidOid); /* Numeric OID? */ if (class_name_or_oid[0] >= '0' && class_name_or_oid[0] <= '9' && strspn(class_name_or_oid, "0123456789") == strlen(class_name_or_oid)) { result = DatumGetObjectId(DirectFunctionCall1(oidin, CStringGetDatum(class_name_or_oid))); PG_RETURN_OID(result); } /* Else it's a name, possibly schema-qualified */ /* * In bootstrap mode we assume the given name is not schema-qualified, and * just search pg_class for a match. This is needed for initializing * other system catalogs (pg_namespace may not exist yet, and certainly * there are no schemas other than pg_catalog). */ if (IsBootstrapProcessingMode()) { Relation hdesc; ScanKeyData skey[1]; SysScanDesc sysscan; HeapTuple tuple; ScanKeyInit(&skey[0], Anum_pg_class_relname, BTEqualStrategyNumber, F_NAMEEQ, CStringGetDatum(class_name_or_oid)); hdesc = heap_open(RelationRelationId, AccessShareLock); sysscan = systable_beginscan(hdesc, ClassNameNspIndexId, true, NULL, 1, skey); if (HeapTupleIsValid(tuple = systable_getnext(sysscan))) result = HeapTupleGetOid(tuple); else ereport(ERROR, (errcode(ERRCODE_UNDEFINED_TABLE), errmsg("relation \"%s\" does not exist", class_name_or_oid))); /* We assume there can be only one match */ systable_endscan(sysscan); heap_close(hdesc, AccessShareLock); PG_RETURN_OID(result); } /* * Normal case: parse the name into components and see if it matches any * pg_class entries in the current search path. */ names = stringToQualifiedNameList(class_name_or_oid); /* We might not even have permissions on this relation; don't lock it. */ result = RangeVarGetRelid(makeRangeVarFromNameList(names), NoLock, false); PG_RETURN_OID(result); }
/** * @brief Parse function expression */ ParsedFunction ParseFunction(const char *value, bool argistype) { int i; ParsedFunction ret; char *buf; const char *nextp; bool done = false; List *names; ListCell *l; int nargs; FuncCandidateList candidates; FuncCandidateList find = NULL; int ncandidates = 0; HeapTuple ftup; Form_pg_proc pp; AclResult aclresult; buf = palloc(strlen(value) + 1); /* parse function name */ nextp = value; do { if (*nextp == '\"') { /* Quoted name */ for (;;) { nextp = strchr(nextp + 1, '\"'); /* mismatched quotes */ if (nextp == NULL) ereport(ERROR, (errcode(ERRCODE_SYNTAX_ERROR), errmsg("function call syntax error: %s", value))); if (nextp[1] != '\"') break; /* found end of quoted name */ } /* nextp now points at the terminating quote */ nextp = nextp + 1; } else if (IsIdentStart((unsigned char) *nextp)) { /* Unquoted name */ nextp++; while (IsIdentContent((unsigned char) *nextp)) nextp++; } else { /* invalid syntax */ ereport(ERROR, (errcode(ERRCODE_SYNTAX_ERROR), errmsg("function call syntax error: %s", value))); } while (isspace((unsigned char) *nextp)) nextp++; /* skip trailing whitespace */ if (*nextp == '.') { nextp++; while (isspace((unsigned char) *nextp)) nextp++; /* skip leading whitespace for next */ /* we expect another name, so done remains false */ } else if (*nextp == '\0' || *nextp == '(') done = true; else { /* invalid syntax */ ereport(ERROR, (errcode(ERRCODE_SYNTAX_ERROR), errmsg("function call syntax error: %s", value))); } /* Loop back if we didn't reach end of function name */ } while (!done); strncpy(buf, value, nextp - value); buf[nextp - value] = '\0'; names = stringToQualifiedNameList(buf); pfree(buf); if (*nextp == '\0') { if (!argistype) ereport(ERROR, (errcode(ERRCODE_SYNTAX_ERROR), errmsg("function call syntax error: %s", value))); nargs = -1; /* Get list of possible candidates from namespace search */ candidates = FuncnameGetCandidates(names, nargs, NIL, false, false); } else { /* parse function arguments */ nargs = 0; while (GetNextArgument(nextp, &ret.args[nargs], &ret.argtypes[nargs], &nextp, value, argistype)) { nargs++; if (nargs > FUNC_MAX_ARGS) ereport(ERROR, (errcode(ERRCODE_TOO_MANY_ARGUMENTS), errmsg("functions cannot have more than %d arguments", FUNC_MAX_ARGS))); } /* Get list of possible candidates from namespace search */ candidates = FuncnameGetCandidates(names, nargs, NIL, true, true); } /* so now try to match up candidates */ if (!argistype) { FuncCandidateList current_candidates; ncandidates = func_match_argtypes(nargs, ret.argtypes, candidates, ¤t_candidates); /* one match only? then run with it... */ if (ncandidates == 1) find = current_candidates; /* multiple candidates? then better decide or throw an error... */ else if (ncandidates > 1) { find = func_select_candidate(nargs, ret.argtypes, current_candidates); } } else if (nargs > 0) { /* Quickly check if there is an exact match to the input datatypes */ for (find = candidates; find; find = find->next) { if (memcmp(find->args, ret.argtypes, nargs * sizeof(Oid)) == 0) { ncandidates = 1; break; } } } else { FuncCandidateList c; for (c = candidates; c; c = c->next) ncandidates++; find = candidates; } if (ncandidates == 0) ereport(ERROR, (errcode(ERRCODE_UNDEFINED_FUNCTION), errmsg("function %s does not exist", func_signature_string(names, nargs, NIL, ret.argtypes)), errhint("No function matches the given name and argument types."))); /* * If we were able to choose a best candidate, we're done. * Otherwise, ambiguous function call. */ if (ncandidates > 1 || !OidIsValid(find->oid)) ereport(ERROR, (errcode(ERRCODE_AMBIGUOUS_FUNCTION), errmsg("function %s is not unique", func_signature_string(names, nargs, NIL, ret.argtypes)), errhint("Could not choose a best candidate function."))); foreach (l, names) { Value *v = lfirst(l); pfree(strVal(v)); pfree(v); }