1 /*-------------------------------------------------------------------------
5 * Routines for operator manipulation commands
7 * Portions Copyright (c) 1996-2019, 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 functions
30 * Most of the parse-tree manipulation routines are defined in
33 *-------------------------------------------------------------------------
37 #include "access/htup_details.h"
38 #include "access/table.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_type.h"
44 #include "commands/alter.h"
45 #include "commands/defrem.h"
46 #include "miscadmin.h"
47 #include "parser/parse_func.h"
48 #include "parser/parse_oper.h"
49 #include "parser/parse_type.h"
50 #include "utils/builtins.h"
51 #include "utils/lsyscache.h"
52 #include "utils/rel.h"
53 #include "utils/syscache.h"
55 static Oid ValidateRestrictionEstimator(List *restrictionName);
56 static Oid ValidateJoinEstimator(List *joinName);
60 * this function extracts all the information from the
61 * parameter list generated by the parser and then has
62 * OperatorCreate() do all the actual work.
64 * 'parameters' is a list of DefElem
67 DefineOperator(List *names, List *parameters)
72 bool canMerge = false; /* operator merges */
73 bool canHash = false; /* operator hashes */
74 List *functionName = NIL; /* function for operator */
75 TypeName *typeName1 = NULL; /* first type name */
76 TypeName *typeName2 = NULL; /* second type name */
77 Oid typeId1 = InvalidOid; /* types converted to OID */
78 Oid typeId2 = InvalidOid;
80 List *commutatorName = NIL; /* optional commutator operator name */
81 List *negatorName = NIL; /* optional negator operator name */
82 List *restrictionName = NIL; /* optional restrict. sel. function */
83 List *joinName = NIL; /* optional join sel. function */
84 Oid functionOid; /* functions converted to OID */
87 Oid typeId[2]; /* to hold left and right arg */
91 /* Convert list of names to a name and namespace */
92 oprNamespace = QualifiedNameGetCreationNamespace(names, &oprName);
94 /* Check we have creation rights in target namespace */
95 aclresult = pg_namespace_aclcheck(oprNamespace, GetUserId(), ACL_CREATE);
96 if (aclresult != ACLCHECK_OK)
97 aclcheck_error(aclresult, OBJECT_SCHEMA,
98 get_namespace_name(oprNamespace));
101 * loop over the definition list and extract the information we need.
103 foreach(pl, parameters)
105 DefElem *defel = (DefElem *) lfirst(pl);
107 if (strcmp(defel->defname, "leftarg") == 0)
109 typeName1 = defGetTypeName(defel);
110 if (typeName1->setof)
112 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
113 errmsg("SETOF type not allowed for operator argument")));
115 else if (strcmp(defel->defname, "rightarg") == 0)
117 typeName2 = defGetTypeName(defel);
118 if (typeName2->setof)
120 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
121 errmsg("SETOF type not allowed for operator argument")));
123 /* "function" and "procedure" are equivalent here */
124 else if (strcmp(defel->defname, "function") == 0)
125 functionName = defGetQualifiedName(defel);
126 else if (strcmp(defel->defname, "procedure") == 0)
127 functionName = defGetQualifiedName(defel);
128 else if (strcmp(defel->defname, "commutator") == 0)
129 commutatorName = defGetQualifiedName(defel);
130 else if (strcmp(defel->defname, "negator") == 0)
131 negatorName = defGetQualifiedName(defel);
132 else if (strcmp(defel->defname, "restrict") == 0)
133 restrictionName = defGetQualifiedName(defel);
134 else if (strcmp(defel->defname, "join") == 0)
135 joinName = defGetQualifiedName(defel);
136 else if (strcmp(defel->defname, "hashes") == 0)
137 canHash = defGetBoolean(defel);
138 else if (strcmp(defel->defname, "merges") == 0)
139 canMerge = defGetBoolean(defel);
140 /* These obsolete options are taken as meaning canMerge */
141 else if (strcmp(defel->defname, "sort1") == 0)
143 else if (strcmp(defel->defname, "sort2") == 0)
145 else if (strcmp(defel->defname, "ltcmp") == 0)
147 else if (strcmp(defel->defname, "gtcmp") == 0)
151 /* WARNING, not ERROR, for historical backwards-compatibility */
153 (errcode(ERRCODE_SYNTAX_ERROR),
154 errmsg("operator attribute \"%s\" not recognized",
160 * make sure we have our required definitions
162 if (functionName == NIL)
164 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
165 errmsg("operator function must be specified")));
167 /* Transform type names to type OIDs */
169 typeId1 = typenameTypeId(NULL, typeName1);
171 typeId2 = typenameTypeId(NULL, typeName2);
173 if (!OidIsValid(typeId1) && !OidIsValid(typeId2))
175 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
176 errmsg("at least one of leftarg or rightarg must be specified")));
180 aclresult = pg_type_aclcheck(typeId1, GetUserId(), ACL_USAGE);
181 if (aclresult != ACLCHECK_OK)
182 aclcheck_error_type(aclresult, typeId1);
187 aclresult = pg_type_aclcheck(typeId2, GetUserId(), ACL_USAGE);
188 if (aclresult != ACLCHECK_OK)
189 aclcheck_error_type(aclresult, typeId2);
193 * Look up the operator's underlying function.
195 if (!OidIsValid(typeId1))
200 else if (!OidIsValid(typeId2))
211 functionOid = LookupFuncName(functionName, nargs, typeId, false);
214 * We require EXECUTE rights for the function. This isn't strictly
215 * necessary, since EXECUTE will be checked at any attempted use of the
216 * operator, but it seems like a good idea anyway.
218 aclresult = pg_proc_aclcheck(functionOid, GetUserId(), ACL_EXECUTE);
219 if (aclresult != ACLCHECK_OK)
220 aclcheck_error(aclresult, OBJECT_FUNCTION,
221 NameListToString(functionName));
223 rettype = get_func_rettype(functionOid);
224 aclresult = pg_type_aclcheck(rettype, GetUserId(), ACL_USAGE);
225 if (aclresult != ACLCHECK_OK)
226 aclcheck_error_type(aclresult, rettype);
229 * Look up restriction and join estimators if specified
232 restrictionOid = ValidateRestrictionEstimator(restrictionName);
234 restrictionOid = InvalidOid;
236 joinOid = ValidateJoinEstimator(joinName);
238 joinOid = InvalidOid;
241 * now have OperatorCreate do all the work..
244 OperatorCreate(oprName, /* operator name */
245 oprNamespace, /* namespace */
246 typeId1, /* left type id */
247 typeId2, /* right type id */
248 functionOid, /* function for operator */
249 commutatorName, /* optional commutator operator name */
250 negatorName, /* optional negator operator name */
251 restrictionOid, /* optional restrict. sel. function */
252 joinOid, /* optional join sel. function name */
253 canMerge, /* operator merges */
254 canHash); /* operator hashes */
258 * Look up a restriction estimator function ny name, and verify that it has
259 * the correct signature and we have the permissions to attach it to an
263 ValidateRestrictionEstimator(List *restrictionName)
269 typeId[0] = INTERNALOID; /* PlannerInfo */
270 typeId[1] = OIDOID; /* operator OID */
271 typeId[2] = INTERNALOID; /* args list */
272 typeId[3] = INT4OID; /* varRelid */
274 restrictionOid = LookupFuncName(restrictionName, 4, typeId, false);
276 /* estimators must return float8 */
277 if (get_func_rettype(restrictionOid) != FLOAT8OID)
279 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
280 errmsg("restriction estimator function %s must return type %s",
281 NameListToString(restrictionName), "float8")));
283 /* Require EXECUTE rights for the estimator */
284 aclresult = pg_proc_aclcheck(restrictionOid, GetUserId(), ACL_EXECUTE);
285 if (aclresult != ACLCHECK_OK)
286 aclcheck_error(aclresult, OBJECT_FUNCTION,
287 NameListToString(restrictionName));
289 return restrictionOid;
293 * Look up a join estimator function ny name, and verify that it has the
294 * correct signature and we have the permissions to attach it to an
298 ValidateJoinEstimator(List *joinName)
304 typeId[0] = INTERNALOID; /* PlannerInfo */
305 typeId[1] = OIDOID; /* operator OID */
306 typeId[2] = INTERNALOID; /* args list */
307 typeId[3] = INT2OID; /* jointype */
308 typeId[4] = INTERNALOID; /* SpecialJoinInfo */
311 * As of Postgres 8.4, the preferred signature for join estimators has 5
312 * arguments, but we still allow the old 4-argument form. Try the
313 * preferred form first.
315 joinOid = LookupFuncName(joinName, 5, typeId, true);
316 if (!OidIsValid(joinOid))
317 joinOid = LookupFuncName(joinName, 4, typeId, true);
318 /* If not found, reference the 5-argument signature in error msg */
319 if (!OidIsValid(joinOid))
320 joinOid = LookupFuncName(joinName, 5, typeId, false);
322 /* estimators must return float8 */
323 if (get_func_rettype(joinOid) != FLOAT8OID)
325 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
326 errmsg("join estimator function %s must return type %s",
327 NameListToString(joinName), "float8")));
329 /* Require EXECUTE rights for the estimator */
330 aclresult = pg_proc_aclcheck(joinOid, GetUserId(), ACL_EXECUTE);
331 if (aclresult != ACLCHECK_OK)
332 aclcheck_error(aclresult, OBJECT_FUNCTION,
333 NameListToString(joinName));
339 * Guts of operator deletion.
342 RemoveOperatorById(Oid operOid)
348 relation = heap_open(OperatorRelationId, RowExclusiveLock);
350 tup = SearchSysCache1(OPEROID, ObjectIdGetDatum(operOid));
351 if (!HeapTupleIsValid(tup)) /* should not happen */
352 elog(ERROR, "cache lookup failed for operator %u", operOid);
353 op = (Form_pg_operator) GETSTRUCT(tup);
356 * Reset links from commutator and negator, if any. In case of a
357 * self-commutator or self-negator, this means we have to re-fetch the
358 * updated tuple. (We could optimize away updates on the tuple we're
359 * about to drop, but it doesn't seem worth convoluting the logic for.)
361 if (OidIsValid(op->oprcom) || OidIsValid(op->oprnegate))
363 OperatorUpd(operOid, op->oprcom, op->oprnegate, true);
364 if (operOid == op->oprcom || operOid == op->oprnegate)
366 ReleaseSysCache(tup);
367 tup = SearchSysCache1(OPEROID, ObjectIdGetDatum(operOid));
368 if (!HeapTupleIsValid(tup)) /* should not happen */
369 elog(ERROR, "cache lookup failed for operator %u", operOid);
373 CatalogTupleDelete(relation, &tup->t_self);
375 ReleaseSysCache(tup);
377 heap_close(relation, RowExclusiveLock);
382 * routine implementing ALTER OPERATOR <operator> SET (option = ...).
384 * Currently, only RESTRICT and JOIN estimator functions can be changed.
387 AlterOperator(AlterOperatorStmt *stmt)
389 ObjectAddress address;
393 Form_pg_operator oprForm;
396 Datum values[Natts_pg_operator];
397 bool nulls[Natts_pg_operator];
398 bool replaces[Natts_pg_operator];
399 List *restrictionName = NIL; /* optional restrict. sel. function */
400 bool updateRestriction = false;
402 List *joinName = NIL; /* optional join sel. function */
403 bool updateJoin = false;
406 /* Look up the operator */
407 oprId = LookupOperWithArgs(stmt->opername, false);
408 catalog = heap_open(OperatorRelationId, RowExclusiveLock);
409 tup = SearchSysCacheCopy1(OPEROID, ObjectIdGetDatum(oprId));
411 elog(ERROR, "cache lookup failed for operator %u", oprId);
412 oprForm = (Form_pg_operator) GETSTRUCT(tup);
414 /* Process options */
415 foreach(pl, stmt->options)
417 DefElem *defel = (DefElem *) lfirst(pl);
420 if (defel->arg == NULL)
421 param = NIL; /* NONE, removes the function */
423 param = defGetQualifiedName(defel);
425 if (strcmp(defel->defname, "restrict") == 0)
427 restrictionName = param;
428 updateRestriction = true;
430 else if (strcmp(defel->defname, "join") == 0)
437 * The rest of the options that CREATE accepts cannot be changed.
438 * Check for them so that we can give a meaningful error message.
440 else if (strcmp(defel->defname, "leftarg") == 0 ||
441 strcmp(defel->defname, "rightarg") == 0 ||
442 strcmp(defel->defname, "function") == 0 ||
443 strcmp(defel->defname, "procedure") == 0 ||
444 strcmp(defel->defname, "commutator") == 0 ||
445 strcmp(defel->defname, "negator") == 0 ||
446 strcmp(defel->defname, "hashes") == 0 ||
447 strcmp(defel->defname, "merges") == 0)
450 (errcode(ERRCODE_SYNTAX_ERROR),
451 errmsg("operator attribute \"%s\" cannot be changed",
456 (errcode(ERRCODE_SYNTAX_ERROR),
457 errmsg("operator attribute \"%s\" not recognized",
461 /* Check permissions. Must be owner. */
462 if (!pg_oper_ownercheck(oprId, GetUserId()))
463 aclcheck_error(ACLCHECK_NOT_OWNER, OBJECT_OPERATOR,
464 NameStr(oprForm->oprname));
467 * Look up restriction and join estimators if specified
470 restrictionOid = ValidateRestrictionEstimator(restrictionName);
472 restrictionOid = InvalidOid;
474 joinOid = ValidateJoinEstimator(joinName);
476 joinOid = InvalidOid;
478 /* Perform additional checks, like OperatorCreate does */
479 if (!(OidIsValid(oprForm->oprleft) && OidIsValid(oprForm->oprright)))
481 /* If it's not a binary op, these things mustn't be set: */
482 if (OidIsValid(joinOid))
484 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
485 errmsg("only binary operators can have join selectivity")));
488 if (oprForm->oprresult != BOOLOID)
490 if (OidIsValid(restrictionOid))
492 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
493 errmsg("only boolean operators can have restriction selectivity")));
494 if (OidIsValid(joinOid))
496 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
497 errmsg("only boolean operators can have join selectivity")));
500 /* Update the tuple */
501 for (i = 0; i < Natts_pg_operator; ++i)
503 values[i] = (Datum) 0;
507 if (updateRestriction)
509 replaces[Anum_pg_operator_oprrest - 1] = true;
510 values[Anum_pg_operator_oprrest - 1] = restrictionOid;
514 replaces[Anum_pg_operator_oprjoin - 1] = true;
515 values[Anum_pg_operator_oprjoin - 1] = joinOid;
518 tup = heap_modify_tuple(tup, RelationGetDescr(catalog),
519 values, nulls, replaces);
521 CatalogTupleUpdate(catalog, &tup->t_self, tup);
523 address = makeOperatorDependencies(tup, true);
525 InvokeObjectPostAlterHook(OperatorRelationId, oprId, 0);
527 heap_close(catalog, NoLock);