1 /*-------------------------------------------------------------------------
4 * Convenience routines for common queries in the system catalog cache.
6 * Portions Copyright (c) 1996-2003, 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.111 2003/11/29 19:52:00 pgsql Exp $
13 * Eventually, the index information should go through here, too.
14 *-------------------------------------------------------------------------
17 #include "miscadmin.h"
19 #include "access/hash.h"
20 #include "access/tupmacs.h"
21 #include "catalog/pg_amop.h"
22 #include "catalog/pg_amproc.h"
23 #include "catalog/pg_namespace.h"
24 #include "catalog/pg_opclass.h"
25 #include "catalog/pg_operator.h"
26 #include "catalog/pg_proc.h"
27 #include "catalog/pg_shadow.h"
28 #include "catalog/pg_statistic.h"
29 #include "catalog/pg_type.h"
30 #include "nodes/makefuncs.h"
31 #include "utils/array.h"
32 #include "utils/builtins.h"
33 #include "utils/catcache.h"
34 #include "utils/datum.h"
35 #include "utils/lsyscache.h"
36 #include "utils/syscache.h"
39 /* ---------- AMOP CACHES ---------- */
44 * Return t iff operator 'opno' is in operator class 'opclass'.
47 op_in_opclass(Oid opno, Oid opclass)
49 return SearchSysCacheExists(AMOPOPID,
50 ObjectIdGetDatum(opno),
51 ObjectIdGetDatum(opclass),
56 * get_op_opclass_properties
58 * Get the operator's strategy number, subtype, and recheck (lossy) flag
59 * within the specified opclass.
61 * Caller should already have verified that opno is a member of opclass,
62 * therefore we raise an error if the tuple is not found.
65 get_op_opclass_properties(Oid opno, Oid opclass,
66 int *strategy, Oid *subtype, bool *recheck)
69 Form_pg_amop amop_tup;
71 tp = SearchSysCache(AMOPOPID,
72 ObjectIdGetDatum(opno),
73 ObjectIdGetDatum(opclass),
75 if (!HeapTupleIsValid(tp))
76 elog(ERROR, "operator %u is not a member of opclass %u",
78 amop_tup = (Form_pg_amop) GETSTRUCT(tp);
79 *strategy = amop_tup->amopstrategy;
80 *subtype = amop_tup->amopsubtype;
81 *recheck = amop_tup->amopreqcheck;
87 * Get the OID of the operator that implements the specified strategy
88 * with the specified subtype for the specified opclass.
90 * Returns InvalidOid if there is no pg_amop entry for the given keys.
93 get_opclass_member(Oid opclass, Oid subtype, int16 strategy)
96 Form_pg_amop amop_tup;
99 tp = SearchSysCache(AMOPSTRATEGY,
100 ObjectIdGetDatum(opclass),
101 ObjectIdGetDatum(subtype),
102 Int16GetDatum(strategy),
104 if (!HeapTupleIsValid(tp))
106 amop_tup = (Form_pg_amop) GETSTRUCT(tp);
107 result = amop_tup->amopopr;
113 * get_op_hash_function
114 * Get the OID of the datatype-specific hash function associated with
115 * a hashable equality operator.
117 * Returns InvalidOid if no hash function can be found. (This indicates
118 * that the operator should not have been marked oprcanhash.)
121 get_op_hash_function(Oid opno)
125 Oid opclass = InvalidOid;
128 * Search pg_amop to see if the target operator is registered as the
129 * "=" operator of any hash opclass. If the operator is registered in
130 * multiple opclasses, assume we can use the associated hash function
133 catlist = SearchSysCacheList(AMOPOPID, 1,
134 ObjectIdGetDatum(opno),
137 for (i = 0; i < catlist->n_members; i++)
139 HeapTuple tuple = &catlist->members[i]->tuple;
140 Form_pg_amop aform = (Form_pg_amop) GETSTRUCT(tuple);
142 if (aform->amopstrategy == HTEqualStrategyNumber &&
143 opclass_is_hash(aform->amopclaid))
145 opclass = aform->amopclaid;
150 ReleaseSysCacheList(catlist);
152 if (OidIsValid(opclass))
154 /* Found a suitable opclass, get its default hash support function */
155 return get_opclass_proc(opclass, InvalidOid, HASHPROC);
158 /* Didn't find a match... */
163 /* ---------- AMPROC CACHES ---------- */
167 * Get the OID of the specified support function
168 * for the specified opclass and subtype.
170 * Returns InvalidOid if there is no pg_amproc entry for the given keys.
173 get_opclass_proc(Oid opclass, Oid subtype, int16 procnum)
176 Form_pg_amproc amproc_tup;
179 tp = SearchSysCache(AMPROCNUM,
180 ObjectIdGetDatum(opclass),
181 ObjectIdGetDatum(subtype),
182 Int16GetDatum(procnum),
184 if (!HeapTupleIsValid(tp))
186 amproc_tup = (Form_pg_amproc) GETSTRUCT(tp);
187 result = amproc_tup->amproc;
193 /* ---------- ATTRIBUTE CACHES ---------- */
197 * Given the relation id and the attribute number,
198 * return the "attname" field from the attribute relation.
200 * Note: returns a palloc'd copy of the string, or NULL if no such attribute.
203 get_attname(Oid relid, AttrNumber attnum)
207 tp = SearchSysCache(ATTNUM,
208 ObjectIdGetDatum(relid),
209 Int16GetDatum(attnum),
211 if (HeapTupleIsValid(tp))
213 Form_pg_attribute att_tup = (Form_pg_attribute) GETSTRUCT(tp);
216 result = pstrdup(NameStr(att_tup->attname));
225 * get_relid_attribute_name
227 * Same as above routine get_attname(), except that error
228 * is handled by elog() instead of returning NULL.
231 get_relid_attribute_name(Oid relid, AttrNumber attnum)
235 attname = get_attname(relid, attnum);
237 elog(ERROR, "cache lookup failed for attribute %d of relation %u",
245 * Given the relation id and the attribute name,
246 * return the "attnum" field from the attribute relation.
248 * Returns InvalidAttrNumber if the attr doesn't exist (or is dropped).
251 get_attnum(Oid relid, const char *attname)
255 tp = SearchSysCacheAttName(relid, attname);
256 if (HeapTupleIsValid(tp))
258 Form_pg_attribute att_tup = (Form_pg_attribute) GETSTRUCT(tp);
261 result = att_tup->attnum;
266 return InvalidAttrNumber;
272 * Given the relation OID and the attribute number with the relation,
273 * return the attribute type OID.
276 get_atttype(Oid relid, AttrNumber attnum)
280 tp = SearchSysCache(ATTNUM,
281 ObjectIdGetDatum(relid),
282 Int16GetDatum(attnum),
284 if (HeapTupleIsValid(tp))
286 Form_pg_attribute att_tup = (Form_pg_attribute) GETSTRUCT(tp);
289 result = att_tup->atttypid;
300 * Given the relation id and the attribute number,
301 * return the "atttypmod" field from the attribute relation.
304 get_atttypmod(Oid relid, AttrNumber attnum)
308 tp = SearchSysCache(ATTNUM,
309 ObjectIdGetDatum(relid),
310 Int16GetDatum(attnum),
312 if (HeapTupleIsValid(tp))
314 Form_pg_attribute att_tup = (Form_pg_attribute) GETSTRUCT(tp);
317 result = att_tup->atttypmod;
328 * A two-fer: given the relation id and the attribute number,
329 * fetch both type OID and atttypmod in a single cache lookup.
331 * Unlike the otherwise-similar get_atttype/get_atttypmod, this routine
332 * raises an error if it can't obtain the information.
335 get_atttypetypmod(Oid relid, AttrNumber attnum,
336 Oid *typid, int32 *typmod)
339 Form_pg_attribute att_tup;
341 tp = SearchSysCache(ATTNUM,
342 ObjectIdGetDatum(relid),
343 Int16GetDatum(attnum),
345 if (!HeapTupleIsValid(tp))
346 elog(ERROR, "cache lookup failed for attribute %d of relation %u",
348 att_tup = (Form_pg_attribute) GETSTRUCT(tp);
350 *typid = att_tup->atttypid;
351 *typmod = att_tup->atttypmod;
355 /* ---------- INDEX CACHE ---------- */
357 /* watch this space...
360 /* ---------- OPCLASS CACHE ---------- */
365 * Returns TRUE iff the specified opclass is associated with the
366 * btree index access method.
369 opclass_is_btree(Oid opclass)
372 Form_pg_opclass cla_tup;
375 tp = SearchSysCache(CLAOID,
376 ObjectIdGetDatum(opclass),
378 if (!HeapTupleIsValid(tp))
379 elog(ERROR, "cache lookup failed for opclass %u", opclass);
380 cla_tup = (Form_pg_opclass) GETSTRUCT(tp);
382 result = (cla_tup->opcamid == BTREE_AM_OID);
390 * Returns TRUE iff the specified opclass is associated with the
391 * hash index access method.
394 opclass_is_hash(Oid opclass)
397 Form_pg_opclass cla_tup;
400 tp = SearchSysCache(CLAOID,
401 ObjectIdGetDatum(opclass),
403 if (!HeapTupleIsValid(tp))
404 elog(ERROR, "cache lookup failed for opclass %u", opclass);
405 cla_tup = (Form_pg_opclass) GETSTRUCT(tp);
407 result = (cla_tup->opcamid == HASH_AM_OID);
412 /* ---------- OPERATOR CACHE ---------- */
417 * Returns the regproc id of the routine used to implement an
418 * operator given the operator oid.
425 tp = SearchSysCache(OPEROID,
426 ObjectIdGetDatum(opno),
428 if (HeapTupleIsValid(tp))
430 Form_pg_operator optup = (Form_pg_operator) GETSTRUCT(tp);
433 result = optup->oprcode;
438 return (RegProcedure) InvalidOid;
443 * returns the name of the operator with the given opno
445 * Note: returns a palloc'd copy of the string, or NULL if no such operator.
452 tp = SearchSysCache(OPEROID,
453 ObjectIdGetDatum(opno),
455 if (HeapTupleIsValid(tp))
457 Form_pg_operator optup = (Form_pg_operator) GETSTRUCT(tp);
460 result = pstrdup(NameStr(optup->oprname));
471 * Returns the left and right sort operators corresponding to a
472 * mergejoinable operator, or false if the operator is not mergejoinable.
475 op_mergejoinable(Oid opno, Oid *leftOp, Oid *rightOp)
480 tp = SearchSysCache(OPEROID,
481 ObjectIdGetDatum(opno),
483 if (HeapTupleIsValid(tp))
485 Form_pg_operator optup = (Form_pg_operator) GETSTRUCT(tp);
487 if (optup->oprlsortop &&
490 *leftOp = optup->oprlsortop;
491 *rightOp = optup->oprrsortop;
500 * op_mergejoin_crossops
502 * Returns the cross-type comparison operators (ltype "<" rtype and
503 * ltype ">" rtype) for an operator previously determined to be
504 * mergejoinable. Optionally, fetches the regproc ids of these
505 * operators, as well as their operator OIDs.
508 op_mergejoin_crossops(Oid opno, Oid *ltop, Oid *gtop,
509 RegProcedure *ltproc, RegProcedure *gtproc)
512 Form_pg_operator optup;
515 * Get the declared comparison operators of the operator.
517 tp = SearchSysCache(OPEROID,
518 ObjectIdGetDatum(opno),
520 if (!HeapTupleIsValid(tp)) /* shouldn't happen */
521 elog(ERROR, "cache lookup failed for operator %u", opno);
522 optup = (Form_pg_operator) GETSTRUCT(tp);
523 *ltop = optup->oprltcmpop;
524 *gtop = optup->oprgtcmpop;
527 /* Check < op provided */
528 if (!OidIsValid(*ltop))
529 elog(ERROR, "mergejoin operator %u has no matching < operator",
532 *ltproc = get_opcode(*ltop);
534 /* Check > op provided */
535 if (!OidIsValid(*gtop))
536 elog(ERROR, "mergejoin operator %u has no matching > operator",
539 *gtproc = get_opcode(*gtop);
545 * Returns true if the operator is hashjoinable.
548 op_hashjoinable(Oid opno)
553 tp = SearchSysCache(OPEROID,
554 ObjectIdGetDatum(opno),
556 if (HeapTupleIsValid(tp))
558 Form_pg_operator optup = (Form_pg_operator) GETSTRUCT(tp);
560 result = optup->oprcanhash;
569 * Get the proisstrict flag for the operator's underlying function.
574 RegProcedure funcid = get_opcode(opno);
576 if (funcid == (RegProcedure) InvalidOid)
577 elog(ERROR, "operator %u does not exist", opno);
579 return func_strict((Oid) funcid);
585 * Get the provolatile flag for the operator's underlying function.
588 op_volatile(Oid opno)
590 RegProcedure funcid = get_opcode(opno);
592 if (funcid == (RegProcedure) InvalidOid)
593 elog(ERROR, "operator %u does not exist", opno);
595 return func_volatile((Oid) funcid);
601 * Returns the corresponding commutator of an operator.
604 get_commutator(Oid opno)
608 tp = SearchSysCache(OPEROID,
609 ObjectIdGetDatum(opno),
611 if (HeapTupleIsValid(tp))
613 Form_pg_operator optup = (Form_pg_operator) GETSTRUCT(tp);
616 result = optup->oprcom;
627 * Returns the corresponding negator of an operator.
630 get_negator(Oid opno)
634 tp = SearchSysCache(OPEROID,
635 ObjectIdGetDatum(opno),
637 if (HeapTupleIsValid(tp))
639 Form_pg_operator optup = (Form_pg_operator) GETSTRUCT(tp);
642 result = optup->oprnegate;
653 * Returns procedure id for computing selectivity of an operator.
656 get_oprrest(Oid opno)
660 tp = SearchSysCache(OPEROID,
661 ObjectIdGetDatum(opno),
663 if (HeapTupleIsValid(tp))
665 Form_pg_operator optup = (Form_pg_operator) GETSTRUCT(tp);
668 result = optup->oprrest;
673 return (RegProcedure) InvalidOid;
679 * Returns procedure id for computing selectivity of a join.
682 get_oprjoin(Oid opno)
686 tp = SearchSysCache(OPEROID,
687 ObjectIdGetDatum(opno),
689 if (HeapTupleIsValid(tp))
691 Form_pg_operator optup = (Form_pg_operator) GETSTRUCT(tp);
694 result = optup->oprjoin;
699 return (RegProcedure) InvalidOid;
702 /* ---------- FUNCTION CACHE ---------- */
706 * returns the name of the function with the given funcid
708 * Note: returns a palloc'd copy of the string, or NULL if no such function.
711 get_func_name(Oid funcid)
715 tp = SearchSysCache(PROCOID,
716 ObjectIdGetDatum(funcid),
718 if (HeapTupleIsValid(tp))
720 Form_pg_proc functup = (Form_pg_proc) GETSTRUCT(tp);
723 result = pstrdup(NameStr(functup->proname));
733 * Given procedure id, return the function's result type.
736 get_func_rettype(Oid funcid)
741 tp = SearchSysCache(PROCOID,
742 ObjectIdGetDatum(funcid),
744 if (!HeapTupleIsValid(tp))
745 elog(ERROR, "cache lookup failed for function %u", funcid);
747 result = ((Form_pg_proc) GETSTRUCT(tp))->prorettype;
754 * Given procedure id, return the function's argument and result types.
755 * (The return value is the result type.)
757 * argtypes must point to a vector of size FUNC_MAX_ARGS.
760 get_func_signature(Oid funcid, Oid *argtypes, int *nargs)
763 Form_pg_proc procstruct;
766 tp = SearchSysCache(PROCOID,
767 ObjectIdGetDatum(funcid),
769 if (!HeapTupleIsValid(tp))
770 elog(ERROR, "cache lookup failed for function %u", funcid);
772 procstruct = (Form_pg_proc) GETSTRUCT(tp);
774 result = procstruct->prorettype;
775 memcpy(argtypes, procstruct->proargtypes, FUNC_MAX_ARGS * sizeof(Oid));
776 *nargs = (int) procstruct->pronargs;
784 * Given procedure id, return the function's proretset flag.
787 get_func_retset(Oid funcid)
792 tp = SearchSysCache(PROCOID,
793 ObjectIdGetDatum(funcid),
795 if (!HeapTupleIsValid(tp))
796 elog(ERROR, "cache lookup failed for function %u", funcid);
798 result = ((Form_pg_proc) GETSTRUCT(tp))->proretset;
805 * Given procedure id, return the function's proisstrict flag.
808 func_strict(Oid funcid)
813 tp = SearchSysCache(PROCOID,
814 ObjectIdGetDatum(funcid),
816 if (!HeapTupleIsValid(tp))
817 elog(ERROR, "cache lookup failed for function %u", funcid);
819 result = ((Form_pg_proc) GETSTRUCT(tp))->proisstrict;
826 * Given procedure id, return the function's provolatile flag.
829 func_volatile(Oid funcid)
834 tp = SearchSysCache(PROCOID,
835 ObjectIdGetDatum(funcid),
837 if (!HeapTupleIsValid(tp))
838 elog(ERROR, "cache lookup failed for function %u", funcid);
840 result = ((Form_pg_proc) GETSTRUCT(tp))->provolatile;
845 /* ---------- RELATION CACHE ---------- */
849 * Given name and namespace of a relation, look up the OID.
851 * Returns InvalidOid if there is no such relation.
854 get_relname_relid(const char *relname, Oid relnamespace)
856 return GetSysCacheOid(RELNAMENSP,
857 PointerGetDatum(relname),
858 ObjectIdGetDatum(relnamespace),
863 * get_system_catalog_relid
864 * Get the OID of a system catalog identified by name.
867 get_system_catalog_relid(const char *catname)
871 relid = GetSysCacheOid(RELNAMENSP,
872 PointerGetDatum(catname),
873 ObjectIdGetDatum(PG_CATALOG_NAMESPACE),
875 if (!OidIsValid(relid))
876 elog(ERROR, "cache lookup failed for system relation %s", catname);
885 * Returns the number of attributes for a given relation.
888 get_relnatts(Oid relid)
892 tp = SearchSysCache(RELOID,
893 ObjectIdGetDatum(relid),
895 if (HeapTupleIsValid(tp))
897 Form_pg_class reltup = (Form_pg_class) GETSTRUCT(tp);
900 result = reltup->relnatts;
905 return InvalidAttrNumber;
911 * Returns the name of a given relation.
913 * Returns a palloc'd copy of the string, or NULL if no such relation.
915 * NOTE: since relation name is not unique, be wary of code that uses this
916 * for anything except preparing error messages.
919 get_rel_name(Oid relid)
923 tp = SearchSysCache(RELOID,
924 ObjectIdGetDatum(relid),
926 if (HeapTupleIsValid(tp))
928 Form_pg_class reltup = (Form_pg_class) GETSTRUCT(tp);
931 result = pstrdup(NameStr(reltup->relname));
942 * Returns the pg_namespace OID associated with a given relation.
945 get_rel_namespace(Oid relid)
949 tp = SearchSysCache(RELOID,
950 ObjectIdGetDatum(relid),
952 if (HeapTupleIsValid(tp))
954 Form_pg_class reltup = (Form_pg_class) GETSTRUCT(tp);
957 result = reltup->relnamespace;
968 * Returns the pg_type OID associated with a given relation.
970 * Note: not all pg_class entries have associated pg_type OIDs; so be
971 * careful to check for InvalidOid result.
974 get_rel_type_id(Oid relid)
978 tp = SearchSysCache(RELOID,
979 ObjectIdGetDatum(relid),
981 if (HeapTupleIsValid(tp))
983 Form_pg_class reltup = (Form_pg_class) GETSTRUCT(tp);
986 result = reltup->reltype;
997 * Returns the relkind associated with a given relation.
1000 get_rel_relkind(Oid relid)
1004 tp = SearchSysCache(RELOID,
1005 ObjectIdGetDatum(relid),
1007 if (HeapTupleIsValid(tp))
1009 Form_pg_class reltup = (Form_pg_class) GETSTRUCT(tp);
1012 result = reltup->relkind;
1013 ReleaseSysCache(tp);
1021 /* ---------- TYPE CACHE ---------- */
1026 * Given the type OID, determine whether the type is defined
1027 * (if not, it's only a shell).
1030 get_typisdefined(Oid typid)
1034 tp = SearchSysCache(TYPEOID,
1035 ObjectIdGetDatum(typid),
1037 if (HeapTupleIsValid(tp))
1039 Form_pg_type typtup = (Form_pg_type) GETSTRUCT(tp);
1042 result = typtup->typisdefined;
1043 ReleaseSysCache(tp);
1053 * Given the type OID, return the length of the type.
1056 get_typlen(Oid typid)
1060 tp = SearchSysCache(TYPEOID,
1061 ObjectIdGetDatum(typid),
1063 if (HeapTupleIsValid(tp))
1065 Form_pg_type typtup = (Form_pg_type) GETSTRUCT(tp);
1068 result = typtup->typlen;
1069 ReleaseSysCache(tp);
1079 * Given the type OID, determine whether the type is returned by value or
1080 * not. Returns true if by value, false if by reference.
1083 get_typbyval(Oid typid)
1087 tp = SearchSysCache(TYPEOID,
1088 ObjectIdGetDatum(typid),
1090 if (HeapTupleIsValid(tp))
1092 Form_pg_type typtup = (Form_pg_type) GETSTRUCT(tp);
1095 result = typtup->typbyval;
1096 ReleaseSysCache(tp);
1106 * A two-fer: given the type OID, return both typlen and typbyval.
1108 * Since both pieces of info are needed to know how to copy a Datum,
1109 * many places need both. Might as well get them with one cache lookup
1110 * instead of two. Also, this routine raises an error instead of
1111 * returning a bogus value when given a bad type OID.
1114 get_typlenbyval(Oid typid, int16 *typlen, bool *typbyval)
1117 Form_pg_type typtup;
1119 tp = SearchSysCache(TYPEOID,
1120 ObjectIdGetDatum(typid),
1122 if (!HeapTupleIsValid(tp))
1123 elog(ERROR, "cache lookup failed for type %u", typid);
1124 typtup = (Form_pg_type) GETSTRUCT(tp);
1125 *typlen = typtup->typlen;
1126 *typbyval = typtup->typbyval;
1127 ReleaseSysCache(tp);
1131 * get_typlenbyvalalign
1133 * A three-fer: given the type OID, return typlen, typbyval, typalign.
1136 get_typlenbyvalalign(Oid typid, int16 *typlen, bool *typbyval,
1140 Form_pg_type typtup;
1142 tp = SearchSysCache(TYPEOID,
1143 ObjectIdGetDatum(typid),
1145 if (!HeapTupleIsValid(tp))
1146 elog(ERROR, "cache lookup failed for type %u", typid);
1147 typtup = (Form_pg_type) GETSTRUCT(tp);
1148 *typlen = typtup->typlen;
1149 *typbyval = typtup->typbyval;
1150 *typalign = typtup->typalign;
1151 ReleaseSysCache(tp);
1157 * A six-fer: given the type OID, return typlen, typbyval, typalign,
1158 * typdelim, typelem, and IO function OID. The IO function
1159 * returned is controlled by IOFuncSelector
1162 get_type_io_data(Oid typid,
1163 IOFuncSelector which_func,
1171 HeapTuple typeTuple;
1172 Form_pg_type typeStruct;
1174 typeTuple = SearchSysCache(TYPEOID,
1175 ObjectIdGetDatum(typid),
1177 if (!HeapTupleIsValid(typeTuple))
1178 elog(ERROR, "cache lookup failed for type %u", typid);
1179 typeStruct = (Form_pg_type) GETSTRUCT(typeTuple);
1181 *typlen = typeStruct->typlen;
1182 *typbyval = typeStruct->typbyval;
1183 *typalign = typeStruct->typalign;
1184 *typdelim = typeStruct->typdelim;
1185 *typelem = typeStruct->typelem;
1189 *func = typeStruct->typinput;
1192 *func = typeStruct->typoutput;
1194 case IOFunc_receive:
1195 *func = typeStruct->typreceive;
1198 *func = typeStruct->typsend;
1201 ReleaseSysCache(typeTuple);
1206 get_typalign(Oid typid)
1210 tp = SearchSysCache(TYPEOID,
1211 ObjectIdGetDatum(typid),
1213 if (HeapTupleIsValid(tp))
1215 Form_pg_type typtup = (Form_pg_type) GETSTRUCT(tp);
1218 result = typtup->typalign;
1219 ReleaseSysCache(tp);
1228 get_typstorage(Oid typid)
1232 tp = SearchSysCache(TYPEOID,
1233 ObjectIdGetDatum(typid),
1235 if (HeapTupleIsValid(tp))
1237 Form_pg_type typtup = (Form_pg_type) GETSTRUCT(tp);
1240 result = typtup->typstorage;
1241 ReleaseSysCache(tp);
1251 * Given the type OID, return the typtypmod field (domain's typmod
1255 get_typtypmod(Oid typid)
1259 tp = SearchSysCache(TYPEOID,
1260 ObjectIdGetDatum(typid),
1262 if (HeapTupleIsValid(tp))
1264 Form_pg_type typtup = (Form_pg_type) GETSTRUCT(tp);
1267 result = typtup->typtypmod;
1268 ReleaseSysCache(tp);
1277 * Given a type OID, return the type's default value, if any.
1279 * The result is a palloc'd expression node tree, or NULL if there
1280 * is no defined default for the datatype.
1282 * NB: caller should be prepared to coerce result to correct datatype;
1283 * the returned expression tree might produce something of the wrong type.
1286 get_typdefault(Oid typid)
1288 HeapTuple typeTuple;
1294 typeTuple = SearchSysCache(TYPEOID,
1295 ObjectIdGetDatum(typid),
1297 if (!HeapTupleIsValid(typeTuple))
1298 elog(ERROR, "cache lookup failed for type %u", typid);
1299 type = (Form_pg_type) GETSTRUCT(typeTuple);
1302 * typdefault and typdefaultbin are potentially null, so don't try to
1303 * access 'em as struct fields. Must do it the hard way with
1306 datum = SysCacheGetAttr(TYPEOID,
1308 Anum_pg_type_typdefaultbin,
1313 /* We have an expression default */
1314 expr = stringToNode(DatumGetCString(DirectFunctionCall1(textout,
1319 /* Perhaps we have a plain literal default */
1320 datum = SysCacheGetAttr(TYPEOID,
1322 Anum_pg_type_typdefault,
1327 char *strDefaultVal;
1329 /* Convert text datum to C string */
1330 strDefaultVal = DatumGetCString(DirectFunctionCall1(textout,
1332 /* Convert C string to a value of the given type */
1333 datum = OidFunctionCall3(type->typinput,
1334 CStringGetDatum(strDefaultVal),
1335 ObjectIdGetDatum(type->typelem),
1337 /* Build a Const node containing the value */
1338 expr = (Node *) makeConst(typid,
1343 pfree(strDefaultVal);
1352 ReleaseSysCache(typeTuple);
1359 * If the given type is a domain, return its base type;
1360 * otherwise return the type's own OID.
1363 getBaseType(Oid typid)
1366 * We loop to find the bottom base type in a stack of domains.
1371 Form_pg_type typTup;
1373 tup = SearchSysCache(TYPEOID,
1374 ObjectIdGetDatum(typid),
1376 if (!HeapTupleIsValid(tup))
1377 elog(ERROR, "cache lookup failed for type %u", typid);
1378 typTup = (Form_pg_type) GETSTRUCT(tup);
1379 if (typTup->typtype != 'd')
1381 /* Not a domain, so done */
1382 ReleaseSysCache(tup);
1386 typid = typTup->typbasetype;
1387 ReleaseSysCache(tup);
1396 * Given a type OID and a typmod value (pass -1 if typmod is unknown),
1397 * estimate the average width of values of the type. This is used by
1398 * the planner, which doesn't require absolutely correct results;
1399 * it's OK (and expected) to guess if we don't know for sure.
1402 get_typavgwidth(Oid typid, int32 typmod)
1404 int typlen = get_typlen(typid);
1408 * Easy if it's a fixed-width type
1414 * type_maximum_size knows the encoding of typmod for some datatypes;
1415 * don't duplicate that knowledge here.
1417 maxwidth = type_maximum_size(typid, typmod);
1421 * For BPCHAR, the max width is also the only width. Otherwise we
1422 * need to guess about the typical data width given the max. A
1423 * sliding scale for percentage of max width seems reasonable.
1425 if (typid == BPCHAROID)
1428 return maxwidth; /* assume full width */
1429 if (maxwidth < 1000)
1430 return 32 + (maxwidth - 32) / 2; /* assume 50% */
1433 * Beyond 1000, assume we're looking at something like
1434 * "varchar(10000)" where the limit isn't actually reached often,
1435 * and use a fixed estimate.
1437 return 32 + (1000 - 32) / 2;
1441 * Ooops, we have no idea ... wild guess time.
1449 * Given the type OID, find if it is a basic type, a complex type, etc.
1450 * It returns the null char if the cache lookup fails...
1453 get_typtype(Oid typid)
1457 tp = SearchSysCache(TYPEOID,
1458 ObjectIdGetDatum(typid),
1460 if (HeapTupleIsValid(tp))
1462 Form_pg_type typtup = (Form_pg_type) GETSTRUCT(tp);
1465 result = typtup->typtype;
1466 ReleaseSysCache(tp);
1476 * Given the type OID, get the typrelid (InvalidOid if not a complex
1480 get_typ_typrelid(Oid typid)
1484 tp = SearchSysCache(TYPEOID,
1485 ObjectIdGetDatum(typid),
1487 if (HeapTupleIsValid(tp))
1489 Form_pg_type typtup = (Form_pg_type) GETSTRUCT(tp);
1492 result = typtup->typrelid;
1493 ReleaseSysCache(tp);
1503 * Given the type OID, get the typelem (InvalidOid if not an array type).
1505 * NB: this only considers varlena arrays to be true arrays; InvalidOid is
1506 * returned if the input is a fixed-length array type.
1509 get_element_type(Oid typid)
1513 tp = SearchSysCache(TYPEOID,
1514 ObjectIdGetDatum(typid),
1516 if (HeapTupleIsValid(tp))
1518 Form_pg_type typtup = (Form_pg_type) GETSTRUCT(tp);
1521 if (typtup->typlen == -1)
1522 result = typtup->typelem;
1524 result = InvalidOid;
1525 ReleaseSysCache(tp);
1535 * Given the type OID, get the corresponding array type.
1536 * Returns InvalidOid if no array type can be found.
1538 * NB: this only considers varlena arrays to be true arrays.
1541 get_array_type(Oid typid)
1545 tp = SearchSysCache(TYPEOID,
1546 ObjectIdGetDatum(typid),
1548 if (HeapTupleIsValid(tp))
1550 Form_pg_type typtup = (Form_pg_type) GETSTRUCT(tp);
1551 char *array_typename;
1554 array_typename = makeArrayTypeName(NameStr(typtup->typname));
1555 namespaceId = typtup->typnamespace;
1556 ReleaseSysCache(tp);
1558 tp = SearchSysCache(TYPENAMENSP,
1559 PointerGetDatum(array_typename),
1560 ObjectIdGetDatum(namespaceId),
1563 pfree(array_typename);
1565 if (HeapTupleIsValid(tp))
1569 typtup = (Form_pg_type) GETSTRUCT(tp);
1570 if (typtup->typlen == -1 && typtup->typelem == typid)
1571 result = HeapTupleGetOid(tp);
1573 result = InvalidOid;
1574 ReleaseSysCache(tp);
1584 * Get info needed for converting values of a type to internal form
1587 getTypeInputInfo(Oid type, Oid *typInput, Oid *typElem)
1589 HeapTuple typeTuple;
1592 typeTuple = SearchSysCache(TYPEOID,
1593 ObjectIdGetDatum(type),
1595 if (!HeapTupleIsValid(typeTuple))
1596 elog(ERROR, "cache lookup failed for type %u", type);
1597 pt = (Form_pg_type) GETSTRUCT(typeTuple);
1599 if (!pt->typisdefined)
1601 (errcode(ERRCODE_UNDEFINED_OBJECT),
1602 errmsg("type %s is only a shell",
1603 format_type_be(type))));
1604 if (!OidIsValid(pt->typinput))
1606 (errcode(ERRCODE_UNDEFINED_FUNCTION),
1607 errmsg("no input function available for type %s",
1608 format_type_be(type))));
1610 *typInput = pt->typinput;
1611 *typElem = pt->typelem;
1613 ReleaseSysCache(typeTuple);
1619 * Get info needed for printing values of a type
1622 getTypeOutputInfo(Oid type, Oid *typOutput, Oid *typElem,
1625 HeapTuple typeTuple;
1628 typeTuple = SearchSysCache(TYPEOID,
1629 ObjectIdGetDatum(type),
1631 if (!HeapTupleIsValid(typeTuple))
1632 elog(ERROR, "cache lookup failed for type %u", type);
1633 pt = (Form_pg_type) GETSTRUCT(typeTuple);
1635 if (!pt->typisdefined)
1637 (errcode(ERRCODE_UNDEFINED_OBJECT),
1638 errmsg("type %s is only a shell",
1639 format_type_be(type))));
1640 if (!OidIsValid(pt->typoutput))
1642 (errcode(ERRCODE_UNDEFINED_FUNCTION),
1643 errmsg("no output function available for type %s",
1644 format_type_be(type))));
1646 *typOutput = pt->typoutput;
1647 *typElem = pt->typelem;
1648 *typIsVarlena = (!pt->typbyval) && (pt->typlen == -1);
1650 ReleaseSysCache(typeTuple);
1654 * getTypeBinaryInputInfo
1656 * Get info needed for binary input of values of a type
1659 getTypeBinaryInputInfo(Oid type, Oid *typReceive, Oid *typElem)
1661 HeapTuple typeTuple;
1664 typeTuple = SearchSysCache(TYPEOID,
1665 ObjectIdGetDatum(type),
1667 if (!HeapTupleIsValid(typeTuple))
1668 elog(ERROR, "cache lookup failed for type %u", type);
1669 pt = (Form_pg_type) GETSTRUCT(typeTuple);
1671 if (!pt->typisdefined)
1673 (errcode(ERRCODE_UNDEFINED_OBJECT),
1674 errmsg("type %s is only a shell",
1675 format_type_be(type))));
1676 if (!OidIsValid(pt->typreceive))
1678 (errcode(ERRCODE_UNDEFINED_FUNCTION),
1679 errmsg("no binary input function available for type %s",
1680 format_type_be(type))));
1682 *typReceive = pt->typreceive;
1683 *typElem = pt->typelem;
1685 ReleaseSysCache(typeTuple);
1689 * getTypeBinaryOutputInfo
1691 * Get info needed for binary output of values of a type
1694 getTypeBinaryOutputInfo(Oid type, Oid *typSend, Oid *typElem,
1697 HeapTuple typeTuple;
1700 typeTuple = SearchSysCache(TYPEOID,
1701 ObjectIdGetDatum(type),
1703 if (!HeapTupleIsValid(typeTuple))
1704 elog(ERROR, "cache lookup failed for type %u", type);
1705 pt = (Form_pg_type) GETSTRUCT(typeTuple);
1707 if (!pt->typisdefined)
1709 (errcode(ERRCODE_UNDEFINED_OBJECT),
1710 errmsg("type %s is only a shell",
1711 format_type_be(type))));
1712 if (!OidIsValid(pt->typsend))
1714 (errcode(ERRCODE_UNDEFINED_FUNCTION),
1715 errmsg("no binary output function available for type %s",
1716 format_type_be(type))));
1718 *typSend = pt->typsend;
1719 *typElem = pt->typelem;
1720 *typIsVarlena = (!pt->typbyval) && (pt->typlen == -1);
1722 ReleaseSysCache(typeTuple);
1726 /* ---------- STATISTICS CACHE ---------- */
1731 * Given the table and attribute number of a column, get the average
1732 * width of entries in the column. Return zero if no data available.
1735 get_attavgwidth(Oid relid, AttrNumber attnum)
1739 tp = SearchSysCache(STATRELATT,
1740 ObjectIdGetDatum(relid),
1741 Int16GetDatum(attnum),
1743 if (HeapTupleIsValid(tp))
1745 int32 stawidth = ((Form_pg_statistic) GETSTRUCT(tp))->stawidth;
1747 ReleaseSysCache(tp);
1757 * Extract the contents of a "slot" of a pg_statistic tuple.
1758 * Returns TRUE if requested slot type was found, else FALSE.
1760 * Unlike other routines in this file, this takes a pointer to an
1761 * already-looked-up tuple in the pg_statistic cache. We do this since
1762 * most callers will want to extract more than one value from the cache
1763 * entry, and we don't want to repeat the cache lookup unnecessarily.
1765 * statstuple: pg_statistics tuple to be examined.
1766 * atttype: type OID of attribute.
1767 * atttypmod: typmod of attribute.
1768 * reqkind: STAKIND code for desired statistics slot kind.
1769 * reqop: STAOP value wanted, or InvalidOid if don't care.
1770 * values, nvalues: if not NULL, the slot's stavalues are extracted.
1771 * numbers, nnumbers: if not NULL, the slot's stanumbers are extracted.
1773 * If assigned, values and numbers are set to point to palloc'd arrays.
1774 * If the attribute type is pass-by-reference, the values referenced by
1775 * the values array are themselves palloc'd. The palloc'd stuff can be
1776 * freed by calling free_attstatsslot.
1779 get_attstatsslot(HeapTuple statstuple,
1780 Oid atttype, int32 atttypmod,
1781 int reqkind, Oid reqop,
1782 Datum **values, int *nvalues,
1783 float4 **numbers, int *nnumbers)
1785 Form_pg_statistic stats = (Form_pg_statistic) GETSTRUCT(statstuple);
1790 ArrayType *statarray;
1792 HeapTuple typeTuple;
1793 Form_pg_type typeForm;
1795 for (i = 0; i < STATISTIC_NUM_SLOTS; i++)
1797 if ((&stats->stakind1)[i] == reqkind &&
1798 (reqop == InvalidOid || (&stats->staop1)[i] == reqop))
1801 if (i >= STATISTIC_NUM_SLOTS)
1802 return false; /* not there */
1806 val = SysCacheGetAttr(STATRELATT, statstuple,
1807 Anum_pg_statistic_stavalues1 + i,
1810 elog(ERROR, "stavalues is null");
1811 statarray = DatumGetArrayTypeP(val);
1813 /* Need to get info about the array element type */
1814 typeTuple = SearchSysCache(TYPEOID,
1815 ObjectIdGetDatum(atttype),
1817 if (!HeapTupleIsValid(typeTuple))
1818 elog(ERROR, "cache lookup failed for type %u", atttype);
1819 typeForm = (Form_pg_type) GETSTRUCT(typeTuple);
1821 /* Deconstruct array into Datum elements */
1822 deconstruct_array(statarray,
1830 * If the element type is pass-by-reference, we now have a bunch
1831 * of Datums that are pointers into the syscache value. Copy them
1832 * to avoid problems if syscache decides to drop the entry.
1834 if (!typeForm->typbyval)
1836 for (j = 0; j < *nvalues; j++)
1838 (*values)[j] = datumCopy((*values)[j],
1844 ReleaseSysCache(typeTuple);
1847 * Free statarray if it's a detoasted copy.
1849 if ((Pointer) statarray != DatumGetPointer(val))
1855 val = SysCacheGetAttr(STATRELATT, statstuple,
1856 Anum_pg_statistic_stanumbers1 + i,
1859 elog(ERROR, "stanumbers is null");
1860 statarray = DatumGetArrayTypeP(val);
1863 * We expect the array to be a 1-D float4 array; verify that. We
1864 * don't need to use deconstruct_array() since the array data is
1865 * just going to look like a C array of float4 values.
1867 narrayelem = ARR_DIMS(statarray)[0];
1868 if (ARR_NDIM(statarray) != 1 || narrayelem <= 0 ||
1869 ARR_ELEMTYPE(statarray) != FLOAT4OID)
1870 elog(ERROR, "stanumbers is not a 1-D float4 array");
1871 *numbers = (float4 *) palloc(narrayelem * sizeof(float4));
1872 memcpy(*numbers, ARR_DATA_PTR(statarray), narrayelem * sizeof(float4));
1873 *nnumbers = narrayelem;
1876 * Free statarray if it's a detoasted copy.
1878 if ((Pointer) statarray != DatumGetPointer(val))
1886 free_attstatsslot(Oid atttype,
1887 Datum *values, int nvalues,
1888 float4 *numbers, int nnumbers)
1892 if (!get_typbyval(atttype))
1896 for (i = 0; i < nvalues; i++)
1897 pfree(DatumGetPointer(values[i]));
1905 /* ---------- PG_NAMESPACE CACHE ---------- */
1908 * get_namespace_name
1909 * Returns the name of a given namespace
1911 * Returns a palloc'd copy of the string, or NULL if no such namespace.
1914 get_namespace_name(Oid nspid)
1918 tp = SearchSysCache(NAMESPACEOID,
1919 ObjectIdGetDatum(nspid),
1921 if (HeapTupleIsValid(tp))
1923 Form_pg_namespace nsptup = (Form_pg_namespace) GETSTRUCT(tp);
1926 result = pstrdup(NameStr(nsptup->nspname));
1927 ReleaseSysCache(tp);
1934 /* ---------- PG_SHADOW CACHE ---------- */
1939 * Given a user name, look up the user's sysid.
1940 * Raises an error if no such user (rather than returning zero,
1941 * which might possibly be a valid usesysid).
1943 * Note: the type of usesysid is currently int4, but may change to Oid
1944 * someday. It'd be reasonable to return zero on failure if we were
1948 get_usesysid(const char *username)
1953 userTup = SearchSysCache(SHADOWNAME,
1954 PointerGetDatum(username),
1956 if (!HeapTupleIsValid(userTup))
1958 (errcode(ERRCODE_UNDEFINED_OBJECT),
1959 errmsg("user \"%s\" does not exist", username)));
1961 result = ((Form_pg_shadow) GETSTRUCT(userTup))->usesysid;
1963 ReleaseSysCache(userTup);