PostgreSQL Source Code git master
Loading...
Searching...
No Matches
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-2026, 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"
23#include "catalog/pg_type_d.h" /* IWYU pragma: export */
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 */
37
39{
40 Oid oid; /* oid */
41
42 /* type name */
44
45 /* OID of namespace containing this type */
47
48 /* type owner */
50
51 /*
52 * For a fixed-size type, typlen is the number of bytes we use to
53 * represent a value of this type, e.g. 4 for an int4. But for a
54 * variable-length type, typlen is negative. We use -1 to indicate a
55 * "varlena" type (one that has a length word), -2 to indicate a
56 * null-terminated C string.
57 */
59
60 /*
61 * typbyval determines whether internal Postgres routines pass a value of
62 * this type by value or by reference. typbyval had better be false if
63 * the length is not 1, 2, or 4 (or 8 on 8-byte-Datum machines).
64 * Variable-length types are always passed by reference. Note that
65 * typbyval can be false even if the length would allow pass-by-value; for
66 * example, type macaddr8 is pass-by-ref even when Datum is 8 bytes.
67 */
68 bool typbyval BKI_ARRAY_DEFAULT(f);
69
70 /*
71 * typtype is 'b' for a base type, 'c' for a composite type (e.g., a
72 * table's rowtype), 'd' for a domain, 'e' for an enum type, 'p' for a
73 * pseudo-type, or 'r' for a range type. (Use the TYPTYPE macros below.)
74 *
75 * If typtype is 'c', typrelid is the OID of the class' entry in pg_class.
76 */
78
79 /*
80 * typcategory and typispreferred help the parser distinguish preferred
81 * and non-preferred coercions. The category can be any single ASCII
82 * character (but not \0). The categories used for built-in types are
83 * identified by the TYPCATEGORY macros below.
84 */
85
86 /* arbitrary type classification */
88
89 /* is type "preferred" within its category? */
91
92 /*
93 * If typisdefined is false, the entry is only a placeholder (forward
94 * reference). We know the type's name and owner, but not yet anything
95 * else about it.
96 */
98
99 /* delimiter for arrays of this type */
100 char typdelim BKI_DEFAULT(',');
101
102 /* associated pg_class OID if a composite type, else 0 */
104
105 /*
106 * Type-specific subscripting handler. If typsubscript is 0, it means
107 * that this type doesn't support subscripting. Note that various parts
108 * of the system deem types to be "true" array types only if their
109 * typsubscript is array_subscript_handler.
110 */
112
113 /*
114 * If typelem is not 0 then it identifies another row in pg_type, defining
115 * the type yielded by subscripting. This should be 0 if typsubscript is
116 * 0. However, it can be 0 when typsubscript isn't 0, if the handler
117 * doesn't need typelem to determine the subscripting result type. Note
118 * that a typelem dependency is considered to imply physical containment
119 * of the element type in this type; so DDL changes on the element type
120 * might be restricted by the presence of this type.
121 */
123
124 /*
125 * If there is a "true" array type having this type as element type,
126 * typarray links to it. Zero if no associated "true" array type.
127 */
129
130 /*
131 * I/O conversion procedures for the datatype.
132 */
133
134 /* text format (required) */
137
138 /* binary format (optional) */
141
142 /*
143 * I/O functions for optional type modifiers.
144 */
147
148 /*
149 * Custom ANALYZE procedure for the datatype (0 selects the default).
150 */
152
153 /* ----------------
154 * typalign is the alignment required when storing a value of this
155 * type. It applies to storage on disk as well as most
156 * representations of the value inside Postgres. When multiple values
157 * are stored consecutively, such as in the representation of a
158 * complete row on disk, padding is inserted before a datum of this
159 * type so that it begins on the specified boundary. The alignment
160 * reference is the beginning of the first datum in the sequence.
161 *
162 * 'c' = CHAR alignment, ie no alignment needed.
163 * 's' = SHORT alignment (2 bytes on most machines).
164 * 'i' = INT alignment (4 bytes on most machines).
165 * 'd' = DOUBLE alignment (8 bytes on many machines, but by no means all).
166 * (Use the TYPALIGN macros below for these.)
167 *
168 * See include/access/tupmacs.h for the macros that compute these
169 * alignment requirements. Note also that we allow the nominal alignment
170 * to be violated when storing "packed" varlenas; the TOAST mechanism
171 * takes care of hiding that from most code.
172 *
173 * NOTE: for types used in system tables, it is critical that the
174 * size and alignment defined in pg_type agree with the way that the
175 * compiler will lay out the field in a struct representing a table row.
176 * ----------------
177 */
179
180 /* ----------------
181 * typstorage tells if the type is prepared for toasting and what
182 * the default strategy for attributes of this type should be.
183 *
184 * 'p' PLAIN type not prepared for toasting
185 * 'e' EXTERNAL external storage possible, don't try to compress
186 * 'x' EXTENDED try to compress and store external if required
187 * 'm' MAIN like 'x' but try to keep in main tuple
188 * (Use the TYPSTORAGE macros below for these.)
189 *
190 * Note that 'm' fields can also be moved out to secondary storage,
191 * but only as a last resort ('e' and 'x' fields are moved first).
192 * ----------------
193 */
194 char typstorage BKI_DEFAULT(p) BKI_ARRAY_DEFAULT(x);
195
196 /*
197 * This flag represents a "NOT NULL" constraint against this datatype.
198 *
199 * If true, the attnotnull column for a corresponding table column using
200 * this datatype will always enforce the NOT NULL constraint.
201 *
202 * Used primarily for domain types.
203 */
204 bool typnotnull BKI_DEFAULT(f);
205
206 /*
207 * Domains use typbasetype to show the base (or domain) type that the
208 * domain is based on. Zero if the type is not a domain.
209 */
211
212 /*
213 * Domains use typtypmod to record the typmod to be applied to their base
214 * type (-1 if base type does not use a typmod). -1 if this type is not a
215 * domain.
216 */
217 int32 typtypmod BKI_DEFAULT(-1);
218
219 /*
220 * typndims is the declared number of dimensions for an array domain type
221 * (i.e., typbasetype is an array type). Otherwise zero.
222 */
224
225 /*
226 * Collation: 0 if type cannot use collations, nonzero (typically
227 * DEFAULT_COLLATION_OID) for collatable base types, possibly some other
228 * OID for domains over collatable types
229 */
231
232#ifdef CATALOG_VARLEN /* variable-length fields start here */
233
234 /*
235 * If typdefaultbin is not NULL, it is the nodeToString representation of
236 * a default expression for the type. Currently this is only used for
237 * domains.
238 */
240
241 /*
242 * typdefault is NULL if the type has no associated default value. If
243 * typdefaultbin is not NULL, typdefault must contain a human-readable
244 * version of the default expression represented by typdefaultbin. If
245 * typdefaultbin is NULL and typdefault is not, then typdefault is the
246 * external representation of the type's default value, which may be fed
247 * to the type's input converter to produce a constant.
248 */
250
251 /*
252 * Access permissions
253 */
255#endif
257
259
260/* ----------------
261 * Form_pg_type corresponds to a pointer to a row with
262 * the format of pg_type relation.
263 * ----------------
264 */
266
268
271
274
275#ifdef EXPOSE_TO_CLIENT_CODE
276
277/*
278 * macros for values of poor-mans-enumerated-type columns
279 */
280#define TYPTYPE_BASE 'b' /* base type (ordinary scalar type) */
281#define TYPTYPE_COMPOSITE 'c' /* composite (e.g., table's rowtype) */
282#define TYPTYPE_DOMAIN 'd' /* domain over another type */
283#define TYPTYPE_ENUM 'e' /* enumerated type */
284#define TYPTYPE_MULTIRANGE 'm' /* multirange type */
285#define TYPTYPE_PSEUDO 'p' /* pseudo-type */
286#define TYPTYPE_RANGE 'r' /* range type */
287
288#define TYPCATEGORY_INVALID '\0' /* not an allowed category */
289#define TYPCATEGORY_ARRAY 'A'
290#define TYPCATEGORY_BOOLEAN 'B'
291#define TYPCATEGORY_COMPOSITE 'C'
292#define TYPCATEGORY_DATETIME 'D'
293#define TYPCATEGORY_ENUM 'E'
294#define TYPCATEGORY_GEOMETRIC 'G'
295#define TYPCATEGORY_NETWORK 'I' /* think INET */
296#define TYPCATEGORY_NUMERIC 'N'
297#define TYPCATEGORY_PSEUDOTYPE 'P'
298#define TYPCATEGORY_RANGE 'R'
299#define TYPCATEGORY_STRING 'S'
300#define TYPCATEGORY_TIMESPAN 'T'
301#define TYPCATEGORY_USER 'U'
302#define TYPCATEGORY_BITSTRING 'V' /* er ... "varbit"? */
303#define TYPCATEGORY_UNKNOWN 'X'
304#define TYPCATEGORY_INTERNAL 'Z'
305
306#define TYPALIGN_CHAR 'c' /* char alignment (i.e. unaligned) */
307#define TYPALIGN_SHORT 's' /* short alignment (typically 2 bytes) */
308#define TYPALIGN_INT 'i' /* int alignment (typically 4 bytes) */
309#define TYPALIGN_DOUBLE 'd' /* double alignment (often 8 bytes) */
310
311#define TYPSTORAGE_PLAIN 'p' /* type not prepared for toasting */
312#define TYPSTORAGE_EXTERNAL 'e' /* toastable, don't try to compress */
313#define TYPSTORAGE_EXTENDED 'x' /* fully toastable */
314#define TYPSTORAGE_MAIN 'm' /* like 'x' but try to store inline */
315
316/* Is a type OID a polymorphic pseudotype? (Beware of multiple evaluation) */
317#define IsPolymorphicType(typid) \
318 (IsPolymorphicTypeFamily1(typid) || \
319 IsPolymorphicTypeFamily2(typid))
320
321/* Code not part of polymorphic type resolution should not use these macros: */
322#define IsPolymorphicTypeFamily1(typid) \
323 ((typid) == ANYELEMENTOID || \
324 (typid) == ANYARRAYOID || \
325 (typid) == ANYNONARRAYOID || \
326 (typid) == ANYENUMOID || \
327 (typid) == ANYRANGEOID || \
328 (typid) == ANYMULTIRANGEOID)
329
330#define IsPolymorphicTypeFamily2(typid) \
331 ((typid) == ANYCOMPATIBLEOID || \
332 (typid) == ANYCOMPATIBLEARRAYOID || \
333 (typid) == ANYCOMPATIBLENONARRAYOID || \
334 (typid) == ANYCOMPATIBLERANGEOID || \
335 (typid) == ANYCOMPATIBLEMULTIRANGEOID)
336
337/* Is this a "true" array type? (Requires fmgroids.h) */
338#define IsTrueArrayType(typeForm) \
339 (OidIsValid((typeForm)->typelem) && \
340 (typeForm)->typsubscript == F_ARRAY_SUBSCRIPT_HANDLER)
341
342/*
343 * Backwards compatibility for ancient random spellings of pg_type OID macros.
344 * Don't use these names in new code.
345 */
346#define CASHOID MONEYOID
347#define LSNOID PG_LSNOID
348
349#endif /* EXPOSE_TO_CLIENT_CODE */
350
351
352extern ObjectAddress TypeShellMake(const char *typeName,
354 Oid ownerId);
355
357 const char *typeName,
359 Oid relationOid,
360 char relationKind,
361 Oid ownerId,
363 char typeType,
364 char typeCategory,
365 bool typePreferred,
366 char typDelim,
376 bool isImplicitArray,
378 Oid baseType,
379 const char *defaultTypeValue,
380 char *defaultTypeBin,
381 bool passedByValue,
382 char alignment,
383 char storage,
386 bool typeNotNull,
388
392 void *typacl,
393 char relationKind, /* only for relation
394 * rowtypes */
395 bool isImplicitArray,
396 bool isDependentType,
397 bool makeExtensionDep,
398 bool rebuild);
399
400extern void RenameTypeInternal(Oid typeOid, const char *newTypeName,
402
403extern char *makeArrayTypeName(const char *typeName, Oid typeNamespace);
404
405extern bool moveArrayTypeName(Oid typeOid, const char *typeName,
407
408extern char *makeMultirangeTypeName(const char *rangeTypeName,
410
411#endif /* PG_TYPE_H */
Datum array_typanalyze(PG_FUNCTION_ARGS)
Datum array_recv(PG_FUNCTION_ARGS)
Datum array_send(PG_FUNCTION_ARGS)
Datum array_in(PG_FUNCTION_ARGS)
Definition arrayfuncs.c:181
Datum array_out(PG_FUNCTION_ARGS)
Datum array_subscript_handler(PG_FUNCTION_ARGS)
Definition arraysubs.c:540
Oid regproc
Definition c.h:675
int16_t int16
Definition c.h:553
int32_t int32
Definition c.h:554
#define BEGIN_CATALOG_STRUCT
Definition genbki.h:37
#define DECLARE_UNIQUE_INDEX_PKEY(name, oid, oidmacro, tblname, decl)
Definition genbki.h:105
#define BKI_LOOKUP(catalog)
Definition genbki.h:65
#define END_CATALOG_STRUCT
Definition genbki.h:38
#define BKI_DEFAULT(value)
Definition genbki.h:54
#define BKI_LOOKUP_OPT(catalog)
Definition genbki.h:66
#define DECLARE_UNIQUE_INDEX(name, oid, oidmacro, tblname, decl)
Definition genbki.h:104
#define DECLARE_TOAST(name, toastoid, indexoid)
Definition genbki.h:82
#define CATALOG(name, oid, oidmacro)
Definition genbki.h:42
#define BKI_BOOTSTRAP
Definition genbki.h:45
#define MAKE_SYSCACHE(name, idxname, nbuckets)
Definition genbki.h:146
#define BKI_ARRAY_DEFAULT(value)
Definition genbki.h:56
#define BKI_ROWTYPE_OID(oid, oidmacro)
Definition genbki.h:47
#define storage
int b
Definition isn.c:74
int x
Definition isn.c:75
END_CATALOG_STRUCT typedef FormData_pg_type * Form_pg_type
Definition pg_type.h:265
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
void RenameTypeInternal(Oid typeOid, const char *newTypeName, Oid typeNamespace)
Definition pg_type.c:763
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:195
bool moveArrayTypeName(Oid typeOid, const char *typeName, Oid typeNamespace)
Definition pg_type.c:903
char typalign
Definition pg_type.h:178
FormData_pg_type
Definition pg_type.h:256
ObjectAddress TypeShellMake(const char *typeName, Oid typeNamespace, Oid ownerId)
Definition pg_type.c:57
char * makeMultirangeTypeName(const char *rangeTypeName, Oid typeNamespace)
Definition pg_type.c:948
char * makeArrayTypeName(const char *typeName, Oid typeNamespace)
Definition pg_type.c:838
BEGIN_CATALOG_STRUCT TypeRelation_Rowtype_Id BKI_SCHEMA_MACRO
Definition pg_type.h:39
NameData typname
Definition pg_type.h:43
unsigned int Oid
static int fb(int x)
Definition nodes.h:135
Definition c.h:772
Definition c.h:718