{
    "content": [
        {
            "type": "text",
            "text": "# CREATE_TYPE(7) (man)\n\n**Summary:** CREATETYPE - define a new data type\n\n**Synopsis:** CREATE TYPE name AS\n( [ attributename datatype [ COLLATE collation ] [, ... ] ] )\nCREATE TYPE name AS ENUM\n( [ 'label' [, ... ] ] )\nCREATE TYPE name AS RANGE (\nSUBTYPE = subtype\n[ , SUBTYPEOPCLASS = subtypeoperatorclass ]\n[ , COLLATION = collation ]\n[ , CANONICAL = canonicalfunction ]\n[ , SUBTYPEDIFF = subtypedifffunction ]\n[ , MULTIRANGETYPENAME = multirangetypename ]\n)\nCREATE TYPE name (\nINPUT = inputfunction,\nOUTPUT = outputfunction\n[ , RECEIVE = receivefunction ]\n[ , SEND = sendfunction ]\n[ , TYPMODIN = typemodifierinputfunction ]\n[ , TYPMODOUT = typemodifieroutputfunction ]\n[ , ANALYZE = analyzefunction ]\n[ , SUBSCRIPT = subscriptfunction ]\n[ , INTERNALLENGTH = { internallength | VARIABLE } ]\n[ , PASSEDBYVALUE ]\n[ , ALIGNMENT = alignment ]\n[ , STORAGE = storage ]\n[ , LIKE = liketype ]\n[ , CATEGORY = category ]\n[ , PREFERRED = preferred ]\n[ , DEFAULT = default ]\n[ , ELEMENT = element ]\n[ , DELIMITER = delimiter ]\n[ , COLLATABLE = collatable ]\n)\nCREATE TYPE name\n\n## Examples\n\n- `This example creates a composite type and uses it in a function definition:`\n- `CREATE TYPE compfoo AS (f1 int, f2 text);`\n- `CREATE FUNCTION getfoo() RETURNS SETOF compfoo AS $$`\n- `SELECT fooid, fooname FROM foo`\n- `$$ LANGUAGE SQL;`\n- `This example creates an enumerated type and uses it in a table definition:`\n- `CREATE TYPE bugstatus AS ENUM ('new', 'open', 'closed');`\n- `CREATE TABLE bug (`\n- `id serial,`\n- `description text,`\n- `status bugstatus`\n- `);`\n- `This example creates a range type:`\n- `CREATE TYPE float8range AS RANGE (subtype = float8, subtypediff = float8mi);`\n- `This example creates the base data type box and then uses the type in a table definition:`\n- `CREATE TYPE box;`\n- `CREATE FUNCTION myboxinfunction(cstring) RETURNS box AS ... ;`\n- `CREATE FUNCTION myboxoutfunction(box) RETURNS cstring AS ... ;`\n- `CREATE TYPE box (`\n- `INTERNALLENGTH = 16,`\n- `INPUT = myboxinfunction,`\n- `OUTPUT = myboxoutfunction`\n- `);`\n- `CREATE TABLE myboxes (`\n- `id integer,`\n- `description box`\n- `);`\n- `If the internal structure of box were an array of four float4 elements, we might instead use:`\n- `CREATE TYPE box (`\n- `INTERNALLENGTH = 16,`\n- `INPUT = myboxinfunction,`\n- `OUTPUT = myboxoutfunction,`\n- `ELEMENT = float4`\n- `);`\n- `which would allow a box value's component numbers to be accessed by subscripting. Otherwise`\n- `the type behaves the same as before.`\n- `This example creates a large object type and uses it in a table definition:`\n- `CREATE TYPE bigobj (`\n- `INPUT = lofilein, OUTPUT = lofileout,`\n- `INTERNALLENGTH = VARIABLE`\n- `);`\n- `CREATE TABLE bigobjs (`\n- `id integer,`\n- `obj bigobj`\n- `);`\n- `More examples, including suitable input and output functions, are in Section 38.13.`\n\n## See Also\n\n- ALTERTYPE(7)\n- CREATEDOMAIN(7)\n- CREATEFUNCTION(7)\n- DROPTYPE(7)\n- TYPE(7)\n\n## Section Outline\n\n- **NAME** (2 lines)\n- **SYNOPSIS** (39 lines)\n- **DESCRIPTION** (15 lines) — 5 subsections\n  - Composite Types (9 lines)\n  - Enumerated Types (6 lines)\n  - Range Types (30 lines)\n  - Base Types (164 lines)\n  - Array Types (33 lines)\n- **PARAMETERS** (101 lines)\n- **NOTES** (29 lines)\n- **EXAMPLES** (65 lines)\n- **COMPATIBILITY** (7 lines)\n- **SEE ALSO** (6 lines)\n\n## Full Content\n\n### NAME\n\nCREATETYPE - define a new data type\n\n### SYNOPSIS\n\nCREATE TYPE name AS\n( [ attributename datatype [ COLLATE collation ] [, ... ] ] )\n\nCREATE TYPE name AS ENUM\n( [ 'label' [, ... ] ] )\n\nCREATE TYPE name AS RANGE (\nSUBTYPE = subtype\n[ , SUBTYPEOPCLASS = subtypeoperatorclass ]\n[ , COLLATION = collation ]\n[ , CANONICAL = canonicalfunction ]\n[ , SUBTYPEDIFF = subtypedifffunction ]\n[ , MULTIRANGETYPENAME = multirangetypename ]\n)\n\nCREATE TYPE name (\nINPUT = inputfunction,\nOUTPUT = outputfunction\n[ , RECEIVE = receivefunction ]\n[ , SEND = sendfunction ]\n[ , TYPMODIN = typemodifierinputfunction ]\n[ , TYPMODOUT = typemodifieroutputfunction ]\n[ , ANALYZE = analyzefunction ]\n[ , SUBSCRIPT = subscriptfunction ]\n[ , INTERNALLENGTH = { internallength | VARIABLE } ]\n[ , PASSEDBYVALUE ]\n[ , ALIGNMENT = alignment ]\n[ , STORAGE = storage ]\n[ , LIKE = liketype ]\n[ , CATEGORY = category ]\n[ , PREFERRED = preferred ]\n[ , DEFAULT = default ]\n[ , ELEMENT = element ]\n[ , DELIMITER = delimiter ]\n[ , COLLATABLE = collatable ]\n)\n\nCREATE TYPE name\n\n### DESCRIPTION\n\nCREATE TYPE registers a new data type for use in the current database. The user who defines a\ntype becomes its owner.\n\nIf a schema name is given then the type is created in the specified schema. Otherwise it is\ncreated in the current schema. The type name must be distinct from the name of any existing\ntype or domain in the same schema. (Because tables have associated data types, the type name\nmust also be distinct from the name of any existing table in the same schema.)\n\nThere are five forms of CREATE TYPE, as shown in the syntax synopsis above. They respectively\ncreate a composite type, an enum type, a range type, a base type, or a shell type. The first\nfour of these are discussed in turn below. A shell type is simply a placeholder for a type to\nbe defined later; it is created by issuing CREATE TYPE with no parameters except for the type\nname. Shell types are needed as forward references when creating range types and base types,\nas discussed in those sections.\n\n#### Composite Types\n\nThe first form of CREATE TYPE creates a composite type. The composite type is specified by a\nlist of attribute names and data types. An attribute's collation can be specified too, if its\ndata type is collatable. A composite type is essentially the same as the row type of a table,\nbut using CREATE TYPE avoids the need to create an actual table when all that is wanted is to\ndefine a type. A stand-alone composite type is useful, for example, as the argument or return\ntype of a function.\n\nTo be able to create a composite type, you must have USAGE privilege on all attribute types.\n\n#### Enumerated Types\n\nThe second form of CREATE TYPE creates an enumerated (enum) type, as described in\nSection 8.7. Enum types take a list of quoted labels, each of which must be less than\nNAMEDATALEN bytes long (64 bytes in a standard PostgreSQL build). (It is possible to create\nan enumerated type with zero labels, but such a type cannot be used to hold values before at\nleast one label is added using ALTER TYPE.)\n\n#### Range Types\n\nThe third form of CREATE TYPE creates a new range type, as described in Section 8.17.\n\nThe range type's subtype can be any type with an associated b-tree operator class (to\ndetermine the ordering of values for the range type). Normally the subtype's default b-tree\noperator class is used to determine ordering; to use a non-default operator class, specify\nits name with subtypeopclass. If the subtype is collatable, and you want to use a\nnon-default collation in the range's ordering, specify the desired collation with the\ncollation option.\n\nThe optional canonical function must take one argument of the range type being defined, and\nreturn a value of the same type. This is used to convert range values to a canonical form,\nwhen applicable. See Section 8.17.8 for more information. Creating a canonical function is a\nbit tricky, since it must be defined before the range type can be declared. To do this, you\nmust first create a shell type, which is a placeholder type that has no properties except a\nname and an owner. This is done by issuing the command CREATE TYPE name, with no additional\nparameters. Then the function can be declared using the shell type as argument and result,\nand finally the range type can be declared using the same name. This automatically replaces\nthe shell type entry with a valid range type.\n\nThe optional subtypediff function must take two values of the subtype type as argument, and\nreturn a double precision value representing the difference between the two given values.\nWhile this is optional, providing it allows much greater efficiency of GiST indexes on\ncolumns of the range type. See Section 8.17.8 for more information.\n\nThe optional multirangetypename parameter specifies the name of the corresponding\nmultirange type. If not specified, this name is chosen automatically as follows. If the range\ntype name contains the substring range, then the multirange type name is formed by\nreplacement of the range substring with multirange in the range type name. Otherwise, the\nmultirange type name is formed by appending a multirange suffix to the range type name.\n\n#### Base Types\n\nThe fourth form of CREATE TYPE creates a new base type (scalar type). To create a new base\ntype, you must be a superuser. (This restriction is made because an erroneous type definition\ncould confuse or even crash the server.)\n\nThe parameters can appear in any order, not only that illustrated above, and most are\noptional. You must register two or more functions (using CREATE FUNCTION) before defining the\ntype. The support functions inputfunction and outputfunction are required, while the\nfunctions receivefunction, sendfunction, typemodifierinputfunction,\ntypemodifieroutputfunction, analyzefunction, and subscriptfunction are optional.\nGenerally these functions have to be coded in C or another low-level language.\n\nThe inputfunction converts the type's external textual representation to the internal\nrepresentation used by the operators and functions defined for the type.  outputfunction\nperforms the reverse transformation. The input function can be declared as taking one\nargument of type cstring, or as taking three arguments of types cstring, oid, integer. The\nfirst argument is the input text as a C string, the second argument is the type's own OID\n(except for array types, which instead receive their element type's OID), and the third is\nthe typmod of the destination column, if known (-1 will be passed if not). The input function\nmust return a value of the data type itself. Usually, an input function should be declared\nSTRICT; if it is not, it will be called with a NULL first parameter when reading a NULL input\nvalue. The function must still return NULL in this case, unless it raises an error. (This\ncase is mainly meant to support domain input functions, which might need to reject NULL\ninputs.) The output function must be declared as taking one argument of the new data type.\nThe output function must return type cstring. Output functions are not invoked for NULL\nvalues.\n\nThe optional receivefunction converts the type's external binary representation to the\ninternal representation. If this function is not supplied, the type cannot participate in\nbinary input. The binary representation should be chosen to be cheap to convert to internal\nform, while being reasonably portable. (For example, the standard integer data types use\nnetwork byte order as the external binary representation, while the internal representation\nis in the machine's native byte order.) The receive function should perform adequate checking\nto ensure that the value is valid. The receive function can be declared as taking one\nargument of type internal, or as taking three arguments of types internal, oid, integer. The\nfirst argument is a pointer to a StringInfo buffer holding the received byte string; the\noptional arguments are the same as for the text input function. The receive function must\nreturn a value of the data type itself. Usually, a receive function should be declared\nSTRICT; if it is not, it will be called with a NULL first parameter when reading a NULL input\nvalue. The function must still return NULL in this case, unless it raises an error. (This\ncase is mainly meant to support domain receive functions, which might need to reject NULL\ninputs.) Similarly, the optional sendfunction converts from the internal representation to\nthe external binary representation. If this function is not supplied, the type cannot\nparticipate in binary output. The send function must be declared as taking one argument of\nthe new data type. The send function must return type bytea. Send functions are not invoked\nfor NULL values.\n\nYou should at this point be wondering how the input and output functions can be declared to\nhave results or arguments of the new type, when they have to be created before the new type\ncan be created. The answer is that the type should first be defined as a shell type, which is\na placeholder type that has no properties except a name and an owner. This is done by issuing\nthe command CREATE TYPE name, with no additional parameters. Then the C I/O functions can be\ndefined referencing the shell type. Finally, CREATE TYPE with a full definition replaces the\nshell entry with a complete, valid type definition, after which the new type can be used\nnormally.\n\nThe optional typemodifierinputfunction and typemodifieroutputfunction are needed if the\ntype supports modifiers, that is optional constraints attached to a type declaration, such as\nchar(5) or numeric(30,2).  PostgreSQL allows user-defined types to take one or more simple\nconstants or identifiers as modifiers. However, this information must be capable of being\npacked into a single non-negative integer value for storage in the system catalogs. The\ntypemodifierinputfunction is passed the declared modifier(s) in the form of a cstring\narray. It must check the values for validity (throwing an error if they are wrong), and if\nthey are correct, return a single non-negative integer value that will be stored as the\ncolumn “typmod”. Type modifiers will be rejected if the type does not have a\ntypemodifierinputfunction. The typemodifieroutputfunction converts the internal integer\ntypmod value back to the correct form for user display. It must return a cstring value that\nis the exact string to append to the type name; for example numeric's function might return\n(30,2). It is allowed to omit the typemodifieroutputfunction, in which case the default\ndisplay format is just the stored typmod integer value enclosed in parentheses.\n\nThe optional analyzefunction performs type-specific statistics collection for columns of the\ndata type. By default, ANALYZE will attempt to gather statistics using the type's “equals”\nand “less-than” operators, if there is a default b-tree operator class for the type. For\nnon-scalar types this behavior is likely to be unsuitable, so it can be overridden by\nspecifying a custom analysis function. The analysis function must be declared to take a\nsingle argument of type internal, and return a boolean result. The detailed API for analysis\nfunctions appears in src/include/commands/vacuum.h.\n\nThe optional subscriptfunction allows the data type to be subscripted in SQL commands.\nSpecifying this function does not cause the type to be considered a “true” array type; for\nexample, it will not be a candidate for the result type of ARRAY[] constructs. But if\nsubscripting a value of the type is a natural notation for extracting data from it, then a\nsubscriptfunction can be written to define what that means. The subscript function must be\ndeclared to take a single argument of type internal, and return an internal result, which is\na pointer to a struct of methods (functions) that implement subscripting. The detailed API\nfor subscript functions appears in src/include/nodes/subscripting.h. It may also be useful to\nread the array implementation in src/backend/utils/adt/arraysubs.c, or the simpler code in\ncontrib/hstore/hstoresubs.c. Additional information appears in Array Types below.\n\nWhile the details of the new type's internal representation are only known to the I/O\nfunctions and other functions you create to work with the type, there are several properties\nof the internal representation that must be declared to PostgreSQL. Foremost of these is\ninternallength. Base data types can be fixed-length, in which case internallength is a\npositive integer, or variable-length, indicated by setting internallength to VARIABLE.\n(Internally, this is represented by setting typlen to -1.) The internal representation of all\nvariable-length types must start with a 4-byte integer giving the total length of this value\nof the type. (Note that the length field is often encoded, as described in Section 70.2; it's\nunwise to access it directly.)\n\nThe optional flag PASSEDBYVALUE indicates that values of this data type are passed by value,\nrather than by reference. Types passed by value must be fixed-length, and their internal\nrepresentation cannot be larger than the size of the Datum type (4 bytes on some machines, 8\nbytes on others).\n\nThe alignment parameter specifies the storage alignment required for the data type. The\nallowed values equate to alignment on 1, 2, 4, or 8 byte boundaries. Note that\nvariable-length types must have an alignment of at least 4, since they necessarily contain an\nint4 as their first component.\n\nThe storage parameter allows selection of storage strategies for variable-length data types.\n(Only plain is allowed for fixed-length types.)  plain specifies that data of the type will\nalways be stored in-line and not compressed.  extended specifies that the system will first\ntry to compress a long data value, and will move the value out of the main table row if it's\nstill too long.  external allows the value to be moved out of the main table, but the system\nwill not try to compress it.  main allows compression, but discourages moving the value out\nof the main table. (Data items with this storage strategy might still be moved out of the\nmain table if there is no other way to make a row fit, but they will be kept in the main\ntable preferentially over extended and external items.)\n\nAll storage values other than plain imply that the functions of the data type can handle\nvalues that have been toasted, as described in Section 70.2 and Section 38.13.1. The specific\nother value given merely determines the default TOAST storage strategy for columns of a\ntoastable data type; users can pick other strategies for individual columns using ALTER TABLE\nSET STORAGE.\n\nThe liketype parameter provides an alternative method for specifying the basic\nrepresentation properties of a data type: copy them from some existing type. The values of\ninternallength, passedbyvalue, alignment, and storage are copied from the named type. (It is\npossible, though usually undesirable, to override some of these values by specifying them\nalong with the LIKE clause.) Specifying representation this way is especially useful when the\nlow-level implementation of the new type “piggybacks” on an existing type in some fashion.\n\nThe category and preferred parameters can be used to help control which implicit cast will be\napplied in ambiguous situations. Each data type belongs to a category named by a single ASCII\ncharacter, and each type is either “preferred” or not within its category. The parser will\nprefer casting to preferred types (but only from other types within the same category) when\nthis rule is helpful in resolving overloaded functions or operators. For more details see\nChapter 10. For types that have no implicit casts to or from any other types, it is\nsufficient to leave these settings at the defaults. However, for a group of related types\nthat have implicit casts, it is often helpful to mark them all as belonging to a category and\nselect one or two of the “most general” types as being preferred within the category. The\ncategory parameter is especially useful when adding a user-defined type to an existing\nbuilt-in category, such as the numeric or string types. However, it is also possible to\ncreate new entirely-user-defined type categories. Select any ASCII character other than an\nupper-case letter to name such a category.\n\nA default value can be specified, in case a user wants columns of the data type to default to\nsomething other than the null value. Specify the default with the DEFAULT key word. (Such a\ndefault can be overridden by an explicit DEFAULT clause attached to a particular column.)\n\nTo indicate that a type is a fixed-length array type, specify the type of the array elements\nusing the ELEMENT key word. For example, to define an array of 4-byte integers (int4),\nspecify ELEMENT = int4. For more details, see Array Types below.\n\nTo indicate the delimiter to be used between values in the external representation of arrays\nof this type, delimiter can be set to a specific character. The default delimiter is the\ncomma (,). Note that the delimiter is associated with the array element type, not the array\ntype itself.\n\nIf the optional Boolean parameter collatable is true, column definitions and expressions of\nthe type may carry collation information through use of the COLLATE clause. It is up to the\nimplementations of the functions operating on the type to actually make use of the collation\ninformation; this does not happen automatically merely by marking the type collatable.\n\n#### Array Types\n\nWhenever a user-defined type is created, PostgreSQL automatically creates an associated array\ntype, whose name consists of the element type's name prepended with an underscore, and\ntruncated if necessary to keep it less than NAMEDATALEN bytes long. (If the name so generated\ncollides with an existing type name, the process is repeated until a non-colliding name is\nfound.) This implicitly-created array type is variable length and uses the built-in input and\noutput functions arrayin and arrayout. Furthermore, this type is what the system uses for\nconstructs such as ARRAY[] over the user-defined type. The array type tracks any changes in\nits element type's owner or schema, and is dropped if the element type is.\n\nYou might reasonably ask why there is an ELEMENT option, if the system makes the correct\narray type automatically. The main case where it's useful to use ELEMENT is when you are\nmaking a fixed-length type that happens to be internally an array of a number of identical\nthings, and you want to allow these things to be accessed directly by subscripting, in\naddition to whatever operations you plan to provide for the type as a whole. For example,\ntype point is represented as just two floating-point numbers, which can be accessed using\npoint[0] and point[1]. Note that this facility only works for fixed-length types whose\ninternal form is exactly a sequence of identical fixed-length fields. For historical reasons\n(i.e., this is clearly wrong but it's far too late to change it), subscripting of\nfixed-length array types starts from zero, rather than from one as for variable-length\narrays.\n\nSpecifying the SUBSCRIPT option allows a data type to be subscripted, even though the system\ndoes not otherwise regard it as an array type. The behavior just described for fixed-length\narrays is actually implemented by the SUBSCRIPT handler function rawarraysubscripthandler,\nwhich is used automatically if you specify ELEMENT for a fixed-length type without also\nwriting SUBSCRIPT.\n\nWhen specifying a custom SUBSCRIPT function, it is not necessary to specify ELEMENT unless\nthe SUBSCRIPT handler function needs to consult typelem to find out what to return. Be aware\nthat specifying ELEMENT causes the system to assume that the new type contains, or is somehow\nphysically dependent on, the element type; thus for example changing properties of the\nelement type won't be allowed if there are any columns of the dependent type.\n\n### PARAMETERS\n\nname\nThe name (optionally schema-qualified) of a type to be created.\n\nattributename\nThe name of an attribute (column) for the composite type.\n\ndatatype\nThe name of an existing data type to become a column of the composite type.\n\ncollation\nThe name of an existing collation to be associated with a column of a composite type, or\nwith a range type.\n\nlabel\nA string literal representing the textual label associated with one value of an enum\ntype.\n\nsubtype\nThe name of the element type that the range type will represent ranges of.\n\nsubtypeoperatorclass\nThe name of a b-tree operator class for the subtype.\n\ncanonicalfunction\nThe name of the canonicalization function for the range type.\n\nsubtypedifffunction\nThe name of a difference function for the subtype.\n\nmultirangetypename\nThe name of the corresponding multirange type.\n\ninputfunction\nThe name of a function that converts data from the type's external textual form to its\ninternal form.\n\noutputfunction\nThe name of a function that converts data from the type's internal form to its external\ntextual form.\n\nreceivefunction\nThe name of a function that converts data from the type's external binary form to its\ninternal form.\n\nsendfunction\nThe name of a function that converts data from the type's internal form to its external\nbinary form.\n\ntypemodifierinputfunction\nThe name of a function that converts an array of modifier(s) for the type into internal\nform.\n\ntypemodifieroutputfunction\nThe name of a function that converts the internal form of the type's modifier(s) to\nexternal textual form.\n\nanalyzefunction\nThe name of a function that performs statistical analysis for the data type.\n\nsubscriptfunction\nThe name of a function that defines what subscripting a value of the data type does.\n\ninternallength\nA numeric constant that specifies the length in bytes of the new type's internal\nrepresentation. The default assumption is that it is variable-length.\n\nalignment\nThe storage alignment requirement of the data type. If specified, it must be char, int2,\nint4, or double; the default is int4.\n\nstorage\nThe storage strategy for the data type. If specified, must be plain, external, extended,\nor main; the default is plain.\n\nliketype\nThe name of an existing data type that the new type will have the same representation as.\nThe values of internallength, passedbyvalue, alignment, and storage are copied from that\ntype, unless overridden by explicit specification elsewhere in this CREATE TYPE command.\n\ncategory\nThe category code (a single ASCII character) for this type. The default is 'U' for\n“user-defined type”. Other standard category codes can be found in Table 52.63. You may\nalso choose other ASCII characters in order to create custom categories.\n\npreferred\nTrue if this type is a preferred type within its type category, else false. The default\nis false. Be very careful about creating a new preferred type within an existing type\ncategory, as this could cause surprising changes in behavior.\n\ndefault\nThe default value for the data type. If this is omitted, the default is null.\n\nelement\nThe type being created is an array; this specifies the type of the array elements.\n\ndelimiter\nThe delimiter character to be used between values in arrays made of this type.\n\ncollatable\nTrue if this type's operations can use collation information. The default is false.\n\n### NOTES\n\nBecause there are no restrictions on use of a data type once it's been created, creating a\nbase type or range type is tantamount to granting public execute permission on the functions\nmentioned in the type definition. This is usually not an issue for the sorts of functions\nthat are useful in a type definition. But you might want to think twice before designing a\ntype in a way that would require “secret” information to be used while converting it to or\nfrom external form.\n\nBefore PostgreSQL version 8.3, the name of a generated array type was always exactly the\nelement type's name with one underscore character () prepended. (Type names were therefore\nrestricted in length to one fewer character than other names.) While this is still usually\nthe case, the array type name may vary from this in case of maximum-length names or\ncollisions with user type names that begin with underscore. Writing code that depends on this\nconvention is therefore deprecated. Instead, use pgtype.typarray to locate the array type\nassociated with a given type.\n\nIt may be advisable to avoid using type and table names that begin with underscore. While the\nserver will change generated array type names to avoid collisions with user-given names,\nthere is still risk of confusion, particularly with old client software that may assume that\ntype names beginning with underscores always represent arrays.\n\nBefore PostgreSQL version 8.2, the shell-type creation syntax CREATE TYPE name did not exist.\nThe way to create a new base type was to create its input function first. In this approach,\nPostgreSQL will first see the name of the new data type as the return type of the input\nfunction. The shell type is implicitly created in this situation, and then it can be\nreferenced in the definitions of the remaining I/O functions. This approach still works, but\nis deprecated and might be disallowed in some future release. Also, to avoid accidentally\ncluttering the catalogs with shell types as a result of simple typos in function definitions,\na shell type will only be made this way when the input function is written in C.\n\n### EXAMPLES\n\nThis example creates a composite type and uses it in a function definition:\n\nCREATE TYPE compfoo AS (f1 int, f2 text);\n\nCREATE FUNCTION getfoo() RETURNS SETOF compfoo AS $$\nSELECT fooid, fooname FROM foo\n$$ LANGUAGE SQL;\n\nThis example creates an enumerated type and uses it in a table definition:\n\nCREATE TYPE bugstatus AS ENUM ('new', 'open', 'closed');\n\nCREATE TABLE bug (\nid serial,\ndescription text,\nstatus bugstatus\n);\n\nThis example creates a range type:\n\nCREATE TYPE float8range AS RANGE (subtype = float8, subtypediff = float8mi);\n\nThis example creates the base data type box and then uses the type in a table definition:\n\nCREATE TYPE box;\n\nCREATE FUNCTION myboxinfunction(cstring) RETURNS box AS ... ;\nCREATE FUNCTION myboxoutfunction(box) RETURNS cstring AS ... ;\n\nCREATE TYPE box (\nINTERNALLENGTH = 16,\nINPUT = myboxinfunction,\nOUTPUT = myboxoutfunction\n);\n\nCREATE TABLE myboxes (\nid integer,\ndescription box\n);\n\nIf the internal structure of box were an array of four float4 elements, we might instead use:\n\nCREATE TYPE box (\nINTERNALLENGTH = 16,\nINPUT = myboxinfunction,\nOUTPUT = myboxoutfunction,\nELEMENT = float4\n);\n\nwhich would allow a box value's component numbers to be accessed by subscripting. Otherwise\nthe type behaves the same as before.\n\nThis example creates a large object type and uses it in a table definition:\n\nCREATE TYPE bigobj (\nINPUT = lofilein, OUTPUT = lofileout,\nINTERNALLENGTH = VARIABLE\n);\nCREATE TABLE bigobjs (\nid integer,\nobj bigobj\n);\n\nMore examples, including suitable input and output functions, are in Section 38.13.\n\n### COMPATIBILITY\n\nThe first form of the CREATE TYPE command, which creates a composite type, conforms to the\nSQL standard. The other forms are PostgreSQL extensions. The CREATE TYPE statement in the SQL\nstandard also defines other forms that are not implemented in PostgreSQL.\n\nThe ability to create a composite type with zero attributes is a PostgreSQL-specific\ndeviation from the standard (analogous to the same case in CREATE TABLE).\n\n### SEE ALSO\n\nALTER TYPE (ALTERTYPE(7)), CREATE DOMAIN (CREATEDOMAIN(7)), CREATE FUNCTION\n(CREATEFUNCTION(7)), DROP TYPE (DROPTYPE(7))\n\n\n\nPostgreSQL 14.23                                2026                                  CREATE TYPE(7)\n\n"
        }
    ],
    "structuredContent": {
        "command": "CREATE_TYPE",
        "section": "7",
        "mode": "man",
        "summary": "CREATETYPE - define a new data type",
        "synopsis": "CREATE TYPE name AS\n( [ attributename datatype [ COLLATE collation ] [, ... ] ] )\nCREATE TYPE name AS ENUM\n( [ 'label' [, ... ] ] )\nCREATE TYPE name AS RANGE (\nSUBTYPE = subtype\n[ , SUBTYPEOPCLASS = subtypeoperatorclass ]\n[ , COLLATION = collation ]\n[ , CANONICAL = canonicalfunction ]\n[ , SUBTYPEDIFF = subtypedifffunction ]\n[ , MULTIRANGETYPENAME = multirangetypename ]\n)\nCREATE TYPE name (\nINPUT = inputfunction,\nOUTPUT = outputfunction\n[ , RECEIVE = receivefunction ]\n[ , SEND = sendfunction ]\n[ , TYPMODIN = typemodifierinputfunction ]\n[ , TYPMODOUT = typemodifieroutputfunction ]\n[ , ANALYZE = analyzefunction ]\n[ , SUBSCRIPT = subscriptfunction ]\n[ , INTERNALLENGTH = { internallength | VARIABLE } ]\n[ , PASSEDBYVALUE ]\n[ , ALIGNMENT = alignment ]\n[ , STORAGE = storage ]\n[ , LIKE = liketype ]\n[ , CATEGORY = category ]\n[ , PREFERRED = preferred ]\n[ , DEFAULT = default ]\n[ , ELEMENT = element ]\n[ , DELIMITER = delimiter ]\n[ , COLLATABLE = collatable ]\n)\nCREATE TYPE name",
        "tldr_summary": null,
        "tldr_examples": [],
        "tldr_source": null,
        "flags": [],
        "examples": [
            "This example creates a composite type and uses it in a function definition:",
            "CREATE TYPE compfoo AS (f1 int, f2 text);",
            "CREATE FUNCTION getfoo() RETURNS SETOF compfoo AS $$",
            "SELECT fooid, fooname FROM foo",
            "$$ LANGUAGE SQL;",
            "This example creates an enumerated type and uses it in a table definition:",
            "CREATE TYPE bugstatus AS ENUM ('new', 'open', 'closed');",
            "CREATE TABLE bug (",
            "id serial,",
            "description text,",
            "status bugstatus",
            ");",
            "This example creates a range type:",
            "CREATE TYPE float8range AS RANGE (subtype = float8, subtypediff = float8mi);",
            "This example creates the base data type box and then uses the type in a table definition:",
            "CREATE TYPE box;",
            "CREATE FUNCTION myboxinfunction(cstring) RETURNS box AS ... ;",
            "CREATE FUNCTION myboxoutfunction(box) RETURNS cstring AS ... ;",
            "CREATE TYPE box (",
            "INTERNALLENGTH = 16,",
            "INPUT = myboxinfunction,",
            "OUTPUT = myboxoutfunction",
            ");",
            "CREATE TABLE myboxes (",
            "id integer,",
            "description box",
            ");",
            "If the internal structure of box were an array of four float4 elements, we might instead use:",
            "CREATE TYPE box (",
            "INTERNALLENGTH = 16,",
            "INPUT = myboxinfunction,",
            "OUTPUT = myboxoutfunction,",
            "ELEMENT = float4",
            ");",
            "which would allow a box value's component numbers to be accessed by subscripting. Otherwise",
            "the type behaves the same as before.",
            "This example creates a large object type and uses it in a table definition:",
            "CREATE TYPE bigobj (",
            "INPUT = lofilein, OUTPUT = lofileout,",
            "INTERNALLENGTH = VARIABLE",
            ");",
            "CREATE TABLE bigobjs (",
            "id integer,",
            "obj bigobj",
            ");",
            "More examples, including suitable input and output functions, are in Section 38.13."
        ],
        "see_also": [
            {
                "name": "ALTERTYPE",
                "section": "7",
                "url": "https://www.chedong.com/phpMan.php/man/ALTERTYPE/7/json"
            },
            {
                "name": "CREATEDOMAIN",
                "section": "7",
                "url": "https://www.chedong.com/phpMan.php/man/CREATEDOMAIN/7/json"
            },
            {
                "name": "CREATEFUNCTION",
                "section": "7",
                "url": "https://www.chedong.com/phpMan.php/man/CREATEFUNCTION/7/json"
            },
            {
                "name": "DROPTYPE",
                "section": "7",
                "url": "https://www.chedong.com/phpMan.php/man/DROPTYPE/7/json"
            },
            {
                "name": "TYPE",
                "section": "7",
                "url": "https://www.chedong.com/phpMan.php/man/TYPE/7/json"
            }
        ],
        "section_outline": [
            {
                "name": "NAME",
                "lines": 2,
                "subsections": []
            },
            {
                "name": "SYNOPSIS",
                "lines": 39,
                "subsections": []
            },
            {
                "name": "DESCRIPTION",
                "lines": 15,
                "subsections": [
                    {
                        "name": "Composite Types",
                        "lines": 9
                    },
                    {
                        "name": "Enumerated Types",
                        "lines": 6
                    },
                    {
                        "name": "Range Types",
                        "lines": 30
                    },
                    {
                        "name": "Base Types",
                        "lines": 164
                    },
                    {
                        "name": "Array Types",
                        "lines": 33
                    }
                ]
            },
            {
                "name": "PARAMETERS",
                "lines": 101,
                "subsections": []
            },
            {
                "name": "NOTES",
                "lines": 29,
                "subsections": []
            },
            {
                "name": "EXAMPLES",
                "lines": 65,
                "subsections": []
            },
            {
                "name": "COMPATIBILITY",
                "lines": 7,
                "subsections": []
            },
            {
                "name": "SEE ALSO",
                "lines": 6,
                "subsections": []
            }
        ],
        "sections": {
            "NAME": {
                "content": "CREATETYPE - define a new data type\n",
                "subsections": []
            },
            "SYNOPSIS": {
                "content": "CREATE TYPE name AS\n( [ attributename datatype [ COLLATE collation ] [, ... ] ] )\n\nCREATE TYPE name AS ENUM\n( [ 'label' [, ... ] ] )\n\nCREATE TYPE name AS RANGE (\nSUBTYPE = subtype\n[ , SUBTYPEOPCLASS = subtypeoperatorclass ]\n[ , COLLATION = collation ]\n[ , CANONICAL = canonicalfunction ]\n[ , SUBTYPEDIFF = subtypedifffunction ]\n[ , MULTIRANGETYPENAME = multirangetypename ]\n)\n\nCREATE TYPE name (\nINPUT = inputfunction,\nOUTPUT = outputfunction\n[ , RECEIVE = receivefunction ]\n[ , SEND = sendfunction ]\n[ , TYPMODIN = typemodifierinputfunction ]\n[ , TYPMODOUT = typemodifieroutputfunction ]\n[ , ANALYZE = analyzefunction ]\n[ , SUBSCRIPT = subscriptfunction ]\n[ , INTERNALLENGTH = { internallength | VARIABLE } ]\n[ , PASSEDBYVALUE ]\n[ , ALIGNMENT = alignment ]\n[ , STORAGE = storage ]\n[ , LIKE = liketype ]\n[ , CATEGORY = category ]\n[ , PREFERRED = preferred ]\n[ , DEFAULT = default ]\n[ , ELEMENT = element ]\n[ , DELIMITER = delimiter ]\n[ , COLLATABLE = collatable ]\n)\n\nCREATE TYPE name\n",
                "subsections": []
            },
            "DESCRIPTION": {
                "content": "CREATE TYPE registers a new data type for use in the current database. The user who defines a\ntype becomes its owner.\n\nIf a schema name is given then the type is created in the specified schema. Otherwise it is\ncreated in the current schema. The type name must be distinct from the name of any existing\ntype or domain in the same schema. (Because tables have associated data types, the type name\nmust also be distinct from the name of any existing table in the same schema.)\n\nThere are five forms of CREATE TYPE, as shown in the syntax synopsis above. They respectively\ncreate a composite type, an enum type, a range type, a base type, or a shell type. The first\nfour of these are discussed in turn below. A shell type is simply a placeholder for a type to\nbe defined later; it is created by issuing CREATE TYPE with no parameters except for the type\nname. Shell types are needed as forward references when creating range types and base types,\nas discussed in those sections.\n",
                "subsections": [
                    {
                        "name": "Composite Types",
                        "content": "The first form of CREATE TYPE creates a composite type. The composite type is specified by a\nlist of attribute names and data types. An attribute's collation can be specified too, if its\ndata type is collatable. A composite type is essentially the same as the row type of a table,\nbut using CREATE TYPE avoids the need to create an actual table when all that is wanted is to\ndefine a type. A stand-alone composite type is useful, for example, as the argument or return\ntype of a function.\n\nTo be able to create a composite type, you must have USAGE privilege on all attribute types.\n"
                    },
                    {
                        "name": "Enumerated Types",
                        "content": "The second form of CREATE TYPE creates an enumerated (enum) type, as described in\nSection 8.7. Enum types take a list of quoted labels, each of which must be less than\nNAMEDATALEN bytes long (64 bytes in a standard PostgreSQL build). (It is possible to create\nan enumerated type with zero labels, but such a type cannot be used to hold values before at\nleast one label is added using ALTER TYPE.)\n"
                    },
                    {
                        "name": "Range Types",
                        "content": "The third form of CREATE TYPE creates a new range type, as described in Section 8.17.\n\nThe range type's subtype can be any type with an associated b-tree operator class (to\ndetermine the ordering of values for the range type). Normally the subtype's default b-tree\noperator class is used to determine ordering; to use a non-default operator class, specify\nits name with subtypeopclass. If the subtype is collatable, and you want to use a\nnon-default collation in the range's ordering, specify the desired collation with the\ncollation option.\n\nThe optional canonical function must take one argument of the range type being defined, and\nreturn a value of the same type. This is used to convert range values to a canonical form,\nwhen applicable. See Section 8.17.8 for more information. Creating a canonical function is a\nbit tricky, since it must be defined before the range type can be declared. To do this, you\nmust first create a shell type, which is a placeholder type that has no properties except a\nname and an owner. This is done by issuing the command CREATE TYPE name, with no additional\nparameters. Then the function can be declared using the shell type as argument and result,\nand finally the range type can be declared using the same name. This automatically replaces\nthe shell type entry with a valid range type.\n\nThe optional subtypediff function must take two values of the subtype type as argument, and\nreturn a double precision value representing the difference between the two given values.\nWhile this is optional, providing it allows much greater efficiency of GiST indexes on\ncolumns of the range type. See Section 8.17.8 for more information.\n\nThe optional multirangetypename parameter specifies the name of the corresponding\nmultirange type. If not specified, this name is chosen automatically as follows. If the range\ntype name contains the substring range, then the multirange type name is formed by\nreplacement of the range substring with multirange in the range type name. Otherwise, the\nmultirange type name is formed by appending a multirange suffix to the range type name.\n"
                    },
                    {
                        "name": "Base Types",
                        "content": "The fourth form of CREATE TYPE creates a new base type (scalar type). To create a new base\ntype, you must be a superuser. (This restriction is made because an erroneous type definition\ncould confuse or even crash the server.)\n\nThe parameters can appear in any order, not only that illustrated above, and most are\noptional. You must register two or more functions (using CREATE FUNCTION) before defining the\ntype. The support functions inputfunction and outputfunction are required, while the\nfunctions receivefunction, sendfunction, typemodifierinputfunction,\ntypemodifieroutputfunction, analyzefunction, and subscriptfunction are optional.\nGenerally these functions have to be coded in C or another low-level language.\n\nThe inputfunction converts the type's external textual representation to the internal\nrepresentation used by the operators and functions defined for the type.  outputfunction\nperforms the reverse transformation. The input function can be declared as taking one\nargument of type cstring, or as taking three arguments of types cstring, oid, integer. The\nfirst argument is the input text as a C string, the second argument is the type's own OID\n(except for array types, which instead receive their element type's OID), and the third is\nthe typmod of the destination column, if known (-1 will be passed if not). The input function\nmust return a value of the data type itself. Usually, an input function should be declared\nSTRICT; if it is not, it will be called with a NULL first parameter when reading a NULL input\nvalue. The function must still return NULL in this case, unless it raises an error. (This\ncase is mainly meant to support domain input functions, which might need to reject NULL\ninputs.) The output function must be declared as taking one argument of the new data type.\nThe output function must return type cstring. Output functions are not invoked for NULL\nvalues.\n\nThe optional receivefunction converts the type's external binary representation to the\ninternal representation. If this function is not supplied, the type cannot participate in\nbinary input. The binary representation should be chosen to be cheap to convert to internal\nform, while being reasonably portable. (For example, the standard integer data types use\nnetwork byte order as the external binary representation, while the internal representation\nis in the machine's native byte order.) The receive function should perform adequate checking\nto ensure that the value is valid. The receive function can be declared as taking one\nargument of type internal, or as taking three arguments of types internal, oid, integer. The\nfirst argument is a pointer to a StringInfo buffer holding the received byte string; the\noptional arguments are the same as for the text input function. The receive function must\nreturn a value of the data type itself. Usually, a receive function should be declared\nSTRICT; if it is not, it will be called with a NULL first parameter when reading a NULL input\nvalue. The function must still return NULL in this case, unless it raises an error. (This\ncase is mainly meant to support domain receive functions, which might need to reject NULL\ninputs.) Similarly, the optional sendfunction converts from the internal representation to\nthe external binary representation. If this function is not supplied, the type cannot\nparticipate in binary output. The send function must be declared as taking one argument of\nthe new data type. The send function must return type bytea. Send functions are not invoked\nfor NULL values.\n\nYou should at this point be wondering how the input and output functions can be declared to\nhave results or arguments of the new type, when they have to be created before the new type\ncan be created. The answer is that the type should first be defined as a shell type, which is\na placeholder type that has no properties except a name and an owner. This is done by issuing\nthe command CREATE TYPE name, with no additional parameters. Then the C I/O functions can be\ndefined referencing the shell type. Finally, CREATE TYPE with a full definition replaces the\nshell entry with a complete, valid type definition, after which the new type can be used\nnormally.\n\nThe optional typemodifierinputfunction and typemodifieroutputfunction are needed if the\ntype supports modifiers, that is optional constraints attached to a type declaration, such as\nchar(5) or numeric(30,2).  PostgreSQL allows user-defined types to take one or more simple\nconstants or identifiers as modifiers. However, this information must be capable of being\npacked into a single non-negative integer value for storage in the system catalogs. The\ntypemodifierinputfunction is passed the declared modifier(s) in the form of a cstring\narray. It must check the values for validity (throwing an error if they are wrong), and if\nthey are correct, return a single non-negative integer value that will be stored as the\ncolumn “typmod”. Type modifiers will be rejected if the type does not have a\ntypemodifierinputfunction. The typemodifieroutputfunction converts the internal integer\ntypmod value back to the correct form for user display. It must return a cstring value that\nis the exact string to append to the type name; for example numeric's function might return\n(30,2). It is allowed to omit the typemodifieroutputfunction, in which case the default\ndisplay format is just the stored typmod integer value enclosed in parentheses.\n\nThe optional analyzefunction performs type-specific statistics collection for columns of the\ndata type. By default, ANALYZE will attempt to gather statistics using the type's “equals”\nand “less-than” operators, if there is a default b-tree operator class for the type. For\nnon-scalar types this behavior is likely to be unsuitable, so it can be overridden by\nspecifying a custom analysis function. The analysis function must be declared to take a\nsingle argument of type internal, and return a boolean result. The detailed API for analysis\nfunctions appears in src/include/commands/vacuum.h.\n\nThe optional subscriptfunction allows the data type to be subscripted in SQL commands.\nSpecifying this function does not cause the type to be considered a “true” array type; for\nexample, it will not be a candidate for the result type of ARRAY[] constructs. But if\nsubscripting a value of the type is a natural notation for extracting data from it, then a\nsubscriptfunction can be written to define what that means. The subscript function must be\ndeclared to take a single argument of type internal, and return an internal result, which is\na pointer to a struct of methods (functions) that implement subscripting. The detailed API\nfor subscript functions appears in src/include/nodes/subscripting.h. It may also be useful to\nread the array implementation in src/backend/utils/adt/arraysubs.c, or the simpler code in\ncontrib/hstore/hstoresubs.c. Additional information appears in Array Types below.\n\nWhile the details of the new type's internal representation are only known to the I/O\nfunctions and other functions you create to work with the type, there are several properties\nof the internal representation that must be declared to PostgreSQL. Foremost of these is\ninternallength. Base data types can be fixed-length, in which case internallength is a\npositive integer, or variable-length, indicated by setting internallength to VARIABLE.\n(Internally, this is represented by setting typlen to -1.) The internal representation of all\nvariable-length types must start with a 4-byte integer giving the total length of this value\nof the type. (Note that the length field is often encoded, as described in Section 70.2; it's\nunwise to access it directly.)\n\nThe optional flag PASSEDBYVALUE indicates that values of this data type are passed by value,\nrather than by reference. Types passed by value must be fixed-length, and their internal\nrepresentation cannot be larger than the size of the Datum type (4 bytes on some machines, 8\nbytes on others).\n\nThe alignment parameter specifies the storage alignment required for the data type. The\nallowed values equate to alignment on 1, 2, 4, or 8 byte boundaries. Note that\nvariable-length types must have an alignment of at least 4, since they necessarily contain an\nint4 as their first component.\n\nThe storage parameter allows selection of storage strategies for variable-length data types.\n(Only plain is allowed for fixed-length types.)  plain specifies that data of the type will\nalways be stored in-line and not compressed.  extended specifies that the system will first\ntry to compress a long data value, and will move the value out of the main table row if it's\nstill too long.  external allows the value to be moved out of the main table, but the system\nwill not try to compress it.  main allows compression, but discourages moving the value out\nof the main table. (Data items with this storage strategy might still be moved out of the\nmain table if there is no other way to make a row fit, but they will be kept in the main\ntable preferentially over extended and external items.)\n\nAll storage values other than plain imply that the functions of the data type can handle\nvalues that have been toasted, as described in Section 70.2 and Section 38.13.1. The specific\nother value given merely determines the default TOAST storage strategy for columns of a\ntoastable data type; users can pick other strategies for individual columns using ALTER TABLE\nSET STORAGE.\n\nThe liketype parameter provides an alternative method for specifying the basic\nrepresentation properties of a data type: copy them from some existing type. The values of\ninternallength, passedbyvalue, alignment, and storage are copied from the named type. (It is\npossible, though usually undesirable, to override some of these values by specifying them\nalong with the LIKE clause.) Specifying representation this way is especially useful when the\nlow-level implementation of the new type “piggybacks” on an existing type in some fashion.\n\nThe category and preferred parameters can be used to help control which implicit cast will be\napplied in ambiguous situations. Each data type belongs to a category named by a single ASCII\ncharacter, and each type is either “preferred” or not within its category. The parser will\nprefer casting to preferred types (but only from other types within the same category) when\nthis rule is helpful in resolving overloaded functions or operators. For more details see\nChapter 10. For types that have no implicit casts to or from any other types, it is\nsufficient to leave these settings at the defaults. However, for a group of related types\nthat have implicit casts, it is often helpful to mark them all as belonging to a category and\nselect one or two of the “most general” types as being preferred within the category. The\ncategory parameter is especially useful when adding a user-defined type to an existing\nbuilt-in category, such as the numeric or string types. However, it is also possible to\ncreate new entirely-user-defined type categories. Select any ASCII character other than an\nupper-case letter to name such a category.\n\nA default value can be specified, in case a user wants columns of the data type to default to\nsomething other than the null value. Specify the default with the DEFAULT key word. (Such a\ndefault can be overridden by an explicit DEFAULT clause attached to a particular column.)\n\nTo indicate that a type is a fixed-length array type, specify the type of the array elements\nusing the ELEMENT key word. For example, to define an array of 4-byte integers (int4),\nspecify ELEMENT = int4. For more details, see Array Types below.\n\nTo indicate the delimiter to be used between values in the external representation of arrays\nof this type, delimiter can be set to a specific character. The default delimiter is the\ncomma (,). Note that the delimiter is associated with the array element type, not the array\ntype itself.\n\nIf the optional Boolean parameter collatable is true, column definitions and expressions of\nthe type may carry collation information through use of the COLLATE clause. It is up to the\nimplementations of the functions operating on the type to actually make use of the collation\ninformation; this does not happen automatically merely by marking the type collatable.\n"
                    },
                    {
                        "name": "Array Types",
                        "content": "Whenever a user-defined type is created, PostgreSQL automatically creates an associated array\ntype, whose name consists of the element type's name prepended with an underscore, and\ntruncated if necessary to keep it less than NAMEDATALEN bytes long. (If the name so generated\ncollides with an existing type name, the process is repeated until a non-colliding name is\nfound.) This implicitly-created array type is variable length and uses the built-in input and\noutput functions arrayin and arrayout. Furthermore, this type is what the system uses for\nconstructs such as ARRAY[] over the user-defined type. The array type tracks any changes in\nits element type's owner or schema, and is dropped if the element type is.\n\nYou might reasonably ask why there is an ELEMENT option, if the system makes the correct\narray type automatically. The main case where it's useful to use ELEMENT is when you are\nmaking a fixed-length type that happens to be internally an array of a number of identical\nthings, and you want to allow these things to be accessed directly by subscripting, in\naddition to whatever operations you plan to provide for the type as a whole. For example,\ntype point is represented as just two floating-point numbers, which can be accessed using\npoint[0] and point[1]. Note that this facility only works for fixed-length types whose\ninternal form is exactly a sequence of identical fixed-length fields. For historical reasons\n(i.e., this is clearly wrong but it's far too late to change it), subscripting of\nfixed-length array types starts from zero, rather than from one as for variable-length\narrays.\n\nSpecifying the SUBSCRIPT option allows a data type to be subscripted, even though the system\ndoes not otherwise regard it as an array type. The behavior just described for fixed-length\narrays is actually implemented by the SUBSCRIPT handler function rawarraysubscripthandler,\nwhich is used automatically if you specify ELEMENT for a fixed-length type without also\nwriting SUBSCRIPT.\n\nWhen specifying a custom SUBSCRIPT function, it is not necessary to specify ELEMENT unless\nthe SUBSCRIPT handler function needs to consult typelem to find out what to return. Be aware\nthat specifying ELEMENT causes the system to assume that the new type contains, or is somehow\nphysically dependent on, the element type; thus for example changing properties of the\nelement type won't be allowed if there are any columns of the dependent type.\n"
                    }
                ]
            },
            "PARAMETERS": {
                "content": "name\nThe name (optionally schema-qualified) of a type to be created.\n\nattributename\nThe name of an attribute (column) for the composite type.\n\ndatatype\nThe name of an existing data type to become a column of the composite type.\n\ncollation\nThe name of an existing collation to be associated with a column of a composite type, or\nwith a range type.\n\nlabel\nA string literal representing the textual label associated with one value of an enum\ntype.\n\nsubtype\nThe name of the element type that the range type will represent ranges of.\n\nsubtypeoperatorclass\nThe name of a b-tree operator class for the subtype.\n\ncanonicalfunction\nThe name of the canonicalization function for the range type.\n\nsubtypedifffunction\nThe name of a difference function for the subtype.\n\nmultirangetypename\nThe name of the corresponding multirange type.\n\ninputfunction\nThe name of a function that converts data from the type's external textual form to its\ninternal form.\n\noutputfunction\nThe name of a function that converts data from the type's internal form to its external\ntextual form.\n\nreceivefunction\nThe name of a function that converts data from the type's external binary form to its\ninternal form.\n\nsendfunction\nThe name of a function that converts data from the type's internal form to its external\nbinary form.\n\ntypemodifierinputfunction\nThe name of a function that converts an array of modifier(s) for the type into internal\nform.\n\ntypemodifieroutputfunction\nThe name of a function that converts the internal form of the type's modifier(s) to\nexternal textual form.\n\nanalyzefunction\nThe name of a function that performs statistical analysis for the data type.\n\nsubscriptfunction\nThe name of a function that defines what subscripting a value of the data type does.\n\ninternallength\nA numeric constant that specifies the length in bytes of the new type's internal\nrepresentation. The default assumption is that it is variable-length.\n\nalignment\nThe storage alignment requirement of the data type. If specified, it must be char, int2,\nint4, or double; the default is int4.\n\nstorage\nThe storage strategy for the data type. If specified, must be plain, external, extended,\nor main; the default is plain.\n\nliketype\nThe name of an existing data type that the new type will have the same representation as.\nThe values of internallength, passedbyvalue, alignment, and storage are copied from that\ntype, unless overridden by explicit specification elsewhere in this CREATE TYPE command.\n\ncategory\nThe category code (a single ASCII character) for this type. The default is 'U' for\n“user-defined type”. Other standard category codes can be found in Table 52.63. You may\nalso choose other ASCII characters in order to create custom categories.\n\npreferred\nTrue if this type is a preferred type within its type category, else false. The default\nis false. Be very careful about creating a new preferred type within an existing type\ncategory, as this could cause surprising changes in behavior.\n\ndefault\nThe default value for the data type. If this is omitted, the default is null.\n\nelement\nThe type being created is an array; this specifies the type of the array elements.\n\ndelimiter\nThe delimiter character to be used between values in arrays made of this type.\n\ncollatable\nTrue if this type's operations can use collation information. The default is false.\n",
                "subsections": []
            },
            "NOTES": {
                "content": "Because there are no restrictions on use of a data type once it's been created, creating a\nbase type or range type is tantamount to granting public execute permission on the functions\nmentioned in the type definition. This is usually not an issue for the sorts of functions\nthat are useful in a type definition. But you might want to think twice before designing a\ntype in a way that would require “secret” information to be used while converting it to or\nfrom external form.\n\nBefore PostgreSQL version 8.3, the name of a generated array type was always exactly the\nelement type's name with one underscore character () prepended. (Type names were therefore\nrestricted in length to one fewer character than other names.) While this is still usually\nthe case, the array type name may vary from this in case of maximum-length names or\ncollisions with user type names that begin with underscore. Writing code that depends on this\nconvention is therefore deprecated. Instead, use pgtype.typarray to locate the array type\nassociated with a given type.\n\nIt may be advisable to avoid using type and table names that begin with underscore. While the\nserver will change generated array type names to avoid collisions with user-given names,\nthere is still risk of confusion, particularly with old client software that may assume that\ntype names beginning with underscores always represent arrays.\n\nBefore PostgreSQL version 8.2, the shell-type creation syntax CREATE TYPE name did not exist.\nThe way to create a new base type was to create its input function first. In this approach,\nPostgreSQL will first see the name of the new data type as the return type of the input\nfunction. The shell type is implicitly created in this situation, and then it can be\nreferenced in the definitions of the remaining I/O functions. This approach still works, but\nis deprecated and might be disallowed in some future release. Also, to avoid accidentally\ncluttering the catalogs with shell types as a result of simple typos in function definitions,\na shell type will only be made this way when the input function is written in C.\n",
                "subsections": []
            },
            "EXAMPLES": {
                "content": "This example creates a composite type and uses it in a function definition:\n\nCREATE TYPE compfoo AS (f1 int, f2 text);\n\nCREATE FUNCTION getfoo() RETURNS SETOF compfoo AS $$\nSELECT fooid, fooname FROM foo\n$$ LANGUAGE SQL;\n\nThis example creates an enumerated type and uses it in a table definition:\n\nCREATE TYPE bugstatus AS ENUM ('new', 'open', 'closed');\n\nCREATE TABLE bug (\nid serial,\ndescription text,\nstatus bugstatus\n);\n\nThis example creates a range type:\n\nCREATE TYPE float8range AS RANGE (subtype = float8, subtypediff = float8mi);\n\nThis example creates the base data type box and then uses the type in a table definition:\n\nCREATE TYPE box;\n\nCREATE FUNCTION myboxinfunction(cstring) RETURNS box AS ... ;\nCREATE FUNCTION myboxoutfunction(box) RETURNS cstring AS ... ;\n\nCREATE TYPE box (\nINTERNALLENGTH = 16,\nINPUT = myboxinfunction,\nOUTPUT = myboxoutfunction\n);\n\nCREATE TABLE myboxes (\nid integer,\ndescription box\n);\n\nIf the internal structure of box were an array of four float4 elements, we might instead use:\n\nCREATE TYPE box (\nINTERNALLENGTH = 16,\nINPUT = myboxinfunction,\nOUTPUT = myboxoutfunction,\nELEMENT = float4\n);\n\nwhich would allow a box value's component numbers to be accessed by subscripting. Otherwise\nthe type behaves the same as before.\n\nThis example creates a large object type and uses it in a table definition:\n\nCREATE TYPE bigobj (\nINPUT = lofilein, OUTPUT = lofileout,\nINTERNALLENGTH = VARIABLE\n);\nCREATE TABLE bigobjs (\nid integer,\nobj bigobj\n);\n\nMore examples, including suitable input and output functions, are in Section 38.13.\n",
                "subsections": []
            },
            "COMPATIBILITY": {
                "content": "The first form of the CREATE TYPE command, which creates a composite type, conforms to the\nSQL standard. The other forms are PostgreSQL extensions. The CREATE TYPE statement in the SQL\nstandard also defines other forms that are not implemented in PostgreSQL.\n\nThe ability to create a composite type with zero attributes is a PostgreSQL-specific\ndeviation from the standard (analogous to the same case in CREATE TABLE).\n",
                "subsections": []
            },
            "SEE ALSO": {
                "content": "ALTER TYPE (ALTERTYPE(7)), CREATE DOMAIN (CREATEDOMAIN(7)), CREATE FUNCTION\n(CREATEFUNCTION(7)), DROP TYPE (DROPTYPE(7))\n\n\n\nPostgreSQL 14.23                                2026                                  CREATE TYPE(7)",
                "subsections": []
            }
        }
    }
}