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 */
41 	NameData	typname;
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 	 */
109 	regproc		typsubscript BKI_DEFAULT(-) BKI_ARRAY_DEFAULT(array_subscript_handler) BKI_LOOKUP_OPT(pg_proc);
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);
138 	regproc		typsend BKI_ARRAY_DEFAULT(array_send) 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 	 */
149 	regproc		typanalyze BKI_DEFAULT(-) BKI_ARRAY_DEFAULT(array_typanalyze) BKI_LOOKUP_OPT(pg_proc);
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
254 } FormData_pg_type;
255 
256 /* ----------------
257  *		Form_pg_type corresponds to a pointer to a row with
258  *		the format of pg_type relation.
259  * ----------------
260  */
261 typedef FormData_pg_type *Form_pg_type;
262 
263 DECLARE_TOAST(pg_type, 4171, 4172);
264 
265 DECLARE_UNIQUE_INDEX_PKEY(pg_type_oid_index, 2703, on pg_type using btree(oid oid_ops));
266 #define TypeOidIndexId	2703
267 DECLARE_UNIQUE_INDEX(pg_type_typname_nsp_index, 2704, on pg_type using btree(typname name_ops, typnamespace oid_ops));
268 #define TypeNameNspIndexId	2704
269 
270 #ifdef EXPOSE_TO_CLIENT_CODE
271 
272 /*
273  * macros for values of poor-mans-enumerated-type columns
274  */
275 #define  TYPTYPE_BASE		'b' /* base type (ordinary scalar type) */
276 #define  TYPTYPE_COMPOSITE	'c' /* composite (e.g., table's rowtype) */
277 #define  TYPTYPE_DOMAIN		'd' /* domain over another type */
278 #define  TYPTYPE_ENUM		'e' /* enumerated type */
279 #define  TYPTYPE_MULTIRANGE	'm' /* multirange type */
280 #define  TYPTYPE_PSEUDO		'p' /* pseudo-type */
281 #define  TYPTYPE_RANGE		'r' /* range type */
282 
283 #define  TYPCATEGORY_INVALID	'\0'	/* not an allowed category */
284 #define  TYPCATEGORY_ARRAY		'A'
285 #define  TYPCATEGORY_BOOLEAN	'B'
286 #define  TYPCATEGORY_COMPOSITE	'C'
287 #define  TYPCATEGORY_DATETIME	'D'
288 #define  TYPCATEGORY_ENUM		'E'
289 #define  TYPCATEGORY_GEOMETRIC	'G'
290 #define  TYPCATEGORY_NETWORK	'I' /* think INET */
291 #define  TYPCATEGORY_NUMERIC	'N'
292 #define  TYPCATEGORY_PSEUDOTYPE 'P'
293 #define  TYPCATEGORY_RANGE		'R'
294 #define  TYPCATEGORY_STRING		'S'
295 #define  TYPCATEGORY_TIMESPAN	'T'
296 #define  TYPCATEGORY_USER		'U'
297 #define  TYPCATEGORY_BITSTRING	'V' /* er ... "varbit"? */
298 #define  TYPCATEGORY_UNKNOWN	'X'
299 
300 #define  TYPALIGN_CHAR			'c' /* char alignment (i.e. unaligned) */
301 #define  TYPALIGN_SHORT			's' /* short alignment (typically 2 bytes) */
302 #define  TYPALIGN_INT			'i' /* int alignment (typically 4 bytes) */
303 #define  TYPALIGN_DOUBLE		'd' /* double alignment (often 8 bytes) */
304 
305 #define  TYPSTORAGE_PLAIN		'p' /* type not prepared for toasting */
306 #define  TYPSTORAGE_EXTERNAL	'e' /* toastable, don't try to compress */
307 #define  TYPSTORAGE_EXTENDED	'x' /* fully toastable */
308 #define  TYPSTORAGE_MAIN		'm' /* like 'x' but try to store inline */
309 
310 /* Is a type OID a polymorphic pseudotype?	(Beware of multiple evaluation) */
311 #define IsPolymorphicType(typid)  \
312 	(IsPolymorphicTypeFamily1(typid) || \
313 	 IsPolymorphicTypeFamily2(typid))
314 
315 /* Code not part of polymorphic type resolution should not use these macros: */
316 #define IsPolymorphicTypeFamily1(typid)  \
317 	((typid) == ANYELEMENTOID || \
318 	 (typid) == ANYARRAYOID || \
319 	 (typid) == ANYNONARRAYOID || \
320 	 (typid) == ANYENUMOID || \
321 	 (typid) == ANYRANGEOID || \
322 	 (typid) == ANYMULTIRANGEOID)
323 
324 #define IsPolymorphicTypeFamily2(typid)  \
325 	((typid) == ANYCOMPATIBLEOID || \
326 	 (typid) == ANYCOMPATIBLEARRAYOID || \
327 	 (typid) == ANYCOMPATIBLENONARRAYOID || \
328 	 (typid) == ANYCOMPATIBLERANGEOID || \
329 	 (typid) == ANYCOMPATIBLEMULTIRANGEOID)
330 
331 /* Is this a "true" array type?  (Requires fmgroids.h) */
332 #define IsTrueArrayType(typeForm)  \
333 	(OidIsValid((typeForm)->typelem) && \
334 	 (typeForm)->typsubscript == F_ARRAY_SUBSCRIPT_HANDLER)
335 
336 /*
337  * Backwards compatibility for ancient random spellings of pg_type OID macros.
338  * Don't use these names in new code.
339  */
340 #define CASHOID	MONEYOID
341 #define LSNOID	PG_LSNOID
342 
343 #endif							/* EXPOSE_TO_CLIENT_CODE */
344 
345 
346 extern ObjectAddress TypeShellMake(const char *typeName,
347 								   Oid typeNamespace,
348 								   Oid ownerId);
349 
350 extern ObjectAddress TypeCreate(Oid newTypeOid,
351 								const char *typeName,
352 								Oid typeNamespace,
353 								Oid relationOid,
354 								char relationKind,
355 								Oid ownerId,
356 								int16 internalSize,
357 								char typeType,
358 								char typeCategory,
359 								bool typePreferred,
360 								char typDelim,
361 								Oid inputProcedure,
362 								Oid outputProcedure,
363 								Oid receiveProcedure,
364 								Oid sendProcedure,
365 								Oid typmodinProcedure,
366 								Oid typmodoutProcedure,
367 								Oid analyzeProcedure,
368 								Oid subscriptProcedure,
369 								Oid elementType,
370 								bool isImplicitArray,
371 								Oid arrayType,
372 								Oid baseType,
373 								const char *defaultTypeValue,
374 								char *defaultTypeBin,
375 								bool passedByValue,
376 								char alignment,
377 								char storage,
378 								int32 typeMod,
379 								int32 typNDims,
380 								bool typeNotNull,
381 								Oid typeCollation);
382 
383 extern void GenerateTypeDependencies(HeapTuple typeTuple,
384 									 Relation typeCatalog,
385 									 Node *defaultExpr,
386 									 void *typacl,
387 									 char relationKind, /* only for relation
388 														 * rowtypes */
389 									 bool isImplicitArray,
390 									 bool isDependentType,
391 									 bool makeExtensionDep,
392 									 bool rebuild);
393 
394 extern void RenameTypeInternal(Oid typeOid, const char *newTypeName,
395 							   Oid typeNamespace);
396 
397 extern char *makeArrayTypeName(const char *typeName, Oid typeNamespace);
398 
399 extern bool moveArrayTypeName(Oid typeOid, const char *typeName,
400 							  Oid typeNamespace);
401 
402 extern char *makeMultirangeTypeName(const char *rangeTypeName,
403 									Oid typeNamespace);
404 
405 #endif							/* PG_TYPE_H */
406