1 /*-------------------------------------------------------------------------
5 * Routines for operator manipulation commands
7 * Portions Copyright (c) 1996-2015, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
12 * src/backend/commands/operatorcmds.c
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.
22 * These things must be defined and committed in the following order:
24 * input/output, recv/send procedures
30 * Most of the parse-tree manipulation routines are defined in
33 *-------------------------------------------------------------------------
37 #include "access/heapam.h"
38 #include "access/htup_details.h"
39 #include "catalog/dependency.h"
40 #include "catalog/indexing.h"
41 #include "catalog/objectaccess.h"
42 #include "catalog/pg_operator.h"
43 #include "catalog/pg_operator_fn.h"
44 #include "catalog/pg_type.h"
45 #include "commands/alter.h"
46 #include "commands/defrem.h"
47 #include "miscadmin.h"
48 #include "parser/parse_func.h"
49 #include "parser/parse_oper.h"
50 #include "parser/parse_type.h"
51 #include "utils/builtins.h"
52 #include "utils/lsyscache.h"
53 #include "utils/rel.h"
54 #include "utils/syscache.h"
56 static Oid ValidateRestrictionEstimator(List *restrictionName);
57 static Oid ValidateJoinEstimator(List *joinName);
61 * this function extracts all the information from the
62 * parameter list generated by the parser and then has
63 * OperatorCreate() do all the actual work.
65 * 'parameters' is a list of DefElem
68 DefineOperator(List *names, List *parameters)
73 bool canMerge = false; /* operator merges */
74 bool canHash = false; /* operator hashes */
75 List *functionName = NIL; /* function for operator */
76 TypeName *typeName1 = NULL; /* first type name */
77 TypeName *typeName2 = NULL; /* second type name */
78 Oid typeId1 = InvalidOid; /* types converted to OID */
79 Oid typeId2 = InvalidOid;
81 List *commutatorName = NIL; /* optional commutator operator name */
82 List *negatorName = NIL; /* optional negator operator name */
83 List *restrictionName = NIL; /* optional restrict. sel. procedure */
84 List *joinName = NIL; /* optional join sel. procedure */
85 Oid functionOid; /* functions converted to OID */
88 Oid typeId[2]; /* to hold left and right arg */
92 /* Convert list of names to a name and namespace */
93 oprNamespace = QualifiedNameGetCreationNamespace(names, &oprName);
95 /* Check we have creation rights in target namespace */
96 aclresult = pg_namespace_aclcheck(oprNamespace, GetUserId(), ACL_CREATE);
97 if (aclresult != ACLCHECK_OK)
98 aclcheck_error(aclresult, ACL_KIND_NAMESPACE,
99 get_namespace_name(oprNamespace));
102 * loop over the definition list and extract the information we need.
104 foreach(pl, parameters)
106 DefElem *defel = (DefElem *) lfirst(pl);
108 if (pg_strcasecmp(defel->defname, "leftarg") == 0)
110 typeName1 = defGetTypeName(defel);
111 if (typeName1->setof)
113 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
114 errmsg("SETOF type not allowed for operator argument")));
116 else if (pg_strcasecmp(defel->defname, "rightarg") == 0)
118 typeName2 = defGetTypeName(defel);
119 if (typeName2->setof)
121 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
122 errmsg("SETOF type not allowed for operator argument")));
124 else if (pg_strcasecmp(defel->defname, "procedure") == 0)
125 functionName = defGetQualifiedName(defel);
126 else if (pg_strcasecmp(defel->defname, "commutator") == 0)
127 commutatorName = defGetQualifiedName(defel);
128 else if (pg_strcasecmp(defel->defname, "negator") == 0)
129 negatorName = defGetQualifiedName(defel);
130 else if (pg_strcasecmp(defel->defname, "restrict") == 0)
131 restrictionName = defGetQualifiedName(defel);
132 else if (pg_strcasecmp(defel->defname, "join") == 0)
133 joinName = defGetQualifiedName(defel);
134 else if (pg_strcasecmp(defel->defname, "hashes") == 0)
135 canHash = defGetBoolean(defel);
136 else if (pg_strcasecmp(defel->defname, "merges") == 0)
137 canMerge = defGetBoolean(defel);
138 /* These obsolete options are taken as meaning canMerge */
139 else if (pg_strcasecmp(defel->defname, "sort1") == 0)
141 else if (pg_strcasecmp(defel->defname, "sort2") == 0)
143 else if (pg_strcasecmp(defel->defname, "ltcmp") == 0)
145 else if (pg_strcasecmp(defel->defname, "gtcmp") == 0)
149 /* WARNING, not ERROR, for historical backwards-compatibility */
151 (errcode(ERRCODE_SYNTAX_ERROR),
152 errmsg("operator attribute \"%s\" not recognized",
158 * make sure we have our required definitions
160 if (functionName == NIL)
162 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
163 errmsg("operator procedure must be specified")));
165 /* Transform type names to type OIDs */
167 typeId1 = typenameTypeId(NULL, typeName1);
169 typeId2 = typenameTypeId(NULL, typeName2);
171 if (!OidIsValid(typeId1) && !OidIsValid(typeId2))
173 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
174 errmsg("at least one of leftarg or rightarg must be specified")));
178 aclresult = pg_type_aclcheck(typeId1, GetUserId(), ACL_USAGE);
179 if (aclresult != ACLCHECK_OK)
180 aclcheck_error_type(aclresult, typeId1);
185 aclresult = pg_type_aclcheck(typeId2, GetUserId(), ACL_USAGE);
186 if (aclresult != ACLCHECK_OK)
187 aclcheck_error_type(aclresult, typeId2);
191 * Look up the operator's underlying function.
193 if (!OidIsValid(typeId1))
198 else if (!OidIsValid(typeId2))
209 functionOid = LookupFuncName(functionName, nargs, typeId, false);
212 * We require EXECUTE rights for the function. This isn't strictly
213 * necessary, since EXECUTE will be checked at any attempted use of the
214 * operator, but it seems like a good idea anyway.
216 aclresult = pg_proc_aclcheck(functionOid, GetUserId(), ACL_EXECUTE);
217 if (aclresult != ACLCHECK_OK)
218 aclcheck_error(aclresult, ACL_KIND_PROC,
219 NameListToString(functionName));
221 rettype = get_func_rettype(functionOid);
222 aclresult = pg_type_aclcheck(rettype, GetUserId(), ACL_USAGE);
223 if (aclresult != ACLCHECK_OK)
224 aclcheck_error_type(aclresult, rettype);
227 * Look up restriction and join estimators if specified
230 restrictionOid = ValidateRestrictionEstimator(restrictionName);
232 restrictionOid = InvalidOid;
234 joinOid = ValidateJoinEstimator(joinName);
236 joinOid = InvalidOid;
239 * now have OperatorCreate do all the work..
242 OperatorCreate(oprName, /* operator name */
243 oprNamespace, /* namespace */
244 typeId1, /* left type id */
245 typeId2, /* right type id */
246 functionOid, /* function for operator */
247 commutatorName, /* optional commutator operator name */
248 negatorName, /* optional negator operator name */
249 restrictionOid, /* optional restrict. sel. procedure */
250 joinOid, /* optional join sel. procedure name */
251 canMerge, /* operator merges */
252 canHash); /* operator hashes */
256 * Look up a restriction estimator function ny name, and verify that it has
257 * the correct signature and we have the permissions to attach it to an
261 ValidateRestrictionEstimator(List *restrictionName)
267 typeId[0] = INTERNALOID; /* PlannerInfo */
268 typeId[1] = OIDOID; /* operator OID */
269 typeId[2] = INTERNALOID; /* args list */
270 typeId[3] = INT4OID; /* varRelid */
272 restrictionOid = LookupFuncName(restrictionName, 4, typeId, false);
274 /* estimators must return float8 */
275 if (get_func_rettype(restrictionOid) != FLOAT8OID)
277 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
278 errmsg("restriction estimator function %s must return type \"float8\"",
279 NameListToString(restrictionName))));
281 /* Require EXECUTE rights for the estimator */
282 aclresult = pg_proc_aclcheck(restrictionOid, GetUserId(), ACL_EXECUTE);
283 if (aclresult != ACLCHECK_OK)
284 aclcheck_error(aclresult, ACL_KIND_PROC,
285 NameListToString(restrictionName));
287 return restrictionOid;
291 * Look up a join estimator function ny name, and verify that it has the
292 * correct signature and we have the permissions to attach it to an
296 ValidateJoinEstimator(List *joinName)
302 typeId[0] = INTERNALOID; /* PlannerInfo */
303 typeId[1] = OIDOID; /* operator OID */
304 typeId[2] = INTERNALOID; /* args list */
305 typeId[3] = INT2OID; /* jointype */
306 typeId[4] = INTERNALOID; /* SpecialJoinInfo */
309 * As of Postgres 8.4, the preferred signature for join estimators has 5
310 * arguments, but we still allow the old 4-argument form. Try the
311 * preferred form first.
313 joinOid = LookupFuncName(joinName, 5, typeId, true);
314 if (!OidIsValid(joinOid))
315 joinOid = LookupFuncName(joinName, 4, typeId, true);
316 /* If not found, reference the 5-argument signature in error msg */
317 if (!OidIsValid(joinOid))
318 joinOid = LookupFuncName(joinName, 5, typeId, false);
320 /* estimators must return float8 */
321 if (get_func_rettype(joinOid) != FLOAT8OID)
323 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
324 errmsg("join estimator function %s must return type \"float8\"",
325 NameListToString(joinName))));
327 /* Require EXECUTE rights for the estimator */
328 aclresult = pg_proc_aclcheck(joinOid, GetUserId(), ACL_EXECUTE);
329 if (aclresult != ACLCHECK_OK)
330 aclcheck_error(aclresult, ACL_KIND_PROC,
331 NameListToString(joinName));
337 * Guts of operator deletion.
340 RemoveOperatorById(Oid operOid)
345 relation = heap_open(OperatorRelationId, RowExclusiveLock);
347 tup = SearchSysCache1(OPEROID, ObjectIdGetDatum(operOid));
348 if (!HeapTupleIsValid(tup)) /* should not happen */
349 elog(ERROR, "cache lookup failed for operator %u", operOid);
351 simple_heap_delete(relation, &tup->t_self);
353 ReleaseSysCache(tup);
355 heap_close(relation, RowExclusiveLock);
360 * routine implementing ALTER OPERATOR <operator> SET (option = ...).
362 * Currently, only RESTRICT and JOIN estimator functions can be changed.
365 AlterOperator(AlterOperatorStmt *stmt)
367 ObjectAddress address;
371 Form_pg_operator oprForm;
374 Datum values[Natts_pg_operator];
375 bool nulls[Natts_pg_operator];
376 bool replaces[Natts_pg_operator];
377 List *restrictionName = NIL; /* optional restrict. sel. procedure */
378 bool updateRestriction = false;
380 List *joinName = NIL; /* optional join sel. procedure */
381 bool updateJoin = false;
384 /* Look up the operator */
385 oprId = LookupOperNameTypeNames(NULL, stmt->opername,
386 (TypeName *) linitial(stmt->operargs),
387 (TypeName *) lsecond(stmt->operargs),
389 catalog = heap_open(OperatorRelationId, RowExclusiveLock);
390 tup = SearchSysCacheCopy1(OPEROID, ObjectIdGetDatum(oprId));
392 elog(ERROR, "cache lookup failed for operator %u", oprId);
393 oprForm = (Form_pg_operator) GETSTRUCT(tup);
395 /* Process options */
396 foreach(pl, stmt->options)
398 DefElem *defel = (DefElem *) lfirst(pl);
401 if (defel->arg == NULL)
402 param = NIL; /* NONE, removes the function */
404 param = defGetQualifiedName(defel);
406 if (pg_strcasecmp(defel->defname, "restrict") == 0)
408 restrictionName = param;
409 updateRestriction = true;
411 else if (pg_strcasecmp(defel->defname, "join") == 0)
418 * The rest of the options that CREATE accepts cannot be changed.
419 * Check for them so that we can give a meaningful error message.
421 else if (pg_strcasecmp(defel->defname, "leftarg") == 0 ||
422 pg_strcasecmp(defel->defname, "rightarg") == 0 ||
423 pg_strcasecmp(defel->defname, "procedure") == 0 ||
424 pg_strcasecmp(defel->defname, "commutator") == 0 ||
425 pg_strcasecmp(defel->defname, "negator") == 0 ||
426 pg_strcasecmp(defel->defname, "hashes") == 0 ||
427 pg_strcasecmp(defel->defname, "merges") == 0)
430 (errcode(ERRCODE_SYNTAX_ERROR),
431 errmsg("operator attribute \"%s\" can not be changed",
436 (errcode(ERRCODE_SYNTAX_ERROR),
437 errmsg("operator attribute \"%s\" not recognized",
441 /* Check permissions. Must be owner. */
442 if (!pg_oper_ownercheck(oprId, GetUserId()))
443 aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_OPER,
444 NameStr(oprForm->oprname));
447 * Look up restriction and join estimators if specified
450 restrictionOid = ValidateRestrictionEstimator(restrictionName);
452 restrictionOid = InvalidOid;
454 joinOid = ValidateJoinEstimator(joinName);
456 joinOid = InvalidOid;
458 /* Perform additional checks, like OperatorCreate does */
459 if (!(OidIsValid(oprForm->oprleft) && OidIsValid(oprForm->oprright)))
461 /* If it's not a binary op, these things mustn't be set: */
462 if (OidIsValid(joinOid))
464 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
465 errmsg("only binary operators can have join selectivity")));
468 if (oprForm->oprresult != BOOLOID)
470 if (OidIsValid(restrictionOid))
472 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
473 errmsg("only boolean operators can have restriction selectivity")));
474 if (OidIsValid(joinOid))
476 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
477 errmsg("only boolean operators can have join selectivity")));
480 /* Update the tuple */
481 for (i = 0; i < Natts_pg_operator; ++i)
483 values[i] = (Datum) 0;
487 if (updateRestriction)
489 replaces[Anum_pg_operator_oprrest - 1] = true;
490 values[Anum_pg_operator_oprrest - 1] = restrictionOid;
494 replaces[Anum_pg_operator_oprjoin - 1] = true;
495 values[Anum_pg_operator_oprjoin - 1] = joinOid;
498 tup = heap_modify_tuple(tup, RelationGetDescr(catalog),
499 values, nulls, replaces);
501 simple_heap_update(catalog, &tup->t_self, tup);
502 CatalogUpdateIndexes(catalog, tup);
504 address = makeOperatorDependencies(tup, true);
506 InvokeObjectPostAlterHook(OperatorRelationId, oprId, 0);
508 heap_close(catalog, NoLock);