create_type.7

来自「PostgreSQL 8.2中增加了很多企业用户所需要的功能和性能上的提高,其开」· 7 代码 · 共 415 行 · 第 1/2 页

7
415
字号
.\\" auto-generated by docbook2man-spec $Revision: 1.1.1.1 $.TH "CREATE TYPE" "" "2008-01-03" "SQL - Language Statements" "SQL Commands".SH NAMECREATE TYPE \- define a new data type.SH SYNOPSIS.sp.nfCREATE TYPE \fIname\fR AS    ( \fIattribute_name\fR \fIdata_type\fR [, ... ] )CREATE TYPE \fIname\fR (    INPUT = \fIinput_function\fR,    OUTPUT = \fIoutput_function\fR    [ , RECEIVE = \fIreceive_function\fR ]    [ , SEND = \fIsend_function\fR ]    [ , ANALYZE = \fIanalyze_function\fR ]    [ , INTERNALLENGTH = { \fIinternallength\fR | VARIABLE } ]    [ , PASSEDBYVALUE ]    [ , ALIGNMENT = \fIalignment\fR ]    [ , STORAGE = \fIstorage\fR ]    [ , DEFAULT = \fIdefault\fR ]    [ , ELEMENT = \fIelement\fR ]    [ , DELIMITER = \fIdelimiter\fR ])CREATE TYPE \fIname\fR.sp.fi.SH "DESCRIPTION".PP\fBCREATE TYPE\fR registers a new data type for use inthe current database. The user who defines a type becomes itsowner..PPIf a schema name is given then the type is created in the specifiedschema. Otherwise it is created in the current schema. The typename must be distinct from the name of any existing type or domainin the same schema. (Because tables have associated data types,the type name must also be distinct from the name of any existingtable in the same schema.).SS "COMPOSITE TYPES".PPThe first form of \fBCREATE TYPE\fRcreates a composite type.The composite type is specified by a list of attribute names and data types.This is essentially the same as the row typeof a table, but using \fBCREATE TYPE\fR avoids the need tocreate an actual table when all that is wanted is to define a type.A stand-alone composite type is useful as the argument or return type of afunction..SS "BASE TYPES".PPThe second form of \fBCREATE TYPE\fR creates a new base type(scalar type). The parameters may appear in any order, not only thatillustrated above, and most are optional. You must registertwo or more functions (using \fBCREATE FUNCTION\fR) beforedefining the type. The support functions \fIinput_function\fR and\fIoutput_function\fRare required, while the functions\fIreceive_function\fR,\fIsend_function\fR and\fIanalyze_function\fRare optional. Generally these functions have to be coded in Cor another low-level language..PPThe \fIinput_function\fRconverts the type's external textual representation to the internalrepresentation used by the operators and functions defined for the type.\fIoutput_function\fRperforms the reverse transformation. The input function may bedeclared as taking one argument of type \fBcstring\fR,or as taking three arguments of types\fBcstring\fR, \fBoid\fR, \fBinteger\fR.The first argument is the input text as a C string, the secondargument is the type's own OID (except for array types, which insteadreceive their element type's OID),and the third is the typmod of the destination column, if known(-1 will be passed if not).The input function must return a value of the data type itself.Usually, an input function should be declared STRICT; if it is not,it will be called with a NULL first parameter when reading a NULLinput value. The function must still return NULL in this case, unlessit raises an error.(This case is mainly meant to support domain input functions, whichmay need to reject NULL inputs.)The output function must bedeclared as taking one argument of the new data type.The output function must return type \fBcstring\fR.Output functions are not invoked for NULL values..PPThe optional \fIreceive_function\fRconverts the type's external binary representation to the internalrepresentation. If this function is not supplied, the type cannotparticipate in binary input. The binary representation should bechosen to be cheap to convert to internal form, while being reasonablyportable. (For example, the standard integer data types use networkbyte order as the external binary representation, while the internalrepresentation is in the machine's native byte order.) The receivefunction should perform adequate checking to ensure that the value isvalid.The receive function may be declared as taking one argument of type\fBinternal\fR, or as taking three arguments of types\fBinternal\fR, \fBoid\fR, \fBinteger\fR.The first argument is a pointer to a \fBStringInfo\fR bufferholding the received byte string; the optional arguments are thesame as for the text input function.The receive function must return a value of the data type itself.Usually, a receive function should be declared STRICT; if it is not,it will be called with a NULL first parameter when reading a NULLinput value. The function must still return NULL in this case, unlessit raises an error.(This case is mainly meant to support domain receive functions, whichmay need to reject NULL inputs.)Similarly, the optional\fIsend_function\fR convertsfrom the internal representation to the external binary representation.If this function is not supplied, the type cannot participate in binaryoutput. The send function must bedeclared as taking one argument of the new data type.The send function must return type \fBbytea\fR.Send functions are not invoked for NULL values..PPYou should at this point be wondering how the input and output functionscan be declared to have results or arguments of the new type, when theyhave to be created before the new type can be created. The answer is thatthe type should first be defined as a \fIshell type\fR, which is aplaceholder type that has no properties except a name and an owner. Thisis done by issuing the command CREATE TYPE\fIname\fR, with no additional parameters. Then theI/O functions can be defined referencing the shell type. Finally,\fBCREATE TYPE\fR with a full definition replaces the shell entrywith a complete, valid type definition, after which the new type can beused normally..PPThe optional \fIanalyze_function\fRperforms type-specific statistics collection for columns of the data type.By default, \fBANALYZE\fR will attempt to gather statistics usingthe type's ``equals'' and ``less-than'' operators, if thereis a default b-tree operator class for the type. For non-scalar typesthis behavior is likely to be unsuitable, so it can be overridden byspecifying a custom analysis function. The analysis function must bedeclared to take a single argument of type \fBinternal\fR, and returna \fBboolean\fR result. The detailed API for analysis functions appearsin \fIsrc/include/commands/vacuum.h\fR..PPWhile the details of the new type's internal representation are onlyknown to the I/O functions and other functions you create to work withthe type, there are several properties of the internal representationthat must be declared to PostgreSQL.Foremost of these is\fIinternallength\fR.Base data types can be fixed-length, in which case\fIinternallength\fR is apositive integer, or variable length, indicated by setting\fIinternallength\fRto VARIABLE. (Internally, this is representedby setting typlen to -1.) The internal representation of allvariable-length types must start with a 4-byte integer giving the totallength of this value of the type..PPThe optional flag PASSEDBYVALUE indicates thatvalues of this data type are passed by value, rather than byreference. You may not pass by value types whose internalrepresentation is larger than the size of the \fBDatum\fR type(4 bytes on most machines, 8 bytes on a few)..PPThe \fIalignment\fR parameterspecifies the storage alignment required for the data type. Theallowed values equate to alignment on 1, 2, 4, or 8 byte boundaries.Note that variable-length types must have an alignment of at least4, since they necessarily contain an \fBint4\fR as their first component..PPThe \fIstorage\fR parameterallows selection of storage strategies for variable-length datatypes. (Only plain is allowed for fixed-lengthtypes.) plain specifies that data of the typewill always be stored in-line and not compressed.extended specifies that the system will firsttry to compress a long data value, and will move the value out ofthe main table row if it's still too long.external allows the value to be moved out of themain table, but the system will not try to compress it.main allows compression, but discourages movingthe value out of the main table. (Data items with this storagestrategy may still be moved out of the main table if there is noother way to make a row fit, but they will be kept in the maintable preferentially over extended andexternal items.).PPA default value may be specified, in case a user wants columns of thedata type to default to something other than the null value.Specify the default with the DEFAULT key word.(Such a default may be overridden by an explicit DEFAULTclause attached to a particular column.).PPTo indicate that a type is an array, specify the type of the arrayelements using the ELEMENT key word. For example, todefine an array of 4-byte integers (\fBint4\fR), specifyELEMENT = int4. More details about array typesappear below..PPTo indicate the delimiter to be used between values in the externalrepresentation of arrays of this type, \fIdelimiter\fR can beset to a specific character. The default delimiter is the comma(,). Note that the delimiter is associatedwith the array element type, not the array type itself.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?