1 /*-------------------------------------------------------------------------
4 * Display type names "nicely".
7 * Portions Copyright (c) 1996-2018, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
11 * src/backend/utils/adt/format_type.c
13 *-------------------------------------------------------------------------
20 #include "access/htup_details.h"
21 #include "catalog/namespace.h"
22 #include "catalog/pg_type.h"
23 #include "utils/builtins.h"
24 #include "utils/lsyscache.h"
25 #include "utils/numeric.h"
26 #include "utils/syscache.h"
27 #include "mb/pg_wchar.h"
29 #define MAX_INT32_LEN 11
31 static char *format_type_internal(Oid type_oid, int32 typemod,
32 bool typemod_given, bool allow_invalid,
34 static char *printTypmod(const char *typname, int32 typmod, Oid typmodout);
38 * SQL function: format_type(type_oid, typemod)
40 * `type_oid' is from pg_type.oid, `typemod' is from
41 * pg_attribute.atttypmod. This function will get the type name and
42 * format it and the modifier to canonical SQL format, if the type is
43 * a standard type. Otherwise you just get pg_type.typname back,
44 * double quoted if it contains funny characters or matches a keyword.
46 * If typemod is NULL then we are formatting a type name in a context where
47 * no typemod is available, eg a function argument or result type. This
48 * yields a slightly different result from specifying typemod = -1 in some
49 * cases. Given typemod = -1 we feel compelled to produce an output that
50 * the parser will interpret as having typemod -1, so that pg_dump will
51 * produce CREATE TABLE commands that recreate the original state. But
52 * given NULL typemod, we assume that the parser's interpretation of
53 * typemod doesn't matter, and so we are willing to output a slightly
54 * "prettier" representation of the same type. For example, type = bpchar
55 * and typemod = NULL gets you "character", whereas typemod = -1 gets you
56 * "bpchar" --- the former will be interpreted as character(1) by the
57 * parser, which does not yield typemod -1.
59 * XXX encoding a meaning in typemod = NULL is ugly; it'd have been
60 * cleaner to make two functions of one and two arguments respectively.
61 * Not worth changing it now, however.
64 format_type(PG_FUNCTION_ARGS)
70 /* Since this function is not strict, we must test for null args */
74 type_oid = PG_GETARG_OID(0);
77 result = format_type_internal(type_oid, -1, false, true, false);
80 typemod = PG_GETARG_INT32(1);
81 result = format_type_internal(type_oid, typemod, true, true, false);
84 PG_RETURN_TEXT_P(cstring_to_text(result));
88 * This version is for use within the backend in error messages, etc.
89 * One difference is that it will fail for an invalid type.
91 * The result is always a palloc'd string.
94 format_type_be(Oid type_oid)
96 return format_type_internal(type_oid, -1, false, false, false);
100 * This version returns a name that is always qualified (unless it's one
101 * of the SQL-keyword type names, such as TIMESTAMP WITH TIME ZONE).
104 format_type_be_qualified(Oid type_oid)
106 return format_type_internal(type_oid, -1, false, false, true);
110 * This version allows a nondefault typemod to be specified.
113 format_type_with_typemod(Oid type_oid, int32 typemod)
115 return format_type_internal(type_oid, typemod, true, false, false);
119 * This version allows a nondefault typemod to be specified, and forces
120 * qualification of normal type names.
123 format_type_with_typemod_qualified(Oid type_oid, int32 typemod)
125 return format_type_internal(type_oid, typemod, true, false, true);
132 format_type_internal(Oid type_oid, int32 typemod,
133 bool typemod_given, bool allow_invalid,
136 bool with_typemod = typemod_given && (typemod >= 0);
138 Form_pg_type typeform;
143 if (type_oid == InvalidOid && allow_invalid)
146 tuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(type_oid));
147 if (!HeapTupleIsValid(tuple))
150 return pstrdup("???");
152 elog(ERROR, "cache lookup failed for type %u", type_oid);
154 typeform = (Form_pg_type) GETSTRUCT(tuple);
157 * Check if it's a regular (variable length) array type. Fixed-length
158 * array types such as "name" shouldn't get deconstructed. As of Postgres
159 * 8.1, rather than checking typlen we check the toast property, and don't
160 * deconstruct "plain storage" array types --- this is because we don't
161 * want to show oidvector as oid[].
163 array_base_type = typeform->typelem;
165 if (array_base_type != InvalidOid &&
166 typeform->typstorage != 'p')
168 /* Switch our attention to the array element type */
169 ReleaseSysCache(tuple);
170 tuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(array_base_type));
171 if (!HeapTupleIsValid(tuple))
174 return pstrdup("???[]");
176 elog(ERROR, "cache lookup failed for type %u", type_oid);
178 typeform = (Form_pg_type) GETSTRUCT(tuple);
179 type_oid = array_base_type;
186 * See if we want to special-case the output for certain built-in types.
187 * Note that these special cases should all correspond to special
188 * productions in gram.y, to ensure that the type name will be taken as a
189 * system type, not a user type of the same name.
191 * If we do not provide a special-case output here, the type name will be
192 * handled the same way as a user type name --- in particular, it will be
193 * double-quoted if it matches any lexer keyword. This behavior is
194 * essential for some cases, such as types "bit" and "char".
196 buf = NULL; /* flag for no special case */
202 buf = printTypmod("bit", typemod, typeform->typmodout);
203 else if (typemod_given)
206 * bit with typmod -1 is not the same as BIT, which means
207 * BIT(1) per SQL spec. Report it as the quoted typename so
208 * that parser will not assign a bogus typmod.
212 buf = pstrdup("bit");
216 buf = pstrdup("boolean");
221 buf = printTypmod("character", typemod, typeform->typmodout);
222 else if (typemod_given)
225 * bpchar with typmod -1 is not the same as CHARACTER, which
226 * means CHARACTER(1) per SQL spec. Report it as bpchar so
227 * that parser will not assign a bogus typmod.
231 buf = pstrdup("character");
235 buf = pstrdup("real");
239 buf = pstrdup("double precision");
243 buf = pstrdup("smallint");
247 buf = pstrdup("integer");
251 buf = pstrdup("bigint");
256 buf = printTypmod("numeric", typemod, typeform->typmodout);
258 buf = pstrdup("numeric");
263 buf = printTypmod("interval", typemod, typeform->typmodout);
265 buf = pstrdup("interval");
270 buf = printTypmod("time", typemod, typeform->typmodout);
272 buf = pstrdup("time without time zone");
277 buf = printTypmod("time", typemod, typeform->typmodout);
279 buf = pstrdup("time with time zone");
284 buf = printTypmod("timestamp", typemod, typeform->typmodout);
286 buf = pstrdup("timestamp without time zone");
291 buf = printTypmod("timestamp", typemod, typeform->typmodout);
293 buf = pstrdup("timestamp with time zone");
298 buf = printTypmod("bit varying", typemod, typeform->typmodout);
300 buf = pstrdup("bit varying");
305 buf = printTypmod("character varying", typemod, typeform->typmodout);
307 buf = pstrdup("character varying");
314 * Default handling: report the name as it appears in the catalog.
315 * Here, we must qualify the name if it is not visible in the search
316 * path, and we must double-quote it if it's not a standard identifier
317 * or if it matches any keyword.
322 if (!force_qualify && TypeIsVisible(type_oid))
325 nspname = get_namespace_name_or_temp(typeform->typnamespace);
327 typname = NameStr(typeform->typname);
329 buf = quote_qualified_identifier(nspname, typname);
332 buf = printTypmod(buf, typemod, typeform->typmodout);
336 buf = psprintf("%s[]", buf);
338 ReleaseSysCache(tuple);
345 * Add typmod decoration to the basic type name
348 printTypmod(const char *typname, int32 typmod, Oid typmodout)
352 /* Shouldn't be called if typmod is -1 */
355 if (typmodout == InvalidOid)
357 /* Default behavior: just print the integer typmod with parens */
358 res = psprintf("%s(%d)", typname, (int) typmod);
362 /* Use the type-specific typmodout procedure */
365 tmstr = DatumGetCString(OidFunctionCall1(typmodout,
366 Int32GetDatum(typmod)));
367 res = psprintf("%s%s", typname, tmstr);
375 * type_maximum_size --- determine maximum width of a variable-width column
377 * If the max width is indeterminate, return -1. In particular, we return
378 * -1 for any type not known to this routine. We assume the caller has
379 * already determined that the type is a variable-width type, so it's not
380 * necessary to look up the type's pg_type tuple here.
382 * This may appear unrelated to format_type(), but in fact the two routines
383 * share knowledge of the encoding of typmod for different types, so it's
384 * convenient to keep them together. (XXX now that most of this knowledge
385 * has been pushed out of format_type into the typmodout functions, it's
386 * interesting to wonder if it's worth trying to factor this code too...)
389 type_maximum_size(Oid type_oid, int32 typemod)
398 /* typemod includes varlena header */
400 /* typemod is in characters not bytes */
401 return (typemod - VARHDRSZ) *
402 pg_encoding_max_length(GetDatabaseEncoding())
406 return numeric_maximum_size(typemod);
410 /* typemod is the (max) number of bits */
411 return (typemod + (BITS_PER_BYTE - 1)) / BITS_PER_BYTE
415 /* Unknown type, or unlimited-width type such as 'text' */
421 * oidvectortypes - converts a vector of type OIDs to "typname" list
424 oidvectortypes(PG_FUNCTION_ARGS)
426 oidvector *oidArray = (oidvector *) PG_GETARG_POINTER(0);
428 int numargs = oidArray->dim1;
433 total = 20 * numargs + 1;
434 result = palloc(total);
438 for (num = 0; num < numargs; num++)
440 char *typename = format_type_internal(oidArray->values[num], -1,
442 size_t slen = strlen(typename);
444 if (left < (slen + 2))
447 result = repalloc(result, total);
453 strcat(result, ", ");
456 strcat(result, typename);
460 PG_RETURN_TEXT_P(cstring_to_text(result));