PostgreSQL Source Code git master
Loading...
Searching...
No Matches
parse_oper.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * parse_oper.c
4 * handle operator things for parser
5 *
6 * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
8 *
9 *
10 * IDENTIFICATION
11 * src/backend/parser/parse_oper.c
12 *
13 *-------------------------------------------------------------------------
14 */
15
16#include "postgres.h"
17
18#include "access/htup_details.h"
19#include "catalog/pg_operator.h"
20#include "catalog/pg_type.h"
21#include "lib/stringinfo.h"
22#include "nodes/nodeFuncs.h"
23#include "parser/parse_coerce.h"
24#include "parser/parse_func.h"
25#include "parser/parse_oper.h"
26#include "parser/parse_type.h"
27#include "utils/builtins.h"
28#include "utils/hsearch.h"
29#include "utils/inval.h"
30#include "utils/lsyscache.h"
31#include "utils/syscache.h"
32#include "utils/typcache.h"
33
34
35/*
36 * The lookup key for the operator lookaside hash table. Unused bits must be
37 * zeroes to ensure hashing works consistently --- in particular, oprname
38 * must be zero-padded and any unused entries in search_path must be zero.
39 *
40 * search_path contains the actual search_path with which the entry was
41 * derived (minus temp namespace if any), or else the single specified
42 * schema OID if we are looking up an explicitly-qualified operator name.
43 *
44 * search_path has to be fixed-length since the hashtable code insists on
45 * fixed-size keys. If your search path is longer than that, we just punt
46 * and don't cache anything.
47 */
48
49/* If your search_path is longer than this, sucks to be you ... */
50#define MAX_CACHED_PATH_LEN 16
51
52typedef struct OprCacheKey
53{
55 Oid left_arg; /* Left input OID, or 0 if prefix op */
56 Oid right_arg; /* Right input OID */
59
60typedef struct OprCacheEntry
61{
62 /* the hash lookup key MUST BE FIRST */
64
65 Oid opr_oid; /* OID of the resolved operator */
67
68
73 Oid *operOid);
74static void op_error(ParseState *pstate, List *op,
76 FuncDetailCode fdresult, int fgc_flags, int location);
78static bool make_oper_cache_key(ParseState *pstate, OprCacheKey *key,
80 int location);
82static void make_oper_cache_entry(OprCacheKey *key, Oid opr_oid);
84 uint32 hashvalue);
85
86
87/*
88 * LookupOperName
89 * Given a possibly-qualified operator name and exact input datatypes,
90 * look up the operator.
91 *
92 * Pass oprleft = InvalidOid for a prefix op.
93 *
94 * If the operator name is not schema-qualified, it is sought in the current
95 * namespace search path.
96 *
97 * If the operator is not found, we return InvalidOid if noError is true,
98 * else raise an error. pstate and location are used only to report the
99 * error position; pass NULL/-1 if not available.
100 */
101Oid
102LookupOperName(ParseState *pstate, List *opername, Oid oprleft, Oid oprright,
103 bool noError, int location)
104{
105 Oid result;
106
107 result = OpernameGetOprid(opername, oprleft, oprright);
108 if (OidIsValid(result))
109 return result;
110
111 /* we don't use op_error here because only an exact match is wanted */
112 if (!noError)
113 {
114 if (!OidIsValid(oprright))
117 errmsg("postfix operators are not supported"),
118 parser_errposition(pstate, location)));
119
122 errmsg("operator does not exist: %s",
123 op_signature_string(opername, oprleft, oprright)),
124 parser_errposition(pstate, location)));
125 }
126
127 return InvalidOid;
128}
129
130/*
131 * LookupOperWithArgs
132 * Like LookupOperName, but the argument types are specified by
133 * a ObjectWithArgs node.
134 */
135Oid
137{
138 TypeName *oprleft,
139 *oprright;
140 Oid leftoid,
141 rightoid;
142
143 Assert(list_length(oper->objargs) == 2);
144 oprleft = linitial_node(TypeName, oper->objargs);
145 oprright = lsecond_node(TypeName, oper->objargs);
146
147 if (oprleft == NULL)
149 else
151
152 if (oprright == NULL)
154 else
156
157 return LookupOperName(NULL, oper->objname, leftoid, rightoid,
158 noError, -1);
159}
160
161/*
162 * get_sort_group_operators - get default sorting/grouping operators for type
163 *
164 * We fetch the "<", "=", and ">" operators all at once to reduce lookup
165 * overhead (knowing that most callers will be interested in at least two).
166 * However, a given datatype might have only an "=" operator, if it is
167 * hashable but not sortable. (Other combinations of present and missing
168 * operators shouldn't happen, unless the system catalogs are messed up.)
169 *
170 * If an operator is missing and the corresponding needXX flag is true,
171 * throw a standard error message, else return InvalidOid.
172 *
173 * In addition to the operator OIDs themselves, this function can identify
174 * whether the "=" operator is hashable.
175 *
176 * Callers can pass NULL pointers for any results they don't care to get.
177 *
178 * Note: the results are guaranteed to be exact or binary-compatible matches,
179 * since most callers are not prepared to cope with adding any run-time type
180 * coercion steps.
181 */
182void
184 bool needLT, bool needEQ, bool needGT,
185 Oid *ltOpr, Oid *eqOpr, Oid *gtOpr,
186 bool *isHashable)
187{
188 TypeCacheEntry *typentry;
189 int cache_flags;
190 Oid lt_opr;
191 Oid eq_opr;
192 Oid gt_opr;
193 bool hashable;
194
195 /*
196 * Look up the operators using the type cache.
197 *
198 * Note: the search algorithm used by typcache.c ensures that the results
199 * are consistent, ie all from matching opclasses.
200 */
201 if (isHashable != NULL)
204 else
206
207 typentry = lookup_type_cache(argtype, cache_flags);
208 lt_opr = typentry->lt_opr;
209 eq_opr = typentry->eq_opr;
210 gt_opr = typentry->gt_opr;
211 hashable = OidIsValid(typentry->hash_proc);
212
213 /* Report errors if needed */
214 if ((needLT && !OidIsValid(lt_opr)) ||
215 (needGT && !OidIsValid(gt_opr)))
218 errmsg("could not identify an ordering operator for type %s",
219 format_type_be(argtype)),
220 errhint("Use an explicit ordering operator or modify the query.")));
221 if (needEQ && !OidIsValid(eq_opr))
224 errmsg("could not identify an equality operator for type %s",
225 format_type_be(argtype))));
226
227 /* Return results as needed */
228 if (ltOpr)
229 *ltOpr = lt_opr;
230 if (eqOpr)
231 *eqOpr = eq_opr;
232 if (gtOpr)
233 *gtOpr = gt_opr;
234 if (isHashable)
235 *isHashable = hashable;
236}
237
238
239/* given operator tuple, return the operator OID */
240Oid
242{
243 return ((Form_pg_operator) GETSTRUCT(op))->oid;
244}
245
246/* given operator tuple, return the underlying function's OID */
247Oid
249{
251
252 return pgopform->oprcode;
253}
254
255
256/* binary_oper_exact()
257 * Check for an "exact" match to the specified operand types.
258 *
259 * If one operand is an unknown literal, assume it should be taken to be
260 * the same type as the other operand for this purpose. Also, consider
261 * the possibility that the other operand is a domain type that needs to
262 * be reduced to its base type to find an "exact" match.
263 */
264static Oid
266{
267 Oid result;
268 bool was_unknown = false;
269
270 /* Unspecified type for one of the arguments? then use the other */
271 if ((arg1 == UNKNOWNOID) && (arg2 != InvalidOid))
272 {
273 arg1 = arg2;
274 was_unknown = true;
275 }
276 else if ((arg2 == UNKNOWNOID) && (arg1 != InvalidOid))
277 {
278 arg2 = arg1;
279 was_unknown = true;
280 }
281
283 if (OidIsValid(result))
284 return result;
285
286 if (was_unknown)
287 {
288 /* arg1 and arg2 are the same here, need only look at arg1 */
290
291 if (basetype != arg1)
292 {
294 if (OidIsValid(result))
295 return result;
296 }
297 }
298
299 return InvalidOid;
300}
301
302
303/* oper_select_candidate()
304 * Given the input argtype array and one or more candidates
305 * for the operator, attempt to resolve the conflict.
306 *
307 * Returns FUNCDETAIL_NOTFOUND, FUNCDETAIL_MULTIPLE, or FUNCDETAIL_NORMAL.
308 * In the success case the Oid of the best candidate is stored in *operOid.
309 *
310 * Note that the caller has already determined that there is no candidate
311 * exactly matching the input argtype(s). Incompatible candidates are not yet
312 * pruned away, however.
313 */
314static FuncDetailCode
318 Oid *operOid) /* output argument */
319{
320 int ncandidates;
321
322 /*
323 * Delete any candidates that cannot actually accept the given input
324 * types, whether directly or by coercion.
325 */
328
329 /* Done if no candidate or only one candidate survives */
330 if (ncandidates == 0)
331 {
333 return FUNCDETAIL_NOTFOUND;
334 }
335 if (ncandidates == 1)
336 {
337 *operOid = candidates->oid;
338 return FUNCDETAIL_NORMAL;
339 }
340
341 /*
342 * Use the same heuristics as for ambiguous functions to resolve the
343 * conflict.
344 */
346
347 if (candidates)
348 {
349 *operOid = candidates->oid;
350 return FUNCDETAIL_NORMAL;
351 }
352
354 return FUNCDETAIL_MULTIPLE; /* failed to select a best candidate */
355}
356
357
358/* oper() -- search for a binary operator
359 * Given operator name, types of arg1 and arg2, return oper struct.
360 *
361 * IMPORTANT: the returned operator (if any) is only promised to be
362 * coercion-compatible with the input datatypes. Do not use this if
363 * you need an exact- or binary-compatible match; see compatible_oper.
364 *
365 * If no matching operator found, return NULL if noError is true,
366 * raise an error if it is false. pstate and location are used only to report
367 * the error position; pass NULL/-1 if not available.
368 *
369 * NOTE: on success, the returned object is a syscache entry. The caller
370 * must ReleaseSysCache() the entry when done with it.
371 */
374 bool noError, int location)
375{
376 Oid operOid;
377 OprCacheKey key;
378 bool key_ok;
379 int fgc_flags = 0;
382
383 /*
384 * Try to find the mapping in the lookaside cache.
385 */
386 key_ok = make_oper_cache_key(pstate, &key, opname, ltypeId, rtypeId, location);
387
388 if (key_ok)
389 {
391 if (OidIsValid(operOid))
392 {
395 return (Operator) tup;
396 }
397 }
398
399 /*
400 * First try for an "exact" match.
401 */
403 if (!OidIsValid(operOid))
404 {
405 /*
406 * Otherwise, search for the most suitable candidate.
407 */
409
410 /* Get binary operators of given name */
412
413 /* No operators found? Then fail... */
414 if (clist != NULL)
415 {
416 /*
417 * Unspecified type for one of the arguments? then use the other
418 * (XXX this is probably dead code?)
419 */
420 Oid inputOids[2];
421
422 if (rtypeId == InvalidOid)
424 else if (ltypeId == InvalidOid)
426 inputOids[0] = ltypeId;
427 inputOids[1] = rtypeId;
429 }
430 }
431
432 if (OidIsValid(operOid))
434
436 {
437 if (key_ok)
439 }
440 else if (!noError)
441 op_error(pstate, opname, ltypeId, rtypeId,
442 fdresult, fgc_flags, location);
443
444 return (Operator) tup;
445}
446
447/* compatible_oper()
448 * given an opname and input datatypes, find a compatible binary operator
449 *
450 * This is tighter than oper() because it will not return an operator that
451 * requires coercion of the input datatypes (but binary-compatible operators
452 * are accepted). Otherwise, the semantics are the same.
453 */
456 bool noError, int location)
457{
460
461 /* oper() will find the best available match */
462 optup = oper(pstate, op, arg1, arg2, noError, location);
463 if (optup == (Operator) NULL)
464 return (Operator) NULL; /* must be noError case */
465
466 /* but is it good enough? */
468 if (IsBinaryCoercible(arg1, opform->oprleft) &&
469 IsBinaryCoercible(arg2, opform->oprright))
470 return optup;
471
472 /* nope... */
474
475 if (!noError)
478 errmsg("operator requires run-time type coercion: %s",
480 parser_errposition(pstate, location)));
481
482 return (Operator) NULL;
483}
484
485/* compatible_oper_opid() -- get OID of a binary operator
486 *
487 * This is a convenience routine that extracts only the operator OID
488 * from the result of compatible_oper(). InvalidOid is returned if the
489 * lookup fails and noError is true.
490 */
491Oid
493{
495 Oid result;
496
498 if (optup != NULL)
499 {
500 result = oprid(optup);
502 return result;
503 }
504 return InvalidOid;
505}
506
507
508/* left_oper() -- search for a unary left operator (prefix operator)
509 * Given operator name and type of arg, return oper struct.
510 *
511 * IMPORTANT: the returned operator (if any) is only promised to be
512 * coercion-compatible with the input datatype. Do not use this if
513 * you need an exact- or binary-compatible match.
514 *
515 * If no matching operator found, return NULL if noError is true,
516 * raise an error if it is false. pstate and location are used only to report
517 * the error position; pass NULL/-1 if not available.
518 *
519 * NOTE: on success, the returned object is a syscache entry. The caller
520 * must ReleaseSysCache() the entry when done with it.
521 */
523left_oper(ParseState *pstate, List *op, Oid arg, bool noError, int location)
524{
525 Oid operOid;
526 OprCacheKey key;
527 bool key_ok;
528 int fgc_flags = 0;
531
532 /*
533 * Try to find the mapping in the lookaside cache.
534 */
535 key_ok = make_oper_cache_key(pstate, &key, op, InvalidOid, arg, location);
536
537 if (key_ok)
538 {
540 if (OidIsValid(operOid))
541 {
544 return (Operator) tup;
545 }
546 }
547
548 /*
549 * First try for an "exact" match.
550 */
552 if (!OidIsValid(operOid))
553 {
554 /*
555 * Otherwise, search for the most suitable candidate.
556 */
558
559 /* Get prefix operators of given name */
560 clist = OpernameGetCandidates(op, 'l', false, &fgc_flags);
561
562 /* No operators found? Then fail... */
563 if (clist != NULL)
564 {
565 /*
566 * The returned list has args in the form (0, oprright). Move the
567 * useful data into args[0] to keep oper_select_candidate simple.
568 * XXX we are assuming here that we may scribble on the list!
569 */
571
572 for (clisti = clist; clisti != NULL; clisti = clisti->next)
573 {
574 clisti->args[0] = clisti->args[1];
575 }
576
577 /*
578 * We must run oper_select_candidate even if only one candidate,
579 * otherwise we may falsely return a non-type-compatible operator.
580 */
582 }
583 }
584
585 if (OidIsValid(operOid))
587
589 {
590 if (key_ok)
592 }
593 else if (!noError)
594 op_error(pstate, op, InvalidOid, arg,
595 fdresult, fgc_flags, location);
596
597 return (Operator) tup;
598}
599
600/*
601 * op_signature_string
602 * Build a string representing an operator name, including arg type(s).
603 * The result is something like "integer + integer".
604 *
605 * This is typically used in the construction of operator-not-found error
606 * messages.
607 */
608const char *
610{
612
614
615 if (OidIsValid(arg1))
617
619
621
622 return argbuf.data; /* return palloc'd string buffer */
623}
624
625/*
626 * op_error - utility routine to complain about an unresolvable operator
627 */
628static void
630 Oid arg1, Oid arg2,
631 FuncDetailCode fdresult, int fgc_flags, int location)
632{
636 errmsg("operator is not unique: %s",
638 errdetail("Could not choose a best candidate operator."),
639 errhint("You might need to add explicit type casts."),
640 parser_errposition(pstate, location)));
641 else
644 errmsg("operator does not exist: %s",
647 parser_errposition(pstate, location)));
648}
649
650/*
651 * Interpret the fgc_flags and issue a suitable detail or hint message.
652 */
653static int
655{
656 /*
657 * If not FGC_NAME_VISIBLE, we shouldn't raise the question of whether the
658 * arguments are wrong. If the operator name was not schema-qualified,
659 * it's helpful to distinguish between doesn't-exist-anywhere and
660 * not-in-search-path; but if it was, there's really nothing to add to the
661 * basic "operator does not exist" message.
662 *
663 * Note: we passed missing_ok = false to OpernameGetCandidates, so there's
664 * no need to consider FGC_SCHEMA_EXISTS here: we'd have already thrown an
665 * error if an explicitly-given schema doesn't exist.
666 */
668 {
670 return 0; /* schema-qualified name */
671 else if (!(fgc_flags & FGC_NAME_EXISTS))
672 return errdetail("There is no operator of that name.");
673 else
674 return errdetail("An operator of that name exists, but it is not in the search_path.");
675 }
676
677 /*
678 * Otherwise, the problem must be incorrect argument type(s).
679 */
680 if (is_unary_op)
681 {
682 (void) errdetail("No operator of that name accepts the given argument type.");
683 return errhint("You might need to add an explicit type cast.");
684 }
685 else
686 {
687 (void) errdetail("No operator of that name accepts the given argument types.");
688 return errhint("You might need to add explicit type casts.");
689 }
690}
691
692/*
693 * make_op()
694 * Operator expression construction.
695 *
696 * Transform operator expression ensuring type compatibility.
697 * This is where some type conversion happens.
698 *
699 * last_srf should be a copy of pstate->p_last_srf from just before we
700 * started transforming the operator's arguments; this is used for nested-SRF
701 * detection. If the caller will throw an error anyway for a set-returning
702 * expression, it's okay to cheat and just pass pstate->p_last_srf.
703 */
704Expr *
706 Node *last_srf, int location)
707{
708 Oid ltypeId,
709 rtypeId;
714 int nargs;
715 List *args;
716 Oid rettype;
717 OpExpr *result;
718
719 /* Check it's not a postfix operator */
720 if (rtree == NULL)
723 errmsg("postfix operators are not supported")));
724
725 /* Select the operator */
726 if (ltree == NULL)
727 {
728 /* prefix operator */
731 tup = left_oper(pstate, opname, rtypeId, false, location);
732 }
733 else
734 {
735 /* otherwise, binary operator */
738 tup = oper(pstate, opname, ltypeId, rtypeId, false, location);
739 }
740
742
743 /* Check it's not a shell */
744 if (!RegProcedureIsValid(opform->oprcode))
747 errmsg("operator is only a shell: %s",
749 opform->oprleft,
750 opform->oprright)),
751 parser_errposition(pstate, location)));
752
753 /* Do typecasting and build the expression tree */
754 if (ltree == NULL)
755 {
756 /* prefix operator */
757 args = list_make1(rtree);
759 declared_arg_types[0] = opform->oprright;
760 nargs = 1;
761 }
762 else
763 {
764 /* otherwise, binary operator */
765 args = list_make2(ltree, rtree);
768 declared_arg_types[0] = opform->oprleft;
769 declared_arg_types[1] = opform->oprright;
770 nargs = 2;
771 }
772
773 /*
774 * enforce consistency with polymorphic argument and return types,
775 * possibly adjusting return type or declared_arg_types (which will be
776 * used as the cast destination by make_fn_arguments)
777 */
780 nargs,
781 opform->oprresult,
782 false);
783
784 /* perform the necessary typecasting of arguments */
786
787 /* and build the expression node */
789 result->opno = oprid(tup);
790 result->opfuncid = opform->oprcode;
791 result->opresulttype = rettype;
792 result->opretset = get_func_retset(opform->oprcode);
793 /* opcollid and inputcollid will be set by parse_collate.c */
794 result->args = args;
795 result->location = location;
796
797 /* if it returns a set, check that's OK */
798 if (result->opretset)
799 {
800 check_srf_call_placement(pstate, last_srf, location);
801 /* ... and remember it for error checks at higher levels */
802 pstate->p_last_srf = (Node *) result;
803 }
804
806
807 return (Expr *) result;
808}
809
810/*
811 * make_scalar_array_op()
812 * Build expression tree for "scalar op ANY/ALL (array)" construct.
813 */
814Expr *
816 bool useOr,
817 Node *ltree, Node *rtree,
818 int location)
819{
820 Oid ltypeId,
821 rtypeId,
822 atypeId,
828 List *args;
829 Oid rettype;
831
834
835 /*
836 * The right-hand input of the operator will be the element type of the
837 * array. However, if we currently have just an untyped literal on the
838 * right, stay with that and hope we can resolve the operator.
839 */
840 if (atypeId == UNKNOWNOID)
842 else
843 {
845 if (!OidIsValid(rtypeId))
848 errmsg("op ANY/ALL (array) requires array on right side"),
849 parser_errposition(pstate, location)));
850 }
851
852 /* Now resolve the operator */
853 tup = oper(pstate, opname, ltypeId, rtypeId, false, location);
855
856 /* Check it's not a shell */
857 if (!RegProcedureIsValid(opform->oprcode))
860 errmsg("operator is only a shell: %s",
862 opform->oprleft,
863 opform->oprright)),
864 parser_errposition(pstate, location)));
865
866 args = list_make2(ltree, rtree);
869 declared_arg_types[0] = opform->oprleft;
870 declared_arg_types[1] = opform->oprright;
871
872 /*
873 * enforce consistency with polymorphic argument and return types,
874 * possibly adjusting return type or declared_arg_types (which will be
875 * used as the cast destination by make_fn_arguments)
876 */
879 2,
880 opform->oprresult,
881 false);
882
883 /*
884 * Check that operator result is boolean
885 */
886 if (rettype != BOOLOID)
889 errmsg("op ANY/ALL (array) requires operator to yield boolean"),
890 parser_errposition(pstate, location)));
891 if (get_func_retset(opform->oprcode))
894 errmsg("op ANY/ALL (array) requires operator not to return a set"),
895 parser_errposition(pstate, location)));
896
897 /*
898 * Now switch back to the array type on the right, arranging for any
899 * needed cast to be applied. Beware of polymorphic operators here;
900 * enforce_generic_type_consistency may or may not have replaced a
901 * polymorphic type with a real one.
902 */
904 {
905 /* assume the actual array type is OK */
907 }
908 else
909 {
914 errmsg("could not find array type for data type %s",
916 parser_errposition(pstate, location)));
917 }
920
921 /* perform the necessary typecasting of arguments */
923
924 /* and build the expression node */
926 result->opno = oprid(tup);
927 result->opfuncid = opform->oprcode;
928 result->hashfuncid = InvalidOid;
929 result->negfuncid = InvalidOid;
930 result->useOr = useOr;
931 /* inputcollid will be set by parse_collate.c */
932 result->args = args;
933 result->location = location;
934
936
937 return (Expr *) result;
938}
939
940
941/*
942 * Lookaside cache to speed operator lookup. Possibly this should be in
943 * a separate module under utils/cache/ ?
944 *
945 * The idea here is that the mapping from operator name and given argument
946 * types is constant for a given search path (or single specified schema OID)
947 * so long as the contents of pg_operator and pg_cast don't change. And that
948 * mapping is pretty expensive to compute, especially for ambiguous operators;
949 * this is mainly because there are a *lot* of instances of popular operator
950 * names such as "=", and we have to check each one to see which is the
951 * best match. So once we have identified the correct mapping, we save it
952 * in a cache that need only be flushed on pg_operator or pg_cast change.
953 * (pg_cast must be considered because changes in the set of implicit casts
954 * affect the set of applicable operators for any given input datatype.)
955 *
956 * XXX in principle, ALTER TABLE ... INHERIT could affect the mapping as
957 * well, but we disregard that since there's no convenient way to find out
958 * about it, and it seems a pretty far-fetched corner-case anyway.
959 *
960 * Note: at some point it might be worth doing a similar cache for function
961 * lookups. However, the potential gain is a lot less since (a) function
962 * names are generally not overloaded as heavily as operator names, and
963 * (b) we'd have to flush on pg_proc updates, which are probably a good
964 * deal more common than pg_operator updates.
965 */
966
967/* The operator cache hashtable */
969
970
971/*
972 * make_oper_cache_key
973 * Fill the lookup key struct given operator name and arg types.
974 *
975 * Returns true if successful, false if the search_path overflowed
976 * (hence no caching is possible).
977 *
978 * pstate/location are used only to report the error position; pass NULL/-1
979 * if not available.
980 */
981static bool
983 Oid ltypeId, Oid rtypeId, int location)
984{
985 char *schemaname;
986 char *opername;
987
988 /* deconstruct the name list */
989 DeconstructQualifiedName(opname, &schemaname, &opername);
990
991 /* ensure zero-fill for stable hashing */
992 MemSet(key, 0, sizeof(OprCacheKey));
993
994 /* save operator name and input types into key */
995 strlcpy(key->oprname, opername, NAMEDATALEN);
996 key->left_arg = ltypeId;
997 key->right_arg = rtypeId;
998
999 if (schemaname)
1000 {
1002
1003 /* search only in exact schema given */
1004 setup_parser_errposition_callback(&pcbstate, pstate, location);
1005 key->search_path[0] = LookupExplicitNamespace(schemaname, false);
1007 }
1008 else
1009 {
1010 /* get the active search path */
1011 if (fetch_search_path_array(key->search_path,
1013 return false; /* oops, didn't fit */
1014 }
1015
1016 return true;
1017}
1018
1019/*
1020 * find_oper_cache_entry
1021 *
1022 * Look for a cache entry matching the given key. If found, return the
1023 * contained operator OID, else return InvalidOid.
1024 */
1025static Oid
1027{
1029
1030 if (OprCacheHash == NULL)
1031 {
1032 /* First time through: initialize the hash table */
1033 HASHCTL ctl;
1034
1035 ctl.keysize = sizeof(OprCacheKey);
1036 ctl.entrysize = sizeof(OprCacheEntry);
1037 OprCacheHash = hash_create("Operator lookup cache", 256,
1039
1040 /* Arrange to flush cache on pg_operator and pg_cast changes */
1043 (Datum) 0);
1046 (Datum) 0);
1047 }
1048
1049 /* Look for an existing entry */
1051 key,
1052 HASH_FIND, NULL);
1053 if (oprentry == NULL)
1054 return InvalidOid;
1055
1056 return oprentry->opr_oid;
1057}
1058
1059/*
1060 * make_oper_cache_entry
1061 *
1062 * Insert a cache entry for the given key.
1063 */
1064static void
1066{
1068
1070
1072 key,
1073 HASH_ENTER, NULL);
1074 oprentry->opr_oid = opr_oid;
1075}
1076
1077/*
1078 * Callback for pg_operator and pg_cast inval events
1079 */
1080static void
1082 uint32 hashvalue)
1083{
1084 HASH_SEQ_STATUS status;
1086
1088
1089 /* Currently we just flush all entries; hard to be smarter ... */
1090 hash_seq_init(&status, OprCacheHash);
1091
1092 while ((hentry = (OprCacheEntry *) hash_seq_search(&status)) != NULL)
1093 {
1095 &hentry->key,
1096 HASH_REMOVE, NULL) == NULL)
1097 elog(ERROR, "hash table corrupted");
1098 }
1099}
#define RegProcedureIsValid(p)
Definition c.h:862
#define Assert(condition)
Definition c.h:943
uint32_t uint32
Definition c.h:624
#define MemSet(start, val, len)
Definition c.h:1107
#define OidIsValid(objectId)
Definition c.h:858
uint32 result
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition dynahash.c:889
HTAB * hash_create(const char *tabname, int64 nelem, const HASHCTL *info, int flags)
Definition dynahash.c:360
void * hash_seq_search(HASH_SEQ_STATUS *status)
Definition dynahash.c:1352
void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp)
Definition dynahash.c:1317
Datum arg
Definition elog.c:1322
int errcode(int sqlerrcode)
Definition elog.c:874
int errhint(const char *fmt,...) pg_attribute_printf(1
int errdetail(const char *fmt,...) pg_attribute_printf(1
#define ERROR
Definition elog.h:40
#define elog(elevel,...)
Definition elog.h:228
#define ereport(elevel,...)
Definition elog.h:152
char * format_type_be(Oid type_oid)
@ HASH_FIND
Definition hsearch.h:108
@ HASH_REMOVE
Definition hsearch.h:110
@ HASH_ENTER
Definition hsearch.h:109
#define HASH_ELEM
Definition hsearch.h:90
#define HASH_BLOBS
Definition hsearch.h:92
#define HeapTupleIsValid(tuple)
Definition htup.h:78
static void * GETSTRUCT(const HeapTupleData *tuple)
void CacheRegisterSyscacheCallback(SysCacheIdentifier cacheid, SyscacheCallbackFunction func, Datum arg)
Definition inval.c:1816
bool get_func_retset(Oid funcid)
Definition lsyscache.c:1962
Oid get_base_element_type(Oid typid)
Definition lsyscache.c:3054
Oid getBaseType(Oid typid)
Definition lsyscache.c:2743
Oid get_array_type(Oid typid)
Definition lsyscache.c:3009
Oid OpernameGetOprid(List *names, Oid oprleft, Oid oprright)
Definition namespace.c:1834
char * NameListToString(const List *names)
Definition namespace.c:3666
Oid LookupExplicitNamespace(const char *nspname, bool missing_ok)
Definition namespace.c:3457
void DeconstructQualifiedName(const List *names, char **nspname_p, char **objname_p)
Definition namespace.c:3373
FuncCandidateList OpernameGetCandidates(List *names, char oprkind, bool missing_schema_ok, int *fgc_flags)
Definition namespace.c:1947
int fetch_search_path_array(Oid *sarray, int sarray_len)
Definition namespace.c:4931
#define FGC_NAME_EXISTS
Definition namespace.h:49
#define FGC_SCHEMA_GIVEN
Definition namespace.h:47
#define FGC_NAME_VISIBLE
Definition namespace.h:50
Oid exprType(const Node *expr)
Definition nodeFuncs.c:42
#define makeNode(_type_)
Definition nodes.h:161
static char * errmsg
Oid enforce_generic_type_consistency(const Oid *actual_arg_types, Oid *declared_arg_types, int nargs, Oid rettype, bool allow_poly)
bool IsBinaryCoercible(Oid srctype, Oid targettype)
void make_fn_arguments(ParseState *pstate, List *fargs, Oid *actual_arg_types, Oid *declared_arg_types)
FuncCandidateList func_select_candidate(int nargs, Oid *input_typeids, FuncCandidateList candidates)
void check_srf_call_placement(ParseState *pstate, Node *last_srf, int location)
int func_match_argtypes(int nargs, Oid *input_typeids, FuncCandidateList raw_candidates, FuncCandidateList *candidates)
FuncDetailCode
Definition parse_func.h:23
@ FUNCDETAIL_MULTIPLE
Definition parse_func.h:25
@ FUNCDETAIL_NORMAL
Definition parse_func.h:26
@ FUNCDETAIL_NOTFOUND
Definition parse_func.h:24
void cancel_parser_errposition_callback(ParseCallbackState *pcbstate)
Definition parse_node.c:156
int parser_errposition(ParseState *pstate, int location)
Definition parse_node.c:106
void setup_parser_errposition_callback(ParseCallbackState *pcbstate, ParseState *pstate, int location)
Definition parse_node.c:140
static void make_oper_cache_entry(OprCacheKey *key, Oid opr_oid)
static FuncDetailCode oper_select_candidate(int nargs, Oid *input_typeids, FuncCandidateList candidates, Oid *operOid)
Definition parse_oper.c:315
Operator left_oper(ParseState *pstate, List *op, Oid arg, bool noError, int location)
Definition parse_oper.c:523
static void InvalidateOprCacheCallBack(Datum arg, SysCacheIdentifier cacheid, uint32 hashvalue)
void get_sort_group_operators(Oid argtype, bool needLT, bool needEQ, bool needGT, Oid *ltOpr, Oid *eqOpr, Oid *gtOpr, bool *isHashable)
Definition parse_oper.c:183
#define MAX_CACHED_PATH_LEN
Definition parse_oper.c:50
static int oper_lookup_failure_details(int fgc_flags, bool is_unary_op)
Definition parse_oper.c:654
Expr * make_op(ParseState *pstate, List *opname, Node *ltree, Node *rtree, Node *last_srf, int location)
Definition parse_oper.c:705
Oid LookupOperName(ParseState *pstate, List *opername, Oid oprleft, Oid oprright, bool noError, int location)
Definition parse_oper.c:102
Expr * make_scalar_array_op(ParseState *pstate, List *opname, bool useOr, Node *ltree, Node *rtree, int location)
Definition parse_oper.c:815
Oid oprfuncid(Operator op)
Definition parse_oper.c:248
static bool make_oper_cache_key(ParseState *pstate, OprCacheKey *key, List *opname, Oid ltypeId, Oid rtypeId, int location)
Definition parse_oper.c:982
static HTAB * OprCacheHash
Definition parse_oper.c:968
Oid oprid(Operator op)
Definition parse_oper.c:241
static void op_error(ParseState *pstate, List *op, Oid arg1, Oid arg2, FuncDetailCode fdresult, int fgc_flags, int location)
Definition parse_oper.c:629
Operator oper(ParseState *pstate, List *opname, Oid ltypeId, Oid rtypeId, bool noError, int location)
Definition parse_oper.c:373
static Oid binary_oper_exact(List *opname, Oid arg1, Oid arg2)
Definition parse_oper.c:265
static Oid find_oper_cache_entry(OprCacheKey *key)
const char * op_signature_string(List *op, Oid arg1, Oid arg2)
Definition parse_oper.c:609
Oid compatible_oper_opid(List *op, Oid arg1, Oid arg2, bool noError)
Definition parse_oper.c:492
Operator compatible_oper(ParseState *pstate, List *op, Oid arg1, Oid arg2, bool noError, int location)
Definition parse_oper.c:455
Oid LookupOperWithArgs(ObjectWithArgs *oper, bool noError)
Definition parse_oper.c:136
Oid LookupTypeNameOid(ParseState *pstate, const TypeName *typeName, bool missing_ok)
Definition parse_type.c:232
#define NAMEDATALEN
static int list_length(const List *l)
Definition pg_list.h:152
#define linitial_node(type, l)
Definition pg_list.h:181
#define lsecond_node(type, l)
Definition pg_list.h:186
#define list_make1(x1)
Definition pg_list.h:244
#define list_make2(x1, x2)
Definition pg_list.h:246
END_CATALOG_STRUCT typedef FormData_pg_operator * Form_pg_operator
Definition pg_operator.h:87
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition strlcpy.c:45
static Datum ObjectIdGetDatum(Oid X)
Definition postgres.h:252
uint64_t Datum
Definition postgres.h:70
#define InvalidOid
unsigned int Oid
static int fb(int x)
tree ctl
Definition radixtree.h:1838
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition stringinfo.c:145
void appendStringInfoString(StringInfo str, const char *s)
Definition stringinfo.c:230
void initStringInfo(StringInfo str)
Definition stringinfo.c:97
Size keysize
Definition hsearch.h:69
Definition pg_list.h:54
Definition nodes.h:135
OprCacheKey key
Definition parse_oper.c:63
Oid search_path[MAX_CACHED_PATH_LEN]
Definition parse_oper.c:57
char oprname[NAMEDATALEN]
Definition parse_oper.c:54
Node * p_last_srf
Definition parse_node.h:252
Oid args[FLEXIBLE_ARRAY_MEMBER]
Definition namespace.h:39
Definition ltree.h:43
void ReleaseSysCache(HeapTuple tuple)
Definition syscache.c:265
HeapTuple SearchSysCache1(SysCacheIdentifier cacheId, Datum key1)
Definition syscache.c:221
TypeCacheEntry * lookup_type_cache(Oid type_id, int flags)
Definition typcache.c:389
#define TYPECACHE_EQ_OPR
Definition typcache.h:138
#define TYPECACHE_GT_OPR
Definition typcache.h:140
#define TYPECACHE_LT_OPR
Definition typcache.h:139
#define TYPECACHE_HASH_PROC
Definition typcache.h:142