1 /*-------------------------------------------------------------------------
4 * Convenience routines for common queries in the system catalog cache.
6 * Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
10 * $PostgreSQL: pgsql/src/backend/utils/cache/lsyscache.c,v 1.166 2010/01/04 02:44:40 tgl Exp $
13 * Eventually, the index information should go through here, too.
14 *-------------------------------------------------------------------------
18 #include "access/hash.h"
19 #include "access/nbtree.h"
20 #include "bootstrap/bootstrap.h"
21 #include "catalog/pg_amop.h"
22 #include "catalog/pg_amproc.h"
23 #include "catalog/pg_constraint.h"
24 #include "catalog/pg_namespace.h"
25 #include "catalog/pg_opclass.h"
26 #include "catalog/pg_operator.h"
27 #include "catalog/pg_proc.h"
28 #include "catalog/pg_statistic.h"
29 #include "catalog/pg_type.h"
30 #include "miscadmin.h"
31 #include "nodes/makefuncs.h"
32 #include "utils/array.h"
33 #include "utils/builtins.h"
34 #include "utils/datum.h"
35 #include "utils/lsyscache.h"
36 #include "utils/syscache.h"
38 /* Hook for plugins to get control in get_attavgwidth() */
39 get_attavgwidth_hook_type get_attavgwidth_hook = NULL;
42 /* ---------- AMOP CACHES ---------- */
47 * Return t iff operator 'opno' is in operator family 'opfamily'.
50 op_in_opfamily(Oid opno, Oid opfamily)
52 return SearchSysCacheExists(AMOPOPID,
53 ObjectIdGetDatum(opno),
54 ObjectIdGetDatum(opfamily),
59 * get_op_opfamily_strategy
61 * Get the operator's strategy number within the specified opfamily,
62 * or 0 if it's not a member of the opfamily.
65 get_op_opfamily_strategy(Oid opno, Oid opfamily)
68 Form_pg_amop amop_tup;
71 tp = SearchSysCache(AMOPOPID,
72 ObjectIdGetDatum(opno),
73 ObjectIdGetDatum(opfamily),
75 if (!HeapTupleIsValid(tp))
77 amop_tup = (Form_pg_amop) GETSTRUCT(tp);
78 result = amop_tup->amopstrategy;
84 * get_op_opfamily_properties
86 * Get the operator's strategy number and declared input data types
87 * within the specified opfamily.
89 * Caller should already have verified that opno is a member of opfamily,
90 * therefore we raise an error if the tuple is not found.
93 get_op_opfamily_properties(Oid opno, Oid opfamily,
99 Form_pg_amop amop_tup;
101 tp = SearchSysCache(AMOPOPID,
102 ObjectIdGetDatum(opno),
103 ObjectIdGetDatum(opfamily),
105 if (!HeapTupleIsValid(tp))
106 elog(ERROR, "operator %u is not a member of opfamily %u",
108 amop_tup = (Form_pg_amop) GETSTRUCT(tp);
109 *strategy = amop_tup->amopstrategy;
110 *lefttype = amop_tup->amoplefttype;
111 *righttype = amop_tup->amoprighttype;
116 * get_opfamily_member
117 * Get the OID of the operator that implements the specified strategy
118 * with the specified datatypes for the specified opfamily.
120 * Returns InvalidOid if there is no pg_amop entry for the given keys.
123 get_opfamily_member(Oid opfamily, Oid lefttype, Oid righttype,
127 Form_pg_amop amop_tup;
130 tp = SearchSysCache(AMOPSTRATEGY,
131 ObjectIdGetDatum(opfamily),
132 ObjectIdGetDatum(lefttype),
133 ObjectIdGetDatum(righttype),
134 Int16GetDatum(strategy));
135 if (!HeapTupleIsValid(tp))
137 amop_tup = (Form_pg_amop) GETSTRUCT(tp);
138 result = amop_tup->amopopr;
144 * get_ordering_op_properties
145 * Given the OID of an ordering operator (a btree "<" or ">" operator),
146 * determine its opfamily, its declared input datatype, and its
147 * strategy number (BTLessStrategyNumber or BTGreaterStrategyNumber).
149 * Returns TRUE if successful, FALSE if no matching pg_amop entry exists.
150 * (This indicates that the operator is not a valid ordering operator.)
152 * Note: the operator could be registered in multiple families, for example
153 * if someone were to build a "reverse sort" opfamily. This would result in
154 * uncertainty as to whether "ORDER BY USING op" would default to NULLS FIRST
155 * or NULLS LAST, as well as inefficient planning due to failure to match up
156 * pathkeys that should be the same. So we want a determinate result here.
157 * Because of the way the syscache search works, we'll use the interpretation
158 * associated with the opfamily with smallest OID, which is probably
159 * determinate enough. Since there is no longer any particularly good reason
160 * to build reverse-sort opfamilies, it doesn't seem worth expending any
161 * additional effort on ensuring consistency.
164 get_ordering_op_properties(Oid opno,
165 Oid *opfamily, Oid *opcintype, int16 *strategy)
171 /* ensure outputs are initialized on failure */
172 *opfamily = InvalidOid;
173 *opcintype = InvalidOid;
177 * Search pg_amop to see if the target operator is registered as the "<"
178 * or ">" operator of any btree opfamily.
180 catlist = SearchSysCacheList(AMOPOPID, 1,
181 ObjectIdGetDatum(opno),
184 for (i = 0; i < catlist->n_members; i++)
186 HeapTuple tuple = &catlist->members[i]->tuple;
187 Form_pg_amop aform = (Form_pg_amop) GETSTRUCT(tuple);
190 if (aform->amopmethod != BTREE_AM_OID)
193 if (aform->amopstrategy == BTLessStrategyNumber ||
194 aform->amopstrategy == BTGreaterStrategyNumber)
196 /* Found it ... should have consistent input types */
197 if (aform->amoplefttype == aform->amoprighttype)
199 /* Found a suitable opfamily, return info */
200 *opfamily = aform->amopfamily;
201 *opcintype = aform->amoplefttype;
202 *strategy = aform->amopstrategy;
209 ReleaseSysCacheList(catlist);
215 * get_compare_function_for_ordering_op
216 * Get the OID of the datatype-specific btree comparison function
217 * associated with an ordering operator (a "<" or ">" operator).
219 * *cmpfunc receives the comparison function OID.
220 * *reverse is set FALSE if the operator is "<", TRUE if it's ">"
221 * (indicating the comparison result must be negated before use).
223 * Returns TRUE if successful, FALSE if no btree function can be found.
224 * (This indicates that the operator is not a valid ordering operator.)
227 get_compare_function_for_ordering_op(Oid opno, Oid *cmpfunc, bool *reverse)
233 /* Find the operator in pg_amop */
234 if (get_ordering_op_properties(opno,
235 &opfamily, &opcintype, &strategy))
237 /* Found a suitable opfamily, get matching support function */
238 *cmpfunc = get_opfamily_proc(opfamily,
243 if (!OidIsValid(*cmpfunc)) /* should not happen */
244 elog(ERROR, "missing support function %d(%u,%u) in opfamily %u",
245 BTORDER_PROC, opcintype, opcintype, opfamily);
246 *reverse = (strategy == BTGreaterStrategyNumber);
250 /* ensure outputs are set on failure */
251 *cmpfunc = InvalidOid;
258 * get_equality_op_for_ordering_op
259 * Get the OID of the datatype-specific btree equality operator
260 * associated with an ordering operator (a "<" or ">" operator).
262 * If "reverse" isn't NULL, also set *reverse to FALSE if the operator is "<",
265 * Returns InvalidOid if no matching equality operator can be found.
266 * (This indicates that the operator is not a valid ordering operator.)
269 get_equality_op_for_ordering_op(Oid opno, bool *reverse)
271 Oid result = InvalidOid;
276 /* Find the operator in pg_amop */
277 if (get_ordering_op_properties(opno,
278 &opfamily, &opcintype, &strategy))
280 /* Found a suitable opfamily, get matching equality operator */
281 result = get_opfamily_member(opfamily,
284 BTEqualStrategyNumber);
286 *reverse = (strategy == BTGreaterStrategyNumber);
293 * get_ordering_op_for_equality_op
294 * Get the OID of a datatype-specific btree ordering operator
295 * associated with an equality operator. (If there are multiple
296 * possibilities, assume any one will do.)
298 * This function is used when we have to sort data before unique-ifying,
299 * and don't much care which sorting op is used as long as it's compatible
300 * with the intended equality operator. Since we need a sorting operator,
301 * it should be single-data-type even if the given operator is cross-type.
302 * The caller specifies whether to find an op for the LHS or RHS data type.
304 * Returns InvalidOid if no matching ordering operator can be found.
307 get_ordering_op_for_equality_op(Oid opno, bool use_lhs_type)
309 Oid result = InvalidOid;
314 * Search pg_amop to see if the target operator is registered as the "="
315 * operator of any btree opfamily.
317 catlist = SearchSysCacheList(AMOPOPID, 1,
318 ObjectIdGetDatum(opno),
321 for (i = 0; i < catlist->n_members; i++)
323 HeapTuple tuple = &catlist->members[i]->tuple;
324 Form_pg_amop aform = (Form_pg_amop) GETSTRUCT(tuple);
327 if (aform->amopmethod != BTREE_AM_OID)
330 if (aform->amopstrategy == BTEqualStrategyNumber)
332 /* Found a suitable opfamily, get matching ordering operator */
335 typid = use_lhs_type ? aform->amoplefttype : aform->amoprighttype;
336 result = get_opfamily_member(aform->amopfamily,
338 BTLessStrategyNumber);
339 if (OidIsValid(result))
341 /* failure probably shouldn't happen, but keep looking if so */
345 ReleaseSysCacheList(catlist);
351 * get_mergejoin_opfamilies
352 * Given a putatively mergejoinable operator, return a list of the OIDs
353 * of the btree opfamilies in which it represents equality.
355 * It is possible (though at present unusual) for an operator to be equality
356 * in more than one opfamily, hence the result is a list. This also lets us
357 * return NIL if the operator is not found in any opfamilies.
359 * The planner currently uses simple equal() tests to compare the lists
360 * returned by this function, which makes the list order relevant, though
361 * strictly speaking it should not be. Because of the way syscache list
362 * searches are handled, in normal operation the result will be sorted by OID
363 * so everything works fine. If running with system index usage disabled,
364 * the result ordering is unspecified and hence the planner might fail to
365 * recognize optimization opportunities ... but that's hardly a scenario in
366 * which performance is good anyway, so there's no point in expending code
367 * or cycles here to guarantee the ordering in that case.
370 get_mergejoin_opfamilies(Oid opno)
377 * Search pg_amop to see if the target operator is registered as the "="
378 * operator of any btree opfamily.
380 catlist = SearchSysCacheList(AMOPOPID, 1,
381 ObjectIdGetDatum(opno),
384 for (i = 0; i < catlist->n_members; i++)
386 HeapTuple tuple = &catlist->members[i]->tuple;
387 Form_pg_amop aform = (Form_pg_amop) GETSTRUCT(tuple);
389 /* must be btree equality */
390 if (aform->amopmethod == BTREE_AM_OID &&
391 aform->amopstrategy == BTEqualStrategyNumber)
392 result = lappend_oid(result, aform->amopfamily);
395 ReleaseSysCacheList(catlist);
401 * get_compatible_hash_operators
402 * Get the OID(s) of hash equality operator(s) compatible with the given
403 * operator, but operating on its LHS and/or RHS datatype.
405 * An operator for the LHS type is sought and returned into *lhs_opno if
406 * lhs_opno isn't NULL. Similarly, an operator for the RHS type is sought
407 * and returned into *rhs_opno if rhs_opno isn't NULL.
409 * If the given operator is not cross-type, the results should be the same
410 * operator, but in cross-type situations they will be different.
412 * Returns true if able to find the requested operator(s), false if not.
413 * (This indicates that the operator should not have been marked oprcanhash.)
416 get_compatible_hash_operators(Oid opno,
417 Oid *lhs_opno, Oid *rhs_opno)
423 /* Ensure output args are initialized on failure */
425 *lhs_opno = InvalidOid;
427 *rhs_opno = InvalidOid;
430 * Search pg_amop to see if the target operator is registered as the "="
431 * operator of any hash opfamily. If the operator is registered in
432 * multiple opfamilies, assume we can use any one.
434 catlist = SearchSysCacheList(AMOPOPID, 1,
435 ObjectIdGetDatum(opno),
438 for (i = 0; i < catlist->n_members; i++)
440 HeapTuple tuple = &catlist->members[i]->tuple;
441 Form_pg_amop aform = (Form_pg_amop) GETSTRUCT(tuple);
443 if (aform->amopmethod == HASH_AM_OID &&
444 aform->amopstrategy == HTEqualStrategyNumber)
446 /* No extra lookup needed if given operator is single-type */
447 if (aform->amoplefttype == aform->amoprighttype)
458 * Get the matching single-type operator(s). Failure probably
459 * shouldn't happen --- it implies a bogus opfamily --- but
460 * continue looking if so.
464 *lhs_opno = get_opfamily_member(aform->amopfamily,
467 HTEqualStrategyNumber);
468 if (!OidIsValid(*lhs_opno))
470 /* Matching LHS found, done if caller doesn't want RHS */
479 *rhs_opno = get_opfamily_member(aform->amopfamily,
480 aform->amoprighttype,
481 aform->amoprighttype,
482 HTEqualStrategyNumber);
483 if (!OidIsValid(*rhs_opno))
485 /* Forget any LHS operator from this opfamily */
487 *lhs_opno = InvalidOid;
490 /* Matching RHS found, so done */
497 ReleaseSysCacheList(catlist);
503 * get_op_hash_functions
504 * Get the OID(s) of hash support function(s) compatible with the given
505 * operator, operating on its LHS and/or RHS datatype as required.
507 * A function for the LHS type is sought and returned into *lhs_procno if
508 * lhs_procno isn't NULL. Similarly, a function for the RHS type is sought
509 * and returned into *rhs_procno if rhs_procno isn't NULL.
511 * If the given operator is not cross-type, the results should be the same
512 * function, but in cross-type situations they will be different.
514 * Returns true if able to find the requested function(s), false if not.
515 * (This indicates that the operator should not have been marked oprcanhash.)
518 get_op_hash_functions(Oid opno,
519 RegProcedure *lhs_procno, RegProcedure *rhs_procno)
525 /* Ensure output args are initialized on failure */
527 *lhs_procno = InvalidOid;
529 *rhs_procno = InvalidOid;
532 * Search pg_amop to see if the target operator is registered as the "="
533 * operator of any hash opfamily. If the operator is registered in
534 * multiple opfamilies, assume we can use any one.
536 catlist = SearchSysCacheList(AMOPOPID, 1,
537 ObjectIdGetDatum(opno),
540 for (i = 0; i < catlist->n_members; i++)
542 HeapTuple tuple = &catlist->members[i]->tuple;
543 Form_pg_amop aform = (Form_pg_amop) GETSTRUCT(tuple);
545 if (aform->amopmethod == HASH_AM_OID &&
546 aform->amopstrategy == HTEqualStrategyNumber)
549 * Get the matching support function(s). Failure probably
550 * shouldn't happen --- it implies a bogus opfamily --- but
551 * continue looking if so.
555 *lhs_procno = get_opfamily_proc(aform->amopfamily,
559 if (!OidIsValid(*lhs_procno))
561 /* Matching LHS found, done if caller doesn't want RHS */
567 /* Only one lookup needed if given operator is single-type */
568 if (aform->amoplefttype == aform->amoprighttype)
570 *rhs_procno = *lhs_procno;
577 *rhs_procno = get_opfamily_proc(aform->amopfamily,
578 aform->amoprighttype,
579 aform->amoprighttype,
581 if (!OidIsValid(*rhs_procno))
583 /* Forget any LHS function from this opfamily */
585 *lhs_procno = InvalidOid;
588 /* Matching RHS found, so done */
595 ReleaseSysCacheList(catlist);
601 * get_op_btree_interpretation
602 * Given an operator's OID, find out which btree opfamilies it belongs to,
603 * and what strategy number it has within each one. The results are
604 * returned as an OID list and a parallel integer list.
606 * In addition to the normal btree operators, we consider a <> operator to be
607 * a "member" of an opfamily if its negator is an equality operator of the
608 * opfamily. ROWCOMPARE_NE is returned as the strategy number for this case.
611 get_op_btree_interpretation(Oid opno, List **opfamilies, List **opstrats)
621 * Find all the pg_amop entries containing the operator.
623 catlist = SearchSysCacheList(AMOPOPID, 1,
624 ObjectIdGetDatum(opno),
628 * If we can't find any opfamily containing the op, perhaps it is a <>
629 * operator. See if it has a negator that is in an opfamily.
632 if (catlist->n_members == 0)
634 Oid op_negator = get_negator(opno);
636 if (OidIsValid(op_negator))
639 ReleaseSysCacheList(catlist);
640 catlist = SearchSysCacheList(AMOPOPID, 1,
641 ObjectIdGetDatum(op_negator),
646 /* Now search the opfamilies */
647 for (i = 0; i < catlist->n_members; i++)
649 HeapTuple op_tuple = &catlist->members[i]->tuple;
650 Form_pg_amop op_form = (Form_pg_amop) GETSTRUCT(op_tuple);
652 StrategyNumber op_strategy;
655 if (op_form->amopmethod != BTREE_AM_OID)
658 /* Get the operator's btree strategy number */
659 opfamily_id = op_form->amopfamily;
660 op_strategy = (StrategyNumber) op_form->amopstrategy;
661 Assert(op_strategy >= 1 && op_strategy <= 5);
665 /* Only consider negators that are = */
666 if (op_strategy != BTEqualStrategyNumber)
668 op_strategy = ROWCOMPARE_NE;
671 *opfamilies = lappend_oid(*opfamilies, opfamily_id);
672 *opstrats = lappend_int(*opstrats, op_strategy);
675 ReleaseSysCacheList(catlist);
679 * equality_ops_are_compatible
680 * Return TRUE if the two given equality operators have compatible
683 * This is trivially true if they are the same operator. Otherwise,
684 * we look to see if they can be found in the same btree or hash opfamily.
685 * Either finding allows us to assume that they have compatible notions
686 * of equality. (The reason we need to do these pushups is that one might
687 * be a cross-type operator; for instance int24eq vs int4eq.)
690 equality_ops_are_compatible(Oid opno1, Oid opno2)
696 /* Easy if they're the same operator */
701 * We search through all the pg_amop entries for opno1.
703 catlist = SearchSysCacheList(AMOPOPID, 1,
704 ObjectIdGetDatum(opno1),
708 for (i = 0; i < catlist->n_members; i++)
710 HeapTuple op_tuple = &catlist->members[i]->tuple;
711 Form_pg_amop op_form = (Form_pg_amop) GETSTRUCT(op_tuple);
713 /* must be btree or hash */
714 if (op_form->amopmethod == BTREE_AM_OID ||
715 op_form->amopmethod == HASH_AM_OID)
717 if (op_in_opfamily(opno2, op_form->amopfamily))
725 ReleaseSysCacheList(catlist);
731 /* ---------- AMPROC CACHES ---------- */
735 * Get the OID of the specified support function
736 * for the specified opfamily and datatypes.
738 * Returns InvalidOid if there is no pg_amproc entry for the given keys.
741 get_opfamily_proc(Oid opfamily, Oid lefttype, Oid righttype, int16 procnum)
744 Form_pg_amproc amproc_tup;
747 tp = SearchSysCache(AMPROCNUM,
748 ObjectIdGetDatum(opfamily),
749 ObjectIdGetDatum(lefttype),
750 ObjectIdGetDatum(righttype),
751 Int16GetDatum(procnum));
752 if (!HeapTupleIsValid(tp))
754 amproc_tup = (Form_pg_amproc) GETSTRUCT(tp);
755 result = amproc_tup->amproc;
761 /* ---------- ATTRIBUTE CACHES ---------- */
765 * Given the relation id and the attribute number,
766 * return the "attname" field from the attribute relation.
768 * Note: returns a palloc'd copy of the string, or NULL if no such attribute.
771 get_attname(Oid relid, AttrNumber attnum)
775 tp = SearchSysCache(ATTNUM,
776 ObjectIdGetDatum(relid),
777 Int16GetDatum(attnum),
779 if (HeapTupleIsValid(tp))
781 Form_pg_attribute att_tup = (Form_pg_attribute) GETSTRUCT(tp);
784 result = pstrdup(NameStr(att_tup->attname));
793 * get_relid_attribute_name
795 * Same as above routine get_attname(), except that error
796 * is handled by elog() instead of returning NULL.
799 get_relid_attribute_name(Oid relid, AttrNumber attnum)
803 attname = get_attname(relid, attnum);
805 elog(ERROR, "cache lookup failed for attribute %d of relation %u",
813 * Given the relation id and the attribute name,
814 * return the "attnum" field from the attribute relation.
816 * Returns InvalidAttrNumber if the attr doesn't exist (or is dropped).
819 get_attnum(Oid relid, const char *attname)
823 tp = SearchSysCacheAttName(relid, attname);
824 if (HeapTupleIsValid(tp))
826 Form_pg_attribute att_tup = (Form_pg_attribute) GETSTRUCT(tp);
829 result = att_tup->attnum;
834 return InvalidAttrNumber;
840 * Given the relation OID and the attribute number with the relation,
841 * return the attribute type OID.
844 get_atttype(Oid relid, AttrNumber attnum)
848 tp = SearchSysCache(ATTNUM,
849 ObjectIdGetDatum(relid),
850 Int16GetDatum(attnum),
852 if (HeapTupleIsValid(tp))
854 Form_pg_attribute att_tup = (Form_pg_attribute) GETSTRUCT(tp);
857 result = att_tup->atttypid;
868 * Given the relation id and the attribute number,
869 * return the "atttypmod" field from the attribute relation.
872 get_atttypmod(Oid relid, AttrNumber attnum)
876 tp = SearchSysCache(ATTNUM,
877 ObjectIdGetDatum(relid),
878 Int16GetDatum(attnum),
880 if (HeapTupleIsValid(tp))
882 Form_pg_attribute att_tup = (Form_pg_attribute) GETSTRUCT(tp);
885 result = att_tup->atttypmod;
896 * A two-fer: given the relation id and the attribute number,
897 * fetch both type OID and atttypmod in a single cache lookup.
899 * Unlike the otherwise-similar get_atttype/get_atttypmod, this routine
900 * raises an error if it can't obtain the information.
903 get_atttypetypmod(Oid relid, AttrNumber attnum,
904 Oid *typid, int32 *typmod)
907 Form_pg_attribute att_tup;
909 tp = SearchSysCache(ATTNUM,
910 ObjectIdGetDatum(relid),
911 Int16GetDatum(attnum),
913 if (!HeapTupleIsValid(tp))
914 elog(ERROR, "cache lookup failed for attribute %d of relation %u",
916 att_tup = (Form_pg_attribute) GETSTRUCT(tp);
918 *typid = att_tup->atttypid;
919 *typmod = att_tup->atttypmod;
923 /* ---------- CONSTRAINT CACHE ---------- */
926 * get_constraint_name
927 * Returns the name of a given pg_constraint entry.
929 * Returns a palloc'd copy of the string, or NULL if no such constraint.
931 * NOTE: since constraint name is not unique, be wary of code that uses this
932 * for anything except preparing error messages.
935 get_constraint_name(Oid conoid)
939 tp = SearchSysCache(CONSTROID,
940 ObjectIdGetDatum(conoid),
942 if (HeapTupleIsValid(tp))
944 Form_pg_constraint contup = (Form_pg_constraint) GETSTRUCT(tp);
947 result = pstrdup(NameStr(contup->conname));
955 /* ---------- OPCLASS CACHE ---------- */
960 * Returns the OID of the operator family the opclass belongs to.
963 get_opclass_family(Oid opclass)
966 Form_pg_opclass cla_tup;
969 tp = SearchSysCache(CLAOID,
970 ObjectIdGetDatum(opclass),
972 if (!HeapTupleIsValid(tp))
973 elog(ERROR, "cache lookup failed for opclass %u", opclass);
974 cla_tup = (Form_pg_opclass) GETSTRUCT(tp);
976 result = cla_tup->opcfamily;
982 * get_opclass_input_type
984 * Returns the OID of the datatype the opclass indexes.
987 get_opclass_input_type(Oid opclass)
990 Form_pg_opclass cla_tup;
993 tp = SearchSysCache(CLAOID,
994 ObjectIdGetDatum(opclass),
996 if (!HeapTupleIsValid(tp))
997 elog(ERROR, "cache lookup failed for opclass %u", opclass);
998 cla_tup = (Form_pg_opclass) GETSTRUCT(tp);
1000 result = cla_tup->opcintype;
1001 ReleaseSysCache(tp);
1005 /* ---------- OPERATOR CACHE ---------- */
1010 * Returns the regproc id of the routine used to implement an
1011 * operator given the operator oid.
1014 get_opcode(Oid opno)
1018 tp = SearchSysCache(OPEROID,
1019 ObjectIdGetDatum(opno),
1021 if (HeapTupleIsValid(tp))
1023 Form_pg_operator optup = (Form_pg_operator) GETSTRUCT(tp);
1024 RegProcedure result;
1026 result = optup->oprcode;
1027 ReleaseSysCache(tp);
1031 return (RegProcedure) InvalidOid;
1036 * returns the name of the operator with the given opno
1038 * Note: returns a palloc'd copy of the string, or NULL if no such operator.
1041 get_opname(Oid opno)
1045 tp = SearchSysCache(OPEROID,
1046 ObjectIdGetDatum(opno),
1048 if (HeapTupleIsValid(tp))
1050 Form_pg_operator optup = (Form_pg_operator) GETSTRUCT(tp);
1053 result = pstrdup(NameStr(optup->oprname));
1054 ReleaseSysCache(tp);
1064 * Returns the left and right input datatypes for an operator
1065 * (InvalidOid if not relevant).
1068 op_input_types(Oid opno, Oid *lefttype, Oid *righttype)
1071 Form_pg_operator optup;
1073 tp = SearchSysCache(OPEROID,
1074 ObjectIdGetDatum(opno),
1076 if (!HeapTupleIsValid(tp)) /* shouldn't happen */
1077 elog(ERROR, "cache lookup failed for operator %u", opno);
1078 optup = (Form_pg_operator) GETSTRUCT(tp);
1079 *lefttype = optup->oprleft;
1080 *righttype = optup->oprright;
1081 ReleaseSysCache(tp);
1087 * Returns true if the operator is potentially mergejoinable. (The planner
1088 * will fail to find any mergejoin plans unless there are suitable btree
1089 * opfamily entries for this operator and associated sortops. The pg_operator
1090 * flag is just a hint to tell the planner whether to bother looking.)
1093 op_mergejoinable(Oid opno)
1096 bool result = false;
1098 tp = SearchSysCache(OPEROID,
1099 ObjectIdGetDatum(opno),
1101 if (HeapTupleIsValid(tp))
1103 Form_pg_operator optup = (Form_pg_operator) GETSTRUCT(tp);
1105 result = optup->oprcanmerge;
1106 ReleaseSysCache(tp);
1114 * Returns true if the operator is hashjoinable. (There must be a suitable
1115 * hash opfamily entry for this operator if it is so marked.)
1118 op_hashjoinable(Oid opno)
1121 bool result = false;
1123 tp = SearchSysCache(OPEROID,
1124 ObjectIdGetDatum(opno),
1126 if (HeapTupleIsValid(tp))
1128 Form_pg_operator optup = (Form_pg_operator) GETSTRUCT(tp);
1130 result = optup->oprcanhash;
1131 ReleaseSysCache(tp);
1139 * Get the proisstrict flag for the operator's underlying function.
1144 RegProcedure funcid = get_opcode(opno);
1146 if (funcid == (RegProcedure) InvalidOid)
1147 elog(ERROR, "operator %u does not exist", opno);
1149 return func_strict((Oid) funcid);
1155 * Get the provolatile flag for the operator's underlying function.
1158 op_volatile(Oid opno)
1160 RegProcedure funcid = get_opcode(opno);
1162 if (funcid == (RegProcedure) InvalidOid)
1163 elog(ERROR, "operator %u does not exist", opno);
1165 return func_volatile((Oid) funcid);
1171 * Returns the corresponding commutator of an operator.
1174 get_commutator(Oid opno)
1178 tp = SearchSysCache(OPEROID,
1179 ObjectIdGetDatum(opno),
1181 if (HeapTupleIsValid(tp))
1183 Form_pg_operator optup = (Form_pg_operator) GETSTRUCT(tp);
1186 result = optup->oprcom;
1187 ReleaseSysCache(tp);
1197 * Returns the corresponding negator of an operator.
1200 get_negator(Oid opno)
1204 tp = SearchSysCache(OPEROID,
1205 ObjectIdGetDatum(opno),
1207 if (HeapTupleIsValid(tp))
1209 Form_pg_operator optup = (Form_pg_operator) GETSTRUCT(tp);
1212 result = optup->oprnegate;
1213 ReleaseSysCache(tp);
1223 * Returns procedure id for computing selectivity of an operator.
1226 get_oprrest(Oid opno)
1230 tp = SearchSysCache(OPEROID,
1231 ObjectIdGetDatum(opno),
1233 if (HeapTupleIsValid(tp))
1235 Form_pg_operator optup = (Form_pg_operator) GETSTRUCT(tp);
1236 RegProcedure result;
1238 result = optup->oprrest;
1239 ReleaseSysCache(tp);
1243 return (RegProcedure) InvalidOid;
1249 * Returns procedure id for computing selectivity of a join.
1252 get_oprjoin(Oid opno)
1256 tp = SearchSysCache(OPEROID,
1257 ObjectIdGetDatum(opno),
1259 if (HeapTupleIsValid(tp))
1261 Form_pg_operator optup = (Form_pg_operator) GETSTRUCT(tp);
1262 RegProcedure result;
1264 result = optup->oprjoin;
1265 ReleaseSysCache(tp);
1269 return (RegProcedure) InvalidOid;
1272 /* ---------- FUNCTION CACHE ---------- */
1276 * returns the name of the function with the given funcid
1278 * Note: returns a palloc'd copy of the string, or NULL if no such function.
1281 get_func_name(Oid funcid)
1285 tp = SearchSysCache(PROCOID,
1286 ObjectIdGetDatum(funcid),
1288 if (HeapTupleIsValid(tp))
1290 Form_pg_proc functup = (Form_pg_proc) GETSTRUCT(tp);
1293 result = pstrdup(NameStr(functup->proname));
1294 ReleaseSysCache(tp);
1302 * get_func_namespace
1304 * Returns the pg_namespace OID associated with a given function.
1307 get_func_namespace(Oid funcid)
1311 tp = SearchSysCache(PROCOID,
1312 ObjectIdGetDatum(funcid),
1314 if (HeapTupleIsValid(tp))
1316 Form_pg_proc functup = (Form_pg_proc) GETSTRUCT(tp);
1319 result = functup->pronamespace;
1320 ReleaseSysCache(tp);
1329 * Given procedure id, return the function's result type.
1332 get_func_rettype(Oid funcid)
1337 tp = SearchSysCache(PROCOID,
1338 ObjectIdGetDatum(funcid),
1340 if (!HeapTupleIsValid(tp))
1341 elog(ERROR, "cache lookup failed for function %u", funcid);
1343 result = ((Form_pg_proc) GETSTRUCT(tp))->prorettype;
1344 ReleaseSysCache(tp);
1350 * Given procedure id, return the number of arguments.
1353 get_func_nargs(Oid funcid)
1358 tp = SearchSysCache(PROCOID,
1359 ObjectIdGetDatum(funcid),
1361 if (!HeapTupleIsValid(tp))
1362 elog(ERROR, "cache lookup failed for function %u", funcid);
1364 result = ((Form_pg_proc) GETSTRUCT(tp))->pronargs;
1365 ReleaseSysCache(tp);
1370 * get_func_signature
1371 * Given procedure id, return the function's argument and result types.
1372 * (The return value is the result type.)
1374 * The arguments are returned as a palloc'd array.
1377 get_func_signature(Oid funcid, Oid **argtypes, int *nargs)
1380 Form_pg_proc procstruct;
1383 tp = SearchSysCache(PROCOID,
1384 ObjectIdGetDatum(funcid),
1386 if (!HeapTupleIsValid(tp))
1387 elog(ERROR, "cache lookup failed for function %u", funcid);
1389 procstruct = (Form_pg_proc) GETSTRUCT(tp);
1391 result = procstruct->prorettype;
1392 *nargs = (int) procstruct->pronargs;
1393 Assert(*nargs == procstruct->proargtypes.dim1);
1394 *argtypes = (Oid *) palloc(*nargs * sizeof(Oid));
1395 memcpy(*argtypes, procstruct->proargtypes.values, *nargs * sizeof(Oid));
1397 ReleaseSysCache(tp);
1403 * Given procedure id, return the function's proretset flag.
1406 get_func_retset(Oid funcid)
1411 tp = SearchSysCache(PROCOID,
1412 ObjectIdGetDatum(funcid),
1414 if (!HeapTupleIsValid(tp))
1415 elog(ERROR, "cache lookup failed for function %u", funcid);
1417 result = ((Form_pg_proc) GETSTRUCT(tp))->proretset;
1418 ReleaseSysCache(tp);
1424 * Given procedure id, return the function's proisstrict flag.
1427 func_strict(Oid funcid)
1432 tp = SearchSysCache(PROCOID,
1433 ObjectIdGetDatum(funcid),
1435 if (!HeapTupleIsValid(tp))
1436 elog(ERROR, "cache lookup failed for function %u", funcid);
1438 result = ((Form_pg_proc) GETSTRUCT(tp))->proisstrict;
1439 ReleaseSysCache(tp);
1445 * Given procedure id, return the function's provolatile flag.
1448 func_volatile(Oid funcid)
1453 tp = SearchSysCache(PROCOID,
1454 ObjectIdGetDatum(funcid),
1456 if (!HeapTupleIsValid(tp))
1457 elog(ERROR, "cache lookup failed for function %u", funcid);
1459 result = ((Form_pg_proc) GETSTRUCT(tp))->provolatile;
1460 ReleaseSysCache(tp);
1466 * Given procedure id, return the function's procost field.
1469 get_func_cost(Oid funcid)
1474 tp = SearchSysCache(PROCOID,
1475 ObjectIdGetDatum(funcid),
1477 if (!HeapTupleIsValid(tp))
1478 elog(ERROR, "cache lookup failed for function %u", funcid);
1480 result = ((Form_pg_proc) GETSTRUCT(tp))->procost;
1481 ReleaseSysCache(tp);
1487 * Given procedure id, return the function's prorows field.
1490 get_func_rows(Oid funcid)
1495 tp = SearchSysCache(PROCOID,
1496 ObjectIdGetDatum(funcid),
1498 if (!HeapTupleIsValid(tp))
1499 elog(ERROR, "cache lookup failed for function %u", funcid);
1501 result = ((Form_pg_proc) GETSTRUCT(tp))->prorows;
1502 ReleaseSysCache(tp);
1506 /* ---------- RELATION CACHE ---------- */
1510 * Given name and namespace of a relation, look up the OID.
1512 * Returns InvalidOid if there is no such relation.
1515 get_relname_relid(const char *relname, Oid relnamespace)
1517 return GetSysCacheOid(RELNAMENSP,
1518 PointerGetDatum(relname),
1519 ObjectIdGetDatum(relnamespace),
1527 * Returns the number of attributes for a given relation.
1530 get_relnatts(Oid relid)
1534 tp = SearchSysCache(RELOID,
1535 ObjectIdGetDatum(relid),
1537 if (HeapTupleIsValid(tp))
1539 Form_pg_class reltup = (Form_pg_class) GETSTRUCT(tp);
1542 result = reltup->relnatts;
1543 ReleaseSysCache(tp);
1547 return InvalidAttrNumber;
1553 * Returns the name of a given relation.
1555 * Returns a palloc'd copy of the string, or NULL if no such relation.
1557 * NOTE: since relation name is not unique, be wary of code that uses this
1558 * for anything except preparing error messages.
1561 get_rel_name(Oid relid)
1565 tp = SearchSysCache(RELOID,
1566 ObjectIdGetDatum(relid),
1568 if (HeapTupleIsValid(tp))
1570 Form_pg_class reltup = (Form_pg_class) GETSTRUCT(tp);
1573 result = pstrdup(NameStr(reltup->relname));
1574 ReleaseSysCache(tp);
1584 * Returns the pg_namespace OID associated with a given relation.
1587 get_rel_namespace(Oid relid)
1591 tp = SearchSysCache(RELOID,
1592 ObjectIdGetDatum(relid),
1594 if (HeapTupleIsValid(tp))
1596 Form_pg_class reltup = (Form_pg_class) GETSTRUCT(tp);
1599 result = reltup->relnamespace;
1600 ReleaseSysCache(tp);
1610 * Returns the pg_type OID associated with a given relation.
1612 * Note: not all pg_class entries have associated pg_type OIDs; so be
1613 * careful to check for InvalidOid result.
1616 get_rel_type_id(Oid relid)
1620 tp = SearchSysCache(RELOID,
1621 ObjectIdGetDatum(relid),
1623 if (HeapTupleIsValid(tp))
1625 Form_pg_class reltup = (Form_pg_class) GETSTRUCT(tp);
1628 result = reltup->reltype;
1629 ReleaseSysCache(tp);
1639 * Returns the relkind associated with a given relation.
1642 get_rel_relkind(Oid relid)
1646 tp = SearchSysCache(RELOID,
1647 ObjectIdGetDatum(relid),
1649 if (HeapTupleIsValid(tp))
1651 Form_pg_class reltup = (Form_pg_class) GETSTRUCT(tp);
1654 result = reltup->relkind;
1655 ReleaseSysCache(tp);
1663 * get_rel_tablespace
1665 * Returns the pg_tablespace OID associated with a given relation.
1667 * Note: InvalidOid might mean either that we couldn't find the relation,
1668 * or that it is in the database's default tablespace.
1671 get_rel_tablespace(Oid relid)
1675 tp = SearchSysCache(RELOID,
1676 ObjectIdGetDatum(relid),
1678 if (HeapTupleIsValid(tp))
1680 Form_pg_class reltup = (Form_pg_class) GETSTRUCT(tp);
1683 result = reltup->reltablespace;
1684 ReleaseSysCache(tp);
1692 /* ---------- TYPE CACHE ---------- */
1697 * Given the type OID, determine whether the type is defined
1698 * (if not, it's only a shell).
1701 get_typisdefined(Oid typid)
1705 tp = SearchSysCache(TYPEOID,
1706 ObjectIdGetDatum(typid),
1708 if (HeapTupleIsValid(tp))
1710 Form_pg_type typtup = (Form_pg_type) GETSTRUCT(tp);
1713 result = typtup->typisdefined;
1714 ReleaseSysCache(tp);
1724 * Given the type OID, return the length of the type.
1727 get_typlen(Oid typid)
1731 tp = SearchSysCache(TYPEOID,
1732 ObjectIdGetDatum(typid),
1734 if (HeapTupleIsValid(tp))
1736 Form_pg_type typtup = (Form_pg_type) GETSTRUCT(tp);
1739 result = typtup->typlen;
1740 ReleaseSysCache(tp);
1750 * Given the type OID, determine whether the type is returned by value or
1751 * not. Returns true if by value, false if by reference.
1754 get_typbyval(Oid typid)
1758 tp = SearchSysCache(TYPEOID,
1759 ObjectIdGetDatum(typid),
1761 if (HeapTupleIsValid(tp))
1763 Form_pg_type typtup = (Form_pg_type) GETSTRUCT(tp);
1766 result = typtup->typbyval;
1767 ReleaseSysCache(tp);
1777 * A two-fer: given the type OID, return both typlen and typbyval.
1779 * Since both pieces of info are needed to know how to copy a Datum,
1780 * many places need both. Might as well get them with one cache lookup
1781 * instead of two. Also, this routine raises an error instead of
1782 * returning a bogus value when given a bad type OID.
1785 get_typlenbyval(Oid typid, int16 *typlen, bool *typbyval)
1788 Form_pg_type typtup;
1790 tp = SearchSysCache(TYPEOID,
1791 ObjectIdGetDatum(typid),
1793 if (!HeapTupleIsValid(tp))
1794 elog(ERROR, "cache lookup failed for type %u", typid);
1795 typtup = (Form_pg_type) GETSTRUCT(tp);
1796 *typlen = typtup->typlen;
1797 *typbyval = typtup->typbyval;
1798 ReleaseSysCache(tp);
1802 * get_typlenbyvalalign
1804 * A three-fer: given the type OID, return typlen, typbyval, typalign.
1807 get_typlenbyvalalign(Oid typid, int16 *typlen, bool *typbyval,
1811 Form_pg_type typtup;
1813 tp = SearchSysCache(TYPEOID,
1814 ObjectIdGetDatum(typid),
1816 if (!HeapTupleIsValid(tp))
1817 elog(ERROR, "cache lookup failed for type %u", typid);
1818 typtup = (Form_pg_type) GETSTRUCT(tp);
1819 *typlen = typtup->typlen;
1820 *typbyval = typtup->typbyval;
1821 *typalign = typtup->typalign;
1822 ReleaseSysCache(tp);
1827 * Given a pg_type row, select the type OID to pass to I/O functions
1829 * Formerly, all I/O functions were passed pg_type.typelem as their second
1830 * parameter, but we now have a more complex rule about what to pass.
1831 * This knowledge is intended to be centralized here --- direct references
1832 * to typelem elsewhere in the code are wrong, if they are associated with
1833 * I/O calls and not with actual subscripting operations! (But see
1834 * bootstrap.c's boot_get_type_io_data() if you need to change this.)
1836 * As of PostgreSQL 8.1, output functions receive only the value itself
1837 * and not any auxiliary parameters, so the name of this routine is now
1838 * a bit of a misnomer ... it should be getTypeInputParam.
1841 getTypeIOParam(HeapTuple typeTuple)
1843 Form_pg_type typeStruct = (Form_pg_type) GETSTRUCT(typeTuple);
1846 * Array types get their typelem as parameter; everybody else gets their
1847 * own type OID as parameter. (As of 8.2, domains must get their own OID
1848 * even if their base type is an array.)
1850 if (typeStruct->typtype == TYPTYPE_BASE && OidIsValid(typeStruct->typelem))
1851 return typeStruct->typelem;
1853 return HeapTupleGetOid(typeTuple);
1859 * A six-fer: given the type OID, return typlen, typbyval, typalign,
1860 * typdelim, typioparam, and IO function OID. The IO function
1861 * returned is controlled by IOFuncSelector
1864 get_type_io_data(Oid typid,
1865 IOFuncSelector which_func,
1873 HeapTuple typeTuple;
1874 Form_pg_type typeStruct;
1877 * In bootstrap mode, pass it off to bootstrap.c. This hack allows us to
1878 * use array_in and array_out during bootstrap.
1880 if (IsBootstrapProcessingMode())
1885 boot_get_type_io_data(typid,
1902 elog(ERROR, "binary I/O not supported during bootstrap");
1908 typeTuple = SearchSysCache(TYPEOID,
1909 ObjectIdGetDatum(typid),
1911 if (!HeapTupleIsValid(typeTuple))
1912 elog(ERROR, "cache lookup failed for type %u", typid);
1913 typeStruct = (Form_pg_type) GETSTRUCT(typeTuple);
1915 *typlen = typeStruct->typlen;
1916 *typbyval = typeStruct->typbyval;
1917 *typalign = typeStruct->typalign;
1918 *typdelim = typeStruct->typdelim;
1919 *typioparam = getTypeIOParam(typeTuple);
1923 *func = typeStruct->typinput;
1926 *func = typeStruct->typoutput;
1928 case IOFunc_receive:
1929 *func = typeStruct->typreceive;
1932 *func = typeStruct->typsend;
1935 ReleaseSysCache(typeTuple);
1940 get_typalign(Oid typid)
1944 tp = SearchSysCache(TYPEOID,
1945 ObjectIdGetDatum(typid),
1947 if (HeapTupleIsValid(tp))
1949 Form_pg_type typtup = (Form_pg_type) GETSTRUCT(tp);
1952 result = typtup->typalign;
1953 ReleaseSysCache(tp);
1962 get_typstorage(Oid typid)
1966 tp = SearchSysCache(TYPEOID,
1967 ObjectIdGetDatum(typid),
1969 if (HeapTupleIsValid(tp))
1971 Form_pg_type typtup = (Form_pg_type) GETSTRUCT(tp);
1974 result = typtup->typstorage;
1975 ReleaseSysCache(tp);
1984 * Given a type OID, return the type's default value, if any.
1986 * The result is a palloc'd expression node tree, or NULL if there
1987 * is no defined default for the datatype.
1989 * NB: caller should be prepared to coerce result to correct datatype;
1990 * the returned expression tree might produce something of the wrong type.
1993 get_typdefault(Oid typid)
1995 HeapTuple typeTuple;
2001 typeTuple = SearchSysCache(TYPEOID,
2002 ObjectIdGetDatum(typid),
2004 if (!HeapTupleIsValid(typeTuple))
2005 elog(ERROR, "cache lookup failed for type %u", typid);
2006 type = (Form_pg_type) GETSTRUCT(typeTuple);
2009 * typdefault and typdefaultbin are potentially null, so don't try to
2010 * access 'em as struct fields. Must do it the hard way with
2013 datum = SysCacheGetAttr(TYPEOID,
2015 Anum_pg_type_typdefaultbin,
2020 /* We have an expression default */
2021 expr = stringToNode(TextDatumGetCString(datum));
2025 /* Perhaps we have a plain literal default */
2026 datum = SysCacheGetAttr(TYPEOID,
2028 Anum_pg_type_typdefault,
2033 char *strDefaultVal;
2035 /* Convert text datum to C string */
2036 strDefaultVal = TextDatumGetCString(datum);
2037 /* Convert C string to a value of the given type */
2038 datum = OidInputFunctionCall(type->typinput, strDefaultVal,
2039 getTypeIOParam(typeTuple), -1);
2040 /* Build a Const node containing the value */
2041 expr = (Node *) makeConst(typid,
2047 pfree(strDefaultVal);
2056 ReleaseSysCache(typeTuple);
2063 * If the given type is a domain, return its base type;
2064 * otherwise return the type's own OID.
2067 getBaseType(Oid typid)
2071 return getBaseTypeAndTypmod(typid, &typmod);
2075 * getBaseTypeAndTypmod
2076 * If the given type is a domain, return its base type and typmod;
2077 * otherwise return the type's own OID, and leave *typmod unchanged.
2079 * Note that the "applied typmod" should be -1 for every domain level
2080 * above the bottommost; therefore, if the passed-in typid is indeed
2081 * a domain, *typmod should be -1.
2084 getBaseTypeAndTypmod(Oid typid, int32 *typmod)
2087 * We loop to find the bottom base type in a stack of domains.
2092 Form_pg_type typTup;
2094 tup = SearchSysCache(TYPEOID,
2095 ObjectIdGetDatum(typid),
2097 if (!HeapTupleIsValid(tup))
2098 elog(ERROR, "cache lookup failed for type %u", typid);
2099 typTup = (Form_pg_type) GETSTRUCT(tup);
2100 if (typTup->typtype != TYPTYPE_DOMAIN)
2102 /* Not a domain, so done */
2103 ReleaseSysCache(tup);
2107 Assert(*typmod == -1);
2108 typid = typTup->typbasetype;
2109 *typmod = typTup->typtypmod;
2111 ReleaseSysCache(tup);
2120 * Given a type OID and a typmod value (pass -1 if typmod is unknown),
2121 * estimate the average width of values of the type. This is used by
2122 * the planner, which doesn't require absolutely correct results;
2123 * it's OK (and expected) to guess if we don't know for sure.
2126 get_typavgwidth(Oid typid, int32 typmod)
2128 int typlen = get_typlen(typid);
2132 * Easy if it's a fixed-width type
2138 * type_maximum_size knows the encoding of typmod for some datatypes;
2139 * don't duplicate that knowledge here.
2141 maxwidth = type_maximum_size(typid, typmod);
2145 * For BPCHAR, the max width is also the only width. Otherwise we
2146 * need to guess about the typical data width given the max. A sliding
2147 * scale for percentage of max width seems reasonable.
2149 if (typid == BPCHAROID)
2152 return maxwidth; /* assume full width */
2153 if (maxwidth < 1000)
2154 return 32 + (maxwidth - 32) / 2; /* assume 50% */
2157 * Beyond 1000, assume we're looking at something like
2158 * "varchar(10000)" where the limit isn't actually reached often, and
2159 * use a fixed estimate.
2161 return 32 + (1000 - 32) / 2;
2165 * Ooops, we have no idea ... wild guess time.
2173 * Given the type OID, find if it is a basic type, a complex type, etc.
2174 * It returns the null char if the cache lookup fails...
2177 get_typtype(Oid typid)
2181 tp = SearchSysCache(TYPEOID,
2182 ObjectIdGetDatum(typid),
2184 if (HeapTupleIsValid(tp))
2186 Form_pg_type typtup = (Form_pg_type) GETSTRUCT(tp);
2189 result = typtup->typtype;
2190 ReleaseSysCache(tp);
2200 * Convenience function to determine whether a type OID represents
2201 * a "rowtype" type --- either RECORD or a named composite type.
2204 type_is_rowtype(Oid typid)
2206 return (typid == RECORDOID || get_typtype(typid) == TYPTYPE_COMPOSITE);
2211 * Returns true if the given type is an enum type.
2214 type_is_enum(Oid typid)
2216 return (get_typtype(typid) == TYPTYPE_ENUM);
2220 * get_type_category_preferred
2222 * Given the type OID, fetch its category and preferred-type status.
2223 * Throws error on failure.
2226 get_type_category_preferred(Oid typid, char *typcategory, bool *typispreferred)
2229 Form_pg_type typtup;
2231 tp = SearchSysCache(TYPEOID,
2232 ObjectIdGetDatum(typid),
2234 if (!HeapTupleIsValid(tp))
2235 elog(ERROR, "cache lookup failed for type %u", typid);
2236 typtup = (Form_pg_type) GETSTRUCT(tp);
2237 *typcategory = typtup->typcategory;
2238 *typispreferred = typtup->typispreferred;
2239 ReleaseSysCache(tp);
2245 * Given the type OID, get the typrelid (InvalidOid if not a complex
2249 get_typ_typrelid(Oid typid)
2253 tp = SearchSysCache(TYPEOID,
2254 ObjectIdGetDatum(typid),
2256 if (HeapTupleIsValid(tp))
2258 Form_pg_type typtup = (Form_pg_type) GETSTRUCT(tp);
2261 result = typtup->typrelid;
2262 ReleaseSysCache(tp);
2272 * Given the type OID, get the typelem (InvalidOid if not an array type).
2274 * NB: this only considers varlena arrays to be true arrays; InvalidOid is
2275 * returned if the input is a fixed-length array type.
2278 get_element_type(Oid typid)
2282 tp = SearchSysCache(TYPEOID,
2283 ObjectIdGetDatum(typid),
2285 if (HeapTupleIsValid(tp))
2287 Form_pg_type typtup = (Form_pg_type) GETSTRUCT(tp);
2290 if (typtup->typlen == -1)
2291 result = typtup->typelem;
2293 result = InvalidOid;
2294 ReleaseSysCache(tp);
2304 * Given the type OID, get the corresponding "true" array type.
2305 * Returns InvalidOid if no array type can be found.
2308 get_array_type(Oid typid)
2311 Oid result = InvalidOid;
2313 tp = SearchSysCache(TYPEOID,
2314 ObjectIdGetDatum(typid),
2316 if (HeapTupleIsValid(tp))
2318 result = ((Form_pg_type) GETSTRUCT(tp))->typarray;
2319 ReleaseSysCache(tp);
2327 * Get info needed for converting values of a type to internal form
2330 getTypeInputInfo(Oid type, Oid *typInput, Oid *typIOParam)
2332 HeapTuple typeTuple;
2335 typeTuple = SearchSysCache(TYPEOID,
2336 ObjectIdGetDatum(type),
2338 if (!HeapTupleIsValid(typeTuple))
2339 elog(ERROR, "cache lookup failed for type %u", type);
2340 pt = (Form_pg_type) GETSTRUCT(typeTuple);
2342 if (!pt->typisdefined)
2344 (errcode(ERRCODE_UNDEFINED_OBJECT),
2345 errmsg("type %s is only a shell",
2346 format_type_be(type))));
2347 if (!OidIsValid(pt->typinput))
2349 (errcode(ERRCODE_UNDEFINED_FUNCTION),
2350 errmsg("no input function available for type %s",
2351 format_type_be(type))));
2353 *typInput = pt->typinput;
2354 *typIOParam = getTypeIOParam(typeTuple);
2356 ReleaseSysCache(typeTuple);
2362 * Get info needed for printing values of a type
2365 getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
2367 HeapTuple typeTuple;
2370 typeTuple = SearchSysCache(TYPEOID,
2371 ObjectIdGetDatum(type),
2373 if (!HeapTupleIsValid(typeTuple))
2374 elog(ERROR, "cache lookup failed for type %u", type);
2375 pt = (Form_pg_type) GETSTRUCT(typeTuple);
2377 if (!pt->typisdefined)
2379 (errcode(ERRCODE_UNDEFINED_OBJECT),
2380 errmsg("type %s is only a shell",
2381 format_type_be(type))));
2382 if (!OidIsValid(pt->typoutput))
2384 (errcode(ERRCODE_UNDEFINED_FUNCTION),
2385 errmsg("no output function available for type %s",
2386 format_type_be(type))));
2388 *typOutput = pt->typoutput;
2389 *typIsVarlena = (!pt->typbyval) && (pt->typlen == -1);
2391 ReleaseSysCache(typeTuple);
2395 * getTypeBinaryInputInfo
2397 * Get info needed for binary input of values of a type
2400 getTypeBinaryInputInfo(Oid type, Oid *typReceive, Oid *typIOParam)
2402 HeapTuple typeTuple;
2405 typeTuple = SearchSysCache(TYPEOID,
2406 ObjectIdGetDatum(type),
2408 if (!HeapTupleIsValid(typeTuple))
2409 elog(ERROR, "cache lookup failed for type %u", type);
2410 pt = (Form_pg_type) GETSTRUCT(typeTuple);
2412 if (!pt->typisdefined)
2414 (errcode(ERRCODE_UNDEFINED_OBJECT),
2415 errmsg("type %s is only a shell",
2416 format_type_be(type))));
2417 if (!OidIsValid(pt->typreceive))
2419 (errcode(ERRCODE_UNDEFINED_FUNCTION),
2420 errmsg("no binary input function available for type %s",
2421 format_type_be(type))));
2423 *typReceive = pt->typreceive;
2424 *typIOParam = getTypeIOParam(typeTuple);
2426 ReleaseSysCache(typeTuple);
2430 * getTypeBinaryOutputInfo
2432 * Get info needed for binary output of values of a type
2435 getTypeBinaryOutputInfo(Oid type, Oid *typSend, bool *typIsVarlena)
2437 HeapTuple typeTuple;
2440 typeTuple = SearchSysCache(TYPEOID,
2441 ObjectIdGetDatum(type),
2443 if (!HeapTupleIsValid(typeTuple))
2444 elog(ERROR, "cache lookup failed for type %u", type);
2445 pt = (Form_pg_type) GETSTRUCT(typeTuple);
2447 if (!pt->typisdefined)
2449 (errcode(ERRCODE_UNDEFINED_OBJECT),
2450 errmsg("type %s is only a shell",
2451 format_type_be(type))));
2452 if (!OidIsValid(pt->typsend))
2454 (errcode(ERRCODE_UNDEFINED_FUNCTION),
2455 errmsg("no binary output function available for type %s",
2456 format_type_be(type))));
2458 *typSend = pt->typsend;
2459 *typIsVarlena = (!pt->typbyval) && (pt->typlen == -1);
2461 ReleaseSysCache(typeTuple);
2467 * Given the type OID, return the type's typmodin procedure, if any.
2470 get_typmodin(Oid typid)
2474 tp = SearchSysCache(TYPEOID,
2475 ObjectIdGetDatum(typid),
2477 if (HeapTupleIsValid(tp))
2479 Form_pg_type typtup = (Form_pg_type) GETSTRUCT(tp);
2482 result = typtup->typmodin;
2483 ReleaseSysCache(tp);
2494 * Given the type OID, return the type's typmodout procedure, if any.
2497 get_typmodout(Oid typid)
2501 tp = SearchSysCache(TYPEOID,
2502 ObjectIdGetDatum(typid),
2504 if (HeapTupleIsValid(tp))
2506 Form_pg_type typtup = (Form_pg_type) GETSTRUCT(tp);
2509 result = typtup->typmodout;
2510 ReleaseSysCache(tp);
2516 #endif /* NOT_USED */
2519 /* ---------- STATISTICS CACHE ---------- */
2524 * Given the table and attribute number of a column, get the average
2525 * width of entries in the column. Return zero if no data available.
2527 * Currently this is only consulted for individual tables, not for inheritance
2528 * trees, so we don't need an "inh" parameter.
2530 * Calling a hook at this point looks somewhat strange, but is required
2531 * because the optimizer calls this function without any other way for
2532 * plug-ins to control the result.
2535 get_attavgwidth(Oid relid, AttrNumber attnum)
2540 if (get_attavgwidth_hook)
2542 stawidth = (*get_attavgwidth_hook) (relid, attnum);
2546 tp = SearchSysCache(STATRELATTINH,
2547 ObjectIdGetDatum(relid),
2548 Int16GetDatum(attnum),
2549 BoolGetDatum(false),
2551 if (HeapTupleIsValid(tp))
2553 stawidth = ((Form_pg_statistic) GETSTRUCT(tp))->stawidth;
2554 ReleaseSysCache(tp);
2564 * Extract the contents of a "slot" of a pg_statistic tuple.
2565 * Returns TRUE if requested slot type was found, else FALSE.
2567 * Unlike other routines in this file, this takes a pointer to an
2568 * already-looked-up tuple in the pg_statistic cache. We do this since
2569 * most callers will want to extract more than one value from the cache
2570 * entry, and we don't want to repeat the cache lookup unnecessarily.
2571 * Also, this API allows this routine to be used with statistics tuples
2572 * that have been provided by a stats hook and didn't really come from
2575 * statstuple: pg_statistics tuple to be examined.
2576 * atttype: type OID of attribute (can be InvalidOid if values == NULL).
2577 * atttypmod: typmod of attribute (can be 0 if values == NULL).
2578 * reqkind: STAKIND code for desired statistics slot kind.
2579 * reqop: STAOP value wanted, or InvalidOid if don't care.
2580 * actualop: if not NULL, *actualop receives the actual STAOP value.
2581 * values, nvalues: if not NULL, the slot's stavalues are extracted.
2582 * numbers, nnumbers: if not NULL, the slot's stanumbers are extracted.
2584 * If assigned, values and numbers are set to point to palloc'd arrays.
2585 * If the attribute type is pass-by-reference, the values referenced by
2586 * the values array are themselves palloc'd. The palloc'd stuff can be
2587 * freed by calling free_attstatsslot.
2590 get_attstatsslot(HeapTuple statstuple,
2591 Oid atttype, int32 atttypmod,
2592 int reqkind, Oid reqop,
2594 Datum **values, int *nvalues,
2595 float4 **numbers, int *nnumbers)
2597 Form_pg_statistic stats = (Form_pg_statistic) GETSTRUCT(statstuple);
2602 ArrayType *statarray;
2604 HeapTuple typeTuple;
2605 Form_pg_type typeForm;
2607 for (i = 0; i < STATISTIC_NUM_SLOTS; i++)
2609 if ((&stats->stakind1)[i] == reqkind &&
2610 (reqop == InvalidOid || (&stats->staop1)[i] == reqop))
2613 if (i >= STATISTIC_NUM_SLOTS)
2614 return false; /* not there */
2617 *actualop = (&stats->staop1)[i];
2621 val = SysCacheGetAttr(STATRELATTINH, statstuple,
2622 Anum_pg_statistic_stavalues1 + i,
2625 elog(ERROR, "stavalues is null");
2626 statarray = DatumGetArrayTypeP(val);
2628 /* Need to get info about the array element type */
2629 typeTuple = SearchSysCache(TYPEOID,
2630 ObjectIdGetDatum(atttype),
2632 if (!HeapTupleIsValid(typeTuple))
2633 elog(ERROR, "cache lookup failed for type %u", atttype);
2634 typeForm = (Form_pg_type) GETSTRUCT(typeTuple);
2636 /* Deconstruct array into Datum elements; NULLs not expected */
2637 deconstruct_array(statarray,
2642 values, NULL, nvalues);
2645 * If the element type is pass-by-reference, we now have a bunch of
2646 * Datums that are pointers into the syscache value. Copy them to
2647 * avoid problems if syscache decides to drop the entry.
2649 if (!typeForm->typbyval)
2651 for (j = 0; j < *nvalues; j++)
2653 (*values)[j] = datumCopy((*values)[j],
2659 ReleaseSysCache(typeTuple);
2662 * Free statarray if it's a detoasted copy.
2664 if ((Pointer) statarray != DatumGetPointer(val))
2670 val = SysCacheGetAttr(STATRELATTINH, statstuple,
2671 Anum_pg_statistic_stanumbers1 + i,
2674 elog(ERROR, "stanumbers is null");
2675 statarray = DatumGetArrayTypeP(val);
2678 * We expect the array to be a 1-D float4 array; verify that. We don't
2679 * need to use deconstruct_array() since the array data is just going
2680 * to look like a C array of float4 values.
2682 narrayelem = ARR_DIMS(statarray)[0];
2683 if (ARR_NDIM(statarray) != 1 || narrayelem <= 0 ||
2684 ARR_HASNULL(statarray) ||
2685 ARR_ELEMTYPE(statarray) != FLOAT4OID)
2686 elog(ERROR, "stanumbers is not a 1-D float4 array");
2687 *numbers = (float4 *) palloc(narrayelem * sizeof(float4));
2688 memcpy(*numbers, ARR_DATA_PTR(statarray), narrayelem * sizeof(float4));
2689 *nnumbers = narrayelem;
2692 * Free statarray if it's a detoasted copy.
2694 if ((Pointer) statarray != DatumGetPointer(val))
2703 * Free data allocated by get_attstatsslot
2705 * atttype need be valid only if values != NULL.
2708 free_attstatsslot(Oid atttype,
2709 Datum *values, int nvalues,
2710 float4 *numbers, int nnumbers)
2714 if (!get_typbyval(atttype))
2718 for (i = 0; i < nvalues; i++)
2719 pfree(DatumGetPointer(values[i]));
2727 /* ---------- PG_NAMESPACE CACHE ---------- */
2730 * get_namespace_name
2731 * Returns the name of a given namespace
2733 * Returns a palloc'd copy of the string, or NULL if no such namespace.
2736 get_namespace_name(Oid nspid)
2740 tp = SearchSysCache(NAMESPACEOID,
2741 ObjectIdGetDatum(nspid),
2743 if (HeapTupleIsValid(tp))
2745 Form_pg_namespace nsptup = (Form_pg_namespace) GETSTRUCT(tp);
2748 result = pstrdup(NameStr(nsptup->nspname));
2749 ReleaseSysCache(tp);
2756 /* ---------- PG_AUTHID CACHE ---------- */
2760 * Given a role name, look up the role's OID.
2761 * Returns InvalidOid if no such role.
2764 get_roleid(const char *rolname)
2766 return GetSysCacheOid(AUTHNAME,
2767 PointerGetDatum(rolname),
2772 * get_roleid_checked
2773 * Given a role name, look up the role's OID.
2774 * ereports if no such role.
2777 get_roleid_checked(const char *rolname)
2781 roleid = get_roleid(rolname);
2782 if (!OidIsValid(roleid))
2784 (errcode(ERRCODE_UNDEFINED_OBJECT),
2785 errmsg("role \"%s\" does not exist", rolname)));