LCOV - code coverage report
Current view: top level - src/backend/utils/cache - lsyscache.c (source / functions) Hit Total Coverage
Test: PostgreSQL 15devel Lines: 971 1104 88.0 %
Date: 2021-12-09 04:09:06 Functions: 109 111 98.2 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * lsyscache.c
       4             :  *    Convenience routines for common queries in the system catalog cache.
       5             :  *
       6             :  * Portions Copyright (c) 1996-2021, 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 "access/nbtree.h"
      21             : #include "bootstrap/bootstrap.h"
      22             : #include "catalog/namespace.h"
      23             : #include "catalog/pg_am.h"
      24             : #include "catalog/pg_amop.h"
      25             : #include "catalog/pg_amproc.h"
      26             : #include "catalog/pg_cast.h"
      27             : #include "catalog/pg_collation.h"
      28             : #include "catalog/pg_constraint.h"
      29             : #include "catalog/pg_language.h"
      30             : #include "catalog/pg_namespace.h"
      31             : #include "catalog/pg_opclass.h"
      32             : #include "catalog/pg_operator.h"
      33             : #include "catalog/pg_proc.h"
      34             : #include "catalog/pg_range.h"
      35             : #include "catalog/pg_statistic.h"
      36             : #include "catalog/pg_transform.h"
      37             : #include "catalog/pg_type.h"
      38             : #include "miscadmin.h"
      39             : #include "nodes/makefuncs.h"
      40             : #include "utils/array.h"
      41             : #include "utils/builtins.h"
      42             : #include "utils/catcache.h"
      43             : #include "utils/datum.h"
      44             : #include "utils/fmgroids.h"
      45             : #include "utils/lsyscache.h"
      46             : #include "utils/rel.h"
      47             : #include "utils/syscache.h"
      48             : #include "utils/typcache.h"
      49             : 
      50             : /* Hook for plugins to get control in get_attavgwidth() */
      51             : get_attavgwidth_hook_type get_attavgwidth_hook = NULL;
      52             : 
      53             : 
      54             : /*              ---------- AMOP CACHES ----------                        */
      55             : 
      56             : /*
      57             :  * op_in_opfamily
      58             :  *
      59             :  *      Return t iff operator 'opno' is in operator family 'opfamily'.
      60             :  *
      61             :  * This function only considers search operators, not ordering operators.
      62             :  */
      63             : bool
      64      263242 : op_in_opfamily(Oid opno, Oid opfamily)
      65             : {
      66      263242 :     return SearchSysCacheExists3(AMOPOPID,
      67             :                                  ObjectIdGetDatum(opno),
      68             :                                  CharGetDatum(AMOP_SEARCH),
      69             :                                  ObjectIdGetDatum(opfamily));
      70             : }
      71             : 
      72             : /*
      73             :  * get_op_opfamily_strategy
      74             :  *
      75             :  *      Get the operator's strategy number within the specified opfamily,
      76             :  *      or 0 if it's not a member of the opfamily.
      77             :  *
      78             :  * This function only considers search operators, not ordering operators.
      79             :  */
      80             : int
      81      302470 : get_op_opfamily_strategy(Oid opno, Oid opfamily)
      82             : {
      83             :     HeapTuple   tp;
      84             :     Form_pg_amop amop_tup;
      85             :     int         result;
      86             : 
      87      302470 :     tp = SearchSysCache3(AMOPOPID,
      88             :                          ObjectIdGetDatum(opno),
      89             :                          CharGetDatum(AMOP_SEARCH),
      90             :                          ObjectIdGetDatum(opfamily));
      91      302470 :     if (!HeapTupleIsValid(tp))
      92           0 :         return 0;
      93      302470 :     amop_tup = (Form_pg_amop) GETSTRUCT(tp);
      94      302470 :     result = amop_tup->amopstrategy;
      95      302470 :     ReleaseSysCache(tp);
      96      302470 :     return result;
      97             : }
      98             : 
      99             : /*
     100             :  * get_op_opfamily_sortfamily
     101             :  *
     102             :  *      If the operator is an ordering operator within the specified opfamily,
     103             :  *      return its amopsortfamily OID; else return InvalidOid.
     104             :  */
     105             : Oid
     106         328 : get_op_opfamily_sortfamily(Oid opno, Oid opfamily)
     107             : {
     108             :     HeapTuple   tp;
     109             :     Form_pg_amop amop_tup;
     110             :     Oid         result;
     111             : 
     112         328 :     tp = SearchSysCache3(AMOPOPID,
     113             :                          ObjectIdGetDatum(opno),
     114             :                          CharGetDatum(AMOP_ORDER),
     115             :                          ObjectIdGetDatum(opfamily));
     116         328 :     if (!HeapTupleIsValid(tp))
     117           0 :         return InvalidOid;
     118         328 :     amop_tup = (Form_pg_amop) GETSTRUCT(tp);
     119         328 :     result = amop_tup->amopsortfamily;
     120         328 :     ReleaseSysCache(tp);
     121         328 :     return result;
     122             : }
     123             : 
     124             : /*
     125             :  * get_op_opfamily_properties
     126             :  *
     127             :  *      Get the operator's strategy number and declared input data types
     128             :  *      within the specified opfamily.
     129             :  *
     130             :  * Caller should already have verified that opno is a member of opfamily,
     131             :  * therefore we raise an error if the tuple is not found.
     132             :  */
     133             : void
     134      185148 : get_op_opfamily_properties(Oid opno, Oid opfamily, bool ordering_op,
     135             :                            int *strategy,
     136             :                            Oid *lefttype,
     137             :                            Oid *righttype)
     138             : {
     139             :     HeapTuple   tp;
     140             :     Form_pg_amop amop_tup;
     141             : 
     142      185148 :     tp = SearchSysCache3(AMOPOPID,
     143             :                          ObjectIdGetDatum(opno),
     144             :                          CharGetDatum(ordering_op ? AMOP_ORDER : AMOP_SEARCH),
     145             :                          ObjectIdGetDatum(opfamily));
     146      185148 :     if (!HeapTupleIsValid(tp))
     147           0 :         elog(ERROR, "operator %u is not a member of opfamily %u",
     148             :              opno, opfamily);
     149      185148 :     amop_tup = (Form_pg_amop) GETSTRUCT(tp);
     150      185148 :     *strategy = amop_tup->amopstrategy;
     151      185148 :     *lefttype = amop_tup->amoplefttype;
     152      185148 :     *righttype = amop_tup->amoprighttype;
     153      185148 :     ReleaseSysCache(tp);
     154      185148 : }
     155             : 
     156             : /*
     157             :  * get_opfamily_member
     158             :  *      Get the OID of the operator that implements the specified strategy
     159             :  *      with the specified datatypes for the specified opfamily.
     160             :  *
     161             :  * Returns InvalidOid if there is no pg_amop entry for the given keys.
     162             :  */
     163             : Oid
     164     1552110 : get_opfamily_member(Oid opfamily, Oid lefttype, Oid righttype,
     165             :                     int16 strategy)
     166             : {
     167             :     HeapTuple   tp;
     168             :     Form_pg_amop amop_tup;
     169             :     Oid         result;
     170             : 
     171     1552110 :     tp = SearchSysCache4(AMOPSTRATEGY,
     172             :                          ObjectIdGetDatum(opfamily),
     173             :                          ObjectIdGetDatum(lefttype),
     174             :                          ObjectIdGetDatum(righttype),
     175             :                          Int16GetDatum(strategy));
     176     1552110 :     if (!HeapTupleIsValid(tp))
     177         244 :         return InvalidOid;
     178     1551866 :     amop_tup = (Form_pg_amop) GETSTRUCT(tp);
     179     1551866 :     result = amop_tup->amopopr;
     180     1551866 :     ReleaseSysCache(tp);
     181     1551866 :     return result;
     182             : }
     183             : 
     184             : /*
     185             :  * get_ordering_op_properties
     186             :  *      Given the OID of an ordering operator (a btree "<" or ">" operator),
     187             :  *      determine its opfamily, its declared input datatype, and its
     188             :  *      strategy number (BTLessStrategyNumber or BTGreaterStrategyNumber).
     189             :  *
     190             :  * Returns true if successful, false if no matching pg_amop entry exists.
     191             :  * (This indicates that the operator is not a valid ordering operator.)
     192             :  *
     193             :  * Note: the operator could be registered in multiple families, for example
     194             :  * if someone were to build a "reverse sort" opfamily.  This would result in
     195             :  * uncertainty as to whether "ORDER BY USING op" would default to NULLS FIRST
     196             :  * or NULLS LAST, as well as inefficient planning due to failure to match up
     197             :  * pathkeys that should be the same.  So we want a determinate result here.
     198             :  * Because of the way the syscache search works, we'll use the interpretation
     199             :  * associated with the opfamily with smallest OID, which is probably
     200             :  * determinate enough.  Since there is no longer any particularly good reason
     201             :  * to build reverse-sort opfamilies, it doesn't seem worth expending any
     202             :  * additional effort on ensuring consistency.
     203             :  */
     204             : bool
     205      381876 : get_ordering_op_properties(Oid opno,
     206             :                            Oid *opfamily, Oid *opcintype, int16 *strategy)
     207             : {
     208      381876 :     bool        result = false;
     209             :     CatCList   *catlist;
     210             :     int         i;
     211             : 
     212             :     /* ensure outputs are initialized on failure */
     213      381876 :     *opfamily = InvalidOid;
     214      381876 :     *opcintype = InvalidOid;
     215      381876 :     *strategy = 0;
     216             : 
     217             :     /*
     218             :      * Search pg_amop to see if the target operator is registered as the "<"
     219             :      * or ">" operator of any btree opfamily.
     220             :      */
     221      381876 :     catlist = SearchSysCacheList1(AMOPOPID, ObjectIdGetDatum(opno));
     222             : 
     223      381876 :     for (i = 0; i < catlist->n_members; i++)
     224             :     {
     225      381876 :         HeapTuple   tuple = &catlist->members[i]->tuple;
     226      381876 :         Form_pg_amop aform = (Form_pg_amop) GETSTRUCT(tuple);
     227             : 
     228             :         /* must be btree */
     229      381876 :         if (aform->amopmethod != BTREE_AM_OID)
     230           0 :             continue;
     231             : 
     232      381876 :         if (aform->amopstrategy == BTLessStrategyNumber ||
     233        4764 :             aform->amopstrategy == BTGreaterStrategyNumber)
     234             :         {
     235             :             /* Found it ... should have consistent input types */
     236      381876 :             if (aform->amoplefttype == aform->amoprighttype)
     237             :             {
     238             :                 /* Found a suitable opfamily, return info */
     239      381876 :                 *opfamily = aform->amopfamily;
     240      381876 :                 *opcintype = aform->amoplefttype;
     241      381876 :                 *strategy = aform->amopstrategy;
     242      381876 :                 result = true;
     243      381876 :                 break;
     244             :             }
     245             :         }
     246             :     }
     247             : 
     248      381876 :     ReleaseSysCacheList(catlist);
     249             : 
     250      381876 :     return result;
     251             : }
     252             : 
     253             : /*
     254             :  * get_equality_op_for_ordering_op
     255             :  *      Get the OID of the datatype-specific btree equality operator
     256             :  *      associated with an ordering operator (a "<" or ">" operator).
     257             :  *
     258             :  * If "reverse" isn't NULL, also set *reverse to false if the operator is "<",
     259             :  * true if it's ">"
     260             :  *
     261             :  * Returns InvalidOid if no matching equality operator can be found.
     262             :  * (This indicates that the operator is not a valid ordering operator.)
     263             :  */
     264             : Oid
     265         752 : get_equality_op_for_ordering_op(Oid opno, bool *reverse)
     266             : {
     267         752 :     Oid         result = InvalidOid;
     268             :     Oid         opfamily;
     269             :     Oid         opcintype;
     270             :     int16       strategy;
     271             : 
     272             :     /* Find the operator in pg_amop */
     273         752 :     if (get_ordering_op_properties(opno,
     274             :                                    &opfamily, &opcintype, &strategy))
     275             :     {
     276             :         /* Found a suitable opfamily, get matching equality operator */
     277         752 :         result = get_opfamily_member(opfamily,
     278             :                                      opcintype,
     279             :                                      opcintype,
     280             :                                      BTEqualStrategyNumber);
     281         752 :         if (reverse)
     282         602 :             *reverse = (strategy == BTGreaterStrategyNumber);
     283             :     }
     284             : 
     285         752 :     return result;
     286             : }
     287             : 
     288             : /*
     289             :  * get_ordering_op_for_equality_op
     290             :  *      Get the OID of a datatype-specific btree ordering operator
     291             :  *      associated with an equality operator.  (If there are multiple
     292             :  *      possibilities, assume any one will do.)
     293             :  *
     294             :  * This function is used when we have to sort data before unique-ifying,
     295             :  * and don't much care which sorting op is used as long as it's compatible
     296             :  * with the intended equality operator.  Since we need a sorting operator,
     297             :  * it should be single-data-type even if the given operator is cross-type.
     298             :  * The caller specifies whether to find an op for the LHS or RHS data type.
     299             :  *
     300             :  * Returns InvalidOid if no matching ordering operator can be found.
     301             :  */
     302             : Oid
     303           2 : get_ordering_op_for_equality_op(Oid opno, bool use_lhs_type)
     304             : {
     305           2 :     Oid         result = InvalidOid;
     306             :     CatCList   *catlist;
     307             :     int         i;
     308             : 
     309             :     /*
     310             :      * Search pg_amop to see if the target operator is registered as the "="
     311             :      * operator of any btree opfamily.
     312             :      */
     313           2 :     catlist = SearchSysCacheList1(AMOPOPID, ObjectIdGetDatum(opno));
     314             : 
     315           2 :     for (i = 0; i < catlist->n_members; i++)
     316             :     {
     317           2 :         HeapTuple   tuple = &catlist->members[i]->tuple;
     318           2 :         Form_pg_amop aform = (Form_pg_amop) GETSTRUCT(tuple);
     319             : 
     320             :         /* must be btree */
     321           2 :         if (aform->amopmethod != BTREE_AM_OID)
     322           0 :             continue;
     323             : 
     324           2 :         if (aform->amopstrategy == BTEqualStrategyNumber)
     325             :         {
     326             :             /* Found a suitable opfamily, get matching ordering operator */
     327             :             Oid         typid;
     328             : 
     329           2 :             typid = use_lhs_type ? aform->amoplefttype : aform->amoprighttype;
     330           2 :             result = get_opfamily_member(aform->amopfamily,
     331             :                                          typid, typid,
     332             :                                          BTLessStrategyNumber);
     333           2 :             if (OidIsValid(result))
     334           2 :                 break;
     335             :             /* failure probably shouldn't happen, but keep looking if so */
     336             :         }
     337             :     }
     338             : 
     339           2 :     ReleaseSysCacheList(catlist);
     340             : 
     341           2 :     return result;
     342             : }
     343             : 
     344             : /*
     345             :  * get_mergejoin_opfamilies
     346             :  *      Given a putatively mergejoinable operator, return a list of the OIDs
     347             :  *      of the btree opfamilies in which it represents equality.
     348             :  *
     349             :  * It is possible (though at present unusual) for an operator to be equality
     350             :  * in more than one opfamily, hence the result is a list.  This also lets us
     351             :  * return NIL if the operator is not found in any opfamilies.
     352             :  *
     353             :  * The planner currently uses simple equal() tests to compare the lists
     354             :  * returned by this function, which makes the list order relevant, though
     355             :  * strictly speaking it should not be.  Because of the way syscache list
     356             :  * searches are handled, in normal operation the result will be sorted by OID
     357             :  * so everything works fine.  If running with system index usage disabled,
     358             :  * the result ordering is unspecified and hence the planner might fail to
     359             :  * recognize optimization opportunities ... but that's hardly a scenario in
     360             :  * which performance is good anyway, so there's no point in expending code
     361             :  * or cycles here to guarantee the ordering in that case.
     362             :  */
     363             : List *
     364      997728 : get_mergejoin_opfamilies(Oid opno)
     365             : {
     366      997728 :     List       *result = NIL;
     367             :     CatCList   *catlist;
     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             :      */
     374      997728 :     catlist = SearchSysCacheList1(AMOPOPID, ObjectIdGetDatum(opno));
     375             : 
     376     5966902 :     for (i = 0; i < catlist->n_members; i++)
     377             :     {
     378     4969174 :         HeapTuple   tuple = &catlist->members[i]->tuple;
     379     4969174 :         Form_pg_amop aform = (Form_pg_amop) GETSTRUCT(tuple);
     380             : 
     381             :         /* must be btree equality */
     382     4969174 :         if (aform->amopmethod == BTREE_AM_OID &&
     383     1019198 :             aform->amopstrategy == BTEqualStrategyNumber)
     384     1019198 :             result = lappend_oid(result, aform->amopfamily);
     385             :     }
     386             : 
     387      997728 :     ReleaseSysCacheList(catlist);
     388             : 
     389      997728 :     return result;
     390             : }
     391             : 
     392             : /*
     393             :  * get_compatible_hash_operators
     394             :  *      Get the OID(s) of hash equality operator(s) compatible with the given
     395             :  *      operator, but operating on its LHS and/or RHS datatype.
     396             :  *
     397             :  * An operator for the LHS type is sought and returned into *lhs_opno if
     398             :  * lhs_opno isn't NULL.  Similarly, an operator for the RHS type is sought
     399             :  * and returned into *rhs_opno if rhs_opno isn't NULL.
     400             :  *
     401             :  * If the given operator is not cross-type, the results should be the same
     402             :  * operator, but in cross-type situations they will be different.
     403             :  *
     404             :  * Returns true if able to find the requested operator(s), false if not.
     405             :  * (This indicates that the operator should not have been marked oprcanhash.)
     406             :  */
     407             : bool
     408        2098 : get_compatible_hash_operators(Oid opno,
     409             :                               Oid *lhs_opno, Oid *rhs_opno)
     410             : {
     411        2098 :     bool        result = false;
     412             :     CatCList   *catlist;
     413             :     int         i;
     414             : 
     415             :     /* Ensure output args are initialized on failure */
     416        2098 :     if (lhs_opno)
     417           0 :         *lhs_opno = InvalidOid;
     418        2098 :     if (rhs_opno)
     419        2098 :         *rhs_opno = InvalidOid;
     420             : 
     421             :     /*
     422             :      * Search pg_amop to see if the target operator is registered as the "="
     423             :      * operator of any hash opfamily.  If the operator is registered in
     424             :      * multiple opfamilies, assume we can use any one.
     425             :      */
     426        2098 :     catlist = SearchSysCacheList1(AMOPOPID, ObjectIdGetDatum(opno));
     427             : 
     428        4196 :     for (i = 0; i < catlist->n_members; i++)
     429             :     {
     430        4196 :         HeapTuple   tuple = &catlist->members[i]->tuple;
     431        4196 :         Form_pg_amop aform = (Form_pg_amop) GETSTRUCT(tuple);
     432             : 
     433        4196 :         if (aform->amopmethod == HASH_AM_OID &&
     434        2098 :             aform->amopstrategy == HTEqualStrategyNumber)
     435             :         {
     436             :             /* No extra lookup needed if given operator is single-type */
     437        2098 :             if (aform->amoplefttype == aform->amoprighttype)
     438             :             {
     439        2066 :                 if (lhs_opno)
     440           0 :                     *lhs_opno = opno;
     441        2066 :                 if (rhs_opno)
     442        2066 :                     *rhs_opno = opno;
     443        2066 :                 result = true;
     444        2066 :                 break;
     445             :             }
     446             : 
     447             :             /*
     448             :              * Get the matching single-type operator(s).  Failure probably
     449             :              * shouldn't happen --- it implies a bogus opfamily --- but
     450             :              * continue looking if so.
     451             :              */
     452          32 :             if (lhs_opno)
     453             :             {
     454           0 :                 *lhs_opno = get_opfamily_member(aform->amopfamily,
     455             :                                                 aform->amoplefttype,
     456             :                                                 aform->amoplefttype,
     457             :                                                 HTEqualStrategyNumber);
     458           0 :                 if (!OidIsValid(*lhs_opno))
     459           0 :                     continue;
     460             :                 /* Matching LHS found, done if caller doesn't want RHS */
     461           0 :                 if (!rhs_opno)
     462             :                 {
     463           0 :                     result = true;
     464           0 :                     break;
     465             :                 }
     466             :             }
     467          32 :             if (rhs_opno)
     468             :             {
     469          32 :                 *rhs_opno = get_opfamily_member(aform->amopfamily,
     470             :                                                 aform->amoprighttype,
     471             :                                                 aform->amoprighttype,
     472             :                                                 HTEqualStrategyNumber);
     473          32 :                 if (!OidIsValid(*rhs_opno))
     474             :                 {
     475             :                     /* Forget any LHS operator from this opfamily */
     476           0 :                     if (lhs_opno)
     477           0 :                         *lhs_opno = InvalidOid;
     478           0 :                     continue;
     479             :                 }
     480             :                 /* Matching RHS found, so done */
     481          32 :                 result = true;
     482          32 :                 break;
     483             :             }
     484             :         }
     485             :     }
     486             : 
     487        2098 :     ReleaseSysCacheList(catlist);
     488             : 
     489        2098 :     return result;
     490             : }
     491             : 
     492             : /*
     493             :  * get_op_hash_functions
     494             :  *      Get the OID(s) of the standard hash support function(s) compatible with
     495             :  *      the given operator, operating on its LHS and/or RHS datatype as required.
     496             :  *
     497             :  * A function for the LHS type is sought and returned into *lhs_procno if
     498             :  * lhs_procno isn't NULL.  Similarly, a function for the RHS type is sought
     499             :  * and returned into *rhs_procno if rhs_procno isn't NULL.
     500             :  *
     501             :  * If the given operator is not cross-type, the results should be the same
     502             :  * function, but in cross-type situations they will be different.
     503             :  *
     504             :  * Returns true if able to find the requested function(s), false if not.
     505             :  * (This indicates that the operator should not have been marked oprcanhash.)
     506             :  */
     507             : bool
     508       32712 : get_op_hash_functions(Oid opno,
     509             :                       RegProcedure *lhs_procno, RegProcedure *rhs_procno)
     510             : {
     511       32712 :     bool        result = false;
     512             :     CatCList   *catlist;
     513             :     int         i;
     514             : 
     515             :     /* Ensure output args are initialized on failure */
     516       32712 :     if (lhs_procno)
     517       32712 :         *lhs_procno = InvalidOid;
     518       32712 :     if (rhs_procno)
     519       32712 :         *rhs_procno = InvalidOid;
     520             : 
     521             :     /*
     522             :      * Search pg_amop to see if the target operator is registered as the "="
     523             :      * operator of any hash opfamily.  If the operator is registered in
     524             :      * multiple opfamilies, assume we can use any one.
     525             :      */
     526       32712 :     catlist = SearchSysCacheList1(AMOPOPID, ObjectIdGetDatum(opno));
     527             : 
     528       65196 :     for (i = 0; i < catlist->n_members; i++)
     529             :     {
     530       64938 :         HeapTuple   tuple = &catlist->members[i]->tuple;
     531       64938 :         Form_pg_amop aform = (Form_pg_amop) GETSTRUCT(tuple);
     532             : 
     533       64938 :         if (aform->amopmethod == HASH_AM_OID &&
     534       32454 :             aform->amopstrategy == HTEqualStrategyNumber)
     535             :         {
     536             :             /*
     537             :              * Get the matching support function(s).  Failure probably
     538             :              * shouldn't happen --- it implies a bogus opfamily --- but
     539             :              * continue looking if so.
     540             :              */
     541       32454 :             if (lhs_procno)
     542             :             {
     543       32454 :                 *lhs_procno = get_opfamily_proc(aform->amopfamily,
     544             :                                                 aform->amoplefttype,
     545             :                                                 aform->amoplefttype,
     546             :                                                 HASHSTANDARD_PROC);
     547       32454 :                 if (!OidIsValid(*lhs_procno))
     548           0 :                     continue;
     549             :                 /* Matching LHS found, done if caller doesn't want RHS */
     550       32454 :                 if (!rhs_procno)
     551             :                 {
     552           0 :                     result = true;
     553           0 :                     break;
     554             :                 }
     555             :                 /* Only one lookup needed if given operator is single-type */
     556       32454 :                 if (aform->amoplefttype == aform->amoprighttype)
     557             :                 {
     558       32338 :                     *rhs_procno = *lhs_procno;
     559       32338 :                     result = true;
     560       32338 :                     break;
     561             :                 }
     562             :             }
     563         116 :             if (rhs_procno)
     564             :             {
     565         116 :                 *rhs_procno = get_opfamily_proc(aform->amopfamily,
     566             :                                                 aform->amoprighttype,
     567             :                                                 aform->amoprighttype,
     568             :                                                 HASHSTANDARD_PROC);
     569         116 :                 if (!OidIsValid(*rhs_procno))
     570             :                 {
     571             :                     /* Forget any LHS function from this opfamily */
     572           0 :                     if (lhs_procno)
     573           0 :                         *lhs_procno = InvalidOid;
     574           0 :                     continue;
     575             :                 }
     576             :                 /* Matching RHS found, so done */
     577         116 :                 result = true;
     578         116 :                 break;
     579             :             }
     580             :         }
     581             :     }
     582             : 
     583       32712 :     ReleaseSysCacheList(catlist);
     584             : 
     585       32712 :     return result;
     586             : }
     587             : 
     588             : /*
     589             :  * get_op_btree_interpretation
     590             :  *      Given an operator's OID, find out which btree opfamilies it belongs to,
     591             :  *      and what properties it has within each one.  The results are returned
     592             :  *      as a palloc'd list of OpBtreeInterpretation structs.
     593             :  *
     594             :  * In addition to the normal btree operators, we consider a <> operator to be
     595             :  * a "member" of an opfamily if its negator is an equality operator of the
     596             :  * opfamily.  ROWCOMPARE_NE is returned as the strategy number for this case.
     597             :  */
     598             : List *
     599        8840 : get_op_btree_interpretation(Oid opno)
     600             : {
     601        8840 :     List       *result = NIL;
     602             :     OpBtreeInterpretation *thisresult;
     603             :     CatCList   *catlist;
     604             :     int         i;
     605             : 
     606             :     /*
     607             :      * Find all the pg_amop entries containing the operator.
     608             :      */
     609        8840 :     catlist = SearchSysCacheList1(AMOPOPID, ObjectIdGetDatum(opno));
     610             : 
     611       29142 :     for (i = 0; i < catlist->n_members; i++)
     612             :     {
     613       20302 :         HeapTuple   op_tuple = &catlist->members[i]->tuple;
     614       20302 :         Form_pg_amop op_form = (Form_pg_amop) GETSTRUCT(op_tuple);
     615             :         StrategyNumber op_strategy;
     616             : 
     617             :         /* must be btree */
     618       20302 :         if (op_form->amopmethod != BTREE_AM_OID)
     619       15096 :             continue;
     620             : 
     621             :         /* Get the operator's btree strategy number */
     622        5206 :         op_strategy = (StrategyNumber) op_form->amopstrategy;
     623             :         Assert(op_strategy >= 1 && op_strategy <= 5);
     624             : 
     625             :         thisresult = (OpBtreeInterpretation *)
     626        5206 :             palloc(sizeof(OpBtreeInterpretation));
     627        5206 :         thisresult->opfamily_id = op_form->amopfamily;
     628        5206 :         thisresult->strategy = op_strategy;
     629        5206 :         thisresult->oplefttype = op_form->amoplefttype;
     630        5206 :         thisresult->oprighttype = op_form->amoprighttype;
     631        5206 :         result = lappend(result, thisresult);
     632             :     }
     633             : 
     634        8840 :     ReleaseSysCacheList(catlist);
     635             : 
     636             :     /*
     637             :      * If we didn't find any btree opfamily containing the operator, perhaps
     638             :      * it is a <> operator.  See if it has a negator that is in an opfamily.
     639             :      */
     640        8840 :     if (result == NIL)
     641             :     {
     642        4220 :         Oid         op_negator = get_negator(opno);
     643             : 
     644        4220 :         if (OidIsValid(op_negator))
     645             :         {
     646        4204 :             catlist = SearchSysCacheList1(AMOPOPID,
     647             :                                           ObjectIdGetDatum(op_negator));
     648             : 
     649       20220 :             for (i = 0; i < catlist->n_members; i++)
     650             :             {
     651       16016 :                 HeapTuple   op_tuple = &catlist->members[i]->tuple;
     652       16016 :                 Form_pg_amop op_form = (Form_pg_amop) GETSTRUCT(op_tuple);
     653             :                 StrategyNumber op_strategy;
     654             : 
     655             :                 /* must be btree */
     656       16016 :                 if (op_form->amopmethod != BTREE_AM_OID)
     657       12044 :                     continue;
     658             : 
     659             :                 /* Get the operator's btree strategy number */
     660        3972 :                 op_strategy = (StrategyNumber) op_form->amopstrategy;
     661             :                 Assert(op_strategy >= 1 && op_strategy <= 5);
     662             : 
     663             :                 /* Only consider negators that are = */
     664        3972 :                 if (op_strategy != BTEqualStrategyNumber)
     665           0 :                     continue;
     666             : 
     667             :                 /* OK, report it with "strategy" ROWCOMPARE_NE */
     668             :                 thisresult = (OpBtreeInterpretation *)
     669        3972 :                     palloc(sizeof(OpBtreeInterpretation));
     670        3972 :                 thisresult->opfamily_id = op_form->amopfamily;
     671        3972 :                 thisresult->strategy = ROWCOMPARE_NE;
     672        3972 :                 thisresult->oplefttype = op_form->amoplefttype;
     673        3972 :                 thisresult->oprighttype = op_form->amoprighttype;
     674        3972 :                 result = lappend(result, thisresult);
     675             :             }
     676             : 
     677        4204 :             ReleaseSysCacheList(catlist);
     678             :         }
     679             :     }
     680             : 
     681        8840 :     return result;
     682             : }
     683             : 
     684             : /*
     685             :  * equality_ops_are_compatible
     686             :  *      Return true if the two given equality operators have compatible
     687             :  *      semantics.
     688             :  *
     689             :  * This is trivially true if they are the same operator.  Otherwise,
     690             :  * we look to see if they can be found in the same btree or hash opfamily.
     691             :  * Either finding allows us to assume that they have compatible notions
     692             :  * of equality.  (The reason we need to do these pushups is that one might
     693             :  * be a cross-type operator; for instance int24eq vs int4eq.)
     694             :  */
     695             : bool
     696          92 : equality_ops_are_compatible(Oid opno1, Oid opno2)
     697             : {
     698             :     bool        result;
     699             :     CatCList   *catlist;
     700             :     int         i;
     701             : 
     702             :     /* Easy if they're the same operator */
     703          92 :     if (opno1 == opno2)
     704          88 :         return true;
     705             : 
     706             :     /*
     707             :      * We search through all the pg_amop entries for opno1.
     708             :      */
     709           4 :     catlist = SearchSysCacheList1(AMOPOPID, ObjectIdGetDatum(opno1));
     710             : 
     711           4 :     result = false;
     712           4 :     for (i = 0; i < catlist->n_members; i++)
     713             :     {
     714           4 :         HeapTuple   op_tuple = &catlist->members[i]->tuple;
     715           4 :         Form_pg_amop op_form = (Form_pg_amop) GETSTRUCT(op_tuple);
     716             : 
     717             :         /* must be btree or hash */
     718           4 :         if (op_form->amopmethod == BTREE_AM_OID ||
     719           0 :             op_form->amopmethod == HASH_AM_OID)
     720             :         {
     721           4 :             if (op_in_opfamily(opno2, op_form->amopfamily))
     722             :             {
     723           4 :                 result = true;
     724           4 :                 break;
     725             :             }
     726             :         }
     727             :     }
     728             : 
     729           4 :     ReleaseSysCacheList(catlist);
     730             : 
     731           4 :     return result;
     732             : }
     733             : 
     734             : /*
     735             :  * comparison_ops_are_compatible
     736             :  *      Return true if the two given comparison operators have compatible
     737             :  *      semantics.
     738             :  *
     739             :  * This is trivially true if they are the same operator.  Otherwise,
     740             :  * we look to see if they can be found in the same btree opfamily.
     741             :  * For example, '<' and '>=' ops match if they belong to the same family.
     742             :  *
     743             :  * (This is identical to equality_ops_are_compatible(), except that we
     744             :  * don't bother to examine hash opclasses.)
     745             :  */
     746             : bool
     747      102122 : comparison_ops_are_compatible(Oid opno1, Oid opno2)
     748             : {
     749             :     bool        result;
     750             :     CatCList   *catlist;
     751             :     int         i;
     752             : 
     753             :     /* Easy if they're the same operator */
     754      102122 :     if (opno1 == opno2)
     755       47828 :         return true;
     756             : 
     757             :     /*
     758             :      * We search through all the pg_amop entries for opno1.
     759             :      */
     760       54294 :     catlist = SearchSysCacheList1(AMOPOPID, ObjectIdGetDatum(opno1));
     761             : 
     762       54294 :     result = false;
     763       54510 :     for (i = 0; i < catlist->n_members; i++)
     764             :     {
     765       54438 :         HeapTuple   op_tuple = &catlist->members[i]->tuple;
     766       54438 :         Form_pg_amop op_form = (Form_pg_amop) GETSTRUCT(op_tuple);
     767             : 
     768       54438 :         if (op_form->amopmethod == BTREE_AM_OID)
     769             :         {
     770       54294 :             if (op_in_opfamily(opno2, op_form->amopfamily))
     771             :             {
     772       54222 :                 result = true;
     773       54222 :                 break;
     774             :             }
     775             :         }
     776             :     }
     777             : 
     778       54294 :     ReleaseSysCacheList(catlist);
     779             : 
     780       54294 :     return result;
     781             : }
     782             : 
     783             : 
     784             : /*              ---------- AMPROC CACHES ----------                      */
     785             : 
     786             : /*
     787             :  * get_opfamily_proc
     788             :  *      Get the OID of the specified support function
     789             :  *      for the specified opfamily and datatypes.
     790             :  *
     791             :  * Returns InvalidOid if there is no pg_amproc entry for the given keys.
     792             :  */
     793             : Oid
     794      983928 : get_opfamily_proc(Oid opfamily, Oid lefttype, Oid righttype, int16 procnum)
     795             : {
     796             :     HeapTuple   tp;
     797             :     Form_pg_amproc amproc_tup;
     798             :     RegProcedure result;
     799             : 
     800      983928 :     tp = SearchSysCache4(AMPROCNUM,
     801             :                          ObjectIdGetDatum(opfamily),
     802             :                          ObjectIdGetDatum(lefttype),
     803             :                          ObjectIdGetDatum(righttype),
     804             :                          Int16GetDatum(procnum));
     805      983928 :     if (!HeapTupleIsValid(tp))
     806       69528 :         return InvalidOid;
     807      914400 :     amproc_tup = (Form_pg_amproc) GETSTRUCT(tp);
     808      914400 :     result = amproc_tup->amproc;
     809      914400 :     ReleaseSysCache(tp);
     810      914400 :     return result;
     811             : }
     812             : 
     813             : 
     814             : /*              ---------- ATTRIBUTE CACHES ----------                   */
     815             : 
     816             : /*
     817             :  * get_attname
     818             :  *      Given the relation id and the attribute number, return the "attname"
     819             :  *      field from the attribute relation as a palloc'ed string.
     820             :  *
     821             :  * If no such attribute exists and missing_ok is true, NULL is returned;
     822             :  * otherwise a not-intended-for-user-consumption error is thrown.
     823             :  */
     824             : char *
     825       39088 : get_attname(Oid relid, AttrNumber attnum, bool missing_ok)
     826             : {
     827             :     HeapTuple   tp;
     828             : 
     829       39088 :     tp = SearchSysCache2(ATTNUM,
     830             :                          ObjectIdGetDatum(relid), Int16GetDatum(attnum));
     831       39088 :     if (HeapTupleIsValid(tp))
     832             :     {
     833       39072 :         Form_pg_attribute att_tup = (Form_pg_attribute) GETSTRUCT(tp);
     834             :         char       *result;
     835             : 
     836       39072 :         result = pstrdup(NameStr(att_tup->attname));
     837       39072 :         ReleaseSysCache(tp);
     838       39072 :         return result;
     839             :     }
     840             : 
     841          16 :     if (!missing_ok)
     842           0 :         elog(ERROR, "cache lookup failed for attribute %d of relation %u",
     843             :              attnum, relid);
     844          16 :     return NULL;
     845             : }
     846             : 
     847             : /*
     848             :  * get_attnum
     849             :  *
     850             :  *      Given the relation id and the attribute name,
     851             :  *      return the "attnum" field from the attribute relation.
     852             :  *
     853             :  *      Returns InvalidAttrNumber if the attr doesn't exist (or is dropped).
     854             :  */
     855             : AttrNumber
     856        8122 : get_attnum(Oid relid, const char *attname)
     857             : {
     858             :     HeapTuple   tp;
     859             : 
     860        8122 :     tp = SearchSysCacheAttName(relid, attname);
     861        8122 :     if (HeapTupleIsValid(tp))
     862             :     {
     863        8082 :         Form_pg_attribute att_tup = (Form_pg_attribute) GETSTRUCT(tp);
     864             :         AttrNumber  result;
     865             : 
     866        8082 :         result = att_tup->attnum;
     867        8082 :         ReleaseSysCache(tp);
     868        8082 :         return result;
     869             :     }
     870             :     else
     871          40 :         return InvalidAttrNumber;
     872             : }
     873             : 
     874             : /*
     875             :  * get_attstattarget
     876             :  *
     877             :  *      Given the relation id and the attribute number,
     878             :  *      return the "attstattarget" field from the attribute relation.
     879             :  *
     880             :  *      Errors if not found.
     881             :  */
     882             : int
     883         506 : get_attstattarget(Oid relid, AttrNumber attnum)
     884             : {
     885             :     HeapTuple   tp;
     886             :     Form_pg_attribute att_tup;
     887             :     int         result;
     888             : 
     889         506 :     tp = SearchSysCache2(ATTNUM,
     890             :                          ObjectIdGetDatum(relid),
     891             :                          Int16GetDatum(attnum));
     892         506 :     if (!HeapTupleIsValid(tp))
     893           0 :         elog(ERROR, "cache lookup failed for attribute %d of relation %u",
     894             :              attnum, relid);
     895         506 :     att_tup = (Form_pg_attribute) GETSTRUCT(tp);
     896         506 :     result = att_tup->attstattarget;
     897         506 :     ReleaseSysCache(tp);
     898         506 :     return result;
     899             : }
     900             : 
     901             : /*
     902             :  * get_attgenerated
     903             :  *
     904             :  *      Given the relation id and the attribute number,
     905             :  *      return the "attgenerated" field from the attribute relation.
     906             :  *
     907             :  *      Errors if not found.
     908             :  *
     909             :  *      Since not generated is represented by '\0', this can also be used as a
     910             :  *      Boolean test.
     911             :  */
     912             : char
     913         308 : get_attgenerated(Oid relid, AttrNumber attnum)
     914             : {
     915             :     HeapTuple   tp;
     916             :     Form_pg_attribute att_tup;
     917             :     char        result;
     918             : 
     919         308 :     tp = SearchSysCache2(ATTNUM,
     920             :                          ObjectIdGetDatum(relid),
     921             :                          Int16GetDatum(attnum));
     922         308 :     if (!HeapTupleIsValid(tp))
     923           0 :         elog(ERROR, "cache lookup failed for attribute %d of relation %u",
     924             :              attnum, relid);
     925         308 :     att_tup = (Form_pg_attribute) GETSTRUCT(tp);
     926         308 :     result = att_tup->attgenerated;
     927         308 :     ReleaseSysCache(tp);
     928         308 :     return result;
     929             : }
     930             : 
     931             : /*
     932             :  * get_atttype
     933             :  *
     934             :  *      Given the relation OID and the attribute number with the relation,
     935             :  *      return the attribute type OID.
     936             :  */
     937             : Oid
     938        1158 : get_atttype(Oid relid, AttrNumber attnum)
     939             : {
     940             :     HeapTuple   tp;
     941             : 
     942        1158 :     tp = SearchSysCache2(ATTNUM,
     943             :                          ObjectIdGetDatum(relid),
     944             :                          Int16GetDatum(attnum));
     945        1158 :     if (HeapTupleIsValid(tp))
     946             :     {
     947        1158 :         Form_pg_attribute att_tup = (Form_pg_attribute) GETSTRUCT(tp);
     948             :         Oid         result;
     949             : 
     950        1158 :         result = att_tup->atttypid;
     951        1158 :         ReleaseSysCache(tp);
     952        1158 :         return result;
     953             :     }
     954             :     else
     955           0 :         return InvalidOid;
     956             : }
     957             : 
     958             : /*
     959             :  * get_atttypetypmodcoll
     960             :  *
     961             :  *      A three-fer: given the relation id and the attribute number,
     962             :  *      fetch atttypid, atttypmod, and attcollation in a single cache lookup.
     963             :  *
     964             :  * Unlike the otherwise-similar get_atttype, this routine
     965             :  * raises an error if it can't obtain the information.
     966             :  */
     967             : void
     968        7462 : get_atttypetypmodcoll(Oid relid, AttrNumber attnum,
     969             :                       Oid *typid, int32 *typmod, Oid *collid)
     970             : {
     971             :     HeapTuple   tp;
     972             :     Form_pg_attribute att_tup;
     973             : 
     974        7462 :     tp = SearchSysCache2(ATTNUM,
     975             :                          ObjectIdGetDatum(relid),
     976             :                          Int16GetDatum(attnum));
     977        7462 :     if (!HeapTupleIsValid(tp))
     978           0 :         elog(ERROR, "cache lookup failed for attribute %d of relation %u",
     979             :              attnum, relid);
     980        7462 :     att_tup = (Form_pg_attribute) GETSTRUCT(tp);
     981             : 
     982        7462 :     *typid = att_tup->atttypid;
     983        7462 :     *typmod = att_tup->atttypmod;
     984        7462 :     *collid = att_tup->attcollation;
     985        7462 :     ReleaseSysCache(tp);
     986        7462 : }
     987             : 
     988             : /*
     989             :  * get_attoptions
     990             :  *
     991             :  *      Given the relation id and the attribute number,
     992             :  *      return the attribute options text[] datum, if any.
     993             :  */
     994             : Datum
     995      681918 : get_attoptions(Oid relid, int16 attnum)
     996             : {
     997             :     HeapTuple   tuple;
     998             :     Datum       attopts;
     999             :     Datum       result;
    1000             :     bool        isnull;
    1001             : 
    1002      681918 :     tuple = SearchSysCache2(ATTNUM,
    1003             :                             ObjectIdGetDatum(relid),
    1004             :                             Int16GetDatum(attnum));
    1005             : 
    1006      681918 :     if (!HeapTupleIsValid(tuple))
    1007           0 :         elog(ERROR, "cache lookup failed for attribute %d of relation %u",
    1008             :              attnum, relid);
    1009             : 
    1010      681918 :     attopts = SysCacheGetAttr(ATTNAME, tuple, Anum_pg_attribute_attoptions,
    1011             :                               &isnull);
    1012             : 
    1013      681918 :     if (isnull)
    1014      681646 :         result = (Datum) 0;
    1015             :     else
    1016         272 :         result = datumCopy(attopts, false, -1); /* text[] */
    1017             : 
    1018      681918 :     ReleaseSysCache(tuple);
    1019             : 
    1020      681918 :     return result;
    1021             : }
    1022             : 
    1023             : /*              ---------- PG_CAST CACHE ----------                  */
    1024             : 
    1025             : /*
    1026             :  * get_cast_oid - given two type OIDs, look up a cast OID
    1027             :  *
    1028             :  * If missing_ok is false, throw an error if the cast is not found.  If
    1029             :  * true, just return InvalidOid.
    1030             :  */
    1031             : Oid
    1032          50 : get_cast_oid(Oid sourcetypeid, Oid targettypeid, bool missing_ok)
    1033             : {
    1034             :     Oid         oid;
    1035             : 
    1036          50 :     oid = GetSysCacheOid2(CASTSOURCETARGET, Anum_pg_cast_oid,
    1037             :                           ObjectIdGetDatum(sourcetypeid),
    1038             :                           ObjectIdGetDatum(targettypeid));
    1039          50 :     if (!OidIsValid(oid) && !missing_ok)
    1040           4 :         ereport(ERROR,
    1041             :                 (errcode(ERRCODE_UNDEFINED_OBJECT),
    1042             :                  errmsg("cast from type %s to type %s does not exist",
    1043             :                         format_type_be(sourcetypeid),
    1044             :                         format_type_be(targettypeid))));
    1045          46 :     return oid;
    1046             : }
    1047             : 
    1048             : /*              ---------- COLLATION CACHE ----------                    */
    1049             : 
    1050             : /*
    1051             :  * get_collation_name
    1052             :  *      Returns the name of a given pg_collation entry.
    1053             :  *
    1054             :  * Returns a palloc'd copy of the string, or NULL if no such collation.
    1055             :  *
    1056             :  * NOTE: since collation name is not unique, be wary of code that uses this
    1057             :  * for anything except preparing error messages.
    1058             :  */
    1059             : char *
    1060         114 : get_collation_name(Oid colloid)
    1061             : {
    1062             :     HeapTuple   tp;
    1063             : 
    1064         114 :     tp = SearchSysCache1(COLLOID, ObjectIdGetDatum(colloid));
    1065         114 :     if (HeapTupleIsValid(tp))
    1066             :     {
    1067         114 :         Form_pg_collation colltup = (Form_pg_collation) GETSTRUCT(tp);
    1068             :         char       *result;
    1069             : 
    1070         114 :         result = pstrdup(NameStr(colltup->collname));
    1071         114 :         ReleaseSysCache(tp);
    1072         114 :         return result;
    1073             :     }
    1074             :     else
    1075           0 :         return NULL;
    1076             : }
    1077             : 
    1078             : bool
    1079        3586 : get_collation_isdeterministic(Oid colloid)
    1080             : {
    1081             :     HeapTuple   tp;
    1082             :     Form_pg_collation colltup;
    1083             :     bool        result;
    1084             : 
    1085        3586 :     tp = SearchSysCache1(COLLOID, ObjectIdGetDatum(colloid));
    1086        3586 :     if (!HeapTupleIsValid(tp))
    1087           0 :         elog(ERROR, "cache lookup failed for collation %u", colloid);
    1088        3586 :     colltup = (Form_pg_collation) GETSTRUCT(tp);
    1089        3586 :     result = colltup->collisdeterministic;
    1090        3586 :     ReleaseSysCache(tp);
    1091        3586 :     return result;
    1092             : }
    1093             : 
    1094             : /*              ---------- CONSTRAINT CACHE ----------                   */
    1095             : 
    1096             : /*
    1097             :  * get_constraint_name
    1098             :  *      Returns the name of a given pg_constraint entry.
    1099             :  *
    1100             :  * Returns a palloc'd copy of the string, or NULL if no such constraint.
    1101             :  *
    1102             :  * NOTE: since constraint name is not unique, be wary of code that uses this
    1103             :  * for anything except preparing error messages.
    1104             :  */
    1105             : char *
    1106         416 : get_constraint_name(Oid conoid)
    1107             : {
    1108             :     HeapTuple   tp;
    1109             : 
    1110         416 :     tp = SearchSysCache1(CONSTROID, ObjectIdGetDatum(conoid));
    1111         416 :     if (HeapTupleIsValid(tp))
    1112             :     {
    1113         416 :         Form_pg_constraint contup = (Form_pg_constraint) GETSTRUCT(tp);
    1114             :         char       *result;
    1115             : 
    1116         416 :         result = pstrdup(NameStr(contup->conname));
    1117         416 :         ReleaseSysCache(tp);
    1118         416 :         return result;
    1119             :     }
    1120             :     else
    1121           0 :         return NULL;
    1122             : }
    1123             : 
    1124             : /*
    1125             :  * get_constraint_index
    1126             :  *      Given the OID of a unique, primary-key, or exclusion constraint,
    1127             :  *      return the OID of the underlying index.
    1128             :  *
    1129             :  * Return InvalidOid if the index couldn't be found; this suggests the
    1130             :  * given OID is bogus, but we leave it to caller to decide what to do.
    1131             :  */
    1132             : Oid
    1133         368 : get_constraint_index(Oid conoid)
    1134             : {
    1135             :     HeapTuple   tp;
    1136             : 
    1137         368 :     tp = SearchSysCache1(CONSTROID, ObjectIdGetDatum(conoid));
    1138         368 :     if (HeapTupleIsValid(tp))
    1139             :     {
    1140         368 :         Form_pg_constraint contup = (Form_pg_constraint) GETSTRUCT(tp);
    1141             :         Oid         result;
    1142             : 
    1143         368 :         result = contup->conindid;
    1144         368 :         ReleaseSysCache(tp);
    1145         368 :         return result;
    1146             :     }
    1147             :     else
    1148           0 :         return InvalidOid;
    1149             : }
    1150             : 
    1151             : /*              ---------- LANGUAGE CACHE ----------                     */
    1152             : 
    1153             : char *
    1154         166 : get_language_name(Oid langoid, bool missing_ok)
    1155             : {
    1156             :     HeapTuple   tp;
    1157             : 
    1158         166 :     tp = SearchSysCache1(LANGOID, ObjectIdGetDatum(langoid));
    1159         166 :     if (HeapTupleIsValid(tp))
    1160             :     {
    1161         162 :         Form_pg_language lantup = (Form_pg_language) GETSTRUCT(tp);
    1162             :         char       *result;
    1163             : 
    1164         162 :         result = pstrdup(NameStr(lantup->lanname));
    1165         162 :         ReleaseSysCache(tp);
    1166         162 :         return result;
    1167             :     }
    1168             : 
    1169           4 :     if (!missing_ok)
    1170           0 :         elog(ERROR, "cache lookup failed for language %u",
    1171             :              langoid);
    1172           4 :     return NULL;
    1173             : }
    1174             : 
    1175             : /*              ---------- OPCLASS CACHE ----------                      */
    1176             : 
    1177             : /*
    1178             :  * get_opclass_family
    1179             :  *
    1180             :  *      Returns the OID of the operator family the opclass belongs to.
    1181             :  */
    1182             : Oid
    1183      157576 : get_opclass_family(Oid opclass)
    1184             : {
    1185             :     HeapTuple   tp;
    1186             :     Form_pg_opclass cla_tup;
    1187             :     Oid         result;
    1188             : 
    1189      157576 :     tp = SearchSysCache1(CLAOID, ObjectIdGetDatum(opclass));
    1190      157576 :     if (!HeapTupleIsValid(tp))
    1191           0 :         elog(ERROR, "cache lookup failed for opclass %u", opclass);
    1192      157576 :     cla_tup = (Form_pg_opclass) GETSTRUCT(tp);
    1193             : 
    1194      157576 :     result = cla_tup->opcfamily;
    1195      157576 :     ReleaseSysCache(tp);
    1196      157576 :     return result;
    1197             : }
    1198             : 
    1199             : /*
    1200             :  * get_opclass_input_type
    1201             :  *
    1202             :  *      Returns the OID of the datatype the opclass indexes.
    1203             :  */
    1204             : Oid
    1205      158072 : get_opclass_input_type(Oid opclass)
    1206             : {
    1207             :     HeapTuple   tp;
    1208             :     Form_pg_opclass cla_tup;
    1209             :     Oid         result;
    1210             : 
    1211      158072 :     tp = SearchSysCache1(CLAOID, ObjectIdGetDatum(opclass));
    1212      158072 :     if (!HeapTupleIsValid(tp))
    1213           0 :         elog(ERROR, "cache lookup failed for opclass %u", opclass);
    1214      158072 :     cla_tup = (Form_pg_opclass) GETSTRUCT(tp);
    1215             : 
    1216      158072 :     result = cla_tup->opcintype;
    1217      158072 :     ReleaseSysCache(tp);
    1218      158072 :     return result;
    1219             : }
    1220             : 
    1221             : /*
    1222             :  * get_opclass_opfamily_and_input_type
    1223             :  *
    1224             :  *      Returns the OID of the operator family the opclass belongs to,
    1225             :  *              the OID of the datatype the opclass indexes
    1226             :  */
    1227             : bool
    1228         596 : get_opclass_opfamily_and_input_type(Oid opclass, Oid *opfamily, Oid *opcintype)
    1229             : {
    1230             :     HeapTuple   tp;
    1231             :     Form_pg_opclass cla_tup;
    1232             : 
    1233         596 :     tp = SearchSysCache1(CLAOID, ObjectIdGetDatum(opclass));
    1234         596 :     if (!HeapTupleIsValid(tp))
    1235           0 :         return false;
    1236             : 
    1237         596 :     cla_tup = (Form_pg_opclass) GETSTRUCT(tp);
    1238             : 
    1239         596 :     *opfamily = cla_tup->opcfamily;
    1240         596 :     *opcintype = cla_tup->opcintype;
    1241             : 
    1242         596 :     ReleaseSysCache(tp);
    1243             : 
    1244         596 :     return true;
    1245             : }
    1246             : 
    1247             : /*              ---------- OPERATOR CACHE ----------                     */
    1248             : 
    1249             : /*
    1250             :  * get_opcode
    1251             :  *
    1252             :  *      Returns the regproc id of the routine used to implement an
    1253             :  *      operator given the operator oid.
    1254             :  */
    1255             : RegProcedure
    1256     1024178 : get_opcode(Oid opno)
    1257             : {
    1258             :     HeapTuple   tp;
    1259             : 
    1260     1024178 :     tp = SearchSysCache1(OPEROID, ObjectIdGetDatum(opno));
    1261     1024178 :     if (HeapTupleIsValid(tp))
    1262             :     {
    1263     1024178 :         Form_pg_operator optup = (Form_pg_operator) GETSTRUCT(tp);
    1264             :         RegProcedure result;
    1265             : 
    1266     1024178 :         result = optup->oprcode;
    1267     1024178 :         ReleaseSysCache(tp);
    1268     1024178 :         return result;
    1269             :     }
    1270             :     else
    1271           0 :         return (RegProcedure) InvalidOid;
    1272             : }
    1273             : 
    1274             : /*
    1275             :  * get_opname
    1276             :  *    returns the name of the operator with the given opno
    1277             :  *
    1278             :  * Note: returns a palloc'd copy of the string, or NULL if no such operator.
    1279             :  */
    1280             : char *
    1281           8 : get_opname(Oid opno)
    1282             : {
    1283             :     HeapTuple   tp;
    1284             : 
    1285           8 :     tp = SearchSysCache1(OPEROID, ObjectIdGetDatum(opno));
    1286           8 :     if (HeapTupleIsValid(tp))
    1287             :     {
    1288           8 :         Form_pg_operator optup = (Form_pg_operator) GETSTRUCT(tp);
    1289             :         char       *result;
    1290             : 
    1291           8 :         result = pstrdup(NameStr(optup->oprname));
    1292           8 :         ReleaseSysCache(tp);
    1293           8 :         return result;
    1294             :     }
    1295             :     else
    1296           0 :         return NULL;
    1297             : }
    1298             : 
    1299             : /*
    1300             :  * get_op_rettype
    1301             :  *      Given operator oid, return the operator's result type.
    1302             :  */
    1303             : Oid
    1304          74 : get_op_rettype(Oid opno)
    1305             : {
    1306             :     HeapTuple   tp;
    1307             : 
    1308          74 :     tp = SearchSysCache1(OPEROID, ObjectIdGetDatum(opno));
    1309          74 :     if (HeapTupleIsValid(tp))
    1310             :     {
    1311          74 :         Form_pg_operator optup = (Form_pg_operator) GETSTRUCT(tp);
    1312             :         Oid         result;
    1313             : 
    1314          74 :         result = optup->oprresult;
    1315          74 :         ReleaseSysCache(tp);
    1316          74 :         return result;
    1317             :     }
    1318             :     else
    1319           0 :         return InvalidOid;
    1320             : }
    1321             : 
    1322             : /*
    1323             :  * op_input_types
    1324             :  *
    1325             :  *      Returns the left and right input datatypes for an operator
    1326             :  *      (InvalidOid if not relevant).
    1327             :  */
    1328             : void
    1329      215420 : op_input_types(Oid opno, Oid *lefttype, Oid *righttype)
    1330             : {
    1331             :     HeapTuple   tp;
    1332             :     Form_pg_operator optup;
    1333             : 
    1334      215420 :     tp = SearchSysCache1(OPEROID, ObjectIdGetDatum(opno));
    1335      215420 :     if (!HeapTupleIsValid(tp))  /* shouldn't happen */
    1336           0 :         elog(ERROR, "cache lookup failed for operator %u", opno);
    1337      215420 :     optup = (Form_pg_operator) GETSTRUCT(tp);
    1338      215420 :     *lefttype = optup->oprleft;
    1339      215420 :     *righttype = optup->oprright;
    1340      215420 :     ReleaseSysCache(tp);
    1341      215420 : }
    1342             : 
    1343             : /*
    1344             :  * op_mergejoinable
    1345             :  *
    1346             :  * Returns true if the operator is potentially mergejoinable.  (The planner
    1347             :  * will fail to find any mergejoin plans unless there are suitable btree
    1348             :  * opfamily entries for this operator and associated sortops.  The pg_operator
    1349             :  * flag is just a hint to tell the planner whether to bother looking.)
    1350             :  *
    1351             :  * In some cases (currently only array_eq and record_eq), mergejoinability
    1352             :  * depends on the specific input data type the operator is invoked for, so
    1353             :  * that must be passed as well. We currently assume that only one input's type
    1354             :  * is needed to check this --- by convention, pass the left input's data type.
    1355             :  */
    1356             : bool
    1357      303866 : op_mergejoinable(Oid opno, Oid inputtype)
    1358             : {
    1359      303866 :     bool        result = false;
    1360             :     HeapTuple   tp;
    1361             :     TypeCacheEntry *typentry;
    1362             : 
    1363             :     /*
    1364             :      * For array_eq or record_eq, we can sort if the element or field types
    1365             :      * are all sortable.  We could implement all the checks for that here, but
    1366             :      * the typcache already does that and caches the results too, so let's
    1367             :      * rely on the typcache.
    1368             :      */
    1369      303866 :     if (opno == ARRAY_EQ_OP)
    1370             :     {
    1371         100 :         typentry = lookup_type_cache(inputtype, TYPECACHE_CMP_PROC);
    1372         100 :         if (typentry->cmp_proc == F_BTARRAYCMP)
    1373         100 :             result = true;
    1374             :     }
    1375      303766 :     else if (opno == RECORD_EQ_OP)
    1376             :     {
    1377          52 :         typentry = lookup_type_cache(inputtype, TYPECACHE_CMP_PROC);
    1378          52 :         if (typentry->cmp_proc == F_BTRECORDCMP)
    1379          52 :             result = true;
    1380             :     }
    1381             :     else
    1382             :     {
    1383             :         /* For all other operators, rely on pg_operator.oprcanmerge */
    1384      303714 :         tp = SearchSysCache1(OPEROID, ObjectIdGetDatum(opno));
    1385      303714 :         if (HeapTupleIsValid(tp))
    1386             :         {
    1387      303714 :             Form_pg_operator optup = (Form_pg_operator) GETSTRUCT(tp);
    1388             : 
    1389      303714 :             result = optup->oprcanmerge;
    1390      303714 :             ReleaseSysCache(tp);
    1391             :         }
    1392             :     }
    1393      303866 :     return result;
    1394             : }
    1395             : 
    1396             : /*
    1397             :  * op_hashjoinable
    1398             :  *
    1399             :  * Returns true if the operator is hashjoinable.  (There must be a suitable
    1400             :  * hash opfamily entry for this operator if it is so marked.)
    1401             :  *
    1402             :  * In some cases (currently only array_eq), hashjoinability depends on the
    1403             :  * specific input data type the operator is invoked for, so that must be
    1404             :  * passed as well.  We currently assume that only one input's type is needed
    1405             :  * to check this --- by convention, pass the left input's data type.
    1406             :  */
    1407             : bool
    1408      208000 : op_hashjoinable(Oid opno, Oid inputtype)
    1409             : {
    1410      208000 :     bool        result = false;
    1411             :     HeapTuple   tp;
    1412             :     TypeCacheEntry *typentry;
    1413             : 
    1414             :     /* As in op_mergejoinable, let the typcache handle the hard cases */
    1415      208000 :     if (opno == ARRAY_EQ_OP)
    1416             :     {
    1417           0 :         typentry = lookup_type_cache(inputtype, TYPECACHE_HASH_PROC);
    1418           0 :         if (typentry->hash_proc == F_HASH_ARRAY)
    1419           0 :             result = true;
    1420             :     }
    1421      208000 :     else if (opno == RECORD_EQ_OP)
    1422             :     {
    1423          44 :         typentry = lookup_type_cache(inputtype, TYPECACHE_HASH_PROC);
    1424          44 :         if (typentry->hash_proc == F_HASH_RECORD)
    1425          44 :             result = true;
    1426             :     }
    1427             :     else
    1428             :     {
    1429             :         /* For all other operators, rely on pg_operator.oprcanhash */
    1430      207956 :         tp = SearchSysCache1(OPEROID, ObjectIdGetDatum(opno));
    1431      207956 :         if (HeapTupleIsValid(tp))
    1432             :         {
    1433      207956 :             Form_pg_operator optup = (Form_pg_operator) GETSTRUCT(tp);
    1434             : 
    1435      207956 :             result = optup->oprcanhash;
    1436      207956 :             ReleaseSysCache(tp);
    1437             :         }
    1438             :     }
    1439      208000 :     return result;
    1440             : }
    1441             : 
    1442             : /*
    1443             :  * op_strict
    1444             :  *
    1445             :  * Get the proisstrict flag for the operator's underlying function.
    1446             :  */
    1447             : bool
    1448       32736 : op_strict(Oid opno)
    1449             : {
    1450       32736 :     RegProcedure funcid = get_opcode(opno);
    1451             : 
    1452       32736 :     if (funcid == (RegProcedure) InvalidOid)
    1453           0 :         elog(ERROR, "operator %u does not exist", opno);
    1454             : 
    1455       32736 :     return func_strict((Oid) funcid);
    1456             : }
    1457             : 
    1458             : /*
    1459             :  * op_volatile
    1460             :  *
    1461             :  * Get the provolatile flag for the operator's underlying function.
    1462             :  */
    1463             : char
    1464       14356 : op_volatile(Oid opno)
    1465             : {
    1466       14356 :     RegProcedure funcid = get_opcode(opno);
    1467             : 
    1468       14356 :     if (funcid == (RegProcedure) InvalidOid)
    1469           0 :         elog(ERROR, "operator %u does not exist", opno);
    1470             : 
    1471       14356 :     return func_volatile((Oid) funcid);
    1472             : }
    1473             : 
    1474             : /*
    1475             :  * get_commutator
    1476             :  *
    1477             :  *      Returns the corresponding commutator of an operator.
    1478             :  */
    1479             : Oid
    1480       24672 : get_commutator(Oid opno)
    1481             : {
    1482             :     HeapTuple   tp;
    1483             : 
    1484       24672 :     tp = SearchSysCache1(OPEROID, ObjectIdGetDatum(opno));
    1485       24672 :     if (HeapTupleIsValid(tp))
    1486             :     {
    1487       24672 :         Form_pg_operator optup = (Form_pg_operator) GETSTRUCT(tp);
    1488             :         Oid         result;
    1489             : 
    1490       24672 :         result = optup->oprcom;
    1491       24672 :         ReleaseSysCache(tp);
    1492       24672 :         return result;
    1493             :     }
    1494             :     else
    1495           0 :         return InvalidOid;
    1496             : }
    1497             : 
    1498             : /*
    1499             :  * get_negator
    1500             :  *
    1501             :  *      Returns the corresponding negator of an operator.
    1502             :  */
    1503             : Oid
    1504       41564 : get_negator(Oid opno)
    1505             : {
    1506             :     HeapTuple   tp;
    1507             : 
    1508       41564 :     tp = SearchSysCache1(OPEROID, ObjectIdGetDatum(opno));
    1509       41564 :     if (HeapTupleIsValid(tp))
    1510             :     {
    1511       41564 :         Form_pg_operator optup = (Form_pg_operator) GETSTRUCT(tp);
    1512             :         Oid         result;
    1513             : 
    1514       41564 :         result = optup->oprnegate;
    1515       41564 :         ReleaseSysCache(tp);
    1516       41564 :         return result;
    1517             :     }
    1518             :     else
    1519           0 :         return InvalidOid;
    1520             : }
    1521             : 
    1522             : /*
    1523             :  * get_oprrest
    1524             :  *
    1525             :  *      Returns procedure id for computing selectivity of an operator.
    1526             :  */
    1527             : RegProcedure
    1528      535118 : get_oprrest(Oid opno)
    1529             : {
    1530             :     HeapTuple   tp;
    1531             : 
    1532      535118 :     tp = SearchSysCache1(OPEROID, ObjectIdGetDatum(opno));
    1533      535118 :     if (HeapTupleIsValid(tp))
    1534             :     {
    1535      535118 :         Form_pg_operator optup = (Form_pg_operator) GETSTRUCT(tp);
    1536             :         RegProcedure result;
    1537             : 
    1538      535118 :         result = optup->oprrest;
    1539      535118 :         ReleaseSysCache(tp);
    1540      535118 :         return result;
    1541             :     }
    1542             :     else
    1543           0 :         return (RegProcedure) InvalidOid;
    1544             : }
    1545             : 
    1546             : /*
    1547             :  * get_oprjoin
    1548             :  *
    1549             :  *      Returns procedure id for computing selectivity of a join.
    1550             :  */
    1551             : RegProcedure
    1552      105176 : get_oprjoin(Oid opno)
    1553             : {
    1554             :     HeapTuple   tp;
    1555             : 
    1556      105176 :     tp = SearchSysCache1(OPEROID, ObjectIdGetDatum(opno));
    1557      105176 :     if (HeapTupleIsValid(tp))
    1558             :     {
    1559      105176 :         Form_pg_operator optup = (Form_pg_operator) GETSTRUCT(tp);
    1560             :         RegProcedure result;
    1561             : 
    1562      105176 :         result = optup->oprjoin;
    1563      105176 :         ReleaseSysCache(tp);
    1564      105176 :         return result;
    1565             :     }
    1566             :     else
    1567           0 :         return (RegProcedure) InvalidOid;
    1568             : }
    1569             : 
    1570             : /*              ---------- FUNCTION CACHE ----------                     */
    1571             : 
    1572             : /*
    1573             :  * get_func_name
    1574             :  *    returns the name of the function with the given funcid
    1575             :  *
    1576             :  * Note: returns a palloc'd copy of the string, or NULL if no such function.
    1577             :  */
    1578             : char *
    1579         340 : get_func_name(Oid funcid)
    1580             : {
    1581             :     HeapTuple   tp;
    1582             : 
    1583         340 :     tp = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
    1584         340 :     if (HeapTupleIsValid(tp))
    1585             :     {
    1586         340 :         Form_pg_proc functup = (Form_pg_proc) GETSTRUCT(tp);
    1587             :         char       *result;
    1588             : 
    1589         340 :         result = pstrdup(NameStr(functup->proname));
    1590         340 :         ReleaseSysCache(tp);
    1591         340 :         return result;
    1592             :     }
    1593             :     else
    1594           0 :         return NULL;
    1595             : }
    1596             : 
    1597             : /*
    1598             :  * get_func_namespace
    1599             :  *
    1600             :  *      Returns the pg_namespace OID associated with a given function.
    1601             :  */
    1602             : Oid
    1603          60 : get_func_namespace(Oid funcid)
    1604             : {
    1605             :     HeapTuple   tp;
    1606             : 
    1607          60 :     tp = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
    1608          60 :     if (HeapTupleIsValid(tp))
    1609             :     {
    1610          60 :         Form_pg_proc functup = (Form_pg_proc) GETSTRUCT(tp);
    1611             :         Oid         result;
    1612             : 
    1613          60 :         result = functup->pronamespace;
    1614          60 :         ReleaseSysCache(tp);
    1615          60 :         return result;
    1616             :     }
    1617             :     else
    1618           0 :         return InvalidOid;
    1619             : }
    1620             : 
    1621             : /*
    1622             :  * get_func_rettype
    1623             :  *      Given procedure id, return the function's result type.
    1624             :  */
    1625             : Oid
    1626       16294 : get_func_rettype(Oid funcid)
    1627             : {
    1628             :     HeapTuple   tp;
    1629             :     Oid         result;
    1630             : 
    1631       16294 :     tp = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
    1632       16294 :     if (!HeapTupleIsValid(tp))
    1633           0 :         elog(ERROR, "cache lookup failed for function %u", funcid);
    1634             : 
    1635       16294 :     result = ((Form_pg_proc) GETSTRUCT(tp))->prorettype;
    1636       16294 :     ReleaseSysCache(tp);
    1637       16294 :     return result;
    1638             : }
    1639             : 
    1640             : /*
    1641             :  * get_func_nargs
    1642             :  *      Given procedure id, return the number of arguments.
    1643             :  */
    1644             : int
    1645           0 : get_func_nargs(Oid funcid)
    1646             : {
    1647             :     HeapTuple   tp;
    1648             :     int         result;
    1649             : 
    1650           0 :     tp = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
    1651           0 :     if (!HeapTupleIsValid(tp))
    1652           0 :         elog(ERROR, "cache lookup failed for function %u", funcid);
    1653             : 
    1654           0 :     result = ((Form_pg_proc) GETSTRUCT(tp))->pronargs;
    1655           0 :     ReleaseSysCache(tp);
    1656           0 :     return result;
    1657             : }
    1658             : 
    1659             : /*
    1660             :  * get_func_signature
    1661             :  *      Given procedure id, return the function's argument and result types.
    1662             :  *      (The return value is the result type.)
    1663             :  *
    1664             :  * The arguments are returned as a palloc'd array.
    1665             :  */
    1666             : Oid
    1667         768 : get_func_signature(Oid funcid, Oid **argtypes, int *nargs)
    1668             : {
    1669             :     HeapTuple   tp;
    1670             :     Form_pg_proc procstruct;
    1671             :     Oid         result;
    1672             : 
    1673         768 :     tp = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
    1674         768 :     if (!HeapTupleIsValid(tp))
    1675           0 :         elog(ERROR, "cache lookup failed for function %u", funcid);
    1676             : 
    1677         768 :     procstruct = (Form_pg_proc) GETSTRUCT(tp);
    1678             : 
    1679         768 :     result = procstruct->prorettype;
    1680         768 :     *nargs = (int) procstruct->pronargs;
    1681             :     Assert(*nargs == procstruct->proargtypes.dim1);
    1682         768 :     *argtypes = (Oid *) palloc(*nargs * sizeof(Oid));
    1683         768 :     memcpy(*argtypes, procstruct->proargtypes.values, *nargs * sizeof(Oid));
    1684             : 
    1685         768 :     ReleaseSysCache(tp);
    1686         768 :     return result;
    1687             : }
    1688             : 
    1689             : /*
    1690             :  * get_func_variadictype
    1691             :  *      Given procedure id, return the function's provariadic field.
    1692             :  */
    1693             : Oid
    1694         190 : get_func_variadictype(Oid funcid)
    1695             : {
    1696             :     HeapTuple   tp;
    1697             :     Oid         result;
    1698             : 
    1699         190 :     tp = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
    1700         190 :     if (!HeapTupleIsValid(tp))
    1701           0 :         elog(ERROR, "cache lookup failed for function %u", funcid);
    1702             : 
    1703         190 :     result = ((Form_pg_proc) GETSTRUCT(tp))->provariadic;
    1704         190 :     ReleaseSysCache(tp);
    1705         190 :     return result;
    1706             : }
    1707             : 
    1708             : /*
    1709             :  * get_func_retset
    1710             :  *      Given procedure id, return the function's proretset flag.
    1711             :  */
    1712             : bool
    1713      718034 : get_func_retset(Oid funcid)
    1714             : {
    1715             :     HeapTuple   tp;
    1716             :     bool        result;
    1717             : 
    1718      718034 :     tp = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
    1719      718034 :     if (!HeapTupleIsValid(tp))
    1720           0 :         elog(ERROR, "cache lookup failed for function %u", funcid);
    1721             : 
    1722      718034 :     result = ((Form_pg_proc) GETSTRUCT(tp))->proretset;
    1723      718034 :     ReleaseSysCache(tp);
    1724      718034 :     return result;
    1725             : }
    1726             : 
    1727             : /*
    1728             :  * func_strict
    1729             :  *      Given procedure id, return the function's proisstrict flag.
    1730             :  */
    1731             : bool
    1732      171374 : func_strict(Oid funcid)
    1733             : {
    1734             :     HeapTuple   tp;
    1735             :     bool        result;
    1736             : 
    1737      171374 :     tp = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
    1738      171374 :     if (!HeapTupleIsValid(tp))
    1739           0 :         elog(ERROR, "cache lookup failed for function %u", funcid);
    1740             : 
    1741      171374 :     result = ((Form_pg_proc) GETSTRUCT(tp))->proisstrict;
    1742      171374 :     ReleaseSysCache(tp);
    1743      171374 :     return result;
    1744             : }
    1745             : 
    1746             : /*
    1747             :  * func_volatile
    1748             :  *      Given procedure id, return the function's provolatile flag.
    1749             :  */
    1750             : char
    1751      509698 : func_volatile(Oid funcid)
    1752             : {
    1753             :     HeapTuple   tp;
    1754             :     char        result;
    1755             : 
    1756      509698 :     tp = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
    1757      509698 :     if (!HeapTupleIsValid(tp))
    1758           0 :         elog(ERROR, "cache lookup failed for function %u", funcid);
    1759             : 
    1760      509698 :     result = ((Form_pg_proc) GETSTRUCT(tp))->provolatile;
    1761      509698 :     ReleaseSysCache(tp);
    1762      509698 :     return result;
    1763             : }
    1764             : 
    1765             : /*
    1766             :  * func_parallel
    1767             :  *      Given procedure id, return the function's proparallel flag.
    1768             :  */
    1769             : char
    1770      678238 : func_parallel(Oid funcid)
    1771             : {
    1772             :     HeapTuple   tp;
    1773             :     char        result;
    1774             : 
    1775      678238 :     tp = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
    1776      678238 :     if (!HeapTupleIsValid(tp))
    1777           0 :         elog(ERROR, "cache lookup failed for function %u", funcid);
    1778             : 
    1779      678238 :     result = ((Form_pg_proc) GETSTRUCT(tp))->proparallel;
    1780      678238 :     ReleaseSysCache(tp);
    1781      678238 :     return result;
    1782             : }
    1783             : 
    1784             : /*
    1785             :  * get_func_prokind
    1786             :  *     Given procedure id, return the routine kind.
    1787             :  */
    1788             : char
    1789       56558 : get_func_prokind(Oid funcid)
    1790             : {
    1791             :     HeapTuple   tp;
    1792             :     char        result;
    1793             : 
    1794       56558 :     tp = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
    1795       56558 :     if (!HeapTupleIsValid(tp))
    1796           0 :         elog(ERROR, "cache lookup failed for function %u", funcid);
    1797             : 
    1798       56558 :     result = ((Form_pg_proc) GETSTRUCT(tp))->prokind;
    1799       56558 :     ReleaseSysCache(tp);
    1800       56558 :     return result;
    1801             : }
    1802             : 
    1803             : /*
    1804             :  * get_func_leakproof
    1805             :  *     Given procedure id, return the function's leakproof field.
    1806             :  */
    1807             : bool
    1808        3000 : get_func_leakproof(Oid funcid)
    1809             : {
    1810             :     HeapTuple   tp;
    1811             :     bool        result;
    1812             : 
    1813        3000 :     tp = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
    1814        3000 :     if (!HeapTupleIsValid(tp))
    1815           0 :         elog(ERROR, "cache lookup failed for function %u", funcid);
    1816             : 
    1817        3000 :     result = ((Form_pg_proc) GETSTRUCT(tp))->proleakproof;
    1818        3000 :     ReleaseSysCache(tp);
    1819        3000 :     return result;
    1820             : }
    1821             : 
    1822             : /*
    1823             :  * get_func_support
    1824             :  *
    1825             :  *      Returns the support function OID associated with a given function,
    1826             :  *      or InvalidOid if there is none.
    1827             :  */
    1828             : RegProcedure
    1829       15744 : get_func_support(Oid funcid)
    1830             : {
    1831             :     HeapTuple   tp;
    1832             : 
    1833       15744 :     tp = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
    1834       15744 :     if (HeapTupleIsValid(tp))
    1835             :     {
    1836       15744 :         Form_pg_proc functup = (Form_pg_proc) GETSTRUCT(tp);
    1837             :         RegProcedure result;
    1838             : 
    1839       15744 :         result = functup->prosupport;
    1840       15744 :         ReleaseSysCache(tp);
    1841       15744 :         return result;
    1842             :     }
    1843             :     else
    1844           0 :         return (RegProcedure) InvalidOid;
    1845             : }
    1846             : 
    1847             : /*              ---------- RELATION CACHE ----------                     */
    1848             : 
    1849             : /*
    1850             :  * get_relname_relid
    1851             :  *      Given name and namespace of a relation, look up the OID.
    1852             :  *
    1853             :  * Returns InvalidOid if there is no such relation.
    1854             :  */
    1855             : Oid
    1856     1567026 : get_relname_relid(const char *relname, Oid relnamespace)
    1857             : {
    1858     1567026 :     return GetSysCacheOid2(RELNAMENSP, Anum_pg_class_oid,
    1859             :                            PointerGetDatum(relname),
    1860             :                            ObjectIdGetDatum(relnamespace));
    1861             : }
    1862             : 
    1863             : #ifdef NOT_USED
    1864             : /*
    1865             :  * get_relnatts
    1866             :  *
    1867             :  *      Returns the number of attributes for a given relation.
    1868             :  */
    1869             : int
    1870             : get_relnatts(Oid relid)
    1871             : {
    1872             :     HeapTuple   tp;
    1873             : 
    1874             :     tp = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
    1875             :     if (HeapTupleIsValid(tp))
    1876             :     {
    1877             :         Form_pg_class reltup = (Form_pg_class) GETSTRUCT(tp);
    1878             :         int         result;
    1879             : 
    1880             :         result = reltup->relnatts;
    1881             :         ReleaseSysCache(tp);
    1882             :         return result;
    1883             :     }
    1884             :     else
    1885             :         return InvalidAttrNumber;
    1886             : }
    1887             : #endif
    1888             : 
    1889             : /*
    1890             :  * get_rel_name
    1891             :  *      Returns the name of a given relation.
    1892             :  *
    1893             :  * Returns a palloc'd copy of the string, or NULL if no such relation.
    1894             :  *
    1895             :  * NOTE: since relation name is not unique, be wary of code that uses this
    1896             :  * for anything except preparing error messages.
    1897             :  */
    1898             : char *
    1899       57190 : get_rel_name(Oid relid)
    1900             : {
    1901             :     HeapTuple   tp;
    1902             : 
    1903       57190 :     tp = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
    1904       57190 :     if (HeapTupleIsValid(tp))
    1905             :     {
    1906       57182 :         Form_pg_class reltup = (Form_pg_class) GETSTRUCT(tp);
    1907             :         char       *result;
    1908             : 
    1909       57182 :         result = pstrdup(NameStr(reltup->relname));
    1910       57182 :         ReleaseSysCache(tp);
    1911       57182 :         return result;
    1912             :     }
    1913             :     else
    1914           8 :         return NULL;
    1915             : }
    1916             : 
    1917             : /*
    1918             :  * get_rel_namespace
    1919             :  *
    1920             :  *      Returns the pg_namespace OID associated with a given relation.
    1921             :  */
    1922             : Oid
    1923      312696 : get_rel_namespace(Oid relid)
    1924             : {
    1925             :     HeapTuple   tp;
    1926             : 
    1927      312696 :     tp = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
    1928      312696 :     if (HeapTupleIsValid(tp))
    1929             :     {
    1930      312696 :         Form_pg_class reltup = (Form_pg_class) GETSTRUCT(tp);
    1931             :         Oid         result;
    1932             : 
    1933      312696 :         result = reltup->relnamespace;
    1934      312696 :         ReleaseSysCache(tp);
    1935      312696 :         return result;
    1936             :     }
    1937             :     else
    1938           0 :         return InvalidOid;
    1939             : }
    1940             : 
    1941             : /*
    1942             :  * get_rel_type_id
    1943             :  *
    1944             :  *      Returns the pg_type OID associated with a given relation.
    1945             :  *
    1946             :  * Note: not all pg_class entries have associated pg_type OIDs; so be
    1947             :  * careful to check for InvalidOid result.
    1948             :  */
    1949             : Oid
    1950       28826 : get_rel_type_id(Oid relid)
    1951             : {
    1952             :     HeapTuple   tp;
    1953             : 
    1954       28826 :     tp = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
    1955       28826 :     if (HeapTupleIsValid(tp))
    1956             :     {
    1957       28826 :         Form_pg_class reltup = (Form_pg_class) GETSTRUCT(tp);
    1958             :         Oid         result;
    1959             : 
    1960       28826 :         result = reltup->reltype;
    1961       28826 :         ReleaseSysCache(tp);
    1962       28826 :         return result;
    1963             :     }
    1964             :     else
    1965           0 :         return InvalidOid;
    1966             : }
    1967             : 
    1968             : /*
    1969             :  * get_rel_relkind
    1970             :  *
    1971             :  *      Returns the relkind associated with a given relation.
    1972             :  */
    1973             : char
    1974      401626 : get_rel_relkind(Oid relid)
    1975             : {
    1976             :     HeapTuple   tp;
    1977             : 
    1978      401626 :     tp = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
    1979      401626 :     if (HeapTupleIsValid(tp))
    1980             :     {
    1981      401626 :         Form_pg_class reltup = (Form_pg_class) GETSTRUCT(tp);
    1982             :         char        result;
    1983             : 
    1984      401626 :         result = reltup->relkind;
    1985      401626 :         ReleaseSysCache(tp);
    1986      401626 :         return result;
    1987             :     }
    1988             :     else
    1989           0 :         return '\0';
    1990             : }
    1991             : 
    1992             : /*
    1993             :  * get_rel_relispartition
    1994             :  *
    1995             :  *      Returns the relispartition flag associated with a given relation.
    1996             :  */
    1997             : bool
    1998      307832 : get_rel_relispartition(Oid relid)
    1999             : {
    2000             :     HeapTuple   tp;
    2001             : 
    2002      307832 :     tp = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
    2003      307832 :     if (HeapTupleIsValid(tp))
    2004             :     {
    2005      307832 :         Form_pg_class reltup = (Form_pg_class) GETSTRUCT(tp);
    2006             :         bool        result;
    2007             : 
    2008      307832 :         result = reltup->relispartition;
    2009      307832 :         ReleaseSysCache(tp);
    2010      307832 :         return result;
    2011             :     }
    2012             :     else
    2013           0 :         return false;
    2014             : }
    2015             : 
    2016             : /*
    2017             :  * get_rel_tablespace
    2018             :  *
    2019             :  *      Returns the pg_tablespace OID associated with a given relation.
    2020             :  *
    2021             :  * Note: InvalidOid might mean either that we couldn't find the relation,
    2022             :  * or that it is in the database's default tablespace.
    2023             :  */
    2024             : Oid
    2025        5012 : get_rel_tablespace(Oid relid)
    2026             : {
    2027             :     HeapTuple   tp;
    2028             : 
    2029        5012 :     tp = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
    2030        5012 :     if (HeapTupleIsValid(tp))
    2031             :     {
    2032        5012 :         Form_pg_class reltup = (Form_pg_class) GETSTRUCT(tp);
    2033             :         Oid         result;
    2034             : 
    2035        5012 :         result = reltup->reltablespace;
    2036        5012 :         ReleaseSysCache(tp);
    2037        5012 :         return result;
    2038             :     }
    2039             :     else
    2040           0 :         return InvalidOid;
    2041             : }
    2042             : 
    2043             : /*
    2044             :  * get_rel_persistence
    2045             :  *
    2046             :  *      Returns the relpersistence associated with a given relation.
    2047             :  */
    2048             : char
    2049      180450 : get_rel_persistence(Oid relid)
    2050             : {
    2051             :     HeapTuple   tp;
    2052             :     Form_pg_class reltup;
    2053             :     char        result;
    2054             : 
    2055      180450 :     tp = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
    2056      180450 :     if (!HeapTupleIsValid(tp))
    2057           0 :         elog(ERROR, "cache lookup failed for relation %u", relid);
    2058      180450 :     reltup = (Form_pg_class) GETSTRUCT(tp);
    2059      180450 :     result = reltup->relpersistence;
    2060      180450 :     ReleaseSysCache(tp);
    2061             : 
    2062      180450 :     return result;
    2063             : }
    2064             : 
    2065             : 
    2066             : /*              ---------- TRANSFORM CACHE ----------                        */
    2067             : 
    2068             : Oid
    2069        1544 : get_transform_fromsql(Oid typid, Oid langid, List *trftypes)
    2070             : {
    2071             :     HeapTuple   tup;
    2072             : 
    2073        1544 :     if (!list_member_oid(trftypes, typid))
    2074        1382 :         return InvalidOid;
    2075             : 
    2076         162 :     tup = SearchSysCache2(TRFTYPELANG, typid, langid);
    2077         162 :     if (HeapTupleIsValid(tup))
    2078             :     {
    2079             :         Oid         funcid;
    2080             : 
    2081         162 :         funcid = ((Form_pg_transform) GETSTRUCT(tup))->trffromsql;
    2082         162 :         ReleaseSysCache(tup);
    2083         162 :         return funcid;
    2084             :     }
    2085             :     else
    2086           0 :         return InvalidOid;
    2087             : }
    2088             : 
    2089             : Oid
    2090        2014 : get_transform_tosql(Oid typid, Oid langid, List *trftypes)
    2091             : {
    2092             :     HeapTuple   tup;
    2093             : 
    2094        2014 :     if (!list_member_oid(trftypes, typid))
    2095        1834 :         return InvalidOid;
    2096             : 
    2097         180 :     tup = SearchSysCache2(TRFTYPELANG, typid, langid);
    2098         180 :     if (HeapTupleIsValid(tup))
    2099             :     {
    2100             :         Oid         funcid;
    2101             : 
    2102         180 :         funcid = ((Form_pg_transform) GETSTRUCT(tup))->trftosql;
    2103         180 :         ReleaseSysCache(tup);
    2104         180 :         return funcid;
    2105             :     }
    2106             :     else
    2107           0 :         return InvalidOid;
    2108             : }
    2109             : 
    2110             : 
    2111             : /*              ---------- TYPE CACHE ----------                         */
    2112             : 
    2113             : /*
    2114             :  * get_typisdefined
    2115             :  *
    2116             :  *      Given the type OID, determine whether the type is defined
    2117             :  *      (if not, it's only a shell).
    2118             :  */
    2119             : bool
    2120         216 : get_typisdefined(Oid typid)
    2121             : {
    2122             :     HeapTuple   tp;
    2123             : 
    2124         216 :     tp = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typid));
    2125         216 :     if (HeapTupleIsValid(tp))
    2126             :     {
    2127         216 :         Form_pg_type typtup = (Form_pg_type) GETSTRUCT(tp);
    2128             :         bool        result;
    2129             : 
    2130         216 :         result = typtup->typisdefined;
    2131         216 :         ReleaseSysCache(tp);
    2132         216 :         return result;
    2133             :     }
    2134             :     else
    2135           0 :         return false;
    2136             : }
    2137             : 
    2138             : /*
    2139             :  * get_typlen
    2140             :  *
    2141             :  *      Given the type OID, return the length of the type.
    2142             :  */
    2143             : int16
    2144     1593384 : get_typlen(Oid typid)
    2145             : {
    2146             :     HeapTuple   tp;
    2147             : 
    2148     1593384 :     tp = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typid));
    2149     1593384 :     if (HeapTupleIsValid(tp))
    2150             :     {
    2151     1593384 :         Form_pg_type typtup = (Form_pg_type) GETSTRUCT(tp);
    2152             :         int16       result;
    2153             : 
    2154     1593384 :         result = typtup->typlen;
    2155     1593384 :         ReleaseSysCache(tp);
    2156     1593384 :         return result;
    2157             :     }
    2158             :     else
    2159           0 :         return 0;
    2160             : }
    2161             : 
    2162             : /*
    2163             :  * get_typbyval
    2164             :  *
    2165             :  *      Given the type OID, determine whether the type is returned by value or
    2166             :  *      not.  Returns true if by value, false if by reference.
    2167             :  */
    2168             : bool
    2169       22516 : get_typbyval(Oid typid)
    2170             : {
    2171             :     HeapTuple   tp;
    2172             : 
    2173       22516 :     tp = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typid));
    2174       22516 :     if (HeapTupleIsValid(tp))
    2175             :     {
    2176       22516 :         Form_pg_type typtup = (Form_pg_type) GETSTRUCT(tp);
    2177             :         bool        result;
    2178             : 
    2179       22516 :         result = typtup->typbyval;
    2180       22516 :         ReleaseSysCache(tp);
    2181       22516 :         return result;
    2182             :     }
    2183             :     else
    2184           0 :         return false;
    2185             : }
    2186             : 
    2187             : /*
    2188             :  * get_typlenbyval
    2189             :  *
    2190             :  *      A two-fer: given the type OID, return both typlen and typbyval.
    2191             :  *
    2192             :  *      Since both pieces of info are needed to know how to copy a Datum,
    2193             :  *      many places need both.  Might as well get them with one cache lookup
    2194             :  *      instead of two.  Also, this routine raises an error instead of
    2195             :  *      returning a bogus value when given a bad type OID.
    2196             :  */
    2197             : void
    2198      632950 : get_typlenbyval(Oid typid, int16 *typlen, bool *typbyval)
    2199             : {
    2200             :     HeapTuple   tp;
    2201             :     Form_pg_type typtup;
    2202             : 
    2203      632950 :     tp = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typid));
    2204      632950 :     if (!HeapTupleIsValid(tp))
    2205           0 :         elog(ERROR, "cache lookup failed for type %u", typid);
    2206      632950 :     typtup = (Form_pg_type) GETSTRUCT(tp);
    2207      632950 :     *typlen = typtup->typlen;
    2208      632950 :     *typbyval = typtup->typbyval;
    2209      632950 :     ReleaseSysCache(tp);
    2210      632950 : }
    2211             : 
    2212             : /*
    2213             :  * get_typlenbyvalalign
    2214             :  *
    2215             :  *      A three-fer: given the type OID, return typlen, typbyval, typalign.
    2216             :  */
    2217             : void
    2218     1377310 : get_typlenbyvalalign(Oid typid, int16 *typlen, bool *typbyval,
    2219             :                      char *typalign)
    2220             : {
    2221             :     HeapTuple   tp;
    2222             :     Form_pg_type typtup;
    2223             : 
    2224     1377310 :     tp = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typid));
    2225     1377310 :     if (!HeapTupleIsValid(tp))
    2226           0 :         elog(ERROR, "cache lookup failed for type %u", typid);
    2227     1377310 :     typtup = (Form_pg_type) GETSTRUCT(tp);
    2228     1377310 :     *typlen = typtup->typlen;
    2229     1377310 :     *typbyval = typtup->typbyval;
    2230     1377310 :     *typalign = typtup->typalign;
    2231     1377310 :     ReleaseSysCache(tp);
    2232     1377310 : }
    2233             : 
    2234             : /*
    2235             :  * getTypeIOParam
    2236             :  *      Given a pg_type row, select the type OID to pass to I/O functions
    2237             :  *
    2238             :  * Formerly, all I/O functions were passed pg_type.typelem as their second
    2239             :  * parameter, but we now have a more complex rule about what to pass.
    2240             :  * This knowledge is intended to be centralized here --- direct references
    2241             :  * to typelem elsewhere in the code are wrong, if they are associated with
    2242             :  * I/O calls and not with actual subscripting operations!  (But see
    2243             :  * bootstrap.c's boot_get_type_io_data() if you need to change this.)
    2244             :  *
    2245             :  * As of PostgreSQL 8.1, output functions receive only the value itself
    2246             :  * and not any auxiliary parameters, so the name of this routine is now
    2247             :  * a bit of a misnomer ... it should be getTypeInputParam.
    2248             :  */
    2249             : Oid
    2250     1850644 : getTypeIOParam(HeapTuple typeTuple)
    2251             : {
    2252     1850644 :     Form_pg_type typeStruct = (Form_pg_type) GETSTRUCT(typeTuple);
    2253             : 
    2254             :     /*
    2255             :      * Array types get their typelem as parameter; everybody else gets their
    2256             :      * own type OID as parameter.
    2257             :      */
    2258     1850644 :     if (OidIsValid(typeStruct->typelem))
    2259      127552 :         return typeStruct->typelem;
    2260             :     else
    2261     1723092 :         return typeStruct->oid;
    2262             : }
    2263             : 
    2264             : /*
    2265             :  * get_type_io_data
    2266             :  *
    2267             :  *      A six-fer:  given the type OID, return typlen, typbyval, typalign,
    2268             :  *                  typdelim, typioparam, and IO function OID. The IO function
    2269             :  *                  returned is controlled by IOFuncSelector
    2270             :  */
    2271             : void
    2272      213410 : get_type_io_data(Oid typid,
    2273             :                  IOFuncSelector which_func,
    2274             :                  int16 *typlen,
    2275             :                  bool *typbyval,
    2276             :                  char *typalign,
    2277             :                  char *typdelim,
    2278             :                  Oid *typioparam,
    2279             :                  Oid *func)
    2280             : {
    2281             :     HeapTuple   typeTuple;
    2282             :     Form_pg_type typeStruct;
    2283             : 
    2284             :     /*
    2285             :      * In bootstrap mode, pass it off to bootstrap.c.  This hack allows us to
    2286             :      * use array_in and array_out during bootstrap.
    2287             :      */
    2288      213410 :     if (IsBootstrapProcessingMode())
    2289             :     {
    2290             :         Oid         typinput;
    2291             :         Oid         typoutput;
    2292             : 
    2293      175930 :         boot_get_type_io_data(typid,
    2294             :                               typlen,
    2295             :                               typbyval,
    2296             :                               typalign,
    2297             :                               typdelim,
    2298             :                               typioparam,
    2299             :                               &typinput,
    2300             :                               &typoutput);
    2301      175930 :         switch (which_func)
    2302             :         {
    2303      175930 :             case IOFunc_input:
    2304      175930 :                 *func = typinput;
    2305      175930 :                 break;
    2306           0 :             case IOFunc_output:
    2307           0 :                 *func = typoutput;
    2308           0 :                 break;
    2309           0 :             default:
    2310           0 :                 elog(ERROR, "binary I/O not supported during bootstrap");
    2311             :                 break;
    2312             :         }
    2313      175930 :         return;
    2314             :     }
    2315             : 
    2316       37480 :     typeTuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typid));
    2317       37480 :     if (!HeapTupleIsValid(typeTuple))
    2318           0 :         elog(ERROR, "cache lookup failed for type %u", typid);
    2319       37480 :     typeStruct = (Form_pg_type) GETSTRUCT(typeTuple);
    2320             : 
    2321       37480 :     *typlen = typeStruct->typlen;
    2322       37480 :     *typbyval = typeStruct->typbyval;
    2323       37480 :     *typalign = typeStruct->typalign;
    2324       37480 :     *typdelim = typeStruct->typdelim;
    2325       37480 :     *typioparam = getTypeIOParam(typeTuple);
    2326       37480 :     switch (which_func)
    2327             :     {
    2328       19682 :         case IOFunc_input:
    2329       19682 :             *func = typeStruct->typinput;
    2330       19682 :             break;
    2331       17738 :         case IOFunc_output:
    2332       17738 :             *func = typeStruct->typoutput;
    2333       17738 :             break;
    2334          34 :         case IOFunc_receive:
    2335          34 :             *func = typeStruct->typreceive;
    2336          34 :             break;
    2337          26 :         case IOFunc_send:
    2338          26 :             *func = typeStruct->typsend;
    2339          26 :             break;
    2340             :     }
    2341       37480 :     ReleaseSysCache(typeTuple);
    2342             : }
    2343             : 
    2344             : #ifdef NOT_USED
    2345             : char
    2346             : get_typalign(Oid typid)
    2347             : {
    2348             :     HeapTuple   tp;
    2349             : 
    2350             :     tp = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typid));
    2351             :     if (HeapTupleIsValid(tp))
    2352             :     {
    2353             :         Form_pg_type typtup = (Form_pg_type) GETSTRUCT(tp);
    2354             :         char        result;
    2355             : 
    2356             :         result = typtup->typalign;
    2357             :         ReleaseSysCache(tp);
    2358             :         return result;
    2359             :     }
    2360             :     else
    2361             :         return TYPALIGN_INT;
    2362             : }
    2363             : #endif
    2364             : 
    2365             : char
    2366       33016 : get_typstorage(Oid typid)
    2367             : {
    2368             :     HeapTuple   tp;
    2369             : 
    2370       33016 :     tp = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typid));
    2371       33016 :     if (HeapTupleIsValid(tp))
    2372             :     {
    2373       33016 :         Form_pg_type typtup = (Form_pg_type) GETSTRUCT(tp);
    2374             :         char        result;
    2375             : 
    2376       33016 :         result = typtup->typstorage;
    2377       33016 :         ReleaseSysCache(tp);
    2378       33016 :         return result;
    2379             :     }
    2380             :     else
    2381           0 :         return TYPSTORAGE_PLAIN;
    2382             : }
    2383             : 
    2384             : /*
    2385             :  * get_typdefault
    2386             :  *    Given a type OID, return the type's default value, if any.
    2387             :  *
    2388             :  *    The result is a palloc'd expression node tree, or NULL if there
    2389             :  *    is no defined default for the datatype.
    2390             :  *
    2391             :  * NB: caller should be prepared to coerce result to correct datatype;
    2392             :  * the returned expression tree might produce something of the wrong type.
    2393             :  */
    2394             : Node *
    2395       52378 : get_typdefault(Oid typid)
    2396             : {
    2397             :     HeapTuple   typeTuple;
    2398             :     Form_pg_type type;
    2399             :     Datum       datum;
    2400             :     bool        isNull;
    2401             :     Node       *expr;
    2402             : 
    2403       52378 :     typeTuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typid));
    2404       52378 :     if (!HeapTupleIsValid(typeTuple))
    2405           0 :         elog(ERROR, "cache lookup failed for type %u", typid);
    2406       52378 :     type = (Form_pg_type) GETSTRUCT(typeTuple);
    2407             : 
    2408             :     /*
    2409             :      * typdefault and typdefaultbin are potentially null, so don't try to
    2410             :      * access 'em as struct fields. Must do it the hard way with
    2411             :      * SysCacheGetAttr.
    2412             :      */
    2413       52378 :     datum = SysCacheGetAttr(TYPEOID,
    2414             :                             typeTuple,
    2415             :                             Anum_pg_type_typdefaultbin,
    2416             :                             &isNull);
    2417             : 
    2418       52378 :     if (!isNull)
    2419             :     {
    2420             :         /* We have an expression default */
    2421         138 :         expr = stringToNode(TextDatumGetCString(datum));
    2422             :     }
    2423             :     else
    2424             :     {
    2425             :         /* Perhaps we have a plain literal default */
    2426       52240 :         datum = SysCacheGetAttr(TYPEOID,
    2427             :                                 typeTuple,
    2428             :                                 Anum_pg_type_typdefault,
    2429             :                                 &isNull);
    2430             : 
    2431       52240 :         if (!isNull)
    2432             :         {
    2433             :             char       *strDefaultVal;
    2434             : 
    2435             :             /* Convert text datum to C string */
    2436           8 :             strDefaultVal = TextDatumGetCString(datum);
    2437             :             /* Convert C string to a value of the given type */
    2438           8 :             datum = OidInputFunctionCall(type->typinput, strDefaultVal,
    2439             :                                          getTypeIOParam(typeTuple), -1);
    2440             :             /* Build a Const node containing the value */
    2441           8 :             expr = (Node *) makeConst(typid,
    2442             :                                       -1,
    2443             :                                       type->typcollation,
    2444           8 :                                       type->typlen,
    2445             :                                       datum,
    2446             :                                       false,
    2447           8 :                                       type->typbyval);
    2448           8 :             pfree(strDefaultVal);
    2449             :         }
    2450             :         else
    2451             :         {
    2452             :             /* No default */
    2453       52232 :             expr = NULL;
    2454             :         }
    2455             :     }
    2456             : 
    2457       52378 :     ReleaseSysCache(typeTuple);
    2458             : 
    2459       52378 :     return expr;
    2460             : }
    2461             : 
    2462             : /*
    2463             :  * getBaseType
    2464             :  *      If the given type is a domain, return its base type;
    2465             :  *      otherwise return the type's own OID.
    2466             :  */
    2467             : Oid
    2468     7867748 : getBaseType(Oid typid)
    2469             : {
    2470     7867748 :     int32       typmod = -1;
    2471             : 
    2472     7867748 :     return getBaseTypeAndTypmod(typid, &typmod);
    2473             : }
    2474             : 
    2475             : /*
    2476             :  * getBaseTypeAndTypmod
    2477             :  *      If the given type is a domain, return its base type and typmod;
    2478             :  *      otherwise return the type's own OID, and leave *typmod unchanged.
    2479             :  *
    2480             :  * Note that the "applied typmod" should be -1 for every domain level
    2481             :  * above the bottommost; therefore, if the passed-in typid is indeed
    2482             :  * a domain, *typmod should be -1.
    2483             :  */
    2484             : Oid
    2485    11161934 : getBaseTypeAndTypmod(Oid typid, int32 *typmod)
    2486             : {
    2487             :     /*
    2488             :      * We loop to find the bottom base type in a stack of domains.
    2489             :      */
    2490             :     for (;;)
    2491     1424580 :     {
    2492             :         HeapTuple   tup;
    2493             :         Form_pg_type typTup;
    2494             : 
    2495    11161934 :         tup = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typid));
    2496    11161934 :         if (!HeapTupleIsValid(tup))
    2497           0 :             elog(ERROR, "cache lookup failed for type %u", typid);
    2498    11161934 :         typTup = (Form_pg_type) GETSTRUCT(tup);
    2499    11161934 :         if (typTup->typtype != TYPTYPE_DOMAIN)
    2500             :         {
    2501             :             /* Not a domain, so done */
    2502     9737354 :             ReleaseSysCache(tup);
    2503     9737354 :             break;
    2504             :         }
    2505             : 
    2506             :         Assert(*typmod == -1);
    2507     1424580 :         typid = typTup->typbasetype;
    2508     1424580 :         *typmod = typTup->typtypmod;
    2509             : 
    2510     1424580 :         ReleaseSysCache(tup);
    2511             :     }
    2512             : 
    2513     9737354 :     return typid;
    2514             : }
    2515             : 
    2516             : /*
    2517             :  * get_typavgwidth
    2518             :  *
    2519             :  *    Given a type OID and a typmod value (pass -1 if typmod is unknown),
    2520             :  *    estimate the average width of values of the type.  This is used by
    2521             :  *    the planner, which doesn't require absolutely correct results;
    2522             :  *    it's OK (and expected) to guess if we don't know for sure.
    2523             :  */
    2524             : int32
    2525      910934 : get_typavgwidth(Oid typid, int32 typmod)
    2526             : {
    2527      910934 :     int         typlen = get_typlen(typid);
    2528             :     int32       maxwidth;
    2529             : 
    2530             :     /*
    2531             :      * Easy if it's a fixed-width type
    2532             :      */
    2533      910934 :     if (typlen > 0)
    2534      602736 :         return typlen;
    2535             : 
    2536             :     /*
    2537             :      * type_maximum_size knows the encoding of typmod for some datatypes;
    2538             :      * don't duplicate that knowledge here.
    2539             :      */
    2540      308198 :     maxwidth = type_maximum_size(typid, typmod);
    2541      308198 :     if (maxwidth > 0)
    2542             :     {
    2543             :         /*
    2544             :          * For BPCHAR, the max width is also the only width.  Otherwise we
    2545             :          * need to guess about the typical data width given the max. A sliding
    2546             :          * scale for percentage of max width seems reasonable.
    2547             :          */
    2548       18798 :         if (typid == BPCHAROID)
    2549       11722 :             return maxwidth;
    2550        7076 :         if (maxwidth <= 32)
    2551        3948 :             return maxwidth;    /* assume full width */
    2552        3128 :         if (maxwidth < 1000)
    2553        3052 :             return 32 + (maxwidth - 32) / 2;    /* assume 50% */
    2554             : 
    2555             :         /*
    2556             :          * Beyond 1000, assume we're looking at something like
    2557             :          * "varchar(10000)" where the limit isn't actually reached often, and
    2558             :          * use a fixed estimate.
    2559             :          */
    2560          76 :         return 32 + (1000 - 32) / 2;
    2561             :     }
    2562             : 
    2563             :     /*
    2564             :      * Oops, we have no idea ... wild guess time.
    2565             :      */
    2566      289400 :     return 32;
    2567             : }
    2568             : 
    2569             : /*
    2570             :  * get_typtype
    2571             :  *
    2572             :  *      Given the type OID, find if it is a basic type, a complex type, etc.
    2573             :  *      It returns the null char if the cache lookup fails...
    2574             :  */
    2575             : char
    2576     2024646 : get_typtype(Oid typid)
    2577             : {
    2578             :     HeapTuple   tp;
    2579             : 
    2580     2024646 :     tp = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typid));
    2581     2024646 :     if (HeapTupleIsValid(tp))
    2582             :     {
    2583     2024588 :         Form_pg_type typtup = (Form_pg_type) GETSTRUCT(tp);
    2584             :         char        result;
    2585             : 
    2586     2024588 :         result = typtup->typtype;
    2587     2024588 :         ReleaseSysCache(tp);
    2588     2024588 :         return result;
    2589             :     }
    2590             :     else
    2591          58 :         return '\0';
    2592             : }
    2593             : 
    2594             : /*
    2595             :  * type_is_rowtype
    2596             :  *
    2597             :  *      Convenience function to determine whether a type OID represents
    2598             :  *      a "rowtype" type --- either RECORD or a named composite type
    2599             :  *      (including a domain over a named composite type).
    2600             :  */
    2601             : bool
    2602      106904 : type_is_rowtype(Oid typid)
    2603             : {
    2604      106904 :     if (typid == RECORDOID)
    2605       57582 :         return true;            /* easy case */
    2606       49322 :     switch (get_typtype(typid))
    2607             :     {
    2608        2554 :         case TYPTYPE_COMPOSITE:
    2609        2554 :             return true;
    2610         210 :         case TYPTYPE_DOMAIN:
    2611         210 :             if (get_typtype(getBaseType(typid)) == TYPTYPE_COMPOSITE)
    2612          52 :                 return true;
    2613         158 :             break;
    2614       46558 :         default:
    2615       46558 :             break;
    2616             :     }
    2617       46716 :     return false;
    2618             : }
    2619             : 
    2620             : /*
    2621             :  * type_is_enum
    2622             :  *    Returns true if the given type is an enum type.
    2623             :  */
    2624             : bool
    2625      145540 : type_is_enum(Oid typid)
    2626             : {
    2627      145540 :     return (get_typtype(typid) == TYPTYPE_ENUM);
    2628             : }
    2629             : 
    2630             : /*
    2631             :  * type_is_range
    2632             :  *    Returns true if the given type is a range type.
    2633             :  */
    2634             : bool
    2635       22516 : type_is_range(Oid typid)
    2636             : {
    2637       22516 :     return (get_typtype(typid) == TYPTYPE_RANGE);
    2638             : }
    2639             : 
    2640             : /*
    2641             :  * type_is_multirange
    2642             :  *    Returns true if the given type is a multirange type.
    2643             :  */
    2644             : bool
    2645      106908 : type_is_multirange(Oid typid)
    2646             : {
    2647      106908 :     return (get_typtype(typid) == TYPTYPE_MULTIRANGE);
    2648             : }
    2649             : 
    2650             : /*
    2651             :  * get_type_category_preferred
    2652             :  *
    2653             :  *      Given the type OID, fetch its category and preferred-type status.
    2654             :  *      Throws error on failure.
    2655             :  */
    2656             : void
    2657      456758 : get_type_category_preferred(Oid typid, char *typcategory, bool *typispreferred)
    2658             : {
    2659             :     HeapTuple   tp;
    2660             :     Form_pg_type typtup;
    2661             : 
    2662      456758 :     tp = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typid));
    2663      456758 :     if (!HeapTupleIsValid(tp))
    2664           0 :         elog(ERROR, "cache lookup failed for type %u", typid);
    2665      456758 :     typtup = (Form_pg_type) GETSTRUCT(tp);
    2666      456758 :     *typcategory = typtup->typcategory;
    2667      456758 :     *typispreferred = typtup->typispreferred;
    2668      456758 :     ReleaseSysCache(tp);
    2669      456758 : }
    2670             : 
    2671             : /*
    2672             :  * get_typ_typrelid
    2673             :  *
    2674             :  *      Given the type OID, get the typrelid (InvalidOid if not a complex
    2675             :  *      type).
    2676             :  */
    2677             : Oid
    2678       69516 : get_typ_typrelid(Oid typid)
    2679             : {
    2680             :     HeapTuple   tp;
    2681             : 
    2682       69516 :     tp = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typid));
    2683       69516 :     if (HeapTupleIsValid(tp))
    2684             :     {
    2685       69516 :         Form_pg_type typtup = (Form_pg_type) GETSTRUCT(tp);
    2686             :         Oid         result;
    2687             : 
    2688       69516 :         result = typtup->typrelid;
    2689       69516 :         ReleaseSysCache(tp);
    2690       69516 :         return result;
    2691             :     }
    2692             :     else
    2693           0 :         return InvalidOid;
    2694             : }
    2695             : 
    2696             : /*
    2697             :  * get_element_type
    2698             :  *
    2699             :  *      Given the type OID, get the typelem (InvalidOid if not an array type).
    2700             :  *
    2701             :  * NB: this only succeeds for "true" arrays having array_subscript_handler
    2702             :  * as typsubscript.  For other types, InvalidOid is returned independently
    2703             :  * of whether they have typelem or typsubscript set.
    2704             :  */
    2705             : Oid
    2706     2220374 : get_element_type(Oid typid)
    2707             : {
    2708             :     HeapTuple   tp;
    2709             : 
    2710     2220374 :     tp = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typid));
    2711     2220374 :     if (HeapTupleIsValid(tp))
    2712             :     {
    2713     2220350 :         Form_pg_type typtup = (Form_pg_type) GETSTRUCT(tp);
    2714             :         Oid         result;
    2715             : 
    2716     2220350 :         if (IsTrueArrayType(typtup))
    2717      114046 :             result = typtup->typelem;
    2718             :         else
    2719     2106304 :             result = InvalidOid;
    2720     2220350 :         ReleaseSysCache(tp);
    2721     2220350 :         return result;
    2722             :     }
    2723             :     else
    2724          24 :         return InvalidOid;
    2725             : }
    2726             : 
    2727             : /*
    2728             :  * get_array_type
    2729             :  *
    2730             :  *      Given the type OID, get the corresponding "true" array type.
    2731             :  *      Returns InvalidOid if no array type can be found.
    2732             :  */
    2733             : Oid
    2734      129012 : get_array_type(Oid typid)
    2735             : {
    2736             :     HeapTuple   tp;
    2737      129012 :     Oid         result = InvalidOid;
    2738             : 
    2739      129012 :     tp = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typid));
    2740      129012 :     if (HeapTupleIsValid(tp))
    2741             :     {
    2742      129012 :         result = ((Form_pg_type) GETSTRUCT(tp))->typarray;
    2743      129012 :         ReleaseSysCache(tp);
    2744             :     }
    2745      129012 :     return result;
    2746             : }
    2747             : 
    2748             : /*
    2749             :  * get_promoted_array_type
    2750             :  *
    2751             :  *      The "promoted" type is what you'd get from an ARRAY(SELECT ...)
    2752             :  *      construct, that is, either the corresponding "true" array type
    2753             :  *      if the input is a scalar type that has such an array type,
    2754             :  *      or the same type if the input is already a "true" array type.
    2755             :  *      Returns InvalidOid if neither rule is satisfied.
    2756             :  */
    2757             : Oid
    2758       15018 : get_promoted_array_type(Oid typid)
    2759             : {
    2760       15018 :     Oid         array_type = get_array_type(typid);
    2761             : 
    2762       15018 :     if (OidIsValid(array_type))
    2763       14994 :         return array_type;
    2764          24 :     if (OidIsValid(get_element_type(typid)))
    2765          24 :         return typid;
    2766           0 :     return InvalidOid;
    2767             : }
    2768             : 
    2769             : /*
    2770             :  * get_base_element_type
    2771             :  *      Given the type OID, get the typelem, looking "through" any domain
    2772             :  *      to its underlying array type.
    2773             :  *
    2774             :  * This is equivalent to get_element_type(getBaseType(typid)), but avoids
    2775             :  * an extra cache lookup.  Note that it fails to provide any information
    2776             :  * about the typmod of the array.
    2777             :  */
    2778             : Oid
    2779      194938 : get_base_element_type(Oid typid)
    2780             : {
    2781             :     /*
    2782             :      * We loop to find the bottom base type in a stack of domains.
    2783             :      */
    2784             :     for (;;)
    2785          44 :     {
    2786             :         HeapTuple   tup;
    2787             :         Form_pg_type typTup;
    2788             : 
    2789      194938 :         tup = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typid));
    2790      194938 :         if (!HeapTupleIsValid(tup))
    2791        1058 :             break;
    2792      193880 :         typTup = (Form_pg_type) GETSTRUCT(tup);
    2793      193880 :         if (typTup->typtype != TYPTYPE_DOMAIN)
    2794             :         {
    2795             :             /* Not a domain, so stop descending */
    2796             :             Oid         result;
    2797             : 
    2798             :             /* This test must match get_element_type */
    2799      193836 :             if (IsTrueArrayType(typTup))
    2800       86044 :                 result = typTup->typelem;
    2801             :             else
    2802      107792 :                 result = InvalidOid;
    2803      193836 :             ReleaseSysCache(tup);
    2804      193836 :             return result;
    2805             :         }
    2806             : 
    2807          44 :         typid = typTup->typbasetype;
    2808          44 :         ReleaseSysCache(tup);
    2809             :     }
    2810             : 
    2811             :     /* Like get_element_type, silently return InvalidOid for bogus input */
    2812        1058 :     return InvalidOid;
    2813             : }
    2814             : 
    2815             : /*
    2816             :  * getTypeInputInfo
    2817             :  *
    2818             :  *      Get info needed for converting values of a type to internal form
    2819             :  */
    2820             : void
    2821      775582 : getTypeInputInfo(Oid type, Oid *typInput, Oid *typIOParam)
    2822             : {
    2823             :     HeapTuple   typeTuple;
    2824             :     Form_pg_type pt;
    2825             : 
    2826      775582 :     typeTuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(type));
    2827      775582 :     if (!HeapTupleIsValid(typeTuple))
    2828           0 :         elog(ERROR, "cache lookup failed for type %u", type);
    2829      775582 :     pt = (Form_pg_type) GETSTRUCT(typeTuple);
    2830             : 
    2831      775582 :     if (!pt->typisdefined)
    2832           0 :         ereport(ERROR,
    2833             :                 (errcode(ERRCODE_UNDEFINED_OBJECT),
    2834             :                  errmsg("type %s is only a shell",
    2835             :                         format_type_be(type))));
    2836      775582 :     if (!OidIsValid(pt->typinput))
    2837           0 :         ereport(ERROR,
    2838             :                 (errcode(ERRCODE_UNDEFINED_FUNCTION),
    2839             :                  errmsg("no input function available for type %s",
    2840             :                         format_type_be(type))));
    2841             : 
    2842      775582 :     *typInput = pt->typinput;
    2843      775582 :     *typIOParam = getTypeIOParam(typeTuple);
    2844             : 
    2845      775582 :     ReleaseSysCache(typeTuple);
    2846      775582 : }
    2847             : 
    2848             : /*
    2849             :  * getTypeOutputInfo
    2850             :  *
    2851             :  *      Get info needed for printing values of a type
    2852             :  */
    2853             : void
    2854     1038014 : getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
    2855             : {
    2856             :     HeapTuple   typeTuple;
    2857             :     Form_pg_type pt;
    2858             : 
    2859     1038014 :     typeTuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(type));
    2860     1038014 :     if (!HeapTupleIsValid(typeTuple))
    2861           0 :         elog(ERROR, "cache lookup failed for type %u", type);
    2862     1038014 :     pt = (Form_pg_type) GETSTRUCT(typeTuple);
    2863             : 
    2864     1038014 :     if (!pt->typisdefined)
    2865           0 :         ereport(ERROR,
    2866             :                 (errcode(ERRCODE_UNDEFINED_OBJECT),
    2867             :                  errmsg("type %s is only a shell",
    2868             :                         format_type_be(type))));
    2869     1038014 :     if (!OidIsValid(pt->typoutput))
    2870           0 :         ereport(ERROR,
    2871             :                 (errcode(ERRCODE_UNDEFINED_FUNCTION),
    2872             :                  errmsg("no output function available for type %s",
    2873             :                         format_type_be(type))));
    2874             : 
    2875     1038014 :     *typOutput = pt->typoutput;
    2876     1038014 :     *typIsVarlena = (!pt->typbyval) && (pt->typlen == -1);
    2877             : 
    2878     1038014 :     ReleaseSysCache(typeTuple);
    2879     1038014 : }
    2880             : 
    2881             : /*
    2882             :  * getTypeBinaryInputInfo
    2883             :  *
    2884             :  *      Get info needed for binary input of values of a type
    2885             :  */
    2886             : void
    2887      114024 : getTypeBinaryInputInfo(Oid type, Oid *typReceive, Oid *typIOParam)
    2888             : {
    2889             :     HeapTuple   typeTuple;
    2890             :     Form_pg_type pt;
    2891             : 
    2892      114024 :     typeTuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(type));
    2893      114024 :     if (!HeapTupleIsValid(typeTuple))
    2894           0 :         elog(ERROR, "cache lookup failed for type %u", type);
    2895      114024 :     pt = (Form_pg_type) GETSTRUCT(typeTuple);
    2896             : 
    2897      114024 :     if (!pt->typisdefined)
    2898           0 :         ereport(ERROR,
    2899             :                 (errcode(ERRCODE_UNDEFINED_OBJECT),
    2900             :                  errmsg("type %s is only a shell",
    2901             :                         format_type_be(type))));
    2902      114024 :     if (!OidIsValid(pt->typreceive))
    2903           0 :         ereport(ERROR,
    2904             :                 (errcode(ERRCODE_UNDEFINED_FUNCTION),
    2905             :                  errmsg("no binary input function available for type %s",
    2906             :                         format_type_be(type))));
    2907             : 
    2908      114024 :     *typReceive = pt->typreceive;
    2909      114024 :     *typIOParam = getTypeIOParam(typeTuple);
    2910             : 
    2911      114024 :     ReleaseSysCache(typeTuple);
    2912      114024 : }
    2913             : 
    2914             : /*
    2915             :  * getTypeBinaryOutputInfo
    2916             :  *
    2917             :  *      Get info needed for binary output of values of a type
    2918             :  */
    2919             : void
    2920        1366 : getTypeBinaryOutputInfo(Oid type, Oid *typSend, bool *typIsVarlena)
    2921             : {
    2922             :     HeapTuple   typeTuple;
    2923             :     Form_pg_type pt;
    2924             : 
    2925        1366 :     typeTuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(type));
    2926        1366 :     if (!HeapTupleIsValid(typeTuple))
    2927           0 :         elog(ERROR, "cache lookup failed for type %u", type);
    2928        1366 :     pt = (Form_pg_type) GETSTRUCT(typeTuple);
    2929             : 
    2930        1366 :     if (!pt->typisdefined)
    2931           0 :         ereport(ERROR,
    2932             :                 (errcode(ERRCODE_UNDEFINED_OBJECT),
    2933             :                  errmsg("type %s is only a shell",
    2934             :                         format_type_be(type))));
    2935        1366 :     if (!OidIsValid(pt->typsend))
    2936           0 :         ereport(ERROR,
    2937             :                 (errcode(ERRCODE_UNDEFINED_FUNCTION),
    2938             :                  errmsg("no binary output function available for type %s",
    2939             :                         format_type_be(type))));
    2940             : 
    2941        1366 :     *typSend = pt->typsend;
    2942        1366 :     *typIsVarlena = (!pt->typbyval) && (pt->typlen == -1);
    2943             : 
    2944        1366 :     ReleaseSysCache(typeTuple);
    2945        1366 : }
    2946             : 
    2947             : /*
    2948             :  * get_typmodin
    2949             :  *
    2950             :  *      Given the type OID, return the type's typmodin procedure, if any.
    2951             :  */
    2952             : Oid
    2953           0 : get_typmodin(Oid typid)
    2954             : {
    2955             :     HeapTuple   tp;
    2956             : 
    2957           0 :     tp = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typid));
    2958           0 :     if (HeapTupleIsValid(tp))
    2959             :     {
    2960           0 :         Form_pg_type typtup = (Form_pg_type) GETSTRUCT(tp);
    2961             :         Oid         result;
    2962             : 
    2963           0 :         result = typtup->typmodin;
    2964           0 :         ReleaseSysCache(tp);
    2965           0 :         return result;
    2966             :     }
    2967             :     else
    2968           0 :         return InvalidOid;
    2969             : }
    2970             : 
    2971             : #ifdef NOT_USED
    2972             : /*
    2973             :  * get_typmodout
    2974             :  *
    2975             :  *      Given the type OID, return the type's typmodout procedure, if any.
    2976             :  */
    2977             : Oid
    2978             : get_typmodout(Oid typid)
    2979             : {
    2980             :     HeapTuple   tp;
    2981             : 
    2982             :     tp = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typid));
    2983             :     if (HeapTupleIsValid(tp))
    2984             :     {
    2985             :         Form_pg_type typtup = (Form_pg_type) GETSTRUCT(tp);
    2986             :         Oid         result;
    2987             : 
    2988             :         result = typtup->typmodout;
    2989             :         ReleaseSysCache(tp);
    2990             :         return result;
    2991             :     }
    2992             :     else
    2993             :         return InvalidOid;
    2994             : }
    2995             : #endif                          /* NOT_USED */
    2996             : 
    2997             : /*
    2998             :  * get_typcollation
    2999             :  *
    3000             :  *      Given the type OID, return the type's typcollation attribute.
    3001             :  */
    3002             : Oid
    3003     5175814 : get_typcollation(Oid typid)
    3004             : {
    3005             :     HeapTuple   tp;
    3006             : 
    3007     5175814 :     tp = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typid));
    3008     5175814 :     if (HeapTupleIsValid(tp))
    3009             :     {
    3010     5175802 :         Form_pg_type typtup = (Form_pg_type) GETSTRUCT(tp);
    3011             :         Oid         result;
    3012             : 
    3013     5175802 :         result = typtup->typcollation;
    3014     5175802 :         ReleaseSysCache(tp);
    3015     5175802 :         return result;
    3016             :     }
    3017             :     else
    3018          12 :         return InvalidOid;
    3019             : }
    3020             : 
    3021             : 
    3022             : /*
    3023             :  * type_is_collatable
    3024             :  *
    3025             :  *      Return whether the type cares about collations
    3026             :  */
    3027             : bool
    3028     1460490 : type_is_collatable(Oid typid)
    3029             : {
    3030     1460490 :     return OidIsValid(get_typcollation(typid));
    3031             : }
    3032             : 
    3033             : 
    3034             : /*
    3035             :  * get_typsubscript
    3036             :  *
    3037             :  *      Given the type OID, return the type's subscripting handler's OID,
    3038             :  *      if it has one.
    3039             :  *
    3040             :  * If typelemp isn't NULL, we also store the type's typelem value there.
    3041             :  * This saves some callers an extra catalog lookup.
    3042             :  */
    3043             : RegProcedure
    3044       22076 : get_typsubscript(Oid typid, Oid *typelemp)
    3045             : {
    3046             :     HeapTuple   tp;
    3047             : 
    3048       22076 :     tp = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typid));
    3049       22076 :     if (HeapTupleIsValid(tp))
    3050             :     {
    3051       22076 :         Form_pg_type typform = (Form_pg_type) GETSTRUCT(tp);
    3052       22076 :         RegProcedure handler = typform->typsubscript;
    3053             : 
    3054       22076 :         if (typelemp)
    3055       12256 :             *typelemp = typform->typelem;
    3056       22076 :         ReleaseSysCache(tp);
    3057       22076 :         return handler;
    3058             :     }
    3059             :     else
    3060             :     {
    3061           0 :         if (typelemp)
    3062           0 :             *typelemp = InvalidOid;
    3063           0 :         return InvalidOid;
    3064             :     }
    3065             : }
    3066             : 
    3067             : /*
    3068             :  * getSubscriptingRoutines
    3069             :  *
    3070             :  *      Given the type OID, fetch the type's subscripting methods struct.
    3071             :  *      Return NULL if type is not subscriptable.
    3072             :  *
    3073             :  * If typelemp isn't NULL, we also store the type's typelem value there.
    3074             :  * This saves some callers an extra catalog lookup.
    3075             :  */
    3076             : const struct SubscriptRoutines *
    3077       22036 : getSubscriptingRoutines(Oid typid, Oid *typelemp)
    3078             : {
    3079       22036 :     RegProcedure typsubscript = get_typsubscript(typid, typelemp);
    3080             : 
    3081       22036 :     if (!OidIsValid(typsubscript))
    3082           8 :         return NULL;
    3083             : 
    3084       22028 :     return (const struct SubscriptRoutines *)
    3085       22028 :         DatumGetPointer(OidFunctionCall0(typsubscript));
    3086             : }
    3087             : 
    3088             : 
    3089             : /*              ---------- STATISTICS CACHE ----------                   */
    3090             : 
    3091             : /*
    3092             :  * get_attavgwidth
    3093             :  *
    3094             :  *    Given the table and attribute number of a column, get the average
    3095             :  *    width of entries in the column.  Return zero if no data available.
    3096             :  *
    3097             :  * Currently this is only consulted for individual tables, not for inheritance
    3098             :  * trees, so we don't need an "inh" parameter.
    3099             :  *
    3100             :  * Calling a hook at this point looks somewhat strange, but is required
    3101             :  * because the optimizer calls this function without any other way for
    3102             :  * plug-ins to control the result.
    3103             :  */
    3104             : int32
    3105      626288 : get_attavgwidth(Oid relid, AttrNumber attnum)
    3106             : {
    3107             :     HeapTuple   tp;
    3108             :     int32       stawidth;
    3109             : 
    3110      626288 :     if (get_attavgwidth_hook)
    3111             :     {
    3112           0 :         stawidth = (*get_attavgwidth_hook) (relid, attnum);
    3113           0 :         if (stawidth > 0)
    3114           0 :             return stawidth;
    3115             :     }
    3116      626288 :     tp = SearchSysCache3(STATRELATTINH,
    3117             :                          ObjectIdGetDatum(relid),
    3118             :                          Int16GetDatum(attnum),
    3119             :                          BoolGetDatum(false));
    3120      626288 :     if (HeapTupleIsValid(tp))
    3121             :     {
    3122      308240 :         stawidth = ((Form_pg_statistic) GETSTRUCT(tp))->stawidth;
    3123      308240 :         ReleaseSysCache(tp);
    3124      308240 :         if (stawidth > 0)
    3125      302902 :             return stawidth;
    3126             :     }
    3127      323386 :     return 0;
    3128             : }
    3129             : 
    3130             : /*
    3131             :  * get_attstatsslot
    3132             :  *
    3133             :  *      Extract the contents of a "slot" of a pg_statistic tuple.
    3134             :  *      Returns true if requested slot type was found, else false.
    3135             :  *
    3136             :  * Unlike other routines in this file, this takes a pointer to an
    3137             :  * already-looked-up tuple in the pg_statistic cache.  We do this since
    3138             :  * most callers will want to extract more than one value from the cache
    3139             :  * entry, and we don't want to repeat the cache lookup unnecessarily.
    3140             :  * Also, this API allows this routine to be used with statistics tuples
    3141             :  * that have been provided by a stats hook and didn't really come from
    3142             :  * pg_statistic.
    3143             :  *
    3144             :  * sslot: pointer to output area (typically, a local variable in the caller).
    3145             :  * statstuple: pg_statistic tuple to be examined.
    3146             :  * reqkind: STAKIND code for desired statistics slot kind.
    3147             :  * reqop: STAOP value wanted, or InvalidOid if don't care.
    3148             :  * flags: bitmask of ATTSTATSSLOT_VALUES and/or ATTSTATSSLOT_NUMBERS.
    3149             :  *
    3150             :  * If a matching slot is found, true is returned, and *sslot is filled thus:
    3151             :  * staop: receives the actual STAOP value.
    3152             :  * stacoll: receives the actual STACOLL value.
    3153             :  * valuetype: receives actual datatype of the elements of stavalues.
    3154             :  * values: receives pointer to an array of the slot's stavalues.
    3155             :  * nvalues: receives number of stavalues.
    3156             :  * numbers: receives pointer to an array of the slot's stanumbers (as float4).
    3157             :  * nnumbers: receives number of stanumbers.
    3158             :  *
    3159             :  * valuetype/values/nvalues are InvalidOid/NULL/0 if ATTSTATSSLOT_VALUES
    3160             :  * wasn't specified.  Likewise, numbers/nnumbers are NULL/0 if
    3161             :  * ATTSTATSSLOT_NUMBERS wasn't specified.
    3162             :  *
    3163             :  * If no matching slot is found, false is returned, and *sslot is zeroed.
    3164             :  *
    3165             :  * Note that the current API doesn't allow for searching for a slot with
    3166             :  * a particular collation.  If we ever actually support recording more than
    3167             :  * one collation, we'll have to extend the API, but for now simple is good.
    3168             :  *
    3169             :  * The data referred to by the fields of sslot is locally palloc'd and
    3170             :  * is independent of the original pg_statistic tuple.  When the caller
    3171             :  * is done with it, call free_attstatsslot to release the palloc'd data.
    3172             :  *
    3173             :  * If it's desirable to call free_attstatsslot when get_attstatsslot might
    3174             :  * not have been called, memset'ing sslot to zeroes will allow that.
    3175             :  */
    3176             : bool
    3177     1013932 : get_attstatsslot(AttStatsSlot *sslot, HeapTuple statstuple,
    3178             :                  int reqkind, Oid reqop, int flags)
    3179             : {
    3180     1013932 :     Form_pg_statistic stats = (Form_pg_statistic) GETSTRUCT(statstuple);
    3181             :     int         i;
    3182             :     Datum       val;
    3183             :     bool        isnull;
    3184             :     ArrayType  *statarray;
    3185             :     Oid         arrayelemtype;
    3186             :     int         narrayelem;
    3187             :     HeapTuple   typeTuple;
    3188             :     Form_pg_type typeForm;
    3189             : 
    3190             :     /* initialize *sslot properly */
    3191     1013932 :     memset(sslot, 0, sizeof(AttStatsSlot));
    3192             : 
    3193     2806196 :     for (i = 0; i < STATISTIC_NUM_SLOTS; i++)
    3194             :     {
    3195     2519870 :         if ((&stats->stakind1)[i] == reqkind &&
    3196      299096 :             (reqop == InvalidOid || (&stats->staop1)[i] == reqop))
    3197             :             break;
    3198             :     }
    3199     1013932 :     if (i >= STATISTIC_NUM_SLOTS)
    3200      286326 :         return false;           /* not there */
    3201             : 
    3202      727606 :     sslot->staop = (&stats->staop1)[i];
    3203      727606 :     sslot->stacoll = (&stats->stacoll1)[i];
    3204             : 
    3205      727606 :     if (flags & ATTSTATSSLOT_VALUES)
    3206             :     {
    3207      404618 :         val = SysCacheGetAttr(STATRELATTINH, statstuple,
    3208      404618 :                               Anum_pg_statistic_stavalues1 + i,
    3209             :                               &isnull);
    3210      404618 :         if (isnull)
    3211           0 :             elog(ERROR, "stavalues is null");
    3212             : 
    3213             :         /*
    3214             :          * Detoast the array if needed, and in any case make a copy that's
    3215             :          * under control of this AttStatsSlot.
    3216             :          */
    3217      404618 :         statarray = DatumGetArrayTypePCopy(val);
    3218             : 
    3219             :         /*
    3220             :          * Extract the actual array element type, and pass it back in case the
    3221             :          * caller needs it.
    3222             :          */
    3223      404618 :         sslot->valuetype = arrayelemtype = ARR_ELEMTYPE(statarray);
    3224             : 
    3225             :         /* Need info about element type */
    3226      404618 :         typeTuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(arrayelemtype));
    3227      404618 :         if (!HeapTupleIsValid(typeTuple))
    3228           0 :             elog(ERROR, "cache lookup failed for type %u", arrayelemtype);
    3229      404618 :         typeForm = (Form_pg_type) GETSTRUCT(typeTuple);
    3230             : 
    3231             :         /* Deconstruct array into Datum elements; NULLs not expected */
    3232      404618 :         deconstruct_array(statarray,
    3233             :                           arrayelemtype,
    3234      404618 :                           typeForm->typlen,
    3235      404618 :                           typeForm->typbyval,
    3236      404618 :                           typeForm->typalign,
    3237             :                           &sslot->values, NULL, &sslot->nvalues);
    3238             : 
    3239             :         /*
    3240             :          * If the element type is pass-by-reference, we now have a bunch of
    3241             :          * Datums that are pointers into the statarray, so we need to keep
    3242             :          * that until free_attstatsslot.  Otherwise, all the useful info is in
    3243             :          * sslot->values[], so we can free the array object immediately.
    3244             :          */
    3245      404618 :         if (!typeForm->typbyval)
    3246       25944 :             sslot->values_arr = statarray;
    3247             :         else
    3248      378674 :             pfree(statarray);
    3249             : 
    3250      404618 :         ReleaseSysCache(typeTuple);
    3251             :     }
    3252             : 
    3253      727606 :     if (flags & ATTSTATSSLOT_NUMBERS)
    3254             :     {
    3255      553160 :         val = SysCacheGetAttr(STATRELATTINH, statstuple,
    3256      553160 :                               Anum_pg_statistic_stanumbers1 + i,
    3257             :                               &isnull);
    3258      553160 :         if (isnull)
    3259           0 :             elog(ERROR, "stanumbers is null");
    3260             : 
    3261             :         /*
    3262             :          * Detoast the array if needed, and in any case make a copy that's
    3263             :          * under control of this AttStatsSlot.
    3264             :          */
    3265      553160 :         statarray = DatumGetArrayTypePCopy(val);
    3266             : 
    3267             :         /*
    3268             :          * We expect the array to be a 1-D float4 array; verify that. We don't
    3269             :          * need to use deconstruct_array() since the array data is just going
    3270             :          * to look like a C array of float4 values.
    3271             :          */
    3272      553160 :         narrayelem = ARR_DIMS(statarray)[0];
    3273      553160 :         if (ARR_NDIM(statarray) != 1 || narrayelem <= 0 ||
    3274      553160 :             ARR_HASNULL(statarray) ||
    3275      553160 :             ARR_ELEMTYPE(statarray) != FLOAT4OID)
    3276           0 :             elog(ERROR, "stanumbers is not a 1-D float4 array");
    3277             : 
    3278             :         /* Give caller a pointer directly into the statarray */
    3279      553160 :         sslot->numbers = (float4 *) ARR_DATA_PTR(statarray);
    3280      553160 :         sslot->nnumbers = narrayelem;
    3281             : 
    3282             :         /* We'll free the statarray in free_attstatsslot */
    3283      553160 :         sslot->numbers_arr = statarray;
    3284             :     }
    3285             : 
    3286      727606 :     return true;
    3287             : }
    3288             : 
    3289             : /*
    3290             :  * free_attstatsslot
    3291             :  *      Free data allocated by get_attstatsslot
    3292             :  */
    3293             : void
    3294      877124 : free_attstatsslot(AttStatsSlot *sslot)
    3295             : {
    3296             :     /* The values[] array was separately palloc'd by deconstruct_array */
    3297      877124 :     if (sslot->values)
    3298      404618 :         pfree(sslot->values);
    3299             :     /* The numbers[] array points into numbers_arr, do not pfree it */
    3300             :     /* Free the detoasted array objects, if any */
    3301      877124 :     if (sslot->values_arr)
    3302       25944 :         pfree(sslot->values_arr);
    3303      877124 :     if (sslot->numbers_arr)
    3304      553160 :         pfree(sslot->numbers_arr);
    3305      877124 : }
    3306             : 
    3307             : /*              ---------- PG_NAMESPACE CACHE ----------                 */
    3308             : 
    3309             : /*
    3310             :  * get_namespace_name
    3311             :  *      Returns the name of a given namespace
    3312             :  *
    3313             :  * Returns a palloc'd copy of the string, or NULL if no such namespace.
    3314             :  */
    3315             : char *
    3316      714384 : get_namespace_name(Oid nspid)
    3317             : {
    3318             :     HeapTuple   tp;
    3319             : 
    3320      714384 :     tp = SearchSysCache1(NAMESPACEOID, ObjectIdGetDatum(nspid));
    3321      714384 :     if (HeapTupleIsValid(tp))
    3322             :     {
    3323      714372 :         Form_pg_namespace nsptup = (Form_pg_namespace) GETSTRUCT(tp);
    3324             :         char       *result;
    3325             : 
    3326      714372 :         result = pstrdup(NameStr(nsptup->nspname));
    3327      714372 :         ReleaseSysCache(tp);
    3328      714372 :         return result;
    3329             :     }
    3330             :     else
    3331          12 :         return NULL;
    3332             : }
    3333             : 
    3334             : /*
    3335             :  * get_namespace_name_or_temp
    3336             :  *      As above, but if it is this backend's temporary namespace, return
    3337             :  *      "pg_temp" instead.
    3338             :  */
    3339             : char *
    3340       16596 : get_namespace_name_or_temp(Oid nspid)
    3341             : {
    3342       16596 :     if (isTempNamespace(nspid))
    3343          72 :         return pstrdup("pg_temp");
    3344             :     else
    3345       16524 :         return get_namespace_name(nspid);
    3346             : }
    3347             : 
    3348             : /*              ---------- PG_RANGE CACHES ----------                */
    3349             : 
    3350             : /*
    3351             :  * get_range_subtype
    3352             :  *      Returns the subtype of a given range type
    3353             :  *
    3354             :  * Returns InvalidOid if the type is not a range type.
    3355             :  */
    3356             : Oid
    3357       12242 : get_range_subtype(Oid rangeOid)
    3358             : {
    3359             :     HeapTuple   tp;
    3360             : 
    3361       12242 :     tp = SearchSysCache1(RANGETYPE, ObjectIdGetDatum(rangeOid));
    3362       12242 :     if (HeapTupleIsValid(tp))
    3363             :     {
    3364        5856 :         Form_pg_range rngtup = (Form_pg_range) GETSTRUCT(tp);
    3365             :         Oid         result;
    3366             : 
    3367        5856 :         result = rngtup->rngsubtype;
    3368        5856 :         ReleaseSysCache(tp);
    3369        5856 :         return result;
    3370             :     }
    3371             :     else
    3372        6386 :         return InvalidOid;
    3373             : }
    3374             : 
    3375             : /*
    3376             :  * get_range_collation
    3377             :  *      Returns the collation of a given range type
    3378             :  *
    3379             :  * Returns InvalidOid if the type is not a range type,
    3380             :  * or if its subtype is not collatable.
    3381             :  */
    3382             : Oid
    3383         300 : get_range_collation(Oid rangeOid)
    3384             : {
    3385             :     HeapTuple   tp;
    3386             : 
    3387         300 :     tp = SearchSysCache1(RANGETYPE, ObjectIdGetDatum(rangeOid));
    3388         300 :     if (HeapTupleIsValid(tp))
    3389             :     {
    3390         300 :         Form_pg_range rngtup = (Form_pg_range) GETSTRUCT(tp);
    3391             :         Oid         result;
    3392             : 
    3393         300 :         result = rngtup->rngcollation;
    3394         300 :         ReleaseSysCache(tp);
    3395         300 :         return result;
    3396             :     }
    3397             :     else
    3398           0 :         return InvalidOid;
    3399             : }
    3400             : 
    3401             : /*
    3402             :  * get_range_multirange
    3403             :  *      Returns the multirange type of a given range type
    3404             :  *
    3405             :  * Returns InvalidOid if the type is not a range type.
    3406             :  */
    3407             : Oid
    3408         116 : get_range_multirange(Oid rangeOid)
    3409             : {
    3410             :     HeapTuple   tp;
    3411             : 
    3412         116 :     tp = SearchSysCache1(RANGETYPE, ObjectIdGetDatum(rangeOid));
    3413         116 :     if (HeapTupleIsValid(tp))
    3414             :     {
    3415         116 :         Form_pg_range rngtup = (Form_pg_range) GETSTRUCT(tp);
    3416             :         Oid         result;
    3417             : 
    3418         116 :         result = rngtup->rngmultitypid;
    3419         116 :         ReleaseSysCache(tp);
    3420         116 :         return result;
    3421             :     }
    3422             :     else
    3423           0 :         return InvalidOid;
    3424             : }
    3425             : 
    3426             : /*
    3427             :  * get_multirange_range
    3428             :  *      Returns the range type of a given multirange
    3429             :  *
    3430             :  * Returns InvalidOid if the type is not a multirange.
    3431             :  */
    3432             : Oid
    3433       15336 : get_multirange_range(Oid multirangeOid)
    3434             : {
    3435             :     HeapTuple   tp;
    3436             : 
    3437       15336 :     tp = SearchSysCache1(RANGEMULTIRANGE, ObjectIdGetDatum(multirangeOid));
    3438       15336 :     if (HeapTupleIsValid(tp))
    3439             :     {
    3440        2968 :         Form_pg_range rngtup = (Form_pg_range) GETSTRUCT(tp);
    3441             :         Oid         result;
    3442             : 
    3443        2968 :         result = rngtup->rngtypid;
    3444        2968 :         ReleaseSysCache(tp);
    3445        2968 :         return result;
    3446             :     }
    3447             :     else
    3448       12368 :         return InvalidOid;
    3449             : }
    3450             : 
    3451             : /*              ---------- PG_INDEX CACHE ----------                 */
    3452             : 
    3453             : /*
    3454             :  * get_index_column_opclass
    3455             :  *
    3456             :  *      Given the index OID and column number,
    3457             :  *      return opclass of the index column
    3458             :  *          or InvalidOid if the index was not found
    3459             :  *              or column is non-key one.
    3460             :  */
    3461             : Oid
    3462          24 : get_index_column_opclass(Oid index_oid, int attno)
    3463             : {
    3464             :     HeapTuple   tuple;
    3465             :     Form_pg_index rd_index;
    3466             :     Datum       datum;
    3467             :     bool        isnull;
    3468             :     oidvector  *indclass;
    3469             :     Oid         opclass;
    3470             : 
    3471             :     /* First we need to know the column's opclass. */
    3472             : 
    3473          24 :     tuple = SearchSysCache1(INDEXRELID, ObjectIdGetDatum(index_oid));
    3474          24 :     if (!HeapTupleIsValid(tuple))
    3475           0 :         return InvalidOid;
    3476             : 
    3477          24 :     rd_index = (Form_pg_index) GETSTRUCT(tuple);
    3478             : 
    3479             :     /* caller is supposed to guarantee this */
    3480             :     Assert(attno > 0 && attno <= rd_index->indnatts);
    3481             : 
    3482             :     /* Non-key attributes don't have an opclass */
    3483          24 :     if (attno > rd_index->indnkeyatts)
    3484             :     {
    3485           0 :         ReleaseSysCache(tuple);
    3486           0 :         return InvalidOid;
    3487             :     }
    3488             : 
    3489          24 :     datum = SysCacheGetAttr(INDEXRELID, tuple,
    3490             :                             Anum_pg_index_indclass, &isnull);
    3491             :     Assert(!isnull);
    3492             : 
    3493          24 :     indclass = ((oidvector *) DatumGetPointer(datum));
    3494             : 
    3495             :     Assert(attno <= indclass->dim1);
    3496          24 :     opclass = indclass->values[attno - 1];
    3497             : 
    3498          24 :     ReleaseSysCache(tuple);
    3499             : 
    3500          24 :     return opclass;
    3501             : }
    3502             : 
    3503             : /*
    3504             :  * get_index_isreplident
    3505             :  *
    3506             :  *      Given the index OID, return pg_index.indisreplident.
    3507             :  */
    3508             : bool
    3509         284 : get_index_isreplident(Oid index_oid)
    3510             : {
    3511             :     HeapTuple   tuple;
    3512             :     Form_pg_index rd_index;
    3513             :     bool        result;
    3514             : 
    3515         284 :     tuple = SearchSysCache1(INDEXRELID, ObjectIdGetDatum(index_oid));
    3516         284 :     if (!HeapTupleIsValid(tuple))
    3517           0 :         return false;
    3518             : 
    3519         284 :     rd_index = (Form_pg_index) GETSTRUCT(tuple);
    3520         284 :     result = rd_index->indisreplident;
    3521         284 :     ReleaseSysCache(tuple);
    3522             : 
    3523         284 :     return result;
    3524             : }
    3525             : 
    3526             : /*
    3527             :  * get_index_isvalid
    3528             :  *
    3529             :  *      Given the index OID, return pg_index.indisvalid.
    3530             :  */
    3531             : bool
    3532        4250 : get_index_isvalid(Oid index_oid)
    3533             : {
    3534             :     bool        isvalid;
    3535             :     HeapTuple   tuple;
    3536             :     Form_pg_index rd_index;
    3537             : 
    3538        4250 :     tuple = SearchSysCache1(INDEXRELID, ObjectIdGetDatum(index_oid));
    3539        4250 :     if (!HeapTupleIsValid(tuple))
    3540           0 :         elog(ERROR, "cache lookup failed for index %u", index_oid);
    3541             : 
    3542        4250 :     rd_index = (Form_pg_index) GETSTRUCT(tuple);
    3543        4250 :     isvalid = rd_index->indisvalid;
    3544        4250 :     ReleaseSysCache(tuple);
    3545             : 
    3546        4250 :     return isvalid;
    3547             : }
    3548             : 
    3549             : /*
    3550             :  * get_index_isclustered
    3551             :  *
    3552             :  *      Given the index OID, return pg_index.indisclustered.
    3553             :  */
    3554             : bool
    3555         424 : get_index_isclustered(Oid index_oid)
    3556             : {
    3557             :     bool        isclustered;
    3558             :     HeapTuple   tuple;
    3559             :     Form_pg_index rd_index;
    3560             : 
    3561         424 :     tuple = SearchSysCache1(INDEXRELID, ObjectIdGetDatum(index_oid));
    3562         424 :     if (!HeapTupleIsValid(tuple))
    3563           0 :         elog(ERROR, "cache lookup failed for index %u", index_oid);
    3564             : 
    3565         424 :     rd_index = (Form_pg_index) GETSTRUCT(tuple);
    3566         424 :     isclustered = rd_index->indisclustered;
    3567         424 :     ReleaseSysCache(tuple);
    3568             : 
    3569         424 :     return isclustered;
    3570             : }

Generated by: LCOV version 1.14