1 /*-------------------------------------------------------------------------
4 * Display type names "nicely".
7 * Portions Copyright (c) 1996-2019, 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 static char *printTypmod(const char *typname, int32 typmod, Oid typmodout);
33 * SQL function: format_type(type_oid, typemod)
35 * `type_oid' is from pg_type.oid, `typemod' is from
36 * pg_attribute.atttypmod. This function will get the type name and
37 * format it and the modifier to canonical SQL format, if the type is
38 * a standard type. Otherwise you just get pg_type.typname back,
39 * double quoted if it contains funny characters or matches a keyword.
41 * If typemod is NULL then we are formatting a type name in a context where
42 * no typemod is available, eg a function argument or result type. This
43 * yields a slightly different result from specifying typemod = -1 in some
44 * cases. Given typemod = -1 we feel compelled to produce an output that
45 * the parser will interpret as having typemod -1, so that pg_dump will
46 * produce CREATE TABLE commands that recreate the original state. But
47 * given NULL typemod, we assume that the parser's interpretation of
48 * typemod doesn't matter, and so we are willing to output a slightly
49 * "prettier" representation of the same type. For example, type = bpchar
50 * and typemod = NULL gets you "character", whereas typemod = -1 gets you
51 * "bpchar" --- the former will be interpreted as character(1) by the
52 * parser, which does not yield typemod -1.
54 * XXX encoding a meaning in typemod = NULL is ugly; it'd have been
55 * cleaner to make two functions of one and two arguments respectively.
56 * Not worth changing it now, however.
59 format_type(PG_FUNCTION_ARGS)
64 bits16 flags = FORMAT_TYPE_ALLOW_INVALID;
66 /* Since this function is not strict, we must test for null args */
70 type_oid = PG_GETARG_OID(0);
76 typemod = PG_GETARG_INT32(1);
77 flags |= FORMAT_TYPE_TYPEMOD_GIVEN;
80 result = format_type_extended(type_oid, typemod, flags);
82 PG_RETURN_TEXT_P(cstring_to_text(result));
86 * format_type_extended
87 * Generate a possibly-qualified type name.
89 * The default behavior is to only qualify if the type is not in the search
90 * path, to ignore the given typmod, and to raise an error if a non-existent
93 * The following bits in 'flags' modify the behavior:
94 * - FORMAT_TYPE_TYPEMOD_GIVEN
95 * include the typmod in the output (typmod could still be -1 though)
96 * - FORMAT_TYPE_ALLOW_INVALID
97 * if the type OID is invalid or unknown, return ??? or such instead
99 * - FORMAT_TYPE_FORCE_QUALIFY
100 * always schema-qualify type names, regardless of search_path
102 * Note that TYPEMOD_GIVEN is not interchangeable with "typemod == -1";
103 * see the comments above for format_type().
105 * Returns a palloc'd string.
108 format_type_extended(Oid type_oid, int32 typemod, bits16 flags)
111 Form_pg_type typeform;
117 if (type_oid == InvalidOid && (flags & FORMAT_TYPE_ALLOW_INVALID) != 0)
120 tuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(type_oid));
121 if (!HeapTupleIsValid(tuple))
123 if ((flags & FORMAT_TYPE_ALLOW_INVALID) != 0)
124 return pstrdup("???");
126 elog(ERROR, "cache lookup failed for type %u", type_oid);
128 typeform = (Form_pg_type) GETSTRUCT(tuple);
131 * Check if it's a regular (variable length) array type. Fixed-length
132 * array types such as "name" shouldn't get deconstructed. As of Postgres
133 * 8.1, rather than checking typlen we check the toast property, and don't
134 * deconstruct "plain storage" array types --- this is because we don't
135 * want to show oidvector as oid[].
137 array_base_type = typeform->typelem;
139 if (array_base_type != InvalidOid && typeform->typstorage != 'p')
141 /* Switch our attention to the array element type */
142 ReleaseSysCache(tuple);
143 tuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(array_base_type));
144 if (!HeapTupleIsValid(tuple))
146 if ((flags & FORMAT_TYPE_ALLOW_INVALID) != 0)
147 return pstrdup("???[]");
149 elog(ERROR, "cache lookup failed for type %u", type_oid);
151 typeform = (Form_pg_type) GETSTRUCT(tuple);
152 type_oid = array_base_type;
158 with_typemod = (flags & FORMAT_TYPE_TYPEMOD_GIVEN) != 0 && (typemod >= 0);
161 * See if we want to special-case the output for certain built-in types.
162 * Note that these special cases should all correspond to special
163 * productions in gram.y, to ensure that the type name will be taken as a
164 * system type, not a user type of the same name.
166 * If we do not provide a special-case output here, the type name will be
167 * handled the same way as a user type name --- in particular, it will be
168 * double-quoted if it matches any lexer keyword. This behavior is
169 * essential for some cases, such as types "bit" and "char".
171 buf = NULL; /* flag for no special case */
177 buf = printTypmod("bit", typemod, typeform->typmodout);
178 else if ((flags & FORMAT_TYPE_TYPEMOD_GIVEN) != 0)
181 * bit with typmod -1 is not the same as BIT, which means
182 * BIT(1) per SQL spec. Report it as the quoted typename so
183 * that parser will not assign a bogus typmod.
187 buf = pstrdup("bit");
191 buf = pstrdup("boolean");
196 buf = printTypmod("character", typemod, typeform->typmodout);
197 else if ((flags & FORMAT_TYPE_TYPEMOD_GIVEN) != 0)
200 * bpchar with typmod -1 is not the same as CHARACTER, which
201 * means CHARACTER(1) per SQL spec. Report it as bpchar so
202 * that parser will not assign a bogus typmod.
206 buf = pstrdup("character");
210 buf = pstrdup("real");
214 buf = pstrdup("double precision");
218 buf = pstrdup("smallint");
222 buf = pstrdup("integer");
226 buf = pstrdup("bigint");
231 buf = printTypmod("numeric", typemod, typeform->typmodout);
233 buf = pstrdup("numeric");
238 buf = printTypmod("interval", typemod, typeform->typmodout);
240 buf = pstrdup("interval");
245 buf = printTypmod("time", typemod, typeform->typmodout);
247 buf = pstrdup("time without time zone");
252 buf = printTypmod("time", typemod, typeform->typmodout);
254 buf = pstrdup("time with time zone");
259 buf = printTypmod("timestamp", typemod, typeform->typmodout);
261 buf = pstrdup("timestamp without time zone");
266 buf = printTypmod("timestamp", typemod, typeform->typmodout);
268 buf = pstrdup("timestamp with time zone");
273 buf = printTypmod("bit varying", typemod, typeform->typmodout);
275 buf = pstrdup("bit varying");
280 buf = printTypmod("character varying", typemod, typeform->typmodout);
282 buf = pstrdup("character varying");
289 * Default handling: report the name as it appears in the catalog.
290 * Here, we must qualify the name if it is not visible in the search
291 * path or if caller requests it; and we must double-quote it if it's
292 * not a standard identifier or if it matches any keyword.
297 if ((flags & FORMAT_TYPE_FORCE_QUALIFY) == 0 &&
298 TypeIsVisible(type_oid))
301 nspname = get_namespace_name_or_temp(typeform->typnamespace);
303 typname = NameStr(typeform->typname);
305 buf = quote_qualified_identifier(nspname, typname);
308 buf = printTypmod(buf, typemod, typeform->typmodout);
312 buf = psprintf("%s[]", buf);
314 ReleaseSysCache(tuple);
320 * This version is for use within the backend in error messages, etc.
321 * One difference is that it will fail for an invalid type.
323 * The result is always a palloc'd string.
326 format_type_be(Oid type_oid)
328 return format_type_extended(type_oid, -1, 0);
332 * This version returns a name that is always qualified (unless it's one
333 * of the SQL-keyword type names, such as TIMESTAMP WITH TIME ZONE).
336 format_type_be_qualified(Oid type_oid)
338 return format_type_extended(type_oid, -1, FORMAT_TYPE_FORCE_QUALIFY);
342 * This version allows a nondefault typemod to be specified.
345 format_type_with_typemod(Oid type_oid, int32 typemod)
347 return format_type_extended(type_oid, typemod, FORMAT_TYPE_TYPEMOD_GIVEN);
351 * Add typmod decoration to the basic type name
354 printTypmod(const char *typname, int32 typmod, Oid typmodout)
358 /* Shouldn't be called if typmod is -1 */
361 if (typmodout == InvalidOid)
363 /* Default behavior: just print the integer typmod with parens */
364 res = psprintf("%s(%d)", typname, (int) typmod);
368 /* Use the type-specific typmodout procedure */
371 tmstr = DatumGetCString(OidFunctionCall1(typmodout,
372 Int32GetDatum(typmod)));
373 res = psprintf("%s%s", typname, tmstr);
381 * type_maximum_size --- determine maximum width of a variable-width column
383 * If the max width is indeterminate, return -1. In particular, we return
384 * -1 for any type not known to this routine. We assume the caller has
385 * already determined that the type is a variable-width type, so it's not
386 * necessary to look up the type's pg_type tuple here.
388 * This may appear unrelated to format_type(), but in fact the two routines
389 * share knowledge of the encoding of typmod for different types, so it's
390 * convenient to keep them together. (XXX now that most of this knowledge
391 * has been pushed out of format_type into the typmodout functions, it's
392 * interesting to wonder if it's worth trying to factor this code too...)
395 type_maximum_size(Oid type_oid, int32 typemod)
404 /* typemod includes varlena header */
406 /* typemod is in characters not bytes */
407 return (typemod - VARHDRSZ) *
408 pg_encoding_max_length(GetDatabaseEncoding())
412 return numeric_maximum_size(typemod);
416 /* typemod is the (max) number of bits */
417 return (typemod + (BITS_PER_BYTE - 1)) / BITS_PER_BYTE
421 /* Unknown type, or unlimited-width type such as 'text' */
427 * oidvectortypes - converts a vector of type OIDs to "typname" list
430 oidvectortypes(PG_FUNCTION_ARGS)
432 oidvector *oidArray = (oidvector *) PG_GETARG_POINTER(0);
434 int numargs = oidArray->dim1;
439 total = 20 * numargs + 1;
440 result = palloc(total);
444 for (num = 0; num < numargs; num++)
446 char *typename = format_type_extended(oidArray->values[num], -1,
447 FORMAT_TYPE_ALLOW_INVALID);
448 size_t slen = strlen(typename);
450 if (left < (slen + 2))
453 result = repalloc(result, total);
459 strcat(result, ", ");
462 strcat(result, typename);
466 PG_RETURN_TEXT_P(cstring_to_text(result));