LCOV - differential code coverage report
Current view: top level - src/backend/utils/cache - lsyscache.c (source / functions) Coverage Total Hit UBC CBC
Current: 603a3335f2b60b2c798da5c627b3bb288b92a7bd vs e395fbd32a07557de4ac98088928c1749d4845d8 Lines: 88.5 % 1274 1128 146 1128
Current Date: 2026-07-25 17:13:00 -0400 Functions: 98.4 % 129 127 2 127
Baseline: lcov-20260726-baseline Branches: 56.3 % 662 373 289 373
Baseline Date: 2026-07-25 19:16:42 +0200 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(30,360] days: 86.7 % 98 85 13 85
(360..) days: 88.7 % 1176 1043 133 1043
Function coverage date bins:
(30,360] days: 100.0 % 8 8 8
(360..) days: 98.3 % 121 119 2 119
Branch coverage date bins:
(30,360] days: 58.6 % 70 41 29 41
(360..) days: 56.1 % 592 332 260 332

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

Generated by: LCOV version 2.0-1