1 /*-------------------------------------------------------------------------
4 * Display type names "nicely".
7 * Portions Copyright (c) 1996-2014, 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 format_type_be_qualified(Oid type_oid)
102 return format_type_internal(type_oid, -1, false, false, true);
106 * This version allows a nondefault typemod to be specified.
109 format_type_with_typemod(Oid type_oid, int32 typemod)
111 return format_type_internal(type_oid, typemod, true, false, false);
115 format_type_internal(Oid type_oid, int32 typemod,
116 bool typemod_given, bool allow_invalid,
119 bool with_typemod = typemod_given && (typemod >= 0);
121 Form_pg_type typeform;
126 if (type_oid == InvalidOid && allow_invalid)
129 tuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(type_oid));
130 if (!HeapTupleIsValid(tuple))
133 return pstrdup("???");
135 elog(ERROR, "cache lookup failed for type %u", type_oid);
137 typeform = (Form_pg_type) GETSTRUCT(tuple);
140 * Check if it's a regular (variable length) array type. Fixed-length
141 * array types such as "name" shouldn't get deconstructed. As of Postgres
142 * 8.1, rather than checking typlen we check the toast property, and don't
143 * deconstruct "plain storage" array types --- this is because we don't
144 * want to show oidvector as oid[].
146 array_base_type = typeform->typelem;
148 if (array_base_type != InvalidOid &&
149 typeform->typstorage != 'p')
151 /* Switch our attention to the array element type */
152 ReleaseSysCache(tuple);
153 tuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(array_base_type));
154 if (!HeapTupleIsValid(tuple))
157 return pstrdup("???[]");
159 elog(ERROR, "cache lookup failed for type %u", type_oid);
161 typeform = (Form_pg_type) GETSTRUCT(tuple);
162 type_oid = array_base_type;
169 * See if we want to special-case the output for certain built-in types.
170 * Note that these special cases should all correspond to special
171 * productions in gram.y, to ensure that the type name will be taken as a
172 * system type, not a user type of the same name.
174 * If we do not provide a special-case output here, the type name will be
175 * handled the same way as a user type name --- in particular, it will be
176 * double-quoted if it matches any lexer keyword. This behavior is
177 * essential for some cases, such as types "bit" and "char".
179 buf = NULL; /* flag for no special case */
185 buf = printTypmod("bit", typemod, typeform->typmodout);
186 else if (typemod_given)
189 * bit with typmod -1 is not the same as BIT, which means
190 * BIT(1) per SQL spec. Report it as the quoted typename so
191 * that parser will not assign a bogus typmod.
195 buf = pstrdup("bit");
199 buf = pstrdup("boolean");
204 buf = printTypmod("character", typemod, typeform->typmodout);
205 else if (typemod_given)
208 * bpchar with typmod -1 is not the same as CHARACTER, which
209 * means CHARACTER(1) per SQL spec. Report it as bpchar so
210 * that parser will not assign a bogus typmod.
214 buf = pstrdup("character");
218 buf = pstrdup("real");
222 buf = pstrdup("double precision");
226 buf = pstrdup("smallint");
230 buf = pstrdup("integer");
234 buf = pstrdup("bigint");
239 buf = printTypmod("numeric", typemod, typeform->typmodout);
241 buf = pstrdup("numeric");
246 buf = printTypmod("interval", typemod, typeform->typmodout);
248 buf = pstrdup("interval");
253 buf = printTypmod("time", typemod, typeform->typmodout);
255 buf = pstrdup("time without time zone");
260 buf = printTypmod("time", typemod, typeform->typmodout);
262 buf = pstrdup("time with time zone");
267 buf = printTypmod("timestamp", typemod, typeform->typmodout);
269 buf = pstrdup("timestamp without time zone");
274 buf = printTypmod("timestamp", typemod, typeform->typmodout);
276 buf = pstrdup("timestamp with time zone");
281 buf = printTypmod("bit varying", typemod, typeform->typmodout);
283 buf = pstrdup("bit varying");
288 buf = printTypmod("character varying", typemod, typeform->typmodout);
290 buf = pstrdup("character varying");
297 * Default handling: report the name as it appears in the catalog.
298 * Here, we must qualify the name if it is not visible in the search
299 * path, and we must double-quote it if it's not a standard identifier
300 * or if it matches any keyword.
305 if (!force_qualify && TypeIsVisible(type_oid))
308 nspname = get_namespace_name(typeform->typnamespace);
310 typname = NameStr(typeform->typname);
312 buf = quote_qualified_identifier(nspname, typname);
315 buf = printTypmod(buf, typemod, typeform->typmodout);
319 buf = psprintf("%s[]", buf);
321 ReleaseSysCache(tuple);
328 * Add typmod decoration to the basic type name
331 printTypmod(const char *typname, int32 typmod, Oid typmodout)
335 /* Shouldn't be called if typmod is -1 */
338 if (typmodout == InvalidOid)
340 /* Default behavior: just print the integer typmod with parens */
341 res = psprintf("%s(%d)", typname, (int) typmod);
345 /* Use the type-specific typmodout procedure */
348 tmstr = DatumGetCString(OidFunctionCall1(typmodout,
349 Int32GetDatum(typmod)));
350 res = psprintf("%s%s", typname, tmstr);
358 * type_maximum_size --- determine maximum width of a variable-width column
360 * If the max width is indeterminate, return -1. In particular, we return
361 * -1 for any type not known to this routine. We assume the caller has
362 * already determined that the type is a variable-width type, so it's not
363 * necessary to look up the type's pg_type tuple here.
365 * This may appear unrelated to format_type(), but in fact the two routines
366 * share knowledge of the encoding of typmod for different types, so it's
367 * convenient to keep them together. (XXX now that most of this knowledge
368 * has been pushed out of format_type into the typmodout functions, it's
369 * interesting to wonder if it's worth trying to factor this code too...)
372 type_maximum_size(Oid type_oid, int32 typemod)
381 /* typemod includes varlena header */
383 /* typemod is in characters not bytes */
384 return (typemod - VARHDRSZ) *
385 pg_encoding_max_length(GetDatabaseEncoding())
389 return numeric_maximum_size(typemod);
393 /* typemod is the (max) number of bits */
394 return (typemod + (BITS_PER_BYTE - 1)) / BITS_PER_BYTE
398 /* Unknown type, or unlimited-width type such as 'text' */
404 * oidvectortypes - converts a vector of type OIDs to "typname" list
407 oidvectortypes(PG_FUNCTION_ARGS)
409 oidvector *oidArray = (oidvector *) PG_GETARG_POINTER(0);
411 int numargs = oidArray->dim1;
416 total = 20 * numargs + 1;
417 result = palloc(total);
421 for (num = 0; num < numargs; num++)
423 char *typename = format_type_internal(oidArray->values[num], -1,
425 size_t slen = strlen(typename);
427 if (left < (slen + 2))
430 result = repalloc(result, total);
436 strcat(result, ", ");
439 strcat(result, typename);
443 PG_RETURN_TEXT_P(cstring_to_text(result));