Make LC_COLLATE and LC_CTYPE database-level settings. Collation and
[PostgreSQL.git] / doc / src / sgml / ref / create_type.sgml
blob70ddae648879660171e826c2b0e333255b010672
1 <!--
2 $PostgreSQL$
3 PostgreSQL documentation
4 -->
6 <refentry id="SQL-CREATETYPE">
7 <refmeta>
8 <refentrytitle id="sql-createtype-title">CREATE TYPE</refentrytitle>
9 <refmiscinfo>SQL - Language Statements</refmiscinfo>
10 </refmeta>
12 <refnamediv>
13 <refname>CREATE TYPE</refname>
14 <refpurpose>define a new data type</refpurpose>
15 </refnamediv>
17 <indexterm zone="sql-createtype">
18 <primary>CREATE TYPE</primary>
19 </indexterm>
21 <refsynopsisdiv>
22 <synopsis>
23 CREATE TYPE <replaceable class="parameter">name</replaceable> AS
24 ( <replaceable class="PARAMETER">attribute_name</replaceable> <replaceable class="PARAMETER">data_type</replaceable> [, ... ] )
26 CREATE TYPE <replaceable class="parameter">name</replaceable> AS ENUM
27 ( '<replaceable class="parameter">label</replaceable>' [, ... ] )
29 CREATE TYPE <replaceable class="parameter">name</replaceable> (
30 INPUT = <replaceable class="parameter">input_function</replaceable>,
31 OUTPUT = <replaceable class="parameter">output_function</replaceable>
32 [ , RECEIVE = <replaceable class="parameter">receive_function</replaceable> ]
33 [ , SEND = <replaceable class="parameter">send_function</replaceable> ]
34 [ , TYPMOD_IN = <replaceable class="parameter">type_modifier_input_function</replaceable> ]
35 [ , TYPMOD_OUT = <replaceable class="parameter">type_modifier_output_function</replaceable> ]
36 [ , ANALYZE = <replaceable class="parameter">analyze_function</replaceable> ]
37 [ , INTERNALLENGTH = { <replaceable class="parameter">internallength</replaceable> | VARIABLE } ]
38 [ , PASSEDBYVALUE ]
39 [ , ALIGNMENT = <replaceable class="parameter">alignment</replaceable> ]
40 [ , STORAGE = <replaceable class="parameter">storage</replaceable> ]
41 [ , CATEGORY = <replaceable class="parameter">category</replaceable> ]
42 [ , PREFERRED = <replaceable class="parameter">preferred</replaceable> ]
43 [ , DEFAULT = <replaceable class="parameter">default</replaceable> ]
44 [ , ELEMENT = <replaceable class="parameter">element</replaceable> ]
45 [ , DELIMITER = <replaceable class="parameter">delimiter</replaceable> ]
48 CREATE TYPE <replaceable class="parameter">name</replaceable>
49 </synopsis>
50 </refsynopsisdiv>
52 <refsect1>
53 <title>Description</title>
55 <para>
56 <command>CREATE TYPE</command> registers a new data type for use in
57 the current database. The user who defines a type becomes its
58 owner.
59 </para>
61 <para>
62 If a schema name is given then the type is created in the specified
63 schema. Otherwise it is created in the current schema. The type
64 name must be distinct from the name of any existing type or domain
65 in the same schema. (Because tables have associated data types,
66 the type name must also be distinct from the name of any existing
67 table in the same schema.)
68 </para>
70 <refsect2>
71 <title>Composite Types</title>
73 <para>
74 The first form of <command>CREATE TYPE</command>
75 creates a composite type.
76 The composite type is specified by a list of attribute names and data types.
77 This is essentially the same as the row type
78 of a table, but using <command>CREATE TYPE</command> avoids the need to
79 create an actual table when all that is wanted is to define a type.
80 A stand-alone composite type is useful as the argument or return type of a
81 function.
82 </para>
83 </refsect2>
85 <refsect2>
86 <title>Enumerated Types</title>
88 <para>
89 The second form of <command>CREATE TYPE</command> creates an enumerated
90 (enum) type, as described in <xref linkend="datatype-enum">.
91 Enum types take a list of one or more quoted labels, each of which
92 must be less than <symbol>NAMEDATALEN</symbol> bytes long (64 in a standard
93 <productname>PostgreSQL</productname> build).
94 </para>
95 </refsect2>
97 <refsect2>
98 <title>Base Types</title>
100 <para>
101 The third form of <command>CREATE TYPE</command> creates a new base type
102 (scalar type). To create a new base type, you must be a superuser.
103 (This restriction is made because an erroneous type definition could
104 confuse or even crash the server.)
105 </para>
107 <para>
108 The parameters can appear in any order, not only that
109 illustrated above, and most are optional. You must register
110 two or more functions (using <command>CREATE FUNCTION</command>) before
111 defining the type. The support functions
112 <replaceable class="parameter">input_function</replaceable> and
113 <replaceable class="parameter">output_function</replaceable>
114 are required, while the functions
115 <replaceable class="parameter">receive_function</replaceable>,
116 <replaceable class="parameter">send_function</replaceable>,
117 <replaceable class="parameter">type_modifier_input_function</replaceable>,
118 <replaceable class="parameter">type_modifier_output_function</replaceable> and
119 <replaceable class="parameter">analyze_function</replaceable>
120 are optional. Generally these functions have to be coded in C
121 or another low-level language.
122 </para>
124 <para>
125 The <replaceable class="parameter">input_function</replaceable>
126 converts the type's external textual representation to the internal
127 representation used by the operators and functions defined for the type.
128 <replaceable class="parameter">output_function</replaceable>
129 performs the reverse transformation. The input function can be
130 declared as taking one argument of type <type>cstring</type>,
131 or as taking three arguments of types
132 <type>cstring</type>, <type>oid</type>, <type>integer</type>.
133 The first argument is the input text as a C string, the second
134 argument is the type's own OID (except for array types, which instead
135 receive their element type's OID),
136 and the third is the <literal>typmod</> of the destination column, if known
137 (-1 will be passed if not).
138 The input function must return a value of the data type itself.
139 Usually, an input function should be declared STRICT; if it is not,
140 it will be called with a NULL first parameter when reading a NULL
141 input value. The function must still return NULL in this case, unless
142 it raises an error.
143 (This case is mainly meant to support domain input functions, which
144 might need to reject NULL inputs.)
145 The output function must be
146 declared as taking one argument of the new data type.
147 The output function must return type <type>cstring</type>.
148 Output functions are not invoked for NULL values.
149 </para>
151 <para>
152 The optional <replaceable class="parameter">receive_function</replaceable>
153 converts the type's external binary representation to the internal
154 representation. If this function is not supplied, the type cannot
155 participate in binary input. The binary representation should be
156 chosen to be cheap to convert to internal form, while being reasonably
157 portable. (For example, the standard integer data types use network
158 byte order as the external binary representation, while the internal
159 representation is in the machine's native byte order.) The receive
160 function should perform adequate checking to ensure that the value is
161 valid.
162 The receive function can be declared as taking one argument of type
163 <type>internal</type>, or as taking three arguments of types
164 <type>internal</type>, <type>oid</type>, <type>integer</type>.
165 The first argument is a pointer to a <type>StringInfo</type> buffer
166 holding the received byte string; the optional arguments are the
167 same as for the text input function.
168 The receive function must return a value of the data type itself.
169 Usually, a receive function should be declared STRICT; if it is not,
170 it will be called with a NULL first parameter when reading a NULL
171 input value. The function must still return NULL in this case, unless
172 it raises an error.
173 (This case is mainly meant to support domain receive functions, which
174 might need to reject NULL inputs.)
175 Similarly, the optional
176 <replaceable class="parameter">send_function</replaceable> converts
177 from the internal representation to the external binary representation.
178 If this function is not supplied, the type cannot participate in binary
179 output. The send function must be
180 declared as taking one argument of the new data type.
181 The send function must return type <type>bytea</type>.
182 Send functions are not invoked for NULL values.
183 </para>
185 <para>
186 You should at this point be wondering how the input and output functions
187 can be declared to have results or arguments of the new type, when they
188 have to be created before the new type can be created. The answer is that
189 the type should first be defined as a <firstterm>shell type</>, which is a
190 placeholder type that has no properties except a name and an owner. This
191 is done by issuing the command <literal>CREATE TYPE
192 <replaceable>name</></literal>, with no additional parameters. Then the
193 I/O functions can be defined referencing the shell type. Finally,
194 <command>CREATE TYPE</> with a full definition replaces the shell entry
195 with a complete, valid type definition, after which the new type can be
196 used normally.
197 </para>
199 <para>
200 The optional
201 <replaceable class="parameter">type_modifier_input_function</replaceable>
202 and <replaceable class="parameter">type_modifier_output_function</replaceable>
203 are needed if the type supports modifiers, that is optional constraints
204 attached to a type declaration, such as <literal>char(5)</> or
205 <literal>numeric(30,2)</>. <productname>PostgreSQL</productname> allows
206 user-defined types to take one or more simple constants or identifiers as
207 modifiers. However, this information must be capable of being packed into a
208 single non-negative integer value for storage in the system catalogs. The
209 <replaceable class="parameter">type_modifier_input_function</replaceable>
210 is passed the declared modifier(s) in the form of a <type>cstring</>
211 array. It must check the values for validity (throwing an error if they
212 are wrong), and if they are correct, return a single non-negative
213 <type>integer</> value that will be stored as the column <quote>typmod</>.
214 Type modifiers will be rejected if the type does not have a
215 <replaceable class="parameter">type_modifier_input_function</replaceable>.
216 The <replaceable class="parameter">type_modifier_output_function</replaceable>
217 converts the internal integer typmod value back to the correct form for
218 user display. It must return a <type>cstring</> value that is the exact
219 string to append to the type name; for example <type>numeric</>'s
220 function might return <literal>(30,2)</>.
221 It is allowed to omit the
222 <replaceable class="parameter">type_modifier_output_function</replaceable>,
223 in which case the default display format is just the stored typmod integer
224 value enclosed in parentheses.
225 </para>
227 <para>
228 The optional <replaceable class="parameter">analyze_function</replaceable>
229 performs type-specific statistics collection for columns of the data type.
230 By default, <command>ANALYZE</> will attempt to gather statistics using
231 the type's <quote>equals</> and <quote>less-than</> operators, if there
232 is a default b-tree operator class for the type. For non-scalar types
233 this behavior is likely to be unsuitable, so it can be overridden by
234 specifying a custom analysis function. The analysis function must be
235 declared to take a single argument of type <type>internal</>, and return
236 a <type>boolean</> result. The detailed API for analysis functions appears
237 in <filename>src/include/commands/vacuum.h</>.
238 </para>
240 <para>
241 While the details of the new type's internal representation are only
242 known to the I/O functions and other functions you create to work with
243 the type, there are several properties of the internal representation
244 that must be declared to <productname>PostgreSQL</productname>.
245 Foremost of these is
246 <replaceable class="parameter">internallength</replaceable>.
247 Base data types can be fixed-length, in which case
248 <replaceable class="parameter">internallength</replaceable> is a
249 positive integer, or variable length, indicated by setting
250 <replaceable class="parameter">internallength</replaceable>
251 to <literal>VARIABLE</literal>. (Internally, this is represented
252 by setting <literal>typlen</> to -1.) The internal representation of all
253 variable-length types must start with a 4-byte integer giving the total
254 length of this value of the type.
255 </para>
257 <para>
258 The optional flag <literal>PASSEDBYVALUE</literal> indicates that
259 values of this data type are passed by value, rather than by
260 reference. You cannot pass by value types whose internal
261 representation is larger than the size of the <type>Datum</> type
262 (4 bytes on most machines, 8 bytes on a few).
263 </para>
265 <para>
266 The <replaceable class="parameter">alignment</replaceable> parameter
267 specifies the storage alignment required for the data type. The
268 allowed values equate to alignment on 1, 2, 4, or 8 byte boundaries.
269 Note that variable-length types must have an alignment of at least
270 4, since they necessarily contain an <type>int4</> as their first component.
271 </para>
273 <para>
274 The <replaceable class="parameter">storage</replaceable> parameter
275 allows selection of storage strategies for variable-length data
276 types. (Only <literal>plain</literal> is allowed for fixed-length
277 types.) <literal>plain</literal> specifies that data of the type
278 will always be stored in-line and not compressed.
279 <literal>extended</literal> specifies that the system will first
280 try to compress a long data value, and will move the value out of
281 the main table row if it's still too long.
282 <literal>external</literal> allows the value to be moved out of the
283 main table, but the system will not try to compress it.
284 <literal>main</literal> allows compression, but discourages moving
285 the value out of the main table. (Data items with this storage
286 strategy might still be moved out of the main table if there is no
287 other way to make a row fit, but they will be kept in the main
288 table preferentially over <literal>extended</literal> and
289 <literal>external</literal> items.)
290 </para>
292 <para>
293 The <replaceable class="parameter">category</replaceable> and
294 <replaceable class="parameter">preferred</replaceable> parameters can be
295 used to help control which implicit cast will be applied in ambiguous
296 situations. Each data type belongs to a category named by a single ASCII
297 character, and each type is either <quote>preferred</> or not within its
298 category. The parser will prefer casting to preferred types (but only from
299 other types within the same category) when this rule is helpful in
300 resolving overloaded functions or operators. For more details see <xref
301 linkend="typeconv">. For types that have no implicit casts to or from any
302 other types, it is sufficient to leave these settings at the defaults.
303 However, for a group of related types that have implicit casts, it is often
304 helpful to mark them all as belonging to a category and select one or two
305 of the <quote>most general</> types as being preferred within the category.
306 The <replaceable class="parameter">category</replaceable> parameter is
307 especially useful when adding a user-defined type to an existing built-in
308 category, such as the numeric or string types. However, it is also
309 possible to create new entirely-user-defined type categories. Select any
310 ASCII character other than an upper-case letter to name such a category.
311 </para>
313 <para>
314 A default value can be specified, in case a user wants columns of the
315 data type to default to something other than the null value.
316 Specify the default with the <literal>DEFAULT</literal> key word.
317 (Such a default can be overridden by an explicit <literal>DEFAULT</literal>
318 clause attached to a particular column.)
319 </para>
321 <para>
322 To indicate that a type is an array, specify the type of the array
323 elements using the <literal>ELEMENT</> key word. For example, to
324 define an array of 4-byte integers (<type>int4</type>), specify
325 <literal>ELEMENT = int4</literal>. More details about array types
326 appear below.
327 </para>
329 <para>
330 To indicate the delimiter to be used between values in the external
331 representation of arrays of this type, <replaceable
332 class="parameter">delimiter</replaceable> can be
333 set to a specific character. The default delimiter is the comma
334 (<literal>,</literal>). Note that the delimiter is associated
335 with the array element type, not the array type itself.
336 </para>
338 </refsect2>
340 <refsect2>
341 <title>Array Types</title>
343 <para>
344 Whenever a user-defined type is created,
345 <productname>PostgreSQL</productname> automatically creates an
346 associated array type, whose name consists of the base type's
347 name prepended with an underscore, and truncated if necessary to keep
348 it less than <symbol>NAMEDATALEN</symbol> bytes long. (If the name
349 so generated collides with an existing type name, the process is
350 repeated until a non-colliding name is found.)
351 This implicitly-created array type is variable length and uses the
352 built-in input and output functions <literal>array_in</> and
353 <literal>array_out</>. The array type tracks any changes in its
354 element type's owner or schema, and is dropped if the element type is.
355 </para>
357 <para>
358 You might reasonably ask why there is an <option>ELEMENT</>
359 option, if the system makes the correct array type automatically.
360 The only case where it's useful to use <option>ELEMENT</> is when you are
361 making a fixed-length type that happens to be internally an array of a number of
362 identical things, and you want to allow these things to be accessed
363 directly by subscripting, in addition to whatever operations you plan
364 to provide for the type as a whole. For example, type <type>point</>
365 is represented as just two floating-point numbers, which it allows to be
366 accessed as <literal>point[0]</> and <literal>point[1]</>.
367 Note that
368 this facility only works for fixed-length types whose internal form
369 is exactly a sequence of identical fixed-length fields. A subscriptable
370 variable-length type must have the generalized internal representation
371 used by <literal>array_in</> and <literal>array_out</>.
372 For historical reasons (i.e., this is clearly wrong but it's far too
373 late to change it), subscripting of fixed-length array types starts from
374 zero, rather than from one as for variable-length arrays.
375 </para>
376 </refsect2>
377 </refsect1>
379 <refsect1>
380 <title>Parameters</title>
382 <variablelist>
383 <varlistentry>
384 <term><replaceable class="parameter">name</replaceable></term>
385 <listitem>
386 <para>
387 The name (optionally schema-qualified) of a type to be created.
388 </para>
389 </listitem>
390 </varlistentry>
392 <varlistentry>
393 <term><replaceable class="parameter">attribute_name</replaceable></term>
394 <listitem>
395 <para>
396 The name of an attribute (column) for the composite type.
397 </para>
398 </listitem>
399 </varlistentry>
401 <varlistentry>
402 <term><replaceable class="parameter">data_type</replaceable></term>
403 <listitem>
404 <para>
405 The name of an existing data type to become a column of the
406 composite type.
407 </para>
408 </listitem>
409 </varlistentry>
411 <varlistentry>
412 <term><replaceable class="parameter">label</replaceable></term>
413 <listitem>
414 <para>
415 A string literal representing the textual label associated with
416 one value of an enum type.
417 </para>
418 </listitem>
419 </varlistentry>
421 <varlistentry>
422 <term><replaceable class="parameter">input_function</replaceable></term>
423 <listitem>
424 <para>
425 The name of a function that converts data from the type's
426 external textual form to its internal form.
427 </para>
428 </listitem>
429 </varlistentry>
431 <varlistentry>
432 <term><replaceable class="parameter">output_function</replaceable></term>
433 <listitem>
434 <para>
435 The name of a function that converts data from the type's
436 internal form to its external textual form.
437 </para>
438 </listitem>
439 </varlistentry>
441 <varlistentry>
442 <term><replaceable class="parameter">receive_function</replaceable></term>
443 <listitem>
444 <para>
445 The name of a function that converts data from the type's
446 external binary form to its internal form.
447 </para>
448 </listitem>
449 </varlistentry>
451 <varlistentry>
452 <term><replaceable class="parameter">send_function</replaceable></term>
453 <listitem>
454 <para>
455 The name of a function that converts data from the type's
456 internal form to its external binary form.
457 </para>
458 </listitem>
459 </varlistentry>
461 <varlistentry>
462 <term><replaceable class="parameter">type_modifier_input_function</replaceable></term>
463 <listitem>
464 <para>
465 The name of a function that converts an array of modifier(s) for the type
466 into internal form.
467 </para>
468 </listitem>
469 </varlistentry>
471 <varlistentry>
472 <term><replaceable class="parameter">type_modifier_output_function</replaceable></term>
473 <listitem>
474 <para>
475 The name of a function that converts the internal form of the type's
476 modifier(s) to external textual form.
477 </para>
478 </listitem>
479 </varlistentry>
481 <varlistentry>
482 <term><replaceable class="parameter">analyze_function</replaceable></term>
483 <listitem>
484 <para>
485 The name of a function that performs statistical analysis for the
486 data type.
487 </para>
488 </listitem>
489 </varlistentry>
491 <varlistentry>
492 <term><replaceable class="parameter">internallength</replaceable></term>
493 <listitem>
494 <para>
495 A numeric constant that specifies the length in bytes of the new
496 type's internal representation. The default assumption is that
497 it is variable-length.
498 </para>
499 </listitem>
500 </varlistentry>
502 <varlistentry>
503 <term><replaceable class="parameter">alignment</replaceable></term>
504 <listitem>
505 <para>
506 The storage alignment requirement of the data type. If specified,
507 it must be <literal>char</literal>, <literal>int2</literal>,
508 <literal>int4</literal>, or <literal>double</literal>; the
509 default is <literal>int4</literal>.
510 </para>
511 </listitem>
512 </varlistentry>
514 <varlistentry>
515 <term><replaceable class="parameter">storage</replaceable></term>
516 <listitem>
517 <para>
518 The storage strategy for the data type. If specified, must be
519 <literal>plain</literal>, <literal>external</literal>,
520 <literal>extended</literal>, or <literal>main</literal>; the
521 default is <literal>plain</literal>.
522 </para>
523 </listitem>
524 </varlistentry>
526 <varlistentry>
527 <term><replaceable class="parameter">category</replaceable></term>
528 <listitem>
529 <para>
530 The category code (a single ASCII character) for this type.
531 The default is <literal>'U'</> for <quote>user-defined type</>.
532 Other standard category codes can be found in
533 <xref linkend="catalog-typcategory-table">. You may also choose
534 other ASCII characters in order to create custom categories.
535 </para>
536 </listitem>
537 </varlistentry>
539 <varlistentry>
540 <term><replaceable class="parameter">preferred</replaceable></term>
541 <listitem>
542 <para>
543 True if this type is a preferred type within its type category,
544 else false. The default is false. Be very careful about creating
545 a new preferred type within an existing type category, as this
546 could cause surprising changes in behavior.
547 </para>
548 </listitem>
549 </varlistentry>
551 <varlistentry>
552 <term><replaceable class="parameter">default</replaceable></term>
553 <listitem>
554 <para>
555 The default value for the data type. If this is omitted, the
556 default is null.
557 </para>
558 </listitem>
559 </varlistentry>
561 <varlistentry>
562 <term><replaceable class="parameter">element</replaceable></term>
563 <listitem>
564 <para>
565 The type being created is an array; this specifies the type of
566 the array elements.
567 </para>
568 </listitem>
569 </varlistentry>
571 <varlistentry>
572 <term><replaceable class="parameter">delimiter</replaceable></term>
573 <listitem>
574 <para>
575 The delimiter character to be used between values in arrays made
576 of this type.
577 </para>
578 </listitem>
579 </varlistentry>
580 </variablelist>
581 </refsect1>
583 <refsect1 id="SQL-CREATETYPE-notes">
584 <title>Notes</title>
586 <para>
587 Because there are no restrictions on use of a data type once it's been
588 created, creating a base type is tantamount to granting public execute
589 permission on the functions mentioned in the type definition.
590 This is usually
591 not an issue for the sorts of functions that are useful in a type
592 definition. But you might want to think twice before designing a type
593 in a way that would require <quote>secret</> information to be used
594 while converting it to or from external form.
595 </para>
597 <para>
598 Before <productname>PostgreSQL</productname> version 8.3, the name of
599 a generated array type was always exactly the element type's name with one
600 underscore character (<literal>_</literal>) prepended. (Type names were
601 therefore restricted in length to one less character than other names.)
602 While this is still usually the case, the array type name may vary from
603 this in case of maximum-length names or collisions with user type names
604 that begin with underscore. Writing code that depends on this convention
605 is therefore deprecated. Instead, use
606 <structname>pg_type</>.<structfield>typarray</> to locate the array type
607 associated with a given type.
608 </para>
610 <para>
611 It may be advisable to avoid using type and table names that begin with
612 underscore. While the server will change generated array type names to
613 avoid collisions with user-given names, there is still risk of confusion,
614 particularly with old client software that may assume that type names
615 beginning with underscores always represent arrays.
616 </para>
618 <para>
619 Before <productname>PostgreSQL</productname> version 8.2, the syntax
620 <literal>CREATE TYPE <replaceable>name</></literal> did not exist.
621 The way to create a new base type was to create its input function first.
622 In this approach, <productname>PostgreSQL</productname> will first see
623 the name of the new data type as the return type of the input function.
624 The shell type is implicitly created in this situation, and then it
625 can be referenced in the definitions of the remaining I/O functions.
626 This approach still works, but is deprecated and might be disallowed in
627 some future release. Also, to avoid accidentally cluttering
628 the catalogs with shell types as a result of simple typos in function
629 definitions, a shell type will only be made this way when the input
630 function is written in C.
631 </para>
633 <para>
634 In <productname>PostgreSQL</productname> versions before 7.3, it
635 was customary to avoid creating a shell type at all, by replacing the
636 functions' forward references to the type name with the placeholder
637 pseudotype <type>opaque</>. The <type>cstring</> arguments and
638 results also had to be declared as <type>opaque</> before 7.3. To
639 support loading of old dump files, <command>CREATE TYPE</> will
640 accept I/O functions declared using <type>opaque</>, but it will issue
641 a notice and change the function declarations to use the correct
642 types.
643 </para>
645 </refsect1>
647 <refsect1>
648 <title>Examples</title>
650 <para>
651 This example creates a composite type and uses it in
652 a function definition:
653 <programlisting>
654 CREATE TYPE compfoo AS (f1 int, f2 text);
656 CREATE FUNCTION getfoo() RETURNS SETOF compfoo AS $$
657 SELECT fooid, fooname FROM foo
658 $$ LANGUAGE SQL;
659 </programlisting>
660 </para>
662 <para>
663 This example creates an enumerated type and uses it in
664 a table definition:
665 <programlisting>
666 CREATE TYPE bug_status AS ENUM ('new', 'open', 'closed');
668 CREATE TABLE bug (
669 id serial,
670 description text,
671 status bug_status
673 </programlisting>
674 </para>
676 <para>
677 This example creates the base data type <type>box</type> and then uses the
678 type in a table definition:
679 <programlisting>
680 CREATE TYPE box;
682 CREATE FUNCTION my_box_in_function(cstring) RETURNS box AS ... ;
683 CREATE FUNCTION my_box_out_function(box) RETURNS cstring AS ... ;
685 CREATE TYPE box (
686 INTERNALLENGTH = 16,
687 INPUT = my_box_in_function,
688 OUTPUT = my_box_out_function
691 CREATE TABLE myboxes (
692 id integer,
693 description box
695 </programlisting>
696 </para>
698 <para>
699 If the internal structure of <type>box</type> were an array of four
700 <type>float4</> elements, we might instead use:
701 <programlisting>
702 CREATE TYPE box (
703 INTERNALLENGTH = 16,
704 INPUT = my_box_in_function,
705 OUTPUT = my_box_out_function,
706 ELEMENT = float4
708 </programlisting>
709 which would allow a box value's component numbers to be accessed
710 by subscripting. Otherwise the type behaves the same as before.
711 </para>
713 <para>
714 This example creates a large object type and uses it in
715 a table definition:
716 <programlisting>
717 CREATE TYPE bigobj (
718 INPUT = lo_filein, OUTPUT = lo_fileout,
719 INTERNALLENGTH = VARIABLE
721 CREATE TABLE big_objs (
722 id integer,
723 obj bigobj
725 </programlisting>
726 </para>
728 <para>
729 More examples, including suitable input and output functions, are
730 in <xref linkend="xtypes">.
731 </para>
732 </refsect1>
734 <refsect1 id="SQL-CREATETYPE-compatibility">
735 <title>Compatibility</title>
737 <para>
738 This <command>CREATE TYPE</command> command is a
739 <productname>PostgreSQL</productname> extension. There is a
740 <command>CREATE TYPE</command> statement in the <acronym>SQL</> standard
741 that is rather different in detail.
742 </para>
743 </refsect1>
745 <refsect1 id="SQL-CREATETYPE-see-also">
746 <title>See Also</title>
748 <simplelist type="inline">
749 <member><xref linkend="sql-createfunction" endterm="sql-createfunction-title"></member>
750 <member><xref linkend="sql-droptype" endterm="sql-droptype-title"></member>
751 <member><xref linkend="sql-altertype" endterm="sql-altertype-title"></member>
752 <member><xref linkend="sql-createdomain" endterm="sql-createdomain-title"></member>
753 </simplelist>
754 </refsect1>
756 </refentry>