2002-04-15 07:22:04 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* operatorcmds.c
|
|
|
|
*
|
|
|
|
* Routines for operator manipulation commands
|
|
|
|
*
|
2015-01-06 17:43:47 +01:00
|
|
|
* Portions Copyright (c) 1996-2015, PostgreSQL Global Development Group
|
2002-04-15 07:22:04 +02:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2010-09-20 22:08:53 +02:00
|
|
|
* src/backend/commands/operatorcmds.c
|
2002-04-15 07:22:04 +02:00
|
|
|
*
|
|
|
|
* DESCRIPTION
|
|
|
|
* The "DefineFoo" routines take the parse tree and pick out the
|
|
|
|
* appropriate arguments/flags, passing the results to the
|
|
|
|
* corresponding "FooDefine" routines (in src/catalog) that do
|
|
|
|
* the actual catalog-munging. These routines also verify permission
|
|
|
|
* of the user to execute the command.
|
|
|
|
*
|
|
|
|
* NOTES
|
|
|
|
* These things must be defined and committed in the following order:
|
|
|
|
* "create function":
|
|
|
|
* input/output, recv/send procedures
|
|
|
|
* "create type":
|
|
|
|
* type
|
|
|
|
* "create operator":
|
|
|
|
* operators
|
|
|
|
*
|
|
|
|
* Most of the parse-tree manipulation routines are defined in
|
|
|
|
* commands/manip.c.
|
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
|
|
|
|
|
|
|
#include "access/heapam.h"
|
2012-08-30 22:15:44 +02:00
|
|
|
#include "access/htup_details.h"
|
2002-07-12 20:43:19 +02:00
|
|
|
#include "catalog/dependency.h"
|
2004-06-25 23:55:59 +02:00
|
|
|
#include "catalog/indexing.h"
|
2002-04-15 07:22:04 +02:00
|
|
|
#include "catalog/pg_operator.h"
|
2008-08-16 02:01:38 +02:00
|
|
|
#include "catalog/pg_type.h"
|
2010-11-26 23:27:23 +01:00
|
|
|
#include "commands/alter.h"
|
2002-04-15 07:22:04 +02:00
|
|
|
#include "commands/defrem.h"
|
|
|
|
#include "miscadmin.h"
|
2008-08-16 02:01:38 +02:00
|
|
|
#include "parser/parse_func.h"
|
2002-04-17 01:08:12 +02:00
|
|
|
#include "parser/parse_oper.h"
|
2002-04-15 07:22:04 +02:00
|
|
|
#include "parser/parse_type.h"
|
2011-12-19 23:05:19 +01:00
|
|
|
#include "utils/builtins.h"
|
2002-04-27 05:45:03 +02:00
|
|
|
#include "utils/lsyscache.h"
|
2008-06-19 02:46:06 +02:00
|
|
|
#include "utils/rel.h"
|
2002-04-15 07:22:04 +02:00
|
|
|
#include "utils/syscache.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* DefineOperator
|
|
|
|
* this function extracts all the information from the
|
|
|
|
* parameter list generated by the parser and then has
|
|
|
|
* OperatorCreate() do all the actual work.
|
|
|
|
*
|
|
|
|
* 'parameters' is a list of DefElem
|
|
|
|
*/
|
Change many routines to return ObjectAddress rather than OID
The changed routines are mostly those that can be directly called by
ProcessUtilitySlow; the intention is to make the affected object
information more precise, in support for future event trigger changes.
Originally it was envisioned that the OID of the affected object would
be enough, and in most cases that is correct, but upon actually
implementing the event trigger changes it turned out that ObjectAddress
is more widely useful.
Additionally, some command execution routines grew an output argument
that's an object address which provides further info about the executed
command. To wit:
* for ALTER DOMAIN / ADD CONSTRAINT, it corresponds to the address of
the new constraint
* for ALTER OBJECT / SET SCHEMA, it corresponds to the address of the
schema that originally contained the object.
* for ALTER EXTENSION {ADD, DROP} OBJECT, it corresponds to the address
of the object added to or dropped from the extension.
There's no user-visible change in this commit, and no functional change
either.
Discussion: 20150218213255.GC6717@tamriel.snowman.net
Reviewed-By: Stephen Frost, Andres Freund
2015-03-03 18:10:50 +01:00
|
|
|
ObjectAddress
|
2002-04-15 07:22:04 +02:00
|
|
|
DefineOperator(List *names, List *parameters)
|
|
|
|
{
|
|
|
|
char *oprName;
|
|
|
|
Oid oprNamespace;
|
2002-04-27 05:45:03 +02:00
|
|
|
AclResult aclresult;
|
2002-04-17 01:08:12 +02:00
|
|
|
bool canMerge = false; /* operator merges */
|
2007-11-15 22:14:46 +01:00
|
|
|
bool canHash = false; /* operator hashes */
|
2002-04-17 01:08:12 +02:00
|
|
|
List *functionName = NIL; /* function for operator */
|
2002-04-15 07:22:04 +02:00
|
|
|
TypeName *typeName1 = NULL; /* first type name */
|
|
|
|
TypeName *typeName2 = NULL; /* second type name */
|
|
|
|
Oid typeId1 = InvalidOid; /* types converted to OID */
|
|
|
|
Oid typeId2 = InvalidOid;
|
2011-12-19 23:05:19 +01:00
|
|
|
Oid rettype;
|
2005-10-15 04:49:52 +02:00
|
|
|
List *commutatorName = NIL; /* optional commutator operator name */
|
2002-04-17 01:08:12 +02:00
|
|
|
List *negatorName = NIL; /* optional negator operator name */
|
2005-10-15 04:49:52 +02:00
|
|
|
List *restrictionName = NIL; /* optional restrict. sel. procedure */
|
2002-09-04 22:31:48 +02:00
|
|
|
List *joinName = NIL; /* optional join sel. procedure */
|
2008-08-16 02:01:38 +02:00
|
|
|
Oid functionOid; /* functions converted to OID */
|
|
|
|
Oid restrictionOid;
|
|
|
|
Oid joinOid;
|
|
|
|
Oid typeId[5]; /* only need up to 5 args here */
|
|
|
|
int nargs;
|
2004-05-26 06:41:50 +02:00
|
|
|
ListCell *pl;
|
2002-04-15 07:22:04 +02:00
|
|
|
|
|
|
|
/* Convert list of names to a name and namespace */
|
|
|
|
oprNamespace = QualifiedNameGetCreationNamespace(names, &oprName);
|
|
|
|
|
2002-04-27 05:45:03 +02:00
|
|
|
/* Check we have creation rights in target namespace */
|
|
|
|
aclresult = pg_namespace_aclcheck(oprNamespace, GetUserId(), ACL_CREATE);
|
|
|
|
if (aclresult != ACLCHECK_OK)
|
2003-08-01 02:15:26 +02:00
|
|
|
aclcheck_error(aclresult, ACL_KIND_NAMESPACE,
|
|
|
|
get_namespace_name(oprNamespace));
|
2002-04-27 05:45:03 +02:00
|
|
|
|
2002-04-15 07:22:04 +02:00
|
|
|
/*
|
|
|
|
* loop over the definition list and extract the information we need.
|
|
|
|
*/
|
|
|
|
foreach(pl, parameters)
|
|
|
|
{
|
|
|
|
DefElem *defel = (DefElem *) lfirst(pl);
|
|
|
|
|
2004-05-07 02:24:59 +02:00
|
|
|
if (pg_strcasecmp(defel->defname, "leftarg") == 0)
|
2002-04-15 07:22:04 +02:00
|
|
|
{
|
|
|
|
typeName1 = defGetTypeName(defel);
|
|
|
|
if (typeName1->setof)
|
2003-07-20 23:56:35 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
|
2007-06-03 01:36:35 +02:00
|
|
|
errmsg("SETOF type not allowed for operator argument")));
|
2002-04-15 07:22:04 +02:00
|
|
|
}
|
2004-05-07 02:24:59 +02:00
|
|
|
else if (pg_strcasecmp(defel->defname, "rightarg") == 0)
|
2002-04-15 07:22:04 +02:00
|
|
|
{
|
|
|
|
typeName2 = defGetTypeName(defel);
|
|
|
|
if (typeName2->setof)
|
2003-07-20 23:56:35 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
|
2007-06-03 01:36:35 +02:00
|
|
|
errmsg("SETOF type not allowed for operator argument")));
|
2002-04-15 07:22:04 +02:00
|
|
|
}
|
2004-05-07 02:24:59 +02:00
|
|
|
else if (pg_strcasecmp(defel->defname, "procedure") == 0)
|
2002-04-17 01:08:12 +02:00
|
|
|
functionName = defGetQualifiedName(defel);
|
2004-05-07 02:24:59 +02:00
|
|
|
else if (pg_strcasecmp(defel->defname, "commutator") == 0)
|
2002-04-17 01:08:12 +02:00
|
|
|
commutatorName = defGetQualifiedName(defel);
|
2004-05-07 02:24:59 +02:00
|
|
|
else if (pg_strcasecmp(defel->defname, "negator") == 0)
|
2002-04-17 01:08:12 +02:00
|
|
|
negatorName = defGetQualifiedName(defel);
|
2004-05-07 02:24:59 +02:00
|
|
|
else if (pg_strcasecmp(defel->defname, "restrict") == 0)
|
2002-04-17 01:08:12 +02:00
|
|
|
restrictionName = defGetQualifiedName(defel);
|
2004-05-07 02:24:59 +02:00
|
|
|
else if (pg_strcasecmp(defel->defname, "join") == 0)
|
2002-04-17 01:08:12 +02:00
|
|
|
joinName = defGetQualifiedName(defel);
|
2004-05-07 02:24:59 +02:00
|
|
|
else if (pg_strcasecmp(defel->defname, "hashes") == 0)
|
2004-05-14 18:11:25 +02:00
|
|
|
canHash = defGetBoolean(defel);
|
2004-05-07 02:24:59 +02:00
|
|
|
else if (pg_strcasecmp(defel->defname, "merges") == 0)
|
2004-05-14 18:11:25 +02:00
|
|
|
canMerge = defGetBoolean(defel);
|
2006-12-23 01:43:13 +01:00
|
|
|
/* These obsolete options are taken as meaning canMerge */
|
2004-05-07 02:24:59 +02:00
|
|
|
else if (pg_strcasecmp(defel->defname, "sort1") == 0)
|
2006-12-23 01:43:13 +01:00
|
|
|
canMerge = true;
|
2004-05-07 02:24:59 +02:00
|
|
|
else if (pg_strcasecmp(defel->defname, "sort2") == 0)
|
2006-12-23 01:43:13 +01:00
|
|
|
canMerge = true;
|
2004-05-07 02:24:59 +02:00
|
|
|
else if (pg_strcasecmp(defel->defname, "ltcmp") == 0)
|
2006-12-23 01:43:13 +01:00
|
|
|
canMerge = true;
|
2004-05-07 02:24:59 +02:00
|
|
|
else if (pg_strcasecmp(defel->defname, "gtcmp") == 0)
|
2006-12-23 01:43:13 +01:00
|
|
|
canMerge = true;
|
2002-04-15 07:22:04 +02:00
|
|
|
else
|
2003-07-20 23:56:35 +02:00
|
|
|
ereport(WARNING,
|
|
|
|
(errcode(ERRCODE_SYNTAX_ERROR),
|
|
|
|
errmsg("operator attribute \"%s\" not recognized",
|
|
|
|
defel->defname)));
|
2002-04-15 07:22:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* make sure we have our required definitions
|
|
|
|
*/
|
2002-04-17 01:08:12 +02:00
|
|
|
if (functionName == NIL)
|
2003-07-20 23:56:35 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
|
|
|
|
errmsg("operator procedure must be specified")));
|
2002-04-15 07:22:04 +02:00
|
|
|
|
|
|
|
/* Transform type names to type OIDs */
|
|
|
|
if (typeName1)
|
2010-10-25 20:40:46 +02:00
|
|
|
typeId1 = typenameTypeId(NULL, typeName1);
|
2002-04-15 07:22:04 +02:00
|
|
|
if (typeName2)
|
2010-10-25 20:40:46 +02:00
|
|
|
typeId2 = typenameTypeId(NULL, typeName2);
|
2002-04-15 07:22:04 +02:00
|
|
|
|
2008-08-16 02:01:38 +02:00
|
|
|
if (!OidIsValid(typeId1) && !OidIsValid(typeId2))
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
|
|
|
|
errmsg("at least one of leftarg or rightarg must be specified")));
|
|
|
|
|
2011-12-19 23:05:19 +01:00
|
|
|
if (typeName1)
|
|
|
|
{
|
|
|
|
aclresult = pg_type_aclcheck(typeId1, GetUserId(), ACL_USAGE);
|
|
|
|
if (aclresult != ACLCHECK_OK)
|
2012-06-15 21:55:03 +02:00
|
|
|
aclcheck_error_type(aclresult, typeId1);
|
2011-12-19 23:05:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (typeName2)
|
|
|
|
{
|
|
|
|
aclresult = pg_type_aclcheck(typeId2, GetUserId(), ACL_USAGE);
|
|
|
|
if (aclresult != ACLCHECK_OK)
|
2012-06-15 21:55:03 +02:00
|
|
|
aclcheck_error_type(aclresult, typeId2);
|
2011-12-19 23:05:19 +01:00
|
|
|
}
|
|
|
|
|
2008-08-16 02:01:38 +02:00
|
|
|
/*
|
|
|
|
* Look up the operator's underlying function.
|
|
|
|
*/
|
|
|
|
if (!OidIsValid(typeId1))
|
|
|
|
{
|
|
|
|
typeId[0] = typeId2;
|
|
|
|
nargs = 1;
|
|
|
|
}
|
|
|
|
else if (!OidIsValid(typeId2))
|
|
|
|
{
|
|
|
|
typeId[0] = typeId1;
|
|
|
|
nargs = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
typeId[0] = typeId1;
|
|
|
|
typeId[1] = typeId2;
|
|
|
|
nargs = 2;
|
|
|
|
}
|
|
|
|
functionOid = LookupFuncName(functionName, nargs, typeId, false);
|
|
|
|
|
|
|
|
/*
|
2014-05-06 18:12:18 +02:00
|
|
|
* We require EXECUTE rights for the function. This isn't strictly
|
2009-06-11 16:49:15 +02:00
|
|
|
* necessary, since EXECUTE will be checked at any attempted use of the
|
|
|
|
* operator, but it seems like a good idea anyway.
|
2008-08-16 02:01:38 +02:00
|
|
|
*/
|
|
|
|
aclresult = pg_proc_aclcheck(functionOid, GetUserId(), ACL_EXECUTE);
|
|
|
|
if (aclresult != ACLCHECK_OK)
|
|
|
|
aclcheck_error(aclresult, ACL_KIND_PROC,
|
|
|
|
NameListToString(functionName));
|
|
|
|
|
2011-12-19 23:05:19 +01:00
|
|
|
rettype = get_func_rettype(functionOid);
|
|
|
|
aclresult = pg_type_aclcheck(rettype, GetUserId(), ACL_USAGE);
|
|
|
|
if (aclresult != ACLCHECK_OK)
|
2012-06-15 21:55:03 +02:00
|
|
|
aclcheck_error_type(aclresult, rettype);
|
2011-12-19 23:05:19 +01:00
|
|
|
|
2008-08-16 02:01:38 +02:00
|
|
|
/*
|
|
|
|
* Look up restriction estimator if specified
|
|
|
|
*/
|
|
|
|
if (restrictionName)
|
|
|
|
{
|
|
|
|
typeId[0] = INTERNALOID; /* PlannerInfo */
|
|
|
|
typeId[1] = OIDOID; /* operator OID */
|
|
|
|
typeId[2] = INTERNALOID; /* args list */
|
|
|
|
typeId[3] = INT4OID; /* varRelid */
|
|
|
|
|
|
|
|
restrictionOid = LookupFuncName(restrictionName, 4, typeId, false);
|
|
|
|
|
|
|
|
/* estimators must return float8 */
|
|
|
|
if (get_func_rettype(restrictionOid) != FLOAT8OID)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
|
|
|
|
errmsg("restriction estimator function %s must return type \"float8\"",
|
|
|
|
NameListToString(restrictionName))));
|
|
|
|
|
|
|
|
/* Require EXECUTE rights for the estimator */
|
|
|
|
aclresult = pg_proc_aclcheck(restrictionOid, GetUserId(), ACL_EXECUTE);
|
|
|
|
if (aclresult != ACLCHECK_OK)
|
|
|
|
aclcheck_error(aclresult, ACL_KIND_PROC,
|
|
|
|
NameListToString(restrictionName));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
restrictionOid = InvalidOid;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Look up join estimator if specified
|
|
|
|
*/
|
|
|
|
if (joinName)
|
|
|
|
{
|
|
|
|
typeId[0] = INTERNALOID; /* PlannerInfo */
|
|
|
|
typeId[1] = OIDOID; /* operator OID */
|
|
|
|
typeId[2] = INTERNALOID; /* args list */
|
|
|
|
typeId[3] = INT2OID; /* jointype */
|
|
|
|
typeId[4] = INTERNALOID; /* SpecialJoinInfo */
|
|
|
|
|
|
|
|
/*
|
2009-06-11 16:49:15 +02:00
|
|
|
* As of Postgres 8.4, the preferred signature for join estimators has
|
|
|
|
* 5 arguments, but we still allow the old 4-argument form. Try the
|
|
|
|
* preferred form first.
|
2008-08-16 02:01:38 +02:00
|
|
|
*/
|
|
|
|
joinOid = LookupFuncName(joinName, 5, typeId, true);
|
|
|
|
if (!OidIsValid(joinOid))
|
|
|
|
joinOid = LookupFuncName(joinName, 4, typeId, true);
|
|
|
|
/* If not found, reference the 5-argument signature in error msg */
|
|
|
|
if (!OidIsValid(joinOid))
|
|
|
|
joinOid = LookupFuncName(joinName, 5, typeId, false);
|
|
|
|
|
|
|
|
/* estimators must return float8 */
|
|
|
|
if (get_func_rettype(joinOid) != FLOAT8OID)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
|
2009-06-11 16:49:15 +02:00
|
|
|
errmsg("join estimator function %s must return type \"float8\"",
|
|
|
|
NameListToString(joinName))));
|
2008-08-16 02:01:38 +02:00
|
|
|
|
|
|
|
/* Require EXECUTE rights for the estimator */
|
|
|
|
aclresult = pg_proc_aclcheck(joinOid, GetUserId(), ACL_EXECUTE);
|
|
|
|
if (aclresult != ACLCHECK_OK)
|
|
|
|
aclcheck_error(aclresult, ACL_KIND_PROC,
|
|
|
|
NameListToString(joinName));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
joinOid = InvalidOid;
|
|
|
|
|
2002-04-15 07:22:04 +02:00
|
|
|
/*
|
|
|
|
* now have OperatorCreate do all the work..
|
|
|
|
*/
|
2012-12-24 00:25:03 +01:00
|
|
|
return
|
2013-05-29 22:58:43 +02:00
|
|
|
OperatorCreate(oprName, /* operator name */
|
2012-12-24 00:25:03 +01:00
|
|
|
oprNamespace, /* namespace */
|
2013-05-29 22:58:43 +02:00
|
|
|
typeId1, /* left type id */
|
|
|
|
typeId2, /* right type id */
|
|
|
|
functionOid, /* function for operator */
|
|
|
|
commutatorName, /* optional commutator operator name */
|
|
|
|
negatorName, /* optional negator operator name */
|
|
|
|
restrictionOid, /* optional restrict. sel. procedure */
|
|
|
|
joinOid, /* optional join sel. procedure name */
|
2012-12-24 00:25:03 +01:00
|
|
|
canMerge, /* operator merges */
|
|
|
|
canHash); /* operator hashes */
|
2002-04-15 07:22:04 +02:00
|
|
|
}
|
|
|
|
|
2002-07-12 20:43:19 +02:00
|
|
|
/*
|
|
|
|
* Guts of operator deletion.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
RemoveOperatorById(Oid operOid)
|
|
|
|
{
|
|
|
|
Relation relation;
|
|
|
|
HeapTuple tup;
|
|
|
|
|
2005-04-14 22:03:27 +02:00
|
|
|
relation = heap_open(OperatorRelationId, RowExclusiveLock);
|
2002-07-12 20:43:19 +02:00
|
|
|
|
2010-02-14 19:42:19 +01:00
|
|
|
tup = SearchSysCache1(OPEROID, ObjectIdGetDatum(operOid));
|
2002-09-04 22:31:48 +02:00
|
|
|
if (!HeapTupleIsValid(tup)) /* should not happen */
|
2003-07-20 23:56:35 +02:00
|
|
|
elog(ERROR, "cache lookup failed for operator %u", operOid);
|
2002-04-15 07:22:04 +02:00
|
|
|
|
2002-04-17 01:08:12 +02:00
|
|
|
simple_heap_delete(relation, &tup->t_self);
|
2002-04-15 07:22:04 +02:00
|
|
|
|
2002-07-12 20:43:19 +02:00
|
|
|
ReleaseSysCache(tup);
|
|
|
|
|
2002-04-15 07:22:04 +02:00
|
|
|
heap_close(relation, RowExclusiveLock);
|
|
|
|
}
|