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