]> granicus.if.org Git - postgresql/blob - doc/src/sgml/datatype.sgml
Add documentation about the issues of casting MONEY to/from numeric
[postgresql] / doc / src / sgml / datatype.sgml
1 <!-- $PostgreSQL: pgsql/doc/src/sgml/datatype.sgml,v 1.218 2007/11/27 05:49:58 momjian Exp $ -->
2
3  <chapter id="datatype">
4   <title id="datatype-title">Data Types</title>
5
6   <indexterm zone="datatype">
7    <primary>data type</primary>
8   </indexterm>
9
10   <indexterm>
11    <primary>type</primary>
12    <see>data type</see>
13   </indexterm>
14
15   <para>
16    <productname>PostgreSQL</productname> has a rich set of native data
17    types available to users.  Users can add new types to
18    <productname>PostgreSQL</productname> using the <xref
19    linkend="sql-createtype" endterm="sql-createtype-title"> command.
20   </para>
21
22   <para>
23    <xref linkend="datatype-table"> shows all the built-in general-purpose data
24    types. Most of the alternative names listed in the 
25    <quote>Aliases</quote> column are the names used internally by
26    <productname>PostgreSQL</productname> for historical reasons.  In
27    addition, some internally used or deprecated types are available,
28    but they are not listed here.
29   </para>
30
31    <table id="datatype-table">
32     <title>Data Types</title>
33     <tgroup cols="3">
34      <thead>
35       <row>
36        <entry>Name</entry>
37        <entry>Aliases</entry>
38        <entry>Description</entry>
39       </row>
40      </thead>
41
42      <tbody>
43       <row>
44        <entry><type>bigint</type></entry>
45        <entry><type>int8</type></entry>
46        <entry>signed eight-byte integer</entry>
47       </row>
48
49       <row>
50        <entry><type>bigserial</type></entry>
51        <entry><type>serial8</type></entry>
52        <entry>autoincrementing eight-byte integer</entry>
53       </row>
54
55       <row>
56        <entry><type>bit [ (<replaceable>n</replaceable>) ]</type></entry>
57        <entry></entry>
58        <entry>fixed-length bit string</entry>
59       </row>
60
61       <row>
62        <entry><type>bit varying [ (<replaceable>n</replaceable>) ]</type></entry>
63        <entry><type>varbit</type></entry>
64        <entry>variable-length bit string</entry>
65       </row>
66
67       <row>
68        <entry><type>boolean</type></entry>
69        <entry><type>bool</type></entry>
70        <entry>logical Boolean (true/false)</entry>
71       </row>
72
73       <row>
74        <entry><type>box</type></entry>
75        <entry></entry>
76        <entry>rectangular box in the plane</entry>
77       </row>
78
79       <row>
80        <entry><type>bytea</type></entry>
81        <entry></entry>
82        <entry>binary data (<quote>byte array</>)</entry>
83       </row>
84
85       <row>
86        <entry><type>character varying [ (<replaceable>n</replaceable>) ]</type></entry>
87        <entry><type>varchar [ (<replaceable>n</replaceable>) ]</type></entry>
88        <entry>variable-length character string</entry>
89       </row>
90
91       <row>
92        <entry><type>character [ (<replaceable>n</replaceable>) ]</type></entry>
93        <entry><type>char [ (<replaceable>n</replaceable>) ]</type></entry>
94        <entry>fixed-length character string</entry>
95       </row>
96
97       <row>
98        <entry><type>cidr</type></entry>
99        <entry></entry>
100        <entry>IPv4 or IPv6 network address</entry>
101       </row>
102
103       <row>
104        <entry><type>circle</type></entry>
105        <entry></entry>
106        <entry>circle in the plane</entry>
107       </row>
108
109       <row>
110        <entry><type>date</type></entry>
111        <entry></entry>
112        <entry>calendar date (year, month, day)</entry>
113       </row>
114
115       <row>
116        <entry><type>double precision</type></entry>
117        <entry><type>float8</type></entry>
118        <entry>double precision floating-point number</entry>
119       </row>
120
121       <row>
122        <entry><type>inet</type></entry>
123        <entry></entry>
124        <entry>IPv4 or IPv6 host address</entry>
125       </row>
126
127       <row>
128        <entry><type>integer</type></entry>
129        <entry><type>int</type>, <type>int4</type></entry>
130        <entry>signed four-byte integer</entry>
131       </row>
132
133       <row>
134        <entry><type>interval [ (<replaceable>p</replaceable>) ]</type></entry>
135        <entry></entry>
136        <entry>time span</entry>
137       </row>
138
139       <row>
140        <entry><type>line</type></entry>
141        <entry></entry>
142        <entry>infinite line in the plane</entry>
143       </row>
144
145       <row>
146        <entry><type>lseg</type></entry>
147        <entry></entry>
148        <entry>line segment in the plane</entry>
149       </row>
150
151       <row>
152        <entry><type>macaddr</type></entry>
153        <entry></entry>
154        <entry>MAC address</entry>
155       </row>
156
157       <row>
158        <entry><type>money</type></entry>
159        <entry></entry>
160        <entry>currency amount</entry>
161       </row>
162
163       <row>
164        <entry><type>numeric [ (<replaceable>p</replaceable>,
165          <replaceable>s</replaceable>) ]</type></entry>
166        <entry><type>decimal [ (<replaceable>p</replaceable>,
167          <replaceable>s</replaceable>) ]</type></entry>
168        <entry>exact numeric of selectable precision</entry>
169       </row>
170
171       <row>
172        <entry><type>path</type></entry>
173        <entry></entry>
174        <entry>geometric path in the plane</entry>
175       </row>
176
177       <row>
178        <entry><type>point</type></entry>
179        <entry></entry>
180        <entry>geometric point in the plane</entry>
181       </row>
182
183       <row>
184        <entry><type>polygon</type></entry>
185        <entry></entry>
186        <entry>closed geometric path in the plane</entry>
187       </row>
188
189       <row>
190        <entry><type>real</type></entry>
191        <entry><type>float4</type></entry>
192        <entry>single precision floating-point number</entry>
193       </row>
194
195       <row>
196        <entry><type>smallint</type></entry>
197        <entry><type>int2</type></entry>
198        <entry>signed two-byte integer</entry>
199       </row>
200
201       <row>
202        <entry><type>serial</type></entry>
203        <entry><type>serial4</type></entry>
204        <entry>autoincrementing four-byte integer</entry>
205       </row>
206
207       <row>
208        <entry><type>text</type></entry>
209        <entry></entry>
210        <entry>variable-length character string</entry>
211       </row>
212
213       <row>
214        <entry><type>time [ (<replaceable>p</replaceable>) ] [ without time zone ]</type></entry>
215        <entry></entry>
216        <entry>time of day</entry>
217       </row>
218
219       <row>
220        <entry><type>time [ (<replaceable>p</replaceable>) ] with time zone</type></entry>
221        <entry><type>timetz</type></entry>
222        <entry>time of day, including time zone</entry>
223       </row>
224
225       <row>
226        <entry><type>timestamp [ (<replaceable>p</replaceable>) ] [ without time zone ]</type></entry>
227        <entry></entry>
228        <entry>date and time</entry>
229       </row>
230
231       <row>
232        <entry><type>timestamp [ (<replaceable>p</replaceable>) ] with time zone</type></entry>
233        <entry><type>timestamptz</type></entry>
234        <entry>date and time, including time zone</entry>
235       </row>
236
237       <row>
238        <entry><type>tsquery</type></entry>
239        <entry></entry>
240        <entry>text search query</entry>
241       </row>
242
243       <row>
244        <entry><type>tsvector</type></entry>
245        <entry></entry>
246        <entry>text search document</entry>
247       </row>
248
249       <row>
250        <entry><type>txid_snapshot</type></entry>
251        <entry></entry>
252        <entry>user-level transaction ID snapshot</entry>
253       </row>
254
255       <row>
256        <entry><type>uuid</type></entry>
257        <entry></entry>
258        <entry>universally unique identifier</entry>
259       </row>
260
261       <row>
262        <entry><type>xml</type></entry>
263        <entry></entry>
264        <entry>XML data</entry>
265       </row>
266      </tbody>
267     </tgroup>
268    </table>
269
270   <note>
271    <title>Compatibility</title>
272    <para>
273     The following types (or spellings thereof) are specified by
274     <acronym>SQL</acronym>: <type>bigint</type>, <type>bit</type>, <type>bit
275     varying</type>, <type>boolean</type>, <type>char</type>,
276     <type>character varying</type>, <type>character</type>,
277     <type>varchar</type>, <type>date</type>, <type>double
278     precision</type>, <type>integer</type>, <type>interval</type>,
279     <type>numeric</type>, <type>decimal</type>, <type>real</type>,
280     <type>smallint</type>, <type>time</type> (with or without time zone),
281     <type>timestamp</type> (with or without time zone),
282     <type>xml</type>.
283    </para>
284   </note>
285
286   <para>
287    Each data type has an external representation determined by its input
288    and output functions.  Many of the built-in types have
289    obvious external formats.  However, several types are either unique
290    to <productname>PostgreSQL</productname>, such as geometric
291    paths, or have several possibilities for formats, such as the date
292    and time types.
293    Some of the input and output functions are not invertible.  That is,
294    the result of an output function might lose accuracy when compared to
295    the original input.
296   </para>
297
298   <sect1 id="datatype-numeric">
299    <title>Numeric Types</title>
300
301    <indexterm zone="datatype-numeric">
302     <primary>data type</primary>
303     <secondary>numeric</secondary>
304    </indexterm>
305
306    <para>
307     Numeric types consist of two-, four-, and eight-byte integers,
308     four- and eight-byte floating-point numbers, and selectable-precision
309     decimals.  <xref linkend="datatype-numeric-table"> lists the
310     available types.
311    </para>
312
313     <table id="datatype-numeric-table">
314      <title>Numeric Types</title>
315      <tgroup cols="4">
316       <thead>
317        <row>
318         <entry>Name</entry>
319         <entry>Storage Size</entry>
320         <entry>Description</entry>
321         <entry>Range</entry>
322        </row>
323       </thead>
324
325       <tbody>
326        <row>
327         <entry><type>smallint</></entry>
328         <entry>2 bytes</entry>
329         <entry>small-range integer</entry>
330         <entry>-32768 to +32767</entry>
331        </row>
332        <row>
333         <entry><type>integer</></entry>
334         <entry>4 bytes</entry>
335         <entry>usual choice for integer</entry>
336         <entry>-2147483648 to +2147483647</entry>
337        </row>
338        <row>
339         <entry><type>bigint</></entry>
340         <entry>8 bytes</entry>
341         <entry>large-range integer</entry>
342         <entry>-9223372036854775808 to 9223372036854775807</entry>
343        </row>
344
345        <row>
346         <entry><type>decimal</></entry>
347         <entry>variable</entry>
348         <entry>user-specified precision, exact</entry>
349         <entry>no limit</entry>
350        </row>
351        <row>
352         <entry><type>numeric</></entry>
353         <entry>variable</entry>
354         <entry>user-specified precision, exact</entry>
355         <entry>no limit</entry>
356        </row>
357
358        <row>
359         <entry><type>real</></entry>
360         <entry>4 bytes</entry>
361         <entry>variable-precision, inexact</entry>
362         <entry>6 decimal digits precision</entry>
363        </row>
364        <row>
365         <entry><type>double precision</></entry>
366         <entry>8 bytes</entry>
367         <entry>variable-precision, inexact</entry>
368         <entry>15 decimal digits precision</entry>
369        </row>
370
371        <row>
372         <entry><type>serial</></entry>
373         <entry>4 bytes</entry>
374         <entry>autoincrementing integer</entry>
375         <entry>1 to 2147483647</entry>
376        </row>
377
378        <row>
379         <entry><type>bigserial</type></entry>
380         <entry>8 bytes</entry>
381         <entry>large autoincrementing integer</entry>
382         <entry>1 to 9223372036854775807</entry>
383        </row>
384       </tbody>
385      </tgroup>
386     </table>
387
388    <para>
389     The syntax of constants for the numeric types is described in
390     <xref linkend="sql-syntax-constants">.  The numeric types have a
391     full set of corresponding arithmetic operators and
392     functions. Refer to <xref linkend="functions"> for more
393     information.  The following sections describe the types in detail.
394    </para>
395
396    <sect2 id="datatype-int">
397     <title>Integer Types</title>
398
399     <indexterm zone="datatype-int">
400      <primary>integer</primary>
401     </indexterm>
402
403     <indexterm zone="datatype-int">
404      <primary>smallint</primary>
405     </indexterm>
406
407     <indexterm zone="datatype-int">
408      <primary>bigint</primary>
409     </indexterm>
410
411     <indexterm>
412      <primary>int4</primary>
413      <see>integer</see>
414     </indexterm>
415
416     <indexterm>
417      <primary>int2</primary>
418      <see>smallint</see>
419     </indexterm>
420
421     <indexterm>
422      <primary>int8</primary>
423      <see>bigint</see>
424     </indexterm>
425
426     <para>
427      The types <type>smallint</type>, <type>integer</type>, and
428      <type>bigint</type> store whole numbers, that is, numbers without
429      fractional components, of various ranges.  Attempts to store
430      values outside of the allowed range will result in an error.
431     </para>
432
433     <para>
434      The type <type>integer</type> is the usual choice, as it offers
435      the best balance between range, storage size, and performance.
436      The <type>smallint</type> type is generally only used if disk
437      space is at a premium.  The <type>bigint</type> type should only
438      be used if the <type>integer</type> range is not sufficient,
439      because the latter is definitely faster.
440     </para>
441
442     <para>
443      The <type>bigint</type> type might not function correctly on all
444      platforms, since it relies on compiler support for eight-byte
445      integers.  On a machine without such support, <type>bigint</type>
446      acts the same as <type>integer</type> (but still takes up eight
447      bytes of storage).  However, we are not aware of any reasonable
448      platform where this is actually the case.
449     </para>
450
451     <para>
452      <acronym>SQL</acronym> only specifies the integer types
453      <type>integer</type> (or <type>int</type>),
454      <type>smallint</type>, and <type>bigint</type>.  The
455      type names <type>int2</type>, <type>int4</type>, and
456      <type>int8</type> are extensions, which are shared with various
457      other <acronym>SQL</acronym> database systems.
458     </para>
459
460    </sect2>
461
462    <sect2 id="datatype-numeric-decimal">
463     <title>Arbitrary Precision Numbers</title>
464
465     <indexterm>
466      <primary>numeric (data type)</primary>
467     </indexterm>
468
469    <indexterm>
470     <primary>arbitrary precision numbers</primary>
471    </indexterm>
472
473     <indexterm>
474      <primary>decimal</primary>
475      <see>numeric</see>
476     </indexterm>
477
478     <para>
479      The type <type>numeric</type> can store numbers with up to 1000
480      digits of precision and perform calculations exactly. It is
481      especially recommended for storing monetary amounts and other
482      quantities where exactness is required. However, arithmetic on
483      <type>numeric</type> values is very slow compared to the integer
484      types, or to the floating-point types described in the next section.
485     </para>
486
487     <para>
488      In what follows we use these terms:  The
489      <firstterm>scale</firstterm> of a <type>numeric</type> is the
490      count of decimal digits in the fractional part, to the right of
491      the decimal point.  The <firstterm>precision</firstterm> of a
492      <type>numeric</type> is the total count of significant digits in
493      the whole number, that is, the number of digits to both sides of
494      the decimal point.  So the number 23.5141 has a precision of 6
495      and a scale of 4.  Integers can be considered to have a scale of
496      zero.
497     </para>
498
499     <para>
500      Both the maximum precision and the maximum scale of a
501      <type>numeric</type> column can be
502      configured.  To declare a column of type <type>numeric</type> use
503      the syntax:
504 <programlisting>
505 NUMERIC(<replaceable>precision</replaceable>, <replaceable>scale</replaceable>)
506 </programlisting>
507      The precision must be positive, the scale zero or positive.
508      Alternatively:
509 <programlisting>
510 NUMERIC(<replaceable>precision</replaceable>)
511 </programlisting>
512      selects a scale of 0.  Specifying:
513 <programlisting>
514 NUMERIC
515 </programlisting>
516      without any precision or scale creates a column in which numeric
517      values of any precision and scale can be stored, up to the
518      implementation limit on precision.  A column of this kind will
519      not coerce input values to any particular scale, whereas
520      <type>numeric</type> columns with a declared scale will coerce
521      input values to that scale.  (The <acronym>SQL</acronym> standard
522      requires a default scale of 0, i.e., coercion to integer
523      precision.  We find this a bit useless.  If you're concerned
524      about portability, always specify the precision and scale
525      explicitly.)
526     </para>
527
528     <para>
529      If the scale of a value to be stored is greater than the declared
530      scale of the column, the system will round the value to the specified
531      number of fractional digits.  Then, if the number of digits to the
532      left of the decimal point exceeds the declared precision minus the
533      declared scale, an error is raised.
534     </para>
535
536     <para>
537      Numeric values are physically stored without any extra leading or
538      trailing zeroes.  Thus, the declared precision and scale of a column
539      are maximums, not fixed allocations.  (In this sense the <type>numeric</>
540      type is more akin to <type>varchar(<replaceable>n</>)</type>
541      than to <type>char(<replaceable>n</>)</type>.)  The actual storage
542      requirement is two bytes for each group of four decimal digits,
543      plus five to eight bytes overhead.
544     </para>
545
546     <indexterm>
547      <primary>NaN</primary>
548      <see>not a number</see>
549    </indexterm>
550
551     <indexterm>
552      <primary>not a number</primary>
553      <secondary>numeric (data type)</secondary>
554     </indexterm>
555
556     <para>
557      In addition to ordinary numeric values, the <type>numeric</type>
558      type allows the special value <literal>NaN</>, meaning 
559      <quote>not-a-number</quote>.  Any operation on <literal>NaN</>
560      yields another <literal>NaN</>.  When writing this value
561      as a constant in a SQL command, you must put quotes around it,
562      for example <literal>UPDATE table SET x = 'NaN'</>.  On input,
563      the string <literal>NaN</> is recognized in a case-insensitive manner.
564     </para>
565
566     <note>
567      <para>
568       In most implementations of the <quote>not-a-number</> concept,
569       <literal>NaN</> is not considered equal to any other numeric
570       value (including <literal>NaN</>).  In order to allow
571       <type>numeric</> values to be sorted and used in tree-based
572       indexes, <productname>PostgreSQL</> treats <literal>NaN</>
573       values as equal, and greater than all non-<literal>NaN</>
574       values.
575      </para>
576     </note>
577
578     <para>
579      The types <type>decimal</type> and <type>numeric</type> are
580      equivalent.  Both types are part of the <acronym>SQL</acronym>
581      standard.
582     </para>
583    </sect2>
584
585
586    <sect2 id="datatype-float">
587     <title>Floating-Point Types</title>
588
589     <indexterm zone="datatype-float">
590      <primary>real</primary>
591     </indexterm>
592
593     <indexterm zone="datatype-float">
594      <primary>double precision</primary>
595     </indexterm>
596
597     <indexterm>
598      <primary>float4</primary>
599      <see>real</see>
600     </indexterm>
601
602     <indexterm>
603      <primary>float8</primary>
604      <see>double precision</see>
605     </indexterm>
606
607     <indexterm zone="datatype-float">
608      <primary>floating point</primary>
609     </indexterm>
610
611     <para>
612      The data types <type>real</type> and <type>double
613      precision</type> are inexact, variable-precision numeric types.
614      In practice, these types are usually implementations of
615      <acronym>IEEE</acronym> Standard 754 for Binary Floating-Point
616      Arithmetic (single and double precision, respectively), to the
617      extent that the underlying processor, operating system, and
618      compiler support it.
619     </para>
620
621     <para>
622      Inexact means that some values cannot be converted exactly to the
623      internal format and are stored as approximations, so that storing
624      and printing back out a value might show slight discrepancies.
625      Managing these errors and how they propagate through calculations
626      is the subject of an entire branch of mathematics and computer
627      science and will not be discussed further here, except for the
628      following points:
629      <itemizedlist>
630       <listitem>
631        <para>
632         If you require exact storage and calculations (such as for
633         monetary amounts), use the <type>numeric</type> type instead.
634        </para>
635       </listitem>
636
637       <listitem>
638        <para>
639         If you want to do complicated calculations with these types
640         for anything important, especially if you rely on certain
641         behavior in boundary cases (infinity, underflow), you should
642         evaluate the implementation carefully.
643        </para>
644       </listitem>
645
646       <listitem>
647        <para>
648         Comparing two floating-point values for equality might or might
649         not work as expected.
650        </para>
651       </listitem>
652      </itemizedlist>
653     </para>
654
655     <para>
656      On most platforms, the <type>real</type> type has a range of at least
657      1E-37 to 1E+37 with a precision of at least 6 decimal digits.  The
658      <type>double precision</type> type typically has a range of around
659      1E-307 to 1E+308 with a precision of at least 15 digits.  Values that
660      are too large or too small will cause an error.  Rounding might
661      take place if the precision of an input number is too high.
662      Numbers too close to zero that are not representable as distinct
663      from zero will cause an underflow error.
664     </para>
665
666     <indexterm>
667      <primary>not a number</primary>
668      <secondary>double precision</secondary>
669     </indexterm>
670
671     <para>
672      In addition to ordinary numeric values, the floating-point types
673      have several special values:
674 <literallayout>
675 <literal>Infinity</literal>
676 <literal>-Infinity</literal>
677 <literal>NaN</literal>
678 </literallayout>
679      These represent the IEEE 754 special values
680      <quote>infinity</quote>, <quote>negative infinity</quote>, and
681      <quote>not-a-number</quote>, respectively.  (On a machine whose
682      floating-point arithmetic does not follow IEEE 754, these values
683      will probably not work as expected.)  When writing these values
684      as constants in a SQL command, you must put quotes around them,
685      for example <literal>UPDATE table SET x = 'Infinity'</>.  On input,
686      these strings are recognized in a case-insensitive manner.
687     </para>
688
689     <note>
690      <para>
691       IEEE754 specifies that <literal>NaN</> should not compare equal
692       to any other floating-point value (including <literal>NaN</>).
693       In order to allow floating-point values to be sorted and used
694       in tree-based indexes, <productname>PostgreSQL</> treats
695       <literal>NaN</> values as equal, and greater than all
696       non-<literal>NaN</> values.
697      </para>
698     </note>
699
700     <para>
701      <productname>PostgreSQL</productname> also supports the SQL-standard
702      notations <type>float</type> and
703      <type>float(<replaceable>p</replaceable>)</type> for specifying
704      inexact numeric types.  Here, <replaceable>p</replaceable> specifies
705      the minimum acceptable precision in binary digits.
706      <productname>PostgreSQL</productname> accepts 
707      <type>float(1)</type> to <type>float(24)</type> as selecting the
708      <type>real</type> type, while 
709      <type>float(25)</type> to <type>float(53)</type> select
710      <type>double precision</type>.  Values of <replaceable>p</replaceable>
711      outside the allowed range draw an error.
712      <type>float</type> with no precision specified is taken to mean
713      <type>double precision</type>.
714     </para>
715
716     <note>
717      <para>
718       Prior to <productname>PostgreSQL</productname> 7.4, the precision in
719       <type>float(<replaceable>p</replaceable>)</type> was taken to mean
720       so many decimal digits.  This has been corrected to match the SQL
721       standard, which specifies that the precision is measured in binary
722       digits.  The assumption that <type>real</type> and
723       <type>double precision</type> have exactly 24 and 53 bits in the
724       mantissa respectively is correct for IEEE-standard floating point
725       implementations.  On non-IEEE platforms it might be off a little, but
726       for simplicity the same ranges of <replaceable>p</replaceable> are used
727       on all platforms.
728      </para>
729     </note>
730
731    </sect2>
732
733    <sect2 id="datatype-serial">
734     <title>Serial Types</title>
735
736     <indexterm zone="datatype-serial">
737      <primary>serial</primary>
738     </indexterm>
739
740     <indexterm zone="datatype-serial">
741      <primary>bigserial</primary>
742     </indexterm>
743
744     <indexterm zone="datatype-serial">
745      <primary>serial4</primary>
746     </indexterm>
747
748     <indexterm zone="datatype-serial">
749      <primary>serial8</primary>
750     </indexterm>
751
752     <indexterm>
753      <primary>auto-increment</primary>
754      <see>serial</see>
755     </indexterm>
756
757     <indexterm>
758      <primary>sequence</primary>
759      <secondary>and serial type</secondary>
760     </indexterm>
761
762     <para>
763      The data types <type>serial</type> and <type>bigserial</type>
764      are not true types, but merely
765      a notational convenience for setting up unique identifier columns
766      (similar to the <literal>AUTO_INCREMENT</literal> property
767      supported by some other databases). In the current
768      implementation, specifying:
769
770 <programlisting>
771 CREATE TABLE <replaceable class="parameter">tablename</replaceable> (
772     <replaceable class="parameter">colname</replaceable> SERIAL
773 );
774 </programlisting>
775
776      is equivalent to specifying:
777
778 <programlisting>
779 CREATE SEQUENCE <replaceable class="parameter">tablename</replaceable>_<replaceable class="parameter">colname</replaceable>_seq;
780 CREATE TABLE <replaceable class="parameter">tablename</replaceable> (
781     <replaceable class="parameter">colname</replaceable> integer NOT NULL DEFAULT nextval('<replaceable class="parameter">tablename</replaceable>_<replaceable class="parameter">colname</replaceable>_seq')
782 );
783 ALTER SEQUENCE <replaceable class="parameter">tablename</replaceable>_<replaceable class="parameter">colname</replaceable>_seq OWNED BY <replaceable class="parameter">tablename</replaceable>.<replaceable class="parameter">colname</replaceable>;
784 </programlisting>
785
786      Thus, we have created an integer column and arranged for its default
787      values to be assigned from a sequence generator.  A <literal>NOT NULL</>
788      constraint is applied to ensure that a null value cannot be explicitly
789      inserted, either.  (In most cases you would also want to attach a
790      <literal>UNIQUE</> or <literal>PRIMARY KEY</> constraint to prevent
791      duplicate values from being inserted by accident, but this is
792      not automatic.)  Lastly, the sequence is marked as <quote>owned by</>
793      the column, so that it will be dropped if the column or table is dropped.
794     </para>
795
796     <note>
797      <para>
798       Prior to <productname>PostgreSQL</productname> 7.3, <type>serial</type>
799       implied <literal>UNIQUE</literal>.  This is no longer automatic.  If
800       you wish a serial column to be in a unique constraint or a 
801       primary key, it must now be specified, same as with
802       any other data type.
803      </para>
804     </note>
805
806     <para>
807      To insert the next value of the sequence into the <type>serial</type>
808      column, specify that the <type>serial</type>
809      column should be assigned its default value. This can be done
810      either by excluding the column from the list of columns in
811      the <command>INSERT</command> statement, or through the use of
812      the <literal>DEFAULT</literal> key word.
813     </para>
814
815     <para>
816      The type names <type>serial</type> and <type>serial4</type> are
817      equivalent: both create <type>integer</type> columns.  The type
818      names <type>bigserial</type> and <type>serial8</type> work just
819      the same way, except that they create a <type>bigint</type>
820      column.  <type>bigserial</type> should be used if you anticipate
821      the use of more than 2<superscript>31</> identifiers over the
822      lifetime of the table.
823     </para>
824
825     <para>
826      The sequence created for a <type>serial</type> column is
827      automatically dropped when the owning column is dropped.
828      You can drop the sequence without dropping the column, but this
829      will force removal of the column default expression.
830     </para>
831    </sect2>
832   </sect1>
833
834   <sect1 id="datatype-money">
835    <title>Monetary Types</title>
836
837    <para>
838     The <type>money</type> type stores a currency amount with a fixed
839     fractional precision; see <xref
840     linkend="datatype-money-table">.
841     Input is accepted in a variety of formats, including integer and
842     floating-point literals, as well as <quote>typical</quote>
843     currency formatting, such as <literal>'$1,000.00'</literal>.
844     Output is generally in the latter form but depends on the locale.
845     Non-quoted numeric values can be converted to <type>money</type> by
846     casting the numeric value to <type>text</type> and then
847     <type>money</type>:
848 <programlisting>
849 SELECT 1234::text::money;
850 </programlisting>
851     There is no simple way of doing the reverse;  casting a <type>money</type> value to a
852     numeric type.
853    </para>
854
855    <para>
856     Since the output of this data type is locale-sensitive, it may not
857     work to load <type>money</> data into a database that has a different
858     setting of <varname>lc_monetary</>.  To avoid problems, before
859     restoring a dump make sure <varname>lc_monetary</> has the same or
860     equivalent value as in the database that was dumped.
861    </para>
862
863     <table id="datatype-money-table">
864      <title>Monetary Types</title>
865      <tgroup cols="4">
866       <thead>
867        <row>
868         <entry>Name</entry>
869         <entry>Storage Size</entry>
870         <entry>Description</entry>
871         <entry>Range</entry>
872        </row>
873       </thead>
874       <tbody>
875        <row>
876         <entry>money</entry>
877         <entry>8 bytes</entry>
878         <entry>currency amount</entry>
879         <entry>-92233720368547758.08 to +92233720368547758.07</entry>
880        </row>
881       </tbody>
882      </tgroup>
883     </table>
884   </sect1>
885
886
887   <sect1 id="datatype-character">
888    <title>Character Types</title>
889
890    <indexterm zone="datatype-character">
891     <primary>character string</primary>
892     <secondary>data types</secondary>
893    </indexterm>
894
895    <indexterm>
896     <primary>string</primary>
897     <see>character string</see>
898    </indexterm>
899
900    <indexterm zone="datatype-character">
901     <primary>character</primary>
902    </indexterm>
903
904    <indexterm zone="datatype-character">
905     <primary>character varying</primary>
906    </indexterm>
907
908    <indexterm zone="datatype-character">
909     <primary>text</primary>
910    </indexterm>
911
912    <indexterm zone="datatype-character">
913     <primary>char</primary>
914    </indexterm>
915
916    <indexterm zone="datatype-character">
917     <primary>varchar</primary>
918    </indexterm>
919
920     <table id="datatype-character-table">
921      <title>Character Types</title>
922      <tgroup cols="2">
923       <thead>
924        <row>
925         <entry>Name</entry>
926         <entry>Description</entry>
927        </row>
928       </thead>
929       <tbody>
930        <row>
931         <entry><type>character varying(<replaceable>n</>)</type>, <type>varchar(<replaceable>n</>)</type></entry>
932         <entry>variable-length with limit</entry>
933        </row>
934        <row>
935         <entry><type>character(<replaceable>n</>)</type>, <type>char(<replaceable>n</>)</type></entry>
936         <entry>fixed-length, blank padded</entry>
937        </row>
938        <row>
939         <entry><type>text</type></entry>
940         <entry>variable unlimited length</entry>
941        </row>
942      </tbody>
943      </tgroup>
944     </table>
945
946    <para>
947     <xref linkend="datatype-character-table"> shows the
948     general-purpose character types available in
949     <productname>PostgreSQL</productname>.
950    </para>
951
952    <para>
953     <acronym>SQL</acronym> defines two primary character types:
954     <type>character varying(<replaceable>n</>)</type> and
955     <type>character(<replaceable>n</>)</type>, where <replaceable>n</>
956     is a positive integer.  Both of these types can store strings up to
957     <replaceable>n</> characters in length.  An attempt to store a
958     longer string into a column of these types will result in an
959     error, unless the excess characters are all spaces, in which case
960     the string will be truncated to the maximum length. (This somewhat
961     bizarre exception is required by the <acronym>SQL</acronym>
962     standard.) If the string to be stored is shorter than the declared
963     length, values of type <type>character</type> will be space-padded;
964     values of type <type>character varying</type> will simply store the
965     shorter
966     string.
967    </para>
968
969    <para>
970     If one explicitly casts a value to <type>character
971     varying(<replaceable>n</>)</type> or
972     <type>character(<replaceable>n</>)</type>, then an over-length
973     value will be truncated to <replaceable>n</> characters without
974     raising an error. (This too is required by the
975     <acronym>SQL</acronym> standard.)
976    </para>
977
978    <para>
979     The notations <type>varchar(<replaceable>n</>)</type> and
980     <type>char(<replaceable>n</>)</type> are aliases for <type>character
981     varying(<replaceable>n</>)</type> and
982     <type>character(<replaceable>n</>)</type>, respectively.
983     <type>character</type> without length specifier is equivalent to
984     <type>character(1)</type>. If <type>character varying</type> is used
985     without length specifier, the type accepts strings of any size. The
986     latter is a <productname>PostgreSQL</> extension.
987    </para>
988
989    <para>
990     In addition, <productname>PostgreSQL</productname> provides the
991     <type>text</type> type, which stores strings of any length.
992     Although the type <type>text</type> is not in the
993     <acronym>SQL</acronym> standard, several other SQL database
994     management systems have it as well.
995    </para>
996
997    <para>
998     Values of type <type>character</type> are physically padded
999     with spaces to the specified width <replaceable>n</>, and are
1000     stored and displayed that way.  However, the padding spaces are
1001     treated as semantically insignificant.  Trailing spaces are
1002     disregarded when comparing two values of type <type>character</type>,
1003     and they will be removed when converting a <type>character</type> value
1004     to one of the other string types.  Note that trailing spaces
1005     <emphasis>are</> semantically significant in
1006     <type>character varying</type> and <type>text</type> values.
1007    </para>
1008
1009    <para>
1010     The storage requirement for a short string (up to 126 bytes) is 1 byte
1011     plus the actual string, which includes the space padding in the case of
1012     <type>character</type>.  Longer strings have 4 bytes overhead instead
1013     of 1.  Long strings are compressed by the system automatically, so
1014     the physical requirement on disk might be less. Very long values are also
1015     stored in background tables so that they do not interfere with rapid
1016     access to shorter column values. In any case, the longest
1017     possible character string that can be stored is about 1 GB. (The
1018     maximum value that will be allowed for <replaceable>n</> in the data
1019     type declaration is less than that. It wouldn't be very useful to
1020     change this because with multibyte character encodings the number of
1021     characters and bytes can be quite different anyway. If you desire to
1022     store long strings with no specific upper limit, use
1023     <type>text</type> or <type>character varying</type> without a length
1024     specifier, rather than making up an arbitrary length limit.)
1025    </para>
1026
1027    <tip>
1028     <para>
1029      There are no performance differences between these three types,
1030      apart from increased storage size when using the blank-padded
1031      type, and a few extra cycles to check the length when storing into
1032      a length-constrained column.  While
1033      <type>character(<replaceable>n</>)</type> has performance
1034      advantages in some other database systems, it has no such advantages in
1035      <productname>PostgreSQL</productname>.  In most situations
1036      <type>text</type> or <type>character varying</type> should be used
1037      instead.
1038     </para>
1039    </tip>
1040
1041    <para>
1042     Refer to <xref linkend="sql-syntax-strings"> for information about
1043     the syntax of string literals, and to <xref linkend="functions">
1044     for information about available operators and functions. The
1045     database character set determines the character set used to store
1046     textual values; for more information on character set support,
1047     refer to <xref linkend="multibyte">.
1048    </para>
1049
1050    <example>
1051     <title>Using the character types</title>
1052
1053 <programlisting>
1054 CREATE TABLE test1 (a character(4));
1055 INSERT INTO test1 VALUES ('ok');
1056 SELECT a, char_length(a) FROM test1; -- <co id="co.datatype-char">
1057 <computeroutput>
1058   a   | char_length
1059 ------+-------------
1060  ok   |           2
1061 </computeroutput>
1062
1063 CREATE TABLE test2 (b varchar(5));
1064 INSERT INTO test2 VALUES ('ok');
1065 INSERT INTO test2 VALUES ('good      ');
1066 INSERT INTO test2 VALUES ('too long');
1067 <computeroutput>ERROR:  value too long for type character varying(5)</computeroutput>
1068 INSERT INTO test2 VALUES ('too long'::varchar(5)); -- explicit truncation
1069 SELECT b, char_length(b) FROM test2;
1070 <computeroutput>
1071    b   | char_length
1072 -------+-------------
1073  ok    |           2
1074  good  |           5
1075  too l |           5
1076 </computeroutput>
1077 </programlisting>
1078     <calloutlist>
1079      <callout arearefs="co.datatype-char">
1080       <para>
1081        The <function>char_length</function> function is discussed in
1082        <xref linkend="functions-string">.
1083       </para>
1084      </callout>
1085     </calloutlist>
1086    </example>
1087
1088    <para>
1089     There are two other fixed-length character types in
1090     <productname>PostgreSQL</productname>, shown in <xref
1091     linkend="datatype-character-special-table">. The <type>name</type>
1092     type exists <emphasis>only</emphasis> for storage of identifiers
1093     in the internal system catalogs and is not intended for use by the general user. Its
1094     length is currently defined as 64 bytes (63 usable characters plus
1095     terminator) but should be referenced using the constant
1096     <symbol>NAMEDATALEN</symbol>. The length is set at compile time (and
1097     is therefore adjustable for special uses); the default maximum
1098     length might change in a future release. The type <type>"char"</type>
1099     (note the quotes) is different from <type>char(1)</type> in that it
1100     only uses one byte of storage. It is internally used in the system
1101     catalogs as a poor-man's enumeration type.
1102    </para>
1103
1104     <table id="datatype-character-special-table">
1105      <title>Special Character Types</title>
1106      <tgroup cols="3">
1107       <thead>
1108        <row>
1109         <entry>Name</entry>
1110         <entry>Storage Size</entry>
1111         <entry>Description</entry>
1112        </row>
1113       </thead>
1114       <tbody>
1115        <row>
1116         <entry><type>"char"</type></entry>
1117         <entry>1 byte</entry>
1118         <entry>single-byte internal type</entry>
1119        </row>
1120        <row>
1121         <entry><type>name</type></entry>
1122         <entry>64 bytes</entry>
1123         <entry>internal type for object names</entry>
1124        </row>
1125       </tbody>
1126      </tgroup>
1127     </table>
1128
1129   </sect1>
1130
1131  <sect1 id="datatype-binary">
1132   <title>Binary Data Types</title>
1133
1134   <indexterm zone="datatype-binary">
1135    <primary>binary data</primary>
1136   </indexterm>
1137
1138   <indexterm zone="datatype-binary">
1139    <primary>bytea</primary>
1140   </indexterm>
1141
1142    <para>
1143     The <type>bytea</type> data type allows storage of binary strings;
1144     see <xref linkend="datatype-binary-table">.
1145    </para>
1146
1147    <table id="datatype-binary-table">
1148     <title>Binary Data Types</title>
1149     <tgroup cols="3">
1150      <thead>
1151       <row>
1152        <entry>Name</entry>
1153        <entry>Storage Size</entry>
1154        <entry>Description</entry>
1155       </row>
1156      </thead>
1157      <tbody>
1158       <row>
1159        <entry><type>bytea</type></entry>
1160        <entry>1 or 4 bytes plus the actual binary string</entry>
1161        <entry>variable-length binary string</entry>
1162       </row>
1163      </tbody>
1164     </tgroup>
1165    </table>
1166
1167    <para>
1168     A binary string is a sequence of octets (or bytes).  Binary
1169     strings are distinguished from character strings by two
1170     characteristics: First, binary strings specifically allow storing
1171     octets of value zero and other <quote>non-printable</quote>
1172     octets (usually, octets outside the range 32 to 126).
1173     Character strings disallow zero octets, and also disallow any
1174     other octet values and sequences of octet values that are invalid
1175     according to the database's selected character set encoding.
1176     Second, operations on binary strings process the actual bytes,
1177     whereas the processing of character strings depends on locale settings.
1178     In short, binary strings are appropriate for storing data that the
1179     programmer thinks of as <quote>raw bytes</>, whereas character
1180     strings are appropriate for storing text.
1181    </para>
1182
1183    <para>
1184     When entering <type>bytea</type> values, octets of certain
1185     values <emphasis>must</emphasis> be escaped (but all octet
1186     values <emphasis>can</emphasis> be escaped) when used as part
1187     of a string literal in an <acronym>SQL</acronym> statement. In
1188     general, to escape an octet, it is converted into the three-digit
1189     octal number equivalent of its decimal octet value, and preceded
1190     by two backslashes.  <xref linkend="datatype-binary-sqlesc">
1191     shows the characters that must be escaped, and gives the alternative
1192     escape sequences where applicable.
1193    </para>
1194
1195    <table id="datatype-binary-sqlesc">
1196     <title><type>bytea</> Literal Escaped Octets</title>
1197     <tgroup cols="5">
1198      <thead>
1199       <row>
1200        <entry>Decimal Octet Value</entry>
1201        <entry>Description</entry>
1202        <entry>Escaped Input Representation</entry>
1203        <entry>Example</entry>
1204        <entry>Output Representation</entry>
1205       </row>
1206      </thead>
1207
1208      <tbody>
1209       <row>
1210        <entry>0</entry>
1211        <entry>zero octet</entry>
1212        <entry><literal>E'\\000'</literal></entry>
1213        <entry><literal>SELECT E'\\000'::bytea;</literal></entry>
1214        <entry><literal>\000</literal></entry>
1215       </row>
1216
1217       <row>
1218        <entry>39</entry>
1219        <entry>single quote</entry>
1220        <entry><literal>''''</literal> or <literal>E'\\047'</literal></entry>
1221        <entry><literal>SELECT E'\''::bytea;</literal></entry>
1222        <entry><literal>'</literal></entry>
1223       </row>
1224
1225       <row>
1226        <entry>92</entry>
1227        <entry>backslash</entry>
1228        <entry><literal>E'\\\\'</literal> or <literal>E'\\134'</literal></entry>
1229        <entry><literal>SELECT E'\\\\'::bytea;</literal></entry>
1230        <entry><literal>\\</literal></entry>
1231       </row>
1232
1233       <row>
1234        <entry>0 to 31 and 127 to 255</entry>
1235        <entry><quote>non-printable</quote> octets</entry>
1236        <entry><literal>E'\\<replaceable>xxx'</></literal> (octal value)</entry>
1237        <entry><literal>SELECT E'\\001'::bytea;</literal></entry>
1238        <entry><literal>\001</literal></entry>
1239       </row>
1240
1241      </tbody>
1242     </tgroup>
1243    </table>
1244
1245    <para>
1246     The requirement to escape <quote>non-printable</quote> octets actually
1247     varies depending on locale settings. In some instances you can get away
1248     with leaving them unescaped. Note that the result in each of the examples
1249     in <xref linkend="datatype-binary-sqlesc"> was exactly one octet in
1250     length, even though the output representation of the zero octet and
1251     backslash are more than one character.
1252    </para>
1253
1254    <para>
1255     The reason that you have to write so many backslashes, as shown
1256     in <xref linkend="datatype-binary-sqlesc">, is that an input
1257     string written as a string literal must pass through two parse
1258     phases in the <productname>PostgreSQL</productname> server.
1259     The first backslash of each pair is interpreted as an escape
1260     character by the string-literal parser (assuming escape string
1261     syntax is used) and is therefore consumed, leaving the second backslash of the
1262     pair.  (Dollar-quoted strings can be used to avoid this level
1263     of escaping.)  The remaining backslash is then recognized by the
1264     <type>bytea</type> input function as starting either a three
1265     digit octal value or escaping another backslash.  For example,
1266     a string literal passed to the server as <literal>E'\\001'</literal>
1267     becomes <literal>\001</literal> after passing through the
1268     escape string parser. The <literal>\001</literal> is then sent
1269     to the <type>bytea</type> input function, where it is converted
1270     to a single octet with a decimal value of 1.  Note that the
1271     single-quote character is not treated specially by <type>bytea</type>,
1272     so it follows the normal rules for string literals.  (See also
1273     <xref linkend="sql-syntax-strings">.)
1274    </para>
1275
1276    <para>
1277     <type>Bytea</type> octets are also escaped in the output. In general, each
1278     <quote>non-printable</quote> octet is converted into
1279     its equivalent three-digit octal value and preceded by one backslash.
1280     Most <quote>printable</quote> octets are represented by their standard
1281     representation in the client character set. The octet with decimal
1282     value 92 (backslash) has a special alternative output representation.
1283     Details are in <xref linkend="datatype-binary-resesc">.
1284    </para>
1285
1286    <table id="datatype-binary-resesc">
1287     <title><type>bytea</> Output Escaped Octets</title>
1288     <tgroup cols="5">
1289      <thead>
1290       <row>
1291        <entry>Decimal Octet Value</entry>
1292        <entry>Description</entry>
1293        <entry>Escaped Output Representation</entry>
1294        <entry>Example</entry>
1295        <entry>Output Result</entry>
1296       </row>
1297      </thead>
1298
1299      <tbody>
1300
1301       <row>
1302        <entry>92</entry>
1303        <entry>backslash</entry>
1304        <entry><literal>\\</literal></entry>
1305        <entry><literal>SELECT E'\\134'::bytea;</literal></entry>
1306        <entry><literal>\\</literal></entry>
1307       </row>
1308
1309       <row>
1310        <entry>0 to 31 and 127 to 255</entry>
1311        <entry><quote>non-printable</quote> octets</entry>
1312        <entry><literal>\<replaceable>xxx</></literal> (octal value)</entry>
1313        <entry><literal>SELECT E'\\001'::bytea;</literal></entry>
1314        <entry><literal>\001</literal></entry>
1315       </row>
1316
1317       <row>
1318        <entry>32 to 126</entry>
1319        <entry><quote>printable</quote> octets</entry>
1320        <entry>client character set representation</entry>
1321        <entry><literal>SELECT E'\\176'::bytea;</literal></entry>
1322        <entry><literal>~</literal></entry>
1323       </row>
1324
1325      </tbody>
1326     </tgroup>
1327    </table>
1328
1329    <para>
1330     Depending on the front end to <productname>PostgreSQL</> you use,
1331     you might have additional work to do in terms of escaping and
1332     unescaping <type>bytea</type> strings. For example, you might also
1333     have to escape line feeds and carriage returns if your interface
1334     automatically translates these.
1335    </para>
1336
1337    <para>
1338         The <acronym>SQL</acronym> standard defines a different binary
1339         string type, called <type>BLOB</type> or <type>BINARY LARGE
1340         OBJECT</type>.  The input format is different from
1341         <type>bytea</type>, but the provided functions and operators are
1342         mostly the same.
1343    </para>
1344  </sect1>
1345
1346
1347   <sect1 id="datatype-datetime">
1348    <title>Date/Time Types</title>
1349
1350    <indexterm zone="datatype-datetime">
1351     <primary>date</primary>
1352    </indexterm>
1353    <indexterm zone="datatype-datetime">
1354     <primary>time</primary>
1355    </indexterm>
1356    <indexterm zone="datatype-datetime">
1357     <primary>time without time zone</primary>
1358    </indexterm>
1359    <indexterm zone="datatype-datetime">
1360     <primary>time with time zone</primary>
1361    </indexterm>
1362    <indexterm zone="datatype-datetime">
1363     <primary>timestamp</primary>
1364    </indexterm>
1365    <indexterm zone="datatype-datetime">
1366     <primary>timestamp with time zone</primary>
1367    </indexterm>
1368    <indexterm zone="datatype-datetime">
1369     <primary>timestamp without time zone</primary>
1370    </indexterm>
1371    <indexterm zone="datatype-datetime">
1372     <primary>interval</primary>
1373    </indexterm>
1374    <indexterm zone="datatype-datetime">
1375     <primary>time span</primary>
1376    </indexterm>
1377
1378    <para>
1379     <productname>PostgreSQL</productname> supports the full set of
1380     <acronym>SQL</acronym> date and time types, shown in <xref
1381     linkend="datatype-datetime-table">.  The operations available
1382     on these data types are described in
1383     <xref linkend="functions-datetime">.
1384    </para>
1385
1386     <table id="datatype-datetime-table">
1387      <title>Date/Time Types</title>
1388      <tgroup cols="6">
1389       <thead>
1390        <row>
1391         <entry>Name</entry>
1392         <entry>Storage Size</entry>
1393         <entry>Description</entry>
1394         <entry>Low Value</entry>
1395         <entry>High Value</entry>
1396         <entry>Resolution</entry>
1397        </row>
1398       </thead>
1399       <tbody>
1400        <row>
1401         <entry><type>timestamp [ (<replaceable>p</replaceable>) ] [ without time zone ]</type></entry>
1402         <entry>8 bytes</entry>
1403         <entry>both date and time</entry>
1404         <entry>4713 BC</entry>
1405         <entry>5874897 AD</entry>
1406         <entry>1 microsecond / 14 digits</entry>
1407        </row>
1408        <row>
1409         <entry><type>timestamp [ (<replaceable>p</replaceable>) ] with time zone</type></entry>
1410         <entry>8 bytes</entry>
1411         <entry>both date and time, with time zone</entry>
1412         <entry>4713 BC</entry>
1413         <entry>5874897 AD</entry>
1414         <entry>1 microsecond / 14 digits</entry>
1415        </row>
1416        <row>
1417         <entry><type>interval [ (<replaceable>p</replaceable>) ]</type></entry>
1418         <entry>12 bytes</entry>
1419         <entry>time intervals</entry>
1420         <entry>-178000000 years</entry>
1421         <entry>178000000 years</entry>
1422         <entry>1 microsecond / 14 digits</entry>
1423        </row>
1424        <row>
1425         <entry><type>date</type></entry>
1426         <entry>4 bytes</entry>
1427         <entry>dates only</entry>
1428         <entry>4713 BC</entry>
1429         <entry>5874897 AD</entry>
1430         <entry>1 day</entry>
1431        </row>
1432        <row>
1433         <entry><type>time [ (<replaceable>p</replaceable>) ] [ without time zone ]</type></entry>
1434         <entry>8 bytes</entry>
1435         <entry>times of day only</entry>
1436         <entry>00:00:00</entry>
1437         <entry>24:00:00</entry>
1438         <entry>1 microsecond / 14 digits</entry>
1439        </row>
1440        <row>
1441         <entry><type>time [ (<replaceable>p</replaceable>) ] with time zone</type></entry>
1442         <entry>12 bytes</entry>
1443         <entry>times of day only, with time zone</entry>
1444         <entry>00:00:00+1459</entry>
1445         <entry>24:00:00-1459</entry>
1446         <entry>1 microsecond / 14 digits</entry>
1447        </row>
1448       </tbody>
1449      </tgroup>
1450     </table>
1451
1452    <note>
1453     <para>
1454      Prior to <productname>PostgreSQL</productname> 7.3, writing just
1455      <type>timestamp</type> was equivalent to <type>timestamp with
1456      time zone</type>.  This was changed for SQL compliance.
1457     </para>
1458    </note>
1459
1460    <para>
1461     <type>time</type>, <type>timestamp</type>, and
1462     <type>interval</type> accept an optional precision value
1463     <replaceable>p</replaceable> which specifies the number of
1464     fractional digits retained in the seconds field. By default, there
1465     is no explicit bound on precision.  The allowed range of
1466     <replaceable>p</replaceable> is from 0 to 6 for the
1467     <type>timestamp</type> and <type>interval</type> types.
1468    </para>
1469
1470    <note>
1471    <para>
1472     When <type>timestamp</> values are stored as double precision floating-point
1473     numbers (currently the default), the effective limit of precision
1474     might be less than 6. <type>timestamp</type> values are stored as seconds
1475     before or after midnight 2000-01-01.  Microsecond precision is achieved for
1476     dates within a few years of 2000-01-01, but the precision degrades for
1477     dates further away.  When <type>timestamp</type> values are stored as
1478     eight-byte integers (a compile-time
1479     option), microsecond precision is available over the full range of
1480     values. However eight-byte integer timestamps have a more limited range of
1481     dates than shown above: from 4713 BC up to 294276 AD.  The same
1482     compile-time option also determines whether <type>time</type> and
1483     <type>interval</type> values are stored as floating-point or eight-byte
1484     integers.  In the floating-point case, large <type>interval</type> values
1485     degrade in precision as the size of the interval increases.
1486    </para>
1487    </note>
1488
1489    <para>
1490     For the <type>time</type> types, the allowed range of
1491     <replaceable>p</replaceable> is from 0 to 6 when eight-byte integer
1492     storage is used, or from 0 to 10 when floating-point storage is used.
1493    </para>
1494
1495    <para>
1496     The type <type>time with time zone</type> is defined by the SQL
1497     standard, but the definition exhibits properties which lead to
1498     questionable usefulness. In most cases, a combination of
1499     <type>date</type>, <type>time</type>, <type>timestamp without time
1500     zone</type>, and <type>timestamp with time zone</type> should
1501     provide a complete range of date/time functionality required by
1502     any application.
1503    </para>
1504
1505    <para>
1506     The types <type>abstime</type>
1507     and <type>reltime</type> are lower precision types which are used internally.
1508     You are discouraged from using these types in new
1509     applications and are encouraged to move any old
1510     ones over when appropriate. Any or all of these internal types
1511     might disappear in a future release.
1512    </para>
1513
1514    <sect2 id="datatype-datetime-input">
1515     <title>Date/Time Input</title>
1516
1517     <para>
1518      Date and time input is accepted in almost any reasonable format, including
1519      ISO 8601, <acronym>SQL</acronym>-compatible, 
1520      traditional <productname>POSTGRES</productname>, and others.
1521      For some formats, ordering of month, day, and year in date input is
1522      ambiguous and there is support for specifying the expected
1523      ordering of these fields.  Set the <xref linkend="guc-datestyle"> parameter
1524      to <literal>MDY</> to select month-day-year interpretation,
1525      <literal>DMY</> to select day-month-year interpretation, or
1526      <literal>YMD</> to select year-month-day interpretation.
1527     </para>
1528
1529     <para>
1530      <productname>PostgreSQL</productname> is more flexible in
1531      handling date/time input than the
1532      <acronym>SQL</acronym> standard requires.
1533      See <xref linkend="datetime-appendix">
1534      for the exact parsing rules of date/time input and for the
1535      recognized text fields including months, days of the week, and
1536      time zones. 
1537     </para>
1538
1539     <para>
1540      Remember that any date or time literal input needs to be enclosed
1541      in single quotes, like text strings.  Refer to 
1542      <xref linkend="sql-syntax-constants-generic"> for more
1543      information.
1544      <acronym>SQL</acronym> requires the following syntax
1545 <synopsis>
1546 <replaceable>type</replaceable> [ (<replaceable>p</replaceable>) ] '<replaceable>value</replaceable>'
1547 </synopsis>
1548      where <replaceable>p</replaceable> in the optional precision
1549      specification is an integer corresponding to the number of
1550      fractional digits in the seconds field. Precision can be
1551      specified for <type>time</type>, <type>timestamp</type>, and
1552      <type>interval</type> types.  The allowed values are mentioned
1553      above.  If no precision is specified in a constant specification,
1554      it defaults to the precision of the literal value.
1555     </para>
1556
1557     <sect3>
1558     <title>Dates</title>
1559
1560     <indexterm>
1561      <primary>date</primary>
1562     </indexterm>
1563  
1564     <para>
1565      <xref linkend="datatype-datetime-date-table"> shows some possible
1566      inputs for the <type>date</type> type.
1567     </para>
1568
1569      <table id="datatype-datetime-date-table">
1570       <title>Date Input</title>
1571       <tgroup cols="2">
1572        <thead>
1573         <row>
1574          <entry>Example</entry>
1575          <entry>Description</entry>
1576         </row>
1577        </thead>
1578        <tbody>
1579         <row>
1580          <entry>January 8, 1999</entry>
1581          <entry>unambiguous in any <varname>datestyle</varname> input mode</entry>
1582         </row>
1583         <row>
1584          <entry>1999-01-08</entry>
1585          <entry>ISO 8601; January 8 in any mode
1586          (recommended format)</entry>
1587         </row>
1588         <row>
1589          <entry>1/8/1999</entry>
1590          <entry>January 8 in <literal>MDY</> mode;
1591           August 1 in <literal>DMY</> mode</entry>
1592         </row>
1593         <row>
1594          <entry>1/18/1999</entry>
1595          <entry>January 18 in <literal>MDY</> mode;
1596           rejected in other modes</entry>
1597         </row>
1598         <row>
1599          <entry>01/02/03</entry>
1600          <entry>January 2, 2003 in <literal>MDY</> mode;
1601           February 1, 2003 in <literal>DMY</> mode;
1602           February 3, 2001 in <literal>YMD</> mode
1603          </entry>
1604         </row>
1605         <row>
1606          <entry>1999-Jan-08</entry>
1607          <entry>January 8 in any mode</entry>
1608         </row>
1609         <row>
1610          <entry>Jan-08-1999</entry>
1611          <entry>January 8 in any mode</entry>
1612         </row>
1613         <row>
1614          <entry>08-Jan-1999</entry>
1615          <entry>January 8 in any mode</entry>
1616         </row>
1617         <row>
1618          <entry>99-Jan-08</entry>
1619          <entry>January 8 in <literal>YMD</> mode, else error</entry>
1620         </row>
1621         <row>
1622          <entry>08-Jan-99</entry>
1623          <entry>January 8, except error in <literal>YMD</> mode</entry>
1624         </row>
1625         <row>
1626          <entry>Jan-08-99</entry>
1627          <entry>January 8, except error in <literal>YMD</> mode</entry>
1628         </row>
1629         <row>
1630          <entry>19990108</entry>
1631          <entry>ISO 8601; January 8, 1999 in any mode</entry>
1632         </row>
1633         <row>
1634          <entry>990108</entry>
1635          <entry>ISO 8601; January 8, 1999 in any mode</entry>
1636         </row>
1637         <row>
1638          <entry>1999.008</entry>
1639          <entry>year and day of year</entry>
1640         </row>
1641         <row>
1642          <entry>J2451187</entry>
1643          <entry>Julian day</entry>
1644         </row>
1645         <row>
1646          <entry>January 8, 99 BC</entry>
1647          <entry>year 99 before the Common Era</entry>
1648         </row>
1649        </tbody>
1650       </tgroup>
1651      </table>
1652     </sect3>
1653
1654     <sect3>
1655      <title>Times</title>
1656
1657      <indexterm>
1658       <primary>time</primary>
1659      </indexterm>
1660      <indexterm>
1661       <primary>time without time zone</primary>
1662      </indexterm>
1663      <indexterm>
1664       <primary>time with time zone</primary>
1665      </indexterm>
1666
1667      <para>
1668       The time-of-day types are <type>time [
1669       (<replaceable>p</replaceable>) ] without time zone</type> and
1670       <type>time [ (<replaceable>p</replaceable>) ] with time
1671       zone</type>.  Writing just <type>time</type> is equivalent to
1672       <type>time without time zone</type>.
1673      </para>
1674
1675      <para>
1676       Valid input for these types consists of a time of day followed
1677       by an optional time zone. (See <xref
1678       linkend="datatype-datetime-time-table"> 
1679       and <xref linkend="datatype-timezone-table">.)  If a time zone is
1680       specified in the input for <type>time without time zone</type>,
1681       it is silently ignored. You can also specify a date but it will
1682       be ignored, except when you use a time zone name that involves a
1683       daylight-savings rule, such as
1684       <literal>America/New_York</literal>. In this case specifying the date
1685       is required in order to determine whether standard or daylight-savings
1686       time applies.  The appropriate time zone offset is recorded in the
1687       <type>time with time zone</type> value.
1688      </para>
1689
1690       <table id="datatype-datetime-time-table">
1691        <title>Time Input</title>
1692        <tgroup cols="2">
1693         <thead>
1694          <row>
1695           <entry>Example</entry>
1696           <entry>Description</entry>
1697          </row>
1698         </thead>
1699         <tbody>
1700          <row>
1701           <entry><literal>04:05:06.789</literal></entry>
1702           <entry>ISO 8601</entry>
1703          </row>
1704          <row>
1705           <entry><literal>04:05:06</literal></entry>
1706           <entry>ISO 8601</entry>
1707          </row>
1708          <row>
1709           <entry><literal>04:05</literal></entry>
1710           <entry>ISO 8601</entry>
1711          </row>
1712          <row>
1713           <entry><literal>040506</literal></entry>
1714           <entry>ISO 8601</entry>
1715          </row>
1716          <row>
1717           <entry><literal>04:05 AM</literal></entry>
1718           <entry>same as 04:05; AM does not affect value</entry>
1719          </row>
1720          <row>
1721           <entry><literal>04:05 PM</literal></entry>
1722           <entry>same as 16:05; input hour must be &lt;= 12</entry>
1723          </row>
1724          <row>
1725           <entry><literal>04:05:06.789-8</literal></entry>
1726           <entry>ISO 8601</entry>
1727          </row>
1728          <row>
1729           <entry><literal>04:05:06-08:00</literal></entry>
1730           <entry>ISO 8601</entry>
1731          </row>
1732          <row>
1733           <entry><literal>04:05-08:00</literal></entry>
1734           <entry>ISO 8601</entry>
1735          </row>
1736          <row>
1737           <entry><literal>040506-08</literal></entry>
1738           <entry>ISO 8601</entry>
1739          </row>
1740          <row>
1741           <entry><literal>04:05:06 PST</literal></entry>
1742           <entry>time zone specified by abbreviation</entry>
1743          </row>
1744          <row>
1745           <entry><literal>2003-04-12 04:05:06 America/New_York</literal></entry>
1746           <entry>time zone specified by full name</entry>
1747          </row>
1748         </tbody>
1749        </tgroup>
1750       </table>
1751
1752       <table tocentry="1" id="datatype-timezone-table">
1753        <title>Time Zone Input</title>
1754        <tgroup cols="2">
1755         <thead>
1756          <row>
1757           <entry>Example</entry>
1758           <entry>Description</entry>
1759          </row>
1760         </thead>
1761         <tbody>
1762          <row>
1763           <entry><literal>PST</literal></entry>
1764           <entry>Abbreviation (for Pacific Standard Time)</entry>
1765          </row>
1766          <row>
1767           <entry><literal>America/New_York</literal></entry>
1768           <entry>Full time zone name</entry>
1769          </row>
1770          <row>
1771           <entry><literal>PST8PDT</literal></entry>
1772           <entry>POSIX-style time zone specification</entry>
1773          </row>
1774          <row>
1775           <entry><literal>-8:00</literal></entry>
1776           <entry>ISO-8601 offset for PST</entry>
1777          </row>
1778          <row>
1779           <entry><literal>-800</literal></entry>
1780           <entry>ISO-8601 offset for PST</entry>
1781          </row>
1782          <row>
1783           <entry><literal>-8</literal></entry>
1784           <entry>ISO-8601 offset for PST</entry>
1785          </row>
1786          <row>
1787           <entry><literal>zulu</literal></entry>
1788           <entry>Military abbreviation for UTC</entry>
1789          </row>
1790          <row>
1791           <entry><literal>z</literal></entry>
1792           <entry>Short form of <literal>zulu</literal></entry>
1793          </row>
1794         </tbody>
1795        </tgroup>
1796       </table>
1797
1798      <para>
1799      Refer to <xref linkend="datatype-timezones"> for more information on how
1800      to specify time zones.
1801     </para>
1802     </sect3>
1803
1804     <sect3>
1805     <title>Time Stamps</title>
1806
1807     <indexterm>
1808      <primary>timestamp</primary>
1809     </indexterm>
1810
1811     <indexterm>
1812      <primary>timestamp with time zone</primary>
1813     </indexterm>
1814
1815     <indexterm>
1816      <primary>timestamp without time zone</primary>
1817     </indexterm>
1818
1819      <para>
1820       Valid input for the time stamp types consists of a concatenation
1821       of a date and a time, followed by an optional time zone,
1822       followed by an optional <literal>AD</literal> or <literal>BC</literal>.
1823       (Alternatively, <literal>AD</literal>/<literal>BC</literal> can appear
1824       before the time zone, but this is not the preferred ordering.)
1825       Thus:
1826
1827 <programlisting>
1828 1999-01-08 04:05:06
1829 </programlisting>
1830       and:
1831 <programlisting>
1832 1999-01-08 04:05:06 -8:00
1833 </programlisting>
1834
1835       are valid values, which follow the <acronym>ISO</acronym> 8601
1836       standard.  In addition, the wide-spread format:
1837 <programlisting>
1838 January 8 04:05:06 1999 PST
1839 </programlisting>
1840       is supported.
1841      </para>
1842
1843      <para>
1844       The <acronym>SQL</acronym> standard differentiates <type>timestamp without time zone</type> 
1845       and <type>timestamp with time zone</type> literals by the presence of a 
1846       <quote>+</quote> or <quote>-</quote>. Hence, according to the standard, 
1847       <programlisting>TIMESTAMP '2004-10-19 10:23:54'</programlisting>
1848       is a <type>timestamp without time zone</type>, while
1849       <programlisting>TIMESTAMP '2004-10-19 10:23:54+02'</programlisting>
1850       is a <type>timestamp with time zone</type>.
1851       <productname>PostgreSQL</productname> never examines the content of a
1852       literal string before determining its type, and therefore will treat
1853       both of the above as <type>timestamp without time zone</type>.  To
1854       ensure that a literal is treated as <type>timestamp with time
1855       zone</type>, give it the correct explicit type:
1856       <programlisting>TIMESTAMP WITH TIME ZONE '2004-10-19 10:23:54+02'</programlisting>
1857       In a literal that has been decided to be <type>timestamp without time
1858       zone</type>, <productname>PostgreSQL</productname> will silently ignore
1859       any time zone indication.
1860       That is, the resulting value is derived from the date/time
1861       fields in the input value, and is not adjusted for time zone.
1862      </para>
1863
1864      <para>
1865       For <type>timestamp with time zone</type>, the internally stored
1866       value is always in UTC (Universal
1867       Coordinated Time, traditionally known as Greenwich Mean Time,
1868       <acronym>GMT</>).  An input value that has an explicit
1869       time zone specified is converted to UTC using the appropriate offset
1870       for that time zone.  If no time zone is stated in the input string,
1871       then it is assumed to be in the time zone indicated by the system's
1872       <xref linkend="guc-timezone"> parameter, and is converted to UTC using the
1873       offset for the <varname>timezone</> zone.
1874      </para>
1875
1876      <para>
1877       When a <type>timestamp with time
1878       zone</type> value is output, it is always converted from UTC to the
1879       current <varname>timezone</> zone, and displayed as local time in that
1880       zone.  To see the time in another time zone, either change
1881       <varname>timezone</> or use the <literal>AT TIME ZONE</> construct
1882       (see <xref linkend="functions-datetime-zoneconvert">).
1883      </para>
1884
1885      <para>
1886       Conversions between <type>timestamp without time zone</type> and
1887       <type>timestamp with time zone</type> normally assume that the
1888       <type>timestamp without time zone</type> value should be taken or given
1889       as <varname>timezone</> local time.  A different zone reference can
1890       be specified for the conversion using <literal>AT TIME ZONE</>.
1891      </para>
1892     </sect3>
1893
1894     <sect3>
1895      <title>Intervals</title>
1896
1897      <indexterm>
1898       <primary>interval</primary>
1899      </indexterm>
1900
1901       <para>
1902        <type>interval</type> values can be written with the following syntax:
1903
1904 <programlisting>
1905 <optional>@</> <replaceable>quantity</> <replaceable>unit</> <optional><replaceable>quantity</> <replaceable>unit</>...</> <optional><replaceable>direction</></optional>
1906 </programlisting>
1907
1908       Where: <replaceable>quantity</> is a number (possibly signed);
1909       <replaceable>unit</> is <literal>microsecond</literal>,
1910       <literal>millisecond</literal>, <literal>second</literal>,
1911       <literal>minute</literal>, <literal>hour</literal>, <literal>day</literal>,
1912       <literal>week</literal>, <literal>month</literal>, <literal>year</literal>,
1913       <literal>decade</literal>, <literal>century</literal>, <literal>millennium</literal>,
1914       or abbreviations or plurals of these units;
1915       <replaceable>direction</> can be <literal>ago</literal> or
1916       empty.  The at sign (<literal>@</>) is optional noise.  The amounts
1917       of different units are implicitly added up with appropriate
1918       sign accounting.
1919      </para>
1920
1921      <para>
1922       Quantities of days, hours, minutes, and seconds can be specified without
1923       explicit unit markings.  For example, <literal>'1 12:59:10'</> is read
1924       the same as <literal>'1 day 12 hours 59 min 10 sec'</>.
1925      </para>
1926
1927      <para>
1928       The optional subsecond precision <replaceable>p</replaceable> should 
1929       be between 0 and 6, and defaults to the precision of the input literal.
1930      </para>
1931
1932      <para>
1933       Internally <type>interval</> values are stored as months, days,
1934       and seconds. This is done because the number of days in a month
1935       varies, and a day can have 23 or 25 hours if a daylight savings
1936       time adjustment is involved. Because intervals are usually created
1937       from constant strings or <type>timestamp</> subtraction, this
1938       storage method works well in most cases. Functions
1939       <function>justify_days</> and <function>justify_hours</> are
1940       available for adjusting days and hours that overflow their normal
1941       periods.
1942      </para>
1943     </sect3>
1944
1945     <sect3>
1946      <title>Special Values</title>
1947
1948      <indexterm>
1949       <primary>time</primary>
1950       <secondary>constants</secondary>
1951      </indexterm>
1952
1953      <indexterm>
1954       <primary>date</primary>
1955       <secondary>constants</secondary>
1956      </indexterm>
1957
1958      <para>
1959       <productname>PostgreSQL</productname> supports several
1960       special date/time input values for convenience, as shown in <xref
1961       linkend="datatype-datetime-special-table">.  The values
1962       <literal>infinity</literal> and <literal>-infinity</literal>
1963       are specially represented inside the system and will be displayed
1964       the same way; but the others are simply notational shorthands
1965       that will be converted to ordinary date/time values when read.
1966       (In particular, <literal>now</> and related strings are converted
1967       to a specific time value as soon as they are read.)
1968       All of these values need to be written in single quotes when used
1969       as constants in SQL commands.
1970      </para>
1971
1972       <table id="datatype-datetime-special-table">
1973        <title>Special Date/Time Inputs</title>
1974        <tgroup cols="3">
1975         <thead>
1976          <row>
1977           <entry>Input String</entry>
1978           <entry>Valid Types</entry>
1979           <entry>Description</entry>
1980          </row>
1981         </thead>
1982         <tbody>
1983          <row>
1984           <entry><literal>epoch</literal></entry>
1985           <entry><type>date</type>, <type>timestamp</type></entry>
1986           <entry>1970-01-01 00:00:00+00 (Unix system time zero)</entry>
1987          </row>
1988          <row>
1989           <entry><literal>infinity</literal></entry>
1990           <entry><type>timestamp</type></entry>
1991           <entry>later than all other time stamps</entry>
1992          </row>
1993          <row>
1994           <entry><literal>-infinity</literal></entry>
1995           <entry><type>timestamp</type></entry>
1996           <entry>earlier than all other time stamps</entry>
1997          </row>
1998          <row>
1999           <entry><literal>now</literal></entry>
2000           <entry><type>date</type>, <type>time</type>, <type>timestamp</type></entry>
2001           <entry>current transaction's start time</entry>
2002          </row>
2003          <row>
2004           <entry><literal>today</literal></entry>
2005           <entry><type>date</type>, <type>timestamp</type></entry>
2006           <entry>midnight today</entry>
2007          </row>
2008          <row>
2009           <entry><literal>tomorrow</literal></entry>
2010           <entry><type>date</type>, <type>timestamp</type></entry>
2011           <entry>midnight tomorrow</entry>
2012          </row>
2013          <row>
2014           <entry><literal>yesterday</literal></entry>
2015           <entry><type>date</type>, <type>timestamp</type></entry>
2016           <entry>midnight yesterday</entry>
2017          </row>
2018          <row>
2019           <entry><literal>allballs</literal></entry>
2020           <entry><type>time</type></entry>
2021           <entry>00:00:00.00 UTC</entry>
2022          </row>
2023         </tbody>
2024        </tgroup>
2025       </table>
2026
2027      <para>
2028       The following <acronym>SQL</acronym>-compatible functions can also
2029       be used to obtain the current time value for the corresponding data
2030       type:
2031       <literal>CURRENT_DATE</literal>, <literal>CURRENT_TIME</literal>, 
2032       <literal>CURRENT_TIMESTAMP</literal>, <literal>LOCALTIME</literal>, 
2033       <literal>LOCALTIMESTAMP</literal>.  The latter four accept an 
2034       optional subsecond precision specification.  (See <xref 
2035       linkend="functions-datetime-current">.)  Note however that these are
2036       SQL functions and are <emphasis>not</> recognized as data input strings.
2037      </para>
2038
2039     </sect3>
2040    </sect2>
2041
2042    <sect2 id="datatype-datetime-output">
2043     <title>Date/Time Output</title>
2044
2045     <indexterm>
2046      <primary>date</primary>
2047      <secondary>output format</secondary>
2048      <seealso>formatting</seealso>
2049     </indexterm>
2050
2051     <indexterm>
2052      <primary>time</primary>
2053      <secondary>output format</secondary>
2054      <seealso>formatting</seealso>
2055     </indexterm>
2056
2057     <para>
2058      The output format of the date/time types can be set to one of the four
2059      styles ISO 8601,
2060      <acronym>SQL</acronym> (Ingres), traditional POSTGRES, and
2061      German, using the command <literal>SET datestyle</literal>.  The default
2062      is the <acronym>ISO</acronym> format.  (The
2063      <acronym>SQL</acronym> standard requires the use of the ISO 8601
2064      format.  The name of the <quote>SQL</quote> output format is a
2065      historical accident.)  <xref
2066      linkend="datatype-datetime-output-table"> shows examples of each
2067      output style.  The output of the <type>date</type> and
2068      <type>time</type> types is of course only the date or time part
2069      in accordance with the given examples.
2070     </para>
2071
2072      <table id="datatype-datetime-output-table">
2073       <title>Date/Time Output Styles</title>
2074       <tgroup cols="3">
2075        <thead>
2076         <row>
2077          <entry>Style Specification</entry>
2078          <entry>Description</entry>
2079          <entry>Example</entry>
2080         </row>
2081        </thead>
2082        <tbody>
2083         <row>
2084          <entry>ISO</entry>
2085          <entry>ISO 8601/SQL standard</entry>
2086          <entry>1997-12-17 07:37:16-08</entry>
2087         </row>
2088         <row>
2089          <entry>SQL</entry>
2090          <entry>traditional style</entry>
2091          <entry>12/17/1997 07:37:16.00 PST</entry>
2092         </row>
2093         <row>
2094          <entry>POSTGRES</entry>
2095          <entry>original style</entry>
2096          <entry>Wed Dec 17 07:37:16 1997 PST</entry>
2097         </row>
2098         <row>
2099          <entry>German</entry>
2100          <entry>regional style</entry>
2101          <entry>17.12.1997 07:37:16.00 PST</entry>
2102         </row>
2103        </tbody>
2104       </tgroup>
2105      </table>
2106
2107     <para>
2108      In the <acronym>SQL</acronym> and POSTGRES styles, day appears before
2109      month if DMY field ordering has been specified, otherwise month appears
2110      before day.
2111      (See <xref linkend="datatype-datetime-input">
2112      for how this setting also affects interpretation of input values.)
2113      <xref linkend="datatype-datetime-output2-table"> shows an
2114      example.
2115     </para>
2116
2117      <table id="datatype-datetime-output2-table">
2118       <title>Date Order Conventions</title>
2119       <tgroup cols="3">
2120        <thead>
2121         <row>
2122          <entry><varname>datestyle</varname> Setting</entry>
2123          <entry>Input Ordering</entry>
2124          <entry>Example Output</entry>
2125         </row>
2126        </thead>
2127        <tbody>
2128         <row>
2129          <entry><literal>SQL, DMY</></entry>
2130          <entry><replaceable>day</replaceable>/<replaceable>month</replaceable>/<replaceable>year</replaceable></entry>
2131          <entry>17/12/1997 15:37:16.00 CET</entry>
2132         </row>
2133         <row>
2134          <entry><literal>SQL, MDY</></entry>
2135          <entry><replaceable>month</replaceable>/<replaceable>day</replaceable>/<replaceable>year</replaceable></entry>
2136          <entry>12/17/1997 07:37:16.00 PST</entry>
2137         </row>
2138         <row>
2139          <entry><literal>Postgres, DMY</></entry>
2140          <entry><replaceable>day</replaceable>/<replaceable>month</replaceable>/<replaceable>year</replaceable></entry>
2141          <entry>Wed 17 Dec 07:37:16 1997 PST</entry>
2142         </row>
2143        </tbody>
2144       </tgroup>
2145      </table>
2146
2147     <para>
2148      <type>interval</type> output looks like the input format, except
2149      that units like <literal>century</literal> or
2150      <literal>week</literal> are converted to years and days and
2151      <literal>ago</literal> is converted to an appropriate sign.  In
2152      ISO mode the output looks like:
2153
2154 <programlisting>
2155 <optional> <replaceable>quantity</> <replaceable>unit</> <optional> ... </> </> <optional> <replaceable>days</> </> <optional> <replaceable>hours</>:<replaceable>minutes</>:<replaceable>seconds</> </optional>
2156 </programlisting>
2157     </para>
2158
2159     <para>
2160      The date/time styles can be selected by the user using the
2161      <command>SET datestyle</command> command, the <xref
2162      linkend="guc-datestyle"> parameter in the
2163      <filename>postgresql.conf</filename> configuration file, or the
2164      <envar>PGDATESTYLE</envar> environment variable on the server or
2165      client.  The formatting function <function>to_char</function>
2166      (see <xref linkend="functions-formatting">) is also available as
2167      a more flexible way to format the date/time output.
2168     </para>
2169    </sect2>
2170
2171    <sect2 id="datatype-timezones">
2172     <title>Time Zones</title>
2173
2174     <indexterm zone="datatype-timezones">
2175      <primary>time zone</primary>
2176     </indexterm>
2177
2178    <para>
2179     Time zones, and time-zone conventions, are influenced by
2180     political decisions, not just earth geometry. Time zones around the
2181     world became somewhat standardized during the 1900's,
2182     but continue to be prone to arbitrary changes, particularly with
2183     respect to daylight-savings rules.
2184     <productname>PostgreSQL</productname> currently supports daylight-savings
2185     rules over the time period 1902 through 2038 (corresponding to the full
2186     range of conventional Unix system time).  Times outside that range are
2187     taken to be in <quote>standard time</> for the selected time zone, no
2188     matter what part of the year they fall in.
2189    </para>
2190
2191     <para>
2192      <productname>PostgreSQL</productname> endeavors to be compatible with
2193      the <acronym>SQL</acronym> standard definitions for typical usage.
2194      However, the <acronym>SQL</acronym> standard has an odd mix of date and
2195      time types and capabilities. Two obvious problems are:
2196
2197      <itemizedlist>
2198       <listitem>
2199        <para>
2200         Although the <type>date</type> type 
2201         does not have an associated time zone, the
2202         <type>time</type> type can.
2203         Time zones in the real world have little meaning unless 
2204         associated with a date as well as a time,
2205         since the offset can vary through the year with daylight-saving
2206         time boundaries.
2207        </para>
2208       </listitem>
2209
2210       <listitem>
2211        <para>
2212         The default time zone is specified as a constant numeric offset 
2213         from <acronym>UTC</>. It is therefore not possible to adapt to
2214         daylight-saving time when doing date/time arithmetic across
2215         <acronym>DST</acronym> boundaries.
2216        </para>
2217       </listitem>
2218
2219      </itemizedlist>
2220     </para>
2221
2222     <para>
2223      To address these difficulties, we recommend using date/time types
2224      that contain both date and time when using time zones. We
2225      recommend <emphasis>not</emphasis> using the type <type>time with
2226      time zone</type> (though it is supported by
2227      <productname>PostgreSQL</productname> for legacy applications and
2228      for compliance with the <acronym>SQL</acronym> standard).
2229      <productname>PostgreSQL</productname> assumes
2230      your local time zone for any type containing only date or time.
2231     </para>
2232
2233     <para>
2234      All timezone-aware dates and times are stored internally in
2235      <acronym>UTC</acronym>.  They are converted to local time
2236      in the zone specified by the <xref linkend="guc-timezone"> configuration
2237      parameter before being displayed to the client.
2238     </para>
2239
2240     <para>
2241      <productname>PostgreSQL</productname> allows you to specify time zones in
2242      three different forms:
2243      <itemizedlist>
2244       <listitem>
2245        <para>
2246         A full time zone name, for example <literal>America/New_York</>.
2247         The recognized time zone names are listed in the
2248         <literal>pg_timezone_names</literal> view (see <xref
2249         linkend="view-pg-timezone-names">).
2250         <productname>PostgreSQL</productname> uses the widely-used
2251         <literal>zic</> time zone data for this purpose, so the same
2252         names are also recognized by much other software.
2253        </para>
2254       </listitem>
2255       <listitem>
2256        <para>
2257         A time zone abbreviation, for example <literal>PST</>.  Such a
2258         specification merely defines a particular offset from UTC, in
2259         contrast to full time zone names which might imply a set of daylight
2260         savings transition-date rules as well.  The recognized abbreviations
2261         are listed in the <literal>pg_timezone_abbrevs</> view (see <xref
2262         linkend="view-pg-timezone-abbrevs">).  You cannot set the
2263         configuration parameters <xref linkend="guc-timezone"> or
2264         <xref linkend="guc-log-timezone"> using a time
2265         zone abbreviation, but you can use abbreviations in
2266         date/time input values and with the <literal>AT TIME ZONE</>
2267         operator.
2268        </para>
2269       </listitem>
2270       <listitem>
2271        <para>
2272         In addition to the timezone names and abbreviations,
2273         <productname>PostgreSQL</productname> will accept POSIX-style time zone
2274         specifications of the form <replaceable>STD</><replaceable>offset</> or
2275         <replaceable>STD</><replaceable>offset</><replaceable>DST</>, where
2276         <replaceable>STD</> is a zone abbreviation, <replaceable>offset</> is a
2277         numeric offset in hours west from UTC, and <replaceable>DST</> is an
2278         optional daylight-savings zone abbreviation, assumed to stand for one
2279         hour ahead of the given offset. For example, if <literal>EST5EDT</>
2280         were not already a recognized zone name, it would be accepted and would
2281         be functionally equivalent to USA East Coast time.  When a
2282         daylight-savings zone name is present, it is assumed to be used
2283         according to the same daylight-savings transition rules used in the
2284         <literal>zic</> time zone database's <filename>posixrules</> entry.
2285         In a standard <productname>PostgreSQL</productname> installation,
2286         <filename>posixrules</> is the same as <literal>US/Eastern</>, so
2287         that POSIX-style time zone specifications follow USA daylight-savings
2288         rules.  If needed, you can adjust this behavior by replacing the
2289         <filename>posixrules</> file.
2290        </para>
2291       </listitem>
2292      </itemizedlist>
2293
2294      There is a conceptual and practical difference between the abbreviations
2295      and the full names: abbreviations always represent a fixed offset from
2296      UTC, whereas most of the full names imply a local daylight-savings time
2297      rule and so have two possible UTC offsets.
2298     </para>
2299
2300     <para>
2301      One should be wary that the POSIX-style time zone feature can
2302      lead to silently accepting bogus input, since there is no check on the
2303      reasonableness of the zone abbreviations.  For example, <literal>SET
2304      TIMEZONE TO FOOBAR0</> will work, leaving the system effectively using
2305      a rather peculiar abbreviation for UTC.
2306      Another issue to keep in mind is that in POSIX time zone names,
2307      positive offsets are used for locations <emphasis>west</> of Greenwich.
2308      Everywhere else, <productname>PostgreSQL</productname> follows the
2309      ISO-8601 convention that positive timezone offsets are <emphasis>east</>
2310      of Greenwich.
2311     </para>
2312
2313     <para>
2314      In all cases, timezone names are recognized case-insensitively.
2315      (This is a change from <productname>PostgreSQL</productname> versions
2316      prior to 8.2, which were case-sensitive in some contexts and not others.)
2317     </para>
2318
2319     <para>
2320      Neither full names nor abbreviations are hard-wired into the server;
2321      they are obtained from configuration files stored under
2322      <filename>.../share/timezone/</> and <filename>.../share/timezonesets/</>
2323      of the installation directory
2324      (see <xref linkend="datetime-config-files">).
2325     </para>
2326
2327     <para>
2328      The <xref linkend="guc-timezone"> configuration parameter can
2329      be set in the file <filename>postgresql.conf</>, or in any of the
2330      other standard ways described in <xref linkend="runtime-config">.
2331      There are also several special ways to set it:
2332
2333      <itemizedlist>
2334       <listitem>
2335        <para>
2336         If <varname>timezone</> is not specified in
2337         <filename>postgresql.conf</> nor as a server command-line option,
2338         the server attempts to use the value of the <envar>TZ</envar>
2339         environment variable as the default time zone.  If <envar>TZ</envar>
2340         is not defined or is not any of the time zone names known to
2341         <productname>PostgreSQL</productname>, the server attempts to
2342         determine the operating system's default time zone by checking the
2343         behavior of the C library function <literal>localtime()</>.  The
2344         default time zone is selected as the closest match among
2345         <productname>PostgreSQL</productname>'s known time zones.
2346         (These rules are also used to choose the default value of
2347         <xref linkend="guc-log-timezone">, if it is not specified.)
2348        </para>
2349       </listitem>
2350
2351       <listitem>
2352        <para>
2353         The <acronym>SQL</acronym> command <command>SET TIME ZONE</command>
2354         sets the time zone for the session.  This is an alternative spelling
2355         of <command>SET TIMEZONE TO</> with a more SQL-spec-compatible syntax.
2356        </para>
2357       </listitem>
2358
2359       <listitem>
2360        <para>
2361         The <envar>PGTZ</envar> environment variable, if set at the
2362         client, is used by <application>libpq</application>
2363         applications to send a <command>SET TIME ZONE</command>
2364         command to the server upon connection.
2365        </para>
2366       </listitem>
2367      </itemizedlist>
2368     </para>
2369    </sect2>
2370
2371    <sect2 id="datatype-datetime-internals">
2372     <title>Internals</title>
2373
2374     <para>
2375      <productname>PostgreSQL</productname> uses Julian dates
2376      for all date/time calculations. They have the nice property of correctly
2377      predicting/calculating any date more recent than 4713 BC
2378      to far into the future, using the assumption that the length of the
2379      year is 365.2425 days.
2380     </para>
2381
2382     <para>
2383      Date conventions before the 19th century make for interesting reading,
2384      but are not consistent enough to warrant coding into a date/time handler.
2385     </para>
2386    </sect2>
2387
2388   </sect1>
2389
2390   <sect1 id="datatype-boolean">
2391    <title>Boolean Type</title>
2392
2393    <indexterm zone="datatype-boolean">
2394     <primary>Boolean</primary>
2395     <secondary>data type</secondary>
2396    </indexterm>
2397
2398    <indexterm zone="datatype-boolean">
2399     <primary>true</primary>
2400    </indexterm>
2401
2402    <indexterm zone="datatype-boolean">
2403     <primary>false</primary>
2404    </indexterm>
2405
2406    <para>
2407     <productname>PostgreSQL</productname> provides the
2408     standard <acronym>SQL</acronym> type <type>boolean</type>.
2409     <type>boolean</type> can have one of only two states:
2410     <quote>true</quote> or <quote>false</quote>.  A third state,
2411     <quote>unknown</quote>, is represented by the
2412     <acronym>SQL</acronym> null value.
2413    </para>
2414
2415    <para>
2416     Valid literal values for the <quote>true</quote> state are:
2417     <simplelist>
2418      <member><literal>TRUE</literal></member>
2419      <member><literal>'t'</literal></member>
2420      <member><literal>'true'</literal></member>
2421      <member><literal>'y'</literal></member>
2422      <member><literal>'yes'</literal></member>
2423      <member><literal>'1'</literal></member>
2424     </simplelist>
2425     For the <quote>false</quote> state, the following values can be
2426     used:
2427     <simplelist>
2428      <member><literal>FALSE</literal></member>
2429      <member><literal>'f'</literal></member>
2430      <member><literal>'false'</literal></member>
2431      <member><literal>'n'</literal></member>
2432      <member><literal>'no'</literal></member>
2433      <member><literal>'0'</literal></member>
2434     </simplelist>
2435     Leading and trailing whitespace is ignored. Using the key words
2436     <literal>TRUE</literal> and <literal>FALSE</literal> is preferred
2437     (and <acronym>SQL</acronym>-compliant).
2438    </para>
2439
2440    <example id="datatype-boolean-example">
2441     <title>Using the <type>boolean</type> type</title>
2442
2443 <programlisting>
2444 CREATE TABLE test1 (a boolean, b text);
2445 INSERT INTO test1 VALUES (TRUE, 'sic est');
2446 INSERT INTO test1 VALUES (FALSE, 'non est');
2447 SELECT * FROM test1;
2448  a |    b
2449 ---+---------
2450  t | sic est
2451  f | non est
2452
2453 SELECT * FROM test1 WHERE a;
2454  a |    b
2455 ---+---------
2456  t | sic est
2457 </programlisting>
2458    </example>
2459
2460    <para>
2461     <xref linkend="datatype-boolean-example"> shows that
2462     <type>boolean</type> values are output using the letters
2463     <literal>t</literal> and <literal>f</literal>.
2464    </para>
2465
2466    <para>
2467     <type>boolean</type> uses 1 byte of storage.
2468    </para>
2469   </sect1>
2470
2471   <sect1 id="datatype-enum">
2472    <title>Enumerated Types</title>
2473
2474    <indexterm zone="datatype-enum">
2475     <primary>data type</primary>
2476     <secondary>enumerated (enum)</secondary>
2477    </indexterm>
2478
2479    <para>
2480     Enumerated (enum) types are data types that
2481     are comprised of a static, predefined set of values with a
2482     specific order. They are equivalent to the <type>enum</type>
2483     types in a number of programming languages. An example of an enum
2484     type might be the days of the week, or a set of status values for
2485     a piece of data.
2486    </para>
2487
2488    <sect2>
2489     <title>Declaration of Enumerated Types</title>
2490
2491     <para>
2492      Enum types are created using the <xref
2493      linkend="sql-createtype" endterm="sql-createtype-title"> command,
2494      for example:
2495
2496 <programlisting>
2497 CREATE TYPE mood AS ENUM ('sad', 'ok', 'happy');
2498 </programlisting>
2499
2500      Once created, the enum type can be used in table and function
2501      definitions much like any other type:
2502     </para>
2503
2504     <example>
2505      <title>Basic Enum Usage</title>
2506 <programlisting>
2507 CREATE TYPE mood AS ENUM ('sad', 'ok', 'happy');
2508 CREATE TABLE person (
2509     name text,
2510     current_mood mood
2511 );
2512 INSERT INTO person VALUES ('Moe', 'happy');
2513 SELECT * FROM person WHERE current_mood = 'happy';
2514  name | current_mood 
2515 ------+--------------
2516  Moe  | happy
2517 (1 row)
2518 </programlisting>
2519      </example>
2520     </sect2>
2521
2522     <sect2>
2523      <title>Ordering</title>
2524
2525      <para>
2526       The ordering of the values in an enum type is the
2527       order in which the values were listed when the type was declared.
2528       All standard comparison operators and related
2529       aggregate functions are supported for enums.  For example:
2530      </para>
2531
2532      <example>
2533       <title>Enum Ordering</title>
2534 <programlisting>
2535 INSERT INTO person VALUES ('Larry', 'sad');
2536 INSERT INTO person VALUES ('Curly', 'ok');
2537 SELECT * FROM person WHERE current_mood > 'sad';
2538  name  | current_mood 
2539 -------+--------------
2540  Moe   | happy
2541  Curly | ok
2542 (2 rows)
2543
2544 SELECT * FROM person WHERE current_mood > 'sad' ORDER BY current_mood;
2545  name  | current_mood 
2546 -------+--------------
2547  Curly | ok
2548  Moe   | happy
2549 (2 rows)
2550
2551 SELECT name FROM person
2552   WHERE current_mood = (SELECT MIN(current_mood) FROM person);
2553  name  
2554 -------
2555  Larry
2556 (1 row)
2557 </programlisting>
2558     </example>
2559    </sect2>
2560
2561    <sect2>
2562     <title>Type Safety</title>
2563
2564     <para>
2565      Enumerated types are completely separate data types and may not
2566      be compared with each other.
2567     </para>
2568
2569     <example>
2570      <title>Lack of Casting</title>
2571 <programlisting>
2572 CREATE TYPE happiness AS ENUM ('happy', 'very happy', 'ecstatic');
2573 CREATE TABLE holidays (                                           
2574     num_weeks int,
2575     happiness happiness
2576 );
2577 INSERT INTO holidays(num_weeks,happiness) VALUES (4, 'happy');
2578 INSERT INTO holidays(num_weeks,happiness) VALUES (6, 'very happy');
2579 INSERT INTO holidays(num_weeks,happiness) VALUES (8, 'ecstatic');
2580 INSERT INTO holidays(num_weeks,happiness) VALUES (2, 'sad');
2581 ERROR:  invalid input value for enum happiness: "sad"
2582 SELECT person.name, holidays.num_weeks FROM person, holidays
2583   WHERE person.current_mood = holidays.happiness;
2584 ERROR:  operator does not exist: mood = happiness
2585 </programlisting>
2586     </example>
2587
2588     <para>
2589      If you really need to do something like that, you can either
2590      write a custom operator or add explicit casts to your query:
2591     </para>
2592
2593     <example>
2594      <title>Comparing Different Enums by Casting to Text</title>
2595 <programlisting>
2596 SELECT person.name, holidays.num_weeks FROM person, holidays
2597   WHERE person.current_mood::text = holidays.happiness::text;
2598  name | num_weeks 
2599 ------+-----------
2600  Moe  |         4
2601 (1 row)
2602
2603 </programlisting>
2604     </example>
2605    </sect2>
2606
2607    <sect2>
2608     <title>Implementation Details</title>
2609     
2610     <para>
2611      An enum value occupies four bytes on disk.  The length of an enum
2612      value's textual label is limited by the <symbol>NAMEDATALEN</symbol>
2613      setting compiled into <productname>PostgreSQL</productname>; in standard
2614      builds this means at most 63 bytes.
2615     </para>
2616
2617     <para>
2618      Enum labels are case sensitive, so
2619      <type>'happy'</type> is not the same as <type>'HAPPY'</type>.
2620      Spaces in the labels are significant, too.
2621     </para>
2622
2623    </sect2>
2624   </sect1>
2625
2626   <sect1 id="datatype-geometric">
2627    <title>Geometric Types</title>
2628
2629    <para>
2630     Geometric data types represent two-dimensional spatial
2631     objects. <xref linkend="datatype-geo-table"> shows the geometric
2632     types available in <productname>PostgreSQL</productname>.  The
2633     most fundamental type, the point, forms the basis for all of the
2634     other types.
2635    </para>
2636
2637     <table id="datatype-geo-table">
2638      <title>Geometric Types</title>
2639      <tgroup cols="4">
2640       <thead>
2641        <row>
2642         <entry>Name</entry>
2643         <entry>Storage Size</entry>
2644         <entry>Representation</entry>
2645         <entry>Description</entry>
2646        </row>
2647       </thead>
2648       <tbody>
2649        <row>
2650         <entry><type>point</type></entry>
2651         <entry>16 bytes</entry>
2652         <entry>Point on the plane</entry>
2653         <entry>(x,y)</entry>
2654        </row>
2655        <row>
2656         <entry><type>line</type></entry>
2657         <entry>32 bytes</entry>
2658         <entry>Infinite line (not fully implemented)</entry>
2659         <entry>((x1,y1),(x2,y2))</entry>
2660        </row>
2661        <row>
2662         <entry><type>lseg</type></entry>
2663         <entry>32 bytes</entry>
2664         <entry>Finite line segment</entry>
2665         <entry>((x1,y1),(x2,y2))</entry>
2666        </row>
2667        <row>
2668         <entry><type>box</type></entry>
2669         <entry>32 bytes</entry>
2670         <entry>Rectangular box</entry>
2671         <entry>((x1,y1),(x2,y2))</entry>
2672        </row>
2673        <row>
2674         <entry><type>path</type></entry>
2675         <entry>16+16n bytes</entry>
2676         <entry>Closed path (similar to polygon)</entry>
2677         <entry>((x1,y1),...)</entry>
2678        </row>
2679        <row>
2680         <entry><type>path</type></entry>
2681         <entry>16+16n bytes</entry>
2682         <entry>Open path</entry>
2683         <entry>[(x1,y1),...]</entry>
2684        </row>
2685        <row>
2686         <entry><type>polygon</type></entry>
2687         <entry>40+16n bytes</entry>
2688         <entry>Polygon (similar to closed path)</entry>
2689         <entry>((x1,y1),...)</entry>
2690        </row>
2691        <row>
2692         <entry><type>circle</type></entry>
2693         <entry>24 bytes</entry>
2694         <entry>Circle</entry>
2695         <entry>&lt;(x,y),r&gt; (center and radius)</entry>
2696        </row>
2697       </tbody>
2698      </tgroup>
2699     </table>
2700
2701    <para>
2702     A rich set of functions and operators is available to perform various geometric
2703     operations such as scaling, translation, rotation, and determining 
2704     intersections.  They are explained in <xref linkend="functions-geometry">.
2705    </para>
2706
2707    <sect2>
2708     <title>Points</title>
2709
2710     <indexterm>
2711      <primary>point</primary>
2712     </indexterm>
2713
2714     <para>
2715      Points are the fundamental two-dimensional building block for geometric types.
2716      Values of type <type>point</type> are specified using the following syntax:
2717
2718 <synopsis>
2719 ( <replaceable>x</replaceable> , <replaceable>y</replaceable> )
2720   <replaceable>x</replaceable> , <replaceable>y</replaceable>
2721 </synopsis>
2722
2723      where <replaceable>x</> and <replaceable>y</> are the respective
2724      coordinates as floating-point numbers.
2725     </para>
2726    </sect2>
2727
2728    <sect2>
2729     <title>Line Segments</title>
2730
2731     <indexterm>
2732      <primary>lseg</primary>
2733     </indexterm>
2734
2735     <indexterm>
2736      <primary>line segment</primary>
2737     </indexterm>
2738
2739     <para>
2740      Line segments (<type>lseg</type>) are represented by pairs of points.
2741      Values of type <type>lseg</type> are specified using the following syntax:
2742
2743 <synopsis>
2744 ( ( <replaceable>x1</replaceable> , <replaceable>y1</replaceable> ) , ( <replaceable>x2</replaceable> , <replaceable>y2</replaceable> ) )
2745   ( <replaceable>x1</replaceable> , <replaceable>y1</replaceable> ) , ( <replaceable>x2</replaceable> , <replaceable>y2</replaceable> )  
2746     <replaceable>x1</replaceable> , <replaceable>y1</replaceable>   ,   <replaceable>x2</replaceable> , <replaceable>y2</replaceable>
2747 </synopsis>
2748
2749      where
2750      <literal>(<replaceable>x1</replaceable>,<replaceable>y1</replaceable>)</literal>
2751      and
2752      <literal>(<replaceable>x2</replaceable>,<replaceable>y2</replaceable>)</literal>
2753      are the end points of the line segment.
2754     </para>
2755    </sect2>
2756
2757    <sect2>
2758     <title>Boxes</title>
2759
2760     <indexterm>
2761      <primary>box (data type)</primary>
2762     </indexterm>
2763
2764     <indexterm>
2765      <primary>rectangle</primary>
2766     </indexterm>
2767
2768     <para>
2769      Boxes are represented by pairs of points that are opposite
2770      corners of the box.
2771      Values of type <type>box</type> are specified using the following syntax:
2772
2773 <synopsis>
2774 ( ( <replaceable>x1</replaceable> , <replaceable>y1</replaceable> ) , ( <replaceable>x2</replaceable> , <replaceable>y2</replaceable> ) )
2775   ( <replaceable>x1</replaceable> , <replaceable>y1</replaceable> ) , ( <replaceable>x2</replaceable> , <replaceable>y2</replaceable> )  
2776     <replaceable>x1</replaceable> , <replaceable>y1</replaceable>   ,   <replaceable>x2</replaceable> , <replaceable>y2</replaceable>
2777 </synopsis>
2778
2779      where
2780      <literal>(<replaceable>x1</replaceable>,<replaceable>y1</replaceable>)</literal>
2781      and
2782      <literal>(<replaceable>x2</replaceable>,<replaceable>y2</replaceable>)</literal>
2783      are any two opposite corners of the box.
2784     </para>
2785
2786     <para>
2787      Boxes are output using the first syntax.
2788      The corners are reordered on input to store
2789      the upper right corner, then the lower left corner.
2790      Other corners of the box can be entered, but the lower
2791      left and upper right corners are determined from the input and stored.
2792     </para>
2793    </sect2>
2794
2795    <sect2>
2796     <title>Paths</title>
2797
2798     <indexterm>
2799      <primary>path (data type)</primary>
2800     </indexterm>
2801
2802     <para>
2803      Paths are represented by lists of connected points. Paths can be
2804      <firstterm>open</firstterm>, where
2805      the first and last points in the list are not considered connected, or
2806      <firstterm>closed</firstterm>,
2807      where the first and last points are considered connected.
2808     </para>
2809
2810     <para>
2811      Values of type <type>path</type> are specified using the following syntax:
2812
2813 <synopsis>
2814 ( ( <replaceable>x1</replaceable> , <replaceable>y1</replaceable> ) , ... , ( <replaceable>xn</replaceable> , <replaceable>yn</replaceable> ) )
2815 [ ( <replaceable>x1</replaceable> , <replaceable>y1</replaceable> ) , ... , ( <replaceable>xn</replaceable> , <replaceable>yn</replaceable> ) ]
2816   ( <replaceable>x1</replaceable> , <replaceable>y1</replaceable> ) , ... , ( <replaceable>xn</replaceable> , <replaceable>yn</replaceable> )  
2817   ( <replaceable>x1</replaceable> , <replaceable>y1</replaceable>   , ... ,   <replaceable>xn</replaceable> , <replaceable>yn</replaceable> )  
2818     <replaceable>x1</replaceable> , <replaceable>y1</replaceable>   , ... ,   <replaceable>xn</replaceable> , <replaceable>yn</replaceable>    
2819 </synopsis>
2820
2821      where the points are the end points of the line segments
2822      comprising the path.  Square brackets (<literal>[]</>) indicate
2823      an open path, while parentheses (<literal>()</>) indicate a
2824      closed path.
2825     </para>
2826
2827     <para>
2828      Paths are output using the first syntax.
2829     </para>
2830    </sect2>
2831
2832    <sect2>
2833     <title>Polygons</title>
2834
2835     <indexterm>
2836      <primary>polygon</primary>
2837     </indexterm>
2838
2839     <para>
2840      Polygons are represented by lists of points (the vertexes of the
2841      polygon). Polygons should probably be
2842      considered equivalent to closed paths, but are stored differently 
2843      and have their own set of support routines.
2844     </para>
2845
2846     <para>
2847      Values of type <type>polygon</type> are specified using the following syntax:
2848
2849 <synopsis>
2850 ( ( <replaceable>x1</replaceable> , <replaceable>y1</replaceable> ) , ... , ( <replaceable>xn</replaceable> , <replaceable>yn</replaceable> ) )
2851   ( <replaceable>x1</replaceable> , <replaceable>y1</replaceable> ) , ... , ( <replaceable>xn</replaceable> , <replaceable>yn</replaceable> )  
2852   ( <replaceable>x1</replaceable> , <replaceable>y1</replaceable>   , ... ,   <replaceable>xn</replaceable> , <replaceable>yn</replaceable> )  
2853     <replaceable>x1</replaceable> , <replaceable>y1</replaceable>   , ... ,   <replaceable>xn</replaceable> , <replaceable>yn</replaceable>    
2854 </synopsis>
2855
2856      where the points are the end points of the line segments
2857      comprising the boundary of the polygon.
2858     </para>
2859
2860     <para>
2861      Polygons are output using the first syntax.
2862     </para>
2863    </sect2>
2864
2865    <sect2>
2866     <title>Circles</title>
2867
2868     <indexterm>
2869      <primary>circle</primary>
2870     </indexterm>
2871
2872     <para>
2873      Circles are represented by a center point and a radius.
2874      Values of type <type>circle</type> are specified using the following syntax:
2875
2876 <synopsis>
2877 &lt; ( <replaceable>x</replaceable> , <replaceable>y</replaceable> ) , <replaceable>r</replaceable> &gt;
2878 ( ( <replaceable>x</replaceable> , <replaceable>y</replaceable> ) , <replaceable>r</replaceable> )
2879   ( <replaceable>x</replaceable> , <replaceable>y</replaceable> ) , <replaceable>r</replaceable>  
2880     <replaceable>x</replaceable> , <replaceable>y</replaceable>   , <replaceable>r</replaceable>  
2881 </synopsis>
2882
2883      where
2884      <literal>(<replaceable>x</replaceable>,<replaceable>y</replaceable>)</literal>
2885      is the center and <replaceable>r</replaceable> is the radius of the circle.
2886     </para>
2887
2888     <para>
2889      Circles are output using the first syntax.
2890     </para>
2891    </sect2>
2892
2893   </sect1>
2894
2895   <sect1 id="datatype-net-types">
2896    <title>Network Address Types</title>
2897
2898    <indexterm zone="datatype-net-types">
2899     <primary>network</primary>
2900     <secondary>data types</secondary>
2901    </indexterm>
2902
2903    <para>
2904     <productname>PostgreSQL</> offers data types to store IPv4, IPv6, and MAC
2905     addresses, as shown in <xref linkend="datatype-net-types-table">.  It
2906     is preferable to use these types instead of plain text types to store
2907     network addresses, because
2908     these types offer input error checking and several specialized
2909     operators and functions (see <xref linkend="functions-net">).
2910    </para>
2911
2912     <table tocentry="1" id="datatype-net-types-table">
2913      <title>Network Address Types</title>
2914      <tgroup cols="3">
2915       <thead>
2916        <row>
2917         <entry>Name</entry>
2918         <entry>Storage Size</entry>
2919         <entry>Description</entry>
2920        </row>
2921       </thead>
2922       <tbody>
2923
2924        <row>
2925         <entry><type>cidr</type></entry>
2926         <entry>7 or 19 bytes</entry>
2927         <entry>IPv4 and IPv6 networks</entry>
2928        </row>
2929
2930        <row>
2931         <entry><type>inet</type></entry>
2932         <entry>7 or 19 bytes</entry>
2933         <entry>IPv4 and IPv6 hosts and networks</entry>
2934        </row>
2935
2936        <row>
2937         <entry><type>macaddr</type></entry>
2938         <entry>6 bytes</entry>
2939         <entry>MAC addresses</entry>
2940        </row>
2941
2942       </tbody>
2943      </tgroup>
2944     </table>
2945
2946    <para>
2947     When sorting <type>inet</type> or <type>cidr</type> data types,
2948     IPv4 addresses will always sort before IPv6 addresses, including
2949     IPv4 addresses encapsulated or mapped into IPv6 addresses, such as
2950     ::10.2.3.4 or ::ffff::10.4.3.2.
2951    </para>
2952
2953
2954    <sect2 id="datatype-inet">
2955     <title><type>inet</type></title>
2956
2957     <indexterm>
2958      <primary>inet (data type)</primary>
2959     </indexterm>
2960
2961     <para>
2962      The <type>inet</type> type holds an IPv4 or IPv6 host address, and
2963      optionally the identity of the subnet it is in, all in one field.
2964      The subnet identity is represented by stating how many bits of
2965      the host address represent the network address (the
2966      <quote>netmask</quote>).  If the netmask is 32 and the address is IPv4,
2967      then the value does not indicate a subnet, only a single host.
2968      In IPv6, the address length is 128 bits, so 128 bits specify a
2969      unique host address.  Note that if you
2970      want to accept networks only, you should use the
2971      <type>cidr</type> type rather than <type>inet</type>.
2972     </para>
2973
2974     <para>
2975       The input format for this type is
2976       <replaceable class="parameter">address/y</replaceable>
2977       where
2978       <replaceable class="parameter">address</replaceable>
2979       is an IPv4 or IPv6 address and
2980       <replaceable class="parameter">y</replaceable>
2981       is the number of bits in the netmask.  If the
2982       <replaceable class="parameter">/y</replaceable>
2983       part is left off, then the
2984       netmask is 32 for IPv4 and 128 for IPv6, so the value represents
2985       just a single host.  On display, the
2986       <replaceable class="parameter">/y</replaceable>
2987       portion is suppressed if the netmask specifies a single host.
2988     </para>
2989    </sect2>
2990
2991    <sect2 id="datatype-cidr">
2992     <title><type>cidr</></title>
2993
2994     <indexterm>
2995      <primary>cidr</primary>
2996     </indexterm>
2997
2998     <para>
2999      The <type>cidr</type> type holds an IPv4 or IPv6 network specification.
3000      Input and output formats follow Classless Internet Domain Routing
3001      conventions.
3002      The format for specifying networks is <replaceable
3003      class="parameter">address/y</> where <replaceable
3004      class="parameter">address</> is the network represented as an
3005      IPv4 or IPv6 address, and <replaceable
3006      class="parameter">y</> is the number of bits in the netmask.  If
3007      <replaceable class="parameter">y</> is omitted, it is calculated
3008      using assumptions from the older classful network numbering system, except
3009      that it will be at least large enough to include all of the octets
3010      written in the input.  It is an error to specify a network address
3011      that has bits set to the right of the specified netmask.
3012     </para>
3013
3014     <para>
3015      <xref linkend="datatype-net-cidr-table"> shows some examples.
3016     </para>
3017
3018      <table id="datatype-net-cidr-table">
3019       <title><type>cidr</> Type Input Examples</title>
3020       <tgroup cols="3">
3021        <thead> 
3022         <row> 
3023          <entry><type>cidr</type> Input</entry>
3024          <entry><type>cidr</type> Output</entry>
3025          <entry><literal><function>abbrev</function>(<type>cidr</type>)</literal></entry>
3026         </row>
3027        </thead>
3028        <tbody>
3029         <row>
3030          <entry>192.168.100.128/25</entry>
3031          <entry>192.168.100.128/25</entry>
3032          <entry>192.168.100.128/25</entry>
3033         </row>
3034         <row>
3035          <entry>192.168/24</entry>
3036          <entry>192.168.0.0/24</entry>
3037          <entry>192.168.0/24</entry>
3038         </row>
3039         <row>
3040          <entry>192.168/25</entry>
3041          <entry>192.168.0.0/25</entry>
3042          <entry>192.168.0.0/25</entry>
3043         </row>
3044         <row>
3045          <entry>192.168.1</entry>
3046          <entry>192.168.1.0/24</entry>
3047          <entry>192.168.1/24</entry>
3048         </row>
3049         <row>
3050          <entry>192.168</entry>
3051          <entry>192.168.0.0/24</entry>
3052          <entry>192.168.0/24</entry>
3053         </row>
3054         <row>
3055          <entry>128.1</entry>
3056          <entry>128.1.0.0/16</entry>
3057          <entry>128.1/16</entry>
3058         </row>
3059         <row>
3060          <entry>128</entry>
3061          <entry>128.0.0.0/16</entry>
3062          <entry>128.0/16</entry>
3063         </row>
3064         <row>
3065          <entry>128.1.2</entry>
3066          <entry>128.1.2.0/24</entry>
3067          <entry>128.1.2/24</entry>
3068         </row>
3069         <row>
3070          <entry>10.1.2</entry>
3071          <entry>10.1.2.0/24</entry>
3072          <entry>10.1.2/24</entry>
3073         </row>
3074         <row>
3075          <entry>10.1</entry>
3076          <entry>10.1.0.0/16</entry>
3077          <entry>10.1/16</entry>
3078         </row>
3079         <row>
3080          <entry>10</entry>
3081          <entry>10.0.0.0/8</entry>
3082          <entry>10/8</entry>
3083         </row>
3084         <row>
3085          <entry>10.1.2.3/32</entry>
3086          <entry>10.1.2.3/32</entry>
3087          <entry>10.1.2.3/32</entry>
3088         </row>
3089         <row>
3090          <entry>2001:4f8:3:ba::/64</entry>
3091          <entry>2001:4f8:3:ba::/64</entry>
3092          <entry>2001:4f8:3:ba::/64</entry>
3093         </row>
3094         <row>
3095          <entry>2001:4f8:3:ba:2e0:81ff:fe22:d1f1/128</entry>
3096          <entry>2001:4f8:3:ba:2e0:81ff:fe22:d1f1/128</entry>
3097          <entry>2001:4f8:3:ba:2e0:81ff:fe22:d1f1</entry>
3098         </row>
3099         <row>
3100          <entry>::ffff:1.2.3.0/120</entry>
3101          <entry>::ffff:1.2.3.0/120</entry>
3102          <entry>::ffff:1.2.3/120</entry>
3103         </row>
3104         <row>
3105          <entry>::ffff:1.2.3.0/128</entry>
3106          <entry>::ffff:1.2.3.0/128</entry>
3107          <entry>::ffff:1.2.3.0/128</entry>
3108         </row>
3109        </tbody>
3110       </tgroup>
3111      </table>
3112    </sect2>
3113
3114    <sect2 id="datatype-inet-vs-cidr">
3115     <title><type>inet</type> vs. <type>cidr</type></title>
3116
3117     <para>
3118     The essential difference between <type>inet</type> and <type>cidr</type>
3119     data types is that <type>inet</type> accepts values with nonzero bits to
3120     the right of the netmask, whereas <type>cidr</type> does not.
3121     </para>
3122
3123       <tip>
3124         <para>
3125         If you do not like the output format for <type>inet</type> or
3126         <type>cidr</type> values, try the functions <function>host</>,
3127         <function>text</>, and <function>abbrev</>.
3128         </para>
3129       </tip>
3130    </sect2>
3131
3132    <sect2 id="datatype-macaddr">
3133     <title><type>macaddr</></>
3134
3135     <indexterm>
3136      <primary>macaddr (data type)</primary>
3137     </indexterm>
3138
3139     <indexterm>
3140      <primary>MAC address</primary>
3141      <see>macaddr</see>
3142     </indexterm>
3143
3144     <para>
3145      The <type>macaddr</> type stores MAC addresses, i.e., Ethernet
3146      card hardware addresses (although MAC addresses are used for
3147      other purposes as well).  Input is accepted in various customary
3148      formats, including
3149
3150      <simplelist>
3151       <member><literal>'08002b:010203'</></member>
3152       <member><literal>'08002b-010203'</></member>
3153       <member><literal>'0800.2b01.0203'</></member>
3154       <member><literal>'08-00-2b-01-02-03'</></member>
3155       <member><literal>'08:00:2b:01:02:03'</></member>
3156      </simplelist>
3157
3158      which would all specify the same
3159      address.  Upper and lower case is accepted for the digits
3160      <literal>a</> through <literal>f</>.  Output is always in the
3161      last of the forms shown.
3162     </para>
3163    </sect2>
3164
3165   </sect1>
3166
3167   <sect1 id="datatype-bit">
3168    <title>Bit String Types</title>
3169
3170    <indexterm zone="datatype-bit">
3171     <primary>bit string</primary>
3172     <secondary>data type</secondary>
3173    </indexterm>
3174
3175    <para>
3176     Bit strings are strings of 1's and 0's.  They can be used to store
3177     or visualize bit masks.  There are two SQL bit types:
3178     <type>bit(<replaceable>n</replaceable>)</type> and <type>bit
3179     varying(<replaceable>n</replaceable>)</type>, where
3180     <replaceable>n</replaceable> is a positive integer.
3181    </para>
3182
3183    <para>
3184     <type>bit</type> type data must match the length
3185     <replaceable>n</replaceable> exactly; it is an error to attempt to
3186     store shorter or longer bit strings.  <type>bit varying</type> data is
3187     of variable length up to the maximum length
3188     <replaceable>n</replaceable>; longer strings will be rejected.
3189     Writing <type>bit</type> without a length is equivalent to
3190     <literal>bit(1)</literal>, while <type>bit varying</type> without a length
3191     specification means unlimited length.
3192    </para>
3193
3194    <note>
3195     <para>
3196      If one explicitly casts a bit-string value to
3197      <type>bit(<replaceable>n</>)</type>, it will be truncated or
3198      zero-padded on the right to be exactly <replaceable>n</> bits,
3199      without raising an error.  Similarly,
3200      if one explicitly casts a bit-string value to
3201      <type>bit varying(<replaceable>n</>)</type>, it will be truncated
3202      on the right if it is more than <replaceable>n</> bits.
3203     </para>
3204    </note>
3205
3206    <para>
3207     Refer to <xref
3208     linkend="sql-syntax-bit-strings"> for information about the syntax
3209     of bit string constants.  Bit-logical operators and string
3210     manipulation functions are available; see <xref
3211     linkend="functions-bitstring">.
3212    </para>
3213
3214    <example>
3215     <title>Using the bit string types</title>
3216
3217 <programlisting>
3218 CREATE TABLE test (a BIT(3), b BIT VARYING(5));
3219 INSERT INTO test VALUES (B'101', B'00');
3220 INSERT INTO test VALUES (B'10', B'101');
3221 <computeroutput>
3222 ERROR:  bit string length 2 does not match type bit(3)
3223 </computeroutput>
3224 INSERT INTO test VALUES (B'10'::bit(3), B'101');
3225 SELECT * FROM test;
3226 <computeroutput>
3227   a  |  b
3228 -----+-----
3229  101 | 00
3230  100 | 101
3231 </computeroutput>
3232 </programlisting>
3233    </example>
3234
3235    <para>
3236     A bit string value requires 1 byte for each group of 8 bits, plus
3237     5 or 8 bytes overhead depending on the length of the string
3238     (but long values may be compressed or moved out-of-line, as explained
3239     in <xref linkend="datatype-character"> for character strings).
3240    </para>
3241   </sect1>
3242
3243   <sect1 id="datatype-textsearch">
3244    <title>Text Search Types</title>
3245
3246    <indexterm zone="datatype-textsearch">
3247     <primary>full text search</primary>
3248     <secondary>data types</secondary>
3249    </indexterm>
3250
3251    <indexterm zone="datatype-textsearch">
3252     <primary>text search</primary>
3253     <secondary>data types</secondary>
3254    </indexterm>
3255
3256    <para>
3257     <productname>PostgreSQL</productname> provides two data types that
3258     are designed to support full text search, which is the activity of
3259     searching through a collection of natural-language <firstterm>documents</>
3260     to locate those that best match a <firstterm>query</>.
3261     The <type>tsvector</type> type represents a document in a form suited
3262     for text search, while the <type>tsquery</type> type similarly represents
3263     a query.
3264     <xref linkend="textsearch"> provides a detailed explanation of this
3265     facility, and <xref linkend="functions-textsearch"> summarizes the
3266     related functions and operators.
3267    </para>
3268
3269    <sect2 id="datatype-tsvector">
3270     <title><type>tsvector</type></title>
3271
3272     <indexterm>
3273      <primary>tsvector (data type)</primary>
3274     </indexterm>
3275
3276     <para>
3277      A <type>tsvector</type> value is a sorted list of distinct
3278      <firstterm>lexemes</>, which are words that have been
3279      <firstterm>normalized</> to make different variants of the same word look
3280      alike (see <xref linkend="textsearch"> for details).  Sorting and
3281      duplicate-elimination are done automatically during input, as shown in
3282      this example:
3283
3284 <programlisting>
3285 SELECT 'a fat cat sat on a mat and ate a fat rat'::tsvector;
3286                       tsvector
3287 ----------------------------------------------------
3288  'a' 'on' 'and' 'ate' 'cat' 'fat' 'mat' 'rat' 'sat'
3289 </programlisting>
3290
3291      (As the example shows, the sorting is first by length and then
3292      alphabetically, but that detail is seldom important.)  To represent
3293      lexemes containing whitespace or punctuation, surround them with quotes:
3294
3295 <programlisting>
3296 SELECT $$the lexeme '    ' contains spaces$$::tsvector;
3297                  tsvector                  
3298 -------------------------------------------
3299  'the' '    ' 'lexeme' 'spaces' 'contains'
3300 </programlisting>
3301
3302      (We use dollar-quoted string literals in this example and the next one,
3303      to avoid confusing matters by having to double quote marks within the
3304      literals.)  Embedded quotes and backslashes must be doubled:
3305
3306 <programlisting>
3307 SELECT $$the lexeme 'Joe''s' contains a quote$$::tsvector;
3308                     tsvector                    
3309 ------------------------------------------------
3310  'a' 'the' 'Joe''s' 'quote' 'lexeme' 'contains'
3311 </programlisting>
3312
3313      Optionally, integer <firstterm>position(s)</>
3314      can be attached to any or all of the lexemes:
3315
3316 <programlisting>
3317 SELECT 'a:1 fat:2 cat:3 sat:4 on:5 a:6 mat:7 and:8 ate:9 a:10 fat:11 rat:12'::tsvector;
3318                                   tsvector
3319 -------------------------------------------------------------------------------
3320  'a':1,6,10 'on':5 'and':8 'ate':9 'cat':3 'fat':2,11 'mat':7 'rat':12 'sat':4
3321 </programlisting>
3322
3323      A position normally indicates the source word's location in the
3324      document.  Positional information can be used for
3325      <firstterm>proximity ranking</firstterm>.  Position values can
3326      range from 1 to 16383; larger numbers are silently clamped to 16383.
3327      Duplicate position entries are discarded.
3328     </para>
3329
3330     <para>
3331      Lexemes that have positions can further be labeled with a
3332      <firstterm>weight</>, which can be <literal>A</literal>,
3333      <literal>B</literal>, <literal>C</literal>, or <literal>D</literal>.
3334      <literal>D</literal> is the default and hence is not shown on output:
3335
3336 <programlisting>
3337 SELECT 'a:1A fat:2B,4C cat:5D'::tsvector;
3338           tsvector          
3339 ----------------------------
3340  'a':1A 'cat':5 'fat':2B,4C
3341 </programlisting>
3342
3343      Weights are typically used to reflect document structure, for example
3344      by marking title words differently from body words.  Text search
3345      ranking functions can assign different priorities to the different
3346      weight markers.
3347     </para>
3348
3349     <para>
3350      It is important to understand that the
3351      <type>tsvector</type> type itself does not perform any normalization;
3352      it assumes that the words it is given are normalized appropriately
3353      for the application.  For example,
3354
3355 <programlisting>
3356 select 'The Fat Rats'::tsvector;
3357       tsvector      
3358 --------------------
3359  'Fat' 'The' 'Rats'
3360 </programlisting>
3361
3362      For most English-text-searching applications the above words would
3363      be considered non-normalized, but <type>tsvector</type> doesn't care.
3364      Raw document text should usually be passed through
3365      <function>to_tsvector</> to normalize the words appropriately
3366      for searching:
3367
3368 <programlisting>
3369 SELECT to_tsvector('english', 'The Fat Rats');         
3370    to_tsvector   
3371 -----------------
3372  'fat':2 'rat':3
3373 </programlisting>
3374
3375      Again, see <xref linkend="textsearch"> for more detail.
3376     </para>
3377
3378    </sect2>
3379
3380    <sect2 id="datatype-tsquery">
3381     <title><type>tsquery</type></title>
3382
3383     <indexterm>
3384      <primary>tsquery (data type)</primary>
3385     </indexterm>
3386
3387     <para>
3388      A <type>tsquery</type> value stores lexemes that are to be
3389      searched for, and combines them using the boolean operators
3390      <literal>&amp;</literal> (AND), <literal>|</literal> (OR), and
3391      <literal>!</> (NOT).  Parentheses can be used to enforce grouping
3392      of the operators:
3393
3394 <programlisting>
3395  SELECT 'fat &amp; rat'::tsquery;
3396     tsquery    
3397 ---------------
3398  'fat' &amp; 'rat'
3399
3400 SELECT 'fat &amp; (rat | cat)'::tsquery;
3401           tsquery          
3402 ---------------------------
3403  'fat' &amp; ( 'rat' | 'cat' )
3404
3405 SELECT 'fat &amp; rat &amp; ! cat'::tsquery;
3406         tsquery         
3407 ------------------------
3408  'fat' &amp; 'rat' &amp; !'cat'
3409 </programlisting>
3410
3411      In the absence of parentheses, <literal>!</> (NOT) binds most tightly,
3412      and <literal>&amp;</literal> (AND) binds more tightly than
3413      <literal>|</literal> (OR).
3414     </para>
3415
3416     <para>
3417      Optionally, lexemes in a <type>tsquery</type> can be labeled with
3418      one or more weight letters, which restricts them to match only
3419      <type>tsvector</> lexemes with one of those weights:
3420
3421 <programlisting>
3422 SELECT 'fat:ab &amp; cat'::tsquery;
3423     tsquery
3424 ------------------
3425  'fat':AB &amp; 'cat'
3426 </programlisting>
3427     </para>
3428
3429     <para>
3430      Quoting rules for lexemes are the same as described above for
3431      lexemes in <type>tsvector</>; and, as with <type>tsvector</>,
3432      any required normalization of words must be done before putting
3433      them into the <type>tsquery</> type.  The <function>to_tsquery</>
3434      function is convenient for performing such normalization:
3435
3436 <programlisting>
3437 SELECT to_tsquery('Fat:ab & Cats');
3438     to_tsquery    
3439 ------------------
3440  'fat':AB & 'cat'
3441 </programlisting>
3442     </para>
3443
3444    </sect2>
3445
3446   </sect1>
3447
3448   <sect1 id="datatype-uuid">
3449    <title><acronym>UUID</acronym> Type</title>
3450
3451    <indexterm zone="datatype-uuid">
3452     <primary>UUID</primary>
3453    </indexterm>
3454
3455    <para>
3456     The data type <type>uuid</type> stores Universally Unique Identifiers
3457     (UUID) as defined by RFC 4122, ISO/IEC 9834-8:2005, and related standards.
3458     (Some systems refer to this data type as globally unique identifier, or
3459     GUID,<indexterm><primary>GUID</primary></indexterm> instead.)  Such an
3460     identifier is a 128-bit quantity that is generated by an algorithm chosen
3461     to make it very unlikely that the same identifier will be generated by
3462     anyone else in the known universe using the same algorithm.  Therefore,
3463     for distributed systems, these identifiers provide a better uniqueness
3464     guarantee than that which can be achieved using sequence generators, which
3465     are only unique within a single database.
3466    </para>
3467
3468    <para>
3469     A UUID is written as a sequence of lower-case hexadecimal digits,
3470     in several groups separated by hyphens, specifically a group of 8
3471     digits followed by three groups of 4 digits followed by a group of
3472     12 digits, for a total of 32 digits representing the 128 bits.  An
3473     example of a UUID in this standard form is:
3474 <programlisting>
3475 a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11
3476 </programlisting>
3477     <productname>PostgreSQL</productname> also accepts the following
3478     alternative forms for input:
3479     use of upper-case digits, the standard format surrounded by
3480     braces, and omitting the hyphens.  Examples are:
3481 <programlisting>
3482 A0EEBC99-9C0B-4EF8-BB6D-6BB9BD380A11
3483 {a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11}
3484 a0eebc999c0b4ef8bb6d6bb9bd380a11
3485 </programlisting>
3486     Output is always in the standard form.
3487    </para>
3488
3489    <para>
3490     <productname>PostgreSQL</productname> provides storage and comparison
3491     functions for UUIDs, but the core database does not include any
3492     function for generating UUIDs, because no single algorithm is well
3493     suited for every application.  The contrib module
3494     <filename>contrib/uuid-ossp</filename> provides functions that implement 
3495     several standard algorithms.
3496     Alternatively, UUIDs could be generated by client applications or
3497     other libraries invoked through a server-side function.
3498    </para>
3499   </sect1>
3500
3501   <sect1 id="datatype-xml">
3502    <title><acronym>XML</> Type</title>
3503
3504    <indexterm zone="datatype-xml">
3505     <primary>XML</primary>
3506    </indexterm>
3507
3508    <para>
3509     The data type <type>xml</type> can be used to store XML data.  Its
3510     advantage over storing XML data in a <type>text</type> field is that it
3511     checks the input values for well-formedness, and there are support
3512     functions to perform type-safe operations on it; see <xref
3513     linkend="functions-xml">.  Use of this data type requires the
3514     installation to have been built with <command>configure 
3515     --with-libxml</>.
3516    </para>
3517
3518    <para>
3519     The <type>xml</type> type can store well-formed
3520     <quote>documents</quote>, as defined by the XML standard, as well
3521     as <quote>content</quote> fragments, which are defined by the
3522     production <literal>XMLDecl? content</literal> in the XML
3523     standard.  Roughly, this means that content fragments can have
3524     more than one top-level element or character node.  The expression
3525     <literal><replaceable>xmlvalue</replaceable> IS DOCUMENT</literal>
3526     can be used to evaluate whether a particular <type>xml</type>
3527     value is a full document or only a content fragment.
3528    </para>
3529
3530    <sect2>
3531     <title>Creating XML Values</title>
3532    <para>
3533     To produce a value of type <type>xml</type> from character data,
3534     use the function
3535     <function>xmlparse</function>:<indexterm><primary>xmlparse</primary></indexterm>
3536 <synopsis>
3537 XMLPARSE ( { DOCUMENT | CONTENT } <replaceable>value</replaceable>)
3538 </synopsis>
3539     Examples:
3540 <programlisting><![CDATA[
3541 XMLPARSE (DOCUMENT '<?xml version="1.0"?><book><title>Manual</title><chapter>...</chapter><book>')
3542 XMLPARSE (CONTENT 'abc<foo>bar</foo><bar>foo</bar>')
3543 ]]></programlisting>
3544     While this is the only way to convert character strings into XML
3545     values according to the SQL standard, the PostgreSQL-specific
3546     syntaxes:
3547 <programlisting><![CDATA[
3548 xml '<foo>bar</foo>'
3549 '<foo>bar</foo>'::xml
3550 ]]></programlisting>
3551     can also be used.
3552    </para>
3553
3554    <para>
3555     The <type>xml</type> type does not validate its input values
3556     against a possibly included document type declaration
3557     (DTD).<indexterm><primary>DTD</primary></indexterm>
3558    </para>
3559
3560    <para>
3561     The inverse operation, producing character string type values from
3562     <type>xml</type>, uses the function
3563     <function>xmlserialize</function>:<indexterm><primary>xmlserialize</primary></indexterm>
3564 <synopsis>
3565 XMLSERIALIZE ( { DOCUMENT | CONTENT } <replaceable>value</replaceable> AS <replaceable>type</replaceable> )
3566 </synopsis>
3567     <replaceable>type</replaceable> can be one of
3568     <type>character</type>, <type>character varying</type>, or
3569     <type>text</type> (or an alias name for those).  Again, according
3570     to the SQL standard, this is the only way to convert between type
3571     <type>xml</type> and character types, but PostgreSQL also allows
3572     you to simply cast the value.
3573    </para>
3574
3575    <para>
3576     When character string values are cast to or from type
3577     <type>xml</type> without going through <type>XMLPARSE</type> or
3578     <type>XMLSERIALIZE</type>, respectively, the choice of
3579     <literal>DOCUMENT</literal> versus <literal>CONTENT</literal> is
3580     determined by the <quote>XML option</quote>
3581     <indexterm><primary>XML option</primary></indexterm>
3582     session configuration parameter, which can be set using the
3583     standard command
3584 <synopsis>
3585 SET XML OPTION { DOCUMENT | CONTENT };
3586 </synopsis>
3587     or the more PostgreSQL-like syntax
3588 <synopsis>
3589 SET xmloption TO { DOCUMENT | CONTENT };
3590 </synopsis>
3591     The default is <literal>CONTENT</literal>, so all forms of XML
3592     data are allowed.
3593    </para>
3594    </sect2>
3595
3596    <sect2>
3597     <title>Encoding Handling</title>
3598    <para>
3599     Care must be taken when dealing with multiple character encodings
3600     on the client, server, and in the XML data passed through them.
3601     When using the text mode to pass queries to the server and query
3602     results to the client (which is the normal mode), PostgreSQL
3603     converts all character data passed between the client and the
3604     server and vice versa to the character encoding of the respective
3605     end; see <xref linkend="multibyte">.  This includes string
3606     representations of XML values, such as in the above examples.
3607     This would ordinarily mean that encoding declarations contained in
3608     XML data might become invalid as the character data is converted
3609     to other encodings while travelling between client and server,
3610     while the embedded encoding declaration is not changed.  To cope
3611     with this behavior, an encoding declaration contained in a
3612     character string presented for input to the <type>xml</type> type
3613     is <emphasis>ignored</emphasis>, and the content is always assumed
3614     to be in the current server encoding.  Consequently, for correct
3615     processing, such character strings of XML data must be sent off
3616     from the client in the current client encoding.  It is the
3617     responsibility of the client to either convert the document to the
3618     current client encoding before sending it off to the server or to
3619     adjust the client encoding appropriately.  On output, values of
3620     type <type>xml</type> will not have an encoding declaration, and
3621     clients must assume that the data is in the current client
3622     encoding.
3623    </para>
3624
3625    <para>
3626     When using the binary mode to pass query parameters to the server
3627     and query results back to the client, no character set conversion
3628     is performed, so the situation is different.  In this case, an
3629     encoding declaration in the XML data will be observed, and if it
3630     is absent, the data will be assumed to be in UTF-8 (as required by
3631     the XML standard; note that PostgreSQL does not support UTF-16 at
3632     all).  On output, data will have an encoding declaration
3633     specifying the client encoding, unless the client encoding is
3634     UTF-8, in which case it will be omitted.
3635    </para>
3636
3637    <para>
3638     Needless to say, processing XML data with PostgreSQL will be less
3639     error-prone and more efficient if data encoding, client encoding,
3640     and server encoding are the same.  Since XML data is internally
3641     processed in UTF-8, computations will be most efficient if the
3642     server encoding is also UTF-8.
3643    </para>
3644    </sect2>
3645
3646    <sect2>
3647    <title>Accessing XML Values</title>
3648
3649    <para>
3650     The <type>xml</type> data type is unusual in that it does not
3651     provide any comparison operators.  This is because there is no
3652     well-defined and universally useful comparison algorithm for XML
3653     data.  One consequence of this is that you cannot retrieve rows by
3654     comparing an <type>xml</type> column against a search value.  XML
3655     values should therefore typically be accompanied by a separate key
3656     field such as an ID.  An alternative solution for comparing XML
3657     values is to convert them to character strings first, but note
3658     that character string comparison has little to do with a useful
3659     XML comparison method.
3660    </para>
3661
3662    <para>
3663     Since there are no comparison operators for the <type>xml</type>
3664     data type, it is not possible to create an index directly on a
3665     column of this type.  If speedy searches in XML data are desired,
3666     possible workarounds would be casting the expression to a
3667     character string type and indexing that, or indexing an XPath
3668     expression.  The actual query would of course have to be adjusted
3669     to search by the indexed expression.
3670    </para>
3671
3672    <para>
3673     The text-search functionality in PostgreSQL could also be used to speed
3674     up full-document searches in XML data.  The necessary
3675     preprocessing support is, however, not available in the PostgreSQL
3676     distribution in this release.
3677    </para>
3678    </sect2>
3679   </sect1>
3680
3681   &array;
3682
3683   &rowtypes;
3684
3685   <sect1 id="datatype-oid">
3686    <title>Object Identifier Types</title>
3687
3688    <indexterm zone="datatype-oid">
3689     <primary>object identifier</primary>
3690     <secondary>data type</secondary>
3691    </indexterm>
3692
3693    <indexterm zone="datatype-oid">
3694     <primary>oid</primary>
3695    </indexterm>
3696
3697    <indexterm zone="datatype-oid">
3698     <primary>regproc</primary>
3699    </indexterm>
3700
3701    <indexterm zone="datatype-oid">
3702     <primary>regprocedure</primary>
3703    </indexterm>
3704
3705    <indexterm zone="datatype-oid">
3706     <primary>regoper</primary>
3707    </indexterm>
3708
3709    <indexterm zone="datatype-oid">
3710     <primary>regoperator</primary>
3711    </indexterm>
3712
3713    <indexterm zone="datatype-oid">
3714     <primary>regclass</primary>
3715    </indexterm>
3716
3717    <indexterm zone="datatype-oid">
3718     <primary>regtype</primary>
3719    </indexterm>
3720
3721    <indexterm zone="datatype-oid">
3722     <primary>regconfig</primary>
3723    </indexterm>
3724
3725    <indexterm zone="datatype-oid">
3726     <primary>regdictionary</primary>
3727    </indexterm>
3728
3729    <indexterm zone="datatype-oid">
3730     <primary>xid</primary>
3731    </indexterm>
3732
3733    <indexterm zone="datatype-oid">
3734     <primary>cid</primary>
3735    </indexterm>
3736
3737    <indexterm zone="datatype-oid">
3738     <primary>tid</primary>
3739    </indexterm>
3740
3741    <para>
3742     Object identifiers (OIDs) are used internally by
3743     <productname>PostgreSQL</productname> as primary keys for various
3744     system tables.  OIDs are not added to user-created tables, unless
3745     <literal>WITH OIDS</literal> is specified when the table is
3746     created, or the <xref linkend="guc-default-with-oids">
3747     configuration variable is enabled.  Type <type>oid</> represents
3748     an object identifier.  There are also several alias types for
3749     <type>oid</>: <type>regproc</>, <type>regprocedure</>,
3750     <type>regoper</>, <type>regoperator</>, <type>regclass</>,
3751     <type>regtype</>, <type>regconfig</>, and <type>regdictionary</>.
3752     <xref linkend="datatype-oid-table"> shows an overview.
3753    </para>
3754
3755    <para>
3756     The <type>oid</> type is currently implemented as an unsigned
3757     four-byte integer.  Therefore, it is not large enough to provide
3758     database-wide uniqueness in large databases, or even in large
3759     individual tables.  So, using a user-created table's OID column as
3760     a primary key is discouraged.  OIDs are best used only for
3761     references to system tables.
3762    </para>
3763
3764    <para>
3765     The <type>oid</> type itself has few operations beyond comparison.
3766     It can be cast to integer, however, and then manipulated using the
3767     standard integer operators.  (Beware of possible
3768     signed-versus-unsigned confusion if you do this.)
3769    </para>
3770
3771    <para>
3772     The OID alias types have no operations of their own except
3773     for specialized input and output routines.  These routines are able
3774     to accept and display symbolic names for system objects, rather than
3775     the raw numeric value that type <type>oid</> would use.  The alias
3776     types allow simplified lookup of OID values for objects.  For example,
3777     to examine the <structname>pg_attribute</> rows related to a table
3778     <literal>mytable</>, one could write:
3779 <programlisting>
3780 SELECT * FROM pg_attribute WHERE attrelid = 'mytable'::regclass;
3781 </programlisting>
3782     rather than:
3783 <programlisting>
3784 SELECT * FROM pg_attribute
3785   WHERE attrelid = (SELECT oid FROM pg_class WHERE relname = 'mytable');
3786 </programlisting>
3787     While that doesn't look all that bad by itself, it's still oversimplified.
3788     A far more complicated sub-select would be needed to
3789     select the right OID if there are multiple tables named
3790     <literal>mytable</> in different schemas.
3791     The <type>regclass</> input converter handles the table lookup according
3792     to the schema path setting, and so it does the <quote>right thing</>
3793     automatically.  Similarly, casting a table's OID to
3794     <type>regclass</> is handy for symbolic display of a numeric OID.
3795    </para>
3796
3797     <table id="datatype-oid-table">
3798      <title>Object Identifier Types</title>
3799      <tgroup cols="4">
3800       <thead>
3801        <row>
3802         <entry>Name</entry>
3803         <entry>References</entry>
3804         <entry>Description</entry>
3805         <entry>Value Example</entry>
3806        </row>
3807       </thead>
3808
3809       <tbody>
3810
3811        <row>
3812         <entry><type>oid</></entry>
3813         <entry>any</entry>
3814         <entry>numeric object identifier</entry>
3815         <entry><literal>564182</></entry>
3816        </row>
3817
3818        <row>
3819         <entry><type>regproc</></entry>
3820         <entry><structname>pg_proc</></entry>
3821         <entry>function name</entry>
3822         <entry><literal>sum</></entry>
3823        </row>
3824
3825        <row>
3826         <entry><type>regprocedure</></entry>
3827         <entry><structname>pg_proc</></entry>
3828         <entry>function with argument types</entry>
3829         <entry><literal>sum(int4)</></entry>
3830        </row>
3831
3832        <row>
3833         <entry><type>regoper</></entry>
3834         <entry><structname>pg_operator</></entry>
3835         <entry>operator name</entry>
3836         <entry><literal>+</></entry>
3837        </row>
3838
3839        <row>
3840         <entry><type>regoperator</></entry>
3841         <entry><structname>pg_operator</></entry>
3842         <entry>operator with argument types</entry>
3843         <entry><literal>*(integer,integer)</> or <literal>-(NONE,integer)</></entry>
3844        </row>
3845
3846        <row>
3847         <entry><type>regclass</></entry>
3848         <entry><structname>pg_class</></entry>
3849         <entry>relation name</entry>
3850         <entry><literal>pg_type</></entry>
3851        </row>
3852
3853        <row>
3854         <entry><type>regtype</></entry>
3855         <entry><structname>pg_type</></entry>
3856         <entry>data type name</entry>
3857         <entry><literal>integer</></entry>
3858        </row>
3859
3860        <row>
3861         <entry><type>regconfig</></entry>
3862         <entry><structname>pg_ts_config</></entry>
3863         <entry>text search configuration</entry>
3864         <entry><literal>english</></entry>
3865        </row>
3866
3867        <row>
3868         <entry><type>regdictionary</></entry>
3869         <entry><structname>pg_ts_dict</></entry>
3870         <entry>text search dictionary</entry>
3871         <entry><literal>simple</></entry>
3872        </row>
3873       </tbody>
3874      </tgroup>
3875     </table>
3876
3877    <para>
3878     All of the OID alias types accept schema-qualified names, and will
3879     display schema-qualified names on output if the object would not
3880     be found in the current search path without being qualified.
3881     The <type>regproc</> and <type>regoper</> alias types will only
3882     accept input names that are unique (not overloaded), so they are
3883     of limited use; for most uses <type>regprocedure</> or
3884     <type>regoperator</> is more appropriate.  For <type>regoperator</>,
3885     unary operators are identified by writing <literal>NONE</> for the unused
3886     operand.
3887    </para>
3888
3889    <para>
3890     An additional property of the OID alias types is that if a
3891     constant of one of these types appears in a stored expression
3892     (such as a column default expression or view), it creates a dependency
3893     on the referenced object.  For example, if a column has a default
3894     expression <literal>nextval('my_seq'::regclass)</>,
3895     <productname>PostgreSQL</productname>
3896     understands that the default expression depends on the sequence
3897     <literal>my_seq</>; the system will not let the sequence be dropped
3898     without first removing the default expression.
3899    </para>
3900
3901    <para>
3902     Another identifier type used by the system is <type>xid</>, or transaction
3903     (abbreviated <abbrev>xact</>) identifier.  This is the data type of the system columns
3904     <structfield>xmin</> and <structfield>xmax</>.  Transaction identifiers are 32-bit quantities.
3905    </para>
3906
3907    <para>
3908     A third identifier type used by the system is <type>cid</>, or
3909     command identifier.  This is the data type of the system columns
3910     <structfield>cmin</> and <structfield>cmax</>. Command identifiers are also 32-bit quantities.
3911    </para>
3912
3913    <para>
3914     A final identifier type used by the system is <type>tid</>, or tuple
3915     identifier (row identifier).  This is the data type of the system column
3916     <structfield>ctid</>.  A tuple ID is a pair
3917     (block number, tuple index within block) that identifies the
3918     physical location of the row within its table.
3919    </para>
3920
3921    <para>
3922     (The system columns are further explained in <xref
3923     linkend="ddl-system-columns">.)
3924    </para>
3925   </sect1>
3926
3927   <sect1 id="datatype-pseudo">
3928    <title>Pseudo-Types</title>
3929
3930    <indexterm zone="datatype-pseudo">
3931     <primary>record</primary>
3932    </indexterm>
3933
3934    <indexterm zone="datatype-pseudo">
3935     <primary>any</primary>
3936    </indexterm>
3937
3938    <indexterm zone="datatype-pseudo">
3939     <primary>anyelement</primary>
3940    </indexterm>
3941
3942    <indexterm zone="datatype-pseudo">
3943     <primary>anyarray</primary>
3944    </indexterm>
3945
3946    <indexterm zone="datatype-pseudo">
3947     <primary>anynonarray</primary>
3948    </indexterm>
3949
3950    <indexterm zone="datatype-pseudo">
3951     <primary>anyenum</primary>
3952    </indexterm>
3953
3954    <indexterm zone="datatype-pseudo">
3955     <primary>void</primary>
3956    </indexterm>
3957
3958    <indexterm zone="datatype-pseudo">
3959     <primary>trigger</primary>
3960    </indexterm>
3961
3962    <indexterm zone="datatype-pseudo">
3963     <primary>language_handler</primary>
3964    </indexterm>
3965
3966    <indexterm zone="datatype-pseudo">
3967     <primary>cstring</primary>
3968    </indexterm>
3969
3970    <indexterm zone="datatype-pseudo">
3971     <primary>internal</primary>
3972    </indexterm>
3973
3974    <indexterm zone="datatype-pseudo">
3975     <primary>opaque</primary>
3976    </indexterm>
3977
3978    <para>
3979     The <productname>PostgreSQL</productname> type system contains a
3980     number of special-purpose entries that are collectively called
3981     <firstterm>pseudo-types</>.  A pseudo-type cannot be used as a
3982     column data type, but it can be used to declare a function's
3983     argument or result type.  Each of the available pseudo-types is
3984     useful in situations where a function's behavior does not
3985     correspond to simply taking or returning a value of a specific
3986     <acronym>SQL</acronym> data type.  <xref
3987     linkend="datatype-pseudotypes-table"> lists the existing
3988     pseudo-types.
3989    </para>
3990
3991     <table id="datatype-pseudotypes-table">
3992      <title>Pseudo-Types</title>
3993      <tgroup cols="2">
3994       <thead>
3995        <row>
3996         <entry>Name</entry>
3997         <entry>Description</entry>
3998        </row>
3999       </thead>
4000
4001       <tbody>
4002        <row>
4003         <entry><type>any</></entry>
4004         <entry>Indicates that a function accepts any input data type whatever.</entry>
4005        </row>
4006
4007        <row>
4008         <entry><type>anyarray</></entry>
4009         <entry>Indicates that a function accepts any array data type
4010         (see <xref linkend="extend-types-polymorphic">).</entry>
4011        </row>
4012
4013        <row>
4014         <entry><type>anyelement</></entry>
4015         <entry>Indicates that a function accepts any data type
4016         (see <xref linkend="extend-types-polymorphic">).</entry>
4017        </row>
4018
4019        <row>
4020         <entry><type>anyenum</></entry>
4021         <entry>Indicates that a function accepts any enum data type
4022         (see <xref linkend="extend-types-polymorphic"> and
4023         <xref linkend="datatype-enum">).</entry>
4024        </row>
4025
4026        <row>
4027         <entry><type>anynonarray</></entry>
4028         <entry>Indicates that a function accepts any non-array data type
4029         (see <xref linkend="extend-types-polymorphic">).</entry>
4030        </row>
4031
4032        <row>
4033         <entry><type>cstring</></entry>
4034         <entry>Indicates that a function accepts or returns a null-terminated C string.</entry>
4035        </row>
4036
4037        <row>
4038         <entry><type>internal</></entry>
4039         <entry>Indicates that a function accepts or returns a server-internal
4040         data type.</entry>
4041        </row>
4042
4043        <row>
4044         <entry><type>language_handler</></entry>
4045         <entry>A procedural language call handler is declared to return <type>language_handler</>.</entry>
4046        </row>
4047
4048        <row>
4049         <entry><type>record</></entry>
4050         <entry>Identifies a function returning an unspecified row type.</entry>
4051        </row>
4052
4053        <row>
4054         <entry><type>trigger</></entry>
4055         <entry>A trigger function is declared to return <type>trigger.</></entry>
4056        </row>
4057
4058        <row>
4059         <entry><type>void</></entry>
4060         <entry>Indicates that a function returns no value.</entry>
4061        </row>
4062
4063        <row>
4064         <entry><type>opaque</></entry>
4065         <entry>An obsolete type name that formerly served all the above purposes.</entry>
4066        </row>
4067       </tbody>
4068      </tgroup>
4069     </table>
4070
4071    <para>
4072     Functions coded in C (whether built-in or dynamically loaded) can be
4073     declared to accept or return any of these pseudo data types.  It is up to
4074     the function author to ensure that the function will behave safely
4075     when a pseudo-type is used as an argument type.
4076    </para>
4077
4078    <para>
4079     Functions coded in procedural languages can use pseudo-types only as
4080     allowed by their implementation languages.  At present the procedural
4081     languages all forbid use of a pseudo-type as argument type, and allow
4082     only <type>void</> and <type>record</> as a result type (plus
4083     <type>trigger</> when the function is used as a trigger).  Some also
4084     support polymorphic functions using the types <type>anyarray</>,
4085     <type>anyelement</>, <type>anyenum</>, and <type>anynonarray</>.
4086    </para>
4087
4088    <para>
4089     The <type>internal</> pseudo-type is used to declare functions
4090     that are meant only to be called internally by the database
4091     system, and not by direct invocation in a <acronym>SQL</acronym>
4092     query.  If a function has at least one <type>internal</>-type
4093     argument then it cannot be called from <acronym>SQL</acronym>.  To
4094     preserve the type safety of this restriction it is important to
4095     follow this coding rule: do not create any function that is
4096     declared to return <type>internal</> unless it has at least one
4097     <type>internal</> argument.
4098    </para>
4099
4100   </sect1>
4101
4102  </chapter>