1
1
<!--
2
- $Header: /cvsroot/pgsql/doc/src/sgml/ref/create_type.sgml,v 1.23 2001/09/13 19:05:29 petere Exp $
2
+ $Header: /cvsroot/pgsql/doc/src/sgml/ref/create_type.sgml,v 1.24 2001/11/03 21:42:47 tgl Exp $
3
3
Postgres documentation
4
4
-->
5
5
@@ -27,7 +27,7 @@ CREATE TYPE <replaceable class="parameter">typename</replaceable> ( INPUT = <rep
27
27
, INTERNALLENGTH = { <replaceable
28
28
class="parameter">internallength</replaceable> | VARIABLE }
29
29
[ , EXTERNALLENGTH = { <replaceable class="parameter">externallength</replaceable> | VARIABLE } ]
30
- [ , DEFAULT = " <replaceable class="parameter">default</replaceable>" ]
30
+ [ , DEFAULT = <replaceable class="parameter">default</replaceable> ]
31
31
[ , ELEMENT = <replaceable class="parameter">element</replaceable> ] [ , DELIMITER = <replaceable class="parameter">delimiter</replaceable> ]
32
32
[ , SEND = <replaceable class="parameter">send_function</replaceable> ] [ , RECEIVE = <replaceable class="parameter">receive_function</replaceable> ]
33
33
[ , PASSEDBYVALUE ]
@@ -113,7 +113,8 @@ CREATE TYPE <replaceable class="parameter">typename</replaceable> ( INPUT = <rep
113
113
<term><replaceable class="parameter">delimiter</replaceable></term>
114
114
<listitem>
115
115
<para>
116
- The delimiter character for the array elements.
116
+ The delimiter character to be used between values in arrays made
117
+ of this type.
117
118
</para>
118
119
</listitem>
119
120
</varlistentry>
@@ -219,82 +220,101 @@ CREATE
219
220
220
221
<para>
221
222
<command>CREATE TYPE</command> requires the registration of two functions
222
- (using create function ) before defining the type. The
223
+ (using CREATE FUNCTION ) before defining the type. The
223
224
representation of a new base type is determined by
224
225
<replaceable class="parameter">input_function</replaceable>, which
225
226
converts the type's external representation to an internal
226
227
representation usable by the
227
228
operators and functions defined for the type. Naturally,
228
229
<replaceable class="parameter">output_function</replaceable>
229
- performs the reverse transformation. Both
230
- the input and output functions must be declared to take
231
- one or two arguments of type <type>opaque</type>.
230
+ performs the reverse transformation. The input function may be
231
+ declared as taking one argument of type <type>opaque</type>,
232
+ or as taking three arguments of types
233
+ <type>opaque</type>, <type>OID</type>, <type>int4</type>.
234
+ (The first argument is the input text as a C string, the second
235
+ argument is the element type in case this is an array type,
236
+ and the third is the typmod of the destination column, if known.)
237
+ The output function may be
238
+ declared as taking one argument of type <type>opaque</type>,
239
+ or as taking two arguments of types
240
+ <type>opaque</type>, <type>OID</type>.
241
+ (The first argument is actually of the datatype itself, but since the
242
+ output function must be declared first, it's easier to declare it as
243
+ accepting type <type>opaque</type>. The second argument is again
244
+ the array element type for array types.)
232
245
</para>
233
246
234
247
<para>
235
248
New base data types can be fixed length, in which case
236
249
<replaceable class="parameter">internallength</replaceable> is a
237
- positive integer, or variable length,
238
- in which case PostgreSQL assumes that the new type has the
239
- same format
240
- as the PostgreSQL-supplied data type, <type>text</type>.
241
- To indicate that a type is variable length, set
250
+ positive integer, or variable length, indicated by setting
242
251
<replaceable class="parameter">internallength</replaceable>
243
- to <option>VARIABLE</option>.
244
- The external representation is similarly specified using the
252
+ to <option>VARIABLE</option>. (Internally, this is represented
253
+ by setting typlen to -1.) The internal representation of all
254
+ variable-length types must start with an integer giving the total
255
+ length of this value of the type.
256
+ </para>
257
+
258
+ <para>
259
+ The external representation length is similarly specified using the
245
260
<replaceable class="parameter">externallength</replaceable>
246
- keyword.
261
+ keyword. (This value is not presently used, and is typically omitted,
262
+ letting it default to <option>VARIABLE</option>.)
247
263
</para>
248
264
249
265
<para>
250
- To indicate that a type is an array and to indicate that a
251
- type has array elements, indicate the type of the array
252
- element using the element keyword. For example, to define
266
+ To indicate that a type is an array,
267
+ specify the type of the array
268
+ elements using the <option>ELEMENT</> keyword. For example, to define
253
269
an array of 4-byte integers ("int4"), specify
254
270
<programlisting>ELEMENT = int4</programlisting>
271
+ More details about array types appear below.
255
272
</para>
256
273
257
274
<para>
258
- To indicate the delimiter to be used on arrays of this
259
- type, <replaceable class="parameter">delimiter</replaceable>
260
- can be
275
+ To indicate the delimiter to be used between values in the external
276
+ representation of arrays of this type, <replaceable
277
+ class="parameter">delimiter</replaceable> can be
261
278
set to a specific character. The default delimiter is the comma
262
- ("<literal>,</literal>").
279
+ ('<literal>,</literal>'). Note that the delimiter is associated
280
+ with the array element type, not the array type itself.
263
281
</para>
264
282
265
283
<para>
266
- A default value is optionally available in case a user
267
- wants some specific bit pattern to mean <quote>data not present</quote> .
268
- Specify the default with the <literal >DEFAULT</literal > keyword.
269
- <comment>How does the user specify that bit pattern and associate
270
- it with the fact that the data is not present></comment>
284
+ A default value may be specified, in case a user wants columns of the
285
+ datatype to default to something other than NULL .
286
+ Specify the default with the <option >DEFAULT</option > keyword.
287
+ (Such a default may be overridden by an explicit <option>DEFAULT</option>
288
+ clause attached to a particular column.)
271
289
</para>
272
290
273
291
<para>
274
292
The optional arguments
275
293
<replaceable class="parameter">send_function</replaceable> and
276
294
<replaceable class="parameter">receive_function</replaceable>
277
- are used when the application program requesting PostgreSQL
278
- services resides on a different machine. In this case,
279
- the machine on which PostgreSQL runs may use a format for the data
280
- type different from that used on the remote machine.
281
- In this case it is appropriate to convert data items to a
282
- standard form when sending from the server to the client
283
- and converting from the standard format to the machine
284
- specific format when the server receives the data from the
285
- client. If these functions are not specified, then it is
286
- assumed that the internal format of the type is acceptable
287
- on all relevant machine architectures. For example, single
288
- characters do not have to be converted if passed from
289
- a Sun-4 to a DECstation, but many other types do.
295
+ are not currently used, and are usually omitted (allowing them
296
+ to default to the
297
+ <replaceable class="parameter">output_function</replaceable> and
298
+ <replaceable class="parameter">input_function</replaceable>
299
+ respectively). These functions may someday be resurrected for use
300
+ in specifying machine-independent binary representations.
290
301
</para>
291
302
292
303
<para>
293
- The optional flag, <option>PASSEDBYVALUE</option>, indicates that operators
294
- and functions which use this data type should be passed an
295
- argument by value rather than by reference. Note that you
304
+ The optional flag, <option>PASSEDBYVALUE</option>, indicates that
305
+ values of this data type are passed
306
+ by value rather than by reference. Note that you
296
307
may not pass by value types whose internal representation is
297
- more than four bytes.
308
+ longer than the width of the <type>Datum</> type (four bytes on
309
+ most machines, eight bytes on a few).
310
+ </para>
311
+
312
+ <para>
313
+ The <replaceable class="parameter">alignment</replaceable> keyword
314
+ specifies the storage alignment required for the datatype. The
315
+ allowed values equate to alignment on 1, 2, 4, or 8 byte boundaries.
316
+ Note that variable-length types must have an alignment of at least
317
+ 4, since they necessarily contain an <type>int4</> as their first component.
298
318
</para>
299
319
300
320
<para>
@@ -315,19 +335,40 @@ CREATE
315
335
<literal>extended</literal> and <literal>external</literal> items.)
316
336
</para>
317
337
318
- <para>
319
- For new base types, a user can define operators, functions
320
- and aggregates using the appropriate facilities described
321
- in this section.
322
- </para>
323
-
324
338
<refsect2>
325
339
<title>Array Types</title>
340
+
326
341
<para>
327
- Two generalized built-in functions, array_in and
328
- array_out, exist for quick creation of variable-length
329
- array types. These functions operate on arrays of any
330
- existing PostgreSQL type.
342
+ Whenever a user-defined datatype is created,
343
+ <productname>PostgreSQL</productname> automatically creates an
344
+ associated array type, whose name consists of the base type's
345
+ name prepended with an underscore. The parser understands this
346
+ naming convention, and translates requests for columns of type
347
+ <literal>foo[]</> into requests for type <literal>_foo</>.
348
+ The implicitly-created array type is variable length and uses the
349
+ built-in input and output functions <literal>array_in</> and
350
+ <literal>array_out</>.
351
+ </para>
352
+
353
+ <para>
354
+ You might reasonably ask <quote>why is there an <option>ELEMENT</>
355
+ option, if the system makes the correct array type automatically?</quote>
356
+ The only case where it's useful to use <option>ELEMENT</> is when you are
357
+ making a fixed-length type that happens to be internally an array of N
358
+ identical things, and you want to allow the N things to be accessed
359
+ directly by subscripting, in addition to whatever operations you plan
360
+ to provide for the type as a whole. For example, type <type>name</>
361
+ allows its constitutent <type>char</>s to be accessed this way.
362
+ A 2-D <type>point</> type could allow its two component floats to be
363
+ accessed like <literal>point[0]</> and <literal>point[1]</>.
364
+ Note that
365
+ this facility only works for fixed-length types whose internal form
366
+ is exactly a sequence of N identical fields. A subscriptable
367
+ variable-length type must have the generalized internal representation
368
+ used by <literal>array_in</> and <literal>array_out</>.
369
+ For historical reasons (i.e., this is clearly wrong but it's far too
370
+ late to change it), subscripting of fixed-length array types starts from
371
+ zero, rather than from one as for variable-length arrays.
331
372
</para>
332
373
</refsect2>
333
374
</refsect1>
@@ -336,41 +377,42 @@ CREATE
336
377
<title>Notes</title>
337
378
338
379
<para>
339
- Type names cannot begin with the underscore character
340
- (<quote><literal>_</literal></quote>) and can only be 31
341
- characters long. This is because PostgreSQL silently creates an
342
- array type for each base type with a name consisting of the base
343
- type's name prepended with an underscore.
380
+ User-defined type names cannot begin with the underscore character
381
+ (<quote><literal>_</literal></quote>) and can only be 30
382
+ characters long (or in general <literal>NAMEDATALEN-2</>, rather than
383
+ the <literal>NAMEDATALEN-1</> characters allowed for other names).
384
+ Type names beginning with underscore are
385
+ reserved for internally-created array type names.
344
386
</para>
345
387
</refsect1>
346
388
347
389
<refsect1>
348
390
<title>Examples</title>
349
391
<para>
350
- This command creates the <type>box</type> data type and then uses the
392
+ This example creates the <type>box</type> data type and then uses the
351
393
type in a table definition:
352
394
<programlisting>
353
- CREATE TYPE box (INTERNALLENGTH = 8 ,
395
+ CREATE TYPE box (INTERNALLENGTH = 16 ,
354
396
INPUT = my_procedure_1, OUTPUT = my_procedure_2);
355
397
CREATE TABLE myboxes (id INT4, description box);
356
398
</programlisting>
357
399
</para>
358
400
359
401
<para>
360
- This command creates a variable length array type with
361
- <type>integer</type> elements:
362
-
402
+ If <type>box</type>'s internal structure were an array of four
403
+ <type>float4</>s, we might instead say
363
404
<programlisting>
364
- CREATE TYPE int4array (INPUT = array_in, OUTPUT = array_out ,
365
- INTERNALLENGTH = VARIABLE, ELEMENT = int4);
366
- CREATE TABLE myarrays (id int4, numbers int4array );
405
+ CREATE TYPE box (INTERNALLENGTH = 16 ,
406
+ INPUT = my_procedure_1, OUTPUT = my_procedure_2,
407
+ ELEMENT = float4 );
367
408
</programlisting>
409
+ which would allow a box value's component floats to be accessed
410
+ by subscripting. Otherwise the type behaves the same as before.
368
411
</para>
369
412
370
413
<para>
371
- This command creates a large object type and uses it in
414
+ This example creates a large object type and uses it in
372
415
a table definition:
373
-
374
416
<programlisting>
375
417
CREATE TYPE bigobj (INPUT = lo_filein, OUTPUT = lo_fileout,
376
418
INTERNALLENGTH = VARIABLE);
0 commit comments