]> granicus.if.org Git - postgresql/blob - src/backend/utils/cache/lsyscache.c
Remove un-needed braces around single statements.
[postgresql] / src / backend / utils / cache / lsyscache.c
1 /*-------------------------------------------------------------------------
2  *
3  * lsyscache.c--
4  *        Routines to access information within system caches
5  *
6  * Copyright (c) 1994, Regents of the University of California
7  *
8  *
9  * IDENTIFICATION
10  *        $Header: /cvsroot/pgsql/src/backend/utils/cache/lsyscache.c,v 1.14 1998/06/15 19:29:40 momjian Exp $
11  *
12  * NOTES
13  *        Eventually, the index information should go through here, too.
14  *
15  *        Most of these routines call SearchSysCacheStruct() and thus simply
16  *        (1) allocate some space for the return struct and (2) call it.
17  *
18  *-------------------------------------------------------------------------
19  */
20 #include <string.h>
21 #include "postgres.h"
22
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"
32
33 #include "catalog/pg_amop.h"
34 #include "catalog/pg_operator.h"
35 #include "catalog/pg_type.h"
36
37 /*                              ---------- AMOP CACHES ----------                                                */
38
39 /*
40  * op_class -
41  *
42  *              Return t iff operator 'opno' is in operator class 'opclass'.
43  *
44  */
45 bool
46 op_class(Oid opno, int32 opclass, Oid amopid)
47 {
48         FormData_pg_amop amoptup;
49
50         if (SearchSysCacheStruct(AMOPOPID,
51                                                          (char *) &amoptup,
52                                                          ObjectIdGetDatum(opclass),
53                                                          ObjectIdGetDatum(opno),
54                                                          ObjectIdGetDatum(amopid),
55                                                          0))
56                 return true;
57         else
58                 return false;
59 }
60
61 /*                              ---------- ATTRIBUTE CACHES ----------                                   */
62
63 /*
64  * get_attname -
65  *
66  *              Given the relation id and the attribute number,
67  *              return the "attname" field from the attribute relation.
68  *
69  */
70 char *
71 get_attname(Oid relid, AttrNumber attnum)
72 {
73         FormData_pg_attribute att_tup;
74
75         if (SearchSysCacheStruct(ATTNUM,
76                                                          (char *) &att_tup,
77                                                          ObjectIdGetDatum(relid),
78                                                          UInt16GetDatum(attnum),
79                                                          0, 0))
80                 return pstrdup(att_tup.attname.data);
81         else
82                 return NULL;
83 }
84
85 /*
86  * get_attnum -
87  *
88  *              Given the relation id and the attribute name,
89  *              return the "attnum" field from the attribute relation.
90  *
91  */
92 AttrNumber
93 get_attnum(Oid relid, char *attname)
94 {
95         FormData_pg_attribute att_tup;
96
97         if (SearchSysCacheStruct(ATTNAME, (char *) &att_tup,
98                                                          ObjectIdGetDatum(relid),
99                                                          PointerGetDatum(attname),
100                                                          0, 0))
101                 return att_tup.attnum;
102         else
103                 return InvalidAttrNumber;
104 }
105
106 /*
107  * get_atttype -
108  *
109  *              Given the relation OID and the attribute number with the relation,
110  *              return the attribute type OID.
111  *
112  */
113 Oid
114 get_atttype(Oid relid, AttrNumber attnum)
115 {
116         AttributeTupleForm att_tup = (AttributeTupleForm) palloc(sizeof(*att_tup));
117
118         if (SearchSysCacheStruct(ATTNUM,
119                                                          (char *) att_tup,
120                                                          ObjectIdGetDatum(relid),
121                                                          UInt16GetDatum(attnum),
122                                                          0, 0))
123                 return att_tup->atttypid;
124         else
125                 return (Oid) NULL;
126 }
127
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.
131  */
132 bool
133 get_attisset(Oid relid, char *attname)
134 {
135         HeapTuple       htup;
136         AttrNumber      attno;
137         AttributeTupleForm att_tup;
138
139         attno = get_attnum(relid, attname);
140
141         htup = SearchSysCacheTuple(ATTNAME,
142                                                            ObjectIdGetDatum(relid),
143                                                            PointerGetDatum(attname),
144                                                            0, 0);
145         if (!HeapTupleIsValid(htup))
146                 elog(ERROR, "get_attisset: no attribute %s in relation %d",
147                          attname, relid);
148         if (heap_attisnull(htup, attno))
149                 return false;
150         else
151         {
152                 att_tup = (AttributeTupleForm) GETSTRUCT(htup);
153                 return att_tup->attisset;
154         }
155 }
156
157 /*
158  * get_atttypmod -
159  *
160  *              Given the relation id and the attribute number,
161  *              return the "atttypmod" field from the attribute relation.
162  *
163  */
164 int16
165 get_atttypmod(Oid relid, AttrNumber attnum)
166 {
167         FormData_pg_attribute att_tup;
168
169         if (SearchSysCacheStruct(ATTNUM,
170                                                          (char *) &att_tup,
171                                                          ObjectIdGetDatum(relid),
172                                                          UInt16GetDatum(attnum),
173                                                          0, 0))
174                 return att_tup.atttypmod;
175         else
176                 return -1;
177 }
178
179 /*                              ---------- INDEX CACHE ----------                                                */
180
181 /*              watch this space...
182  */
183
184 /*                              ---------- OPERATOR CACHE ----------                                     */
185
186 /*
187  * get_opcode -
188  *
189  *              Returns the regproc id of the routine used to implement an
190  *              operator given the operator uid.
191  *
192  */
193 RegProcedure
194 get_opcode(Oid opno)
195 {
196         FormData_pg_operator optup;
197
198         if (SearchSysCacheStruct(OPROID, (char *) &optup,
199                                                          ObjectIdGetDatum(opno),
200                                                          0, 0, 0))
201                 return optup.oprcode;
202         else
203                 return (RegProcedure) NULL;
204 }
205
206 /*
207  * get_opname -
208  *        returns the name of the operator with the given opno
209  *
210  * Note: return the struct so that it gets copied.
211  */
212 char *
213 get_opname(Oid opno)
214 {
215         FormData_pg_operator optup;
216
217         if (SearchSysCacheStruct(OPROID, (char *) &optup,
218                                                          ObjectIdGetDatum(opno),
219                                                          0, 0, 0))
220                 return pstrdup(optup.oprname.data);
221         else
222         {
223                 elog(ERROR, "can't look up operator %d\n", opno);
224                 return NULL;
225         }
226 }
227
228 /*
229  * op_mergesortable -
230  *
231  *              Returns the left and right sort operators and types corresponding to a
232  *              mergesortable operator, or nil if the operator is not mergesortable.
233  *
234  */
235 bool
236 op_mergesortable(Oid opno, Oid ltype, Oid rtype, Oid *leftOp, Oid *rightOp)
237 {
238         FormData_pg_operator optup;
239
240         if (SearchSysCacheStruct(OPROID, (char *) &optup,
241                                                          ObjectIdGetDatum(opno),
242                                                          0, 0, 0) &&
243                 optup.oprlsortop &&
244                 optup.oprrsortop &&
245                 optup.oprleft == ltype &&
246                 optup.oprright == rtype)
247         {
248
249                 *leftOp = ObjectIdGetDatum(optup.oprlsortop);
250                 *rightOp = ObjectIdGetDatum(optup.oprrsortop);
251                 return TRUE;
252         }
253         else
254                 return FALSE;
255 }
256
257 /*
258  * op_hashjoinable--
259  *
260  * Returns the hash operator corresponding to a hashjoinable operator,
261  * or nil if the operator is not hashjoinable.
262  *
263  */
264 Oid
265 op_hashjoinable(Oid opno, Oid ltype, Oid rtype)
266 {
267         FormData_pg_operator optup;
268
269         if (SearchSysCacheStruct(OPROID, (char *) &optup,
270                                                          ObjectIdGetDatum(opno),
271                                                          0, 0, 0) &&
272                 optup.oprcanhash &&
273                 optup.oprleft == ltype &&
274                 optup.oprright == rtype)
275                 return opno;
276         else
277                 return InvalidOid;
278 }
279
280 /*
281  * get_commutator -
282  *
283  *              Returns the corresponding commutator of an operator.
284  *
285  */
286 Oid
287 get_commutator(Oid opno)
288 {
289         FormData_pg_operator optup;
290
291         if (SearchSysCacheStruct(OPROID, (char *) &optup,
292                                                          ObjectIdGetDatum(opno),
293                                                          0, 0, 0))
294                 return optup.oprcom;
295         else
296                 return (Oid) NULL;
297 }
298
299 HeapTuple
300 get_operator_tuple(Oid opno)
301 {
302         HeapTuple       optup;
303
304         if ((optup = SearchSysCacheTuple(OPROID,
305                                                                          ObjectIdGetDatum(opno),
306                                                                          0, 0, 0)))
307                 return optup;
308         else
309                 return (HeapTuple) NULL;
310 }
311
312 /*
313  * get_negator -
314  *
315  *              Returns the corresponding negator of an operator.
316  *
317  */
318 Oid
319 get_negator(Oid opno)
320 {
321         FormData_pg_operator optup;
322
323         if (SearchSysCacheStruct(OPROID, (char *) &optup,
324                                                          ObjectIdGetDatum(opno),
325                                                          0, 0, 0))
326                 return optup.oprnegate;
327         else
328                 return (Oid) NULL;
329 }
330
331 /*
332  * get_oprrest -
333  *
334  *              Returns procedure id for computing selectivity of an operator.
335  *
336  */
337 RegProcedure
338 get_oprrest(Oid opno)
339 {
340         FormData_pg_operator optup;
341
342         if (SearchSysCacheStruct(OPROID, (char *) &optup,
343                                                          ObjectIdGetDatum(opno),
344                                                          0, 0, 0))
345                 return optup.oprrest;
346         else
347                 return (RegProcedure) NULL;
348 }
349
350 /*
351  * get_oprjoin -
352  *
353  *              Returns procedure id for computing selectivity of a join.
354  *
355  */
356 RegProcedure
357 get_oprjoin(Oid opno)
358 {
359         FormData_pg_operator optup;
360
361         if (SearchSysCacheStruct(OPROID, (char *) &optup,
362                                                          ObjectIdGetDatum(opno),
363                                                          0, 0, 0))
364                 return optup.oprjoin;
365         else
366                 return (RegProcedure) NULL;
367 }
368
369 /*                              ---------- RELATION CACHE ----------                                     */
370
371 /*
372  * get_relnatts -
373  *
374  *              Returns the number of attributes for a given relation.
375  *
376  */
377 int
378 get_relnatts(Oid relid)
379 {
380         FormData_pg_class reltup;
381
382         if (SearchSysCacheStruct(RELOID, (char *) &reltup,
383                                                          ObjectIdGetDatum(relid),
384                                                          0, 0, 0))
385                 return reltup.relnatts;
386         else
387                 return InvalidAttrNumber;
388 }
389
390 /*
391  * get_rel_name -
392  *
393  *              Returns the name of a given relation.
394  *
395  */
396 char *
397 get_rel_name(Oid relid)
398 {
399         FormData_pg_class reltup;
400
401         if ((SearchSysCacheStruct(RELOID,
402                                                           (char *) &reltup,
403                                                           ObjectIdGetDatum(relid),
404                                                           0, 0, 0)))
405                 return pstrdup(reltup.relname.data);
406         else
407                 return NULL;
408 }
409
410 /*                              ---------- TYPE CACHE ----------                                                 */
411
412 /*
413  * get_typlen -
414  *
415  *              Given the type OID, return the length of the type.
416  *
417  */
418 int16
419 get_typlen(Oid typid)
420 {
421         TypeTupleFormData typtup;
422
423         if (SearchSysCacheStruct(TYPOID, (char *) &typtup,
424                                                          ObjectIdGetDatum(typid),
425                                                          0, 0, 0))
426                 return typtup.typlen;
427         else
428                 return (int16) NULL;
429 }
430
431 /*
432  * get_typbyval -
433  *
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.
436  *
437  */
438 bool
439 get_typbyval(Oid typid)
440 {
441         TypeTupleFormData typtup;
442
443         if (SearchSysCacheStruct(TYPOID, (char *) &typtup,
444                                                          ObjectIdGetDatum(typid),
445                                                          0, 0, 0))
446                 return (bool) typtup.typbyval;
447         else
448                 return false;
449 }
450
451 /*
452  * get_typbyval -
453  *
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.
456  *
457  */
458 #ifdef NOT_USED
459 char
460 get_typalign(Oid typid)
461 {
462         TypeTupleFormData typtup;
463
464         if (SearchSysCacheStruct(TYPOID, (char *) &typtup,
465                                                          ObjectIdGetDatum(typid),
466                                                          0, 0, 0))
467                 return typtup.typalign;
468         else
469                 return 'i';
470 }
471
472 #endif
473
474 /*
475  * get_typdefault -
476  *
477  *              Given the type OID, return the default value of the ADT.
478  *
479  */
480 struct varlena *
481 get_typdefault(Oid typid)
482 {
483         struct varlena *typdefault =
484         (struct varlena *) TypeDefaultRetrieve(typid);
485
486         return typdefault;
487 }
488
489 /*
490  * get_typtype -
491  *
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...
495  *
496  */
497 #ifdef NOT_USED
498 char
499 get_typtype(Oid typid)
500 {
501         TypeTupleFormData typtup;
502
503         if (SearchSysCacheStruct(TYPOID, (char *) &typtup,
504                                                          ObjectIdGetDatum(typid),
505                                                          0, 0, 0))
506                 return typtup.typtype;
507         else
508                 return '\0';
509 }
510
511 #endif