Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 3d5ddc0

Browse files
committed
Clean up wrong, misleading, or obsolete documentation about array types,
particularly in the CREATE TYPE reference page. Fix some other errors in the CREATE TYPE page, too.
1 parent f008976 commit 3d5ddc0

File tree

4 files changed

+139
-89
lines changed

4 files changed

+139
-89
lines changed

doc/src/sgml/array.sgml

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- $Header: /cvsroot/pgsql/doc/src/sgml/array.sgml,v 1.12 2001/09/09 17:21:44 petere Exp $ -->
1+
<!-- $Header: /cvsroot/pgsql/doc/src/sgml/array.sgml,v 1.13 2001/11/03 21:42:47 tgl Exp $ -->
22

33
<chapter id="arrays">
44
<title>Arrays</title>
@@ -23,15 +23,15 @@ CREATE TABLE sal_emp (
2323
<structname>sal_emp</structname> with a <type>text</type> string
2424
(<structfield>name</structfield>), a one-dimensional array of type
2525
<type>integer</type> (<structfield>pay_by_quarter</structfield>),
26-
which shall represent the employee's salary by quarter, and a
26+
which represents the employee's salary by quarter, and a
2727
two-dimensional array of <type>text</type>
2828
(<structfield>schedule</structfield>), which represents the
2929
employee's weekly schedule.
3030
</para>
3131

3232
<para>
33-
Now we do some <command>INSERT</command>s; note that when appending
34-
to an array, we enclose the values within braces and separate them
33+
Now we do some <command>INSERT</command>s. Observe that to write an array
34+
value, we enclose the element values within braces and separate them
3535
by commas. If you know C, this is not unlike the syntax for
3636
initializing structures.
3737

@@ -200,8 +200,7 @@ SELECT * FROM sal_emp WHERE pay_by_quarter[1] = 10000 OR
200200
However, this quickly becomes tedious for large arrays, and is not
201201
helpful if the size of the array is unknown. Although it is not part
202202
of the primary <productname>PostgreSQL</productname> distribution,
203-
in the contributions directory, there is an extension to
204-
<productname>PostgreSQL</productname> that defines new functions and
203+
there is an extension available that defines new functions and
205204
operators for iterating over array values. Using this, the above
206205
query could be:
207206

doc/src/sgml/catalogs.sgml

+19-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<!--
22
Documentation of the system catalogs, directed toward PostgreSQL developers
3-
$Header: /cvsroot/pgsql/doc/src/sgml/catalogs.sgml,v 2.26 2001/10/15 22:47:47 tgl Exp $
3+
$Header: /cvsroot/pgsql/doc/src/sgml/catalogs.sgml,v 2.27 2001/11/03 21:42:47 tgl Exp $
44
-->
55

66
<chapter id="catalogs">
@@ -420,7 +420,9 @@
420420
<entry><type>int4</type></entry>
421421
<entry></entry>
422422
<entry>
423-
Number of dimensions, if the column is an array; otherwise 0.
423+
Number of dimensions, if the column is an array type; otherwise 0.
424+
(Presently, the number of dimensions of an array is not enforced,
425+
so any nonzero value effectively means <quote>it's an array</>.)
424426
</entry>
425427
</row>
426428

@@ -1064,7 +1066,7 @@
10641066
<entry><type>int2vector</type></entry>
10651067
<entry>pg_attribute.attnum</entry>
10661068
<entry>
1067-
This is an vector (array) of up to
1069+
This is a vector (array) of up to
10681070
<symbol>INDEX_MAX_KEYS</symbol> values that indicate which
10691071
table columns this index pertains to. For example a value of
10701072
<literal>1 3</literal> would mean that the first and the third
@@ -2336,7 +2338,9 @@
23362338
<entry>typdelim</entry>
23372339
<entry><type>char</type></entry>
23382340
<entry></entry>
2339-
<entry>Character that separates two values of this type when parsing array input</entry>
2341+
<entry>Character that separates two values of this type when parsing
2342+
array input. Note that the delimiter is associated with the array
2343+
element datatype, not the array datatype.</entry>
23402344
</row>
23412345

23422346
<row>
@@ -2360,14 +2364,17 @@
23602364
If <structfield>typelem</structfield> is not 0 then it
23612365
identifies another row in <structname>pg_type</structname>.
23622366
The current type can then be subscripted like an array yielding
2363-
values of type <structfield>typelem</structfield>. A non-zero
2364-
<structfield>typelem</structfield> does not guarantee this type
2365-
to be a <quote>real</quote> array type; some ordinary
2366-
fixed-length types can also be subscripted (e.g.,
2367-
<type>oidvector</type>). Variable-length types can
2368-
<emphasis>not</emphasis> be turned into pseudo-arrays like
2369-
that. Hence, the way to determine whether a type is a
2370-
<quote>true</quote> array type is typelem != 0 and typlen < 0.
2367+
values of type <structfield>typelem</structfield>. A
2368+
<quote>true</quote> array type is variable length
2369+
(<structfield>typlen</structfield> = -1),
2370+
but some fixed-length (<structfield>typlen</structfield> &gt; 0) types
2371+
also have nonzero <structfield>typelem</structfield>, for example
2372+
<type>name</type> and <type>oidvector</type>.
2373+
If a fixed-length type has a <structfield>typelem</structfield> then
2374+
its internal representation must be N values of the
2375+
<structfield>typelem</structfield> datatype with no other data.
2376+
Variable-length array types have a header defined by the array
2377+
subroutines.
23712378
</entry>
23722379
</row>
23732380

doc/src/sgml/ref/create_type.sgml

+111-69
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
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 $
33
Postgres documentation
44
-->
55

@@ -27,7 +27,7 @@ CREATE TYPE <replaceable class="parameter">typename</replaceable> ( INPUT = <rep
2727
, INTERNALLENGTH = { <replaceable
2828
class="parameter">internallength</replaceable> | VARIABLE }
2929
[ , EXTERNALLENGTH = { <replaceable class="parameter">externallength</replaceable> | VARIABLE } ]
30-
[ , DEFAULT = "<replaceable class="parameter">default</replaceable>" ]
30+
[ , DEFAULT = <replaceable class="parameter">default</replaceable> ]
3131
[ , ELEMENT = <replaceable class="parameter">element</replaceable> ] [ , DELIMITER = <replaceable class="parameter">delimiter</replaceable> ]
3232
[ , SEND = <replaceable class="parameter">send_function</replaceable> ] [ , RECEIVE = <replaceable class="parameter">receive_function</replaceable> ]
3333
[ , PASSEDBYVALUE ]
@@ -113,7 +113,8 @@ CREATE TYPE <replaceable class="parameter">typename</replaceable> ( INPUT = <rep
113113
<term><replaceable class="parameter">delimiter</replaceable></term>
114114
<listitem>
115115
<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.
117118
</para>
118119
</listitem>
119120
</varlistentry>
@@ -219,82 +220,101 @@ CREATE
219220

220221
<para>
221222
<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
223224
representation of a new base type is determined by
224225
<replaceable class="parameter">input_function</replaceable>, which
225226
converts the type's external representation to an internal
226227
representation usable by the
227228
operators and functions defined for the type. Naturally,
228229
<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.)
232245
</para>
233246

234247
<para>
235248
New base data types can be fixed length, in which case
236249
<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
242251
<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
245260
<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>.)
247263
</para>
248264

249265
<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
253269
an array of 4-byte integers ("int4"), specify
254270
<programlisting>ELEMENT = int4</programlisting>
271+
More details about array types appear below.
255272
</para>
256273

257274
<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
261278
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.
263281
</para>
264282

265283
<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.)
271289
</para>
272290

273291
<para>
274292
The optional arguments
275293
<replaceable class="parameter">send_function</replaceable> and
276294
<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.
290301
</para>
291302

292303
<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
296307
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.
298318
</para>
299319

300320
<para>
@@ -315,19 +335,40 @@ CREATE
315335
<literal>extended</literal> and <literal>external</literal> items.)
316336
</para>
317337

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-
324338
<refsect2>
325339
<title>Array Types</title>
340+
326341
<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.
331372
</para>
332373
</refsect2>
333374
</refsect1>
@@ -336,41 +377,42 @@ CREATE
336377
<title>Notes</title>
337378

338379
<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.
344386
</para>
345387
</refsect1>
346388

347389
<refsect1>
348390
<title>Examples</title>
349391
<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
351393
type in a table definition:
352394
<programlisting>
353-
CREATE TYPE box (INTERNALLENGTH = 8,
395+
CREATE TYPE box (INTERNALLENGTH = 16,
354396
INPUT = my_procedure_1, OUTPUT = my_procedure_2);
355397
CREATE TABLE myboxes (id INT4, description box);
356398
</programlisting>
357399
</para>
358400

359401
<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
363404
<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);
367408
</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.
368411
</para>
369412

370413
<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
372415
a table definition:
373-
374416
<programlisting>
375417
CREATE TYPE bigobj (INPUT = lo_filein, OUTPUT = lo_fileout,
376418
INTERNALLENGTH = VARIABLE);

doc/src/sgml/ref/drop_type.sgml

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$Header: /cvsroot/pgsql/doc/src/sgml/ref/drop_type.sgml,v 1.11 2001/09/13 19:05:29 petere Exp $
2+
$Header: /cvsroot/pgsql/doc/src/sgml/ref/drop_type.sgml,v 1.12 2001/11/03 21:42:47 tgl Exp $
33
Postgres documentation
44
-->
55

@@ -105,7 +105,9 @@ ERROR: RemoveType: type '<replaceable class="parameter">typename</replaceable>'
105105
<para>
106106
It is the user's responsibility to remove any operators,
107107
functions, aggregates, access methods, subtypes, and tables that
108-
use a deleted type.
108+
use a deleted type. However, the associated array datatype
109+
(which was automatically created by <command>CREATE TYPE</command>)
110+
will be removed automatically.
109111
</para>
110112
</listitem>
111113

0 commit comments

Comments
 (0)