create_type.7

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

7
415
字号
.SS "ARRAY TYPES".PPWhenever a user-defined base data type is created, PostgreSQL automatically creates anassociated array type, whose name consists of the base type'sname prepended with an underscore. The parser understands thisnaming convention, and translates requests for columns of typefoo[] into requests for type _foo.The implicitly-created array type is variable length and uses thebuilt-in input and output functions array_in andarray_out..PPYou might reasonably ask why there is an \fBELEMENT\fRoption, if the system makes the correct array type automatically.The only case where it's useful to use \fBELEMENT\fR is when you aremaking a fixed-length type that happens to be internally an array of a number ofidentical things, and you want to allow these things to be accesseddirectly by subscripting, in addition to whatever operations you planto provide for the type as a whole. For example, type \fBname\fRallows its constituent \fBchar\fR elements to be accessed this way.A 2-D \fBpoint\fR type could allow its two component numbers to beaccessed like point[0] and point[1].Note thatthis facility only works for fixed-length types whose internal formis exactly a sequence of identical fixed-length fields. A subscriptablevariable-length type must have the generalized internal representationused by array_in and array_out.For historical reasons (i.e., this is clearly wrong but it's far toolate to change it), subscripting of fixed-length array types starts fromzero, rather than from one as for variable-length arrays..SH "PARAMETERS".TP\fB\fIname\fB\fRThe name (optionally schema-qualified) of a type to be created..TP\fB\fIattribute_name\fB\fRThe name of an attribute (column) for the composite type..TP\fB\fIdata_type\fB\fRThe name of an existing data type to become a column of thecomposite type..TP\fB\fIinput_function\fB\fRThe name of a function that converts data from the type'sexternal textual form to its internal form..TP\fB\fIoutput_function\fB\fRThe name of a function that converts data from the type'sinternal form to its external textual form..TP\fB\fIreceive_function\fB\fRThe name of a function that converts data from the type'sexternal binary form to its internal form..TP\fB\fIsend_function\fB\fRThe name of a function that converts data from the type'sinternal form to its external binary form..TP\fB\fIanalyze_function\fB\fRThe name of a function that performs statistical analysis for thedata type..TP\fB\fIinternallength\fB\fRA numeric constant that specifies the length in bytes of the newtype's internal representation. The default assumption is thatit is variable-length..TP\fB\fIalignment\fB\fRThe storage alignment requirement of the data type. If specified,it must be char, int2,int4, or double; thedefault is int4..TP\fB\fIstorage\fB\fRThe storage strategy for the data type. If specified, must beplain, external,extended, or main; thedefault is plain..TP\fB\fIdefault\fB\fRThe default value for the data type. If this is omitted, thedefault is null..TP\fB\fIelement\fB\fRThe type being created is an array; this specifies the type ofthe array elements..TP\fB\fIdelimiter\fB\fRThe delimiter character to be used between values in arrays madeof this type..SH "NOTES".PPUser-defined type names cannot begin with the underscore character(_) and can only be 62 characterslong (or in general \fBNAMEDATALEN\fR - 2, rather thanthe \fBNAMEDATALEN\fR - 1 characters allowed for othernames). Type names beginning with underscore are reserved forinternally-created array type names..PPBecause there are no restrictions on use of a data type once it's beencreated, creating a base type is tantamount to granting public executepermission on the functions mentioned in the type definition. (The creatorof the type is therefore required to own these functions.) This is usuallynot an issue for the sorts of functions that are useful in a typedefinition. But you might want to think twice before designing a typein a way that would require ``secret'' information to be usedwhile converting it to or from external form..PPBefore PostgreSQL version 8.2, the syntaxCREATE TYPE \fIname\fR did not exist.The way to create a new base type was to create its input function first.In this approach, PostgreSQL will first seethe name of the new data type as the return type of the input function.The shell type is implicitly created in this situation, and then itcan be referenced in the definitions of the remaining I/O functions.This approach still works, but is deprecated and may be disallowed insome future release. Also, to avoid accidentally clutteringthe catalogs with shell types as a result of simple typos in functiondefinitions, a shell type will only be made this way when the inputfunction is written in C..PPIn PostgreSQL versions before 7.3, itwas customary to avoid creating a shell type at all, by replacing thefunctions' forward references to the type name with the placeholderpseudotype \fBopaque\fR. The \fBcstring\fR arguments andresults also had to be declared as \fBopaque\fR before 7.3. Tosupport loading of old dump files, \fBCREATE TYPE\fR willaccept I/O functions declared using \fBopaque\fR, but it will issuea notice and change the function declarations to use the correcttypes..SH "EXAMPLES".PPThis example creates a composite type and uses it ina function definition:.sp.nfCREATE TYPE compfoo AS (f1 int, f2 text);CREATE FUNCTION getfoo() RETURNS SETOF compfoo AS $$    SELECT fooid, fooname FROM foo$$ LANGUAGE SQL;.sp.fi.PPThis example creates the base data type \fBbox\fR and then uses thetype in a table definition:.sp.nfCREATE TYPE box;CREATE FUNCTION my_box_in_function(cstring) RETURNS box AS ... ;CREATE FUNCTION my_box_out_function(box) RETURNS cstring AS ... ;CREATE TYPE box (    INTERNALLENGTH = 16,    INPUT = my_box_in_function,    OUTPUT = my_box_out_function);CREATE TABLE myboxes (    id integer,    description box);.sp.fi.PPIf the internal structure of \fBbox\fR were an array of four\fBfloat4\fR elements, we might instead use.sp.nfCREATE TYPE box (    INTERNALLENGTH = 16,    INPUT = my_box_in_function,    OUTPUT = my_box_out_function,    ELEMENT = float4);.sp.fiwhich would allow a box value's component numbers to be accessedby subscripting. Otherwise the type behaves the same as before..PPThis example creates a large object type and uses it ina table definition:.sp.nfCREATE TYPE bigobj (    INPUT = lo_filein, OUTPUT = lo_fileout,    INTERNALLENGTH = VARIABLE);CREATE TABLE big_objs (    id integer,    obj bigobj);.sp.fi.PPMore examples, including suitable input and output functions, arein in the documentation..SH "COMPATIBILITY".PPThis \fBCREATE TYPE\fR command is aPostgreSQL extension. There is a\fBCREATE TYPE\fR statement in the SQL standardthat is rather different in detail..SH "SEE ALSO"CREATE FUNCTION [\fBcreate_function\fR(7)], DROP TYPE [\fBdrop_type\fR(l)], ALTER TYPE [\fBalter_type\fR(l)], CREATE DOMAIN [\fBcreate_domain\fR(l)]

⌨️ 快捷键说明

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