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.29 1999/07/15 22:40:04 momjian Exp $
13 * Eventually, the index information should go through here, too.
14 *-------------------------------------------------------------------------
19 #include "utils/syscache.h"
20 #include "utils/lsyscache.h"
22 #include "catalog/pg_operator.h"
23 #include "catalog/pg_type.h"
25 /* ---------- AMOP CACHES ---------- */
30 * Return t iff operator 'opno' is in operator class 'opclass'.
34 op_class(Oid oprno, int32 opclass, Oid amopid)
36 if (HeapTupleIsValid(SearchSysCacheTuple(AMOPOPID,
37 ObjectIdGetDatum(opclass),
38 ObjectIdGetDatum(oprno),
39 ObjectIdGetDatum(amopid),
46 /* ---------- ATTRIBUTE CACHES ---------- */
51 * Given the relation id and the attribute number,
52 * return the "attname" field from the attribute relation.
56 get_attname(Oid relid, AttrNumber attnum)
60 tp = SearchSysCacheTuple(ATTNUM,
61 ObjectIdGetDatum(relid),
62 UInt16GetDatum(attnum),
64 if (HeapTupleIsValid(tp))
66 Form_pg_attribute att_tup = (Form_pg_attribute) GETSTRUCT(tp);
67 return pstrdup(att_tup->attname.data);
76 * Given the relation id and the attribute name,
77 * return the "attnum" field from the attribute relation.
81 get_attnum(Oid relid, char *attname)
85 tp = SearchSysCacheTuple(ATTNAME,
86 ObjectIdGetDatum(relid),
87 PointerGetDatum(attname),
89 if (HeapTupleIsValid(tp))
91 Form_pg_attribute att_tup = (Form_pg_attribute) GETSTRUCT(tp);
92 return att_tup->attnum;
95 return InvalidAttrNumber;
101 * Given the relation OID and the attribute number with the relation,
102 * return the attribute type OID.
106 get_atttype(Oid relid, AttrNumber attnum)
110 tp = SearchSysCacheTuple(ATTNUM,
111 ObjectIdGetDatum(relid),
112 UInt16GetDatum(attnum),
114 if (HeapTupleIsValid(tp))
116 Form_pg_attribute att_tup = (Form_pg_attribute) GETSTRUCT(tp);
117 return att_tup->atttypid;
123 /* This routine uses the attname instead of the attnum because it
124 * replaces the routine find_atttype, which is called sometimes when
125 * only the attname, not the attno, is available.
128 get_attisset(Oid relid, char *attname)
132 tp = SearchSysCacheTuple(ATTNAME,
133 ObjectIdGetDatum(relid),
134 PointerGetDatum(attname),
136 if (HeapTupleIsValid(tp))
138 Form_pg_attribute att_tup = (Form_pg_attribute) GETSTRUCT(tp);
139 return att_tup->attisset;
148 * Given the relation id and the attribute number,
149 * return the "atttypmod" field from the attribute relation.
153 get_atttypmod(Oid relid, AttrNumber attnum)
157 tp = SearchSysCacheTuple(ATTNUM,
158 ObjectIdGetDatum(relid),
159 UInt16GetDatum(attnum),
161 if (HeapTupleIsValid(tp))
163 Form_pg_attribute att_tup = (Form_pg_attribute) GETSTRUCT(tp);
164 return att_tup->atttypmod;
170 /* ---------- INDEX CACHE ---------- */
172 /* watch this space...
175 /* ---------- OPERATOR CACHE ---------- */
180 * Returns the regproc id of the routine used to implement an
181 * operator given the operator oid.
189 tp = SearchSysCacheTuple(OPROID,
190 ObjectIdGetDatum(opno),
192 if (HeapTupleIsValid(tp))
194 Form_pg_operator optup = (Form_pg_operator) GETSTRUCT(tp);
195 return optup->oprcode;
198 return (RegProcedure) NULL;
203 * returns the name of the operator with the given opno
205 * Note: returns a palloc'd copy of the string, or NULL if no such operator.
212 tp = SearchSysCacheTuple(OPROID,
213 ObjectIdGetDatum(opno),
215 if (HeapTupleIsValid(tp))
217 Form_pg_operator optup = (Form_pg_operator) GETSTRUCT(tp);
218 return pstrdup(optup->oprname.data);
227 * Returns the left and right sort operators and types corresponding to a
228 * mergejoinable operator, or nil if the operator is not mergejoinable.
232 op_mergejoinable(Oid opno, Oid ltype, Oid rtype, Oid *leftOp, Oid *rightOp)
236 tp = SearchSysCacheTuple(OPROID,
237 ObjectIdGetDatum(opno),
239 if (HeapTupleIsValid(tp))
241 Form_pg_operator optup = (Form_pg_operator) GETSTRUCT(tp);
243 if (optup->oprlsortop &&
245 optup->oprleft == ltype &&
246 optup->oprright == rtype)
248 *leftOp = ObjectIdGetDatum(optup->oprlsortop);
249 *rightOp = ObjectIdGetDatum(optup->oprrsortop);
259 * Returns the hash operator corresponding to a hashjoinable operator,
260 * or nil if the operator is not hashjoinable.
264 op_hashjoinable(Oid opno, Oid ltype, Oid rtype)
268 tp = SearchSysCacheTuple(OPROID,
269 ObjectIdGetDatum(opno),
271 if (HeapTupleIsValid(tp))
273 Form_pg_operator optup = (Form_pg_operator) GETSTRUCT(tp);
275 if (optup->oprcanhash &&
276 optup->oprleft == ltype &&
277 optup->oprright == rtype)
284 get_operator_tuple(Oid opno)
288 if ((optup = SearchSysCacheTuple(OPROID,
289 ObjectIdGetDatum(opno),
293 return (HeapTuple) NULL;
299 * Returns the corresponding commutator of an operator.
303 get_commutator(Oid opno)
307 tp = SearchSysCacheTuple(OPROID,
308 ObjectIdGetDatum(opno),
310 if (HeapTupleIsValid(tp))
312 Form_pg_operator optup = (Form_pg_operator) GETSTRUCT(tp);
313 return optup->oprcom;
322 * Returns the corresponding negator of an operator.
326 get_negator(Oid opno)
330 tp = SearchSysCacheTuple(OPROID,
331 ObjectIdGetDatum(opno),
333 if (HeapTupleIsValid(tp))
335 Form_pg_operator optup = (Form_pg_operator) GETSTRUCT(tp);
336 return optup->oprnegate;
345 * Returns procedure id for computing selectivity of an operator.
349 get_oprrest(Oid opno)
353 tp = SearchSysCacheTuple(OPROID,
354 ObjectIdGetDatum(opno),
356 if (HeapTupleIsValid(tp))
358 Form_pg_operator optup = (Form_pg_operator) GETSTRUCT(tp);
359 return optup->oprrest;
362 return (RegProcedure) NULL;
368 * Returns procedure id for computing selectivity of a join.
372 get_oprjoin(Oid opno)
376 tp = SearchSysCacheTuple(OPROID,
377 ObjectIdGetDatum(opno),
379 if (HeapTupleIsValid(tp))
381 Form_pg_operator optup = (Form_pg_operator) GETSTRUCT(tp);
382 return optup->oprjoin;
385 return (RegProcedure) NULL;
388 /* ---------- RELATION CACHE ---------- */
393 * Returns the number of attributes for a given relation.
397 get_relnatts(Oid relid)
401 tp = SearchSysCacheTuple(RELOID,
402 ObjectIdGetDatum(relid),
404 if (HeapTupleIsValid(tp))
406 Form_pg_class reltup = (Form_pg_class) GETSTRUCT(tp);
407 return reltup->relnatts;
410 return InvalidAttrNumber;
416 * Returns the name of a given relation.
420 get_rel_name(Oid relid)
424 tp = SearchSysCacheTuple(RELOID,
425 ObjectIdGetDatum(relid),
427 if (HeapTupleIsValid(tp))
429 Form_pg_class reltup = (Form_pg_class) GETSTRUCT(tp);
430 return pstrdup(reltup->relname.data);
436 /* ---------- TYPE CACHE ---------- */
441 * Given the type OID, return the length of the type.
445 get_typlen(Oid typid)
449 tp = SearchSysCacheTuple(TYPOID,
450 ObjectIdGetDatum(typid),
452 if (HeapTupleIsValid(tp))
454 Form_pg_type typtup = (Form_pg_type) GETSTRUCT(tp);
455 return typtup->typlen;
464 * Given the type OID, determine whether the type is returned by value or
465 * not. Returns 1 if by value, 0 if by reference.
469 get_typbyval(Oid typid)
473 tp = SearchSysCacheTuple(TYPOID,
474 ObjectIdGetDatum(typid),
476 if (HeapTupleIsValid(tp))
478 Form_pg_type typtup = (Form_pg_type) GETSTRUCT(tp);
479 return (bool) typtup->typbyval;
487 get_typalign(Oid typid)
491 tp = SearchSysCacheTuple(TYPOID,
492 ObjectIdGetDatum(typid),
494 if (HeapTupleIsValid(tp))
496 Form_pg_type typtup = (Form_pg_type) GETSTRUCT(tp);
497 return typtup->typalign;
508 * Given the type OID, return the default value of the ADT.
512 get_typdefault(Oid typid)
514 struct varlena *typdefault = (struct varlena *) TypeDefaultRetrieve(typid);
522 * Given the type OID, find if it is a basic type, a named relation
523 * or the generic type 'relation'.
524 * It returns the null char if the cache lookup fails...
529 get_typtype(Oid typid)
533 tp = SearchSysCacheTuple(TYPOID,
534 ObjectIdGetDatum(typid),
536 if (HeapTupleIsValid(tp))
538 Form_pg_type typtup = (Form_pg_type) GETSTRUCT(tp);
539 return typtup->typtype;