PostgreSQL Source Code  git master
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 * printTypmod (const char *typname, int32 typmod, Oid typmodout)
 
Datum format_type (PG_FUNCTION_ARGS)
 
char * format_type_extended (Oid type_oid, int32 typemod, bits16 flags)
 
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)
 
int32 type_maximum_size (Oid type_oid, int32 typemod)
 
Datum oidvectortypes (PG_FUNCTION_ARGS)
 

Macro Definition Documentation

◆ MAX_INT32_LEN

#define MAX_INT32_LEN   11

Definition at line 29 of file format_type.c.

Function Documentation

◆ format_type()

Datum format_type ( PG_FUNCTION_ARGS  )

Definition at line 61 of file format_type.c.

References cstring_to_text(), FORMAT_TYPE_ALLOW_INVALID, format_type_extended(), FORMAT_TYPE_TYPEMOD_GIVEN, PG_ARGISNULL, PG_GETARG_INT32, PG_GETARG_OID, PG_RETURN_NULL, and PG_RETURN_TEXT_P.

62 {
63  Oid type_oid;
64  int32 typemod;
65  char *result;
67 
68  /* Since this function is not strict, we must test for null args */
69  if (PG_ARGISNULL(0))
71 
72  type_oid = PG_GETARG_OID(0);
73 
74  if (PG_ARGISNULL(1))
75  typemod = -1;
76  else
77  {
78  typemod = PG_GETARG_INT32(1);
80  }
81 
82  result = format_type_extended(type_oid, typemod, flags);
83 
85 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:239
uint16 bits16
Definition: c.h:333
#define FORMAT_TYPE_TYPEMOD_GIVEN
Definition: builtins.h:121
unsigned int Oid
Definition: postgres_ext.h:31
signed int int32
Definition: c.h:313
#define PG_GETARG_OID(n)
Definition: fmgr.h:245
#define FORMAT_TYPE_ALLOW_INVALID
Definition: builtins.h:122
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:336
text * cstring_to_text(const char *s)
Definition: varlena.c:149
#define PG_ARGISNULL(n)
Definition: fmgr.h:179
char * format_type_extended(Oid type_oid, int32 typemod, bits16 flags)
Definition: format_type.c:110
#define PG_RETURN_NULL()
Definition: fmgr.h:310

◆ format_type_be()

char* format_type_be ( Oid  type_oid)

Definition at line 328 of file format_type.c.

References format_type_extended().

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(), assign_record_type_identifier(), ATAddForeignKeyConstraint(), ATExecAlterColumnType(), ATExecSetStorage(), ATPrepAlterColumnType(), brinvalidate(), btvalidate(), build_column_default(), build_pertrans_for_aggref(), build_subplan(), check_object_ownership(), check_of_type(), check_sql_fn_retval(), CheckAttributeType(), checkDomainOwner(), checkEnumOwner(), checkRuleResultList(), CheckVarSlotCompatibility(), 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(), CreateStatistics(), 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(), ExecEvalArrayExpr(), ExecEvalConstraintCheck(), ExecEvalConstraintNotNull(), ExecEvalFieldSelect(), ExecEvalParamExtern(), ExecEvalWholeRowVar(), ExecInitExprRec(), 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_expr_result_tupdesc(), 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_array_extended(), hash_range(), hash_range_extended(), hashvalidate(), init_params(), init_sql_fcache(), initArrayResultArr(), initGinState(), internal_get_result_type(), intorel_startup(), load_enum_cache_data(), logicalrep_typmap_gettypname(), lookup_rowtype_tupdesc_domain(), lookup_rowtype_tupdesc_internal(), LookupTypeName(), make_expanded_record_from_tupdesc(), make_expanded_record_from_typeid(), 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_param_eval_generic(), plpgsql_param_eval_generic_ro(), plpgsql_param_eval_recfield(), 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(), RelationBuildPartitionKey(), RenameConstraintById(), RenameType(), resolve_generic_type(), ResolveOpClass(), ri_HashCompareOp(), satisfies_hash_partition(), select_common_type(), slot_store_error_callback(), spgvalidate(), storeOperators(), storeProcedures(), transformAggregateCall(), transformArrayExpr(), transformArraySubscripts(), transformArrayType(), transformAssignedExpr(), transformAssignmentIndirection(), transformAssignmentSubscripts(), transformCollateClause(), transformColumnType(), transformFrameOffset(), transformPartitionBoundValue(), transformTypeCast(), transformXmlSerialize(), tuple_equals_slot(), tuple_to_stringinfo(), tupledesc_match(), unknown_attribute(), validateConnectbyTupleDesc(), variable_coerce_param_hook(), and width_bucket_array().

329 {
330  return format_type_extended(type_oid, -1, 0);
331 }
char * format_type_extended(Oid type_oid, int32 typemod, bits16 flags)
Definition: format_type.c:110

◆ format_type_be_qualified()

char* format_type_be_qualified ( Oid  type_oid)

Definition at line 338 of file format_type.c.

References format_type_extended(), and FORMAT_TYPE_FORCE_QUALIFY.

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

339 {
340  return format_type_extended(type_oid, -1, FORMAT_TYPE_FORCE_QUALIFY);
341 }
#define FORMAT_TYPE_FORCE_QUALIFY
Definition: builtins.h:123
char * format_type_extended(Oid type_oid, int32 typemod, bits16 flags)
Definition: format_type.c:110

◆ format_type_extended()

char* format_type_extended ( Oid  type_oid,
int32  typemod,
bits16  flags 
)

Definition at line 110 of file format_type.c.

References buf, elog, ERROR, FORMAT_TYPE_ALLOW_INVALID, FORMAT_TYPE_FORCE_QUALIFY, FORMAT_TYPE_TYPEMOD_GIVEN, get_namespace_name_or_temp(), GETSTRUCT, HeapTupleIsValid, InvalidOid, NameStr, ObjectIdGetDatum, printTypmod(), psprintf(), pstrdup(), quote_qualified_identifier(), ReleaseSysCache(), SearchSysCache1(), TypeIsVisible(), and TYPEOID.

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

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

◆ format_type_with_typemod()

char* format_type_with_typemod ( Oid  type_oid,
int32  typemod 
)

◆ oidvectortypes()

Datum oidvectortypes ( PG_FUNCTION_ARGS  )

Definition at line 432 of file format_type.c.

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

433 {
434  oidvector *oidArray = (oidvector *) PG_GETARG_POINTER(0);
435  char *result;
436  int numargs = oidArray->dim1;
437  int num;
438  size_t total;
439  size_t left;
440 
441  total = 20 * numargs + 1;
442  result = palloc(total);
443  result[0] = '\0';
444  left = total - 1;
445 
446  for (num = 0; num < numargs; num++)
447  {
448  char *typename = format_type_extended(oidArray->values[num], -1,
450  size_t slen = strlen(typename);
451 
452  if (left < (slen + 2))
453  {
454  total += slen + 2;
455  result = repalloc(result, total);
456  left += slen + 2;
457  }
458 
459  if (num > 0)
460  {
461  strcat(result, ", ");
462  left -= 2;
463  }
464  strcat(result, typename);
465  left -= slen;
466  }
467 
469 }
Definition: c.h:555
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:246
Oid values[FLEXIBLE_ARRAY_MEMBER]
Definition: c.h:563
int dim1
Definition: c.h:561
#define FORMAT_TYPE_ALLOW_INVALID
Definition: builtins.h:122
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:336
text * cstring_to_text(const char *s)
Definition: varlena.c:149
char * format_type_extended(Oid type_oid, int32 typemod, bits16 flags)
Definition: format_type.c:110
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1044
void * palloc(Size size)
Definition: mcxt.c:924

◆ printTypmod()

static char * printTypmod ( const char *  typname,
int32  typmod,
Oid  typmodout 
)
static

Definition at line 356 of file format_type.c.

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

Referenced by format_type_extended().

357 {
358  char *res;
359 
360  /* Shouldn't be called if typmod is -1 */
361  Assert(typmod >= 0);
362 
363  if (typmodout == InvalidOid)
364  {
365  /* Default behavior: just print the integer typmod with parens */
366  res = psprintf("%s(%d)", typname, (int) typmod);
367  }
368  else
369  {
370  /* Use the type-specific typmodout procedure */
371  char *tmstr;
372 
373  tmstr = DatumGetCString(OidFunctionCall1(typmodout,
374  Int32GetDatum(typmod)));
375  res = psprintf("%s%s", typname, tmstr);
376  }
377 
378  return res;
379 }
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
#define DatumGetCString(X)
Definition: postgres.h:551
#define OidFunctionCall1(functionId, arg1)
Definition: fmgr.h:628
#define InvalidOid
Definition: postgres_ext.h:36
#define Assert(condition)
Definition: c.h:699
#define Int32GetDatum(X)
Definition: postgres.h:464

◆ type_maximum_size()

int32 type_maximum_size ( Oid  type_oid,
int32  typemod 
)

Definition at line 397 of file format_type.c.

References BITS_PER_BYTE, GetDatabaseEncoding(), numeric_maximum_size(), pg_encoding_max_length(), and VARHDRSZ.

Referenced by get_typavgwidth(), and needs_toast_table().

398 {
399  if (typemod < 0)
400  return -1;
401 
402  switch (type_oid)
403  {
404  case BPCHAROID:
405  case VARCHAROID:
406  /* typemod includes varlena header */
407 
408  /* typemod is in characters not bytes */
409  return (typemod - VARHDRSZ) *
411  + VARHDRSZ;
412 
413  case NUMERICOID:
414  return numeric_maximum_size(typemod);
415 
416  case VARBITOID:
417  case BITOID:
418  /* typemod is the (max) number of bits */
419  return (typemod + (BITS_PER_BYTE - 1)) / BITS_PER_BYTE
420  + 2 * sizeof(int32);
421  }
422 
423  /* Unknown type, or unlimited-width type such as 'text' */
424  return -1;
425 }
#define BITS_PER_BYTE
#define VARHDRSZ
Definition: c.h:522
signed int int32
Definition: c.h:313
int pg_encoding_max_length(int encoding)
Definition: wchar.c:1820
int GetDatabaseEncoding(void)
Definition: mbutils.c:1004
int32 numeric_maximum_size(int32 typmod)
Definition: numeric.c:691