PostgreSQL Source Code git master
Loading...
Searching...
No Matches
lsyscache.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * lsyscache.c
4 * Convenience routines for common queries in the system catalog cache.
5 *
6 * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
8 *
9 * IDENTIFICATION
10 * src/backend/utils/cache/lsyscache.c
11 *
12 * NOTES
13 * Eventually, the index information should go through here, too.
14 *-------------------------------------------------------------------------
15 */
16#include "postgres.h"
17
18#include "access/hash.h"
19#include "access/htup_details.h"
20#include "bootstrap/bootstrap.h"
21#include "catalog/namespace.h"
22#include "catalog/pg_am.h"
23#include "catalog/pg_amop.h"
24#include "catalog/pg_amproc.h"
25#include "catalog/pg_cast.h"
26#include "catalog/pg_class.h"
29#include "catalog/pg_database.h"
30#include "catalog/pg_index.h"
31#include "catalog/pg_language.h"
33#include "catalog/pg_opclass.h"
34#include "catalog/pg_opfamily.h"
35#include "catalog/pg_operator.h"
36#include "catalog/pg_proc.h"
40#include "catalog/pg_range.h"
44#include "catalog/pg_type.h"
45#include "miscadmin.h"
46#include "nodes/makefuncs.h"
47#include "utils/array.h"
48#include "utils/builtins.h"
49#include "utils/catcache.h"
50#include "utils/datum.h"
51#include "utils/fmgroids.h"
52#include "utils/lsyscache.h"
53#include "utils/syscache.h"
54#include "utils/typcache.h"
55
56/* Hook for plugins to get control in get_attavgwidth() */
58
59
60/* ---------- AMOP CACHES ---------- */
61
62/*
63 * op_in_opfamily
64 *
65 * Return t iff operator 'opno' is in operator family 'opfamily'.
66 *
67 * This function only considers search operators, not ordering operators.
68 */
69bool
70op_in_opfamily(Oid opno, Oid opfamily)
71{
73 ObjectIdGetDatum(opno),
75 ObjectIdGetDatum(opfamily));
76}
77
78/*
79 * get_op_opfamily_strategy
80 *
81 * Get the operator's strategy number within the specified opfamily,
82 * or 0 if it's not a member of the opfamily.
83 *
84 * This function only considers search operators, not ordering operators.
85 */
86int
88{
89 HeapTuple tp;
91 int result;
92
94 ObjectIdGetDatum(opno),
96 ObjectIdGetDatum(opfamily));
97 if (!HeapTupleIsValid(tp))
98 return 0;
100 result = amop_tup->amopstrategy;
101 ReleaseSysCache(tp);
102 return result;
103}
104
105/*
106 * get_op_opfamily_sortfamily
107 *
108 * If the operator is an ordering operator within the specified opfamily,
109 * return its amopsortfamily OID; else return InvalidOid.
110 */
111Oid
113{
114 HeapTuple tp;
116 Oid result;
117
119 ObjectIdGetDatum(opno),
121 ObjectIdGetDatum(opfamily));
122 if (!HeapTupleIsValid(tp))
123 return InvalidOid;
125 result = amop_tup->amopsortfamily;
126 ReleaseSysCache(tp);
127 return result;
128}
129
130/*
131 * get_op_opfamily_properties
132 *
133 * Get the operator's strategy number and declared input data types
134 * within the specified opfamily.
135 *
136 * Caller should already have verified that opno is a member of opfamily,
137 * therefore we raise an error if the tuple is not found.
138 */
139void
141 int *strategy,
142 Oid *lefttype,
143 Oid *righttype)
144{
145 HeapTuple tp;
147
149 ObjectIdGetDatum(opno),
151 ObjectIdGetDatum(opfamily));
152 if (!HeapTupleIsValid(tp))
153 elog(ERROR, "operator %u is not a member of opfamily %u",
154 opno, opfamily);
156 *strategy = amop_tup->amopstrategy;
157 *lefttype = amop_tup->amoplefttype;
158 *righttype = amop_tup->amoprighttype;
159 ReleaseSysCache(tp);
160}
161
162/*
163 * get_opfamily_member
164 * Get the OID of the operator that implements the specified strategy
165 * with the specified datatypes for the specified opfamily.
166 *
167 * Returns InvalidOid if there is no pg_amop entry for the given keys.
168 */
169Oid
170get_opfamily_member(Oid opfamily, Oid lefttype, Oid righttype,
171 int16 strategy)
172{
173 HeapTuple tp;
175 Oid result;
176
178 ObjectIdGetDatum(opfamily),
179 ObjectIdGetDatum(lefttype),
180 ObjectIdGetDatum(righttype),
181 Int16GetDatum(strategy));
182 if (!HeapTupleIsValid(tp))
183 return InvalidOid;
185 result = amop_tup->amopopr;
186 ReleaseSysCache(tp);
187 return result;
188}
189
190/*
191 * get_opfamily_member_for_cmptype
192 * Get the OID of the operator that implements the specified comparison
193 * type with the specified datatypes for the specified opfamily.
194 *
195 * Returns InvalidOid if there is no mapping for the comparison type or no
196 * pg_amop entry for the given keys.
197 */
198Oid
199get_opfamily_member_for_cmptype(Oid opfamily, Oid lefttype, Oid righttype,
200 CompareType cmptype)
201{
203 StrategyNumber strategy;
204
205 opmethod = get_opfamily_method(opfamily);
206 strategy = IndexAmTranslateCompareType(cmptype, opmethod, opfamily, true);
207 if (!strategy)
208 return InvalidOid;
209 return get_opfamily_member(opfamily, lefttype, righttype, strategy);
210}
211
212/*
213 * get_opmethod_canorder
214 * Return amcanorder field for given index AM.
215 *
216 * To speed things up in the common cases, we're hardcoding the results from
217 * the built-in index types. Note that we also need to hardcode the negative
218 * results from the built-in non-btree index types, since you'll usually get a
219 * few hits for those as well. It would be nice to organize and cache this a
220 * bit differently to avoid the hardcoding.
221 */
222static bool
224{
225 switch (amoid)
226 {
227 case BTREE_AM_OID:
228 return true;
229 case HASH_AM_OID:
230 case GIST_AM_OID:
231 case GIN_AM_OID:
232 case SPGIST_AM_OID:
233 case BRIN_AM_OID:
234 return false;
235 default:
237 }
238}
239
240/*
241 * get_ordering_op_properties
242 * Given the OID of an ordering operator (a "<" or ">" operator),
243 * determine its opfamily, its declared input datatype, and its
244 * comparison type.
245 *
246 * Returns true if successful, false if no matching pg_amop entry exists.
247 * (This indicates that the operator is not a valid ordering operator.)
248 *
249 * Note: the operator could be registered in multiple families, for example
250 * if someone were to build a "reverse sort" opfamily. This would result in
251 * uncertainty as to whether "ORDER BY USING op" would default to NULLS FIRST
252 * or NULLS LAST, as well as inefficient planning due to failure to match up
253 * pathkeys that should be the same. So we want a determinate result here.
254 * Because of the way the syscache search works, we'll use the interpretation
255 * associated with the opfamily with smallest OID, which is probably
256 * determinate enough. Since there is no longer any particularly good reason
257 * to build reverse-sort opfamilies, it doesn't seem worth expending any
258 * additional effort on ensuring consistency.
259 */
260bool
262 Oid *opfamily, Oid *opcintype, CompareType *cmptype)
263{
264 bool result = false;
266 int i;
267
268 /* ensure outputs are initialized on failure */
269 *opfamily = InvalidOid;
270 *opcintype = InvalidOid;
271 *cmptype = COMPARE_INVALID;
272
273 /*
274 * Search pg_amop to see if the target operator is registered as the "<"
275 * or ">" operator of any btree opfamily.
276 */
278
279 for (i = 0; i < catlist->n_members; i++)
280 {
281 HeapTuple tuple = &catlist->members[i]->tuple;
284
285 /* must be ordering index */
286 if (!get_opmethod_canorder(aform->amopmethod))
287 continue;
288
290 aform->amopmethod,
291 aform->amopfamily,
292 true);
293
295 {
296 /* Found it ... should have consistent input types */
297 if (aform->amoplefttype == aform->amoprighttype)
298 {
299 /* Found a suitable opfamily, return info */
300 *opfamily = aform->amopfamily;
301 *opcintype = aform->amoplefttype;
302 *cmptype = am_cmptype;
303 result = true;
304 break;
305 }
306 }
307 }
308
310
311 return result;
312}
313
314/*
315 * get_equality_op_for_ordering_op
316 * Get the OID of the datatype-specific equality operator
317 * associated with an ordering operator (a "<" or ">" operator).
318 *
319 * If "reverse" isn't NULL, also set *reverse to false if the operator is "<",
320 * true if it's ">"
321 *
322 * Returns InvalidOid if no matching equality operator can be found.
323 * (This indicates that the operator is not a valid ordering operator.)
324 */
325Oid
327{
329 Oid opfamily;
330 Oid opcintype;
331 CompareType cmptype;
332
333 /* Find the operator in pg_amop */
335 &opfamily, &opcintype, &cmptype))
336 {
337 /* Found a suitable opfamily, get matching equality operator */
339 opcintype,
340 opcintype,
341 COMPARE_EQ);
342 if (reverse)
343 *reverse = (cmptype == COMPARE_GT);
344 }
345
346 return result;
347}
348
349/*
350 * get_ordering_op_for_equality_op
351 * Get the OID of a datatype-specific "less than" ordering operator
352 * associated with an equality operator. (If there are multiple
353 * possibilities, assume any one will do.)
354 *
355 * This function is used when we have to sort data before unique-ifying,
356 * and don't much care which sorting op is used as long as it's compatible
357 * with the intended equality operator. Since we need a sorting operator,
358 * it should be single-data-type even if the given operator is cross-type.
359 * The caller specifies whether to find an op for the LHS or RHS data type.
360 *
361 * Returns InvalidOid if no matching ordering operator can be found.
362 */
363Oid
365{
368 int i;
369
370 /*
371 * Search pg_amop to see if the target operator is registered as the "="
372 * operator of any btree opfamily.
373 */
375
376 for (i = 0; i < catlist->n_members; i++)
377 {
378 HeapTuple tuple = &catlist->members[i]->tuple;
380 CompareType cmptype;
381
382 /* must be ordering index */
383 if (!get_opmethod_canorder(aform->amopmethod))
384 continue;
385
386 cmptype = IndexAmTranslateStrategy(aform->amopstrategy,
387 aform->amopmethod,
388 aform->amopfamily,
389 true);
390 if (cmptype == COMPARE_EQ)
391 {
392 /* Found a suitable opfamily, get matching ordering operator */
393 Oid typid;
394
395 typid = use_lhs_type ? aform->amoplefttype : aform->amoprighttype;
397 typid, typid,
398 COMPARE_LT);
399 if (OidIsValid(result))
400 break;
401 /* failure probably shouldn't happen, but keep looking if so */
402 }
403 }
404
406
407 return result;
408}
409
410/*
411 * get_mergejoin_opfamilies
412 * Given a putatively mergejoinable operator, return a list of the OIDs
413 * of the amcanorder opfamilies in which it represents equality.
414 *
415 * It is possible (though at present unusual) for an operator to be equality
416 * in more than one opfamily, hence the result is a list. This also lets us
417 * return NIL if the operator is not found in any opfamilies.
418 *
419 * The planner currently uses simple equal() tests to compare the lists
420 * returned by this function, which makes the list order relevant, though
421 * strictly speaking it should not be. Because of the way syscache list
422 * searches are handled, in normal operation the result will be sorted by OID
423 * so everything works fine. If running with system index usage disabled,
424 * the result ordering is unspecified and hence the planner might fail to
425 * recognize optimization opportunities ... but that's hardly a scenario in
426 * which performance is good anyway, so there's no point in expending code
427 * or cycles here to guarantee the ordering in that case.
428 */
429List *
431{
432 List *result = NIL;
434 int i;
435
436 /*
437 * Search pg_amop to see if the target operator is registered as the "="
438 * operator of any opfamily of an ordering index type.
439 */
441
442 for (i = 0; i < catlist->n_members; i++)
443 {
444 HeapTuple tuple = &catlist->members[i]->tuple;
446
447 /* must be ordering index equality */
448 if (get_opmethod_canorder(aform->amopmethod) &&
449 IndexAmTranslateStrategy(aform->amopstrategy,
450 aform->amopmethod,
451 aform->amopfamily,
452 true) == COMPARE_EQ)
453 result = lappend_oid(result, aform->amopfamily);
454 }
455
457
458 return result;
459}
460
461/*
462 * get_compatible_hash_operators
463 * Get the OID(s) of hash equality operator(s) compatible with the given
464 * operator, but operating on its LHS and/or RHS datatype.
465 *
466 * An operator for the LHS type is sought and returned into *lhs_opno if
467 * lhs_opno isn't NULL. Similarly, an operator for the RHS type is sought
468 * and returned into *rhs_opno if rhs_opno isn't NULL.
469 *
470 * If the given operator is not cross-type, the results should be the same
471 * operator, but in cross-type situations they will be different.
472 *
473 * Returns true if able to find the requested operator(s), false if not.
474 * (This indicates that the operator should not have been marked oprcanhash.)
475 */
476bool
479{
480 bool result = false;
482 int i;
483
484 /* Ensure output args are initialized on failure */
485 if (lhs_opno)
487 if (rhs_opno)
489
490 /*
491 * Search pg_amop to see if the target operator is registered as the "="
492 * operator of any hash opfamily. If the operator is registered in
493 * multiple opfamilies, assume we can use any one.
494 */
496
497 for (i = 0; i < catlist->n_members; i++)
498 {
499 HeapTuple tuple = &catlist->members[i]->tuple;
501
502 if (aform->amopmethod == HASH_AM_OID &&
503 aform->amopstrategy == HTEqualStrategyNumber)
504 {
505 /* No extra lookup needed if given operator is single-type */
506 if (aform->amoplefttype == aform->amoprighttype)
507 {
508 if (lhs_opno)
509 *lhs_opno = opno;
510 if (rhs_opno)
511 *rhs_opno = opno;
512 result = true;
513 break;
514 }
515
516 /*
517 * Get the matching single-type operator(s). Failure probably
518 * shouldn't happen --- it implies a bogus opfamily --- but
519 * continue looking if so.
520 */
521 if (lhs_opno)
522 {
523 *lhs_opno = get_opfamily_member(aform->amopfamily,
524 aform->amoplefttype,
525 aform->amoplefttype,
527 if (!OidIsValid(*lhs_opno))
528 continue;
529 /* Matching LHS found, done if caller doesn't want RHS */
530 if (!rhs_opno)
531 {
532 result = true;
533 break;
534 }
535 }
536 if (rhs_opno)
537 {
538 *rhs_opno = get_opfamily_member(aform->amopfamily,
539 aform->amoprighttype,
540 aform->amoprighttype,
542 if (!OidIsValid(*rhs_opno))
543 {
544 /* Forget any LHS operator from this opfamily */
545 if (lhs_opno)
547 continue;
548 }
549 /* Matching RHS found, so done */
550 result = true;
551 break;
552 }
553 }
554 }
555
557
558 return result;
559}
560
561/*
562 * get_op_hash_functions
563 * Get the OID(s) of the standard hash support function(s) compatible with
564 * the given operator, operating on its LHS and/or RHS datatype as required.
565 *
566 * A function for the LHS type is sought and returned into *lhs_procno if
567 * lhs_procno isn't NULL. Similarly, a function for the RHS type is sought
568 * and returned into *rhs_procno if rhs_procno isn't NULL.
569 *
570 * If the given operator is not cross-type, the results should be the same
571 * function, but in cross-type situations they will be different.
572 *
573 * Returns true if able to find the requested function(s), false if not.
574 * (This indicates that the operator should not have been marked oprcanhash.)
575 */
576bool
579{
580 bool result = false;
582 int i;
583
584 /* Ensure output args are initialized on failure */
585 if (lhs_procno)
587 if (rhs_procno)
589
590 /*
591 * Search pg_amop to see if the target operator is registered as the "="
592 * operator of any hash opfamily. If the operator is registered in
593 * multiple opfamilies, assume we can use any one.
594 */
596
597 for (i = 0; i < catlist->n_members; i++)
598 {
599 HeapTuple tuple = &catlist->members[i]->tuple;
601
602 if (aform->amopmethod == HASH_AM_OID &&
603 aform->amopstrategy == HTEqualStrategyNumber)
604 {
605 /*
606 * Get the matching support function(s). Failure probably
607 * shouldn't happen --- it implies a bogus opfamily --- but
608 * continue looking if so.
609 */
610 if (lhs_procno)
611 {
612 *lhs_procno = get_opfamily_proc(aform->amopfamily,
613 aform->amoplefttype,
614 aform->amoplefttype,
616 if (!OidIsValid(*lhs_procno))
617 continue;
618 /* Matching LHS found, done if caller doesn't want RHS */
619 if (!rhs_procno)
620 {
621 result = true;
622 break;
623 }
624 /* Only one lookup needed if given operator is single-type */
625 if (aform->amoplefttype == aform->amoprighttype)
626 {
628 result = true;
629 break;
630 }
631 }
632 if (rhs_procno)
633 {
634 *rhs_procno = get_opfamily_proc(aform->amopfamily,
635 aform->amoprighttype,
636 aform->amoprighttype,
638 if (!OidIsValid(*rhs_procno))
639 {
640 /* Forget any LHS function from this opfamily */
641 if (lhs_procno)
643 continue;
644 }
645 /* Matching RHS found, so done */
646 result = true;
647 break;
648 }
649 }
650 }
651
653
654 return result;
655}
656
657/*
658 * get_op_index_interpretation
659 * Given an operator's OID, find out which amcanorder opfamilies it belongs to,
660 * and what properties it has within each one. The results are returned
661 * as a palloc'd list of OpIndexInterpretation structs.
662 *
663 * In addition to the normal btree operators, we consider a <> operator to be
664 * a "member" of an opfamily if its negator is an equality operator of the
665 * opfamily. COMPARE_NE is returned as the strategy number for this case.
666 */
667List *
669{
670 List *result = NIL;
673 int i;
674
675 /*
676 * Find all the pg_amop entries containing the operator.
677 */
679
680 for (i = 0; i < catlist->n_members; i++)
681 {
682 HeapTuple op_tuple = &catlist->members[i]->tuple;
684 CompareType cmptype;
685
686 /* must be ordering index */
687 if (!get_opmethod_canorder(op_form->amopmethod))
688 continue;
689
690 /* Get the operator's comparison type */
691 cmptype = IndexAmTranslateStrategy(op_form->amopstrategy,
692 op_form->amopmethod,
693 op_form->amopfamily,
694 true);
695
696 /* should not happen */
697 if (cmptype == COMPARE_INVALID)
698 continue;
699
701 thisresult->opfamily_id = op_form->amopfamily;
702 thisresult->cmptype = cmptype;
703 thisresult->oplefttype = op_form->amoplefttype;
704 thisresult->oprighttype = op_form->amoprighttype;
706 }
707
709
710 /*
711 * If we didn't find any btree opfamily containing the operator, perhaps
712 * it is a <> operator. See if it has a negator that is in an opfamily.
713 */
714 if (result == NIL)
715 {
716 Oid op_negator = get_negator(opno);
717
719 {
722
723 for (i = 0; i < catlist->n_members; i++)
724 {
725 HeapTuple op_tuple = &catlist->members[i]->tuple;
727 const IndexAmRoutine *amroutine = GetIndexAmRoutineByAmId(op_form->amopmethod, false);
728 CompareType cmptype;
729
730 /* must be ordering index */
731 if (!amroutine->amcanorder)
732 continue;
733
734 /* Get the operator's comparison type */
735 cmptype = IndexAmTranslateStrategy(op_form->amopstrategy,
736 op_form->amopmethod,
737 op_form->amopfamily,
738 true);
739
740 /* Only consider negators that are = */
741 if (cmptype != COMPARE_EQ)
742 continue;
743
744 /* OK, report it as COMPARE_NE */
746 thisresult->opfamily_id = op_form->amopfamily;
747 thisresult->cmptype = COMPARE_NE;
748 thisresult->oplefttype = op_form->amoplefttype;
749 thisresult->oprighttype = op_form->amoprighttype;
751 }
752
754 }
755 }
756
757 return result;
758}
759
760/*
761 * equality_ops_are_compatible
762 * Return true if the two given equality operators have compatible
763 * semantics.
764 *
765 * This is trivially true if they are the same operator. Otherwise,
766 * we look to see if they both belong to an opfamily that guarantees
767 * compatible semantics for equality. Either finding allows us to assume
768 * that they have compatible notions of equality. (The reason we need
769 * to do these pushups is that one might be a cross-type operator; for
770 * instance int24eq vs int4eq.)
771 */
772bool
774{
775 bool result;
777 int i;
778
779 /* Easy if they're the same operator */
780 if (opno1 == opno2)
781 return true;
782
783 /*
784 * We search through all the pg_amop entries for opno1.
785 */
787
788 result = false;
789 for (i = 0; i < catlist->n_members; i++)
790 {
791 HeapTuple op_tuple = &catlist->members[i]->tuple;
793
794 /*
795 * op_in_opfamily() is cheaper than GetIndexAmRoutineByAmId(), so
796 * check it first
797 */
798 if (op_in_opfamily(opno2, op_form->amopfamily) &&
800 {
801 result = true;
802 break;
803 }
804 }
805
807
808 return result;
809}
810
811/*
812 * comparison_ops_are_compatible
813 * Return true if the two given comparison operators have compatible
814 * semantics.
815 *
816 * This is trivially true if they are the same operator. Otherwise, we look
817 * to see if they both belong to an opfamily that guarantees compatible
818 * semantics for ordering. (For example, for btree, '<' and '>=' ops match if
819 * they belong to the same family.)
820 *
821 * (This is identical to equality_ops_are_compatible(), except that we check
822 * amconsistentordering instead of amconsistentequality.)
823 */
824bool
826{
827 bool result;
829 int i;
830
831 /* Easy if they're the same operator */
832 if (opno1 == opno2)
833 return true;
834
835 /*
836 * We search through all the pg_amop entries for opno1.
837 */
839
840 result = false;
841 for (i = 0; i < catlist->n_members; i++)
842 {
843 HeapTuple op_tuple = &catlist->members[i]->tuple;
845
846 /*
847 * op_in_opfamily() is cheaper than GetIndexAmRoutineByAmId(), so
848 * check it first
849 */
850 if (op_in_opfamily(opno2, op_form->amopfamily) &&
852 {
853 result = true;
854 break;
855 }
856 }
857
859
860 return result;
861}
862
863/*
864 * op_is_safe_index_member
865 * Check if the operator is a member of a B-tree or Hash operator family.
866 *
867 * We use this check as a proxy for "null-safety": if an operator is trusted by
868 * the btree or hash opfamily, it implies that the operator adheres to standard
869 * boolean behavior, and would not return NULL when given valid non-null
870 * inputs, as doing so would break index integrity.
871 */
872bool
874{
875 bool result = false;
877 int i;
878
879 /*
880 * Search pg_amop to see if the target operator is registered for any
881 * btree or hash opfamily.
882 */
884
885 for (i = 0; i < catlist->n_members; i++)
886 {
887 HeapTuple tuple = &catlist->members[i]->tuple;
889
890 /* Check if the AM is B-tree or Hash */
891 if (aform->amopmethod == BTREE_AM_OID ||
892 aform->amopmethod == HASH_AM_OID)
893 {
894 result = true;
895 break;
896 }
897 }
898
900
901 return result;
902}
903
904
905/* ---------- AMPROC CACHES ---------- */
906
907/*
908 * get_opfamily_proc
909 * Get the OID of the specified support function
910 * for the specified opfamily and datatypes.
911 *
912 * Returns InvalidOid if there is no pg_amproc entry for the given keys.
913 */
914Oid
915get_opfamily_proc(Oid opfamily, Oid lefttype, Oid righttype, int16 procnum)
916{
917 HeapTuple tp;
920
922 ObjectIdGetDatum(opfamily),
923 ObjectIdGetDatum(lefttype),
924 ObjectIdGetDatum(righttype),
926 if (!HeapTupleIsValid(tp))
927 return InvalidOid;
929 result = amproc_tup->amproc;
930 ReleaseSysCache(tp);
931 return result;
932}
933
934
935/* ---------- ATTRIBUTE CACHES ---------- */
936
937/*
938 * get_attname
939 * Given the relation id and the attribute number, return the "attname"
940 * field from the attribute relation as a palloc'ed string.
941 *
942 * If no such attribute exists and missing_ok is true, NULL is returned;
943 * otherwise a not-intended-for-user-consumption error is thrown.
944 */
945char *
946get_attname(Oid relid, AttrNumber attnum, bool missing_ok)
947{
948 HeapTuple tp;
949
952 if (HeapTupleIsValid(tp))
953 {
955 char *result;
956
957 result = pstrdup(NameStr(att_tup->attname));
958 ReleaseSysCache(tp);
959 return result;
960 }
961
962 if (!missing_ok)
963 elog(ERROR, "cache lookup failed for attribute %d of relation %u",
964 attnum, relid);
965 return NULL;
966}
967
968/*
969 * get_attnum
970 *
971 * Given the relation id and the attribute name,
972 * return the "attnum" field from the attribute relation.
973 *
974 * Returns InvalidAttrNumber if the attr doesn't exist (or is dropped).
975 */
977get_attnum(Oid relid, const char *attname)
978{
979 HeapTuple tp;
980
981 tp = SearchSysCacheAttName(relid, attname);
982 if (HeapTupleIsValid(tp))
983 {
986
987 result = att_tup->attnum;
988 ReleaseSysCache(tp);
989 return result;
990 }
991 else
992 return InvalidAttrNumber;
993}
994
995/*
996 * get_attgenerated
997 *
998 * Given the relation id and the attribute number,
999 * return the "attgenerated" field from the attribute relation.
1000 *
1001 * Errors if not found.
1002 *
1003 * Since not generated is represented by '\0', this can also be used as a
1004 * Boolean test.
1005 */
1006char
1008{
1009 HeapTuple tp;
1011 char result;
1012
1014 ObjectIdGetDatum(relid),
1016 if (!HeapTupleIsValid(tp))
1017 elog(ERROR, "cache lookup failed for attribute %d of relation %u",
1018 attnum, relid);
1020 result = att_tup->attgenerated;
1021 ReleaseSysCache(tp);
1022 return result;
1023}
1024
1025/*
1026 * get_atttype
1027 *
1028 * Given the relation OID and the attribute number with the relation,
1029 * return the attribute type OID.
1030 */
1031Oid
1033{
1034 HeapTuple tp;
1035
1037 ObjectIdGetDatum(relid),
1039 if (HeapTupleIsValid(tp))
1040 {
1042 Oid result;
1043
1044 result = att_tup->atttypid;
1045 ReleaseSysCache(tp);
1046 return result;
1047 }
1048 else
1049 return InvalidOid;
1050}
1051
1052/*
1053 * get_atttypetypmodcoll
1054 *
1055 * A three-fer: given the relation id and the attribute number,
1056 * fetch atttypid, atttypmod, and attcollation in a single cache lookup.
1057 *
1058 * Unlike the otherwise-similar get_atttype, this routine
1059 * raises an error if it can't obtain the information.
1060 */
1061void
1063 Oid *typid, int32 *typmod, Oid *collid)
1064{
1065 HeapTuple tp;
1067
1069 ObjectIdGetDatum(relid),
1071 if (!HeapTupleIsValid(tp))
1072 elog(ERROR, "cache lookup failed for attribute %d of relation %u",
1073 attnum, relid);
1075
1076 *typid = att_tup->atttypid;
1077 *typmod = att_tup->atttypmod;
1078 *collid = att_tup->attcollation;
1079 ReleaseSysCache(tp);
1080}
1081
1082/*
1083 * get_attoptions
1084 *
1085 * Given the relation id and the attribute number,
1086 * return the attribute options text[] datum, if any.
1087 */
1088Datum
1090{
1091 HeapTuple tuple;
1092 Datum attopts;
1093 Datum result;
1094 bool isnull;
1095
1096 tuple = SearchSysCache2(ATTNUM,
1097 ObjectIdGetDatum(relid),
1099
1100 if (!HeapTupleIsValid(tuple))
1101 elog(ERROR, "cache lookup failed for attribute %d of relation %u",
1102 attnum, relid);
1103
1105 &isnull);
1106
1107 if (isnull)
1108 result = (Datum) 0;
1109 else
1110 result = datumCopy(attopts, false, -1); /* text[] */
1111
1112 ReleaseSysCache(tuple);
1113
1114 return result;
1115}
1116
1117/* ---------- PG_CAST CACHE ---------- */
1118
1119/*
1120 * get_cast_oid - given two type OIDs, look up a cast OID
1121 *
1122 * If missing_ok is false, throw an error if the cast is not found. If
1123 * true, just return InvalidOid.
1124 */
1125Oid
1127{
1128 Oid oid;
1129
1133 if (!OidIsValid(oid) && !missing_ok)
1134 ereport(ERROR,
1136 errmsg("cast from type %s to type %s does not exist",
1139 return oid;
1140}
1141
1142/* ---------- COLLATION CACHE ---------- */
1143
1144/*
1145 * get_collation_name
1146 * Returns the name of a given pg_collation entry.
1147 *
1148 * Returns a palloc'd copy of the string, or NULL if no such collation.
1149 *
1150 * NOTE: since collation name is not unique, be wary of code that uses this
1151 * for anything except preparing error messages.
1152 */
1153char *
1155{
1156 HeapTuple tp;
1157
1159 if (HeapTupleIsValid(tp))
1160 {
1162 char *result;
1163
1164 result = pstrdup(NameStr(colltup->collname));
1165 ReleaseSysCache(tp);
1166 return result;
1167 }
1168 else
1169 return NULL;
1170}
1171
1172bool
1174{
1175 HeapTuple tp;
1177 bool result;
1178
1180 if (!HeapTupleIsValid(tp))
1181 elog(ERROR, "cache lookup failed for collation %u", colloid);
1183 result = colltup->collisdeterministic;
1184 ReleaseSysCache(tp);
1185 return result;
1186}
1187
1188/* ---------- CONSTRAINT CACHE ---------- */
1189
1190/*
1191 * get_constraint_name
1192 * Returns the name of a given pg_constraint entry.
1193 *
1194 * Returns a palloc'd copy of the string, or NULL if no such constraint.
1195 *
1196 * NOTE: since constraint name is not unique, be wary of code that uses this
1197 * for anything except preparing error messages.
1198 */
1199char *
1201{
1202 HeapTuple tp;
1203
1205 if (HeapTupleIsValid(tp))
1206 {
1208 char *result;
1209
1210 result = pstrdup(NameStr(contup->conname));
1211 ReleaseSysCache(tp);
1212 return result;
1213 }
1214 else
1215 return NULL;
1216}
1217
1218/*
1219 * get_constraint_index
1220 * Given the OID of a unique, primary-key, or exclusion constraint,
1221 * return the OID of the underlying index.
1222 *
1223 * Returns InvalidOid if the constraint could not be found or is of
1224 * the wrong type.
1225 *
1226 * The intent of this function is to return the index "owned" by the
1227 * specified constraint. Therefore we must check contype, since some
1228 * pg_constraint entries (e.g. for foreign-key constraints) store the
1229 * OID of an index that is referenced but not owned by the constraint.
1230 */
1231Oid
1233{
1234 HeapTuple tp;
1235
1237 if (HeapTupleIsValid(tp))
1238 {
1240 Oid result;
1241
1242 if (contup->contype == CONSTRAINT_UNIQUE ||
1243 contup->contype == CONSTRAINT_PRIMARY ||
1244 contup->contype == CONSTRAINT_EXCLUSION)
1245 result = contup->conindid;
1246 else
1248 ReleaseSysCache(tp);
1249 return result;
1250 }
1251 else
1252 return InvalidOid;
1253}
1254
1255/*
1256 * get_constraint_type
1257 * Return the pg_constraint.contype value for the given constraint.
1258 *
1259 * No frills.
1260 */
1261char
1263{
1264 HeapTuple tp;
1265 char contype;
1266
1268 if (!HeapTupleIsValid(tp))
1269 elog(ERROR, "cache lookup failed for constraint %u", conoid);
1270
1271 contype = ((Form_pg_constraint) GETSTRUCT(tp))->contype;
1272 ReleaseSysCache(tp);
1273
1274 return contype;
1275}
1276
1277/* ---------- DATABASE CACHE ---------- */
1278
1279/*
1280 * get_database_name - given a database OID, look up the name
1281 *
1282 * Returns a palloc'd string, or NULL if no such database.
1283 */
1284char *
1286{
1288 char *result;
1289
1292 {
1295 }
1296 else
1297 result = NULL;
1298
1299 return result;
1300}
1301
1302
1303/* ---------- LANGUAGE CACHE ---------- */
1304
1305char *
1307{
1308 HeapTuple tp;
1309
1311 if (HeapTupleIsValid(tp))
1312 {
1314 char *result;
1315
1316 result = pstrdup(NameStr(lantup->lanname));
1317 ReleaseSysCache(tp);
1318 return result;
1319 }
1320
1321 if (!missing_ok)
1322 elog(ERROR, "cache lookup failed for language %u",
1323 langoid);
1324 return NULL;
1325}
1326
1327/* ---------- OPCLASS CACHE ---------- */
1328
1329/*
1330 * get_opclass_family
1331 *
1332 * Returns the OID of the operator family the opclass belongs to.
1333 */
1334Oid
1336{
1337 HeapTuple tp;
1339 Oid result;
1340
1341 tp = SearchSysCache1(CLAOID, ObjectIdGetDatum(opclass));
1342 if (!HeapTupleIsValid(tp))
1343 elog(ERROR, "cache lookup failed for opclass %u", opclass);
1345
1346 result = cla_tup->opcfamily;
1347 ReleaseSysCache(tp);
1348 return result;
1349}
1350
1351/*
1352 * get_opclass_input_type
1353 *
1354 * Returns the OID of the datatype the opclass indexes.
1355 */
1356Oid
1358{
1359 HeapTuple tp;
1361 Oid result;
1362
1363 tp = SearchSysCache1(CLAOID, ObjectIdGetDatum(opclass));
1364 if (!HeapTupleIsValid(tp))
1365 elog(ERROR, "cache lookup failed for opclass %u", opclass);
1367
1368 result = cla_tup->opcintype;
1369 ReleaseSysCache(tp);
1370 return result;
1371}
1372
1373/*
1374 * get_opclass_opfamily_and_input_type
1375 *
1376 * Returns the OID of the operator family the opclass belongs to,
1377 * the OID of the datatype the opclass indexes
1378 */
1379bool
1380get_opclass_opfamily_and_input_type(Oid opclass, Oid *opfamily, Oid *opcintype)
1381{
1382 HeapTuple tp;
1384
1385 tp = SearchSysCache1(CLAOID, ObjectIdGetDatum(opclass));
1386 if (!HeapTupleIsValid(tp))
1387 return false;
1388
1390
1391 *opfamily = cla_tup->opcfamily;
1392 *opcintype = cla_tup->opcintype;
1393
1394 ReleaseSysCache(tp);
1395
1396 return true;
1397}
1398
1399/*
1400 * get_opclass_method
1401 *
1402 * Returns the OID of the index access method the opclass belongs to.
1403 */
1404Oid
1406{
1407 HeapTuple tp;
1409 Oid result;
1410
1411 tp = SearchSysCache1(CLAOID, ObjectIdGetDatum(opclass));
1412 if (!HeapTupleIsValid(tp))
1413 elog(ERROR, "cache lookup failed for opclass %u", opclass);
1415
1416 result = cla_tup->opcmethod;
1417 ReleaseSysCache(tp);
1418 return result;
1419}
1420
1421/* ---------- OPFAMILY CACHE ---------- */
1422
1423/*
1424 * get_opfamily_method
1425 *
1426 * Returns the OID of the index access method the opfamily is for.
1427 */
1428Oid
1430{
1431 HeapTuple tp;
1433 Oid result;
1434
1436 if (!HeapTupleIsValid(tp))
1437 elog(ERROR, "cache lookup failed for operator family %u", opfid);
1439
1440 result = opfform->opfmethod;
1441 ReleaseSysCache(tp);
1442 return result;
1443}
1444
1445char *
1446get_opfamily_name(Oid opfid, bool missing_ok)
1447{
1448 HeapTuple tup;
1449 char *opfname;
1451
1453
1454 if (!HeapTupleIsValid(tup))
1455 {
1456 if (!missing_ok)
1457 elog(ERROR, "cache lookup failed for operator family %u", opfid);
1458 return NULL;
1459 }
1460
1462 opfname = pstrdup(NameStr(opfform->opfname));
1463
1465
1466 return opfname;
1467}
1468
1469/* ---------- OPERATOR CACHE ---------- */
1470
1471/*
1472 * get_opcode
1473 *
1474 * Returns the regproc id of the routine used to implement an
1475 * operator given the operator oid.
1476 */
1479{
1480 HeapTuple tp;
1481
1483 if (HeapTupleIsValid(tp))
1484 {
1487
1488 result = optup->oprcode;
1489 ReleaseSysCache(tp);
1490 return result;
1491 }
1492 else
1493 return (RegProcedure) InvalidOid;
1494}
1495
1496/*
1497 * get_opname
1498 * returns the name of the operator with the given opno
1499 *
1500 * Note: returns a palloc'd copy of the string, or NULL if no such operator.
1501 */
1502char *
1504{
1505 HeapTuple tp;
1506
1508 if (HeapTupleIsValid(tp))
1509 {
1511 char *result;
1512
1513 result = pstrdup(NameStr(optup->oprname));
1514 ReleaseSysCache(tp);
1515 return result;
1516 }
1517 else
1518 return NULL;
1519}
1520
1521/*
1522 * get_op_rettype
1523 * Given operator oid, return the operator's result type.
1524 */
1525Oid
1527{
1528 HeapTuple tp;
1529
1531 if (HeapTupleIsValid(tp))
1532 {
1534 Oid result;
1535
1536 result = optup->oprresult;
1537 ReleaseSysCache(tp);
1538 return result;
1539 }
1540 else
1541 return InvalidOid;
1542}
1543
1544/*
1545 * op_input_types
1546 *
1547 * Returns the left and right input datatypes for an operator
1548 * (InvalidOid if not relevant).
1549 */
1550void
1551op_input_types(Oid opno, Oid *lefttype, Oid *righttype)
1552{
1553 HeapTuple tp;
1555
1557 if (!HeapTupleIsValid(tp)) /* shouldn't happen */
1558 elog(ERROR, "cache lookup failed for operator %u", opno);
1560 *lefttype = optup->oprleft;
1561 *righttype = optup->oprright;
1562 ReleaseSysCache(tp);
1563}
1564
1565/*
1566 * op_mergejoinable
1567 *
1568 * Returns true if the operator is potentially mergejoinable. (The planner
1569 * will fail to find any mergejoin plans unless there are suitable btree
1570 * opfamily entries for this operator and associated sortops. The pg_operator
1571 * flag is just a hint to tell the planner whether to bother looking.)
1572 *
1573 * In some cases (currently only array_eq and record_eq), mergejoinability
1574 * depends on the specific input data type the operator is invoked for, so
1575 * that must be passed as well. We currently assume that only one input's type
1576 * is needed to check this --- by convention, pass the left input's data type.
1577 */
1578bool
1579op_mergejoinable(Oid opno, Oid inputtype)
1580{
1581 bool result = false;
1582 HeapTuple tp;
1583 TypeCacheEntry *typentry;
1584
1585 /*
1586 * For array_eq or record_eq, we can sort if the element or field types
1587 * are all sortable. We could implement all the checks for that here, but
1588 * the typcache already does that and caches the results too, so let's
1589 * rely on the typcache.
1590 */
1591 if (opno == ARRAY_EQ_OP)
1592 {
1593 typentry = lookup_type_cache(inputtype, TYPECACHE_CMP_PROC);
1594 if (typentry->cmp_proc == F_BTARRAYCMP)
1595 result = true;
1596 }
1597 else if (opno == RECORD_EQ_OP)
1598 {
1599 typentry = lookup_type_cache(inputtype, TYPECACHE_CMP_PROC);
1600 if (typentry->cmp_proc == F_BTRECORDCMP)
1601 result = true;
1602 }
1603 else
1604 {
1605 /* For all other operators, rely on pg_operator.oprcanmerge */
1607 if (HeapTupleIsValid(tp))
1608 {
1610
1611 result = optup->oprcanmerge;
1612 ReleaseSysCache(tp);
1613 }
1614 }
1615 return result;
1616}
1617
1618/*
1619 * op_hashjoinable
1620 *
1621 * Returns true if the operator is hashjoinable. (There must be a suitable
1622 * hash opfamily entry for this operator if it is so marked.)
1623 *
1624 * In some cases (currently only array_eq), hashjoinability depends on the
1625 * specific input data type the operator is invoked for, so that must be
1626 * passed as well. We currently assume that only one input's type is needed
1627 * to check this --- by convention, pass the left input's data type.
1628 */
1629bool
1630op_hashjoinable(Oid opno, Oid inputtype)
1631{
1632 bool result = false;
1633 HeapTuple tp;
1634 TypeCacheEntry *typentry;
1635
1636 /* As in op_mergejoinable, let the typcache handle the hard cases */
1637 if (opno == ARRAY_EQ_OP)
1638 {
1639 typentry = lookup_type_cache(inputtype, TYPECACHE_HASH_PROC);
1640 if (typentry->hash_proc == F_HASH_ARRAY)
1641 result = true;
1642 }
1643 else if (opno == RECORD_EQ_OP)
1644 {
1645 typentry = lookup_type_cache(inputtype, TYPECACHE_HASH_PROC);
1646 if (typentry->hash_proc == F_HASH_RECORD)
1647 result = true;
1648 }
1649 else
1650 {
1651 /* For all other operators, rely on pg_operator.oprcanhash */
1653 if (HeapTupleIsValid(tp))
1654 {
1656
1657 result = optup->oprcanhash;
1658 ReleaseSysCache(tp);
1659 }
1660 }
1661 return result;
1662}
1663
1664/*
1665 * op_strict
1666 *
1667 * Get the proisstrict flag for the operator's underlying function.
1668 */
1669bool
1671{
1672 RegProcedure funcid = get_opcode(opno);
1673
1674 if (funcid == (RegProcedure) InvalidOid)
1675 elog(ERROR, "operator %u does not exist", opno);
1676
1677 return func_strict((Oid) funcid);
1678}
1679
1680/*
1681 * op_volatile
1682 *
1683 * Get the provolatile flag for the operator's underlying function.
1684 */
1685char
1687{
1688 RegProcedure funcid = get_opcode(opno);
1689
1690 if (funcid == (RegProcedure) InvalidOid)
1691 elog(ERROR, "operator %u does not exist", opno);
1692
1693 return func_volatile((Oid) funcid);
1694}
1695
1696/*
1697 * get_commutator
1698 *
1699 * Returns the corresponding commutator of an operator.
1700 */
1701Oid
1703{
1704 HeapTuple tp;
1705
1707 if (HeapTupleIsValid(tp))
1708 {
1710 Oid result;
1711
1712 result = optup->oprcom;
1713 ReleaseSysCache(tp);
1714 return result;
1715 }
1716 else
1717 return InvalidOid;
1718}
1719
1720/*
1721 * get_negator
1722 *
1723 * Returns the corresponding negator of an operator.
1724 */
1725Oid
1727{
1728 HeapTuple tp;
1729
1731 if (HeapTupleIsValid(tp))
1732 {
1734 Oid result;
1735
1736 result = optup->oprnegate;
1737 ReleaseSysCache(tp);
1738 return result;
1739 }
1740 else
1741 return InvalidOid;
1742}
1743
1744/*
1745 * get_oprrest
1746 *
1747 * Returns procedure id for computing selectivity of an operator.
1748 */
1751{
1752 HeapTuple tp;
1753
1755 if (HeapTupleIsValid(tp))
1756 {
1759
1760 result = optup->oprrest;
1761 ReleaseSysCache(tp);
1762 return result;
1763 }
1764 else
1765 return (RegProcedure) InvalidOid;
1766}
1767
1768/*
1769 * get_oprjoin
1770 *
1771 * Returns procedure id for computing selectivity of a join.
1772 */
1775{
1776 HeapTuple tp;
1777
1779 if (HeapTupleIsValid(tp))
1780 {
1783
1784 result = optup->oprjoin;
1785 ReleaseSysCache(tp);
1786 return result;
1787 }
1788 else
1789 return (RegProcedure) InvalidOid;
1790}
1791
1792/* ---------- FUNCTION CACHE ---------- */
1793
1794/*
1795 * get_func_name
1796 * returns the name of the function with the given funcid
1797 *
1798 * Note: returns a palloc'd copy of the string, or NULL if no such function.
1799 */
1800char *
1802{
1803 HeapTuple tp;
1804
1806 if (HeapTupleIsValid(tp))
1807 {
1809 char *result;
1810
1811 result = pstrdup(NameStr(functup->proname));
1812 ReleaseSysCache(tp);
1813 return result;
1814 }
1815 else
1816 return NULL;
1817}
1818
1819/*
1820 * get_func_namespace
1821 *
1822 * Returns the pg_namespace OID associated with a given function.
1823 */
1824Oid
1826{
1827 HeapTuple tp;
1828
1830 if (HeapTupleIsValid(tp))
1831 {
1833 Oid result;
1834
1835 result = functup->pronamespace;
1836 ReleaseSysCache(tp);
1837 return result;
1838 }
1839 else
1840 return InvalidOid;
1841}
1842
1843/*
1844 * get_func_rettype
1845 * Given procedure id, return the function's result type.
1846 */
1847Oid
1849{
1850 HeapTuple tp;
1851 Oid result;
1852
1854 if (!HeapTupleIsValid(tp))
1855 elog(ERROR, "cache lookup failed for function %u", funcid);
1856
1857 result = ((Form_pg_proc) GETSTRUCT(tp))->prorettype;
1858 ReleaseSysCache(tp);
1859 return result;
1860}
1861
1862/*
1863 * get_func_nargs
1864 * Given procedure id, return the number of arguments.
1865 */
1866int
1868{
1869 HeapTuple tp;
1870 int result;
1871
1873 if (!HeapTupleIsValid(tp))
1874 elog(ERROR, "cache lookup failed for function %u", funcid);
1875
1877 ReleaseSysCache(tp);
1878 return result;
1879}
1880
1881/*
1882 * get_func_signature
1883 * Given procedure id, return the function's argument and result types.
1884 * (The return value is the result type.)
1885 *
1886 * The arguments are returned as a palloc'd array.
1887 */
1888Oid
1889get_func_signature(Oid funcid, Oid **argtypes, int *nargs)
1890{
1891 HeapTuple tp;
1893 Oid result;
1894
1896 if (!HeapTupleIsValid(tp))
1897 elog(ERROR, "cache lookup failed for function %u", funcid);
1898
1900
1901 result = procstruct->prorettype;
1902 *nargs = (int) procstruct->pronargs;
1903 Assert(*nargs == procstruct->proargtypes.dim1);
1904 *argtypes = (Oid *) palloc(*nargs * sizeof(Oid));
1905 memcpy(*argtypes, procstruct->proargtypes.values, *nargs * sizeof(Oid));
1906
1907 ReleaseSysCache(tp);
1908 return result;
1909}
1910
1911/*
1912 * get_func_variadictype
1913 * Given procedure id, return the function's provariadic field.
1914 */
1915Oid
1917{
1918 HeapTuple tp;
1919 Oid result;
1920
1922 if (!HeapTupleIsValid(tp))
1923 elog(ERROR, "cache lookup failed for function %u", funcid);
1924
1926 ReleaseSysCache(tp);
1927 return result;
1928}
1929
1930/*
1931 * get_func_retset
1932 * Given procedure id, return the function's proretset flag.
1933 */
1934bool
1936{
1937 HeapTuple tp;
1938 bool result;
1939
1941 if (!HeapTupleIsValid(tp))
1942 elog(ERROR, "cache lookup failed for function %u", funcid);
1943
1945 ReleaseSysCache(tp);
1946 return result;
1947}
1948
1949/*
1950 * func_strict
1951 * Given procedure id, return the function's proisstrict flag.
1952 */
1953bool
1955{
1956 HeapTuple tp;
1957 bool result;
1958
1960 if (!HeapTupleIsValid(tp))
1961 elog(ERROR, "cache lookup failed for function %u", funcid);
1962
1964 ReleaseSysCache(tp);
1965 return result;
1966}
1967
1968/*
1969 * func_volatile
1970 * Given procedure id, return the function's provolatile flag.
1971 */
1972char
1974{
1975 HeapTuple tp;
1976 char result;
1977
1979 if (!HeapTupleIsValid(tp))
1980 elog(ERROR, "cache lookup failed for function %u", funcid);
1981
1983 ReleaseSysCache(tp);
1984 return result;
1985}
1986
1987/*
1988 * func_parallel
1989 * Given procedure id, return the function's proparallel flag.
1990 */
1991char
1993{
1994 HeapTuple tp;
1995 char result;
1996
1998 if (!HeapTupleIsValid(tp))
1999 elog(ERROR, "cache lookup failed for function %u", funcid);
2000
2002 ReleaseSysCache(tp);
2003 return result;
2004}
2005
2006/*
2007 * get_func_prokind
2008 * Given procedure id, return the routine kind.
2009 */
2010char
2012{
2013 HeapTuple tp;
2014 char result;
2015
2017 if (!HeapTupleIsValid(tp))
2018 elog(ERROR, "cache lookup failed for function %u", funcid);
2019
2020 result = ((Form_pg_proc) GETSTRUCT(tp))->prokind;
2021 ReleaseSysCache(tp);
2022 return result;
2023}
2024
2025/*
2026 * get_func_leakproof
2027 * Given procedure id, return the function's leakproof field.
2028 */
2029bool
2031{
2032 HeapTuple tp;
2033 bool result;
2034
2036 if (!HeapTupleIsValid(tp))
2037 elog(ERROR, "cache lookup failed for function %u", funcid);
2038
2040 ReleaseSysCache(tp);
2041 return result;
2042}
2043
2044/*
2045 * get_func_support
2046 *
2047 * Returns the support function OID associated with a given function,
2048 * or InvalidOid if there is none.
2049 */
2052{
2053 HeapTuple tp;
2054
2056 if (HeapTupleIsValid(tp))
2057 {
2060
2061 result = functup->prosupport;
2062 ReleaseSysCache(tp);
2063 return result;
2064 }
2065 else
2066 return (RegProcedure) InvalidOid;
2067}
2068
2069/* ---------- RELATION CACHE ---------- */
2070
2071/*
2072 * get_relname_relid
2073 * Given name and namespace of a relation, look up the OID.
2074 *
2075 * Returns InvalidOid if there is no such relation.
2076 */
2077Oid
2078get_relname_relid(const char *relname, Oid relnamespace)
2079{
2082 ObjectIdGetDatum(relnamespace));
2083}
2084
2085#ifdef NOT_USED
2086/*
2087 * get_relnatts
2088 *
2089 * Returns the number of attributes for a given relation.
2090 */
2091int
2092get_relnatts(Oid relid)
2093{
2094 HeapTuple tp;
2095
2097 if (HeapTupleIsValid(tp))
2098 {
2100 int result;
2101
2102 result = reltup->relnatts;
2103 ReleaseSysCache(tp);
2104 return result;
2105 }
2106 else
2107 return InvalidAttrNumber;
2108}
2109#endif
2110
2111/*
2112 * get_rel_name
2113 * Returns the name of a given relation.
2114 *
2115 * Returns a palloc'd copy of the string, or NULL if no such relation.
2116 *
2117 * NOTE: since relation name is not unique, be wary of code that uses this
2118 * for anything except preparing error messages.
2119 */
2120char *
2122{
2123 HeapTuple tp;
2124
2126 if (HeapTupleIsValid(tp))
2127 {
2129 char *result;
2130
2131 result = pstrdup(NameStr(reltup->relname));
2132 ReleaseSysCache(tp);
2133 return result;
2134 }
2135 else
2136 return NULL;
2137}
2138
2139/*
2140 * get_rel_namespace
2141 *
2142 * Returns the pg_namespace OID associated with a given relation.
2143 */
2144Oid
2146{
2147 HeapTuple tp;
2148
2150 if (HeapTupleIsValid(tp))
2151 {
2153 Oid result;
2154
2155 result = reltup->relnamespace;
2156 ReleaseSysCache(tp);
2157 return result;
2158 }
2159 else
2160 return InvalidOid;
2161}
2162
2163/*
2164 * get_rel_type_id
2165 *
2166 * Returns the pg_type OID associated with a given relation.
2167 *
2168 * Note: not all pg_class entries have associated pg_type OIDs; so be
2169 * careful to check for InvalidOid result.
2170 */
2171Oid
2173{
2174 HeapTuple tp;
2175
2177 if (HeapTupleIsValid(tp))
2178 {
2180 Oid result;
2181
2182 result = reltup->reltype;
2183 ReleaseSysCache(tp);
2184 return result;
2185 }
2186 else
2187 return InvalidOid;
2188}
2189
2190/*
2191 * get_rel_relkind
2192 *
2193 * Returns the relkind associated with a given relation.
2194 */
2195char
2197{
2198 HeapTuple tp;
2199
2201 if (HeapTupleIsValid(tp))
2202 {
2204 char result;
2205
2206 result = reltup->relkind;
2207 ReleaseSysCache(tp);
2208 return result;
2209 }
2210 else
2211 return '\0';
2212}
2213
2214/*
2215 * get_rel_relispartition
2216 *
2217 * Returns the relispartition flag associated with a given relation.
2218 */
2219bool
2221{
2222 HeapTuple tp;
2223
2225 if (HeapTupleIsValid(tp))
2226 {
2228 bool result;
2229
2230 result = reltup->relispartition;
2231 ReleaseSysCache(tp);
2232 return result;
2233 }
2234 else
2235 return false;
2236}
2237
2238/*
2239 * get_rel_tablespace
2240 *
2241 * Returns the pg_tablespace OID associated with a given relation.
2242 *
2243 * Note: InvalidOid might mean either that we couldn't find the relation,
2244 * or that it is in the database's default tablespace.
2245 */
2246Oid
2248{
2249 HeapTuple tp;
2250
2252 if (HeapTupleIsValid(tp))
2253 {
2255 Oid result;
2256
2257 result = reltup->reltablespace;
2258 ReleaseSysCache(tp);
2259 return result;
2260 }
2261 else
2262 return InvalidOid;
2263}
2264
2265/*
2266 * get_rel_persistence
2267 *
2268 * Returns the relpersistence associated with a given relation.
2269 */
2270char
2272{
2273 HeapTuple tp;
2275 char result;
2276
2278 if (!HeapTupleIsValid(tp))
2279 elog(ERROR, "cache lookup failed for relation %u", relid);
2281 result = reltup->relpersistence;
2282 ReleaseSysCache(tp);
2283
2284 return result;
2285}
2286
2287/*
2288 * get_rel_relam
2289 *
2290 * Returns the relam associated with a given relation.
2291 */
2292Oid
2294{
2295 HeapTuple tp;
2297 Oid result;
2298
2300 if (!HeapTupleIsValid(tp))
2301 elog(ERROR, "cache lookup failed for relation %u", relid);
2303 result = reltup->relam;
2304 ReleaseSysCache(tp);
2305
2306 return result;
2307}
2308
2309
2310/* ---------- TRANSFORM CACHE ---------- */
2311
2312Oid
2313get_transform_fromsql(Oid typid, Oid langid, List *trftypes)
2314{
2315 HeapTuple tup;
2316
2317 if (!list_member_oid(trftypes, typid))
2318 return InvalidOid;
2319
2321 ObjectIdGetDatum(langid));
2322 if (HeapTupleIsValid(tup))
2323 {
2324 Oid funcid;
2325
2326 funcid = ((Form_pg_transform) GETSTRUCT(tup))->trffromsql;
2328 return funcid;
2329 }
2330 else
2331 return InvalidOid;
2332}
2333
2334Oid
2335get_transform_tosql(Oid typid, Oid langid, List *trftypes)
2336{
2337 HeapTuple tup;
2338
2339 if (!list_member_oid(trftypes, typid))
2340 return InvalidOid;
2341
2343 ObjectIdGetDatum(langid));
2344 if (HeapTupleIsValid(tup))
2345 {
2346 Oid funcid;
2347
2348 funcid = ((Form_pg_transform) GETSTRUCT(tup))->trftosql;
2350 return funcid;
2351 }
2352 else
2353 return InvalidOid;
2354}
2355
2356
2357/* ---------- TYPE CACHE ---------- */
2358
2359/*
2360 * get_typisdefined
2361 *
2362 * Given the type OID, determine whether the type is defined
2363 * (if not, it's only a shell).
2364 */
2365bool
2367{
2368 HeapTuple tp;
2369
2371 if (HeapTupleIsValid(tp))
2372 {
2374 bool result;
2375
2376 result = typtup->typisdefined;
2377 ReleaseSysCache(tp);
2378 return result;
2379 }
2380 else
2381 return false;
2382}
2383
2384/*
2385 * get_typlen
2386 *
2387 * Given the type OID, return the length of the type.
2388 */
2389int16
2391{
2392 HeapTuple tp;
2393
2395 if (HeapTupleIsValid(tp))
2396 {
2398 int16 result;
2399
2400 result = typtup->typlen;
2401 ReleaseSysCache(tp);
2402 return result;
2403 }
2404 else
2405 return 0;
2406}
2407
2408/*
2409 * get_typbyval
2410 *
2411 * Given the type OID, determine whether the type is returned by value or
2412 * not. Returns true if by value, false if by reference.
2413 */
2414bool
2416{
2417 HeapTuple tp;
2418
2420 if (HeapTupleIsValid(tp))
2421 {
2423 bool result;
2424
2425 result = typtup->typbyval;
2426 ReleaseSysCache(tp);
2427 return result;
2428 }
2429 else
2430 return false;
2431}
2432
2433/*
2434 * get_typlenbyval
2435 *
2436 * A two-fer: given the type OID, return both typlen and typbyval.
2437 *
2438 * Since both pieces of info are needed to know how to copy a Datum,
2439 * many places need both. Might as well get them with one cache lookup
2440 * instead of two. Also, this routine raises an error instead of
2441 * returning a bogus value when given a bad type OID.
2442 */
2443void
2444get_typlenbyval(Oid typid, int16 *typlen, bool *typbyval)
2445{
2446 HeapTuple tp;
2448
2450 if (!HeapTupleIsValid(tp))
2451 elog(ERROR, "cache lookup failed for type %u", typid);
2453 *typlen = typtup->typlen;
2454 *typbyval = typtup->typbyval;
2455 ReleaseSysCache(tp);
2456}
2457
2458/*
2459 * get_typlenbyvalalign
2460 *
2461 * A three-fer: given the type OID, return typlen, typbyval, typalign.
2462 */
2463void
2464get_typlenbyvalalign(Oid typid, int16 *typlen, bool *typbyval,
2465 char *typalign)
2466{
2467 HeapTuple tp;
2469
2471 if (!HeapTupleIsValid(tp))
2472 elog(ERROR, "cache lookup failed for type %u", typid);
2474 *typlen = typtup->typlen;
2475 *typbyval = typtup->typbyval;
2476 *typalign = typtup->typalign;
2477 ReleaseSysCache(tp);
2478}
2479
2480/*
2481 * getTypeIOParam
2482 * Given a pg_type row, select the type OID to pass to I/O functions
2483 *
2484 * Formerly, all I/O functions were passed pg_type.typelem as their second
2485 * parameter, but we now have a more complex rule about what to pass.
2486 * This knowledge is intended to be centralized here --- direct references
2487 * to typelem elsewhere in the code are wrong, if they are associated with
2488 * I/O calls and not with actual subscripting operations! (But see
2489 * bootstrap.c's boot_get_type_io_data() if you need to change this.)
2490 *
2491 * As of PostgreSQL 8.1, output functions receive only the value itself
2492 * and not any auxiliary parameters, so the name of this routine is now
2493 * a bit of a misnomer ... it should be getTypeInputParam.
2494 */
2495Oid
2497{
2499
2500 /*
2501 * Array types get their typelem as parameter; everybody else gets their
2502 * own type OID as parameter.
2503 */
2504 if (OidIsValid(typeStruct->typelem))
2505 return typeStruct->typelem;
2506 else
2507 return typeStruct->oid;
2508}
2509
2510/*
2511 * get_type_io_data
2512 *
2513 * A six-fer: given the type OID, return typlen, typbyval, typalign,
2514 * typdelim, typioparam, and IO function OID. The IO function
2515 * returned is controlled by IOFuncSelector
2516 */
2517void
2520 int16 *typlen,
2521 bool *typbyval,
2522 char *typalign,
2523 char *typdelim,
2524 Oid *typioparam,
2525 Oid *func)
2526{
2529
2530 /*
2531 * In bootstrap mode, pass it off to bootstrap.c. This hack allows us to
2532 * use array_in and array_out during bootstrap.
2533 */
2535 {
2536 Oid typinput;
2537 Oid typoutput;
2538 Oid typcollation;
2539
2541 typlen,
2542 typbyval,
2543 typalign,
2544 typdelim,
2545 typioparam,
2546 &typinput,
2547 &typoutput,
2548 &typcollation);
2549 switch (which_func)
2550 {
2551 case IOFunc_input:
2552 *func = typinput;
2553 break;
2554 case IOFunc_output:
2555 *func = typoutput;
2556 break;
2557 default:
2558 elog(ERROR, "binary I/O not supported during bootstrap");
2559 break;
2560 }
2561 return;
2562 }
2563
2566 elog(ERROR, "cache lookup failed for type %u", typid);
2568
2569 *typlen = typeStruct->typlen;
2570 *typbyval = typeStruct->typbyval;
2571 *typalign = typeStruct->typalign;
2572 *typdelim = typeStruct->typdelim;
2573 *typioparam = getTypeIOParam(typeTuple);
2574 switch (which_func)
2575 {
2576 case IOFunc_input:
2577 *func = typeStruct->typinput;
2578 break;
2579 case IOFunc_output:
2580 *func = typeStruct->typoutput;
2581 break;
2582 case IOFunc_receive:
2583 *func = typeStruct->typreceive;
2584 break;
2585 case IOFunc_send:
2586 *func = typeStruct->typsend;
2587 break;
2588 }
2590}
2591
2592#ifdef NOT_USED
2593char
2594get_typalign(Oid typid)
2595{
2596 HeapTuple tp;
2597
2599 if (HeapTupleIsValid(tp))
2600 {
2602 char result;
2603
2604 result = typtup->typalign;
2605 ReleaseSysCache(tp);
2606 return result;
2607 }
2608 else
2609 return TYPALIGN_INT;
2610}
2611#endif
2612
2613char
2615{
2616 HeapTuple tp;
2617
2619 if (HeapTupleIsValid(tp))
2620 {
2622 char result;
2623
2624 result = typtup->typstorage;
2625 ReleaseSysCache(tp);
2626 return result;
2627 }
2628 else
2629 return TYPSTORAGE_PLAIN;
2630}
2631
2632/*
2633 * get_typdefault
2634 * Given a type OID, return the type's default value, if any.
2635 *
2636 * The result is a palloc'd expression node tree, or NULL if there
2637 * is no defined default for the datatype.
2638 *
2639 * NB: caller should be prepared to coerce result to correct datatype;
2640 * the returned expression tree might produce something of the wrong type.
2641 */
2642Node *
2644{
2647 Datum datum;
2648 bool isNull;
2649 Node *expr;
2650
2653 elog(ERROR, "cache lookup failed for type %u", typid);
2655
2656 /*
2657 * typdefault and typdefaultbin are potentially null, so don't try to
2658 * access 'em as struct fields. Must do it the hard way with
2659 * SysCacheGetAttr.
2660 */
2661 datum = SysCacheGetAttr(TYPEOID,
2662 typeTuple,
2664 &isNull);
2665
2666 if (!isNull)
2667 {
2668 /* We have an expression default */
2669 expr = stringToNode(TextDatumGetCString(datum));
2670 }
2671 else
2672 {
2673 /* Perhaps we have a plain literal default */
2674 datum = SysCacheGetAttr(TYPEOID,
2675 typeTuple,
2677 &isNull);
2678
2679 if (!isNull)
2680 {
2681 char *strDefaultVal;
2682
2683 /* Convert text datum to C string */
2685 /* Convert C string to a value of the given type */
2686 datum = OidInputFunctionCall(type->typinput, strDefaultVal,
2688 /* Build a Const node containing the value */
2689 expr = (Node *) makeConst(typid,
2690 -1,
2691 type->typcollation,
2692 type->typlen,
2693 datum,
2694 false,
2695 type->typbyval);
2697 }
2698 else
2699 {
2700 /* No default */
2701 expr = NULL;
2702 }
2703 }
2704
2706
2707 return expr;
2708}
2709
2710/*
2711 * getBaseType
2712 * If the given type is a domain, return its base type;
2713 * otherwise return the type's own OID.
2714 */
2715Oid
2717{
2718 int32 typmod = -1;
2719
2720 return getBaseTypeAndTypmod(typid, &typmod);
2721}
2722
2723/*
2724 * getBaseTypeAndTypmod
2725 * If the given type is a domain, return its base type and typmod;
2726 * otherwise return the type's own OID, and leave *typmod unchanged.
2727 *
2728 * Note that the "applied typmod" should be -1 for every domain level
2729 * above the bottommost; therefore, if the passed-in typid is indeed
2730 * a domain, *typmod should be -1.
2731 */
2732Oid
2734{
2735 /*
2736 * We loop to find the bottom base type in a stack of domains.
2737 */
2738 for (;;)
2739 {
2740 HeapTuple tup;
2742
2744 if (!HeapTupleIsValid(tup))
2745 elog(ERROR, "cache lookup failed for type %u", typid);
2747 if (typTup->typtype != TYPTYPE_DOMAIN)
2748 {
2749 /* Not a domain, so done */
2751 break;
2752 }
2753
2754 Assert(*typmod == -1);
2755 typid = typTup->typbasetype;
2756 *typmod = typTup->typtypmod;
2757
2759 }
2760
2761 return typid;
2762}
2763
2764/*
2765 * get_typavgwidth
2766 *
2767 * Given a type OID and a typmod value (pass -1 if typmod is unknown),
2768 * estimate the average width of values of the type. This is used by
2769 * the planner, which doesn't require absolutely correct results;
2770 * it's OK (and expected) to guess if we don't know for sure.
2771 */
2772int32
2774{
2775 int typlen = get_typlen(typid);
2777
2778 /*
2779 * Easy if it's a fixed-width type
2780 */
2781 if (typlen > 0)
2782 return typlen;
2783
2784 /*
2785 * type_maximum_size knows the encoding of typmod for some datatypes;
2786 * don't duplicate that knowledge here.
2787 */
2788 maxwidth = type_maximum_size(typid, typmod);
2789 if (maxwidth > 0)
2790 {
2791 /*
2792 * For BPCHAR, the max width is also the only width. Otherwise we
2793 * need to guess about the typical data width given the max. A sliding
2794 * scale for percentage of max width seems reasonable.
2795 */
2796 if (typid == BPCHAROID)
2797 return maxwidth;
2798 if (maxwidth <= 32)
2799 return maxwidth; /* assume full width */
2800 if (maxwidth < 1000)
2801 return 32 + (maxwidth - 32) / 2; /* assume 50% */
2802
2803 /*
2804 * Beyond 1000, assume we're looking at something like
2805 * "varchar(10000)" where the limit isn't actually reached often, and
2806 * use a fixed estimate.
2807 */
2808 return 32 + (1000 - 32) / 2;
2809 }
2810
2811 /*
2812 * Oops, we have no idea ... wild guess time.
2813 */
2814 return 32;
2815}
2816
2817/*
2818 * get_typtype
2819 *
2820 * Given the type OID, find if it is a basic type, a complex type, etc.
2821 * It returns the null char if the cache lookup fails...
2822 */
2823char
2825{
2826 HeapTuple tp;
2827
2829 if (HeapTupleIsValid(tp))
2830 {
2832 char result;
2833
2834 result = typtup->typtype;
2835 ReleaseSysCache(tp);
2836 return result;
2837 }
2838 else
2839 return '\0';
2840}
2841
2842/*
2843 * type_is_rowtype
2844 *
2845 * Convenience function to determine whether a type OID represents
2846 * a "rowtype" type --- either RECORD or a named composite type
2847 * (including a domain over a named composite type).
2848 */
2849bool
2851{
2852 if (typid == RECORDOID)
2853 return true; /* easy case */
2854 switch (get_typtype(typid))
2855 {
2856 case TYPTYPE_COMPOSITE:
2857 return true;
2858 case TYPTYPE_DOMAIN:
2860 return true;
2861 break;
2862 default:
2863 break;
2864 }
2865 return false;
2866}
2867
2868/*
2869 * type_is_enum
2870 * Returns true if the given type is an enum type.
2871 */
2872bool
2874{
2875 return (get_typtype(typid) == TYPTYPE_ENUM);
2876}
2877
2878/*
2879 * type_is_range
2880 * Returns true if the given type is a range type.
2881 */
2882bool
2884{
2885 return (get_typtype(typid) == TYPTYPE_RANGE);
2886}
2887
2888/*
2889 * type_is_multirange
2890 * Returns true if the given type is a multirange type.
2891 */
2892bool
2894{
2895 return (get_typtype(typid) == TYPTYPE_MULTIRANGE);
2896}
2897
2898/*
2899 * get_type_category_preferred
2900 *
2901 * Given the type OID, fetch its category and preferred-type status.
2902 * Throws error on failure.
2903 */
2904void
2906{
2907 HeapTuple tp;
2909
2911 if (!HeapTupleIsValid(tp))
2912 elog(ERROR, "cache lookup failed for type %u", typid);
2914 *typcategory = typtup->typcategory;
2915 *typispreferred = typtup->typispreferred;
2916 ReleaseSysCache(tp);
2917}
2918
2919/*
2920 * get_typ_typrelid
2921 *
2922 * Given the type OID, get the typrelid (InvalidOid if not a complex
2923 * type).
2924 */
2925Oid
2927{
2928 HeapTuple tp;
2929
2931 if (HeapTupleIsValid(tp))
2932 {
2934 Oid result;
2935
2936 result = typtup->typrelid;
2937 ReleaseSysCache(tp);
2938 return result;
2939 }
2940 else
2941 return InvalidOid;
2942}
2943
2944/*
2945 * get_element_type
2946 *
2947 * Given the type OID, get the typelem (InvalidOid if not an array type).
2948 *
2949 * NB: this only succeeds for "true" arrays having array_subscript_handler
2950 * as typsubscript. For other types, InvalidOid is returned independently
2951 * of whether they have typelem or typsubscript set.
2952 */
2953Oid
2955{
2956 HeapTuple tp;
2957
2959 if (HeapTupleIsValid(tp))
2960 {
2962 Oid result;
2963
2965 result = typtup->typelem;
2966 else
2968 ReleaseSysCache(tp);
2969 return result;
2970 }
2971 else
2972 return InvalidOid;
2973}
2974
2975/*
2976 * get_array_type
2977 *
2978 * Given the type OID, get the corresponding "true" array type.
2979 * Returns InvalidOid if no array type can be found.
2980 */
2981Oid
2983{
2984 HeapTuple tp;
2986
2988 if (HeapTupleIsValid(tp))
2989 {
2990 result = ((Form_pg_type) GETSTRUCT(tp))->typarray;
2991 ReleaseSysCache(tp);
2992 }
2993 return result;
2994}
2995
2996/*
2997 * get_promoted_array_type
2998 *
2999 * The "promoted" type is what you'd get from an ARRAY(SELECT ...)
3000 * construct, that is, either the corresponding "true" array type
3001 * if the input is a scalar type that has such an array type,
3002 * or the same type if the input is already a "true" array type.
3003 * Returns InvalidOid if neither rule is satisfied.
3004 */
3005Oid
3007{
3008 Oid array_type = get_array_type(typid);
3009
3010 if (OidIsValid(array_type))
3011 return array_type;
3012 if (OidIsValid(get_element_type(typid)))
3013 return typid;
3014 return InvalidOid;
3015}
3016
3017/*
3018 * get_base_element_type
3019 * Given the type OID, get the typelem, looking "through" any domain
3020 * to its underlying array type.
3021 *
3022 * This is equivalent to get_element_type(getBaseType(typid)), but avoids
3023 * an extra cache lookup. Note that it fails to provide any information
3024 * about the typmod of the array.
3025 */
3026Oid
3028{
3029 /*
3030 * We loop to find the bottom base type in a stack of domains.
3031 */
3032 for (;;)
3033 {
3034 HeapTuple tup;
3036
3038 if (!HeapTupleIsValid(tup))
3039 break;
3041 if (typTup->typtype != TYPTYPE_DOMAIN)
3042 {
3043 /* Not a domain, so stop descending */
3044 Oid result;
3045
3046 /* This test must match get_element_type */
3048 result = typTup->typelem;
3049 else
3052 return result;
3053 }
3054
3055 typid = typTup->typbasetype;
3057 }
3058
3059 /* Like get_element_type, silently return InvalidOid for bogus input */
3060 return InvalidOid;
3061}
3062
3063/*
3064 * getTypeInputInfo
3065 *
3066 * Get info needed for converting values of a type to internal form
3067 */
3068void
3070{
3073
3076 elog(ERROR, "cache lookup failed for type %u", type);
3078
3079 if (!pt->typisdefined)
3080 ereport(ERROR,
3082 errmsg("type %s is only a shell",
3083 format_type_be(type))));
3084 if (!OidIsValid(pt->typinput))
3085 ereport(ERROR,
3087 errmsg("no input function available for type %s",
3088 format_type_be(type))));
3089
3090 *typInput = pt->typinput;
3092
3094}
3095
3096/*
3097 * getTypeOutputInfo
3098 *
3099 * Get info needed for printing values of a type
3100 */
3101void
3103{
3106
3109 elog(ERROR, "cache lookup failed for type %u", type);
3111
3112 if (!pt->typisdefined)
3113 ereport(ERROR,
3115 errmsg("type %s is only a shell",
3116 format_type_be(type))));
3117 if (!OidIsValid(pt->typoutput))
3118 ereport(ERROR,
3120 errmsg("no output function available for type %s",
3121 format_type_be(type))));
3122
3123 *typOutput = pt->typoutput;
3124 *typIsVarlena = (!pt->typbyval) && (pt->typlen == -1);
3125
3127}
3128
3129/*
3130 * getTypeBinaryInputInfo
3131 *
3132 * Get info needed for binary input of values of a type
3133 */
3134void
3136{
3139
3142 elog(ERROR, "cache lookup failed for type %u", type);
3144
3145 if (!pt->typisdefined)
3146 ereport(ERROR,
3148 errmsg("type %s is only a shell",
3149 format_type_be(type))));
3150 if (!OidIsValid(pt->typreceive))
3151 ereport(ERROR,
3153 errmsg("no binary input function available for type %s",
3154 format_type_be(type))));
3155
3156 *typReceive = pt->typreceive;
3158
3160}
3161
3162/*
3163 * getTypeBinaryOutputInfo
3164 *
3165 * Get info needed for binary output of values of a type
3166 */
3167void
3169{
3172
3175 elog(ERROR, "cache lookup failed for type %u", type);
3177
3178 if (!pt->typisdefined)
3179 ereport(ERROR,
3181 errmsg("type %s is only a shell",
3182 format_type_be(type))));
3183 if (!OidIsValid(pt->typsend))
3184 ereport(ERROR,
3186 errmsg("no binary output function available for type %s",
3187 format_type_be(type))));
3188
3189 *typSend = pt->typsend;
3190 *typIsVarlena = (!pt->typbyval) && (pt->typlen == -1);
3191
3193}
3194
3195/*
3196 * get_typmodin
3197 *
3198 * Given the type OID, return the type's typmodin procedure, if any.
3199 */
3200Oid
3202{
3203 HeapTuple tp;
3204
3206 if (HeapTupleIsValid(tp))
3207 {
3209 Oid result;
3210
3211 result = typtup->typmodin;
3212 ReleaseSysCache(tp);
3213 return result;
3214 }
3215 else
3216 return InvalidOid;
3217}
3218
3219#ifdef NOT_USED
3220/*
3221 * get_typmodout
3222 *
3223 * Given the type OID, return the type's typmodout procedure, if any.
3224 */
3225Oid
3226get_typmodout(Oid typid)
3227{
3228 HeapTuple tp;
3229
3231 if (HeapTupleIsValid(tp))
3232 {
3234 Oid result;
3235
3236 result = typtup->typmodout;
3237 ReleaseSysCache(tp);
3238 return result;
3239 }
3240 else
3241 return InvalidOid;
3242}
3243#endif /* NOT_USED */
3244
3245/*
3246 * get_typcollation
3247 *
3248 * Given the type OID, return the type's typcollation attribute.
3249 */
3250Oid
3252{
3253 HeapTuple tp;
3254
3256 if (HeapTupleIsValid(tp))
3257 {
3259 Oid result;
3260
3261 result = typtup->typcollation;
3262 ReleaseSysCache(tp);
3263 return result;
3264 }
3265 else
3266 return InvalidOid;
3267}
3268
3269
3270/*
3271 * type_is_collatable
3272 *
3273 * Return whether the type cares about collations
3274 */
3275bool
3277{
3278 return OidIsValid(get_typcollation(typid));
3279}
3280
3281
3282/*
3283 * get_typsubscript
3284 *
3285 * Given the type OID, return the type's subscripting handler's OID,
3286 * if it has one.
3287 *
3288 * If typelemp isn't NULL, we also store the type's typelem value there.
3289 * This saves some callers an extra catalog lookup.
3290 */
3293{
3294 HeapTuple tp;
3295
3297 if (HeapTupleIsValid(tp))
3298 {
3300 RegProcedure handler = typform->typsubscript;
3301
3302 if (typelemp)
3303 *typelemp = typform->typelem;
3304 ReleaseSysCache(tp);
3305 return handler;
3306 }
3307 else
3308 {
3309 if (typelemp)
3311 return InvalidOid;
3312 }
3313}
3314
3315/*
3316 * getSubscriptingRoutines
3317 *
3318 * Given the type OID, fetch the type's subscripting methods struct.
3319 * Return NULL if type is not subscriptable.
3320 *
3321 * If typelemp isn't NULL, we also store the type's typelem value there.
3322 * This saves some callers an extra catalog lookup.
3323 */
3324const struct SubscriptRoutines *
3326{
3327 RegProcedure typsubscript = get_typsubscript(typid, typelemp);
3328
3329 if (!OidIsValid(typsubscript))
3330 return NULL;
3331
3332 return (const struct SubscriptRoutines *)
3333 DatumGetPointer(OidFunctionCall0(typsubscript));
3334}
3335
3336
3337/* ---------- STATISTICS CACHE ---------- */
3338
3339/*
3340 * get_attavgwidth
3341 *
3342 * Given the table and attribute number of a column, get the average
3343 * width of entries in the column. Return zero if no data available.
3344 *
3345 * Currently this is only consulted for individual tables, not for inheritance
3346 * trees, so we don't need an "inh" parameter.
3347 *
3348 * Calling a hook at this point looks somewhat strange, but is required
3349 * because the optimizer calls this function without any other way for
3350 * plug-ins to control the result.
3351 */
3352int32
3354{
3355 HeapTuple tp;
3356 int32 stawidth;
3357
3359 {
3360 stawidth = (*get_attavgwidth_hook) (relid, attnum);
3361 if (stawidth > 0)
3362 return stawidth;
3363 }
3365 ObjectIdGetDatum(relid),
3367 BoolGetDatum(false));
3368 if (HeapTupleIsValid(tp))
3369 {
3370 stawidth = ((Form_pg_statistic) GETSTRUCT(tp))->stawidth;
3371 ReleaseSysCache(tp);
3372 if (stawidth > 0)
3373 return stawidth;
3374 }
3375 return 0;
3376}
3377
3378/*
3379 * get_attstatsslot
3380 *
3381 * Extract the contents of a "slot" of a pg_statistic tuple.
3382 * Returns true if requested slot type was found, else false.
3383 *
3384 * Unlike other routines in this file, this takes a pointer to an
3385 * already-looked-up tuple in the pg_statistic cache. We do this since
3386 * most callers will want to extract more than one value from the cache
3387 * entry, and we don't want to repeat the cache lookup unnecessarily.
3388 * Also, this API allows this routine to be used with statistics tuples
3389 * that have been provided by a stats hook and didn't really come from
3390 * pg_statistic.
3391 *
3392 * sslot: pointer to output area (typically, a local variable in the caller).
3393 * statstuple: pg_statistic tuple to be examined.
3394 * reqkind: STAKIND code for desired statistics slot kind.
3395 * reqop: STAOP value wanted, or InvalidOid if don't care.
3396 * flags: bitmask of ATTSTATSSLOT_VALUES and/or ATTSTATSSLOT_NUMBERS.
3397 *
3398 * If a matching slot is found, true is returned, and *sslot is filled thus:
3399 * staop: receives the actual STAOP value.
3400 * stacoll: receives the actual STACOLL value.
3401 * valuetype: receives actual datatype of the elements of stavalues.
3402 * values: receives pointer to an array of the slot's stavalues.
3403 * nvalues: receives number of stavalues.
3404 * numbers: receives pointer to an array of the slot's stanumbers (as float4).
3405 * nnumbers: receives number of stanumbers.
3406 *
3407 * valuetype/values/nvalues are InvalidOid/NULL/0 if ATTSTATSSLOT_VALUES
3408 * wasn't specified. Likewise, numbers/nnumbers are NULL/0 if
3409 * ATTSTATSSLOT_NUMBERS wasn't specified.
3410 *
3411 * If no matching slot is found, false is returned, and *sslot is zeroed.
3412 *
3413 * Note that the current API doesn't allow for searching for a slot with
3414 * a particular collation. If we ever actually support recording more than
3415 * one collation, we'll have to extend the API, but for now simple is good.
3416 *
3417 * The data referred to by the fields of sslot is locally palloc'd and
3418 * is independent of the original pg_statistic tuple. When the caller
3419 * is done with it, call free_attstatsslot to release the palloc'd data.
3420 *
3421 * If it's desirable to call free_attstatsslot when get_attstatsslot might
3422 * not have been called, memset'ing sslot to zeroes will allow that.
3423 *
3424 * Passing flags=0 can be useful to quickly check if the requested slot type
3425 * exists. In this case no arrays are extracted, so free_attstatsslot need
3426 * not be called.
3427 */
3428bool
3430 int reqkind, Oid reqop, int flags)
3431{
3433 int i;
3434 Datum val;
3437 int narrayelem;
3440
3441 /* initialize *sslot properly */
3442 memset(sslot, 0, sizeof(AttStatsSlot));
3443
3444 for (i = 0; i < STATISTIC_NUM_SLOTS; i++)
3445 {
3446 if ((&stats->stakind1)[i] == reqkind &&
3447 (reqop == InvalidOid || (&stats->staop1)[i] == reqop))
3448 break;
3449 }
3450 if (i >= STATISTIC_NUM_SLOTS)
3451 return false; /* not there */
3452
3453 sslot->staop = (&stats->staop1)[i];
3454 sslot->stacoll = (&stats->stacoll1)[i];
3455
3456 if (flags & ATTSTATSSLOT_VALUES)
3457 {
3460
3461 /*
3462 * Detoast the array if needed, and in any case make a copy that's
3463 * under control of this AttStatsSlot.
3464 */
3466
3467 /*
3468 * Extract the actual array element type, and pass it back in case the
3469 * caller needs it.
3470 */
3471 sslot->valuetype = arrayelemtype = ARR_ELEMTYPE(statarray);
3472
3473 /* Need info about element type */
3476 elog(ERROR, "cache lookup failed for type %u", arrayelemtype);
3478
3479 /* Deconstruct array into Datum elements; NULLs not expected */
3482 typeForm->typlen,
3483 typeForm->typbyval,
3484 typeForm->typalign,
3485 &sslot->values, NULL, &sslot->nvalues);
3486
3487 /*
3488 * If the element type is pass-by-reference, we now have a bunch of
3489 * Datums that are pointers into the statarray, so we need to keep
3490 * that until free_attstatsslot. Otherwise, all the useful info is in
3491 * sslot->values[], so we can free the array object immediately.
3492 */
3493 if (!typeForm->typbyval)
3494 sslot->values_arr = statarray;
3495 else
3497
3499 }
3500
3501 if (flags & ATTSTATSSLOT_NUMBERS)
3502 {
3505
3506 /*
3507 * Detoast the array if needed, and in any case make a copy that's
3508 * under control of this AttStatsSlot.
3509 */
3511
3512 /*
3513 * We expect the array to be a 1-D float4 array; verify that. We don't
3514 * need to use deconstruct_array() since the array data is just going
3515 * to look like a C array of float4 values.
3516 */
3518 if (ARR_NDIM(statarray) != 1 || narrayelem <= 0 ||
3521 elog(ERROR, "stanumbers is not a 1-D float4 array");
3522
3523 /* Give caller a pointer directly into the statarray */
3524 sslot->numbers = (float4 *) ARR_DATA_PTR(statarray);
3525 sslot->nnumbers = narrayelem;
3526
3527 /* We'll free the statarray in free_attstatsslot */
3528 sslot->numbers_arr = statarray;
3529 }
3530
3531 return true;
3532}
3533
3534/*
3535 * free_attstatsslot
3536 * Free data allocated by get_attstatsslot
3537 */
3538void
3540{
3541 /* The values[] array was separately palloc'd by deconstruct_array */
3542 if (sslot->values)
3543 pfree(sslot->values);
3544 /* The numbers[] array points into numbers_arr, do not pfree it */
3545 /* Free the detoasted array objects, if any */
3546 if (sslot->values_arr)
3547 pfree(sslot->values_arr);
3548 if (sslot->numbers_arr)
3549 pfree(sslot->numbers_arr);
3550}
3551
3552/* ---------- PG_NAMESPACE CACHE ---------- */
3553
3554/*
3555 * get_namespace_name
3556 * Returns the name of a given namespace
3557 *
3558 * Returns a palloc'd copy of the string, or NULL if no such namespace.
3559 */
3560char *
3562{
3563 HeapTuple tp;
3564
3566 if (HeapTupleIsValid(tp))
3567 {
3569 char *result;
3570
3571 result = pstrdup(NameStr(nsptup->nspname));
3572 ReleaseSysCache(tp);
3573 return result;
3574 }
3575 else
3576 return NULL;
3577}
3578
3579/*
3580 * get_namespace_name_or_temp
3581 * As above, but if it is this backend's temporary namespace, return
3582 * "pg_temp" instead.
3583 */
3584char *
3586{
3588 return pstrdup("pg_temp");
3589 else
3590 return get_namespace_name(nspid);
3591}
3592
3593/* ---------- PG_RANGE CACHES ---------- */
3594
3595/*
3596 * get_range_subtype
3597 * Returns the subtype of a given range type
3598 *
3599 * Returns InvalidOid if the type is not a range type.
3600 */
3601Oid
3603{
3604 HeapTuple tp;
3605
3607 if (HeapTupleIsValid(tp))
3608 {
3610 Oid result;
3611
3612 result = rngtup->rngsubtype;
3613 ReleaseSysCache(tp);
3614 return result;
3615 }
3616 else
3617 return InvalidOid;
3618}
3619
3620/*
3621 * get_range_collation
3622 * Returns the collation of a given range type
3623 *
3624 * Returns InvalidOid if the type is not a range type,
3625 * or if its subtype is not collatable.
3626 */
3627Oid
3629{
3630 HeapTuple tp;
3631
3633 if (HeapTupleIsValid(tp))
3634 {
3636 Oid result;
3637
3638 result = rngtup->rngcollation;
3639 ReleaseSysCache(tp);
3640 return result;
3641 }
3642 else
3643 return InvalidOid;
3644}
3645
3646/*
3647 * get_range_constructor2
3648 * Gets the 2-arg constructor for the given rangetype.
3649 *
3650 * Raises an error if not found.
3651 */
3654{
3655 HeapTuple tp;
3656
3658 if (HeapTupleIsValid(tp))
3659 {
3662
3663 result = rngtup->rngconstruct2;
3664 ReleaseSysCache(tp);
3665 return result;
3666 }
3667 else
3668 elog(ERROR, "cache lookup failed for range type %u", rangeOid);
3669}
3670
3671/*
3672 * get_range_multirange
3673 * Returns the multirange type of a given range type
3674 *
3675 * Returns InvalidOid if the type is not a range type.
3676 */
3677Oid
3679{
3680 HeapTuple tp;
3681
3683 if (HeapTupleIsValid(tp))
3684 {
3686 Oid result;
3687
3688 result = rngtup->rngmultitypid;
3689 ReleaseSysCache(tp);
3690 return result;
3691 }
3692 else
3693 return InvalidOid;
3694}
3695
3696/*
3697 * get_multirange_range
3698 * Returns the range type of a given multirange
3699 *
3700 * Returns InvalidOid if the type is not a multirange.
3701 */
3702Oid
3704{
3705 HeapTuple tp;
3706
3708 if (HeapTupleIsValid(tp))
3709 {
3711 Oid result;
3712
3713 result = rngtup->rngtypid;
3714 ReleaseSysCache(tp);
3715 return result;
3716 }
3717 else
3718 return InvalidOid;
3719}
3720
3721/* ---------- PG_INDEX CACHE ---------- */
3722
3723/*
3724 * get_index_column_opclass
3725 *
3726 * Given the index OID and column number,
3727 * return opclass of the index column
3728 * or InvalidOid if the index was not found
3729 * or column is non-key one.
3730 */
3731Oid
3733{
3734 HeapTuple tuple;
3735 Form_pg_index rd_index;
3736 Datum datum;
3738 Oid opclass;
3739
3740 /* First we need to know the column's opclass. */
3741
3743 if (!HeapTupleIsValid(tuple))
3744 return InvalidOid;
3745
3746 rd_index = (Form_pg_index) GETSTRUCT(tuple);
3747
3748 /* caller is supposed to guarantee this */
3749 Assert(attno > 0 && attno <= rd_index->indnatts);
3750
3751 /* Non-key attributes don't have an opclass */
3752 if (attno > rd_index->indnkeyatts)
3753 {
3754 ReleaseSysCache(tuple);
3755 return InvalidOid;
3756 }
3757
3759 indclass = ((oidvector *) DatumGetPointer(datum));
3760
3762 opclass = indclass->values[attno - 1];
3763
3764 ReleaseSysCache(tuple);
3765
3766 return opclass;
3767}
3768
3769/*
3770 * get_index_isreplident
3771 *
3772 * Given the index OID, return pg_index.indisreplident.
3773 */
3774bool
3776{
3777 HeapTuple tuple;
3778 Form_pg_index rd_index;
3779 bool result;
3780
3782 if (!HeapTupleIsValid(tuple))
3783 return false;
3784
3785 rd_index = (Form_pg_index) GETSTRUCT(tuple);
3786 result = rd_index->indisreplident;
3787 ReleaseSysCache(tuple);
3788
3789 return result;
3790}
3791
3792/*
3793 * get_index_isvalid
3794 *
3795 * Given the index OID, return pg_index.indisvalid.
3796 */
3797bool
3799{
3800 bool isvalid;
3801 HeapTuple tuple;
3802 Form_pg_index rd_index;
3803
3805 if (!HeapTupleIsValid(tuple))
3806 elog(ERROR, "cache lookup failed for index %u", index_oid);
3807
3808 rd_index = (Form_pg_index) GETSTRUCT(tuple);
3809 isvalid = rd_index->indisvalid;
3810 ReleaseSysCache(tuple);
3811
3812 return isvalid;
3813}
3814
3815/*
3816 * get_index_isclustered
3817 *
3818 * Given the index OID, return pg_index.indisclustered.
3819 */
3820bool
3822{
3823 bool isclustered;
3824 HeapTuple tuple;
3825 Form_pg_index rd_index;
3826
3828 if (!HeapTupleIsValid(tuple))
3829 elog(ERROR, "cache lookup failed for index %u", index_oid);
3830
3831 rd_index = (Form_pg_index) GETSTRUCT(tuple);
3832 isclustered = rd_index->indisclustered;
3833 ReleaseSysCache(tuple);
3834
3835 return isclustered;
3836}
3837
3838/*
3839 * get_publication_oid - given a publication name, look up the OID
3840 *
3841 * If missing_ok is false, throw an error if name not found. If true, just
3842 * return InvalidOid.
3843 */
3844Oid
3845get_publication_oid(const char *pubname, bool missing_ok)
3846{
3847 Oid oid;
3848
3850 CStringGetDatum(pubname));
3851 if (!OidIsValid(oid) && !missing_ok)
3852 ereport(ERROR,
3854 errmsg("publication \"%s\" does not exist", pubname)));
3855 return oid;
3856}
3857
3858/*
3859 * get_publication_name - given a publication Oid, look up the name
3860 *
3861 * If missing_ok is false, throw an error if name not found. If true, just
3862 * return NULL.
3863 */
3864char *
3865get_publication_name(Oid pubid, bool missing_ok)
3866{
3867 HeapTuple tup;
3868 char *pubname;
3870
3872
3873 if (!HeapTupleIsValid(tup))
3874 {
3875 if (!missing_ok)
3876 elog(ERROR, "cache lookup failed for publication %u", pubid);
3877 return NULL;
3878 }
3879
3881 pubname = pstrdup(NameStr(pubform->pubname));
3882
3884
3885 return pubname;
3886}
3887
3888/*
3889 * get_subscription_oid - given a subscription name, look up the OID
3890 *
3891 * If missing_ok is false, throw an error if name not found. If true, just
3892 * return InvalidOid.
3893 */
3894Oid
3895get_subscription_oid(const char *subname, bool missing_ok)
3896{
3897 Oid oid;
3898
3901 if (!OidIsValid(oid) && !missing_ok)
3902 ereport(ERROR,
3904 errmsg("subscription \"%s\" does not exist", subname)));
3905 return oid;
3906}
3907
3908/*
3909 * get_subscription_name - given a subscription OID, look up the name
3910 *
3911 * If missing_ok is false, throw an error if name not found. If true, just
3912 * return NULL.
3913 */
3914char *
3915get_subscription_name(Oid subid, bool missing_ok)
3916{
3917 HeapTuple tup;
3918 char *subname;
3920
3922
3923 if (!HeapTupleIsValid(tup))
3924 {
3925 if (!missing_ok)
3926 elog(ERROR, "cache lookup failed for subscription %u", subid);
3927 return NULL;
3928 }
3929
3931 subname = pstrdup(NameStr(subform->subname));
3932
3934
3935 return subname;
3936}
3937
3938char *
3940{
3941 HeapTuple tuple;
3942 char *labelname;
3943
3945 if (!tuple)
3946 {
3947 elog(ERROR, "cache lookup failed for label %u", labeloid);
3948 return NULL;
3949 }
3951 ReleaseSysCache(tuple);
3952
3953 return labelname;
3954}
3955
3956char *
3958{
3959 HeapTuple tuple;
3960 char *propname;
3961
3963 if (!tuple)
3964 {
3965 elog(ERROR, "cache lookup failed for property %u", propoid);
3966 return NULL;
3967 }
3969 ReleaseSysCache(tuple);
3970
3971 return propname;
3972}
const IndexAmRoutine * GetIndexAmRoutineByAmId(Oid amoid, bool noerror)
Definition amapi.c:69
StrategyNumber IndexAmTranslateCompareType(CompareType cmptype, Oid amoid, Oid opfamily, bool missing_ok)
Definition amapi.c:161
CompareType IndexAmTranslateStrategy(StrategyNumber strategy, Oid amoid, Oid opfamily, bool missing_ok)
Definition amapi.c:131
#define DatumGetArrayTypePCopy(X)
Definition array.h:262
#define ARR_NDIM(a)
Definition array.h:290
#define ARR_DATA_PTR(a)
Definition array.h:322
#define ARR_ELEMTYPE(a)
Definition array.h:292
#define ARR_DIMS(a)
Definition array.h:294
#define ARR_HASNULL(a)
Definition array.h:291
void deconstruct_array(const ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
int16 AttrNumber
Definition attnum.h:21
#define InvalidAttrNumber
Definition attnum.h:23
void boot_get_type_io_data(Oid typid, int16 *typlen, bool *typbyval, char *typalign, char *typdelim, Oid *typioparam, Oid *typinput, Oid *typoutput, Oid *typcollation)
Definition bootstrap.c:1011
#define TextDatumGetCString(d)
Definition builtins.h:99
#define NameStr(name)
Definition c.h:835
#define Assert(condition)
Definition c.h:943
int16_t int16
Definition c.h:619
regproc RegProcedure
Definition c.h:734
int32_t int32
Definition c.h:620
float float4
Definition c.h:713
#define OidIsValid(objectId)
Definition c.h:858
uint32 result
memcpy(sums, checksumBaseOffsets, sizeof(checksumBaseOffsets))
CompareType
Definition cmptype.h:32
@ COMPARE_INVALID
Definition cmptype.h:33
@ COMPARE_GT
Definition cmptype.h:38
@ COMPARE_EQ
Definition cmptype.h:36
@ COMPARE_NE
Definition cmptype.h:39
@ COMPARE_LT
Definition cmptype.h:34
int nspid
Oid collid
Datum datumCopy(Datum value, bool typByVal, int typLen)
Definition datum.c:132
int errcode(int sqlerrcode)
Definition elog.c:875
#define ERROR
Definition elog.h:40
#define elog(elevel,...)
Definition elog.h:228
#define ereport(elevel,...)
Definition elog.h:152
#define palloc_object(type)
Definition fe_memutils.h:74
Datum OidInputFunctionCall(Oid functionId, char *str, Oid typioparam, int32 typmod)
Definition fmgr.c:1755
#define OidFunctionCall0(functionId)
Definition fmgr.h:720
int32 type_maximum_size(Oid type_oid, int32 typemod)
char * format_type_be(Oid type_oid)
Oid MyDatabaseId
Definition globals.c:96
#define HASHSTANDARD_PROC
Definition hash.h:355
#define HeapTupleIsValid(tuple)
Definition htup.h:78
static void * GETSTRUCT(const HeapTupleData *tuple)
long val
Definition informix.c:689
int i
Definition isn.c:77
List * lappend(List *list, void *datum)
Definition list.c:339
List * lappend_oid(List *list, Oid datum)
Definition list.c:375
bool list_member_oid(const List *list, Oid datum)
Definition list.c:722
Oid get_range_subtype(Oid rangeOid)
Definition lsyscache.c:3602
char * get_rel_name(Oid relid)
Definition lsyscache.c:2121
void get_op_opfamily_properties(Oid opno, Oid opfamily, bool ordering_op, int *strategy, Oid *lefttype, Oid *righttype)
Definition lsyscache.c:140
Oid get_func_variadictype(Oid funcid)
Definition lsyscache.c:1916
Oid get_opclass_method(Oid opclass)
Definition lsyscache.c:1405
bool get_compatible_hash_operators(Oid opno, Oid *lhs_opno, Oid *rhs_opno)
Definition lsyscache.c:477
bool get_rel_relispartition(Oid relid)
Definition lsyscache.c:2220
char * get_propgraph_property_name(Oid propoid)
Definition lsyscache.c:3957
Oid get_op_opfamily_sortfamily(Oid opno, Oid opfamily)
Definition lsyscache.c:112
char get_rel_persistence(Oid relid)
Definition lsyscache.c:2271
char get_func_prokind(Oid funcid)
Definition lsyscache.c:2011
bool get_index_isvalid(Oid index_oid)
Definition lsyscache.c:3798
Oid get_cast_oid(Oid sourcetypeid, Oid targettypeid, bool missing_ok)
Definition lsyscache.c:1126
RegProcedure get_range_constructor2(Oid rangeOid)
Definition lsyscache.c:3653
void getTypeBinaryOutputInfo(Oid type, Oid *typSend, bool *typIsVarlena)
Definition lsyscache.c:3168
AttrNumber get_attnum(Oid relid, const char *attname)
Definition lsyscache.c:977
RegProcedure get_oprrest(Oid opno)
Definition lsyscache.c:1750
void free_attstatsslot(AttStatsSlot *sslot)
Definition lsyscache.c:3539
bool comparison_ops_are_compatible(Oid opno1, Oid opno2)
Definition lsyscache.c:825
Oid get_constraint_index(Oid conoid)
Definition lsyscache.c:1232
bool get_func_retset(Oid funcid)
Definition lsyscache.c:1935
bool get_ordering_op_properties(Oid opno, Oid *opfamily, Oid *opcintype, CompareType *cmptype)
Definition lsyscache.c:261
Oid get_element_type(Oid typid)
Definition lsyscache.c:2954
Oid get_opclass_input_type(Oid opclass)
Definition lsyscache.c:1357
bool type_is_rowtype(Oid typid)
Definition lsyscache.c:2850
bool type_is_range(Oid typid)
Definition lsyscache.c:2883
char func_parallel(Oid funcid)
Definition lsyscache.c:1992
Oid get_opclass_family(Oid opclass)
Definition lsyscache.c:1335
char get_attgenerated(Oid relid, AttrNumber attnum)
Definition lsyscache.c:1007
bool type_is_enum(Oid typid)
Definition lsyscache.c:2873
Oid get_multirange_range(Oid multirangeOid)
Definition lsyscache.c:3703
Oid get_typmodin(Oid typid)
Definition lsyscache.c:3201
Oid get_opfamily_member_for_cmptype(Oid opfamily, Oid lefttype, Oid righttype, CompareType cmptype)
Definition lsyscache.c:199
char get_typstorage(Oid typid)
Definition lsyscache.c:2614
bool get_opclass_opfamily_and_input_type(Oid opclass, Oid *opfamily, Oid *opcintype)
Definition lsyscache.c:1380
RegProcedure get_func_support(Oid funcid)
Definition lsyscache.c:2051
char * get_database_name(Oid dbid)
Definition lsyscache.c:1285
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition lsyscache.c:3102
bool get_typisdefined(Oid typid)
Definition lsyscache.c:2366
char * get_opname(Oid opno)
Definition lsyscache.c:1503
Datum get_attoptions(Oid relid, int16 attnum)
Definition lsyscache.c:1089
void get_typlenbyvalalign(Oid typid, int16 *typlen, bool *typbyval, char *typalign)
Definition lsyscache.c:2464
int32 get_attavgwidth(Oid relid, AttrNumber attnum)
Definition lsyscache.c:3353
bool get_index_isreplident(Oid index_oid)
Definition lsyscache.c:3775
Oid get_opfamily_proc(Oid opfamily, Oid lefttype, Oid righttype, int16 procnum)
Definition lsyscache.c:915
RegProcedure get_oprjoin(Oid opno)
Definition lsyscache.c:1774
Oid get_equality_op_for_ordering_op(Oid opno, bool *reverse)
Definition lsyscache.c:326
bool op_strict(Oid opno)
Definition lsyscache.c:1670
bool op_hashjoinable(Oid opno, Oid inputtype)
Definition lsyscache.c:1630
char get_rel_relkind(Oid relid)
Definition lsyscache.c:2196
void get_typlenbyval(Oid typid, int16 *typlen, bool *typbyval)
Definition lsyscache.c:2444
Oid get_func_signature(Oid funcid, Oid **argtypes, int *nargs)
Definition lsyscache.c:1889
Oid get_publication_oid(const char *pubname, bool missing_ok)
Definition lsyscache.c:3845
Oid get_rel_namespace(Oid relid)
Definition lsyscache.c:2145
static bool get_opmethod_canorder(Oid amoid)
Definition lsyscache.c:223
RegProcedure get_opcode(Oid opno)
Definition lsyscache.c:1478
Oid get_typcollation(Oid typid)
Definition lsyscache.c:3251
Oid get_op_rettype(Oid opno)
Definition lsyscache.c:1526
int get_op_opfamily_strategy(Oid opno, Oid opfamily)
Definition lsyscache.c:87
char * get_collation_name(Oid colloid)
Definition lsyscache.c:1154
Oid get_rel_type_id(Oid relid)
Definition lsyscache.c:2172
char * get_language_name(Oid langoid, bool missing_ok)
Definition lsyscache.c:1306
char * get_namespace_name_or_temp(Oid nspid)
Definition lsyscache.c:3585
void getTypeInputInfo(Oid type, Oid *typInput, Oid *typIOParam)
Definition lsyscache.c:3069
char func_volatile(Oid funcid)
Definition lsyscache.c:1973
bool equality_ops_are_compatible(Oid opno1, Oid opno2)
Definition lsyscache.c:773
get_attavgwidth_hook_type get_attavgwidth_hook
Definition lsyscache.c:57
bool op_is_safe_index_member(Oid opno)
Definition lsyscache.c:873
bool get_index_isclustered(Oid index_oid)
Definition lsyscache.c:3821
Oid get_opfamily_member(Oid opfamily, Oid lefttype, Oid righttype, int16 strategy)
Definition lsyscache.c:170
char * get_propgraph_label_name(Oid labeloid)
Definition lsyscache.c:3939
Oid get_ordering_op_for_equality_op(Oid opno, bool use_lhs_type)
Definition lsyscache.c:364
Oid get_transform_tosql(Oid typid, Oid langid, List *trftypes)
Definition lsyscache.c:2335
bool func_strict(Oid funcid)
Definition lsyscache.c:1954
Oid get_index_column_opclass(Oid index_oid, int attno)
Definition lsyscache.c:3732
char * get_constraint_name(Oid conoid)
Definition lsyscache.c:1200
char * get_attname(Oid relid, AttrNumber attnum, bool missing_ok)
Definition lsyscache.c:946
bool get_func_leakproof(Oid funcid)
Definition lsyscache.c:2030
const struct SubscriptRoutines * getSubscriptingRoutines(Oid typid, Oid *typelemp)
Definition lsyscache.c:3325
Node * get_typdefault(Oid typid)
Definition lsyscache.c:2643
bool get_collation_isdeterministic(Oid colloid)
Definition lsyscache.c:1173
List * get_op_index_interpretation(Oid opno)
Definition lsyscache.c:668
Oid get_subscription_oid(const char *subname, bool missing_ok)
Definition lsyscache.c:3895
char * get_subscription_name(Oid subid, bool missing_ok)
Definition lsyscache.c:3915
Oid get_range_collation(Oid rangeOid)
Definition lsyscache.c:3628
char * get_opfamily_name(Oid opfid, bool missing_ok)
Definition lsyscache.c:1446
char * get_func_name(Oid funcid)
Definition lsyscache.c:1801
Oid get_range_multirange(Oid rangeOid)
Definition lsyscache.c:3678
Oid get_rel_relam(Oid relid)
Definition lsyscache.c:2293
char op_volatile(Oid opno)
Definition lsyscache.c:1686
Oid get_func_namespace(Oid funcid)
Definition lsyscache.c:1825
bool type_is_collatable(Oid typid)
Definition lsyscache.c:3276
Oid get_rel_tablespace(Oid relid)
Definition lsyscache.c:2247
int get_func_nargs(Oid funcid)
Definition lsyscache.c:1867
void get_type_io_data(Oid typid, IOFuncSelector which_func, int16 *typlen, bool *typbyval, char *typalign, char *typdelim, Oid *typioparam, Oid *func)
Definition lsyscache.c:2518
int16 get_typlen(Oid typid)
Definition lsyscache.c:2390
Oid get_typ_typrelid(Oid typid)
Definition lsyscache.c:2926
char get_typtype(Oid typid)
Definition lsyscache.c:2824
Oid get_base_element_type(Oid typid)
Definition lsyscache.c:3027
Oid getTypeIOParam(HeapTuple typeTuple)
Definition lsyscache.c:2496
Oid get_opfamily_method(Oid opfid)
Definition lsyscache.c:1429
Oid getBaseTypeAndTypmod(Oid typid, int32 *typmod)
Definition lsyscache.c:2733
Oid get_transform_fromsql(Oid typid, Oid langid, List *trftypes)
Definition lsyscache.c:2313
char * get_publication_name(Oid pubid, bool missing_ok)
Definition lsyscache.c:3865
Oid getBaseType(Oid typid)
Definition lsyscache.c:2716
bool get_op_hash_functions(Oid opno, RegProcedure *lhs_procno, RegProcedure *rhs_procno)
Definition lsyscache.c:577
bool get_typbyval(Oid typid)
Definition lsyscache.c:2415
bool op_mergejoinable(Oid opno, Oid inputtype)
Definition lsyscache.c:1579
List * get_mergejoin_opfamilies(Oid opno)
Definition lsyscache.c:430
char * get_namespace_name(Oid nspid)
Definition lsyscache.c:3561
Oid get_array_type(Oid typid)
Definition lsyscache.c:2982
Oid get_func_rettype(Oid funcid)
Definition lsyscache.c:1848
Oid get_promoted_array_type(Oid typid)
Definition lsyscache.c:3006
Oid get_atttype(Oid relid, AttrNumber attnum)
Definition lsyscache.c:1032
char get_constraint_type(Oid conoid)
Definition lsyscache.c:1262
int32 get_typavgwidth(Oid typid, int32 typmod)
Definition lsyscache.c:2773
bool op_in_opfamily(Oid opno, Oid opfamily)
Definition lsyscache.c:70
RegProcedure get_typsubscript(Oid typid, Oid *typelemp)
Definition lsyscache.c:3292
void get_type_category_preferred(Oid typid, char *typcategory, bool *typispreferred)
Definition lsyscache.c:2905
bool get_attstatsslot(AttStatsSlot *sslot, HeapTuple statstuple, int reqkind, Oid reqop, int flags)
Definition lsyscache.c:3429
Oid get_relname_relid(const char *relname, Oid relnamespace)
Definition lsyscache.c:2078
Oid get_negator(Oid opno)
Definition lsyscache.c:1726
Oid get_commutator(Oid opno)
Definition lsyscache.c:1702
void op_input_types(Oid opno, Oid *lefttype, Oid *righttype)
Definition lsyscache.c:1551
bool type_is_multirange(Oid typid)
Definition lsyscache.c:2893
void getTypeBinaryInputInfo(Oid type, Oid *typReceive, Oid *typIOParam)
Definition lsyscache.c:3135
void get_atttypetypmodcoll(Oid relid, AttrNumber attnum, Oid *typid, int32 *typmod, Oid *collid)
Definition lsyscache.c:1062
#define ATTSTATSSLOT_NUMBERS
Definition lsyscache.h:44
#define ATTSTATSSLOT_VALUES
Definition lsyscache.h:43
IOFuncSelector
Definition lsyscache.h:35
@ IOFunc_output
Definition lsyscache.h:37
@ IOFunc_input
Definition lsyscache.h:36
@ IOFunc_send
Definition lsyscache.h:39
@ IOFunc_receive
Definition lsyscache.h:38
int32(* get_attavgwidth_hook_type)(Oid relid, AttrNumber attnum)
Definition lsyscache.h:66
Const * makeConst(Oid consttype, int32 consttypmod, Oid constcollid, int constlen, Datum constvalue, bool constisnull, bool constbyval)
Definition makefuncs.c:350
char * pstrdup(const char *in)
Definition mcxt.c:1781
void pfree(void *pointer)
Definition mcxt.c:1616
void * palloc(Size size)
Definition mcxt.c:1387
#define IsBootstrapProcessingMode()
Definition miscadmin.h:495
bool isTempNamespace(Oid namespaceId)
Definition namespace.c:3721
static char * errmsg
END_CATALOG_STRUCT typedef FormData_pg_amop * Form_pg_amop
Definition pg_amop.h:92
END_CATALOG_STRUCT typedef FormData_pg_amproc * Form_pg_amproc
Definition pg_amproc.h:72
NameData attname
int16 attnum
FormData_pg_attribute * Form_pg_attribute
NameData relname
Definition pg_class.h:40
FormData_pg_class * Form_pg_class
Definition pg_class.h:160
END_CATALOG_STRUCT typedef FormData_pg_collation * Form_pg_collation
END_CATALOG_STRUCT typedef FormData_pg_constraint * Form_pg_constraint
NameData datname
Definition pg_database.h:37
END_CATALOG_STRUCT typedef FormData_pg_database * Form_pg_database
END_CATALOG_STRUCT typedef FormData_pg_index * Form_pg_index
Definition pg_index.h:74
END_CATALOG_STRUCT typedef FormData_pg_language * Form_pg_language
Definition pg_language.h:69
#define NIL
Definition pg_list.h:68
END_CATALOG_STRUCT typedef FormData_pg_namespace * Form_pg_namespace
END_CATALOG_STRUCT typedef FormData_pg_opclass * Form_pg_opclass
Definition pg_opclass.h:87
END_CATALOG_STRUCT typedef FormData_pg_operator * Form_pg_operator
Definition pg_operator.h:87
END_CATALOG_STRUCT typedef FormData_pg_opfamily * Form_pg_opfamily
Definition pg_opfamily.h:55
int16 pronargs
Definition pg_proc.h:83
END_CATALOG_STRUCT typedef FormData_pg_proc * Form_pg_proc
Definition pg_proc.h:140
END_CATALOG_STRUCT typedef FormData_pg_propgraph_label * Form_pg_propgraph_label
END_CATALOG_STRUCT typedef FormData_pg_propgraph_property * Form_pg_propgraph_property
END_CATALOG_STRUCT typedef FormData_pg_publication * Form_pg_publication
END_CATALOG_STRUCT typedef FormData_pg_range * Form_pg_range
Definition pg_range.h:71
#define STATISTIC_NUM_SLOTS
FormData_pg_statistic * Form_pg_statistic
NameData subname
END_CATALOG_STRUCT typedef FormData_pg_subscription * Form_pg_subscription
END_CATALOG_STRUCT typedef FormData_pg_transform * Form_pg_transform
END_CATALOG_STRUCT typedef FormData_pg_type * Form_pg_type
Definition pg_type.h:265
char typalign
Definition pg_type.h:178
static Datum Int16GetDatum(int16 X)
Definition postgres.h:172
static Datum BoolGetDatum(bool X)
Definition postgres.h:112
static Datum ObjectIdGetDatum(Oid X)
Definition postgres.h:252
uint64_t Datum
Definition postgres.h:70
static Pointer DatumGetPointer(Datum X)
Definition postgres.h:332
static Datum CStringGetDatum(const char *X)
Definition postgres.h:383
#define PointerGetDatum(X)
Definition postgres.h:354
static Datum CharGetDatum(char X)
Definition postgres.h:132
#define InvalidOid
unsigned int Oid
static int fb(int x)
void * stringToNode(const char *str)
Definition read.c:90
uint16 StrategyNumber
Definition stratnum.h:22
#define HTEqualStrategyNumber
Definition stratnum.h:41
bool amconsistentordering
Definition amapi.h:254
bool amcanorder
Definition amapi.h:246
bool amconsistentequality
Definition amapi.h:252
Definition pg_list.h:54
Definition nodes.h:135
Definition c.h:815
void ReleaseSysCache(HeapTuple tuple)
Definition syscache.c:265
HeapTuple SearchSysCache2(SysCacheIdentifier cacheId, Datum key1, Datum key2)
Definition syscache.c:231
HeapTuple SearchSysCache3(SysCacheIdentifier cacheId, Datum key1, Datum key2, Datum key3)
Definition syscache.c:241
HeapTuple SearchSysCacheAttName(Oid relid, const char *attname)
Definition syscache.c:476
Datum SysCacheGetAttrNotNull(SysCacheIdentifier cacheId, HeapTuple tup, AttrNumber attributeNumber)
Definition syscache.c:626
HeapTuple SearchSysCache4(SysCacheIdentifier cacheId, Datum key1, Datum key2, Datum key3, Datum key4)
Definition syscache.c:251
HeapTuple SearchSysCache1(SysCacheIdentifier cacheId, Datum key1)
Definition syscache.c:221
Datum SysCacheGetAttr(SysCacheIdentifier cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition syscache.c:596
#define ReleaseSysCacheList(x)
Definition syscache.h:134
#define SearchSysCacheList1(cacheId, key1)
Definition syscache.h:127
#define SearchSysCacheExists3(cacheId, key1, key2, key3)
Definition syscache.h:104
#define GetSysCacheOid1(cacheId, oidcol, key1)
Definition syscache.h:109
#define GetSysCacheOid2(cacheId, oidcol, key1, key2)
Definition syscache.h:111
TypeCacheEntry * lookup_type_cache(Oid type_id, int flags)
Definition typcache.c:389
#define TYPECACHE_CMP_PROC
Definition typcache.h:141
#define TYPECACHE_HASH_PROC
Definition typcache.h:142
const char * type