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/pg_type_d.h"
23 
24 #include "catalog/objectaddress.h"
25 #include "nodes/nodes.h"
26 
27 /* ----------------
28  *		pg_type definition.  cpp turns this into
29  *		typedef struct FormData_pg_type
30  *
31  *		Some of the values in a pg_type instance are copied into
32  *		pg_attribute instances.  Some parts of Postgres use the pg_type copy,
33  *		while others use the pg_attribute copy, so they must match.
34  *		See struct FormData_pg_attribute for details.
35  * ----------------
36  */
37 CATALOG(pg_type,1247,TypeRelationId) BKI_BOOTSTRAP BKI_ROWTYPE_OID(71,TypeRelation_Rowtype_Id) BKI_SCHEMA_MACRO
38 {
39 	Oid			oid;			/* oid */
40 
41 	/* type name */
42 	NameData	typname;
43 
44 	/* OID of namespace containing this type */
45 	Oid			typnamespace BKI_DEFAULT(PGNSP);
46 
47 	/* type owner */
48 	Oid			typowner BKI_DEFAULT(PGUID);
49 
50 	/*
51 	 * For a fixed-size type, typlen is the number of bytes we use to
52 	 * represent a value of this type, e.g. 4 for an int4.  But for a
53 	 * variable-length type, typlen is negative.  We use -1 to indicate a
54 	 * "varlena" type (one that has a length word), -2 to indicate a
55 	 * null-terminated C string.
56 	 */
57 	int16		typlen BKI_ARRAY_DEFAULT(-1);
58 
59 	/*
60 	 * typbyval determines whether internal Postgres routines pass a value of
61 	 * this type by value or by reference.  typbyval had better be false if
62 	 * the length is not 1, 2, or 4 (or 8 on 8-byte-Datum machines).
63 	 * Variable-length types are always passed by reference. Note that
64 	 * typbyval can be false even if the length would allow pass-by-value; for
65 	 * example, type macaddr8 is pass-by-ref even when Datum is 8 bytes.
66 	 */
67 	bool		typbyval BKI_ARRAY_DEFAULT(f);
68 
69 	/*
70 	 * typtype is 'b' for a base type, 'c' for a composite type (e.g., a
71 	 * table's rowtype), 'd' for a domain, 'e' for an enum type, 'p' for a
72 	 * pseudo-type, or 'r' for a range type. (Use the TYPTYPE macros below.)
73 	 *
74 	 * If typtype is 'c', typrelid is the OID of the class' entry in pg_class.
75 	 */
76 	char		typtype BKI_DEFAULT(b) BKI_ARRAY_DEFAULT(b);
77 
78 	/*
79 	 * typcategory and typispreferred help the parser distinguish preferred
80 	 * and non-preferred coercions.  The category can be any single ASCII
81 	 * character (but not \0).  The categories used for built-in types are
82 	 * identified by the TYPCATEGORY macros below.
83 	 */
84 
85 	/* arbitrary type classification */
86 	char		typcategory BKI_ARRAY_DEFAULT(A);
87 
88 	/* is type "preferred" within its category? */
89 	bool		typispreferred BKI_DEFAULT(f) BKI_ARRAY_DEFAULT(f);
90 
91 	/*
92 	 * If typisdefined is false, the entry is only a placeholder (forward
93 	 * reference).  We know the type's name and owner, but not yet anything
94 	 * else about it.
95 	 */
96 	bool		typisdefined BKI_DEFAULT(t);
97 
98 	/* delimiter for arrays of this type */
99 	char		typdelim BKI_DEFAULT(',');
100 
101 	/* associated pg_class OID if a composite type, else 0 */
102 	Oid			typrelid BKI_DEFAULT(0) BKI_ARRAY_DEFAULT(0) BKI_LOOKUP(pg_class);
103 
104 	/*
105 	 * If typelem is not 0 then it identifies another row in pg_type. The
106 	 * current type can then be subscripted like an array yielding values of
107 	 * type typelem. A non-zero typelem does not guarantee this type to be a
108 	 * "real" array type; some ordinary fixed-length types can also be
109 	 * subscripted (e.g., name, point). Variable-length types can *not* be
110 	 * turned into pseudo-arrays like that. Hence, the way to determine
111 	 * whether a type is a "true" array type is if:
112 	 *
113 	 * typelem != 0 and typlen == -1.
114 	 */
115 	Oid			typelem BKI_DEFAULT(0) BKI_LOOKUP(pg_type);
116 
117 	/*
118 	 * If there is a "true" array type having this type as element type,
119 	 * typarray links to it.  Zero if no associated "true" array type.
120 	 */
121 	Oid			typarray BKI_DEFAULT(0) BKI_ARRAY_DEFAULT(0) BKI_LOOKUP(pg_type);
122 
123 	/*
124 	 * I/O conversion procedures for the datatype.
125 	 */
126 
127 	/* text format (required) */
128 	regproc		typinput BKI_ARRAY_DEFAULT(array_in) BKI_LOOKUP(pg_proc);
129 	regproc		typoutput BKI_ARRAY_DEFAULT(array_out) BKI_LOOKUP(pg_proc);
130 
131 	/* binary format (optional) */
132 	regproc		typreceive BKI_ARRAY_DEFAULT(array_recv) BKI_LOOKUP(pg_proc);
133 	regproc		typsend BKI_ARRAY_DEFAULT(array_send) BKI_LOOKUP(pg_proc);
134 
135 	/*
136 	 * I/O functions for optional type modifiers.
137 	 */
138 	regproc		typmodin BKI_DEFAULT(-) BKI_LOOKUP(pg_proc);
139 	regproc		typmodout BKI_DEFAULT(-) BKI_LOOKUP(pg_proc);
140 
141 	/*
142 	 * Custom ANALYZE procedure for the datatype (0 selects the default).
143 	 */
144 	regproc		typanalyze BKI_DEFAULT(-) BKI_ARRAY_DEFAULT(array_typanalyze) BKI_LOOKUP(pg_proc);
145 
146 	/* ----------------
147 	 * typalign is the alignment required when storing a value of this
148 	 * type.  It applies to storage on disk as well as most
149 	 * representations of the value inside Postgres.  When multiple values
150 	 * are stored consecutively, such as in the representation of a
151 	 * complete row on disk, padding is inserted before a datum of this
152 	 * type so that it begins on the specified boundary.  The alignment
153 	 * reference is the beginning of the first datum in the sequence.
154 	 *
155 	 * 'c' = CHAR alignment, ie no alignment needed.
156 	 * 's' = SHORT alignment (2 bytes on most machines).
157 	 * 'i' = INT alignment (4 bytes on most machines).
158 	 * 'd' = DOUBLE alignment (8 bytes on many machines, but by no means all).
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 	 * ----------------
181 	 */
182 	char		typstorage BKI_DEFAULT(p) BKI_ARRAY_DEFAULT(x);
183 
184 	/*
185 	 * This flag represents a "NOT NULL" constraint against this datatype.
186 	 *
187 	 * If true, the attnotnull column for a corresponding table column using
188 	 * this datatype will always enforce the NOT NULL constraint.
189 	 *
190 	 * Used primarily for domain types.
191 	 */
192 	bool		typnotnull BKI_DEFAULT(f);
193 
194 	/*
195 	 * Domains use typbasetype to show the base (or domain) type that the
196 	 * domain is based on.  Zero if the type is not a domain.
197 	 */
198 	Oid			typbasetype BKI_DEFAULT(0);
199 
200 	/*
201 	 * Domains use typtypmod to record the typmod to be applied to their base
202 	 * type (-1 if base type does not use a typmod).  -1 if this type is not a
203 	 * domain.
204 	 */
205 	int32		typtypmod BKI_DEFAULT(-1);
206 
207 	/*
208 	 * typndims is the declared number of dimensions for an array domain type
209 	 * (i.e., typbasetype is an array type).  Otherwise zero.
210 	 */
211 	int32		typndims BKI_DEFAULT(0);
212 
213 	/*
214 	 * Collation: 0 if type cannot use collations, nonzero (typically
215 	 * DEFAULT_COLLATION_OID) for collatable base types, possibly some other
216 	 * OID for domains over collatable types
217 	 */
218 	Oid			typcollation BKI_DEFAULT(0) BKI_LOOKUP(pg_collation);
219 
220 #ifdef CATALOG_VARLEN			/* variable-length fields start here */
221 
222 	/*
223 	 * If typdefaultbin is not NULL, it is the nodeToString representation of
224 	 * a default expression for the type.  Currently this is only used for
225 	 * domains.
226 	 */
227 	pg_node_tree typdefaultbin BKI_DEFAULT(_null_) BKI_ARRAY_DEFAULT(_null_);
228 
229 	/*
230 	 * typdefault is NULL if the type has no associated default value. If
231 	 * typdefaultbin is not NULL, typdefault must contain a human-readable
232 	 * version of the default expression represented by typdefaultbin. If
233 	 * typdefaultbin is NULL and typdefault is not, then typdefault is the
234 	 * external representation of the type's default value, which may be fed
235 	 * to the type's input converter to produce a constant.
236 	 */
237 	text		typdefault BKI_DEFAULT(_null_) BKI_ARRAY_DEFAULT(_null_);
238 
239 	/*
240 	 * Access permissions
241 	 */
242 	aclitem		typacl[1] BKI_DEFAULT(_null_);
243 #endif
244 } FormData_pg_type;
245 
246 /* ----------------
247  *		Form_pg_type corresponds to a pointer to a row with
248  *		the format of pg_type relation.
249  * ----------------
250  */
251 typedef FormData_pg_type *Form_pg_type;
252 
253 #ifdef EXPOSE_TO_CLIENT_CODE
254 
255 /*
256  * macros for values of poor-mans-enumerated-type columns
257  */
258 #define  TYPTYPE_BASE		'b' /* base type (ordinary scalar type) */
259 #define  TYPTYPE_COMPOSITE	'c' /* composite (e.g., table's rowtype) */
260 #define  TYPTYPE_DOMAIN		'd' /* domain over another type */
261 #define  TYPTYPE_ENUM		'e' /* enumerated type */
262 #define  TYPTYPE_PSEUDO		'p' /* pseudo-type */
263 #define  TYPTYPE_RANGE		'r' /* range type */
264 
265 #define  TYPCATEGORY_INVALID	'\0'	/* not an allowed category */
266 #define  TYPCATEGORY_ARRAY		'A'
267 #define  TYPCATEGORY_BOOLEAN	'B'
268 #define  TYPCATEGORY_COMPOSITE	'C'
269 #define  TYPCATEGORY_DATETIME	'D'
270 #define  TYPCATEGORY_ENUM		'E'
271 #define  TYPCATEGORY_GEOMETRIC	'G'
272 #define  TYPCATEGORY_NETWORK	'I' /* think INET */
273 #define  TYPCATEGORY_NUMERIC	'N'
274 #define  TYPCATEGORY_PSEUDOTYPE 'P'
275 #define  TYPCATEGORY_RANGE		'R'
276 #define  TYPCATEGORY_STRING		'S'
277 #define  TYPCATEGORY_TIMESPAN	'T'
278 #define  TYPCATEGORY_USER		'U'
279 #define  TYPCATEGORY_BITSTRING	'V' /* er ... "varbit"? */
280 #define  TYPCATEGORY_UNKNOWN	'X'
281 
282 /* Is a type OID a polymorphic pseudotype?	(Beware of multiple evaluation) */
283 #define IsPolymorphicType(typid)  \
284 	((typid) == ANYELEMENTOID || \
285 	 (typid) == ANYARRAYOID || \
286 	 (typid) == ANYNONARRAYOID || \
287 	 (typid) == ANYENUMOID || \
288 	 (typid) == ANYRANGEOID)
289 
290 #endif							/* EXPOSE_TO_CLIENT_CODE */
291 
292 
293 extern ObjectAddress TypeShellMake(const char *typeName,
294 								   Oid typeNamespace,
295 								   Oid ownerId);
296 
297 extern ObjectAddress TypeCreate(Oid newTypeOid,
298 								const char *typeName,
299 								Oid typeNamespace,
300 								Oid relationOid,
301 								char relationKind,
302 								Oid ownerId,
303 								int16 internalSize,
304 								char typeType,
305 								char typeCategory,
306 								bool typePreferred,
307 								char typDelim,
308 								Oid inputProcedure,
309 								Oid outputProcedure,
310 								Oid receiveProcedure,
311 								Oid sendProcedure,
312 								Oid typmodinProcedure,
313 								Oid typmodoutProcedure,
314 								Oid analyzeProcedure,
315 								Oid elementType,
316 								bool isImplicitArray,
317 								Oid arrayType,
318 								Oid baseType,
319 								const char *defaultTypeValue,
320 								char *defaultTypeBin,
321 								bool passedByValue,
322 								char alignment,
323 								char storage,
324 								int32 typeMod,
325 								int32 typNDims,
326 								bool typeNotNull,
327 								Oid typeCollation);
328 
329 extern void GenerateTypeDependencies(Oid typeObjectId,
330 									 Form_pg_type typeForm,
331 									 Node *defaultExpr,
332 									 void *typacl,
333 									 char relationKind, /* only for relation
334 														 * rowtypes */
335 									 bool isImplicitArray,
336 									 bool isDependentType,
337 									 bool rebuild);
338 
339 extern void RenameTypeInternal(Oid typeOid, const char *newTypeName,
340 							   Oid typeNamespace);
341 
342 extern char *makeArrayTypeName(const char *typeName, Oid typeNamespace);
343 
344 extern bool moveArrayTypeName(Oid typeOid, const char *typeName,
345 							  Oid typeNamespace);
346 
347 #endif							/* PG_TYPE_H */
348