PostgreSQL Source Code  git master
pg_type.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * pg_type.h
4  * definition of the "type" system catalog (pg_type)
5  *
6  *
7  * Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  * src/include/catalog/pg_type.h
11  *
12  * NOTES
13  * The Catalog.pm module reads this file and derives schema
14  * information.
15  *
16  *-------------------------------------------------------------------------
17  */
18 #ifndef PG_TYPE_H
19 #define PG_TYPE_H
20 
21 #include "catalog/genbki.h"
22 #include "catalog/objectaddress.h"
23 #include "catalog/pg_type_d.h"
24 #include "nodes/nodes.h"
25 
26 /* ----------------
27  * pg_type definition. cpp turns this into
28  * typedef struct FormData_pg_type
29  *
30  * Some of the values in a pg_type instance are copied into
31  * pg_attribute instances. Some parts of Postgres use the pg_type copy,
32  * while others use the pg_attribute copy, so they must match.
33  * See struct FormData_pg_attribute for details.
34  * ----------------
35  */
36 CATALOG(pg_type,1247,TypeRelationId) BKI_BOOTSTRAP BKI_ROWTYPE_OID(71,TypeRelation_Rowtype_Id) BKI_SCHEMA_MACRO
37 {
38  Oid oid; /* oid */
39 
40  /* type name */
42 
43  /* OID of namespace containing this type */
44  Oid typnamespace BKI_DEFAULT(pg_catalog) BKI_LOOKUP(pg_namespace);
45 
46  /* type owner */
47  Oid typowner BKI_DEFAULT(POSTGRES) BKI_LOOKUP(pg_authid);
48 
49  /*
50  * For a fixed-size type, typlen is the number of bytes we use to
51  * represent a value of this type, e.g. 4 for an int4. But for a
52  * variable-length type, typlen is negative. We use -1 to indicate a
53  * "varlena" type (one that has a length word), -2 to indicate a
54  * null-terminated C string.
55  */
56  int16 typlen BKI_ARRAY_DEFAULT(-1);
57 
58  /*
59  * typbyval determines whether internal Postgres routines pass a value of
60  * this type by value or by reference. typbyval had better be false if
61  * the length is not 1, 2, or 4 (or 8 on 8-byte-Datum machines).
62  * Variable-length types are always passed by reference. Note that
63  * typbyval can be false even if the length would allow pass-by-value; for
64  * example, type macaddr8 is pass-by-ref even when Datum is 8 bytes.
65  */
66  bool typbyval BKI_ARRAY_DEFAULT(f);
67 
68  /*
69  * typtype is 'b' for a base type, 'c' for a composite type (e.g., a
70  * table's rowtype), 'd' for a domain, 'e' for an enum type, 'p' for a
71  * pseudo-type, or 'r' for a range type. (Use the TYPTYPE macros below.)
72  *
73  * If typtype is 'c', typrelid is the OID of the class' entry in pg_class.
74  */
75  char typtype BKI_DEFAULT(b) BKI_ARRAY_DEFAULT(b);
76 
77  /*
78  * typcategory and typispreferred help the parser distinguish preferred
79  * and non-preferred coercions. The category can be any single ASCII
80  * character (but not \0). The categories used for built-in types are
81  * identified by the TYPCATEGORY macros below.
82  */
83 
84  /* arbitrary type classification */
85  char typcategory BKI_ARRAY_DEFAULT(A);
86 
87  /* is type "preferred" within its category? */
88  bool typispreferred BKI_DEFAULT(f) BKI_ARRAY_DEFAULT(f);
89 
90  /*
91  * If typisdefined is false, the entry is only a placeholder (forward
92  * reference). We know the type's name and owner, but not yet anything
93  * else about it.
94  */
95  bool typisdefined BKI_DEFAULT(t);
96 
97  /* delimiter for arrays of this type */
98  char typdelim BKI_DEFAULT(',');
99 
100  /* associated pg_class OID if a composite type, else 0 */
101  Oid typrelid BKI_DEFAULT(0) BKI_ARRAY_DEFAULT(0) BKI_LOOKUP_OPT(pg_class);
102 
103  /*
104  * Type-specific subscripting handler. If typsubscript is 0, it means
105  * that this type doesn't support subscripting. Note that various parts
106  * of the system deem types to be "true" array types only if their
107  * typsubscript is array_subscript_handler.
108  */
110 
111  /*
112  * If typelem is not 0 then it identifies another row in pg_type, defining
113  * the type yielded by subscripting. This should be 0 if typsubscript is
114  * 0. However, it can be 0 when typsubscript isn't 0, if the handler
115  * doesn't need typelem to determine the subscripting result type. Note
116  * that a typelem dependency is considered to imply physical containment
117  * of the element type in this type; so DDL changes on the element type
118  * might be restricted by the presence of this type.
119  */
120  Oid typelem BKI_DEFAULT(0) BKI_LOOKUP_OPT(pg_type);
121 
122  /*
123  * If there is a "true" array type having this type as element type,
124  * typarray links to it. Zero if no associated "true" array type.
125  */
126  Oid typarray BKI_DEFAULT(0) BKI_ARRAY_DEFAULT(0) BKI_LOOKUP_OPT(pg_type);
127 
128  /*
129  * I/O conversion procedures for the datatype.
130  */
131 
132  /* text format (required) */
133  regproc typinput BKI_ARRAY_DEFAULT(array_in) BKI_LOOKUP(pg_proc);
134  regproc typoutput BKI_ARRAY_DEFAULT(array_out) BKI_LOOKUP(pg_proc);
135 
136  /* binary format (optional) */
137  regproc typreceive BKI_ARRAY_DEFAULT(array_recv) BKI_LOOKUP_OPT(pg_proc);
139 
140  /*
141  * I/O functions for optional type modifiers.
142  */
143  regproc typmodin BKI_DEFAULT(-) BKI_LOOKUP_OPT(pg_proc);
144  regproc typmodout BKI_DEFAULT(-) BKI_LOOKUP_OPT(pg_proc);
145 
146  /*
147  * Custom ANALYZE procedure for the datatype (0 selects the default).
148  */
150 
151  /* ----------------
152  * typalign is the alignment required when storing a value of this
153  * type. It applies to storage on disk as well as most
154  * representations of the value inside Postgres. When multiple values
155  * are stored consecutively, such as in the representation of a
156  * complete row on disk, padding is inserted before a datum of this
157  * type so that it begins on the specified boundary. The alignment
158  * reference is the beginning of the first datum in the sequence.
159  *
160  * 'c' = CHAR alignment, ie no alignment needed.
161  * 's' = SHORT alignment (2 bytes on most machines).
162  * 'i' = INT alignment (4 bytes on most machines).
163  * 'd' = DOUBLE alignment (8 bytes on many machines, but by no means all).
164  * (Use the TYPALIGN macros below for these.)
165  *
166  * See include/access/tupmacs.h for the macros that compute these
167  * alignment requirements. Note also that we allow the nominal alignment
168  * to be violated when storing "packed" varlenas; the TOAST mechanism
169  * takes care of hiding that from most code.
170  *
171  * NOTE: for types used in system tables, it is critical that the
172  * size and alignment defined in pg_type agree with the way that the
173  * compiler will lay out the field in a struct representing a table row.
174  * ----------------
175  */
176  char typalign;
177 
178  /* ----------------
179  * typstorage tells if the type is prepared for toasting and what
180  * the default strategy for attributes of this type should be.
181  *
182  * 'p' PLAIN type not prepared for toasting
183  * 'e' EXTERNAL external storage possible, don't try to compress
184  * 'x' EXTENDED try to compress and store external if required
185  * 'm' MAIN like 'x' but try to keep in main tuple
186  * (Use the TYPSTORAGE macros below for these.)
187  *
188  * Note that 'm' fields can also be moved out to secondary storage,
189  * but only as a last resort ('e' and 'x' fields are moved first).
190  * ----------------
191  */
192  char typstorage BKI_DEFAULT(p) BKI_ARRAY_DEFAULT(x);
193 
194  /*
195  * This flag represents a "NOT NULL" constraint against this datatype.
196  *
197  * If true, the attnotnull column for a corresponding table column using
198  * this datatype will always enforce the NOT NULL constraint.
199  *
200  * Used primarily for domain types.
201  */
202  bool typnotnull BKI_DEFAULT(f);
203 
204  /*
205  * Domains use typbasetype to show the base (or domain) type that the
206  * domain is based on. Zero if the type is not a domain.
207  */
208  Oid typbasetype BKI_DEFAULT(0) BKI_LOOKUP_OPT(pg_type);
209 
210  /*
211  * Domains use typtypmod to record the typmod to be applied to their base
212  * type (-1 if base type does not use a typmod). -1 if this type is not a
213  * domain.
214  */
215  int32 typtypmod BKI_DEFAULT(-1);
216 
217  /*
218  * typndims is the declared number of dimensions for an array domain type
219  * (i.e., typbasetype is an array type). Otherwise zero.
220  */
221  int32 typndims BKI_DEFAULT(0);
222 
223  /*
224  * Collation: 0 if type cannot use collations, nonzero (typically
225  * DEFAULT_COLLATION_OID) for collatable base types, possibly some other
226  * OID for domains over collatable types
227  */
228  Oid typcollation BKI_DEFAULT(0) BKI_LOOKUP_OPT(pg_collation);
229 
230 #ifdef CATALOG_VARLEN /* variable-length fields start here */
231 
232  /*
233  * If typdefaultbin is not NULL, it is the nodeToString representation of
234  * a default expression for the type. Currently this is only used for
235  * domains.
236  */
237  pg_node_tree typdefaultbin BKI_DEFAULT(_null_) BKI_ARRAY_DEFAULT(_null_);
238 
239  /*
240  * typdefault is NULL if the type has no associated default value. If
241  * typdefaultbin is not NULL, typdefault must contain a human-readable
242  * version of the default expression represented by typdefaultbin. If
243  * typdefaultbin is NULL and typdefault is not, then typdefault is the
244  * external representation of the type's default value, which may be fed
245  * to the type's input converter to produce a constant.
246  */
247  text typdefault BKI_DEFAULT(_null_) BKI_ARRAY_DEFAULT(_null_);
248 
249  /*
250  * Access permissions
251  */
252  aclitem typacl[1] BKI_DEFAULT(_null_);
253 #endif
255 
256 /* ----------------
257  * Form_pg_type corresponds to a pointer to a row with
258  * the format of pg_type relation.
259  * ----------------
260  */
262 
263 DECLARE_TOAST(pg_type, 4171, 4172);
264 
265 DECLARE_UNIQUE_INDEX_PKEY(pg_type_oid_index, 2703, TypeOidIndexId, on pg_type using btree(oid oid_ops));
266 DECLARE_UNIQUE_INDEX(pg_type_typname_nsp_index, 2704, TypeNameNspIndexId, on pg_type using btree(typname name_ops, typnamespace oid_ops));
267 
268 #ifdef EXPOSE_TO_CLIENT_CODE
269 
270 /*
271  * macros for values of poor-mans-enumerated-type columns
272  */
273 #define TYPTYPE_BASE 'b' /* base type (ordinary scalar type) */
274 #define TYPTYPE_COMPOSITE 'c' /* composite (e.g., table's rowtype) */
275 #define TYPTYPE_DOMAIN 'd' /* domain over another type */
276 #define TYPTYPE_ENUM 'e' /* enumerated type */
277 #define TYPTYPE_MULTIRANGE 'm' /* multirange type */
278 #define TYPTYPE_PSEUDO 'p' /* pseudo-type */
279 #define TYPTYPE_RANGE 'r' /* range type */
280 
281 #define TYPCATEGORY_INVALID '\0' /* not an allowed category */
282 #define TYPCATEGORY_ARRAY 'A'
283 #define TYPCATEGORY_BOOLEAN 'B'
284 #define TYPCATEGORY_COMPOSITE 'C'
285 #define TYPCATEGORY_DATETIME 'D'
286 #define TYPCATEGORY_ENUM 'E'
287 #define TYPCATEGORY_GEOMETRIC 'G'
288 #define TYPCATEGORY_NETWORK 'I' /* think INET */
289 #define TYPCATEGORY_NUMERIC 'N'
290 #define TYPCATEGORY_PSEUDOTYPE 'P'
291 #define TYPCATEGORY_RANGE 'R'
292 #define TYPCATEGORY_STRING 'S'
293 #define TYPCATEGORY_TIMESPAN 'T'
294 #define TYPCATEGORY_USER 'U'
295 #define TYPCATEGORY_BITSTRING 'V' /* er ... "varbit"? */
296 #define TYPCATEGORY_UNKNOWN 'X'
297 
298 #define TYPALIGN_CHAR 'c' /* char alignment (i.e. unaligned) */
299 #define TYPALIGN_SHORT 's' /* short alignment (typically 2 bytes) */
300 #define TYPALIGN_INT 'i' /* int alignment (typically 4 bytes) */
301 #define TYPALIGN_DOUBLE 'd' /* double alignment (often 8 bytes) */
302 
303 #define TYPSTORAGE_PLAIN 'p' /* type not prepared for toasting */
304 #define TYPSTORAGE_EXTERNAL 'e' /* toastable, don't try to compress */
305 #define TYPSTORAGE_EXTENDED 'x' /* fully toastable */
306 #define TYPSTORAGE_MAIN 'm' /* like 'x' but try to store inline */
307 
308 /* Is a type OID a polymorphic pseudotype? (Beware of multiple evaluation) */
309 #define IsPolymorphicType(typid) \
310  (IsPolymorphicTypeFamily1(typid) || \
311  IsPolymorphicTypeFamily2(typid))
312 
313 /* Code not part of polymorphic type resolution should not use these macros: */
314 #define IsPolymorphicTypeFamily1(typid) \
315  ((typid) == ANYELEMENTOID || \
316  (typid) == ANYARRAYOID || \
317  (typid) == ANYNONARRAYOID || \
318  (typid) == ANYENUMOID || \
319  (typid) == ANYRANGEOID || \
320  (typid) == ANYMULTIRANGEOID)
321 
322 #define IsPolymorphicTypeFamily2(typid) \
323  ((typid) == ANYCOMPATIBLEOID || \
324  (typid) == ANYCOMPATIBLEARRAYOID || \
325  (typid) == ANYCOMPATIBLENONARRAYOID || \
326  (typid) == ANYCOMPATIBLERANGEOID || \
327  (typid) == ANYCOMPATIBLEMULTIRANGEOID)
328 
329 /* Is this a "true" array type? (Requires fmgroids.h) */
330 #define IsTrueArrayType(typeForm) \
331  (OidIsValid((typeForm)->typelem) && \
332  (typeForm)->typsubscript == F_ARRAY_SUBSCRIPT_HANDLER)
333 
334 /*
335  * Backwards compatibility for ancient random spellings of pg_type OID macros.
336  * Don't use these names in new code.
337  */
338 #define CASHOID MONEYOID
339 #define LSNOID PG_LSNOID
340 
341 #endif /* EXPOSE_TO_CLIENT_CODE */
342 
343 
344 extern ObjectAddress TypeShellMake(const char *typeName,
345  Oid typeNamespace,
346  Oid ownerId);
347 
348 extern ObjectAddress TypeCreate(Oid newTypeOid,
349  const char *typeName,
350  Oid typeNamespace,
351  Oid relationOid,
352  char relationKind,
353  Oid ownerId,
354  int16 internalSize,
355  char typeType,
356  char typeCategory,
357  bool typePreferred,
358  char typDelim,
359  Oid inputProcedure,
360  Oid outputProcedure,
361  Oid receiveProcedure,
362  Oid sendProcedure,
363  Oid typmodinProcedure,
364  Oid typmodoutProcedure,
365  Oid analyzeProcedure,
366  Oid subscriptProcedure,
367  Oid elementType,
368  bool isImplicitArray,
369  Oid arrayType,
370  Oid baseType,
371  const char *defaultTypeValue,
372  char *defaultTypeBin,
373  bool passedByValue,
374  char alignment,
375  char storage,
376  int32 typeMod,
377  int32 typNDims,
378  bool typeNotNull,
379  Oid typeCollation);
380 
381 extern void GenerateTypeDependencies(HeapTuple typeTuple,
382  Relation typeCatalog,
383  Node *defaultExpr,
384  void *typacl,
385  char relationKind, /* only for relation
386  * rowtypes */
387  bool isImplicitArray,
388  bool isDependentType,
389  bool makeExtensionDep,
390  bool rebuild);
391 
392 extern void RenameTypeInternal(Oid typeOid, const char *newTypeName,
393  Oid typeNamespace);
394 
395 extern char *makeArrayTypeName(const char *typeName, Oid typeNamespace);
396 
397 extern bool moveArrayTypeName(Oid typeOid, const char *typeName,
398  Oid typeNamespace);
399 
400 extern char *makeMultirangeTypeName(const char *rangeTypeName,
401  Oid typeNamespace);
402 
403 #endif /* PG_TYPE_H */
#define BKI_BOOTSTRAP
Definition: genbki.h:26
signed short int16
Definition: c.h:428
Datum array_subscript_handler(PG_FUNCTION_ARGS)
Definition: arraysubs.c:539
Datum array_send(PG_FUNCTION_ARGS)
Definition: arrayfuncs.c:1559
ObjectAddress TypeCreate(Oid newTypeOid, const char *typeName, Oid typeNamespace, Oid relationOid, char relationKind, Oid ownerId, int16 internalSize, char typeType, char typeCategory, bool typePreferred, char typDelim, Oid inputProcedure, Oid outputProcedure, Oid receiveProcedure, Oid sendProcedure, Oid typmodinProcedure, Oid typmodoutProcedure, Oid analyzeProcedure, Oid subscriptProcedure, Oid elementType, bool isImplicitArray, Oid arrayType, Oid baseType, const char *defaultTypeValue, char *defaultTypeBin, bool passedByValue, char alignment, char storage, int32 typeMod, int32 typNDims, bool typeNotNull, Oid typeCollation)
Definition: pg_type.c:198
Definition: nodes.h:536
bool moveArrayTypeName(Oid typeOid, const char *typeName, Oid typeNamespace)
Definition: pg_type.c:856
Datum array_recv(PG_FUNCTION_ARGS)
Definition: arrayfuncs.c:1271
int16 typlen BKI_ARRAY_DEFAULT(-1)
unsigned int Oid
Definition: postgres_ext.h:31
Datum array_out(PG_FUNCTION_ARGS)
Definition: arrayfuncs.c:1016
signed int int32
Definition: c.h:429
FormData_pg_type
Definition: pg_type.h:254
void RenameTypeInternal(Oid typeOid, const char *newTypeName, Oid typeNamespace)
Definition: pg_type.c:741
DECLARE_UNIQUE_INDEX_PKEY(pg_type_oid_index, 2703, TypeOidIndexId, on pg_type using btree(oid oid_ops))
char typalign
Definition: pg_type.h:176
DECLARE_UNIQUE_INDEX(pg_type_typname_nsp_index, 2704, TypeNameNspIndexId, on pg_type using btree(typname name_ops, typnamespace oid_ops))
Definition: c.h:675
char * makeArrayTypeName(const char *typeName, Oid typeNamespace)
Definition: pg_type.c:816
void GenerateTypeDependencies(HeapTuple typeTuple, Relation typeCatalog, Node *defaultExpr, void *typacl, char relationKind, bool isImplicitArray, bool isDependentType, bool makeExtensionDep, bool rebuild)
Definition: pg_type.c:555
Oid typnamespace BKI_DEFAULT(pg_catalog) BKI_LOOKUP(pg_namespace)
#define BKI_LOOKUP_OPT(catalog)
Definition: genbki.h:47
#define BKI_LOOKUP(catalog)
Definition: genbki.h:46
NameData typname
Definition: pg_type.h:41
Datum array_typanalyze(PG_FUNCTION_ARGS)
#define BKI_ROWTYPE_OID(oid, oidmacro)
Definition: genbki.h:28
DECLARE_TOAST(pg_type, 4171, 4172)
TypeRelation_Rowtype_Id BKI_SCHEMA_MACRO
Definition: pg_type.h:37
ObjectAddress TypeShellMake(const char *typeName, Oid typeNamespace, Oid ownerId)
Definition: pg_type.c:60
char * makeMultirangeTypeName(const char *rangeTypeName, Oid typeNamespace)
Definition: pg_type.c:901
FormData_pg_type * Form_pg_type
Definition: pg_type.h:261
Oid regproc
Definition: c.h:584
Definition: c.h:621
Datum array_in(PG_FUNCTION_ARGS)
Definition: arrayfuncs.c:174
CATALOG(pg_type, 1247, TypeRelationId) BKI_BOOTSTRAP BKI_ROWTYPE_OID(71