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-2020, 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(PGNSP);
45 
46  /* type owner */
47  Oid typowner BKI_DEFAULT(PGUID);
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(pg_class);
102 
103  /*
104  * If typelem is not 0 then it identifies another row in pg_type. The
105  * current type can then be subscripted like an array yielding values of
106  * type typelem. A non-zero typelem does not guarantee this type to be a
107  * "real" array type; some ordinary fixed-length types can also be
108  * subscripted (e.g., name, point). Variable-length types can *not* be
109  * turned into pseudo-arrays like that. Hence, the way to determine
110  * whether a type is a "true" array type is if:
111  *
112  * typelem != 0 and typlen == -1.
113  */
114  Oid typelem BKI_DEFAULT(0) BKI_LOOKUP(pg_type);
115 
116  /*
117  * If there is a "true" array type having this type as element type,
118  * typarray links to it. Zero if no associated "true" array type.
119  */
120  Oid typarray BKI_DEFAULT(0) BKI_ARRAY_DEFAULT(0) BKI_LOOKUP(pg_type);
121 
122  /*
123  * I/O conversion procedures for the datatype.
124  */
125 
126  /* text format (required) */
127  regproc typinput BKI_ARRAY_DEFAULT(array_in) BKI_LOOKUP(pg_proc);
128  regproc typoutput BKI_ARRAY_DEFAULT(array_out) BKI_LOOKUP(pg_proc);
129 
130  /* binary format (optional) */
131  regproc typreceive BKI_ARRAY_DEFAULT(array_recv) BKI_LOOKUP(pg_proc);
133 
134  /*
135  * I/O functions for optional type modifiers.
136  */
137  regproc typmodin BKI_DEFAULT(-) BKI_LOOKUP(pg_proc);
138  regproc typmodout BKI_DEFAULT(-) BKI_LOOKUP(pg_proc);
139 
140  /*
141  * Custom ANALYZE procedure for the datatype (0 selects the default).
142  */
144 
145  /* ----------------
146  * typalign is the alignment required when storing a value of this
147  * type. It applies to storage on disk as well as most
148  * representations of the value inside Postgres. When multiple values
149  * are stored consecutively, such as in the representation of a
150  * complete row on disk, padding is inserted before a datum of this
151  * type so that it begins on the specified boundary. The alignment
152  * reference is the beginning of the first datum in the sequence.
153  *
154  * 'c' = CHAR alignment, ie no alignment needed.
155  * 's' = SHORT alignment (2 bytes on most machines).
156  * 'i' = INT alignment (4 bytes on most machines).
157  * 'd' = DOUBLE alignment (8 bytes on many machines, but by no means all).
158  * (Use the TYPALIGN macros below for these.)
159  *
160  * See include/access/tupmacs.h for the macros that compute these
161  * alignment requirements. Note also that we allow the nominal alignment
162  * to be violated when storing "packed" varlenas; the TOAST mechanism
163  * takes care of hiding that from most code.
164  *
165  * NOTE: for types used in system tables, it is critical that the
166  * size and alignment defined in pg_type agree with the way that the
167  * compiler will lay out the field in a struct representing a table row.
168  * ----------------
169  */
170  char typalign;
171 
172  /* ----------------
173  * typstorage tells if the type is prepared for toasting and what
174  * the default strategy for attributes of this type should be.
175  *
176  * 'p' PLAIN type not prepared for toasting
177  * 'e' EXTERNAL external storage possible, don't try to compress
178  * 'x' EXTENDED try to compress and store external if required
179  * 'm' MAIN like 'x' but try to keep in main tuple
180  * (Use the TYPSTORAGE macros below for these.)
181  *
182  * Note that 'm' fields can also be moved out to secondary storage,
183  * but only as a last resort ('e' and 'x' fields are moved first).
184  * ----------------
185  */
186  char typstorage BKI_DEFAULT(p) BKI_ARRAY_DEFAULT(x);
187 
188  /*
189  * This flag represents a "NOT NULL" constraint against this datatype.
190  *
191  * If true, the attnotnull column for a corresponding table column using
192  * this datatype will always enforce the NOT NULL constraint.
193  *
194  * Used primarily for domain types.
195  */
196  bool typnotnull BKI_DEFAULT(f);
197 
198  /*
199  * Domains use typbasetype to show the base (or domain) type that the
200  * domain is based on. Zero if the type is not a domain.
201  */
202  Oid typbasetype BKI_DEFAULT(0);
203 
204  /*
205  * Domains use typtypmod to record the typmod to be applied to their base
206  * type (-1 if base type does not use a typmod). -1 if this type is not a
207  * domain.
208  */
209  int32 typtypmod BKI_DEFAULT(-1);
210 
211  /*
212  * typndims is the declared number of dimensions for an array domain type
213  * (i.e., typbasetype is an array type). Otherwise zero.
214  */
215  int32 typndims BKI_DEFAULT(0);
216 
217  /*
218  * Collation: 0 if type cannot use collations, nonzero (typically
219  * DEFAULT_COLLATION_OID) for collatable base types, possibly some other
220  * OID for domains over collatable types
221  */
222  Oid typcollation BKI_DEFAULT(0) BKI_LOOKUP(pg_collation);
223 
224 #ifdef CATALOG_VARLEN /* variable-length fields start here */
225 
226  /*
227  * If typdefaultbin is not NULL, it is the nodeToString representation of
228  * a default expression for the type. Currently this is only used for
229  * domains.
230  */
231  pg_node_tree typdefaultbin BKI_DEFAULT(_null_) BKI_ARRAY_DEFAULT(_null_);
232 
233  /*
234  * typdefault is NULL if the type has no associated default value. If
235  * typdefaultbin is not NULL, typdefault must contain a human-readable
236  * version of the default expression represented by typdefaultbin. If
237  * typdefaultbin is NULL and typdefault is not, then typdefault is the
238  * external representation of the type's default value, which may be fed
239  * to the type's input converter to produce a constant.
240  */
241  text typdefault BKI_DEFAULT(_null_) BKI_ARRAY_DEFAULT(_null_);
242 
243  /*
244  * Access permissions
245  */
246  aclitem typacl[1] BKI_DEFAULT(_null_);
247 #endif
249 
250 /* ----------------
251  * Form_pg_type corresponds to a pointer to a row with
252  * the format of pg_type relation.
253  * ----------------
254  */
256 
257 #ifdef EXPOSE_TO_CLIENT_CODE
258 
259 /*
260  * macros for values of poor-mans-enumerated-type columns
261  */
262 #define TYPTYPE_BASE 'b' /* base type (ordinary scalar type) */
263 #define TYPTYPE_COMPOSITE 'c' /* composite (e.g., table's rowtype) */
264 #define TYPTYPE_DOMAIN 'd' /* domain over another type */
265 #define TYPTYPE_ENUM 'e' /* enumerated type */
266 #define TYPTYPE_PSEUDO 'p' /* pseudo-type */
267 #define TYPTYPE_RANGE 'r' /* range type */
268 
269 #define TYPCATEGORY_INVALID '\0' /* not an allowed category */
270 #define TYPCATEGORY_ARRAY 'A'
271 #define TYPCATEGORY_BOOLEAN 'B'
272 #define TYPCATEGORY_COMPOSITE 'C'
273 #define TYPCATEGORY_DATETIME 'D'
274 #define TYPCATEGORY_ENUM 'E'
275 #define TYPCATEGORY_GEOMETRIC 'G'
276 #define TYPCATEGORY_NETWORK 'I' /* think INET */
277 #define TYPCATEGORY_NUMERIC 'N'
278 #define TYPCATEGORY_PSEUDOTYPE 'P'
279 #define TYPCATEGORY_RANGE 'R'
280 #define TYPCATEGORY_STRING 'S'
281 #define TYPCATEGORY_TIMESPAN 'T'
282 #define TYPCATEGORY_USER 'U'
283 #define TYPCATEGORY_BITSTRING 'V' /* er ... "varbit"? */
284 #define TYPCATEGORY_UNKNOWN 'X'
285 
286 #define TYPALIGN_CHAR 'c' /* char alignment (i.e. unaligned) */
287 #define TYPALIGN_SHORT 's' /* short alignment (typically 2 bytes) */
288 #define TYPALIGN_INT 'i' /* int alignment (typically 4 bytes) */
289 #define TYPALIGN_DOUBLE 'd' /* double alignment (often 8 bytes) */
290 
291 #define TYPSTORAGE_PLAIN 'p' /* type not prepared for toasting */
292 #define TYPSTORAGE_EXTERNAL 'e' /* toastable, don't try to compress */
293 #define TYPSTORAGE_EXTENDED 'x' /* fully toastable */
294 #define TYPSTORAGE_MAIN 'm' /* like 'x' but try to store inline */
295 
296 /* Is a type OID a polymorphic pseudotype? (Beware of multiple evaluation) */
297 #define IsPolymorphicType(typid) \
298  (IsPolymorphicTypeFamily1(typid) || \
299  IsPolymorphicTypeFamily2(typid))
300 
301 /* Code not part of polymorphic type resolution should not use these macros: */
302 #define IsPolymorphicTypeFamily1(typid) \
303  ((typid) == ANYELEMENTOID || \
304  (typid) == ANYARRAYOID || \
305  (typid) == ANYNONARRAYOID || \
306  (typid) == ANYENUMOID || \
307  (typid) == ANYRANGEOID)
308 
309 #define IsPolymorphicTypeFamily2(typid) \
310  ((typid) == ANYCOMPATIBLEOID || \
311  (typid) == ANYCOMPATIBLEARRAYOID || \
312  (typid) == ANYCOMPATIBLENONARRAYOID || \
313  (typid) == ANYCOMPATIBLERANGEOID)
314 
315 #endif /* EXPOSE_TO_CLIENT_CODE */
316 
317 
318 extern ObjectAddress TypeShellMake(const char *typeName,
319  Oid typeNamespace,
320  Oid ownerId);
321 
322 extern ObjectAddress TypeCreate(Oid newTypeOid,
323  const char *typeName,
324  Oid typeNamespace,
325  Oid relationOid,
326  char relationKind,
327  Oid ownerId,
328  int16 internalSize,
329  char typeType,
330  char typeCategory,
331  bool typePreferred,
332  char typDelim,
333  Oid inputProcedure,
334  Oid outputProcedure,
335  Oid receiveProcedure,
336  Oid sendProcedure,
337  Oid typmodinProcedure,
338  Oid typmodoutProcedure,
339  Oid analyzeProcedure,
340  Oid elementType,
341  bool isImplicitArray,
342  Oid arrayType,
343  Oid baseType,
344  const char *defaultTypeValue,
345  char *defaultTypeBin,
346  bool passedByValue,
347  char alignment,
348  char storage,
349  int32 typeMod,
350  int32 typNDims,
351  bool typeNotNull,
352  Oid typeCollation);
353 
354 extern void GenerateTypeDependencies(HeapTuple typeTuple,
355  Relation typeCatalog,
356  Node *defaultExpr,
357  void *typacl,
358  char relationKind, /* only for relation
359  * rowtypes */
360  bool isImplicitArray,
361  bool isDependentType,
362  bool rebuild);
363 
364 extern void RenameTypeInternal(Oid typeOid, const char *newTypeName,
365  Oid typeNamespace);
366 
367 extern char *makeArrayTypeName(const char *typeName, Oid typeNamespace);
368 
369 extern bool moveArrayTypeName(Oid typeOid, const char *typeName,
370  Oid typeNamespace);
371 
372 #endif /* PG_TYPE_H */
#define BKI_BOOTSTRAP
Definition: genbki.h:26
signed short int16
Definition: c.h:361
Datum array_send(PG_FUNCTION_ARGS)
Definition: arrayfuncs.c:1569
Oid typnamespace BKI_DEFAULT(PGNSP)
Definition: nodes.h:529
bool moveArrayTypeName(Oid typeOid, const char *typeName, Oid typeNamespace)
Definition: pg_type.c:856
Datum array_recv(PG_FUNCTION_ARGS)
Definition: arrayfuncs.c:1268
int16 typlen BKI_ARRAY_DEFAULT(-1)
unsigned int Oid
Definition: postgres_ext.h:31
Datum array_out(PG_FUNCTION_ARGS)
Definition: arrayfuncs.c:1013
signed int int32
Definition: c.h:362
FormData_pg_type
Definition: pg_type.h:248
void RenameTypeInternal(Oid typeOid, const char *newTypeName, Oid typeNamespace)
Definition: pg_type.c:720
char typalign
Definition: pg_type.h:170
Definition: c.h:616
char * makeArrayTypeName(const char *typeName, Oid typeNamespace)
Definition: pg_type.c:795
#define BKI_LOOKUP(catalog)
Definition: genbki.h:42
NameData typname
Definition: pg_type.h:41
Datum array_typanalyze(PG_FUNCTION_ARGS)
#define BKI_ROWTYPE_OID(oid, oidmacro)
Definition: genbki.h:28
void GenerateTypeDependencies(HeapTuple typeTuple, Relation typeCatalog, Node *defaultExpr, void *typacl, char relationKind, bool isImplicitArray, bool isDependentType, bool rebuild)
Definition: pg_type.c:542
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 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:192
TypeRelation_Rowtype_Id BKI_SCHEMA_MACRO
Definition: pg_type.h:37
ObjectAddress TypeShellMake(const char *typeName, Oid typeNamespace, Oid ownerId)
Definition: pg_type.c:56
FormData_pg_type * Form_pg_type
Definition: pg_type.h:255
Oid regproc
Definition: c.h:517
Definition: c.h:562
Datum array_in(PG_FUNCTION_ARGS)
Definition: arrayfuncs.c:173
CATALOG(pg_type, 1247, TypeRelationId) BKI_BOOTSTRAP BKI_ROWTYPE_OID(71