CREATE TYPE - defining a new data type

SYNOPSIS

 

CREATE TYPE name AS
    ( attribute_name data_type [, ... ] )

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

DESCRIPTION Description

CREATE TYPE registers a new data type for the current database. Define the type of user becomes its owner.


 If the schema name is given then the type is created in the specified mode. Otherwise it is created in the current schema. Required mode of the same name and type in a different type or any existing domain. (Because tables have associated data types, the type name can not be in the same schema table name conflicts.)

COMPOSITE TYPES composite type


 The first form of CREATE TYPE creates a composite type. Composite type is through a property name and data type declarations. This is essentially the same row type of a table, but if we want to define a type, then use the CREATE TYPE avoids create an actual table. A separate composite type is useful for a return type of the function.

BASE TYPES basic types


 The second form of CREATE TYPE creates a new base type (scalar type). Parameters may appear in any order, rather than shown above. And most are optional. It requires to be registered before the two functions defined types (using CREATE FUNCTION). Input_function and support functions output_function is a must, and the function receive_function and send_function is optional. In general, these functions must be written in C or other low-level languages.


 The input_function external form of the type of text may be converted into the internal form of operators and functions defined for the type of operation. output_function performs the reverse. The input function may be declared as taking one argument of type c_string or accept three types of parameters are c_string, oid, integer a. (First argument is the C input text string, the second is when the type is an array type of its elements, the third field is the typmod target, if known.) That it should return a data type value itself. Output function may be declared to accept a new type for the data type parameter or accept two types, the second type parameter is oid. The second parameter is also the array type array element type. Output function should return type cstring.


 Alternatively receive_function converting external binary representation of the type of internal representation. If you do not provide this function, you can not use this type of binary input. Binary format that should be selected relatively easily converted into internal format, as well as some portability. (For example, using the standard integer data type network byte order as the external binary representation, and the internal representation is native byte order of the machine.) Receiving function may be declared to accept a type of internal parameters, or both type internal parameters and respectively the oid. It must return a numeric data type itself. (The first parameter is a pointer to a StringInfo buffer holding the received byte string pointer; optional second argument is the element type - if the type is an array type it) Similarly, the optional send_function convert type external binary representation. If you do not provide these functions, the type can not be used in binary output. Sending function can be declared as a new data type, or to receive two parameters, the second parameter is oid. The second argument is still used as an array type. Send function must return bytea.


 This time you should find it strange, how is the input and output functions can be declared to have results or accept a new type of new types of parameters, but you need to create them before you create a new type. The answer is that the input function must be created, then the output function, and finally the data type. PostgreSQL will first be seen as a new data type the name of the input function's return type. It will create a "shell" type, which is just a placeholder in pg_type, then the input function definition to the shell type. Similarly functions will be linked to the (now already existing) shell type. Finally, CREATE TYPE replaces the shell type into a complete type definition, so that the new type can be used.


 Although the new type of internal representation are only I / O and other functions you create to work with the type of function to understand, but there are a few internal performance attributes must be declared to PostgreSQL. The most important of these is internallength. The basic data types can be fixed length, in which case internallength is a positive integer, and may be longer by setting internallength VARIABLE FIG. (Internally, this state is set to -1 by typlen achieved.) The internal representation of all variable-length types must start with a four-byte integer that gives the entire length of this type of this value.


 Optional flag indicates that the value of this type PASSEDBYVALUE is passed by value, rather than by reference. You can not pass that is greater than the internal form Datum (4 bytes on most machines, 8 bytes) of the value of the data type size type.

The alignment parameter specifies the storage alignment of the data type requirements. The allowed values ​​equate to alignment 1,2,4, or 8-byte boundary. Note that the variable-length type must have at least a 4-byte alignment, because they necessarily contain an int4 as their first component.

storage parameter allows to select the type of variable-length data storage policy. (Type only allows the use of fixed-length plain). plain specifies the data type of the inline manner always stored rather than compressed. extended declaration system will first try to compress a long data value, and if it is still too long, then it will be of value out of the main line of the table, but the system will not compress it. main allows compression, but discourages moving the value out of the main table. (May still be moved out of the main table with data items with this storage strategy, if you can not make a row, then, than they are extended and external items are more willing to stay in the main table.)


 If the user wishes to default data type of the field is not NULL, but something else, then you can declare a default. Specify the default with the DEFAULT keyword inside. (Such a default may be attached to a particular column explicit DEFAULT clause cover.)


 To indicate that a type is an array type with the ELEMENT key word array elements. For example, to define an array of a 4-byte integer (int4), statements
  ELEMENT = int4

. More details about array types are described below.


 To declare a separator between the value of the external form of arrays of this type, it can be used to specify a statement separator delimiter. The default delimiter is a comma (,). Note that the delimiter is associated with the array element type, not array type itself.

ARRAY TYPES array type


 Creating user-defined data types, PostgreSQL automatically create an associated array type, whose name prefix consists of the base type's name with an underscore. Request analyzer understands this naming convention, and the type foo [] into the field to a pair of field type _foo request. This implicitly created array type is variable length and uses the built array_in input and output functions and array_out.


 You might reasonably ask if the system automatically makes the correct array type, why there is an ELEMENT option? Use a unique occasion is useful ELEMENT happen within a certain number of array is the same thing, and you want to allow the N things to be accessed directly by subscripting, and that you are ready to put this type of fixed-length type you made operating as a whole. For example, type name allows its constituent char associated with this method. A two-dimensional point type can also allow its two component associated float method in a similar point [0] and point [. 1] is. Note that this feature is only applicable to those same internal form is a type of fixed-length sequence of the domain. Subscriptable a variable-length type must have the generalized internal representation used by array_in and array_out. For historical reasons (that is, those clearly wrong but it's far too late), fixed-length array types of foot zero-based, rather than as a variable-length type from the beginning like that.

PARAMETERS Parameters

name

 Type the name that will be created (schema-qualified).
attribute_name

 The name of a complex type of property (field).
data_type

 To become a type of a complex existing data type of field names.
input_function

 A name of the function, converting the data type from the external to the internal type.
output_function

 Name of a function that converts data from the internal format into a form suitable for display.
receive_function

 Converts data from an external binary form into its internal form of the type of the function name.
send_function

 To convert data from the inside to the outside thereof in the form of a binary type in the form of the function name.
internallength

 A constant value, indicating the length of the internal representation of the new type. The default assumption is that it is becoming longer.
alignment

 This type of storage data alignment requirements. If specified, must be char, int2, int4, or double; the default is int4.
storage

 The type of data storage strategy. If specified, it must be plain, external, extended, or main; the default is plain.
default

 This type of default. If this is omitted, it defaults to NULL.
element

 Type being created is an array; this specifies the type of the array elements.
delimiter

 The data elements used between the array of character delimiters.

NOTES Note


 User-defined type names can not underscore (_) and can only be 62 characters long. (Or typically NAMEDATALEN-2, but not other names can NAMEDATALEN-1 as the character). To type names begin with an underscore are parsed into an array of type names created internally.


 In PostgreSQL versions before 7.3, we have to pass before the placeholder type opaque instead of the pseudo-reference function to use to avoid creating a shell type. Prior to 7.3 cstring same parameters and results to be declared as opaque. To support loading of old dump outside that room, CREATE TYPE will accept those functions declared using opaque, but it sends back a notice and change the function declaration with the correct type.

EXAMPLES Examples


 This example creates a composite type and use 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 command creates the box data type, and the type in a table definition:

 

CREATE TYPE box (
    INTERNALLENGTH = 16,
    INPUT = my_box_in_function,
    OUTPUT = my_box_out_function
);

CREATE TABLE myboxes (
    id integer,
    description box
);


 If the internal structure of box were an array of four float4, we can say

 

CREATE TYPE box (
    INTERNALLENGTH = 16,
    INPUT = my_box_in_function,
    OUTPUT = my_box_out_function,
    ELEMENT = float4
);


 It allows the value of the component members of a box can be accessed foot mark. Otherwise, this type of behavior and in front of the same.


 This command creates a large object type and uses it in a table definition:

 

CREATE TYPE bigobj (
    INPUT = lo_filein, OUTPUT = lo_fileout,
    INTERNALLENGTH = VARIABLE
);
CREATE TABLE big_objs (
    id integer,
    obj bigobj
);


 More examples, including suitable input and output functions, in Chapter 31``Extending SQL '' in the documentation.  

Guess you like

Origin www.cnblogs.com/fanweisheng/p/11078078.html