LCOV - code coverage report
Current view: top level - src/backend/parser - parse_type.c (source / functions) Hit Total Coverage
Test: PostgreSQL 15devel Lines: 195 244 79.9 %
Date: 2021-12-04 23:09:10 Functions: 23 25 92.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * parse_type.c
       4             :  *      handle type operations for parser
       5             :  *
       6             :  * Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
       7             :  * Portions Copyright (c) 1994, Regents of the University of California
       8             :  *
       9             :  *
      10             :  * IDENTIFICATION
      11             :  *    src/backend/parser/parse_type.c
      12             :  *
      13             :  *-------------------------------------------------------------------------
      14             :  */
      15             : #include "postgres.h"
      16             : 
      17             : #include "access/htup_details.h"
      18             : #include "catalog/namespace.h"
      19             : #include "catalog/pg_type.h"
      20             : #include "lib/stringinfo.h"
      21             : #include "nodes/makefuncs.h"
      22             : #include "parser/parse_type.h"
      23             : #include "parser/parser.h"
      24             : #include "utils/array.h"
      25             : #include "utils/builtins.h"
      26             : #include "utils/lsyscache.h"
      27             : #include "utils/syscache.h"
      28             : 
      29             : static int32 typenameTypeMod(ParseState *pstate, const TypeName *typeName,
      30             :                              Type typ);
      31             : 
      32             : 
      33             : /*
      34             :  * LookupTypeName
      35             :  *      Wrapper for typical case.
      36             :  */
      37             : Type
      38     1663360 : LookupTypeName(ParseState *pstate, const TypeName *typeName,
      39             :                int32 *typmod_p, bool missing_ok)
      40             : {
      41     1663360 :     return LookupTypeNameExtended(pstate,
      42             :                                   typeName, typmod_p, true, missing_ok);
      43             : }
      44             : 
      45             : /*
      46             :  * LookupTypeNameExtended
      47             :  *      Given a TypeName object, lookup the pg_type syscache entry of the type.
      48             :  *      Returns NULL if no such type can be found.  If the type is found,
      49             :  *      the typmod value represented in the TypeName struct is computed and
      50             :  *      stored into *typmod_p.
      51             :  *
      52             :  * NB: on success, the caller must ReleaseSysCache the type tuple when done
      53             :  * with it.
      54             :  *
      55             :  * NB: direct callers of this function MUST check typisdefined before assuming
      56             :  * that the type is fully valid.  Most code should go through typenameType
      57             :  * or typenameTypeId instead.
      58             :  *
      59             :  * typmod_p can be passed as NULL if the caller does not care to know the
      60             :  * typmod value, but the typmod decoration (if any) will be validated anyway,
      61             :  * except in the case where the type is not found.  Note that if the type is
      62             :  * found but is a shell, and there is typmod decoration, an error will be
      63             :  * thrown --- this is intentional.
      64             :  *
      65             :  * If temp_ok is false, ignore types in the temporary namespace.  Pass false
      66             :  * when the caller will decide, using goodness of fit criteria, whether the
      67             :  * typeName is actually a type or something else.  If typeName always denotes
      68             :  * a type (or denotes nothing), pass true.
      69             :  *
      70             :  * pstate is only used for error location info, and may be NULL.
      71             :  */
      72             : Type
      73     1762448 : LookupTypeNameExtended(ParseState *pstate,
      74             :                        const TypeName *typeName, int32 *typmod_p,
      75             :                        bool temp_ok, bool missing_ok)
      76             : {
      77             :     Oid         typoid;
      78             :     HeapTuple   tup;
      79             :     int32       typmod;
      80             : 
      81     1762448 :     if (typeName->names == NIL)
      82             :     {
      83             :         /* We have the OID already if it's an internally generated TypeName */
      84      690604 :         typoid = typeName->typeOid;
      85             :     }
      86     1071844 :     else if (typeName->pct_type)
      87             :     {
      88             :         /* Handle %TYPE reference to type of an existing field */
      89          16 :         RangeVar   *rel = makeRangeVar(NULL, NULL, typeName->location);
      90          16 :         char       *field = NULL;
      91             :         Oid         relid;
      92             :         AttrNumber  attnum;
      93             : 
      94             :         /* deconstruct the name list */
      95          16 :         switch (list_length(typeName->names))
      96             :         {
      97           0 :             case 1:
      98           0 :                 ereport(ERROR,
      99             :                         (errcode(ERRCODE_SYNTAX_ERROR),
     100             :                          errmsg("improper %%TYPE reference (too few dotted names): %s",
     101             :                                 NameListToString(typeName->names)),
     102             :                          parser_errposition(pstate, typeName->location)));
     103             :                 break;
     104          16 :             case 2:
     105          16 :                 rel->relname = strVal(linitial(typeName->names));
     106          16 :                 field = strVal(lsecond(typeName->names));
     107          16 :                 break;
     108           0 :             case 3:
     109           0 :                 rel->schemaname = strVal(linitial(typeName->names));
     110           0 :                 rel->relname = strVal(lsecond(typeName->names));
     111           0 :                 field = strVal(lthird(typeName->names));
     112           0 :                 break;
     113           0 :             case 4:
     114           0 :                 rel->catalogname = strVal(linitial(typeName->names));
     115           0 :                 rel->schemaname = strVal(lsecond(typeName->names));
     116           0 :                 rel->relname = strVal(lthird(typeName->names));
     117           0 :                 field = strVal(lfourth(typeName->names));
     118           0 :                 break;
     119           0 :             default:
     120           0 :                 ereport(ERROR,
     121             :                         (errcode(ERRCODE_SYNTAX_ERROR),
     122             :                          errmsg("improper %%TYPE reference (too many dotted names): %s",
     123             :                                 NameListToString(typeName->names)),
     124             :                          parser_errposition(pstate, typeName->location)));
     125             :                 break;
     126             :         }
     127             : 
     128             :         /*
     129             :          * Look up the field.
     130             :          *
     131             :          * XXX: As no lock is taken here, this might fail in the presence of
     132             :          * concurrent DDL.  But taking a lock would carry a performance
     133             :          * penalty and would also require a permissions check.
     134             :          */
     135          16 :         relid = RangeVarGetRelid(rel, NoLock, missing_ok);
     136          16 :         attnum = get_attnum(relid, field);
     137          16 :         if (attnum == InvalidAttrNumber)
     138             :         {
     139           0 :             if (missing_ok)
     140           0 :                 typoid = InvalidOid;
     141             :             else
     142           0 :                 ereport(ERROR,
     143             :                         (errcode(ERRCODE_UNDEFINED_COLUMN),
     144             :                          errmsg("column \"%s\" of relation \"%s\" does not exist",
     145             :                                 field, rel->relname),
     146             :                          parser_errposition(pstate, typeName->location)));
     147             :         }
     148             :         else
     149             :         {
     150          16 :             typoid = get_atttype(relid, attnum);
     151             : 
     152             :             /* this construct should never have an array indicator */
     153             :             Assert(typeName->arrayBounds == NIL);
     154             : 
     155             :             /* emit nuisance notice (intentionally not errposition'd) */
     156          16 :             ereport(NOTICE,
     157             :                     (errmsg("type reference %s converted to %s",
     158             :                             TypeNameToString(typeName),
     159             :                             format_type_be(typoid))));
     160             :         }
     161             :     }
     162             :     else
     163             :     {
     164             :         /* Normal reference to a type name */
     165             :         char       *schemaname;
     166             :         char       *typname;
     167             : 
     168             :         /* deconstruct the name list */
     169     1071828 :         DeconstructQualifiedName(typeName->names, &schemaname, &typname);
     170             : 
     171     1071828 :         if (schemaname)
     172             :         {
     173             :             /* Look in specific schema only */
     174             :             Oid         namespaceId;
     175             :             ParseCallbackState pcbstate;
     176             : 
     177      378758 :             setup_parser_errposition_callback(&pcbstate, pstate, typeName->location);
     178             : 
     179      378758 :             namespaceId = LookupExplicitNamespace(schemaname, missing_ok);
     180      378754 :             if (OidIsValid(namespaceId))
     181      378690 :                 typoid = GetSysCacheOid2(TYPENAMENSP, Anum_pg_type_oid,
     182             :                                          PointerGetDatum(typname),
     183             :                                          ObjectIdGetDatum(namespaceId));
     184             :             else
     185          64 :                 typoid = InvalidOid;
     186             : 
     187      378754 :             cancel_parser_errposition_callback(&pcbstate);
     188             :         }
     189             :         else
     190             :         {
     191             :             /* Unqualified type name, so search the search path */
     192      693070 :             typoid = TypenameGetTypidExtended(typname, temp_ok);
     193             :         }
     194             : 
     195             :         /* If an array reference, return the array type instead */
     196     1071824 :         if (typeName->arrayBounds != NIL)
     197       12624 :             typoid = get_array_type(typoid);
     198             :     }
     199             : 
     200     1762444 :     if (!OidIsValid(typoid))
     201             :     {
     202       98926 :         if (typmod_p)
     203          26 :             *typmod_p = -1;
     204       98926 :         return NULL;
     205             :     }
     206             : 
     207     1663518 :     tup = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typoid));
     208     1663518 :     if (!HeapTupleIsValid(tup)) /* should not happen */
     209           0 :         elog(ERROR, "cache lookup failed for type %u", typoid);
     210             : 
     211     1663518 :     typmod = typenameTypeMod(pstate, typeName, (Type) tup);
     212             : 
     213     1663514 :     if (typmod_p)
     214     1366584 :         *typmod_p = typmod;
     215             : 
     216     1663514 :     return (Type) tup;
     217             : }
     218             : 
     219             : /*
     220             :  * LookupTypeNameOid
     221             :  *      Given a TypeName object, lookup the pg_type syscache entry of the type.
     222             :  *      Returns InvalidOid if no such type can be found.  If the type is found,
     223             :  *      return its Oid.
     224             :  *
     225             :  * NB: direct callers of this function need to be aware that the type OID
     226             :  * returned may correspond to a shell type.  Most code should go through
     227             :  * typenameTypeId instead.
     228             :  *
     229             :  * pstate is only used for error location info, and may be NULL.
     230             :  */
     231             : Oid
     232       41810 : LookupTypeNameOid(ParseState *pstate, const TypeName *typeName, bool missing_ok)
     233             : {
     234             :     Oid         typoid;
     235             :     Type        tup;
     236             : 
     237       41810 :     tup = LookupTypeName(pstate, typeName, NULL, missing_ok);
     238       41810 :     if (tup == NULL)
     239             :     {
     240         122 :         if (!missing_ok)
     241          22 :             ereport(ERROR,
     242             :                     (errcode(ERRCODE_UNDEFINED_OBJECT),
     243             :                      errmsg("type \"%s\" does not exist",
     244             :                             TypeNameToString(typeName)),
     245             :                      parser_errposition(pstate, typeName->location)));
     246             : 
     247         100 :         return InvalidOid;
     248             :     }
     249             : 
     250       41688 :     typoid = ((Form_pg_type) GETSTRUCT(tup))->oid;
     251       41688 :     ReleaseSysCache(tup);
     252             : 
     253       41688 :     return typoid;
     254             : }
     255             : 
     256             : /*
     257             :  * typenameType - given a TypeName, return a Type structure and typmod
     258             :  *
     259             :  * This is equivalent to LookupTypeName, except that this will report
     260             :  * a suitable error message if the type cannot be found or is not defined.
     261             :  * Callers of this can therefore assume the result is a fully valid type.
     262             :  */
     263             : Type
     264     1423030 : typenameType(ParseState *pstate, const TypeName *typeName, int32 *typmod_p)
     265             : {
     266             :     Type        tup;
     267             : 
     268     1423030 :     tup = LookupTypeName(pstate, typeName, typmod_p, false);
     269     1423026 :     if (tup == NULL)
     270          20 :         ereport(ERROR,
     271             :                 (errcode(ERRCODE_UNDEFINED_OBJECT),
     272             :                  errmsg("type \"%s\" does not exist",
     273             :                         TypeNameToString(typeName)),
     274             :                  parser_errposition(pstate, typeName->location)));
     275     1423006 :     if (!((Form_pg_type) GETSTRUCT(tup))->typisdefined)
     276           4 :         ereport(ERROR,
     277             :                 (errcode(ERRCODE_UNDEFINED_OBJECT),
     278             :                  errmsg("type \"%s\" is only a shell",
     279             :                         TypeNameToString(typeName)),
     280             :                  parser_errposition(pstate, typeName->location)));
     281     1423002 :     return tup;
     282             : }
     283             : 
     284             : /*
     285             :  * typenameTypeId - given a TypeName, return the type's OID
     286             :  *
     287             :  * This is similar to typenameType, but we only hand back the type OID
     288             :  * not the syscache entry.
     289             :  */
     290             : Oid
     291        6760 : typenameTypeId(ParseState *pstate, const TypeName *typeName)
     292             : {
     293             :     Oid         typoid;
     294             :     Type        tup;
     295             : 
     296        6760 :     tup = typenameType(pstate, typeName, NULL);
     297        6750 :     typoid = ((Form_pg_type) GETSTRUCT(tup))->oid;
     298        6750 :     ReleaseSysCache(tup);
     299             : 
     300        6750 :     return typoid;
     301             : }
     302             : 
     303             : /*
     304             :  * typenameTypeIdAndMod - given a TypeName, return the type's OID and typmod
     305             :  *
     306             :  * This is equivalent to typenameType, but we only hand back the type OID
     307             :  * and typmod, not the syscache entry.
     308             :  */
     309             : void
     310     1360490 : typenameTypeIdAndMod(ParseState *pstate, const TypeName *typeName,
     311             :                      Oid *typeid_p, int32 *typmod_p)
     312             : {
     313             :     Type        tup;
     314             : 
     315     1360490 :     tup = typenameType(pstate, typeName, typmod_p);
     316     1360486 :     *typeid_p = ((Form_pg_type) GETSTRUCT(tup))->oid;
     317     1360486 :     ReleaseSysCache(tup);
     318     1360486 : }
     319             : 
     320             : /*
     321             :  * typenameTypeMod - given a TypeName, return the internal typmod value
     322             :  *
     323             :  * This will throw an error if the TypeName includes type modifiers that are
     324             :  * illegal for the data type.
     325             :  *
     326             :  * The actual type OID represented by the TypeName must already have been
     327             :  * looked up, and is passed as "typ".
     328             :  *
     329             :  * pstate is only used for error location info, and may be NULL.
     330             :  */
     331             : static int32
     332     1663518 : typenameTypeMod(ParseState *pstate, const TypeName *typeName, Type typ)
     333             : {
     334             :     int32       result;
     335             :     Oid         typmodin;
     336             :     Datum      *datums;
     337             :     int         n;
     338             :     ListCell   *l;
     339             :     ArrayType  *arrtypmod;
     340             :     ParseCallbackState pcbstate;
     341             : 
     342             :     /* Return prespecified typmod if no typmod expressions */
     343     1663518 :     if (typeName->typmods == NIL)
     344     1656808 :         return typeName->typemod;
     345             : 
     346             :     /*
     347             :      * Else, type had better accept typmods.  We give a special error message
     348             :      * for the shell-type case, since a shell couldn't possibly have a
     349             :      * typmodin function.
     350             :      */
     351        6710 :     if (!((Form_pg_type) GETSTRUCT(typ))->typisdefined)
     352           0 :         ereport(ERROR,
     353             :                 (errcode(ERRCODE_SYNTAX_ERROR),
     354             :                  errmsg("type modifier cannot be specified for shell type \"%s\"",
     355             :                         TypeNameToString(typeName)),
     356             :                  parser_errposition(pstate, typeName->location)));
     357             : 
     358        6710 :     typmodin = ((Form_pg_type) GETSTRUCT(typ))->typmodin;
     359             : 
     360        6710 :     if (typmodin == InvalidOid)
     361           0 :         ereport(ERROR,
     362             :                 (errcode(ERRCODE_SYNTAX_ERROR),
     363             :                  errmsg("type modifier is not allowed for type \"%s\"",
     364             :                         TypeNameToString(typeName)),
     365             :                  parser_errposition(pstate, typeName->location)));
     366             : 
     367             :     /*
     368             :      * Convert the list of raw-grammar-output expressions to a cstring array.
     369             :      * Currently, we allow simple numeric constants, string literals, and
     370             :      * identifiers; possibly this list could be extended.
     371             :      */
     372        6710 :     datums = (Datum *) palloc(list_length(typeName->typmods) * sizeof(Datum));
     373        6710 :     n = 0;
     374       15082 :     foreach(l, typeName->typmods)
     375             :     {
     376        8372 :         Node       *tm = (Node *) lfirst(l);
     377        8372 :         char       *cstr = NULL;
     378             : 
     379        8372 :         if (IsA(tm, A_Const))
     380             :         {
     381        8372 :             A_Const    *ac = (A_Const *) tm;
     382             : 
     383        8372 :             if (IsA(&ac->val, Integer))
     384             :             {
     385        8372 :                 cstr = psprintf("%ld", (long) ac->val.ival.val);
     386             :             }
     387           0 :             else if (IsA(&ac->val, Float))
     388             :             {
     389             :                 /* we can just use the string representation directly. */
     390           0 :                 cstr = ac->val.fval.val;
     391             :             }
     392           0 :             else if (IsA(&ac->val, String))
     393             :             {
     394             :                 /* we can just use the string representation directly. */
     395           0 :                 cstr = ac->val.sval.val;
     396             :             }
     397             :         }
     398           0 :         else if (IsA(tm, ColumnRef))
     399             :         {
     400           0 :             ColumnRef  *cr = (ColumnRef *) tm;
     401             : 
     402           0 :             if (list_length(cr->fields) == 1 &&
     403           0 :                 IsA(linitial(cr->fields), String))
     404           0 :                 cstr = strVal(linitial(cr->fields));
     405             :         }
     406        8372 :         if (!cstr)
     407           0 :             ereport(ERROR,
     408             :                     (errcode(ERRCODE_SYNTAX_ERROR),
     409             :                      errmsg("type modifiers must be simple constants or identifiers"),
     410             :                      parser_errposition(pstate, typeName->location)));
     411        8372 :         datums[n++] = CStringGetDatum(cstr);
     412             :     }
     413             : 
     414             :     /* hardwired knowledge about cstring's representation details here */
     415        6710 :     arrtypmod = construct_array(datums, n, CSTRINGOID,
     416             :                                 -2, false, TYPALIGN_CHAR);
     417             : 
     418             :     /* arrange to report location if type's typmodin function fails */
     419        6710 :     setup_parser_errposition_callback(&pcbstate, pstate, typeName->location);
     420             : 
     421        6710 :     result = DatumGetInt32(OidFunctionCall1(typmodin,
     422             :                                             PointerGetDatum(arrtypmod)));
     423             : 
     424        6706 :     cancel_parser_errposition_callback(&pcbstate);
     425             : 
     426        6706 :     pfree(datums);
     427        6706 :     pfree(arrtypmod);
     428             : 
     429        6706 :     return result;
     430             : }
     431             : 
     432             : /*
     433             :  * appendTypeNameToBuffer
     434             :  *      Append a string representing the name of a TypeName to a StringInfo.
     435             :  *      This is the shared guts of TypeNameToString and TypeNameListToString.
     436             :  *
     437             :  * NB: this must work on TypeNames that do not describe any actual type;
     438             :  * it is mostly used for reporting lookup errors.
     439             :  */
     440             : static void
     441       21972 : appendTypeNameToBuffer(const TypeName *typeName, StringInfo string)
     442             : {
     443       21972 :     if (typeName->names != NIL)
     444             :     {
     445             :         /* Emit possibly-qualified name as-is */
     446             :         ListCell   *l;
     447             : 
     448       44084 :         foreach(l, typeName->names)
     449             :         {
     450       22112 :             if (l != list_head(typeName->names))
     451         140 :                 appendStringInfoChar(string, '.');
     452       22112 :             appendStringInfoString(string, strVal(lfirst(l)));
     453             :         }
     454             :     }
     455             :     else
     456             :     {
     457             :         /* Look up internally-specified type */
     458           0 :         appendStringInfoString(string, format_type_be(typeName->typeOid));
     459             :     }
     460             : 
     461             :     /*
     462             :      * Add decoration as needed, but only for fields considered by
     463             :      * LookupTypeName
     464             :      */
     465       21972 :     if (typeName->pct_type)
     466          16 :         appendStringInfoString(string, "%TYPE");
     467             : 
     468       21972 :     if (typeName->arrayBounds != NIL)
     469           4 :         appendStringInfoString(string, "[]");
     470       21972 : }
     471             : 
     472             : /*
     473             :  * TypeNameToString
     474             :  *      Produce a string representing the name of a TypeName.
     475             :  *
     476             :  * NB: this must work on TypeNames that do not describe any actual type;
     477             :  * it is mostly used for reporting lookup errors.
     478             :  */
     479             : char *
     480       21954 : TypeNameToString(const TypeName *typeName)
     481             : {
     482             :     StringInfoData string;
     483             : 
     484       21954 :     initStringInfo(&string);
     485       21954 :     appendTypeNameToBuffer(typeName, &string);
     486       21954 :     return string.data;
     487             : }
     488             : 
     489             : /*
     490             :  * TypeNameListToString
     491             :  *      Produce a string representing the name(s) of a List of TypeNames
     492             :  */
     493             : char *
     494          18 : TypeNameListToString(List *typenames)
     495             : {
     496             :     StringInfoData string;
     497             :     ListCell   *l;
     498             : 
     499          18 :     initStringInfo(&string);
     500          36 :     foreach(l, typenames)
     501             :     {
     502          18 :         TypeName   *typeName = lfirst_node(TypeName, l);
     503             : 
     504          18 :         if (l != list_head(typenames))
     505           8 :             appendStringInfoChar(&string, ',');
     506          18 :         appendTypeNameToBuffer(typeName, &string);
     507             :     }
     508          18 :     return string.data;
     509             : }
     510             : 
     511             : /*
     512             :  * LookupCollation
     513             :  *
     514             :  * Look up collation by name, return OID, with support for error location.
     515             :  */
     516             : Oid
     517        3630 : LookupCollation(ParseState *pstate, List *collnames, int location)
     518             : {
     519             :     Oid         colloid;
     520             :     ParseCallbackState pcbstate;
     521             : 
     522        3630 :     if (pstate)
     523        3498 :         setup_parser_errposition_callback(&pcbstate, pstate, location);
     524             : 
     525        3630 :     colloid = get_collation_oid(collnames, false);
     526             : 
     527        3630 :     if (pstate)
     528        3498 :         cancel_parser_errposition_callback(&pcbstate);
     529             : 
     530        3630 :     return colloid;
     531             : }
     532             : 
     533             : /*
     534             :  * GetColumnDefCollation
     535             :  *
     536             :  * Get the collation to be used for a column being defined, given the
     537             :  * ColumnDef node and the previously-determined column type OID.
     538             :  *
     539             :  * pstate is only used for error location purposes, and can be NULL.
     540             :  */
     541             : Oid
     542      743084 : GetColumnDefCollation(ParseState *pstate, ColumnDef *coldef, Oid typeOid)
     543             : {
     544             :     Oid         result;
     545      743084 :     Oid         typcollation = get_typcollation(typeOid);
     546      743084 :     int         location = coldef->location;
     547             : 
     548      743084 :     if (coldef->collClause)
     549             :     {
     550             :         /* We have a raw COLLATE clause, so look up the collation */
     551         132 :         location = coldef->collClause->location;
     552         132 :         result = LookupCollation(pstate, coldef->collClause->collname,
     553             :                                  location);
     554             :     }
     555      742952 :     else if (OidIsValid(coldef->collOid))
     556             :     {
     557             :         /* Precooked collation spec, use that */
     558      399830 :         result = coldef->collOid;
     559             :     }
     560             :     else
     561             :     {
     562             :         /* Use the type's default collation if any */
     563      343122 :         result = typcollation;
     564             :     }
     565             : 
     566             :     /* Complain if COLLATE is applied to an uncollatable type */
     567      743084 :     if (OidIsValid(result) && !OidIsValid(typcollation))
     568           0 :         ereport(ERROR,
     569             :                 (errcode(ERRCODE_DATATYPE_MISMATCH),
     570             :                  errmsg("collations are not supported by type %s",
     571             :                         format_type_be(typeOid)),
     572             :                  parser_errposition(pstate, location)));
     573             : 
     574      743084 :     return result;
     575             : }
     576             : 
     577             : /* return a Type structure, given a type id */
     578             : /* NB: caller must ReleaseSysCache the type tuple when done with it */
     579             : Type
     580     1200206 : typeidType(Oid id)
     581             : {
     582             :     HeapTuple   tup;
     583             : 
     584     1200206 :     tup = SearchSysCache1(TYPEOID, ObjectIdGetDatum(id));
     585     1200206 :     if (!HeapTupleIsValid(tup))
     586           0 :         elog(ERROR, "cache lookup failed for type %u", id);
     587     1200206 :     return (Type) tup;
     588             : }
     589             : 
     590             : /* given type (as type struct), return the type OID */
     591             : Oid
     592      197810 : typeTypeId(Type tp)
     593             : {
     594      197810 :     if (tp == NULL)             /* probably useless */
     595           0 :         elog(ERROR, "typeTypeId() called with NULL type struct");
     596      197810 :     return ((Form_pg_type) GETSTRUCT(tp))->oid;
     597             : }
     598             : 
     599             : /* given type (as type struct), return the length of type */
     600             : int16
     601     1161676 : typeLen(Type t)
     602             : {
     603             :     Form_pg_type typ;
     604             : 
     605     1161676 :     typ = (Form_pg_type) GETSTRUCT(t);
     606     1161676 :     return typ->typlen;
     607             : }
     608             : 
     609             : /* given type (as type struct), return its 'byval' attribute */
     610             : bool
     611     1161676 : typeByVal(Type t)
     612             : {
     613             :     Form_pg_type typ;
     614             : 
     615     1161676 :     typ = (Form_pg_type) GETSTRUCT(t);
     616     1161676 :     return typ->typbyval;
     617             : }
     618             : 
     619             : /* given type (as type struct), return the type's name */
     620             : char *
     621           0 : typeTypeName(Type t)
     622             : {
     623             :     Form_pg_type typ;
     624             : 
     625           0 :     typ = (Form_pg_type) GETSTRUCT(t);
     626             :     /* pstrdup here because result may need to outlive the syscache entry */
     627           0 :     return pstrdup(NameStr(typ->typname));
     628             : }
     629             : 
     630             : /* given type (as type struct), return its 'typrelid' attribute */
     631             : Oid
     632         464 : typeTypeRelid(Type typ)
     633             : {
     634             :     Form_pg_type typtup;
     635             : 
     636         464 :     typtup = (Form_pg_type) GETSTRUCT(typ);
     637         464 :     return typtup->typrelid;
     638             : }
     639             : 
     640             : /* given type (as type struct), return its 'typcollation' attribute */
     641             : Oid
     642     1161676 : typeTypeCollation(Type typ)
     643             : {
     644             :     Form_pg_type typtup;
     645             : 
     646     1161676 :     typtup = (Form_pg_type) GETSTRUCT(typ);
     647     1161676 :     return typtup->typcollation;
     648             : }
     649             : 
     650             : /*
     651             :  * Given a type structure and a string, returns the internal representation
     652             :  * of that string.  The "string" can be NULL to perform conversion of a NULL
     653             :  * (which might result in failure, if the input function rejects NULLs).
     654             :  */
     655             : Datum
     656     1161676 : stringTypeDatum(Type tp, char *string, int32 atttypmod)
     657             : {
     658     1161676 :     Form_pg_type typform = (Form_pg_type) GETSTRUCT(tp);
     659     1161676 :     Oid         typinput = typform->typinput;
     660     1161676 :     Oid         typioparam = getTypeIOParam(tp);
     661             : 
     662     1161676 :     return OidInputFunctionCall(typinput, string, typioparam, atttypmod);
     663             : }
     664             : 
     665             : /*
     666             :  * Given a typeid, return the type's typrelid (associated relation), if any.
     667             :  * Returns InvalidOid if type is not a composite type.
     668             :  */
     669             : Oid
     670        7830 : typeidTypeRelid(Oid type_id)
     671             : {
     672             :     HeapTuple   typeTuple;
     673             :     Form_pg_type type;
     674             :     Oid         result;
     675             : 
     676        7830 :     typeTuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(type_id));
     677        7830 :     if (!HeapTupleIsValid(typeTuple))
     678           0 :         elog(ERROR, "cache lookup failed for type %u", type_id);
     679        7830 :     type = (Form_pg_type) GETSTRUCT(typeTuple);
     680        7830 :     result = type->typrelid;
     681        7830 :     ReleaseSysCache(typeTuple);
     682        7830 :     return result;
     683             : }
     684             : 
     685             : /*
     686             :  * Given a typeid, return the type's typrelid (associated relation), if any.
     687             :  * Returns InvalidOid if type is not a composite type or a domain over one.
     688             :  * This is the same as typeidTypeRelid(getBaseType(type_id)), but faster.
     689             :  */
     690             : Oid
     691     2385272 : typeOrDomainTypeRelid(Oid type_id)
     692             : {
     693             :     HeapTuple   typeTuple;
     694             :     Form_pg_type type;
     695             :     Oid         result;
     696             : 
     697             :     for (;;)
     698             :     {
     699     2385272 :         typeTuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(type_id));
     700     2385272 :         if (!HeapTupleIsValid(typeTuple))
     701           0 :             elog(ERROR, "cache lookup failed for type %u", type_id);
     702     2385272 :         type = (Form_pg_type) GETSTRUCT(typeTuple);
     703     2385272 :         if (type->typtype != TYPTYPE_DOMAIN)
     704             :         {
     705             :             /* Not a domain, so done looking through domains */
     706     2120296 :             break;
     707             :         }
     708             :         /* It is a domain, so examine the base type instead */
     709      264976 :         type_id = type->typbasetype;
     710      264976 :         ReleaseSysCache(typeTuple);
     711             :     }
     712     2120296 :     result = type->typrelid;
     713     2120296 :     ReleaseSysCache(typeTuple);
     714     2120296 :     return result;
     715             : }
     716             : 
     717             : /*
     718             :  * error context callback for parse failure during parseTypeString()
     719             :  */
     720             : static void
     721           0 : pts_error_callback(void *arg)
     722             : {
     723           0 :     const char *str = (const char *) arg;
     724             : 
     725           0 :     errcontext("invalid type name \"%s\"", str);
     726           0 : }
     727             : 
     728             : /*
     729             :  * Given a string that is supposed to be a SQL-compatible type declaration,
     730             :  * such as "int4" or "integer" or "character varying(32)", parse
     731             :  * the string and return the result as a TypeName.
     732             :  * If the string cannot be parsed as a type, an error is raised.
     733             :  */
     734             : TypeName *
     735        5468 : typeStringToTypeName(const char *str)
     736             : {
     737             :     List       *raw_parsetree_list;
     738             :     TypeName   *typeName;
     739             :     ErrorContextCallback ptserrcontext;
     740             : 
     741             :     /* make sure we give useful error for empty input */
     742        5468 :     if (strspn(str, " \t\n\r\f") == strlen(str))
     743           0 :         goto fail;
     744             : 
     745             :     /*
     746             :      * Setup error traceback support in case of ereport() during parse
     747             :      */
     748        5468 :     ptserrcontext.callback = pts_error_callback;
     749        5468 :     ptserrcontext.arg = unconstify(char *, str);
     750        5468 :     ptserrcontext.previous = error_context_stack;
     751        5468 :     error_context_stack = &ptserrcontext;
     752             : 
     753        5468 :     raw_parsetree_list = raw_parser(str, RAW_PARSE_TYPE_NAME);
     754             : 
     755        5468 :     error_context_stack = ptserrcontext.previous;
     756             : 
     757             :     /* We should get back exactly one TypeName node. */
     758             :     Assert(list_length(raw_parsetree_list) == 1);
     759        5468 :     typeName = linitial_node(TypeName, raw_parsetree_list);
     760             : 
     761             :     /* The grammar allows SETOF in TypeName, but we don't want that here. */
     762        5468 :     if (typeName->setof)
     763           0 :         goto fail;
     764             : 
     765        5468 :     return typeName;
     766             : 
     767           0 : fail:
     768           0 :     ereport(ERROR,
     769             :             (errcode(ERRCODE_SYNTAX_ERROR),
     770             :              errmsg("invalid type name \"%s\"", str)));
     771             :     return NULL;                /* keep compiler quiet */
     772             : }
     773             : 
     774             : /*
     775             :  * Given a string that is supposed to be a SQL-compatible type declaration,
     776             :  * such as "int4" or "integer" or "character varying(32)", parse
     777             :  * the string and convert it to a type OID and type modifier.
     778             :  * If missing_ok is true, InvalidOid is returned rather than raising an error
     779             :  * when the type name is not found.
     780             :  */
     781             : void
     782        1070 : parseTypeString(const char *str, Oid *typeid_p, int32 *typmod_p, bool missing_ok)
     783             : {
     784             :     TypeName   *typeName;
     785             :     Type        tup;
     786             : 
     787        1070 :     typeName = typeStringToTypeName(str);
     788             : 
     789        1070 :     tup = LookupTypeName(NULL, typeName, typmod_p, missing_ok);
     790        1066 :     if (tup == NULL)
     791             :     {
     792          22 :         if (!missing_ok)
     793          14 :             ereport(ERROR,
     794             :                     (errcode(ERRCODE_UNDEFINED_OBJECT),
     795             :                      errmsg("type \"%s\" does not exist",
     796             :                             TypeNameToString(typeName)),
     797             :                      parser_errposition(NULL, typeName->location)));
     798           8 :         *typeid_p = InvalidOid;
     799             :     }
     800             :     else
     801             :     {
     802        1044 :         Form_pg_type typ = (Form_pg_type) GETSTRUCT(tup);
     803             : 
     804        1044 :         if (!typ->typisdefined)
     805           0 :             ereport(ERROR,
     806             :                     (errcode(ERRCODE_UNDEFINED_OBJECT),
     807             :                      errmsg("type \"%s\" is only a shell",
     808             :                             TypeNameToString(typeName)),
     809             :                      parser_errposition(NULL, typeName->location)));
     810        1044 :         *typeid_p = typ->oid;
     811        1044 :         ReleaseSysCache(tup);
     812             :     }
     813        1052 : }

Generated by: LCOV version 1.14