]> granicus.if.org Git - postgresql/blob - src/backend/utils/cache/lsyscache.c
Clean up some sloppy casts --- Oid vs. Datum, that sort of thing.
[postgresql] / src / backend / utils / cache / lsyscache.c
1 /*-------------------------------------------------------------------------
2  *
3  * lsyscache.c
4  *        Convenience routines for common queries in the system catalog cache.
5  *
6  * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  * IDENTIFICATION
10  *        $Header: /cvsroot/pgsql/src/backend/utils/cache/lsyscache.c,v 1.44 2000/07/23 03:50:26 tgl Exp $
11  *
12  * NOTES
13  *        Eventually, the index information should go through here, too.
14  *-------------------------------------------------------------------------
15  */
16 #include "postgres.h"
17
18 #include "catalog/pg_operator.h"
19 #include "catalog/pg_proc.h"
20 #include "catalog/pg_type.h"
21 #include "utils/lsyscache.h"
22 #include "utils/syscache.h"
23
24 /*                              ---------- AMOP CACHES ----------                                                */
25
26 /*
27  * op_class
28  *
29  *              Return t iff operator 'opid' is in operator class 'opclass' for
30  *              access method 'amopid'.
31  */
32 bool
33 op_class(Oid opid, Oid opclass, Oid amopid)
34 {
35         if (HeapTupleIsValid(SearchSysCacheTuple(AMOPOPID,
36                                                                                          ObjectIdGetDatum(opclass),
37                                                                                          ObjectIdGetDatum(opid),
38                                                                                          ObjectIdGetDatum(amopid),
39                                                                                          0)))
40                 return true;
41         else
42                 return false;
43 }
44
45 /*                              ---------- ATTRIBUTE CACHES ----------                                   */
46
47 /*
48  * get_attname
49  *
50  *              Given the relation id and the attribute number,
51  *              return the "attname" field from the attribute relation.
52  */
53 char *
54 get_attname(Oid relid, AttrNumber attnum)
55 {
56         HeapTuple       tp;
57
58         tp = SearchSysCacheTuple(ATTNUM,
59                                                          ObjectIdGetDatum(relid),
60                                                          Int16GetDatum(attnum),
61                                                          0, 0);
62         if (HeapTupleIsValid(tp))
63         {
64                 Form_pg_attribute att_tup = (Form_pg_attribute) GETSTRUCT(tp);
65
66                 return pstrdup(NameStr(att_tup->attname));
67         }
68         else
69                 return NULL;
70 }
71
72 /*
73  * get_attnum
74  *
75  *              Given the relation id and the attribute name,
76  *              return the "attnum" field from the attribute relation.
77  */
78 AttrNumber
79 get_attnum(Oid relid, char *attname)
80 {
81         HeapTuple       tp;
82
83         tp = SearchSysCacheTuple(ATTNAME,
84                                                          ObjectIdGetDatum(relid),
85                                                          PointerGetDatum(attname),
86                                                          0, 0);
87         if (HeapTupleIsValid(tp))
88         {
89                 Form_pg_attribute att_tup = (Form_pg_attribute) GETSTRUCT(tp);
90
91                 return att_tup->attnum;
92         }
93         else
94                 return InvalidAttrNumber;
95 }
96
97 /*
98  * get_atttype
99  *
100  *              Given the relation OID and the attribute number with the relation,
101  *              return the attribute type OID.
102  */
103 Oid
104 get_atttype(Oid relid, AttrNumber attnum)
105 {
106         HeapTuple       tp;
107
108         tp = SearchSysCacheTuple(ATTNUM,
109                                                          ObjectIdGetDatum(relid),
110                                                          Int16GetDatum(attnum),
111                                                          0, 0);
112         if (HeapTupleIsValid(tp))
113         {
114                 Form_pg_attribute att_tup = (Form_pg_attribute) GETSTRUCT(tp);
115
116                 return att_tup->atttypid;
117         }
118         else
119                 return InvalidOid;
120 }
121
122 /* This routine uses the attname instead of the attnum because it
123  * replaces the routine find_atttype, which is called sometimes when
124  * only the attname, not the attno, is available.
125  */
126 bool
127 get_attisset(Oid relid, char *attname)
128 {
129         HeapTuple       tp;
130
131         tp = SearchSysCacheTuple(ATTNAME,
132                                                          ObjectIdGetDatum(relid),
133                                                          PointerGetDatum(attname),
134                                                          0, 0);
135         if (HeapTupleIsValid(tp))
136         {
137                 Form_pg_attribute att_tup = (Form_pg_attribute) GETSTRUCT(tp);
138
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 int32
152 get_atttypmod(Oid relid, AttrNumber attnum)
153 {
154         HeapTuple       tp;
155
156         tp = SearchSysCacheTuple(ATTNUM,
157                                                          ObjectIdGetDatum(relid),
158                                                          Int16GetDatum(attnum),
159                                                          0, 0);
160         if (HeapTupleIsValid(tp))
161         {
162                 Form_pg_attribute att_tup = (Form_pg_attribute) GETSTRUCT(tp);
163
164                 return att_tup->atttypmod;
165         }
166         else
167                 return -1;
168 }
169
170 /*
171  * get_attdisbursion
172  *
173  *        Retrieve the disbursion statistic for an attribute,
174  *        or produce an estimate if no info is available.
175  *
176  * min_estimate is the minimum estimate to return if insufficient data
177  * is available to produce a reliable value.  This value may vary
178  * depending on context.  (For example, when deciding whether it is
179  * safe to use a hashjoin, we want to be more conservative than when
180  * estimating the number of tuples produced by an equijoin.)
181  */
182 double
183 get_attdisbursion(Oid relid, AttrNumber attnum, double min_estimate)
184 {
185         HeapTuple       atp;
186         Form_pg_attribute att_tup;
187         double          disbursion;
188         int32           ntuples;
189
190         atp = SearchSysCacheTuple(ATTNUM,
191                                                           ObjectIdGetDatum(relid),
192                                                           Int16GetDatum(attnum),
193                                                           0, 0);
194         if (!HeapTupleIsValid(atp))
195         {
196                 /* this should not happen */
197                 elog(ERROR, "get_attdisbursion: no attribute tuple %u %d",
198                          relid, attnum);
199                 return min_estimate;
200         }
201         att_tup = (Form_pg_attribute) GETSTRUCT(atp);
202
203         disbursion = att_tup->attdisbursion;
204         if (disbursion > 0.0)
205                 return disbursion;              /* we have a specific estimate from VACUUM */
206
207         /*
208          * Special-case boolean columns: the disbursion of a boolean is highly
209          * unlikely to be anywhere near 1/numtuples, instead it's probably
210          * more like 0.5.
211          *
212          * Are there any other cases we should wire in special estimates for?
213          */
214         if (att_tup->atttypid == BOOLOID)
215                 return 0.5;
216
217         /*
218          * Disbursion is either 0 (no data available) or -1 (disbursion is
219          * 1/numtuples).  Either way, we need the relation size.
220          */
221
222         atp = SearchSysCacheTuple(RELOID,
223                                                           ObjectIdGetDatum(relid),
224                                                           0, 0, 0);
225         if (!HeapTupleIsValid(atp))
226         {
227                 /* this should not happen */
228                 elog(ERROR, "get_attdisbursion: no relation tuple %u", relid);
229                 return min_estimate;
230         }
231
232         ntuples = ((Form_pg_class) GETSTRUCT(atp))->reltuples;
233
234         if (ntuples == 0)
235                 return min_estimate;    /* no data available */
236
237         if (disbursion < 0.0)           /* VACUUM thinks there are no duplicates */
238                 return 1.0 / (double) ntuples;
239
240         /*
241          * VACUUM ANALYZE does not compute disbursion for system attributes,
242          * but some of them can reasonably be assumed unique anyway.
243          */
244         if (attnum == ObjectIdAttributeNumber ||
245                 attnum == SelfItemPointerAttributeNumber)
246                 return 1.0 / (double) ntuples;
247         if (attnum == TableOidAttributeNumber)
248                 return 1.0;
249
250         /*
251          * VACUUM ANALYZE has not been run for this table. Produce an estimate
252          * = 1/numtuples.  This may produce unreasonably small estimates for
253          * large tables, so limit the estimate to no less than min_estimate.
254          */
255         disbursion = 1.0 / (double) ntuples;
256         if (disbursion < min_estimate)
257                 disbursion = min_estimate;
258
259         return disbursion;
260 }
261
262 /*                              ---------- INDEX CACHE ----------                                                */
263
264 /*              watch this space...
265  */
266
267 /*                              ---------- OPERATOR CACHE ----------                                     */
268
269 /*
270  * get_opcode
271  *
272  *              Returns the regproc id of the routine used to implement an
273  *              operator given the operator oid.
274  */
275 RegProcedure
276 get_opcode(Oid opno)
277 {
278         HeapTuple       tp;
279
280         tp = SearchSysCacheTuple(OPEROID,
281                                                          ObjectIdGetDatum(opno),
282                                                          0, 0, 0);
283         if (HeapTupleIsValid(tp))
284         {
285                 Form_pg_operator optup = (Form_pg_operator) GETSTRUCT(tp);
286
287                 return optup->oprcode;
288         }
289         else
290                 return (RegProcedure) NULL;
291 }
292
293 /*
294  * get_opname
295  *        returns the name of the operator with the given opno
296  *
297  * Note: returns a palloc'd copy of the string, or NULL if no such operator.
298  */
299 char *
300 get_opname(Oid opno)
301 {
302         HeapTuple       tp;
303
304         tp = SearchSysCacheTuple(OPEROID,
305                                                          ObjectIdGetDatum(opno),
306                                                          0, 0, 0);
307         if (HeapTupleIsValid(tp))
308         {
309                 Form_pg_operator optup = (Form_pg_operator) GETSTRUCT(tp);
310
311                 return pstrdup(NameStr(optup->oprname));
312         }
313         else
314                 return NULL;
315 }
316
317 /*
318  * op_mergejoinable
319  *
320  *              Returns the left and right sort operators and types corresponding to a
321  *              mergejoinable operator, or nil if the operator is not mergejoinable.
322  */
323 bool
324 op_mergejoinable(Oid opno, Oid ltype, Oid rtype, Oid *leftOp, Oid *rightOp)
325 {
326         HeapTuple       tp;
327
328         tp = SearchSysCacheTuple(OPEROID,
329                                                          ObjectIdGetDatum(opno),
330                                                          0, 0, 0);
331         if (HeapTupleIsValid(tp))
332         {
333                 Form_pg_operator optup = (Form_pg_operator) GETSTRUCT(tp);
334
335                 if (optup->oprlsortop &&
336                         optup->oprrsortop &&
337                         optup->oprleft == ltype &&
338                         optup->oprright == rtype)
339                 {
340                         *leftOp = optup->oprlsortop;
341                         *rightOp = optup->oprrsortop;
342                         return true;
343                 }
344         }
345         return false;
346 }
347
348 /*
349  * op_hashjoinable
350  *
351  * Returns the hash operator corresponding to a hashjoinable operator,
352  * or InvalidOid if the operator is not hashjoinable.
353  */
354 Oid
355 op_hashjoinable(Oid opno, Oid ltype, Oid rtype)
356 {
357         HeapTuple       tp;
358
359         tp = SearchSysCacheTuple(OPEROID,
360                                                          ObjectIdGetDatum(opno),
361                                                          0, 0, 0);
362         if (HeapTupleIsValid(tp))
363         {
364                 Form_pg_operator optup = (Form_pg_operator) GETSTRUCT(tp);
365
366                 if (optup->oprcanhash &&
367                         optup->oprleft == ltype &&
368                         optup->oprright == rtype)
369                         return opno;
370         }
371         return InvalidOid;
372 }
373
374 HeapTuple
375 get_operator_tuple(Oid opno)
376 {
377         HeapTuple       optup;
378
379         if ((optup = SearchSysCacheTuple(OPEROID,
380                                                                          ObjectIdGetDatum(opno),
381                                                                          0, 0, 0)))
382                 return optup;
383         else
384                 return (HeapTuple) NULL;
385 }
386
387 /*
388  * get_commutator
389  *
390  *              Returns the corresponding commutator of an operator.
391  */
392 Oid
393 get_commutator(Oid opno)
394 {
395         HeapTuple       tp;
396
397         tp = SearchSysCacheTuple(OPEROID,
398                                                          ObjectIdGetDatum(opno),
399                                                          0, 0, 0);
400         if (HeapTupleIsValid(tp))
401         {
402                 Form_pg_operator optup = (Form_pg_operator) GETSTRUCT(tp);
403
404                 return optup->oprcom;
405         }
406         else
407                 return InvalidOid;
408 }
409
410 /*
411  * get_negator
412  *
413  *              Returns the corresponding negator of an operator.
414  */
415 Oid
416 get_negator(Oid opno)
417 {
418         HeapTuple       tp;
419
420         tp = SearchSysCacheTuple(OPEROID,
421                                                          ObjectIdGetDatum(opno),
422                                                          0, 0, 0);
423         if (HeapTupleIsValid(tp))
424         {
425                 Form_pg_operator optup = (Form_pg_operator) GETSTRUCT(tp);
426
427                 return optup->oprnegate;
428         }
429         else
430                 return InvalidOid;
431 }
432
433 /*
434  * get_oprrest
435  *
436  *              Returns procedure id for computing selectivity of an operator.
437  */
438 RegProcedure
439 get_oprrest(Oid opno)
440 {
441         HeapTuple       tp;
442
443         tp = SearchSysCacheTuple(OPEROID,
444                                                          ObjectIdGetDatum(opno),
445                                                          0, 0, 0);
446         if (HeapTupleIsValid(tp))
447         {
448                 Form_pg_operator optup = (Form_pg_operator) GETSTRUCT(tp);
449
450                 return optup->oprrest;
451         }
452         else
453                 return (RegProcedure) NULL;
454 }
455
456 /*
457  * get_oprjoin
458  *
459  *              Returns procedure id for computing selectivity of a join.
460  */
461 RegProcedure
462 get_oprjoin(Oid opno)
463 {
464         HeapTuple       tp;
465
466         tp = SearchSysCacheTuple(OPEROID,
467                                                          ObjectIdGetDatum(opno),
468                                                          0, 0, 0);
469         if (HeapTupleIsValid(tp))
470         {
471                 Form_pg_operator optup = (Form_pg_operator) GETSTRUCT(tp);
472
473                 return optup->oprjoin;
474         }
475         else
476                 return (RegProcedure) NULL;
477 }
478
479 /*                              ---------- FUNCTION CACHE ----------                                     */
480
481 /*
482  * get_func_rettype
483  *              Given procedure id, return the function's result type.
484  */
485 Oid
486 get_func_rettype(Oid funcid)
487 {
488         HeapTuple       func_tuple;
489
490         func_tuple = SearchSysCacheTuple(PROCOID,
491                                                                          ObjectIdGetDatum(funcid),
492                                                                          0, 0, 0);
493         if (!HeapTupleIsValid(func_tuple))
494                 elog(ERROR, "Function OID %u does not exist", funcid);
495
496         return ((Form_pg_proc) GETSTRUCT(func_tuple))->prorettype;
497 }
498
499 /*                              ---------- RELATION CACHE ----------                                     */
500
501 #ifdef NOT_USED
502 /*
503  * get_relnatts
504  *
505  *              Returns the number of attributes for a given relation.
506  */
507 int
508 get_relnatts(Oid relid)
509 {
510         HeapTuple       tp;
511
512         tp = SearchSysCacheTuple(RELOID,
513                                                          ObjectIdGetDatum(relid),
514                                                          0, 0, 0);
515         if (HeapTupleIsValid(tp))
516         {
517                 Form_pg_class reltup = (Form_pg_class) GETSTRUCT(tp);
518
519                 return reltup->relnatts;
520         }
521         else
522                 return InvalidAttrNumber;
523 }
524 #endif
525
526 /*
527  * get_rel_name
528  *
529  *              Returns the name of a given relation.
530  */
531 char *
532 get_rel_name(Oid relid)
533 {
534         HeapTuple       tp;
535
536         tp = SearchSysCacheTuple(RELOID,
537                                                          ObjectIdGetDatum(relid),
538                                                          0, 0, 0);
539         if (HeapTupleIsValid(tp))
540         {
541                 Form_pg_class reltup = (Form_pg_class) GETSTRUCT(tp);
542
543                 return pstrdup(NameStr(reltup->relname));
544         }
545         else
546                 return NULL;
547 }
548
549 /*                              ---------- TYPE CACHE ----------                                                 */
550
551 /*
552  * get_typlen
553  *
554  *              Given the type OID, return the length of the type.
555  */
556 int16
557 get_typlen(Oid typid)
558 {
559         HeapTuple       tp;
560
561         tp = SearchSysCacheTuple(TYPEOID,
562                                                          ObjectIdGetDatum(typid),
563                                                          0, 0, 0);
564         if (HeapTupleIsValid(tp))
565         {
566                 Form_pg_type typtup = (Form_pg_type) GETSTRUCT(tp);
567
568                 return typtup->typlen;
569         }
570         else
571                 return 0;
572 }
573
574 /*
575  * get_typbyval
576  *
577  *              Given the type OID, determine whether the type is returned by value or
578  *              not.  Returns true if by value, false if by reference.
579  */
580 bool
581 get_typbyval(Oid typid)
582 {
583         HeapTuple       tp;
584
585         tp = SearchSysCacheTuple(TYPEOID,
586                                                          ObjectIdGetDatum(typid),
587                                                          0, 0, 0);
588         if (HeapTupleIsValid(tp))
589         {
590                 Form_pg_type typtup = (Form_pg_type) GETSTRUCT(tp);
591
592                 return typtup->typbyval;
593         }
594         else
595                 return false;
596 }
597
598 #ifdef NOT_USED
599 char
600 get_typalign(Oid typid)
601 {
602         HeapTuple       tp;
603
604         tp = SearchSysCacheTuple(TYPEOID,
605                                                          ObjectIdGetDatum(typid),
606                                                          0, 0, 0);
607         if (HeapTupleIsValid(tp))
608         {
609                 Form_pg_type typtup = (Form_pg_type) GETSTRUCT(tp);
610
611                 return typtup->typalign;
612         }
613         else
614                 return 'i';
615 }
616
617 #endif
618
619 /*
620  * get_typdefault
621  *
622  *        Given a type OID, return the typdefault field associated with that
623  *        type, or Datum(NULL) if there is no typdefault.  (This implies
624  *        that pass-by-value types can't have a default value that has
625  *        a representation of zero.  Not worth fixing now.)
626  *        The result points to palloc'd storage for non-pass-by-value types.
627  */
628 Datum
629 get_typdefault(Oid typid)
630 {
631         HeapTuple       typeTuple;
632         Form_pg_type type;
633         struct varlena *typDefault;
634         bool            isNull;
635         int32           dataSize;
636         int32           typLen;
637         bool            typByVal;
638         Datum           returnValue;
639
640         typeTuple = SearchSysCacheTuple(TYPEOID,
641                                                                         ObjectIdGetDatum(typid),
642                                                                         0, 0, 0);
643
644         if (!HeapTupleIsValid(typeTuple))
645                 elog(ERROR, "get_typdefault: failed to lookup type %u", typid);
646
647         type = (Form_pg_type) GETSTRUCT(typeTuple);
648
649         /*
650          * First, see if there is a non-null typdefault field (usually there
651          * isn't)
652          */
653         typDefault = (struct varlena *) SysCacheGetAttr(TYPEOID,
654                                                                                                         typeTuple,
655                                                                                                  Anum_pg_type_typdefault,
656                                                                                                         &isNull);
657
658         if (isNull)
659                 return PointerGetDatum(NULL);
660
661         /*
662          * Otherwise, extract/copy the value.
663          */
664         dataSize = VARSIZE(typDefault) - VARHDRSZ;
665         typLen = type->typlen;
666         typByVal = type->typbyval;
667
668         if (typByVal)
669         {
670                 int8            i8;
671                 int16           i16;
672                 int32           i32 = 0;
673
674                 if (dataSize == typLen)
675                 {
676                         switch (typLen)
677                         {
678                                 case sizeof(int8):
679                                         memcpy((char *) &i8, VARDATA(typDefault), sizeof(int8));
680                                         i32 = i8;
681                                         break;
682                                 case sizeof(int16):
683                                         memcpy((char *) &i16, VARDATA(typDefault), sizeof(int16));
684                                         i32 = i16;
685                                         break;
686                                 case sizeof(int32):
687                                         memcpy((char *) &i32, VARDATA(typDefault), sizeof(int32));
688                                         break;
689                         }
690                         returnValue = Int32GetDatum(i32);
691                 }
692                 else
693                         returnValue = PointerGetDatum(NULL);
694         }
695         else if (typLen < 0)
696         {
697                 /* variable-size type */
698                 if (dataSize < 0)
699                         returnValue = PointerGetDatum(NULL);
700                 else
701                 {
702                         returnValue = PointerGetDatum(palloc(VARSIZE(typDefault)));
703                         memcpy((char *) DatumGetPointer(returnValue),
704                                    (char *) typDefault,
705                                    (int) VARSIZE(typDefault));
706                 }
707         }
708         else
709         {
710                 /* fixed-size pass-by-ref type */
711                 if (dataSize != typLen)
712                         returnValue = PointerGetDatum(NULL);
713                 else
714                 {
715                         returnValue = PointerGetDatum(palloc(dataSize));
716                         memcpy((char *) DatumGetPointer(returnValue),
717                                    VARDATA(typDefault),
718                                    (int) dataSize);
719                 }
720         }
721
722         return returnValue;
723 }
724
725 /*
726  * get_typtype
727  *
728  *              Given the type OID, find if it is a basic type, a named relation
729  *              or the generic type 'relation'.
730  *              It returns the null char if the cache lookup fails...
731  */
732 #ifdef NOT_USED
733 char
734 get_typtype(Oid typid)
735 {
736         HeapTuple       tp;
737
738         tp = SearchSysCacheTuple(TYPEOID,
739                                                          ObjectIdGetDatum(typid),
740                                                          0, 0, 0);
741         if (HeapTupleIsValid(tp))
742         {
743                 Form_pg_type typtup = (Form_pg_type) GETSTRUCT(tp);
744
745                 return typtup->typtype;
746         }
747         else
748                 return '\0';
749 }
750
751 #endif