PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
format_type.c File Reference
#include "postgres.h"
#include <ctype.h>
#include "access/htup_details.h"
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "utils/builtins.h"
#include "utils/lsyscache.h"
#include "utils/numeric.h"
#include "utils/syscache.h"
#include "mb/pg_wchar.h"
Include dependency graph for format_type.c:

Go to the source code of this file.

Macros

#define MAX_INT32_LEN   11
 

Functions

static char * format_type_internal (Oid type_oid, int32 typemod, bool typemod_given, bool allow_invalid, bool force_qualify)
 
static char * printTypmod (const char *typname, int32 typmod, Oid typmodout)
 
Datum format_type (PG_FUNCTION_ARGS)
 
char * format_type_be (Oid type_oid)
 
char * format_type_be_qualified (Oid type_oid)
 
char * format_type_with_typemod (Oid type_oid, int32 typemod)
 
char * format_type_with_typemod_qualified (Oid type_oid, int32 typemod)
 
int32 type_maximum_size (Oid type_oid, int32 typemod)
 
Datum oidvectortypes (PG_FUNCTION_ARGS)
 

Macro Definition Documentation

#define MAX_INT32_LEN   11

Definition at line 29 of file format_type.c.

Function Documentation

Datum format_type ( PG_FUNCTION_ARGS  )

Definition at line 64 of file format_type.c.

References cstring_to_text(), format_type_internal(), PG_ARGISNULL, PG_GETARG_INT32, PG_GETARG_OID, PG_RETURN_NULL, and PG_RETURN_TEXT_P.

65 {
66  Oid type_oid;
67  int32 typemod;
68  char *result;
69 
70  /* Since this function is not strict, we must test for null args */
71  if (PG_ARGISNULL(0))
73 
74  type_oid = PG_GETARG_OID(0);
75 
76  if (PG_ARGISNULL(1))
77  result = format_type_internal(type_oid, -1, false, true, false);
78  else
79  {
80  typemod = PG_GETARG_INT32(1);
81  result = format_type_internal(type_oid, typemod, true, true, false);
82  }
83 
85 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:226
unsigned int Oid
Definition: postgres_ext.h:31
signed int int32
Definition: c.h:256
#define PG_GETARG_OID(n)
Definition: fmgr.h:232
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:322
text * cstring_to_text(const char *s)
Definition: varlena.c:151
#define PG_ARGISNULL(n)
Definition: fmgr.h:166
static char * format_type_internal(Oid type_oid, int32 typemod, bool typemod_given, bool allow_invalid, bool force_qualify)
Definition: format_type.c:132
#define PG_RETURN_NULL()
Definition: fmgr.h:297
char* format_type_be ( Oid  type_oid)

Definition at line 94 of file format_type.c.

References format_type_internal().

Referenced by aclcheck_error_type(), addFamilyMember(), addRangeTableEntryForFunction(), AggregateCreate(), AlterTypeNamespace(), AlterTypeNamespace_oid(), AlterTypeNamespaceInternal(), AlterTypeOwner(), appendTypeNameToBuffer(), array_cat(), array_cmp(), array_contain_compare(), array_eq(), array_position_common(), array_positions(), array_recv(), array_replace_internal(), array_send(), ATAddForeignKeyConstraint(), ATExecAlterColumnType(), ATExecSetStorage(), ATPrepAlterColumnType(), brinvalidate(), btvalidate(), build_column_default(), build_subplan(), check_object_ownership(), check_of_type(), check_safe_enum_use(), check_sql_fn_retval(), CheckAttributeType(), checkDomainOwner(), checkEnumOwner(), checkRuleResultList(), coerce_record_to_complex(), coerce_to_boolean(), coerce_to_common_type(), coerce_to_specific_type_typmod(), coerce_type(), compare_values_of_enum(), compile_plperl_function(), compile_pltcl_function(), ComputeIndexAttrs(), ComputePartitionAttrs(), convert_tuples_by_name_map(), cookDefault(), create_ctas_nodata(), CreateCast(), CreateFunction(), CreateTransform(), DefineAggregate(), DefineDomain(), DefineRange(), DefineType(), do_compile(), domain_check_input(), domain_state_setup(), dropOperators(), dropProcedures(), enforce_generic_type_consistency(), enum_first(), enum_in(), enum_last(), enum_recv(), EvaluateParams(), exec_stmt_foreach_a(), ExecCheckPlanOutput(), ExecEvalArray(), ExecEvalCoerceToDomain(), ExecEvalFieldSelect(), ExecEvalParamExtern(), ExecEvalScalarVar(), ExecEvalWholeRowVar(), ExecInitExpr(), exprType(), fetch_cursor_param_value(), findRangeSubOpclass(), findTypeOutputFunction(), fmgr_sql_validator(), format_operator_internal(), format_procedure_internal(), funcname_signature_string(), get_cast_oid(), get_domain_constraint_oid(), get_range_io_data(), get_rels_with_domain(), get_sort_group_operators(), get_transform_oid(), get_ts_parser_func(), get_ts_template_func(), GetColumnDefCollation(), GetDefaultOpClass(), getObjectDescription(), getTypeBinaryInputInfo(), getTypeBinaryOutputInfo(), getTypeInputInfo(), getTypeOutputInfo(), hash_array(), hash_range(), hashvalidate(), init_params(), init_sql_fcache(), initArrayResultArr(), initGinState(), internal_get_result_type(), intorel_startup(), load_enum_cache_data(), lookup_rowtype_tupdesc_internal(), LookupTypeName(), make_row_comparison_op(), make_scalar_array_op(), op_signature_string(), ParseFuncOrColumn(), pg_collation_for(), plperl_array_to_datum(), plperl_sv_to_datum(), plperl_validator(), plpgsql_build_variable(), plpgsql_validator(), PLy_procedure_create(), prepare_sql_fn_parse_info(), print_function_arguments(), print_function_rettype(), print_function_trftypes(), ProcedureCreate(), processIndirection(), record_cmp(), record_eq(), record_image_cmp(), record_image_eq(), regtypeout(), RenameConstraintById(), RenameType(), resolve_generic_type(), ResolveOpClass(), ri_HashCompareOp(), select_common_type(), slot_store_error_callback(), spgvalidate(), storeOperators(), storeProcedures(), transformAggregateCall(), transformArrayExpr(), transformArraySubscripts(), transformArrayType(), transformAssignedExpr(), transformAssignmentIndirection(), transformAssignmentSubscripts(), transformCollateClause(), transformColumnType(), transformPartitionBound(), transformTypeCast(), transformXmlSerialize(), tuple_to_stringinfo(), tupledesc_match(), unknown_attribute(), validateConnectbyTupleDesc(), variable_coerce_param_hook(), and width_bucket_array().

95 {
96  return format_type_internal(type_oid, -1, false, false, false);
97 }
static char * format_type_internal(Oid type_oid, int32 typemod, bool typemod_given, bool allow_invalid, bool force_qualify)
Definition: format_type.c:132
char* format_type_be_qualified ( Oid  type_oid)

Definition at line 104 of file format_type.c.

References format_type_internal().

Referenced by format_operator_internal(), format_operator_parts(), format_procedure_internal(), format_procedure_parts(), and getObjectIdentityParts().

105 {
106  return format_type_internal(type_oid, -1, false, false, true);
107 }
static char * format_type_internal(Oid type_oid, int32 typemod, bool typemod_given, bool allow_invalid, bool force_qualify)
Definition: format_type.c:132
static char * format_type_internal ( Oid  type_oid,
int32  typemod,
bool  typemod_given,
bool  allow_invalid,
bool  force_qualify 
)
static

Definition at line 132 of file format_type.c.

References BITOID, BOOLOID, BPCHAROID, buf, elog, ERROR, FLOAT4OID, FLOAT8OID, get_namespace_name_or_temp(), GETSTRUCT, HeapTupleIsValid, INT2OID, INT4OID, INT8OID, INTERVALOID, InvalidOid, NameStr, NULL, NUMERICOID, ObjectIdGetDatum, printTypmod(), psprintf(), pstrdup(), quote_qualified_identifier(), ReleaseSysCache(), SearchSysCache1, TIMEOID, TIMESTAMPOID, TIMESTAMPTZOID, TIMETZOID, TypeIsVisible(), TYPEOID, VARBITOID, and VARCHAROID.

Referenced by format_type(), format_type_be(), format_type_be_qualified(), format_type_with_typemod(), format_type_with_typemod_qualified(), and oidvectortypes().

135 {
136  bool with_typemod = typemod_given && (typemod >= 0);
137  HeapTuple tuple;
138  Form_pg_type typeform;
139  Oid array_base_type;
140  bool is_array;
141  char *buf;
142 
143  if (type_oid == InvalidOid && allow_invalid)
144  return pstrdup("-");
145 
146  tuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(type_oid));
147  if (!HeapTupleIsValid(tuple))
148  {
149  if (allow_invalid)
150  return pstrdup("???");
151  else
152  elog(ERROR, "cache lookup failed for type %u", type_oid);
153  }
154  typeform = (Form_pg_type) GETSTRUCT(tuple);
155 
156  /*
157  * Check if it's a regular (variable length) array type. Fixed-length
158  * array types such as "name" shouldn't get deconstructed. As of Postgres
159  * 8.1, rather than checking typlen we check the toast property, and don't
160  * deconstruct "plain storage" array types --- this is because we don't
161  * want to show oidvector as oid[].
162  */
163  array_base_type = typeform->typelem;
164 
165  if (array_base_type != InvalidOid &&
166  typeform->typstorage != 'p')
167  {
168  /* Switch our attention to the array element type */
169  ReleaseSysCache(tuple);
170  tuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(array_base_type));
171  if (!HeapTupleIsValid(tuple))
172  {
173  if (allow_invalid)
174  return pstrdup("???[]");
175  else
176  elog(ERROR, "cache lookup failed for type %u", type_oid);
177  }
178  typeform = (Form_pg_type) GETSTRUCT(tuple);
179  type_oid = array_base_type;
180  is_array = true;
181  }
182  else
183  is_array = false;
184 
185  /*
186  * See if we want to special-case the output for certain built-in types.
187  * Note that these special cases should all correspond to special
188  * productions in gram.y, to ensure that the type name will be taken as a
189  * system type, not a user type of the same name.
190  *
191  * If we do not provide a special-case output here, the type name will be
192  * handled the same way as a user type name --- in particular, it will be
193  * double-quoted if it matches any lexer keyword. This behavior is
194  * essential for some cases, such as types "bit" and "char".
195  */
196  buf = NULL; /* flag for no special case */
197 
198  switch (type_oid)
199  {
200  case BITOID:
201  if (with_typemod)
202  buf = printTypmod("bit", typemod, typeform->typmodout);
203  else if (typemod_given)
204  {
205  /*
206  * bit with typmod -1 is not the same as BIT, which means
207  * BIT(1) per SQL spec. Report it as the quoted typename so
208  * that parser will not assign a bogus typmod.
209  */
210  }
211  else
212  buf = pstrdup("bit");
213  break;
214 
215  case BOOLOID:
216  buf = pstrdup("boolean");
217  break;
218 
219  case BPCHAROID:
220  if (with_typemod)
221  buf = printTypmod("character", typemod, typeform->typmodout);
222  else if (typemod_given)
223  {
224  /*
225  * bpchar with typmod -1 is not the same as CHARACTER, which
226  * means CHARACTER(1) per SQL spec. Report it as bpchar so
227  * that parser will not assign a bogus typmod.
228  */
229  }
230  else
231  buf = pstrdup("character");
232  break;
233 
234  case FLOAT4OID:
235  buf = pstrdup("real");
236  break;
237 
238  case FLOAT8OID:
239  buf = pstrdup("double precision");
240  break;
241 
242  case INT2OID:
243  buf = pstrdup("smallint");
244  break;
245 
246  case INT4OID:
247  buf = pstrdup("integer");
248  break;
249 
250  case INT8OID:
251  buf = pstrdup("bigint");
252  break;
253 
254  case NUMERICOID:
255  if (with_typemod)
256  buf = printTypmod("numeric", typemod, typeform->typmodout);
257  else
258  buf = pstrdup("numeric");
259  break;
260 
261  case INTERVALOID:
262  if (with_typemod)
263  buf = printTypmod("interval", typemod, typeform->typmodout);
264  else
265  buf = pstrdup("interval");
266  break;
267 
268  case TIMEOID:
269  if (with_typemod)
270  buf = printTypmod("time", typemod, typeform->typmodout);
271  else
272  buf = pstrdup("time without time zone");
273  break;
274 
275  case TIMETZOID:
276  if (with_typemod)
277  buf = printTypmod("time", typemod, typeform->typmodout);
278  else
279  buf = pstrdup("time with time zone");
280  break;
281 
282  case TIMESTAMPOID:
283  if (with_typemod)
284  buf = printTypmod("timestamp", typemod, typeform->typmodout);
285  else
286  buf = pstrdup("timestamp without time zone");
287  break;
288 
289  case TIMESTAMPTZOID:
290  if (with_typemod)
291  buf = printTypmod("timestamp", typemod, typeform->typmodout);
292  else
293  buf = pstrdup("timestamp with time zone");
294  break;
295 
296  case VARBITOID:
297  if (with_typemod)
298  buf = printTypmod("bit varying", typemod, typeform->typmodout);
299  else
300  buf = pstrdup("bit varying");
301  break;
302 
303  case VARCHAROID:
304  if (with_typemod)
305  buf = printTypmod("character varying", typemod, typeform->typmodout);
306  else
307  buf = pstrdup("character varying");
308  break;
309  }
310 
311  if (buf == NULL)
312  {
313  /*
314  * Default handling: report the name as it appears in the catalog.
315  * Here, we must qualify the name if it is not visible in the search
316  * path, and we must double-quote it if it's not a standard identifier
317  * or if it matches any keyword.
318  */
319  char *nspname;
320  char *typname;
321 
322  if (!force_qualify && TypeIsVisible(type_oid))
323  nspname = NULL;
324  else
325  nspname = get_namespace_name_or_temp(typeform->typnamespace);
326 
327  typname = NameStr(typeform->typname);
328 
329  buf = quote_qualified_identifier(nspname, typname);
330 
331  if (with_typemod)
332  buf = printTypmod(buf, typemod, typeform->typmodout);
333  }
334 
335  if (is_array)
336  buf = psprintf("%s[]", buf);
337 
338  ReleaseSysCache(tuple);
339 
340  return buf;
341 }
#define TIMESTAMPTZOID
Definition: pg_type.h:517
#define TIMEOID
Definition: pg_type.h:506
#define BPCHAROID
Definition: pg_type.h:496
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
#define NUMERICOID
Definition: pg_type.h:546
char * pstrdup(const char *in)
Definition: mcxt.c:1077
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
#define INT4OID
Definition: pg_type.h:316
#define VARBITOID
Definition: pg_type.h:538
FormData_pg_type * Form_pg_type
Definition: pg_type.h:233
unsigned int Oid
Definition: postgres_ext.h:31
#define SearchSysCache1(cacheId, key1)
Definition: syscache.h:149
#define TIMESTAMPOID
Definition: pg_type.h:511
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
#define INT2OID
Definition: pg_type.h:308
static char * buf
Definition: pg_test_fsync.c:65
#define INTERVALOID
Definition: pg_type.h:521
char * quote_qualified_identifier(const char *qualifier, const char *ident)
Definition: ruleutils.c:10200
#define VARCHAROID
Definition: pg_type.h:499
#define FLOAT4OID
Definition: pg_type.h:408
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1081
#define InvalidOid
Definition: postgres_ext.h:36
#define INT8OID
Definition: pg_type.h:304
#define BITOID
Definition: pg_type.h:534
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define NULL
Definition: c.h:229
bool TypeIsVisible(Oid typid)
Definition: namespace.c:776
#define TIMETZOID
Definition: pg_type.h:528
#define FLOAT8OID
Definition: pg_type.h:411
#define BOOLOID
Definition: pg_type.h:288
static char * printTypmod(const char *typname, int32 typmod, Oid typmodout)
Definition: format_type.c:348
char * get_namespace_name_or_temp(Oid nspid)
Definition: lsyscache.c:3030
#define NameStr(name)
Definition: c.h:499
#define elog
Definition: elog.h:219
char* format_type_with_typemod ( Oid  type_oid,
int32  typemod 
)

Definition at line 113 of file format_type.c.

References format_type_internal().

Referenced by analyzeCTE(), ATPrepAlterColumnType(), checkRuleResultList(), checkViewTupleDesc(), compatConnectbyTupleDescs(), convert_tuples_by_position(), deparse_type_name(), get_coercion_expr(), get_const_expr(), get_from_clause_coldeflist(), get_rule_expr(), get_tablefunc(), get_variable(), and MergeAttributes().

114 {
115  return format_type_internal(type_oid, typemod, true, false, false);
116 }
static char * format_type_internal(Oid type_oid, int32 typemod, bool typemod_given, bool allow_invalid, bool force_qualify)
Definition: format_type.c:132
char* format_type_with_typemod_qualified ( Oid  type_oid,
int32  typemod 
)

Definition at line 123 of file format_type.c.

References format_type_internal().

Referenced by deparse_type_name().

124 {
125  return format_type_internal(type_oid, typemod, true, false, true);
126 }
static char * format_type_internal(Oid type_oid, int32 typemod, bool typemod_given, bool allow_invalid, bool force_qualify)
Definition: format_type.c:132
Datum oidvectortypes ( PG_FUNCTION_ARGS  )

Definition at line 424 of file format_type.c.

References cstring_to_text(), oidvector::dim1, format_type_internal(), palloc(), PG_GETARG_POINTER, PG_RETURN_TEXT_P, repalloc(), and oidvector::values.

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_internal(oidArray->values[num], -1,
441  false, true, false);
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 }
Definition: c.h:478
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:233
Oid values[FLEXIBLE_ARRAY_MEMBER]
Definition: c.h:486
int dim1
Definition: c.h:484
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:322
text * cstring_to_text(const char *s)
Definition: varlena.c:151
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:963
void * palloc(Size size)
Definition: mcxt.c:849
static char * format_type_internal(Oid type_oid, int32 typemod, bool typemod_given, bool allow_invalid, bool force_qualify)
Definition: format_type.c:132
static char * printTypmod ( const char *  typname,
int32  typmod,
Oid  typmodout 
)
static

Definition at line 348 of file format_type.c.

References Assert, DatumGetCString, Int32GetDatum, InvalidOid, OidFunctionCall1, and psprintf().

Referenced by format_type_internal().

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 }
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
#define DatumGetCString(X)
Definition: postgres.h:572
#define OidFunctionCall1(functionId, arg1)
Definition: fmgr.h:614
#define InvalidOid
Definition: postgres_ext.h:36
#define Assert(condition)
Definition: c.h:675
#define Int32GetDatum(X)
Definition: postgres.h:485
int32 type_maximum_size ( Oid  type_oid,
int32  typemod 
)

Definition at line 389 of file format_type.c.

References BITOID, BITS_PER_BYTE, BPCHAROID, GetDatabaseEncoding(), numeric_maximum_size(), NUMERICOID, pg_encoding_max_length(), VARBITOID, VARCHAROID, and VARHDRSZ.

Referenced by get_typavgwidth(), and needs_toast_table().

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 }
#define BPCHAROID
Definition: pg_type.h:496
#define BITS_PER_BYTE
#define NUMERICOID
Definition: pg_type.h:546
#define VARHDRSZ
Definition: c.h:445
#define VARBITOID
Definition: pg_type.h:538
signed int int32
Definition: c.h:256
int pg_encoding_max_length(int encoding)
Definition: wchar.c:1820
#define VARCHAROID
Definition: pg_type.h:499
int GetDatabaseEncoding(void)
Definition: mbutils.c:1015
int32 numeric_maximum_size(int32 typmod)
Definition: numeric.c:680
#define BITOID
Definition: pg_type.h:534