1 /*-------------------------------------------------------------------------
4 * Display type names "nicely".
7 * Portions Copyright (c) 1996-2011, 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 "catalog/namespace.h"
21 #include "catalog/pg_type.h"
22 #include "utils/builtins.h"
23 #include "utils/lsyscache.h"
24 #include "utils/numeric.h"
25 #include "utils/syscache.h"
26 #include "mb/pg_wchar.h"
28 #define MAX_INT32_LEN 11
30 static char *format_type_internal(Oid type_oid, int32 typemod,
31 bool typemod_given, bool allow_invalid);
32 static char *printTypmod(const char *typname, int32 typmod, Oid typmodout);
34 psnprintf(size_t len, const char *fmt,...)
35 /* This lets gcc check the format string for consistency. */
36 __attribute__((format(PG_PRINTF_ATTRIBUTE, 2, 3)));
40 * SQL function: format_type(type_oid, typemod)
42 * `type_oid' is from pg_type.oid, `typemod' is from
43 * pg_attribute.atttypmod. This function will get the type name and
44 * format it and the modifier to canonical SQL format, if the type is
45 * a standard type. Otherwise you just get pg_type.typname back,
46 * double quoted if it contains funny characters or matches a keyword.
48 * If typemod is NULL then we are formatting a type name in a context where
49 * no typemod is available, eg a function argument or result type. This
50 * yields a slightly different result from specifying typemod = -1 in some
51 * cases. Given typemod = -1 we feel compelled to produce an output that
52 * the parser will interpret as having typemod -1, so that pg_dump will
53 * produce CREATE TABLE commands that recreate the original state. But
54 * given NULL typemod, we assume that the parser's interpretation of
55 * typemod doesn't matter, and so we are willing to output a slightly
56 * "prettier" representation of the same type. For example, type = bpchar
57 * and typemod = NULL gets you "character", whereas typemod = -1 gets you
58 * "bpchar" --- the former will be interpreted as character(1) by the
59 * parser, which does not yield typemod -1.
61 * XXX encoding a meaning in typemod = NULL is ugly; it'd have been
62 * cleaner to make two functions of one and two arguments respectively.
63 * Not worth changing it now, however.
66 format_type(PG_FUNCTION_ARGS)
72 /* Since this function is not strict, we must test for null args */
76 type_oid = PG_GETARG_OID(0);
79 result = format_type_internal(type_oid, -1, false, true);
82 typemod = PG_GETARG_INT32(1);
83 result = format_type_internal(type_oid, typemod, true, true);
86 PG_RETURN_TEXT_P(cstring_to_text(result));
90 * This version is for use within the backend in error messages, etc.
91 * One difference is that it will fail for an invalid type.
93 * The result is always a palloc'd string.
96 format_type_be(Oid type_oid)
98 return format_type_internal(type_oid, -1, false, false);
102 * This version allows a nondefault typemod to be specified.
105 format_type_with_typemod(Oid type_oid, int32 typemod)
107 return format_type_internal(type_oid, typemod, true, false);
113 format_type_internal(Oid type_oid, int32 typemod,
114 bool typemod_given, bool allow_invalid)
116 bool with_typemod = typemod_given && (typemod >= 0);
118 Form_pg_type typeform;
123 if (type_oid == InvalidOid && allow_invalid)
126 tuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(type_oid));
127 if (!HeapTupleIsValid(tuple))
130 return pstrdup("???");
132 elog(ERROR, "cache lookup failed for type %u", type_oid);
134 typeform = (Form_pg_type) GETSTRUCT(tuple);
137 * Check if it's a regular (variable length) array type. Fixed-length
138 * array types such as "name" shouldn't get deconstructed. As of Postgres
139 * 8.1, rather than checking typlen we check the toast property, and don't
140 * deconstruct "plain storage" array types --- this is because we don't
141 * want to show oidvector as oid[].
143 array_base_type = typeform->typelem;
145 if (array_base_type != InvalidOid &&
146 typeform->typstorage != 'p')
148 /* Switch our attention to the array element type */
149 ReleaseSysCache(tuple);
150 tuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(array_base_type));
151 if (!HeapTupleIsValid(tuple))
154 return pstrdup("???[]");
156 elog(ERROR, "cache lookup failed for type %u", type_oid);
158 typeform = (Form_pg_type) GETSTRUCT(tuple);
159 type_oid = array_base_type;
166 * See if we want to special-case the output for certain built-in types.
167 * Note that these special cases should all correspond to special
168 * productions in gram.y, to ensure that the type name will be taken as a
169 * system type, not a user type of the same name.
171 * If we do not provide a special-case output here, the type name will be
172 * handled the same way as a user type name --- in particular, it will be
173 * double-quoted if it matches any lexer keyword. This behavior is
174 * essential for some cases, such as types "bit" and "char".
176 buf = NULL; /* flag for no special case */
182 buf = printTypmod("bit", typemod, typeform->typmodout);
183 else if (typemod_given)
186 * bit with typmod -1 is not the same as BIT, which means
187 * BIT(1) per SQL spec. Report it as the quoted typename so
188 * that parser will not assign a bogus typmod.
192 buf = pstrdup("bit");
196 buf = pstrdup("boolean");
201 buf = printTypmod("character", typemod, typeform->typmodout);
202 else if (typemod_given)
205 * bpchar with typmod -1 is not the same as CHARACTER, which
206 * means CHARACTER(1) per SQL spec. Report it as bpchar so
207 * that parser will not assign a bogus typmod.
211 buf = pstrdup("character");
215 buf = pstrdup("real");
219 buf = pstrdup("double precision");
223 buf = pstrdup("smallint");
227 buf = pstrdup("integer");
231 buf = pstrdup("bigint");
236 buf = printTypmod("numeric", typemod, typeform->typmodout);
238 buf = pstrdup("numeric");
243 buf = printTypmod("interval", typemod, typeform->typmodout);
245 buf = pstrdup("interval");
250 buf = printTypmod("time", typemod, typeform->typmodout);
252 buf = pstrdup("time without time zone");
257 buf = printTypmod("time", typemod, typeform->typmodout);
259 buf = pstrdup("time with time zone");
264 buf = printTypmod("timestamp", typemod, typeform->typmodout);
266 buf = pstrdup("timestamp without time zone");
271 buf = printTypmod("timestamp", typemod, typeform->typmodout);
273 buf = pstrdup("timestamp with time zone");
278 buf = printTypmod("bit varying", typemod, typeform->typmodout);
280 buf = pstrdup("bit varying");
285 buf = printTypmod("character varying", typemod, typeform->typmodout);
287 buf = pstrdup("character varying");
294 * Default handling: report the name as it appears in the catalog.
295 * Here, we must qualify the name if it is not visible in the search
296 * path, and we must double-quote it if it's not a standard identifier
297 * or if it matches any keyword.
302 if (TypeIsVisible(type_oid))
305 nspname = get_namespace_name(typeform->typnamespace);
307 typname = NameStr(typeform->typname);
309 buf = quote_qualified_identifier(nspname, typname);
312 buf = printTypmod(buf, typemod, typeform->typmodout);
316 buf = psnprintf(strlen(buf) + 3, "%s[]", buf);
318 ReleaseSysCache(tuple);
325 * Add typmod decoration to the basic type name
328 printTypmod(const char *typname, int32 typmod, Oid typmodout)
332 /* Shouldn't be called if typmod is -1 */
335 if (typmodout == InvalidOid)
337 /* Default behavior: just print the integer typmod with parens */
338 res = psnprintf(strlen(typname) + MAX_INT32_LEN + 3, "%s(%d)",
339 typname, (int) typmod);
343 /* Use the type-specific typmodout procedure */
346 tmstr = DatumGetCString(OidFunctionCall1(typmodout,
347 Int32GetDatum(typmod)));
348 res = psnprintf(strlen(typname) + strlen(tmstr) + 1, "%s%s",
357 * type_maximum_size --- determine maximum width of a variable-width column
359 * If the max width is indeterminate, return -1. In particular, we return
360 * -1 for any type not known to this routine. We assume the caller has
361 * already determined that the type is a variable-width type, so it's not
362 * necessary to look up the type's pg_type tuple here.
364 * This may appear unrelated to format_type(), but in fact the two routines
365 * share knowledge of the encoding of typmod for different types, so it's
366 * convenient to keep them together. (XXX now that most of this knowledge
367 * has been pushed out of format_type into the typmodout functions, it's
368 * interesting to wonder if it's worth trying to factor this code too...)
371 type_maximum_size(Oid type_oid, int32 typemod)
380 /* typemod includes varlena header */
382 /* typemod is in characters not bytes */
383 return (typemod - VARHDRSZ) *
384 pg_encoding_max_length(GetDatabaseEncoding())
388 return numeric_maximum_size(typemod);
392 /* typemod is the (max) number of bits */
393 return (typemod + (BITS_PER_BYTE - 1)) / BITS_PER_BYTE
397 /* Unknown type, or unlimited-width type such as 'text' */
403 * oidvectortypes - converts a vector of type OIDs to "typname" list
406 oidvectortypes(PG_FUNCTION_ARGS)
408 oidvector *oidArray = (oidvector *) PG_GETARG_POINTER(0);
410 int numargs = oidArray->dim1;
415 total = 20 * numargs + 1;
416 result = palloc(total);
420 for (num = 0; num < numargs; num++)
422 char *typename = format_type_internal(oidArray->values[num], -1,
424 size_t slen = strlen(typename);
426 if (left < (slen + 2))
429 result = repalloc(result, total);
435 strcat(result, ", ");
438 strcat(result, typename);
442 PG_RETURN_TEXT_P(cstring_to_text(result));
446 /* snprintf into a palloc'd string */
448 psnprintf(size_t len, const char *fmt,...)
456 vsnprintf(buf, len, fmt, ap);