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-2019, 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  *
159  * See include/access/tupmacs.h for the macros that compute these
160  * alignment requirements. Note also that we allow the nominal alignment
161  * to be violated when storing "packed" varlenas; the TOAST mechanism
162  * takes care of hiding that from most code.
163  *
164  * NOTE: for types used in system tables, it is critical that the
165  * size and alignment defined in pg_type agree with the way that the
166  * compiler will lay out the field in a struct representing a table row.
167  * ----------------
168  */
169  char typalign;
170 
171  /* ----------------
172  * typstorage tells if the type is prepared for toasting and what
173  * the default strategy for attributes of this type should be.
174  *
175  * 'p' PLAIN type not prepared for toasting
176  * 'e' EXTERNAL external storage possible, don't try to compress
177  * 'x' EXTENDED try to compress and store external if required
178  * 'm' MAIN like 'x' but try to keep in main tuple
179  * ----------------
180  */
181  char typstorage BKI_DEFAULT(p) BKI_ARRAY_DEFAULT(x);
182 
183  /*
184  * This flag represents a "NOT NULL" constraint against this datatype.
185  *
186  * If true, the attnotnull column for a corresponding table column using
187  * this datatype will always enforce the NOT NULL constraint.
188  *
189  * Used primarily for domain types.
190  */
191  bool typnotnull BKI_DEFAULT(f);
192 
193  /*
194  * Domains use typbasetype to show the base (or domain) type that the
195  * domain is based on. Zero if the type is not a domain.
196  */
197  Oid typbasetype BKI_DEFAULT(0);
198 
199  /*
200  * Domains use typtypmod to record the typmod to be applied to their base
201  * type (-1 if base type does not use a typmod). -1 if this type is not a
202  * domain.
203  */
204  int32 typtypmod BKI_DEFAULT(-1);
205 
206  /*
207  * typndims is the declared number of dimensions for an array domain type
208  * (i.e., typbasetype is an array type). Otherwise zero.
209  */
210  int32 typndims BKI_DEFAULT(0);
211 
212  /*
213  * Collation: 0 if type cannot use collations, nonzero (typically
214  * DEFAULT_COLLATION_OID) for collatable base types, possibly some other
215  * OID for domains over collatable types
216  */
217  Oid typcollation BKI_DEFAULT(0) BKI_LOOKUP(pg_collation);
218 
219 #ifdef CATALOG_VARLEN /* variable-length fields start here */
220 
221  /*
222  * If typdefaultbin is not NULL, it is the nodeToString representation of
223  * a default expression for the type. Currently this is only used for
224  * domains.
225  */
226  pg_node_tree typdefaultbin BKI_DEFAULT(_null_) BKI_ARRAY_DEFAULT(_null_);
227 
228  /*
229  * typdefault is NULL if the type has no associated default value. If
230  * typdefaultbin is not NULL, typdefault must contain a human-readable
231  * version of the default expression represented by typdefaultbin. If
232  * typdefaultbin is NULL and typdefault is not, then typdefault is the
233  * external representation of the type's default value, which may be fed
234  * to the type's input converter to produce a constant.
235  */
236  text typdefault BKI_DEFAULT(_null_) BKI_ARRAY_DEFAULT(_null_);
237 
238  /*
239  * Access permissions
240  */
241  aclitem typacl[1] BKI_DEFAULT(_null_);
242 #endif
244 
245 /* ----------------
246  * Form_pg_type corresponds to a pointer to a row with
247  * the format of pg_type relation.
248  * ----------------
249  */
251 
252 #ifdef EXPOSE_TO_CLIENT_CODE
253 
254 /*
255  * macros for values of poor-mans-enumerated-type columns
256  */
257 #define TYPTYPE_BASE 'b' /* base type (ordinary scalar type) */
258 #define TYPTYPE_COMPOSITE 'c' /* composite (e.g., table's rowtype) */
259 #define TYPTYPE_DOMAIN 'd' /* domain over another type */
260 #define TYPTYPE_ENUM 'e' /* enumerated type */
261 #define TYPTYPE_PSEUDO 'p' /* pseudo-type */
262 #define TYPTYPE_RANGE 'r' /* range type */
263 
264 #define TYPCATEGORY_INVALID '\0' /* not an allowed category */
265 #define TYPCATEGORY_ARRAY 'A'
266 #define TYPCATEGORY_BOOLEAN 'B'
267 #define TYPCATEGORY_COMPOSITE 'C'
268 #define TYPCATEGORY_DATETIME 'D'
269 #define TYPCATEGORY_ENUM 'E'
270 #define TYPCATEGORY_GEOMETRIC 'G'
271 #define TYPCATEGORY_NETWORK 'I' /* think INET */
272 #define TYPCATEGORY_NUMERIC 'N'
273 #define TYPCATEGORY_PSEUDOTYPE 'P'
274 #define TYPCATEGORY_RANGE 'R'
275 #define TYPCATEGORY_STRING 'S'
276 #define TYPCATEGORY_TIMESPAN 'T'
277 #define TYPCATEGORY_USER 'U'
278 #define TYPCATEGORY_BITSTRING 'V' /* er ... "varbit"? */
279 #define TYPCATEGORY_UNKNOWN 'X'
280 
281 /* Is a type OID a polymorphic pseudotype? (Beware of multiple evaluation) */
282 #define IsPolymorphicType(typid) \
283  ((typid) == ANYELEMENTOID || \
284  (typid) == ANYARRAYOID || \
285  (typid) == ANYNONARRAYOID || \
286  (typid) == ANYENUMOID || \
287  (typid) == ANYRANGEOID)
288 
289 #endif /* EXPOSE_TO_CLIENT_CODE */
290 
291 
292 extern ObjectAddress TypeShellMake(const char *typeName,
293  Oid typeNamespace,
294  Oid ownerId);
295 
296 extern ObjectAddress TypeCreate(Oid newTypeOid,
297  const char *typeName,
298  Oid typeNamespace,
299  Oid relationOid,
300  char relationKind,
301  Oid ownerId,
302  int16 internalSize,
303  char typeType,
304  char typeCategory,
305  bool typePreferred,
306  char typDelim,
307  Oid inputProcedure,
308  Oid outputProcedure,
309  Oid receiveProcedure,
310  Oid sendProcedure,
311  Oid typmodinProcedure,
312  Oid typmodoutProcedure,
313  Oid analyzeProcedure,
314  Oid elementType,
315  bool isImplicitArray,
316  Oid arrayType,
317  Oid baseType,
318  const char *defaultTypeValue,
319  char *defaultTypeBin,
320  bool passedByValue,
321  char alignment,
322  char storage,
323  int32 typeMod,
324  int32 typNDims,
325  bool typeNotNull,
326  Oid typeCollation);
327 
328 extern void GenerateTypeDependencies(Oid typeObjectId,
329  Form_pg_type typeForm,
330  Node *defaultExpr,
331  void *typacl,
332  char relationKind, /* only for relation
333  * rowtypes */
334  bool isImplicitArray,
335  bool isDependentType,
336  bool rebuild);
337 
338 extern void RenameTypeInternal(Oid typeOid, const char *newTypeName,
339  Oid typeNamespace);
340 
341 extern char *makeArrayTypeName(const char *typeName, Oid typeNamespace);
342 
343 extern bool moveArrayTypeName(Oid typeOid, const char *typeName,
344  Oid typeNamespace);
345 
346 #endif /* PG_TYPE_H */
#define BKI_BOOTSTRAP
Definition: genbki.h:26
signed short int16
Definition: c.h:346
Datum array_send(PG_FUNCTION_ARGS)
Definition: arrayfuncs.c:1547
Oid typnamespace BKI_DEFAULT(PGNSP)
Definition: nodes.h:525
bool moveArrayTypeName(Oid typeOid, const char *typeName, Oid typeNamespace)
Definition: pg_type.c:850
Datum array_recv(PG_FUNCTION_ARGS)
Definition: arrayfuncs.c:1267
int16 typlen BKI_ARRAY_DEFAULT(-1)
unsigned int Oid
Definition: postgres_ext.h:31
Datum array_out(PG_FUNCTION_ARGS)
Definition: arrayfuncs.c:1012
signed int int32
Definition: c.h:347
FormData_pg_type
Definition: pg_type.h:243
void GenerateTypeDependencies(Oid typeObjectId, Form_pg_type typeForm, Node *defaultExpr, void *typacl, char relationKind, bool isImplicitArray, bool isDependentType, bool rebuild)
Definition: pg_type.c:537
void RenameTypeInternal(Oid typeOid, const char *newTypeName, Oid typeNamespace)
Definition: pg_type.c:709
char typalign
Definition: pg_type.h:169
Definition: c.h:610
char * makeArrayTypeName(const char *typeName, Oid typeNamespace)
Definition: pg_type.c:784
#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
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:250
Oid regproc
Definition: c.h:511
Definition: c.h:556
Datum array_in(PG_FUNCTION_ARGS)
Definition: arrayfuncs.c:172
CATALOG(pg_type, 1247, TypeRelationId) BKI_BOOTSTRAP BKI_ROWTYPE_OID(71