1 /*-------------------------------------------------------------------------
4 * Routines to access information within system caches
6 * Copyright (c) 1994, Regents of the University of California
10 * $Header: /cvsroot/pgsql/src/backend/utils/cache/lsyscache.c,v 1.14 1998/06/15 19:29:40 momjian Exp $
13 * Eventually, the index information should go through here, too.
15 * Most of these routines call SearchSysCacheStruct() and thus simply
16 * (1) allocate some space for the return struct and (2) call it.
18 *-------------------------------------------------------------------------
23 #include "nodes/pg_list.h"
24 #include "utils/syscache.h"
25 #include "utils/lsyscache.h"
26 #include "access/tupmacs.h"
27 #include "utils/rel.h"
28 #include "utils/palloc.h"
29 #include "utils/elog.h"
30 #include "access/attnum.h"
31 #include "access/heapam.h"
33 #include "catalog/pg_amop.h"
34 #include "catalog/pg_operator.h"
35 #include "catalog/pg_type.h"
37 /* ---------- AMOP CACHES ---------- */
42 * Return t iff operator 'opno' is in operator class 'opclass'.
46 op_class(Oid opno, int32 opclass, Oid amopid)
48 FormData_pg_amop amoptup;
50 if (SearchSysCacheStruct(AMOPOPID,
52 ObjectIdGetDatum(opclass),
53 ObjectIdGetDatum(opno),
54 ObjectIdGetDatum(amopid),
61 /* ---------- ATTRIBUTE CACHES ---------- */
66 * Given the relation id and the attribute number,
67 * return the "attname" field from the attribute relation.
71 get_attname(Oid relid, AttrNumber attnum)
73 FormData_pg_attribute att_tup;
75 if (SearchSysCacheStruct(ATTNUM,
77 ObjectIdGetDatum(relid),
78 UInt16GetDatum(attnum),
80 return pstrdup(att_tup.attname.data);
88 * Given the relation id and the attribute name,
89 * return the "attnum" field from the attribute relation.
93 get_attnum(Oid relid, char *attname)
95 FormData_pg_attribute att_tup;
97 if (SearchSysCacheStruct(ATTNAME, (char *) &att_tup,
98 ObjectIdGetDatum(relid),
99 PointerGetDatum(attname),
101 return att_tup.attnum;
103 return InvalidAttrNumber;
109 * Given the relation OID and the attribute number with the relation,
110 * return the attribute type OID.
114 get_atttype(Oid relid, AttrNumber attnum)
116 AttributeTupleForm att_tup = (AttributeTupleForm) palloc(sizeof(*att_tup));
118 if (SearchSysCacheStruct(ATTNUM,
120 ObjectIdGetDatum(relid),
121 UInt16GetDatum(attnum),
123 return att_tup->atttypid;
128 /* This routine uses the attname instead of the attnum because it
129 * replaces the routine find_atttype, which is called sometimes when
130 * only the attname, not the attno, is available.
133 get_attisset(Oid relid, char *attname)
137 AttributeTupleForm att_tup;
139 attno = get_attnum(relid, attname);
141 htup = SearchSysCacheTuple(ATTNAME,
142 ObjectIdGetDatum(relid),
143 PointerGetDatum(attname),
145 if (!HeapTupleIsValid(htup))
146 elog(ERROR, "get_attisset: no attribute %s in relation %d",
148 if (heap_attisnull(htup, attno))
152 att_tup = (AttributeTupleForm) GETSTRUCT(htup);
153 return att_tup->attisset;
160 * Given the relation id and the attribute number,
161 * return the "atttypmod" field from the attribute relation.
165 get_atttypmod(Oid relid, AttrNumber attnum)
167 FormData_pg_attribute att_tup;
169 if (SearchSysCacheStruct(ATTNUM,
171 ObjectIdGetDatum(relid),
172 UInt16GetDatum(attnum),
174 return att_tup.atttypmod;
179 /* ---------- INDEX CACHE ---------- */
181 /* watch this space...
184 /* ---------- OPERATOR CACHE ---------- */
189 * Returns the regproc id of the routine used to implement an
190 * operator given the operator uid.
196 FormData_pg_operator optup;
198 if (SearchSysCacheStruct(OPROID, (char *) &optup,
199 ObjectIdGetDatum(opno),
201 return optup.oprcode;
203 return (RegProcedure) NULL;
208 * returns the name of the operator with the given opno
210 * Note: return the struct so that it gets copied.
215 FormData_pg_operator optup;
217 if (SearchSysCacheStruct(OPROID, (char *) &optup,
218 ObjectIdGetDatum(opno),
220 return pstrdup(optup.oprname.data);
223 elog(ERROR, "can't look up operator %d\n", opno);
231 * Returns the left and right sort operators and types corresponding to a
232 * mergesortable operator, or nil if the operator is not mergesortable.
236 op_mergesortable(Oid opno, Oid ltype, Oid rtype, Oid *leftOp, Oid *rightOp)
238 FormData_pg_operator optup;
240 if (SearchSysCacheStruct(OPROID, (char *) &optup,
241 ObjectIdGetDatum(opno),
245 optup.oprleft == ltype &&
246 optup.oprright == rtype)
249 *leftOp = ObjectIdGetDatum(optup.oprlsortop);
250 *rightOp = ObjectIdGetDatum(optup.oprrsortop);
260 * Returns the hash operator corresponding to a hashjoinable operator,
261 * or nil if the operator is not hashjoinable.
265 op_hashjoinable(Oid opno, Oid ltype, Oid rtype)
267 FormData_pg_operator optup;
269 if (SearchSysCacheStruct(OPROID, (char *) &optup,
270 ObjectIdGetDatum(opno),
273 optup.oprleft == ltype &&
274 optup.oprright == rtype)
283 * Returns the corresponding commutator of an operator.
287 get_commutator(Oid opno)
289 FormData_pg_operator optup;
291 if (SearchSysCacheStruct(OPROID, (char *) &optup,
292 ObjectIdGetDatum(opno),
300 get_operator_tuple(Oid opno)
304 if ((optup = SearchSysCacheTuple(OPROID,
305 ObjectIdGetDatum(opno),
309 return (HeapTuple) NULL;
315 * Returns the corresponding negator of an operator.
319 get_negator(Oid opno)
321 FormData_pg_operator optup;
323 if (SearchSysCacheStruct(OPROID, (char *) &optup,
324 ObjectIdGetDatum(opno),
326 return optup.oprnegate;
334 * Returns procedure id for computing selectivity of an operator.
338 get_oprrest(Oid opno)
340 FormData_pg_operator optup;
342 if (SearchSysCacheStruct(OPROID, (char *) &optup,
343 ObjectIdGetDatum(opno),
345 return optup.oprrest;
347 return (RegProcedure) NULL;
353 * Returns procedure id for computing selectivity of a join.
357 get_oprjoin(Oid opno)
359 FormData_pg_operator optup;
361 if (SearchSysCacheStruct(OPROID, (char *) &optup,
362 ObjectIdGetDatum(opno),
364 return optup.oprjoin;
366 return (RegProcedure) NULL;
369 /* ---------- RELATION CACHE ---------- */
374 * Returns the number of attributes for a given relation.
378 get_relnatts(Oid relid)
380 FormData_pg_class reltup;
382 if (SearchSysCacheStruct(RELOID, (char *) &reltup,
383 ObjectIdGetDatum(relid),
385 return reltup.relnatts;
387 return InvalidAttrNumber;
393 * Returns the name of a given relation.
397 get_rel_name(Oid relid)
399 FormData_pg_class reltup;
401 if ((SearchSysCacheStruct(RELOID,
403 ObjectIdGetDatum(relid),
405 return pstrdup(reltup.relname.data);
410 /* ---------- TYPE CACHE ---------- */
415 * Given the type OID, return the length of the type.
419 get_typlen(Oid typid)
421 TypeTupleFormData typtup;
423 if (SearchSysCacheStruct(TYPOID, (char *) &typtup,
424 ObjectIdGetDatum(typid),
426 return typtup.typlen;
434 * Given the type OID, determine whether the type is returned by value or
435 * not. Returns 1 if by value, 0 if by reference.
439 get_typbyval(Oid typid)
441 TypeTupleFormData typtup;
443 if (SearchSysCacheStruct(TYPOID, (char *) &typtup,
444 ObjectIdGetDatum(typid),
446 return (bool) typtup.typbyval;
454 * Given the type OID, determine whether the type is returned by value or
455 * not. Returns 1 if by value, 0 if by reference.
460 get_typalign(Oid typid)
462 TypeTupleFormData typtup;
464 if (SearchSysCacheStruct(TYPOID, (char *) &typtup,
465 ObjectIdGetDatum(typid),
467 return typtup.typalign;
477 * Given the type OID, return the default value of the ADT.
481 get_typdefault(Oid typid)
483 struct varlena *typdefault =
484 (struct varlena *) TypeDefaultRetrieve(typid);
492 * Given the type OID, find if it is a basic type, a named relation
493 * or the generic type 'relation'.
494 * It returns the null char if the cache lookup fails...
499 get_typtype(Oid typid)
501 TypeTupleFormData typtup;
503 if (SearchSysCacheStruct(TYPOID, (char *) &typtup,
504 ObjectIdGetDatum(typid),
506 return typtup.typtype;