1 /*-------------------------------------------------------------------------
4 * Display type names "nicely".
7 * Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
11 * $PostgreSQL: pgsql/src/backend/utils/adt/format_type.c,v 1.48 2007/11/15 21:14:39 momjian Exp $
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
29 #define _textin(str) DirectFunctionCall1(textin, CStringGetDatum(str))
31 static char *format_type_internal(Oid type_oid, int32 typemod,
32 bool typemod_given, bool allow_invalid);
33 static char *printTypmod(const char *typname, int32 typmod, Oid typmodout);
35 psnprintf(size_t len, const char *fmt,...)
36 /* This lets gcc check the format string for consistency. */
37 __attribute__((format(printf, 2, 3)));
41 * SQL function: format_type(type_oid, typemod)
43 * `type_oid' is from pg_type.oid, `typemod' is from
44 * pg_attribute.atttypmod. This function will get the type name and
45 * format it and the modifier to canonical SQL format, if the type is
46 * a standard type. Otherwise you just get pg_type.typname back,
47 * double quoted if it contains funny characters or matches a keyword.
49 * If typemod is NULL then we are formatting a type name in a context where
50 * no typemod is available, eg a function argument or result type. This
51 * yields a slightly different result from specifying typemod = -1 in some
52 * cases. Given typemod = -1 we feel compelled to produce an output that
53 * the parser will interpret as having typemod -1, so that pg_dump will
54 * produce CREATE TABLE commands that recreate the original state. But
55 * given NULL typemod, we assume that the parser's interpretation of
56 * typemod doesn't matter, and so we are willing to output a slightly
57 * "prettier" representation of the same type. For example, type = bpchar
58 * and typemod = NULL gets you "character", whereas typemod = -1 gets you
59 * "bpchar" --- the former will be interpreted as character(1) by the
60 * parser, which does not yield typemod -1.
62 * XXX encoding a meaning in typemod = NULL is ugly; it'd have been
63 * cleaner to make two functions of one and two arguments respectively.
64 * Not worth changing it now, however.
67 format_type(PG_FUNCTION_ARGS)
73 /* Since this function is not strict, we must test for null args */
77 type_oid = PG_GETARG_OID(0);
80 result = format_type_internal(type_oid, -1, false, true);
83 typemod = PG_GETARG_INT32(1);
84 result = format_type_internal(type_oid, typemod, true, true);
87 PG_RETURN_DATUM(_textin(result));
91 * This version is for use within the backend in error messages, etc.
92 * One difference is that it will fail for an invalid type.
94 * The result is always a palloc'd string.
97 format_type_be(Oid type_oid)
99 return format_type_internal(type_oid, -1, false, false);
103 * This version allows a nondefault typemod to be specified.
106 format_type_with_typemod(Oid type_oid, int32 typemod)
108 return format_type_internal(type_oid, typemod, true, false);
114 format_type_internal(Oid type_oid, int32 typemod,
115 bool typemod_given, bool allow_invalid)
117 bool with_typemod = typemod_given && (typemod >= 0);
119 Form_pg_type typeform;
124 if (type_oid == InvalidOid && allow_invalid)
127 tuple = SearchSysCache(TYPEOID,
128 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 an array (and not a domain --- we don't want to show the
141 * substructure of a domain type). Fixed-length array types such as
142 * "name" shouldn't get deconstructed either. As of Postgres 8.1, rather
143 * than checking typlen we check the toast property, and don't deconstruct
144 * "plain storage" array types --- this is because we don't want to show
145 * oidvector as oid[].
147 array_base_type = typeform->typelem;
149 if (array_base_type != InvalidOid &&
150 typeform->typstorage != 'p' &&
151 typeform->typtype != TYPTYPE_DOMAIN)
153 /* Switch our attention to the array element type */
154 ReleaseSysCache(tuple);
155 tuple = SearchSysCache(TYPEOID,
156 ObjectIdGetDatum(array_base_type),
158 if (!HeapTupleIsValid(tuple))
161 return pstrdup("???[]");
163 elog(ERROR, "cache lookup failed for type %u", type_oid);
165 typeform = (Form_pg_type) GETSTRUCT(tuple);
166 type_oid = array_base_type;
173 * See if we want to special-case the output for certain built-in types.
174 * Note that these special cases should all correspond to special
175 * productions in gram.y, to ensure that the type name will be taken as a
176 * system type, not a user type of the same name.
178 * If we do not provide a special-case output here, the type name will be
179 * handled the same way as a user type name --- in particular, it will be
180 * double-quoted if it matches any lexer keyword. This behavior is
181 * essential for some cases, such as types "bit" and "char".
183 buf = NULL; /* flag for no special case */
189 buf = printTypmod("bit", typemod, typeform->typmodout);
190 else if (typemod_given)
193 * bit with typmod -1 is not the same as BIT, which means
194 * BIT(1) per SQL spec. Report it as the quoted typename so
195 * that parser will not assign a bogus typmod.
199 buf = pstrdup("bit");
203 buf = pstrdup("boolean");
208 buf = printTypmod("character", typemod, typeform->typmodout);
209 else if (typemod_given)
212 * bpchar with typmod -1 is not the same as CHARACTER, which
213 * means CHARACTER(1) per SQL spec. Report it as bpchar so
214 * that parser will not assign a bogus typmod.
218 buf = pstrdup("character");
222 buf = pstrdup("real");
226 buf = pstrdup("double precision");
230 buf = pstrdup("smallint");
234 buf = pstrdup("integer");
238 buf = pstrdup("bigint");
243 buf = printTypmod("numeric", typemod, typeform->typmodout);
245 buf = pstrdup("numeric");
250 buf = printTypmod("interval", typemod, typeform->typmodout);
252 buf = pstrdup("interval");
257 buf = printTypmod("time", typemod, typeform->typmodout);
259 buf = pstrdup("time without time zone");
264 buf = printTypmod("time", typemod, typeform->typmodout);
266 buf = pstrdup("time with time zone");
271 buf = printTypmod("timestamp", typemod, typeform->typmodout);
273 buf = pstrdup("timestamp without time zone");
278 buf = printTypmod("timestamp", typemod, typeform->typmodout);
280 buf = pstrdup("timestamp with time zone");
285 buf = printTypmod("bit varying", typemod, typeform->typmodout);
287 buf = pstrdup("bit varying");
292 buf = printTypmod("character varying", typemod, typeform->typmodout);
294 buf = pstrdup("character varying");
301 * Default handling: report the name as it appears in the catalog.
302 * Here, we must qualify the name if it is not visible in the search
303 * path, and we must double-quote it if it's not a standard identifier
304 * or if it matches any keyword.
309 if (TypeIsVisible(type_oid))
312 nspname = get_namespace_name(typeform->typnamespace);
314 typname = NameStr(typeform->typname);
316 buf = quote_qualified_identifier(nspname, typname);
319 buf = printTypmod(buf, typemod, typeform->typmodout);
323 buf = psnprintf(strlen(buf) + 3, "%s[]", buf);
325 ReleaseSysCache(tuple);
332 * Add typmod decoration to the basic type name
335 printTypmod(const char *typname, int32 typmod, Oid typmodout)
339 /* Shouldn't be called if typmod is -1 */
342 if (typmodout == InvalidOid)
344 /* Default behavior: just print the integer typmod with parens */
345 res = psnprintf(strlen(typname) + MAX_INT32_LEN + 3, "%s(%d)",
346 typname, (int) typmod);
350 /* Use the type-specific typmodout procedure */
353 tmstr = DatumGetCString(OidFunctionCall1(typmodout,
354 Int32GetDatum(typmod)));
355 res = psnprintf(strlen(typname) + strlen(tmstr) + 1, "%s%s",
364 * type_maximum_size --- determine maximum width of a variable-width column
366 * If the max width is indeterminate, return -1. In particular, we return
367 * -1 for any type not known to this routine. We assume the caller has
368 * already determined that the type is a variable-width type, so it's not
369 * necessary to look up the type's pg_type tuple here.
371 * This may appear unrelated to format_type(), but in fact the two routines
372 * share knowledge of the encoding of typmod for different types, so it's
373 * convenient to keep them together. (XXX now that most of this knowledge
374 * has been pushed out of format_type into the typmodout functions, it's
375 * interesting to wonder if it's worth trying to factor this code too...)
378 type_maximum_size(Oid type_oid, int32 typemod)
387 /* typemod includes varlena header */
389 /* typemod is in characters not bytes */
390 return (typemod - VARHDRSZ) *
391 pg_encoding_max_length(GetDatabaseEncoding())
395 /* precision (ie, max # of digits) is in upper bits of typmod */
396 if (typemod > VARHDRSZ)
398 int precision = ((typemod - VARHDRSZ) >> 16) & 0xffff;
400 /* Numeric stores 2 decimal digits/byte, plus header */
401 return (precision + 1) / 2 + NUMERIC_HDRSZ;
407 /* typemod is the (max) number of bits */
408 return (typemod + (BITS_PER_BYTE - 1)) / BITS_PER_BYTE
412 /* Unknown type, or unlimited-width type such as 'text' */
418 * oidvectortypes - converts a vector of type OIDs to "typname" list
421 oidvectortypes(PG_FUNCTION_ARGS)
423 oidvector *oidArray = (oidvector *) PG_GETARG_POINTER(0);
425 int numargs = oidArray->dim1;
430 total = 20 * numargs + 1;
431 result = palloc(total);
435 for (num = 0; num < numargs; num++)
437 char *typename = format_type_internal(oidArray->values[num], -1,
439 size_t slen = strlen(typename);
441 if (left < (slen + 2))
444 result = repalloc(result, total);
450 strcat(result, ", ");
453 strcat(result, typename);
457 PG_RETURN_DATUM(_textin(result));
461 /* snprintf into a palloc'd string */
463 psnprintf(size_t len, const char *fmt,...)
471 vsnprintf(buf, len, fmt, ap);