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.

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)
 

Function Documentation

◆ format_type()

Datum format_type ( PG_FUNCTION_ARGS  )

Definition at line 59 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.

60 {
61  Oid type_oid;
62  int32 typemod;
63  char *result;
65 
66  /* Since this function is not strict, we must test for null args */
67  if (PG_ARGISNULL(0))
69 
70  type_oid = PG_GETARG_OID(0);
71 
72  if (PG_ARGISNULL(1))
73  typemod = -1;
74  else
75  {
76  typemod = PG_GETARG_INT32(1);
78  }
79 
80  result = format_type_extended(type_oid, typemod, flags);
81 
83 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
uint16 bits16
Definition: c.h:366
#define FORMAT_TYPE_TYPEMOD_GIVEN
Definition: builtins.h:109
unsigned int Oid
Definition: postgres_ext.h:31
signed int int32
Definition: c.h:346
#define PG_GETARG_OID(n)
Definition: fmgr.h:270
#define FORMAT_TYPE_ALLOW_INVALID
Definition: builtins.h:110
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:361
text * cstring_to_text(const char *s)
Definition: varlena.c:171
#define PG_ARGISNULL(n)
Definition: fmgr.h:204
char * format_type_extended(Oid type_oid, int32 typemod, bits16 flags)
Definition: format_type.c:108
#define PG_RETURN_NULL()
Definition: fmgr.h:335

◆ format_type_be()

char* format_type_be ( Oid  type_oid)

Definition at line 326 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_datatype(), build_pertrans_for_aggref(), build_subplan(), check_object_ownership(), check_of_type(), check_safe_enum_use(), 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(), LookupTypeNameExtended(), 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(), processIndirection(), record_cmp(), record_eq(), record_image_cmp(), record_image_eq(), regtypeout(), RelationBuildPartitionKey(), RenameConstraintById(), RenameType(), resolve_generic_type(), ResolveOpClass(), revalidate_rectypeid(), ri_HashCompareOp(), satisfies_hash_partition(), select_common_type(), slot_store_error_callback(), spgvalidate(), storeOperators(), storeProcedures(), transformAggregateCall(), transformArrayExpr(), transformAssignedExpr(), transformAssignmentIndirection(), transformAssignmentSubscripts(), transformCollateClause(), transformColumnType(), transformContainerSubscripts(), transformContainerType(), transformFrameOffset(), transformPartitionBoundValue(), transformTypeCast(), transformXmlSerialize(), tuple_to_stringinfo(), tupledesc_match(), tuples_equal(), unknown_attribute(), validateConnectbyTupleDesc(), variable_coerce_param_hook(), and width_bucket_array().

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

◆ format_type_be_qualified()

char* format_type_be_qualified ( Oid  type_oid)

Definition at line 336 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().

337 {
338  return format_type_extended(type_oid, -1, FORMAT_TYPE_FORCE_QUALIFY);
339 }
#define FORMAT_TYPE_FORCE_QUALIFY
Definition: builtins.h:111
char * format_type_extended(Oid type_oid, int32 typemod, bits16 flags)
Definition: format_type.c:108

◆ format_type_extended()

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

Definition at line 108 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(), TYPEOID, and typname.

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

109 {
110  HeapTuple tuple;
111  Form_pg_type typeform;
112  Oid array_base_type;
113  bool is_array;
114  char *buf;
115  bool with_typemod;
116 
117  if (type_oid == InvalidOid && (flags & FORMAT_TYPE_ALLOW_INVALID) != 0)
118  return pstrdup("-");
119 
120  tuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(type_oid));
121  if (!HeapTupleIsValid(tuple))
122  {
123  if ((flags & FORMAT_TYPE_ALLOW_INVALID) != 0)
124  return pstrdup("???");
125  else
126  elog(ERROR, "cache lookup failed for type %u", type_oid);
127  }
128  typeform = (Form_pg_type) GETSTRUCT(tuple);
129 
130  /*
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[].
136  */
137  array_base_type = typeform->typelem;
138 
139  if (array_base_type != InvalidOid && typeform->typstorage != 'p')
140  {
141  /* Switch our attention to the array element type */
142  ReleaseSysCache(tuple);
143  tuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(array_base_type));
144  if (!HeapTupleIsValid(tuple))
145  {
146  if ((flags & FORMAT_TYPE_ALLOW_INVALID) != 0)
147  return pstrdup("???[]");
148  else
149  elog(ERROR, "cache lookup failed for type %u", type_oid);
150  }
151  typeform = (Form_pg_type) GETSTRUCT(tuple);
152  type_oid = array_base_type;
153  is_array = true;
154  }
155  else
156  is_array = false;
157 
158  with_typemod = (flags & FORMAT_TYPE_TYPEMOD_GIVEN) != 0 && (typemod >= 0);
159 
160  /*
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.
165  *
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".
170  */
171  buf = NULL; /* flag for no special case */
172 
173  switch (type_oid)
174  {
175  case BITOID:
176  if (with_typemod)
177  buf = printTypmod("bit", typemod, typeform->typmodout);
178  else if ((flags & FORMAT_TYPE_TYPEMOD_GIVEN) != 0)
179  {
180  /*
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.
184  */
185  }
186  else
187  buf = pstrdup("bit");
188  break;
189 
190  case BOOLOID:
191  buf = pstrdup("boolean");
192  break;
193 
194  case BPCHAROID:
195  if (with_typemod)
196  buf = printTypmod("character", typemod, typeform->typmodout);
197  else if ((flags & FORMAT_TYPE_TYPEMOD_GIVEN) != 0)
198  {
199  /*
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.
203  */
204  }
205  else
206  buf = pstrdup("character");
207  break;
208 
209  case FLOAT4OID:
210  buf = pstrdup("real");
211  break;
212 
213  case FLOAT8OID:
214  buf = pstrdup("double precision");
215  break;
216 
217  case INT2OID:
218  buf = pstrdup("smallint");
219  break;
220 
221  case INT4OID:
222  buf = pstrdup("integer");
223  break;
224 
225  case INT8OID:
226  buf = pstrdup("bigint");
227  break;
228 
229  case NUMERICOID:
230  if (with_typemod)
231  buf = printTypmod("numeric", typemod, typeform->typmodout);
232  else
233  buf = pstrdup("numeric");
234  break;
235 
236  case INTERVALOID:
237  if (with_typemod)
238  buf = printTypmod("interval", typemod, typeform->typmodout);
239  else
240  buf = pstrdup("interval");
241  break;
242 
243  case TIMEOID:
244  if (with_typemod)
245  buf = printTypmod("time", typemod, typeform->typmodout);
246  else
247  buf = pstrdup("time without time zone");
248  break;
249 
250  case TIMETZOID:
251  if (with_typemod)
252  buf = printTypmod("time", typemod, typeform->typmodout);
253  else
254  buf = pstrdup("time with time zone");
255  break;
256 
257  case TIMESTAMPOID:
258  if (with_typemod)
259  buf = printTypmod("timestamp", typemod, typeform->typmodout);
260  else
261  buf = pstrdup("timestamp without time zone");
262  break;
263 
264  case TIMESTAMPTZOID:
265  if (with_typemod)
266  buf = printTypmod("timestamp", typemod, typeform->typmodout);
267  else
268  buf = pstrdup("timestamp with time zone");
269  break;
270 
271  case VARBITOID:
272  if (with_typemod)
273  buf = printTypmod("bit varying", typemod, typeform->typmodout);
274  else
275  buf = pstrdup("bit varying");
276  break;
277 
278  case VARCHAROID:
279  if (with_typemod)
280  buf = printTypmod("character varying", typemod, typeform->typmodout);
281  else
282  buf = pstrdup("character varying");
283  break;
284  }
285 
286  if (buf == NULL)
287  {
288  /*
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.
293  */
294  char *nspname;
295  char *typname;
296 
297  if ((flags & FORMAT_TYPE_FORCE_QUALIFY) == 0 &&
298  TypeIsVisible(type_oid))
299  nspname = NULL;
300  else
301  nspname = get_namespace_name_or_temp(typeform->typnamespace);
302 
303  typname = NameStr(typeform->typname);
304 
305  buf = quote_qualified_identifier(nspname, typname);
306 
307  if (with_typemod)
308  buf = printTypmod(buf, typemod, typeform->typmodout);
309  }
310 
311  if (is_array)
312  buf = psprintf("%s[]", buf);
313 
314  ReleaseSysCache(tuple);
315 
316  return buf;
317 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
char * pstrdup(const char *in)
Definition: mcxt.c:1161
#define FORMAT_TYPE_TYPEMOD_GIVEN
Definition: builtins.h:109
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
unsigned int Oid
Definition: postgres_ext.h:31
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
static char * buf
Definition: pg_test_fsync.c:68
#define FORMAT_TYPE_FORCE_QUALIFY
Definition: builtins.h:111
NameData typname
Definition: pg_type.h:42
char * quote_qualified_identifier(const char *qualifier, const char *ident)
Definition: ruleutils.c:10712
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1124
#define FORMAT_TYPE_ALLOW_INVALID
Definition: builtins.h:110
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1172
#define InvalidOid
Definition: postgres_ext.h:36
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
bool TypeIsVisible(Oid typid)
Definition: namespace.c:808
FormData_pg_type * Form_pg_type
Definition: pg_type.h:251
static char * printTypmod(const char *typname, int32 typmod, Oid typmodout)
Definition: format_type.c:354
char * get_namespace_name_or_temp(Oid nspid)
Definition: lsyscache.c:3118
#define elog(elevel,...)
Definition: elog.h:226
#define NameStr(name)
Definition: c.h:609

◆ format_type_with_typemod()

char* format_type_with_typemod ( Oid  type_oid,
int32  typemod 
)

◆ oidvectortypes()

Datum oidvectortypes ( PG_FUNCTION_ARGS  )

Definition at line 430 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.

431 {
432  oidvector *oidArray = (oidvector *) PG_GETARG_POINTER(0);
433  char *result;
434  int numargs = oidArray->dim1;
435  int num;
436  size_t total;
437  size_t left;
438 
439  total = 20 * numargs + 1;
440  result = palloc(total);
441  result[0] = '\0';
442  left = total - 1;
443 
444  for (num = 0; num < numargs; num++)
445  {
446  char *typename = format_type_extended(oidArray->values[num], -1,
448  size_t slen = strlen(typename);
449 
450  if (left < (slen + 2))
451  {
452  total += slen + 2;
453  result = repalloc(result, total);
454  left += slen + 2;
455  }
456 
457  if (num > 0)
458  {
459  strcat(result, ", ");
460  left -= 2;
461  }
462  strcat(result, typename);
463  left -= slen;
464  }
465 
467 }
Definition: c.h:588
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:271
Oid values[FLEXIBLE_ARRAY_MEMBER]
Definition: c.h:596
int dim1
Definition: c.h:594
#define FORMAT_TYPE_ALLOW_INVALID
Definition: builtins.h:110
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:361
text * cstring_to_text(const char *s)
Definition: varlena.c:171
char * format_type_extended(Oid type_oid, int32 typemod, bits16 flags)
Definition: format_type.c:108
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 354 of file format_type.c.

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

Referenced by format_type_extended().

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

◆ type_maximum_size()

int32 type_maximum_size ( Oid  type_oid,
int32  typemod 
)

Definition at line 395 of file format_type.c.

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

Referenced by get_typavgwidth(), and heapam_relation_needs_toast_table().

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