CREATE TYPE

Name

CREATE TYPE  --  Defines a new data type for use in the database.

Synopsis

CREATE TYPE typename ( INPUT = input_function, OUTPUT = output_function
      , INTERNALLENGTH = { internallength | VARIABLE } 
    [ , EXTERNALLENGTH = { externallength | VARIABLE } ]
    [ , DEFAULT = "default" ]
    [ , ELEMENT = element ] [ , DELIMITER = delimiter ]
    [ , SEND = send_function ] [ , RECEIVE = receive_function ]
    [ , PASSEDBYVALUE ]
    [ , ALIGNMENT =alignment ] 
    [ , STORAGE =storage ] )
  

Inputs

typename

Use this parameter to specify the name of the new type being created.

internallength

Use this parameter to set the internal length of the new type.

externallength

Use this parameter to set the external (displayed) length of the new type.

input_function

Use this parameter to specify the name of the new type's input function. You must have already defined the function with CREATE FUNCTION and it must act to convert data of the type's external form into the type's internal form.

output_function

Use this parameter to specify the name of the new type's output function. This function must convert data of the type's internal form into a displayable form.

element

If the type is an array, use this parameter to identify the type of the array elements.

delimiter

Use this parameter to specify the array element delimiter.

default

Use this parameter to specify what the default value for this type should be. Normally this is NULL if not specified.

send_function

Use this parameter to specify the name of the new type's send function. This function should convert data of this type into a form that can be transfered to another machine.

receive_function

Use this parameter to specify the name of the new type's receive function. This function should accept data of the form outputted by the send_function and convert it into the type internal form.

alignment

Use this parameter to set the storage alignment this type will require. This must be either 'int4' or 'double.' If left unspecified, 'int4' will be chosen by default.

storage

Use this parameter to specify the storage technique that will be used for this type. Set this to one of 'plain,' 'external,' 'extended,' or 'main.' If this is left unspecified, the storage type will default to plain.

Outputs

CREATE

This message is displayed if the type is successfully created.

Description

Use the CREATE TYPE command to register a new user-defined data type with the current database. The user account that issues the command becomes the owner of the data type if creation is successful. All data types within a PostgreSQL database must have unique names, therefor you must choose a unique name for the new data type.

For a type to be created it must use two pre-existing functions (pre-existing in the sense that you have already used CREATE FUNCTION to register them); these functions are the input function and the output function. The input function converts the type's external representation into an internal representation that can be used by the system objects associated with the type. The output function converts the internal representation back to an external representation. They must both take either one or two arguments of the opaque type.

You can set the type as either fixed or variable length. If fixed-length, use internallength to set its length. If variable-length, use 'VARIABLE' for the internallength parameter and the length will be handled in the same way as the text data type. Specify the external length in the same way, using the externallength keyword.

Use the ELEMENT keyword to specify the type is an array. If you wish to provide a delimiter character for the array type, use delimiter to do so. By default this is set to a comma.

To define a default value, use the DEFAULT keyword.

Examples

This example demonstrates the creation of an int4 array type.

booktown=# CREATE TYPE intarray (INPUT = array_in, OUTPUT = array_out,
booktown(#     INTERNALLENGTH = VARIABLE, ELEMENT = int4);