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