PostgreSQL Source Code  git master
format_type.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * format_type.c
4  * Display type names "nicely".
5  *
6  *
7  * Portions Copyright (c) 1996-2018, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  * IDENTIFICATION
11  * src/backend/utils/adt/format_type.c
12  *
13  *-------------------------------------------------------------------------
14  */
15 
16 #include "postgres.h"
17 
18 #include <ctype.h>
19 
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"
28 
29 #define MAX_INT32_LEN 11
30 
31 static char *printTypmod(const char *typname, int32 typmod, Oid typmodout);
32 
33 
34 /*
35  * SQL function: format_type(type_oid, typemod)
36  *
37  * `type_oid' is from pg_type.oid, `typemod' is from
38  * pg_attribute.atttypmod. This function will get the type name and
39  * format it and the modifier to canonical SQL format, if the type is
40  * a standard type. Otherwise you just get pg_type.typname back,
41  * double quoted if it contains funny characters or matches a keyword.
42  *
43  * If typemod is NULL then we are formatting a type name in a context where
44  * no typemod is available, eg a function argument or result type. This
45  * yields a slightly different result from specifying typemod = -1 in some
46  * cases. Given typemod = -1 we feel compelled to produce an output that
47  * the parser will interpret as having typemod -1, so that pg_dump will
48  * produce CREATE TABLE commands that recreate the original state. But
49  * given NULL typemod, we assume that the parser's interpretation of
50  * typemod doesn't matter, and so we are willing to output a slightly
51  * "prettier" representation of the same type. For example, type = bpchar
52  * and typemod = NULL gets you "character", whereas typemod = -1 gets you
53  * "bpchar" --- the former will be interpreted as character(1) by the
54  * parser, which does not yield typemod -1.
55  *
56  * XXX encoding a meaning in typemod = NULL is ugly; it'd have been
57  * cleaner to make two functions of one and two arguments respectively.
58  * Not worth changing it now, however.
59  */
60 Datum
62 {
63  Oid type_oid;
64  int32 typemod;
65  char *result;
66 
67  /* Since this function is not strict, we must test for null args */
68  if (PG_ARGISNULL(0))
70 
71  type_oid = PG_GETARG_OID(0);
72  typemod = PG_ARGISNULL(1) ? -1 : PG_GETARG_INT32(1);
73 
74  result = format_type_extended(type_oid, typemod,
77 
79 }
80 
81 /*
82  * format_type_extended
83  * Generate a possibly-qualified type name.
84  *
85  * The default is to only qualify if the type is not in the search path, to
86  * ignore the given typmod, and to raise an error if a non-existent type_oid is
87  * given.
88  *
89  * The following bits in 'flags' modify the behavior:
90  * - FORMAT_TYPE_TYPEMOD_GIVEN
91  * consider the given typmod in the output (may be -1 to request
92  * the default behavior)
93  *
94  * - FORMAT_TYPE_ALLOW_INVALID
95  * if the type OID is invalid or unknown, return ??? or such instead
96  * of failing
97  *
98  * - FORMAT_TYPE_FORCE_QUALIFY
99  * always schema-qualify type names, regardless of search_path
100  */
101 char *
102 format_type_extended(Oid type_oid, int32 typemod, bits16 flags)
103 {
104  HeapTuple tuple;
105  Form_pg_type typeform;
106  Oid array_base_type;
107  bool is_array;
108  char *buf;
109  bool with_typemod;
110 
111  if (type_oid == InvalidOid && (flags & FORMAT_TYPE_ALLOW_INVALID) != 0)
112  return pstrdup("-");
113 
114  tuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(type_oid));
115  if (!HeapTupleIsValid(tuple))
116  {
117  if ((flags & FORMAT_TYPE_ALLOW_INVALID) != 0)
118  return pstrdup("???");
119  else
120  elog(ERROR, "cache lookup failed for type %u", type_oid);
121  }
122  typeform = (Form_pg_type) GETSTRUCT(tuple);
123 
124  /*
125  * Check if it's a regular (variable length) array type. Fixed-length
126  * array types such as "name" shouldn't get deconstructed. As of Postgres
127  * 8.1, rather than checking typlen we check the toast property, and don't
128  * deconstruct "plain storage" array types --- this is because we don't
129  * want to show oidvector as oid[].
130  */
131  array_base_type = typeform->typelem;
132 
133  if (array_base_type != InvalidOid && typeform->typstorage != 'p')
134  {
135  /* Switch our attention to the array element type */
136  ReleaseSysCache(tuple);
137  tuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(array_base_type));
138  if (!HeapTupleIsValid(tuple))
139  {
140  if ((flags & FORMAT_TYPE_ALLOW_INVALID) != 0)
141  return pstrdup("???[]");
142  else
143  elog(ERROR, "cache lookup failed for type %u", type_oid);
144  }
145  typeform = (Form_pg_type) GETSTRUCT(tuple);
146  type_oid = array_base_type;
147  is_array = true;
148  }
149  else
150  is_array = false;
151 
152  with_typemod = (flags & FORMAT_TYPE_TYPEMOD_GIVEN) != 0 && (typemod >= 0);
153 
154  /*
155  * See if we want to special-case the output for certain built-in types.
156  * Note that these special cases should all correspond to special
157  * productions in gram.y, to ensure that the type name will be taken as a
158  * system type, not a user type of the same name.
159  *
160  * If we do not provide a special-case output here, the type name will be
161  * handled the same way as a user type name --- in particular, it will be
162  * double-quoted if it matches any lexer keyword. This behavior is
163  * essential for some cases, such as types "bit" and "char".
164  */
165  buf = NULL; /* flag for no special case */
166 
167  switch (type_oid)
168  {
169  case BITOID:
170  if (with_typemod)
171  buf = printTypmod("bit", typemod, typeform->typmodout);
172  else if ((flags & FORMAT_TYPE_TYPEMOD_GIVEN) != 0)
173  {
174  /*
175  * bit with typmod -1 is not the same as BIT, which means
176  * BIT(1) per SQL spec. Report it as the quoted typename so
177  * that parser will not assign a bogus typmod.
178  */
179  }
180  else
181  buf = pstrdup("bit");
182  break;
183 
184  case BOOLOID:
185  buf = pstrdup("boolean");
186  break;
187 
188  case BPCHAROID:
189  if (with_typemod)
190  buf = printTypmod("character", typemod, typeform->typmodout);
191  else if ((flags & FORMAT_TYPE_TYPEMOD_GIVEN) != 0)
192  {
193  /*
194  * bpchar with typmod -1 is not the same as CHARACTER, which
195  * means CHARACTER(1) per SQL spec. Report it as bpchar so
196  * that parser will not assign a bogus typmod.
197  */
198  }
199  else
200  buf = pstrdup("character");
201  break;
202 
203  case FLOAT4OID:
204  buf = pstrdup("real");
205  break;
206 
207  case FLOAT8OID:
208  buf = pstrdup("double precision");
209  break;
210 
211  case INT2OID:
212  buf = pstrdup("smallint");
213  break;
214 
215  case INT4OID:
216  buf = pstrdup("integer");
217  break;
218 
219  case INT8OID:
220  buf = pstrdup("bigint");
221  break;
222 
223  case NUMERICOID:
224  if (with_typemod)
225  buf = printTypmod("numeric", typemod, typeform->typmodout);
226  else
227  buf = pstrdup("numeric");
228  break;
229 
230  case INTERVALOID:
231  if (with_typemod)
232  buf = printTypmod("interval", typemod, typeform->typmodout);
233  else
234  buf = pstrdup("interval");
235  break;
236 
237  case TIMEOID:
238  if (with_typemod)
239  buf = printTypmod("time", typemod, typeform->typmodout);
240  else
241  buf = pstrdup("time without time zone");
242  break;
243 
244  case TIMETZOID:
245  if (with_typemod)
246  buf = printTypmod("time", typemod, typeform->typmodout);
247  else
248  buf = pstrdup("time with time zone");
249  break;
250 
251  case TIMESTAMPOID:
252  if (with_typemod)
253  buf = printTypmod("timestamp", typemod, typeform->typmodout);
254  else
255  buf = pstrdup("timestamp without time zone");
256  break;
257 
258  case TIMESTAMPTZOID:
259  if (with_typemod)
260  buf = printTypmod("timestamp", typemod, typeform->typmodout);
261  else
262  buf = pstrdup("timestamp with time zone");
263  break;
264 
265  case VARBITOID:
266  if (with_typemod)
267  buf = printTypmod("bit varying", typemod, typeform->typmodout);
268  else
269  buf = pstrdup("bit varying");
270  break;
271 
272  case VARCHAROID:
273  if (with_typemod)
274  buf = printTypmod("character varying", typemod, typeform->typmodout);
275  else
276  buf = pstrdup("character varying");
277  break;
278  }
279 
280  if (buf == NULL)
281  {
282  /*
283  * Default handling: report the name as it appears in the catalog.
284  * Here, we must qualify the name if it is not visible in the search
285  * path or if caller requests it; and we must double-quote it if it's
286  * not a standard identifier or if it matches any keyword.
287  */
288  char *nspname;
289  char *typname;
290 
291  if ((flags & FORMAT_TYPE_FORCE_QUALIFY) == 0 &&
292  TypeIsVisible(type_oid))
293  nspname = NULL;
294  else
295  nspname = get_namespace_name_or_temp(typeform->typnamespace);
296 
297  typname = NameStr(typeform->typname);
298 
299  buf = quote_qualified_identifier(nspname, typname);
300 
301  if (with_typemod)
302  buf = printTypmod(buf, typemod, typeform->typmodout);
303  }
304 
305  if (is_array)
306  buf = psprintf("%s[]", buf);
307 
308  ReleaseSysCache(tuple);
309 
310  return buf;
311 }
312 
313 /*
314  * This version is for use within the backend in error messages, etc.
315  * One difference is that it will fail for an invalid type.
316  *
317  * The result is always a palloc'd string.
318  */
319 char *
321 {
322  return format_type_extended(type_oid, -1, 0);
323 }
324 
325 /*
326  * This version returns a name that is always qualified (unless it's one
327  * of the SQL-keyword type names, such as TIMESTAMP WITH TIME ZONE).
328  */
329 char *
331 {
332  return format_type_extended(type_oid, -1, FORMAT_TYPE_FORCE_QUALIFY);
333 }
334 
335 /*
336  * This version allows a nondefault typemod to be specified.
337  */
338 char *
340 {
341  return format_type_extended(type_oid, typemod, FORMAT_TYPE_TYPEMOD_GIVEN);
342 }
343 
344 /*
345  * Add typmod decoration to the basic type name
346  */
347 static char *
348 printTypmod(const char *typname, int32 typmod, Oid typmodout)
349 {
350  char *res;
351 
352  /* Shouldn't be called if typmod is -1 */
353  Assert(typmod >= 0);
354 
355  if (typmodout == InvalidOid)
356  {
357  /* Default behavior: just print the integer typmod with parens */
358  res = psprintf("%s(%d)", typname, (int) typmod);
359  }
360  else
361  {
362  /* Use the type-specific typmodout procedure */
363  char *tmstr;
364 
365  tmstr = DatumGetCString(OidFunctionCall1(typmodout,
366  Int32GetDatum(typmod)));
367  res = psprintf("%s%s", typname, tmstr);
368  }
369 
370  return res;
371 }
372 
373 
374 /*
375  * type_maximum_size --- determine maximum width of a variable-width column
376  *
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.
381  *
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...)
387  */
388 int32
389 type_maximum_size(Oid type_oid, int32 typemod)
390 {
391  if (typemod < 0)
392  return -1;
393 
394  switch (type_oid)
395  {
396  case BPCHAROID:
397  case VARCHAROID:
398  /* typemod includes varlena header */
399 
400  /* typemod is in characters not bytes */
401  return (typemod - VARHDRSZ) *
403  + VARHDRSZ;
404 
405  case NUMERICOID:
406  return numeric_maximum_size(typemod);
407 
408  case VARBITOID:
409  case BITOID:
410  /* typemod is the (max) number of bits */
411  return (typemod + (BITS_PER_BYTE - 1)) / BITS_PER_BYTE
412  + 2 * sizeof(int32);
413  }
414 
415  /* Unknown type, or unlimited-width type such as 'text' */
416  return -1;
417 }
418 
419 
420 /*
421  * oidvectortypes - converts a vector of type OIDs to "typname" list
422  */
423 Datum
425 {
426  oidvector *oidArray = (oidvector *) PG_GETARG_POINTER(0);
427  char *result;
428  int numargs = oidArray->dim1;
429  int num;
430  size_t total;
431  size_t left;
432 
433  total = 20 * numargs + 1;
434  result = palloc(total);
435  result[0] = '\0';
436  left = total - 1;
437 
438  for (num = 0; num < numargs; num++)
439  {
440  char *typename = format_type_extended(oidArray->values[num], -1,
442  size_t slen = strlen(typename);
443 
444  if (left < (slen + 2))
445  {
446  total += slen + 2;
447  result = repalloc(result, total);
448  left += slen + 2;
449  }
450 
451  if (num > 0)
452  {
453  strcat(result, ", ");
454  left -= 2;
455  }
456  strcat(result, typename);
457  left -= slen;
458  }
459 
461 }
#define TIMESTAMPTZOID
Definition: pg_type.h:525
#define TIMEOID
Definition: pg_type.h:514
Definition: c.h:544
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
#define BPCHAROID
Definition: pg_type.h:504
#define GETSTRUCT(TUP)
Definition: htup_details.h:661
#define BITS_PER_BYTE
uint16 bits16
Definition: c.h:322
#define NUMERICOID
Definition: pg_type.h:554
#define VARHDRSZ
Definition: c.h:511
char * pstrdup(const char *in)
Definition: mcxt.c:1063
#define FORMAT_TYPE_TYPEMOD_GIVEN
Definition: builtins.h:117
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
#define INT4OID
Definition: pg_type.h:316
#define VARBITOID
Definition: pg_type.h:546
char * format_type_be(Oid type_oid)
Definition: format_type.c:320
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
FormData_pg_type * Form_pg_type
Definition: pg_type.h:233
unsigned int Oid
Definition: postgres_ext.h:31
signed int int32
Definition: c.h:302
#define TIMESTAMPOID
Definition: pg_type.h:519
#define ObjectIdGetDatum(X)
Definition: postgres.h:490
#define ERROR
Definition: elog.h:43
#define DatumGetCString(X)
Definition: postgres.h:549
int32 type_maximum_size(Oid type_oid, int32 typemod)
Definition: format_type.c:389
#define OidFunctionCall1(functionId, arg1)
Definition: fmgr.h:623
#define INT2OID
Definition: pg_type.h:308
int pg_encoding_max_length(int encoding)
Definition: wchar.c:1820
static char * buf
Definition: pg_test_fsync.c:67
#define PG_GETARG_OID(n)
Definition: fmgr.h:240
Oid values[FLEXIBLE_ARRAY_MEMBER]
Definition: c.h:552
int dim1
Definition: c.h:550
#define INTERVALOID
Definition: pg_type.h:529
#define FORMAT_TYPE_FORCE_QUALIFY
Definition: builtins.h:119
Datum oidvectortypes(PG_FUNCTION_ARGS)
Definition: format_type.c:424
char * quote_qualified_identifier(const char *qualifier, const char *ident)
Definition: ruleutils.c:10521
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1112
#define VARCHAROID
Definition: pg_type.h:507
#define FORMAT_TYPE_ALLOW_INVALID
Definition: builtins.h:118
char * format_type_with_typemod(Oid type_oid, int32 typemod)
Definition: format_type.c:339
#define FLOAT4OID
Definition: pg_type.h:416
uintptr_t Datum
Definition: postgres.h:365
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1160
int GetDatabaseEncoding(void)
Definition: mbutils.c:1004
#define InvalidOid
Definition: postgres_ext.h:36
int32 numeric_maximum_size(int32 typmod)
Definition: numeric.c:691
#define INT8OID
Definition: pg_type.h:304
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:331
#define BITOID
Definition: pg_type.h:542
text * cstring_to_text(const char *s)
Definition: varlena.c:150
#define PG_ARGISNULL(n)
Definition: fmgr.h:174
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define Assert(condition)
Definition: c.h:688
bool TypeIsVisible(Oid typid)
Definition: namespace.c:777
char * format_type_be_qualified(Oid type_oid)
Definition: format_type.c:330
#define TIMETZOID
Definition: pg_type.h:536
#define FLOAT8OID
Definition: pg_type.h:419
char * format_type_extended(Oid type_oid, int32 typemod, bits16 flags)
Definition: format_type.c:102
#define BOOLOID
Definition: pg_type.h:288
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:949
static char * printTypmod(const char *typname, int32 typmod, Oid typmodout)
Definition: format_type.c:348
#define Int32GetDatum(X)
Definition: postgres.h:462
void * palloc(Size size)
Definition: mcxt.c:835
char * get_namespace_name_or_temp(Oid nspid)
Definition: lsyscache.c:3075
#define NameStr(name)
Definition: c.h:565
Datum format_type(PG_FUNCTION_ARGS)
Definition: format_type.c:61
#define PG_FUNCTION_ARGS
Definition: fmgr.h:158
#define elog
Definition: elog.h:219
#define PG_RETURN_NULL()
Definition: fmgr.h:305