]> granicus.if.org Git - postgresql/blob - doc/src/sgml/datatype.sgml
This patch improves the documentation for SERIAL columns a little bit.
[postgresql] / doc / src / sgml / datatype.sgml
1 <!--
2 $Header: /cvsroot/pgsql/doc/src/sgml/datatype.sgml,v 1.110 2002/12/06 05:17:42 momjian Exp $
3 -->
4
5  <chapter id="datatype">
6   <title id="datatype-title">Data Types</title>
7
8   <indexterm zone="datatype">
9    <primary>data types</primary>
10   </indexterm>
11
12   <indexterm>
13    <primary>types</primary>
14    <see>data types</see>
15   </indexterm>
16
17   <para>
18    <productname>PostgreSQL</productname> has a rich set of native data 
19    types available to users.
20    Users may add new types to <productname>PostgreSQL</productname> using the
21    <command>CREATE TYPE</command> command.
22   </para>
23
24   <para>
25    <xref linkend="datatype-table"> shows all general-purpose data types
26    included in the standard distribution.  Most of the alternative names
27    listed in the 
28    <quote>Aliases</quote> column are the names used internally by
29    <productname>PostgreSQL</productname> for historical reasons.  In
30    addition, some internally used or deprecated types are available,
31    but they are not listed here.
32   </para>
33
34  <para>
35    <table id="datatype-table">
36     <title>Data Types</title>
37     <tgroup cols="3">
38      <thead>
39       <row>
40        <entry>Type Name</entry>
41        <entry>Aliases</entry>
42        <entry>Description</entry>
43       </row>
44      </thead>
45
46      <tbody>
47       <row>
48        <entry><type>bigint</type></entry>
49        <entry><type>int8</type></entry>
50        <entry>signed eight-byte integer</entry>
51       </row>
52
53       <row>
54        <entry><type>bigserial</type></entry>
55        <entry><type>serial8</type></entry>
56        <entry>autoincrementing eight-byte integer</entry>
57       </row>
58
59       <row>
60        <entry><type>bit</type></entry>
61        <entry></entry>
62        <entry>fixed-length bit string</entry>
63       </row>
64
65       <row>
66        <entry><type>bit varying(<replaceable>n</replaceable>)</type></entry>
67        <entry><type>varbit(<replaceable>n</replaceable>)</type></entry>
68        <entry>variable-length bit string</entry>
69       </row>
70
71       <row>
72        <entry><type>boolean</type></entry>
73        <entry><type>bool</type></entry>
74        <entry>logical Boolean (true/false)</entry>
75       </row>
76
77       <row>
78        <entry><type>box</type></entry>
79        <entry></entry>
80        <entry>rectangular box in 2D plane</entry>
81       </row>
82
83       <row>
84        <entry><type>bytea</type></entry>
85        <entry></entry>
86        <entry>binary data</entry>
87       </row>
88
89       <row>
90        <entry><type>character(<replaceable>n</replaceable>)</type></entry>
91        <entry><type>char(<replaceable>n</replaceable>)</type></entry>
92        <entry>fixed-length character string</entry>
93       </row>
94
95       <row>
96        <entry><type>character varying(<replaceable>n</replaceable>)</type></entry>
97        <entry><type>varchar(<replaceable>n</replaceable>)</type></entry>
98        <entry>variable-length character string</entry>
99       </row>
100
101       <row>
102        <entry><type>cidr</type></entry>
103        <entry></entry>
104        <entry>IP network address</entry>
105       </row>
106
107       <row>
108        <entry><type>circle</type></entry>
109        <entry></entry>
110        <entry>circle in 2D plane</entry>
111       </row>
112
113       <row>
114        <entry><type>date</type></entry>
115        <entry></entry>
116        <entry>calendar date (year, month, day)</entry>
117       </row>
118
119       <row>
120        <entry><type>double precision</type></entry>
121        <entry><type>float8</type></entry>
122        <entry>double precision floating-point number</entry>
123       </row>
124
125       <row>
126        <entry><type>inet</type></entry>
127        <entry></entry>
128        <entry>IP host address</entry>
129       </row>
130
131       <row>
132        <entry><type>integer</type></entry>
133        <entry><type>int</type>, <type>int4</type></entry>
134        <entry>signed four-byte integer</entry>
135       </row>
136
137       <row>
138        <entry><type>interval(<replaceable>p</replaceable>)</type></entry>
139        <entry></entry>
140        <entry>general-use time span</entry>
141       </row>
142
143       <row>
144        <entry><type>line</type></entry>
145        <entry></entry>
146        <entry>infinite line in 2D plane (not implemented)</entry>
147       </row>
148
149       <row>
150        <entry><type>lseg</type></entry>
151        <entry></entry>
152        <entry>line segment in 2D plane</entry>
153       </row>
154
155       <row>
156        <entry><type>macaddr</type></entry>
157        <entry></entry>
158        <entry>MAC address</entry>
159       </row>
160
161       <row>
162        <entry><type>money</type></entry>
163        <entry></entry>
164        <entry>currency amount</entry>
165       </row>
166
167       <row>
168        <entry><type>numeric [ (<replaceable>p</replaceable>,
169          <replaceable>s</replaceable>) ]</type></entry>
170        <entry><type>decimal [ (<replaceable>p</replaceable>,
171          <replaceable>s</replaceable>) ]</type></entry>
172        <entry>exact numeric with selectable precision</entry>
173       </row>
174
175       <row>
176        <entry><type>path</type></entry>
177        <entry></entry>
178        <entry>open and closed geometric path in 2D plane</entry>
179       </row>
180
181       <row>
182        <entry><type>point</type></entry>
183        <entry></entry>
184        <entry>geometric point in 2D plane</entry>
185       </row>
186
187       <row>
188        <entry><type>polygon</type></entry>
189        <entry></entry>
190        <entry>closed geometric path in 2D plane</entry>
191       </row>
192
193       <row>
194        <entry><type>real</type></entry>
195        <entry><type>float4</type></entry>
196        <entry>single precision floating-point number</entry>
197       </row>
198
199       <row>
200        <entry><type>smallint</type></entry>
201        <entry><type>int2</type></entry>
202        <entry>signed two-byte integer</entry>
203       </row>
204
205       <row>
206        <entry><type>serial</type></entry>
207        <entry><type>serial4</type></entry>
208        <entry>autoincrementing four-byte integer</entry>
209       </row>
210
211       <row>
212        <entry><type>text</type></entry>
213        <entry></entry>
214        <entry>variable-length character string</entry>
215       </row>
216
217       <row>
218        <entry><type>time [ (<replaceable>p</replaceable>) ] [ without time zone ]</type></entry>
219        <entry></entry>
220        <entry>time of day</entry>
221       </row>
222
223       <row>
224        <entry><type>time [ (<replaceable>p</replaceable>) ] with time zone</type></entry>
225        <entry><type>timetz</type></entry>
226        <entry>time of day, including time zone</entry>
227       </row>
228
229       <row>
230        <entry><type>timestamp [ (<replaceable>p</replaceable>) ] without time zone</type></entry>
231        <entry><type>timestamp</type></entry>
232        <entry>date and time</entry>
233       </row>
234
235       <row>
236        <entry><type>timestamp [ (<replaceable>p</replaceable>) ] [ with time zone ]</type></entry>
237        <entry><type>timestamptz</type></entry>
238        <entry>date and time, including time zone</entry>
239       </row>
240      </tbody>
241     </tgroup>
242    </table>
243   </para>
244
245   <note>
246    <title>Compatibility</title>
247    <para>
248     The following types (or spellings thereof) are specified by
249     <acronym>SQL</acronym>: <type>bit</type>, <type>bit
250     varying</type>, <type>boolean</type>, <type>char</type>,
251     <type>character</type>, <type>character varying</type>,
252     <type>varchar</type>, <type>date</type>, <type>double
253     precision</type>, <type>integer</type>, <type>interval</type>,
254     <type>numeric</type>, <type>decimal</type>, <type>real</type>,
255     <type>smallint</type>, <type>time</type>, <type>timestamp</type>
256     (both with or without time zone).
257    </para>
258   </note>
259
260   <para>
261    Each data type has an external representation determined by its input
262    and output functions.  Many of the built-in types have
263    obvious external formats.  However, several types are either unique
264    to <productname>PostgreSQL</productname>, such as open and closed
265    paths, or have several possibilities for formats, such as the date
266    and time types.
267    Most of the input and output functions corresponding to the
268    base types (e.g., integers and floating-point numbers) do some
269    error-checking.
270    Some of the input and output functions are not invertible.  That is,
271    the result of an output function may lose precision when compared to
272    the original input.
273   </para>
274
275   <para>
276    Some of the operators and functions (e.g.,
277    addition and multiplication) do not perform run-time error-checking in the
278    interests of improving execution speed.
279    On some systems, for example, the numeric operators for some data types may
280    silently underflow or overflow.
281   </para>
282
283   <sect1 id="datatype-numeric">
284    <title>Numeric Types</title>
285
286    <indexterm zone="datatype-numeric">
287     <primary>data types</primary>
288     <secondary>numeric</secondary>
289    </indexterm>
290
291    <indexterm zone="datatype-numeric">
292     <primary>integer</primary>
293    </indexterm>
294
295    <indexterm zone="datatype-numeric">
296     <primary>smallint</primary>
297    </indexterm>
298
299    <indexterm zone="datatype-numeric">
300     <primary>bigint</primary>
301    </indexterm>
302
303    <indexterm>
304     <primary>int4</primary>
305     <see>integer</see>
306    </indexterm>
307
308    <indexterm>
309     <primary>int2</primary>
310     <see>smallint</see>
311    </indexterm>
312
313    <indexterm>
314     <primary>int8</primary>
315     <see>bigint</see>
316    </indexterm>
317
318    <indexterm zone="datatype-numeric">
319     <primary>numeric (data type)</primary>
320    </indexterm>
321
322    <indexterm>
323     <primary>decimal</primary>
324     <see>numeric</see>
325    </indexterm>
326
327    <indexterm zone="datatype-numeric">
328     <primary>real</primary>
329    </indexterm>
330
331    <indexterm zone="datatype-numeric">
332     <primary>double precision</primary>
333    </indexterm>
334
335    <indexterm>
336     <primary>float4</primary>
337     <see>real</see>
338    </indexterm>
339
340    <indexterm>
341     <primary>float8</primary>
342     <see>double precision</see>
343    </indexterm>
344
345    <indexterm zone="datatype-numeric">
346     <primary>floating point</primary>
347    </indexterm>
348
349    <para>
350     Numeric types consist of two-, four-, and eight-byte integers,
351     four- and eight-byte floating-point numbers, and fixed-precision
352     decimals.  <xref linkend="datatype-numeric-table"> lists the
353     available types.
354    </para>
355
356     <table id="datatype-numeric-table">
357      <title>Numeric Types</title>
358      <tgroup cols="4">
359       <thead>
360        <row>
361         <entry>Type name</entry>
362         <entry>Storage size</entry>
363         <entry>Description</entry>
364         <entry>Range</entry>
365        </row>
366       </thead>
367
368       <tbody>
369        <row>
370         <entry><type>smallint</></entry>
371         <entry>2 bytes</entry>
372         <entry>small range fixed-precision</entry>
373         <entry>-32768 to +32767</entry>
374        </row>
375        <row>
376         <entry><type>integer</></entry>
377         <entry>4 bytes</entry>
378         <entry>usual choice for fixed-precision</entry>
379         <entry>-2147483648 to +2147483647</entry>
380        </row>
381        <row>
382         <entry><type>bigint</></entry>
383         <entry>8 bytes</entry>
384         <entry>large range fixed-precision</entry>
385         <entry>-9223372036854775808 to 9223372036854775807</entry>
386        </row>
387
388        <row>
389         <entry><type>decimal</></entry>
390         <entry>variable</entry>
391         <entry>user-specified precision, exact</entry>
392         <entry>no limit</entry>
393        </row>
394        <row>
395         <entry><type>numeric</></entry>
396         <entry>variable</entry>
397         <entry>user-specified precision, exact</entry>
398         <entry>no limit</entry>
399        </row>
400
401        <row>
402         <entry><type>real</></entry>
403         <entry>4 bytes</entry>
404         <entry>variable-precision, inexact</entry>
405         <entry>6 decimal digits precision</entry>
406        </row>
407        <row>
408         <entry><type>double precision</></entry>
409         <entry>8 bytes</entry>
410         <entry>variable-precision, inexact</entry>
411         <entry>15 decimal digits precision</entry>
412        </row>
413
414        <row>
415         <entry><type>serial</></entry>
416         <entry>4 bytes</entry>
417         <entry>autoincrementing integer</entry>
418         <entry>1 to 2147483647</entry>
419        </row>
420
421        <row>
422         <entry><type>bigserial</type></entry>
423         <entry>8 bytes</entry>
424         <entry>large autoincrementing integer</entry>
425         <entry>1 to 9223372036854775807</entry>
426        </row>
427       </tbody>
428      </tgroup>
429     </table>
430
431    <para>
432     The syntax of constants for the numeric types is described in
433     <xref linkend="sql-syntax-constants">.  The numeric types have a
434     full set of corresponding arithmetic operators and
435     functions. Refer to <xref linkend="functions"> for more
436     information.  The following sections describe the types in detail.
437    </para>
438
439    <sect2 id="datatype-int">
440     <title>The Integer Types</title>
441
442     <para>
443      The types <type>smallint</type>, <type>integer</type>,
444      <type>bigint</type> store whole numbers, that is, numbers without
445      fractional components, of various ranges.  Attempts to store
446      values outside of the allowed range will result in an error.
447     </para>
448
449     <para>
450      The type <type>integer</type> is the usual choice, as it offers
451      the best balance between range, storage size, and performance.
452      The <type>smallint</type> type is generally only used if disk
453      space is at a premium.  The <type>bigint</type> type should only
454      be used if the <type>integer</type> range is not sufficient,
455      because the latter is definitely faster.
456     </para>
457
458     <para>
459      The <type>bigint</type> type may not function correctly on all
460      platforms, since it relies on compiler support for eight-byte
461      integers.  On a machine without such support, <type>bigint</type>
462      acts the same as <type>integer</type> (but still takes up eight
463      bytes of storage).  However, we are not aware of any reasonable
464      platform where this is actually the case.
465     </para>
466
467     <para>
468      <acronym>SQL</acronym> only specifies the integer types
469      <type>integer</type> (or <type>int</type>) and
470      <type>smallint</type>.  The type <type>bigint</type>, and the
471      type names <type>int2</type>, <type>int4</type>, and
472      <type>int8</type> are extensions, which are shared with various
473      other <acronym>SQL</acronym> database systems.
474     </para>
475
476     <note>
477      <para>
478       If you have a column of type <type>smallint</type> or
479       <type>bigint</type> with an index, you may encounter problems
480       getting the system to use that index.  For instance, a clause of
481       the form
482 <programlisting>
483 ... WHERE smallint_column = 42
484 </programlisting>
485       will not use an index, because the system assigns type
486       <type>integer</type> to the constant 42, and
487       <productname>PostgreSQL</productname> currently
488       cannot use an index when two different data types are involved.  A
489       workaround is to single-quote the constant, thus:
490 <programlisting>
491 ... WHERE smallint_column = '42'
492 </programlisting>
493       This will cause the system to delay type resolution and will
494       assign the right type to the constant.
495      </para>
496     </note>
497
498    </sect2>
499
500    <sect2 id="datatype-numeric-decimal">
501     <title>Arbitrary Precision Numbers</title>
502
503     <para>
504      The type <type>numeric</type> can store numbers with up to 1,000
505      digits of precision and perform calculations exactly. It is
506      especially recommended for storing monetary amounts and other
507      quantities where exactness is required. However, the
508      <type>numeric</type> type is very slow compared to the
509      floating-point types described in the next section.
510     </para>
511
512     <para>
513      In what follows we use these terms:  The
514      <firstterm>scale</firstterm> of a <type>numeric</type> is the
515      count of decimal digits in the fractional part, to the right of
516      the decimal point.  The <firstterm>precision</firstterm> of a
517      <type>numeric</type> is the total count of significant digits in
518      the whole number, that is, the number of digits to both sides of
519      the decimal point.  So the number 23.5141 has a precision of 6
520      and a scale of 4.  Integers can be considered to have a scale of
521      zero.
522     </para>
523
524     <para>
525      Both the precision and the scale of the numeric type can be
526      configured.  To declare a column of type <type>numeric</type> use
527      the syntax
528 <programlisting>
529 NUMERIC(<replaceable>precision</replaceable>, <replaceable>scale</replaceable>)
530 </programlisting>
531      The precision must be positive, the scale zero or positive.
532      Alternatively,
533 <programlisting>
534 NUMERIC(<replaceable>precision</replaceable>)
535 </programlisting>
536      selects a scale of 0.  Specifying
537 <programlisting>
538 NUMERIC
539 </programlisting>
540      without any precision or scale creates a column in which numeric
541      values of any precision and scale can be stored, up to the
542      implementation limit on precision.  A column of this kind will
543      not coerce input values to any particular scale, whereas
544      <type>numeric</type> columns with a declared scale will coerce
545      input values to that scale.  (The <acronym>SQL</acronym> standard
546      requires a default scale of 0, i.e., coercion to integer
547      precision.  We find this a bit useless.  If you're concerned
548      about portability, always specify the precision and scale
549      explicitly.)
550     </para>
551
552     <para>
553      If the precision or scale of a value is greater than the declared
554      precision or scale of a column, the system will attempt to round
555      the value.  If the value cannot be rounded so as to satisfy the
556      declared limits, an error is raised.
557     </para>
558
559     <para>
560      The types <type>decimal</type> and <type>numeric</type> are
561      equivalent.  Both types are part of the <acronym>SQL</acronym>
562      standard.
563     </para>
564    </sect2>
565
566
567    <sect2 id="datatype-float">
568     <title>Floating-Point Types</title>
569
570     <para>
571      The data types <type>real</type> and <type>double
572      precision</type> are inexact, variable-precision numeric types.
573      In practice, these types are usually implementations of
574      <acronym>IEEE</acronym> Standard 754 for Binary Floating-Point
575      Arithmetic (single and double precision, respectively), to the
576      extent that the underlying processor, operating system, and
577      compiler support it.
578     </para>
579
580     <para>
581      Inexact means that some values cannot be converted exactly to the
582      internal format and are stored as approximations, so that storing
583      and printing back out a value may show slight discrepancies.
584      Managing these errors and how they propagate through calculations
585      is the subject of an entire branch of mathematics and computer
586      science and will not be discussed further here, except for the
587      following points:
588      <itemizedlist>
589       <listitem>
590        <para>
591         If you require exact storage and calculations (such as for
592         monetary amounts), use the <type>numeric</type> type instead.
593        </para>
594       </listitem>
595
596       <listitem>
597        <para>
598         If you want to do complicated calculations with these types
599         for anything important, especially if you rely on certain
600         behavior in boundary cases (infinity, underflow), you should
601         evaluate the implementation carefully.
602        </para>
603       </listitem>
604
605       <listitem>
606        <para>
607         Comparing two floating-point values for equality may or may
608         not work as expected.
609        </para>
610       </listitem>
611      </itemizedlist>
612     </para>
613
614     <para>
615      Normally, the <type>real</type> type has a range of at least
616      -1E+37 to +1E+37 with a precision of at least 6 decimal digits.  The
617      <type>double precision</type> type normally has a range of around
618      -1E+308 to +1E+308 with a precision of at least 15 digits.  Values that
619      are too large or too small will cause an error.  Rounding may
620      take place if the precision of an input number is too high.
621      Numbers too close to zero that are not representable as distinct
622      from zero will cause an underflow error.
623     </para>
624
625    </sect2>
626
627    <sect2 id="datatype-serial">
628     <title>The Serial Types</title>
629
630     <indexterm zone="datatype-serial">
631      <primary>serial</primary>
632     </indexterm>
633
634     <indexterm zone="datatype-serial">
635      <primary>bigserial</primary>
636     </indexterm>
637
638     <indexterm zone="datatype-serial">
639      <primary>serial4</primary>
640     </indexterm>
641
642     <indexterm zone="datatype-serial">
643      <primary>serial8</primary>
644     </indexterm>
645
646     <indexterm>
647      <primary>auto-increment</primary>
648      <see>serial</see>
649     </indexterm>
650
651     <indexterm>
652      <primary>sequences</primary>
653      <secondary>and serial type</secondary>
654     </indexterm>
655
656     <para>
657      The <type>serial</type> data type is not a true type, but merely
658      a notational convenience for setting up identifier columns
659      (similar to the <literal>AUTO_INCREMENT</literal> property
660      supported by some other databases). In the current
661      implementation, specifying
662
663 <programlisting>
664 CREATE TABLE <replaceable class="parameter">tablename</replaceable> (
665     <replaceable class="parameter">colname</replaceable> SERIAL
666 );
667 </programlisting>
668
669      is equivalent to specifying:
670
671 <programlisting>
672 CREATE SEQUENCE <replaceable class="parameter">tablename</replaceable>_<replaceable class="parameter">colname</replaceable>_seq;
673 CREATE TABLE <replaceable class="parameter">tablename</replaceable> (
674     <replaceable class="parameter">colname</replaceable> integer DEFAULT nextval('<replaceable class="parameter">tablename</replaceable>_<replaceable class="parameter">colname</replaceable>_seq') NOT NULL
675 );
676 </programlisting>
677
678      Thus, we have created an integer column and arranged for its default
679      values to be assigned from a sequence generator.  A <literal>NOT NULL</>
680      constraint is applied to ensure that a null value cannot be explicitly
681      inserted, either.  In most cases you would also want to attach a
682      <literal>UNIQUE</> or <literal>PRIMARY KEY</> constraint to prevent
683      duplicate values from being inserted by accident, but this is
684      not automatic.
685     </para>
686
687     <para>
688      To use a <type>serial</type> column to insert the next value of
689      the sequence into the table, specify that the <type>serial</type>
690      column should be assigned the default value. This can be done
691      either be excluding from the column from the list of columns in
692      the <command>INSERT</command> statement, or through the use of
693      the <literal>DEFAULT</literal> keyword.
694     </para>
695
696     <para>
697      The type names <type>serial</type> and <type>serial4</type> are
698      equivalent: both create <type>integer</type> columns.  The type
699      names <type>bigserial</type> and <type>serial8</type> work just
700      the same way, except that they create a <type>bigint</type>
701      column.  <type>bigserial</type> should be used if you anticipate
702      the use of more than 2<superscript>31</> identifiers over the
703      lifetime of the table.
704     </para>
705
706     <para>
707      The sequence created by a <type>serial</type> type is
708      automatically dropped when the owning column is dropped, and
709      cannot be dropped otherwise.  (This was not true in
710      <productname>PostgreSQL</productname> releases before 7.3.  Note
711      that this automatic drop linkage will not occur for a sequence
712      created by reloading a dump from a pre-7.3 database; the dump
713      file does not contain the information needed to establish the
714      dependency link.) Furthermore, this dependency between sequence
715      and column is made only for the <type>serial</> column itself; if
716      any other columns reference the sequence (perhaps by manually
717      calling the <function>nextval()</>) function), they may be broken
718      if the sequence is removed. Using <type>serial</> columns in
719      fashion is considered bad form.
720     </para>
721
722     <note>
723      <para>
724       Prior to <productname>PostgreSQL</> 7.3, <type>serial</type>
725       implied <literal>UNIQUE</literal>.  This is no longer automatic.
726       If you wish a serial column to be <literal>UNIQUE</literal> or a
727       <literal>PRIMARY KEY</literal> it must now be specified, just as
728       with any other data type.
729      </para>
730     </note>
731    </sect2>
732   </sect1>
733
734   <sect1 id="datatype-money">
735    <title>Monetary Type</title>
736
737    <note>
738     <title>Note</title>
739     <para>
740      The <type>money</type> type is deprecated. Use
741      <type>numeric</type> or <type>decimal</type> instead, in
742      combination with the <function>to_char</function> function.  The
743      money type may become a locale-aware layer over the
744      <type>numeric</type> type in a future release.
745     </para>
746    </note>
747
748    <para>
749     The <type>money</type> type stores a currency amount with fixed
750     decimal point representation; see <xref
751     linkend="datatype-money-table">.  The output format is
752     locale-specific.
753    </para>
754
755    <para>
756     Input is accepted in a variety of formats, including integer and
757     floating-point literals, as well as <quote>typical</quote>
758     currency formatting, such as <literal>'$1,000.00'</literal>.
759     Output is in the latter form.
760    </para>
761
762     <table id="datatype-money-table">
763      <title>Monetary Types</title>
764      <tgroup cols="4">
765       <thead>
766        <row>
767         <entry>Type Name</entry>
768         <entry>Storage</entry>
769         <entry>Description</entry>
770         <entry>Range</entry>
771        </row>
772       </thead>
773       <tbody>
774        <row>
775         <entry>money</entry>
776         <entry>4 bytes</entry>
777         <entry>currency amount</entry>
778         <entry>-21474836.48 to +21474836.47</entry>
779        </row>
780       </tbody>
781      </tgroup>
782     </table>
783   </sect1>
784
785
786   <sect1 id="datatype-character">
787    <title>Character Types</title>
788
789    <indexterm zone="datatype-character">
790     <primary>character strings</primary>
791     <secondary>data types</secondary>
792    </indexterm>
793
794    <indexterm>
795     <primary>strings</primary>
796     <see>character strings</see>
797    </indexterm>
798
799    <indexterm>
800     <primary>text</primary>
801     <see>character strings</see>
802    </indexterm>
803
804     <table id="datatype-character-table">
805      <title>Character Types</title>
806      <tgroup cols="2">
807       <thead>
808        <row>
809         <entry>Type name</entry>
810         <entry>Description</entry>
811        </row>
812       </thead>
813       <tbody>
814        <row>
815         <entry><type>character(<replaceable>n</>)</type>, <type>char(<replaceable>n</>)</type></entry>
816         <entry>fixed-length, blank padded</entry>
817        </row>
818        <row>
819         <entry><type>character varying(<replaceable>n</>)</type>, <type>varchar(<replaceable>n</>)</type></entry>
820         <entry>variable-length with limit</entry>
821        </row>
822        <row>
823         <entry><type>text</type></entry>
824         <entry>variable unlimited length</entry>
825        </row>
826      </tbody>
827      </tgroup>
828     </table>
829
830    <para>
831     <xref linkend="datatype-character-table"> shows the
832     general-purpose character types available in
833     <productname>PostgreSQL</productname>.
834    </para>
835
836    <para>
837     <acronym>SQL</acronym> defines two primary character types:
838     <type>character(<replaceable>n</>)</type> and <type>character
839     varying(<replaceable>n</>)</type>, where <replaceable>n</> is a
840     positive integer.  Both of these types can store strings up to
841     <replaceable>n</> characters in length.  An attempt to store a
842     longer string into a column of these types will result in an
843     error, unless the excess characters are all spaces, in which case
844     the string will be truncated to the maximum length.  (This
845     somewhat bizarre exception is required by the
846     <acronym>SQL</acronym> standard.)  If the string to be stored is
847     shorter than the declared length, values of type
848     <type>character</type> will be space-padded; values of type
849     <type>character varying</type> will simply store the shorter
850     string.
851    </para>
852
853    <note>
854     <para>
855      If one explicitly casts a value to
856      <type>character(<replaceable>n</>)</type> or <type>character
857      varying(<replaceable>n</>)</type>, then an overlength value will
858      be truncated to <replaceable>n</> characters without raising an
859      error.  (This too is required by the <acronym>SQL</acronym>
860      standard.)
861     </para>
862    </note>
863
864    <note>
865     <para>
866      Prior to <productname>PostgreSQL</> 7.2, strings that were too long were
867      always truncated without raising an error, in either explicit or
868      implicit casting contexts.
869     </para>
870    </note>
871
872    <para>
873     The notations <type>char(<replaceable>n</>)</type> and
874     <type>varchar(<replaceable>n</>)</type> are aliases for
875     <type>character(<replaceable>n</>)</type> and <type>character
876     varying(<replaceable>n</>)</type>,
877     respectively. <type>character</type> without length specifier is
878     equivalent to <type>character(1)</type>; if <type>character
879     varying</type> is used without length specifier, the type accepts
880     strings of any size.  The latter is a <productname>PostgreSQL</> extension.
881    </para>
882
883    <para>
884     In addition, <productname>PostgreSQL</productname> supports the
885     more general <type>text</type> type, which stores strings of any
886     length.  Unlike <type>character varying</type>, <type>text</type>
887     does not require an explicit declared upper limit on the size of
888     the string.  Although the type <type>text</type> is not in the
889     <acronym>SQL</acronym> standard, many other RDBMS packages have it
890     as well.
891    </para>
892
893    <para>
894     The storage requirement for data of these types is 4 bytes plus the
895     actual string, and in case of <type>character</type> plus the
896     padding. Long strings are compressed by the system automatically, so
897     the physical requirement on disk may be less. Long values are also
898     stored in background tables so they don't interfere with rapid
899     access to the shorter column values. In any case, the longest
900     possible character string that can be stored is about 1 GB. (The
901     maximum value that will be allowed for <replaceable>n</> in the data
902     type declaration is less than that. It wouldn't be very useful to
903     change this because with multibyte character encodings the number of
904     characters and bytes can be quite different anyway. If you desire to
905     store long strings with no specific upper limit, use
906     <type>text</type> or <type>character varying</type> without a length
907     specifier, rather than making up an arbitrary length limit.)
908    </para>
909
910    <tip>
911     <para>
912      There are no performance differences between these three types,
913      apart from the increased storage size when using the blank-padded
914      type.
915     </para>
916    </tip>
917
918    <para>
919     Refer to <xref linkend="sql-syntax-strings"> for information about
920     the syntax of string literals, and to <xref linkend="functions">
921     for information about available operators and functions.
922    </para>
923
924    <example>
925     <title>Using the character types</title>
926
927 <programlisting>
928 CREATE TABLE test1 (a character(4));
929 INSERT INTO test1 VALUES ('ok');
930 SELECT a, char_length(a) FROM test1; -- <co id="co.datatype-char">
931 <computeroutput>
932   a   | char_length
933 ------+-------------
934  ok   |           4
935 </computeroutput>
936
937 CREATE TABLE test2 (b varchar(5));
938 INSERT INTO test2 VALUES ('ok');
939 INSERT INTO test2 VALUES ('good      ');
940 INSERT INTO test2 VALUES ('too long');
941 <computeroutput>ERROR:  value too long for type character varying(5)</computeroutput>
942 INSERT INTO test2 VALUES ('too long'::varchar(5)); -- explicit truncation
943 SELECT b, char_length(b) FROM test2;
944 <computeroutput>
945    b   | char_length
946 -------+-------------
947  ok    |           2
948  good  |           5
949  too l |           5
950 </computeroutput>
951 </programlisting>
952     <calloutlist>
953      <callout arearefs="co.datatype-char">
954       <para>
955        The <function>char_length</function> function is discussed in
956        <xref linkend="functions-string">.
957       </para>
958      </callout>
959     </calloutlist>
960    </example>
961
962    <para>
963     There are two other fixed-length character types in
964     <productname>PostgreSQL</productname>, shown in <xref linkend="datatype-character-special-table">.
965     The <type>name</type> type
966     exists <emphasis>only</emphasis> for storage of internal catalog
967     names and is not intended for use by the general user.  Its length
968     is currently defined as 64 bytes (63 usable characters plus terminator)
969     but should be referenced using the constant
970     <symbol>NAMEDATALEN</symbol>.  The length is set at compile time
971     (and is therefore adjustable for special uses); the default
972     maximum length may change in a future release.  The type
973     <type>"char"</type> (note the quotes) is different from
974     <type>char(1)</type> in that it only uses one byte of storage.  It
975     is internally used in the system catalogs as a poor-man's
976     enumeration type.
977    </para>
978
979     <table id="datatype-character-special-table">
980      <title>Specialty Character Types</title>
981      <tgroup cols="3">
982       <thead>
983        <row>
984         <entry>Type Name</entry>
985         <entry>Storage</entry>
986         <entry>Description</entry>
987        </row>
988       </thead>
989       <tbody>
990        <row>
991         <entry><type>"char"</type></entry>
992         <entry>1 byte</entry>
993         <entry>single character internal type</entry>
994        </row>
995        <row>
996         <entry><type>name</type></entry>
997         <entry>64 bytes</entry>
998         <entry>sixty-three character internal type</entry>
999        </row>
1000       </tbody>
1001      </tgroup>
1002     </table>
1003
1004   </sect1>
1005
1006  <sect1 id="datatype-binary">
1007   <title>Binary Strings</title>
1008    <para>
1009     The <type>bytea</type> data type allows storage of binary strings;
1010     see <xref linkend="datatype-binary-table">.
1011    </para>
1012
1013    <table id="datatype-binary-table">
1014     <title>Binary String Types</title>
1015     <tgroup cols="3">
1016      <thead>
1017       <row>
1018        <entry>Type Name</entry>
1019        <entry>Storage</entry>
1020        <entry>Description</entry>
1021       </row>
1022      </thead>
1023      <tbody>
1024       <row>
1025        <entry><type>bytea</type></entry>
1026        <entry>4 bytes plus the actual binary string</entry>
1027        <entry>Variable (not specifically limited)
1028               length binary string</entry>
1029       </row>
1030      </tbody>
1031     </tgroup>
1032    </table>
1033
1034    <para>
1035     A binary string is a sequence of octets (or bytes).  Binary
1036     strings are distinguished from characters strings by two
1037     characteristics: First, binary strings specifically allow storing
1038     octets of zero value and other <quote>non-printable</quote>
1039     octets.  Second, operations on binary strings process the actual
1040     bytes, whereas the encoding and processing of character strings
1041     depends on locale settings.
1042    </para>
1043
1044    <para>
1045     When entering <type>bytea</type> values, octets of certain values
1046     <emphasis>must</emphasis> be escaped (but all octet values
1047     <emphasis>may</emphasis> be escaped) when used as part of a string
1048     literal in an <acronym>SQL</acronym> statement. In general, to
1049     escape an octet, it is converted into the three-digit octal number
1050     equivalent of its decimal octet value, and preceded by two
1051     backslashes. Some octet values have alternate escape sequences, as
1052     shown in <xref linkend="datatype-binary-sqlesc">.
1053    </para>
1054
1055    <table id="datatype-binary-sqlesc">
1056     <title><type>bytea</> Literal Escaped Octets</title>
1057     <tgroup cols="5">
1058      <thead>
1059       <row>
1060        <entry>Decimal Octet Value</entry>
1061        <entry>Description</entry>
1062        <entry>Input Escaped Representation</entry>
1063        <entry>Example</entry>
1064        <entry>Printed Result</entry>
1065       </row>
1066      </thead>
1067
1068      <tbody>
1069       <row>
1070        <entry>0</entry>
1071        <entry>zero octet</entry>
1072        <entry><literal>'\\000'</literal></entry>
1073        <entry><literal>SELECT '\\000'::bytea;</literal></entry>
1074        <entry><literal>\000</literal></entry>
1075       </row>
1076
1077       <row>
1078        <entry>39</entry>
1079        <entry>single quote</entry>
1080        <entry><literal>'\''</literal> or <literal>'\\047'</literal></entry>
1081        <entry><literal>SELECT '\''::bytea;</literal></entry>
1082        <entry><literal>'</literal></entry>
1083       </row>
1084
1085       <row>
1086        <entry>92</entry>
1087        <entry>backslash</entry>
1088        <entry><literal>'\\\\'</literal> or <literal>'\\134'</literal></entry>
1089        <entry><literal>SELECT '\\\\'::bytea;</literal></entry>
1090        <entry><literal>\\</literal></entry>
1091       </row>
1092
1093      </tbody>
1094     </tgroup>
1095    </table>
1096
1097    <para>
1098     Note that the result in each of the examples in <xref linkend="datatype-binary-sqlesc"> was exactly one
1099     octet in length, even though the output representation of the zero
1100     octet and backslash are more than one character. <type>Bytea</type>
1101     output octets are also escaped. In general, each
1102     <quote>non-printable</quote> octet decimal value is converted into
1103     its equivalent three digit octal value, and preceded by one backslash.
1104     Most <quote>printable</quote> octets are represented by their standard
1105     representation in the client character set. The octet with decimal
1106     value 92 (backslash) has a special alternate output representation.
1107     Details are in <xref linkend="datatype-binary-resesc">.
1108    </para>
1109
1110    <table id="datatype-binary-resesc">
1111     <title><type>bytea</> Output Escaped Octets</title>
1112     <tgroup cols="5">
1113      <thead>
1114       <row>
1115        <entry>Decimal Octet Value</entry>
1116        <entry>Description</entry>
1117        <entry>Output Escaped Representation</entry>
1118        <entry>Example</entry>
1119        <entry>Printed Result</entry>
1120       </row>
1121      </thead>
1122
1123      <tbody>
1124
1125       <row>
1126        <entry>92</entry>
1127        <entry>backslash</entry>
1128        <entry><literal>\\</literal></entry>
1129        <entry><literal>SELECT '\\134'::bytea;</literal></entry>
1130        <entry><literal>\\</literal></entry>
1131       </row>
1132
1133       <row>
1134        <entry>0 to 31 and 127 to 255</entry>
1135        <entry><quote>non-printable</quote> octets</entry>
1136        <entry><literal>\### (octal value)</literal></entry>
1137        <entry><literal>SELECT '\\001'::bytea;</literal></entry>
1138        <entry><literal>\001</literal></entry>
1139       </row>
1140
1141       <row>
1142        <entry>32 to 126</entry>
1143        <entry><quote>printable</quote> octets</entry>
1144        <entry>ASCII representation</entry>
1145        <entry><literal>SELECT '\\176'::bytea;</literal></entry>
1146        <entry><literal>~</literal></entry>
1147       </row>
1148
1149      </tbody>
1150     </tgroup>
1151    </table>
1152
1153    <para>
1154     To use the <type>bytea</type> escaped octet notation, string
1155     literals (input strings) must contain two backslashes because they
1156     must pass through two parsers in the <productname>PostgreSQL</>
1157     server. The first backslash is interpreted as an escape character
1158     by the string-literal parser, and therefore is consumed, leaving
1159     the characters that follow.  The remaining backslash is recognized
1160     by the <type>bytea</type> input function as the prefix of a three
1161     digit octal value. For example, a string literal passed to the
1162     backend as <literal>'\\001'</literal> becomes
1163     <literal>'\001'</literal> after passing through the string-literal
1164     parser. The <literal>'\001'</literal> is then sent to the
1165     <type>bytea</type> input function, where it is converted to a
1166     single octet with a decimal value of 1.
1167    </para>
1168
1169    <para>
1170     For a similar reason, a backslash must be input as
1171     <literal>'\\\\'</literal> (or <literal>'\\134'</literal>). The first
1172     and third backslashes are interpreted as escape characters by the
1173     string-literal parser, and therefore are consumed, leaving two
1174     backslashes in the string passed to the <type>bytea</type> input function,
1175     which interprets them as representing a single backslash.
1176     For example, a string literal passed to the
1177     server as <literal>'\\\\'</literal> becomes <literal>'\\'</literal>
1178     after passing through the string-literal parser. The
1179     <literal>'\\'</literal> is then sent to the <type>bytea</type> input
1180     function, where it is converted to a single octet with a decimal
1181     value of 92.
1182    </para>
1183
1184    <para>
1185     A single quote is a bit different in that it must be input as
1186     <literal>'\''</literal> (or <literal>'\\047'</literal>),
1187     <emphasis>not</emphasis> as <literal>'\\''</literal>. This is because,
1188     while the literal parser interprets the single quote as a special
1189     character, and will consume the single backslash, the
1190     <type>bytea</type> input function does <emphasis>not</emphasis>
1191     recognize a single quote as a special octet. Therefore a string
1192     literal passed to the backend as <literal>'\''</literal> becomes
1193     <literal>'''</literal> after passing through the string-literal
1194     parser. The <literal>'''</literal> is then sent to the
1195     <type>bytea</type> input function, where it is retains its single
1196     octet decimal value of 39.
1197    </para>
1198
1199    <para>
1200     Depending on the front end to <productname>PostgreSQL</> you use,
1201     you may have additional work to do in terms of escaping and
1202     unescaping <type>bytea</type> strings. For example, you may also
1203     have to escape line feeds and carriage returns if your interface
1204     automatically translates these. Or you may have to double up on
1205     backslashes if the parser for your language or choice also treats
1206     them as an escape character.
1207    </para>
1208
1209    <para>
1210         The <acronym>SQL</acronym> standard defines a different binary
1211         string type, called <type>BLOB</type> or <type>BINARY LARGE
1212          OBJECT</type>.  The input format is different compared to
1213         <type>bytea</type>, but the provided functions and operators are
1214         mostly the same.
1215    </para>
1216  </sect1>
1217
1218
1219   <sect1 id="datatype-datetime">
1220    <title>Date/Time Types</title>
1221
1222    <para>
1223     <productname>PostgreSQL</productname> supports the full set of
1224     <acronym>SQL</acronym> date and time types, shown in <xref
1225     linkend="datatype-datetime-table">.
1226    </para>
1227
1228     <table id="datatype-datetime-table">
1229      <title>Date/Time Types</title>
1230      <tgroup cols="6">
1231       <thead>
1232        <row>
1233         <entry>Type</entry>
1234         <entry>Description</entry>
1235         <entry>Storage</entry>
1236         <entry>Earliest</entry>
1237         <entry>Latest</entry>
1238         <entry>Resolution</entry>
1239        </row>
1240       </thead>
1241       <tbody>
1242        <row>
1243         <entry><type>timestamp [ (<replaceable>p</replaceable>) ] [ without time zone ]</type></entry>
1244         <entry>both date and time</entry>
1245         <entry>8 bytes</entry>
1246         <entry>4713 BC</entry>
1247         <entry>AD 1465001</entry>
1248         <entry>1 microsecond / 14 digits</entry>
1249        </row>
1250        <row>
1251         <entry><type>timestamp [ (<replaceable>p</replaceable>) ] with time zone</type></entry>
1252         <entry>both date and time</entry>
1253         <entry>8 bytes</entry>
1254         <entry>4713 BC</entry>
1255         <entry>AD 1465001</entry>
1256         <entry>1 microsecond / 14 digits</entry>
1257        </row>
1258        <row>
1259         <entry><type>interval [ (<replaceable>p</replaceable>) ]</type></entry>
1260         <entry>time intervals</entry>
1261         <entry>12 bytes</entry>
1262         <entry>-178000000 years</entry>
1263         <entry>178000000 years</entry>
1264         <entry>1 microsecond</entry>
1265        </row>
1266        <row>
1267         <entry><type>date</type></entry>
1268         <entry>dates only</entry>
1269         <entry>4 bytes</entry>
1270         <entry>4713 BC</entry>
1271         <entry>32767 AD</entry>
1272         <entry>1 day</entry>
1273        </row>
1274        <row>
1275         <entry><type>time [ (<replaceable>p</replaceable>) ] [ without time zone ]</type></entry>
1276         <entry>times of day only</entry>
1277         <entry>8 bytes</entry>
1278         <entry>00:00:00.00</entry>
1279         <entry>23:59:59.99</entry>
1280         <entry>1 microsecond</entry>
1281        </row>
1282        <row>
1283         <entry><type>time [ (<replaceable>p</replaceable>) ] with time zone</type></entry>
1284         <entry>times of day only</entry>
1285         <entry>12 bytes</entry>
1286         <entry>00:00:00.00+12</entry>
1287         <entry>23:59:59.99-12</entry>
1288         <entry>1 microsecond</entry>
1289        </row>
1290       </tbody>
1291      </tgroup>
1292     </table>
1293
1294    <para>
1295     <type>time</type>, <type>timestamp</type>, and
1296     <type>interval</type> accept an optional precision value
1297     <replaceable>p</replaceable> which specifies the number of
1298     fractional digits retained in the seconds field. By default, there
1299     is no explicit bound on precision.  The allowed range of
1300     <replaceable>p</replaceable> is from 0 to 6 for the
1301     <type>timestamp</type> and <type>interval</type> types, 0 to 13
1302     for the <type>time</type> types.
1303    </para>
1304
1305    <note>
1306    <para>
1307     When <type>timestamp</> values are stored as double precision floating-point
1308     numbers (currently the default), the effective limit of precision
1309     may be less than 6, since timestamp values are stored as seconds
1310     since 2000-01-01.  Microsecond precision is achieved for dates within
1311     a few years of 2000-01-01, but the precision degrades for dates further
1312     away.  When timestamps are stored as eight-byte integers (a compile-time
1313     option), microsecond precision is available over the full range of
1314     values.
1315    </para>
1316    </note>
1317
1318    <para>
1319     Time zones, and time-zone conventions, are influenced by
1320     political decisions, not just earth geometry. Time zones around the
1321     world became somewhat standardized during the 1900's,
1322     but continue to be prone to arbitrary changes.
1323     <productname>PostgreSQL</productname> uses your operating
1324     system's underlying features to provide output time-zone
1325     support, and these systems usually contain information for only
1326     the time period 1902 through 2038 (corresponding to the full
1327     range of conventional Unix system time).
1328     <type>timestamp with time zone</type> and <type>time with time
1329      zone</type> will use time zone
1330     information only within that year range, and assume that times
1331     outside that range are in <acronym>UTC</acronym>.
1332    </para>
1333
1334    <para>
1335     The type <type>time with time zone</type> is defined by the SQL
1336     standard, but the definition exhibits properties which lead to
1337     questionable usefulness. In most cases, a combination of
1338     <type>date</type>, <type>time</type>, <type>timestamp without time
1339     zone</type> and <type>timestamp with time zone</type> should
1340     provide a complete range of date/time functionality required by
1341     any application.
1342    </para>
1343
1344    <para>
1345     The types <type>abstime</type>
1346     and <type>reltime</type> are lower precision types which are used internally.
1347     You are discouraged from using these types in new
1348     applications and are encouraged to move any old
1349     ones over when appropriate. Any or all of these internal types
1350     might disappear in a future release.
1351    </para>
1352
1353    <sect2 id="datatype-datetime-input">
1354     <title>Date/Time Input</title>
1355
1356     <para>
1357      Date and time input is accepted in almost any reasonable format, including
1358      <acronym>ISO 8601</acronym>, <acronym>SQL</acronym>-compatible, 
1359      traditional <productname>PostgreSQL</productname>, and others.
1360      For some formats, ordering of month and day in date input can be
1361      ambiguous and there is support for specifying the expected
1362      ordering of these fields.
1363      The command
1364      <literal>SET DateStyle TO 'US'</literal> 
1365      or <literal>SET DateStyle TO 'NonEuropean'</literal>
1366      specifies the variant <quote>month before day</quote>, the command
1367      <literal>SET DateStyle TO 'European'</literal> sets the variant
1368      <quote>day before month</quote>.
1369     </para>
1370
1371     <para>
1372      <productname>PostgreSQL</productname> is more flexible in
1373      handling date/time than the
1374      <acronym>SQL</acronym> standard requires.
1375      See <xref linkend="datetime-appendix">
1376      for the exact parsing rules of date/time input and for the
1377      recognized text fields including months, days of the week, and
1378      time zones. 
1379     </para>
1380
1381     <para>
1382      Remember that any date or time literal input needs to be enclosed
1383      in single quotes, like text strings.  Refer to 
1384      <xref linkend="sql-syntax-constants-generic"> for more
1385      information.
1386      <acronym>SQL</acronym> requires the following syntax
1387 <synopsis>
1388 <replaceable>type</replaceable> [ (<replaceable>p</replaceable>) ] '<replaceable>value</replaceable>'
1389 </synopsis>
1390      where <replaceable>p</replaceable> in the optional precision
1391      specification is an integer corresponding to the
1392      number of fractional digits in the seconds field. Precision can
1393      be specified 
1394      for <type>time</type>, <type>timestamp</type>, and
1395      <type>interval</type> types.
1396     </para>
1397
1398     <sect3>
1399     <title>Dates</title>
1400
1401     <indexterm>
1402      <primary>date</primary>
1403      <secondary>data type</secondary>
1404     </indexterm>
1405  
1406     <para>
1407      <xref linkend="datatype-datetime-date-table"> shows some possible
1408      inputs for the <type>date</type> type.
1409     </para>
1410
1411      <table id="datatype-datetime-date-table">
1412       <title>Date Input</title>
1413       <tgroup cols="2">
1414        <thead>
1415         <row>
1416          <entry>Example</entry>
1417          <entry>Description</entry>
1418         </row>
1419        </thead>
1420        <tbody>
1421         <row>
1422          <entry>January 8, 1999</entry>
1423          <entry>unambiguous</entry>
1424         </row>
1425         <row>
1426          <entry>1999-01-08</entry>
1427          <entry>ISO-8601 format, preferred</entry>
1428         </row>
1429         <row>
1430          <entry>1/8/1999</entry>
1431          <entry>U.S.; read as August 1 in European mode</entry>
1432         </row>
1433         <row>
1434          <entry>8/1/1999</entry>
1435          <entry>European; read as August 1 in U.S. mode</entry>
1436         </row>
1437         <row>
1438          <entry>1/18/1999</entry>
1439          <entry>U.S.; read as January 18 in any mode</entry>
1440         </row>
1441         <row>
1442          <entry>19990108</entry>
1443          <entry>ISO-8601 year, month, day</entry>
1444         </row>
1445         <row>
1446          <entry>990108</entry>
1447          <entry>ISO-8601 year, month, day</entry>
1448         </row>
1449         <row>
1450          <entry>1999.008</entry>
1451          <entry>year and day of year</entry>
1452         </row>
1453         <row>
1454          <entry>99008</entry>
1455          <entry>year and day of year</entry>
1456         </row>
1457         <row>
1458          <entry>J2451187</entry>
1459          <entry>Julian day</entry>
1460         </row>
1461         <row>
1462          <entry>January 8, 99 BC</entry>
1463          <entry>year 99 before the Common Era</entry>
1464         </row>
1465        </tbody>
1466       </tgroup>
1467      </table>
1468     </sect3>
1469
1470     <sect3>
1471      <title>Times</title>
1472
1473      <indexterm>
1474       <primary>time</primary>
1475       <secondary>data type</secondary>
1476      </indexterm>
1477      <indexterm>
1478       <primary>time without time zone</primary>
1479       <secondary>time</secondary>
1480      </indexterm>
1481      <indexterm>
1482       <primary>time with time zone</primary>
1483       <secondary>data type</secondary>
1484      </indexterm>
1485
1486      <para>
1487       The <type>time</type> type can be specified as <type>time</type> or
1488       as <type>time without time zone</type>. The optional precision
1489       <replaceable>p</replaceable> should be between 0 and 13, and
1490       defaults to the precision of the input time literal.
1491      </para>
1492
1493      <para>
1494       <xref linkend="datatype-datetime-time-table"> shows the valid <type>time</type> inputs.
1495      </para>
1496
1497       <table id="datatype-datetime-time-table">
1498        <title>Time Input</title>
1499        <tgroup cols="2">
1500         <thead>
1501          <row>
1502           <entry>Example</entry>
1503           <entry>Description</entry>
1504          </row>
1505         </thead>
1506         <tbody>
1507          <row>
1508           <entry><literal>04:05:06.789</literal></entry>
1509           <entry>ISO 8601</entry>
1510          </row>
1511          <row>
1512           <entry><literal>04:05:06</literal></entry>
1513           <entry>ISO 8601</entry>
1514          </row>
1515          <row>
1516           <entry><literal>04:05</literal></entry>
1517           <entry>ISO 8601</entry>
1518          </row>
1519          <row>
1520           <entry><literal>040506</literal></entry>
1521           <entry>ISO 8601</entry>
1522          </row>
1523          <row>
1524           <entry><literal>04:05 AM</literal></entry>
1525           <entry>same as 04:05; AM does not affect value</entry>
1526          </row>
1527          <row>
1528           <entry><literal>04:05 PM</literal></entry>
1529           <entry>same as 16:05; input hour must be <= 12</entry>
1530          </row>
1531          <row>
1532           <entry><literal>allballs</literal></entry>
1533           <entry>same as 00:00:00</entry>
1534          </row>
1535         </tbody>
1536        </tgroup>
1537       </table>
1538
1539      <para>
1540       The type <type>time with time zone</type> accepts all input also
1541       legal for the <type>time</type> type, appended with a legal time
1542       zone, as shown in <xref
1543       linkend="datatype-datetime-timetz-table">.
1544      </para>
1545
1546       <table id="datatype-datetime-timetz-table">
1547        <title>Time With Time Zone Input</title>
1548        <tgroup cols="2">
1549        <thead>
1550         <row>
1551          <entry>Example</entry>
1552          <entry>Description</entry>
1553         </row>
1554        </thead>
1555        <tbody>
1556         <row>
1557          <entry>04:05:06.789-8</entry>
1558          <entry>ISO 8601</entry>
1559         </row>
1560         <row>
1561          <entry>04:05:06-08:00</entry>
1562          <entry>ISO 8601</entry>
1563         </row>
1564         <row>
1565          <entry>04:05-08:00</entry>
1566          <entry>ISO 8601</entry>
1567         </row>
1568         <row>
1569          <entry>040506-08</entry>
1570          <entry>ISO 8601</entry>
1571         </row>
1572        </tbody>
1573       </tgroup>
1574      </table>
1575
1576      <para>
1577       Refer to <xref linkend="datatype-timezone-table"> for
1578       more examples of time zones.
1579      </para>
1580     </sect3>
1581
1582     <sect3>
1583     <title>Time stamps</title>
1584
1585     <indexterm>
1586      <primary>timestamp</primary>
1587      <secondary>data type</secondary>
1588     </indexterm>
1589
1590     <indexterm>
1591      <primary>timestamp with time zone</primary>
1592      <secondary>data type</secondary>
1593     </indexterm>
1594
1595     <indexterm>
1596      <primary>timestamp without time zone</primary>
1597      <secondary>data type</secondary>
1598     </indexterm>
1599
1600      <para>
1601       The time stamp types are <type>timestamp [
1602       (<replaceable>p</replaceable>) ] without time zone</type> and
1603       <type>timestamp [ (<replaceable>p</replaceable>) ] with time
1604       zone</type>.  Writing just <type>timestamp</type> is equivalent to
1605       <type>timestamp without time zone</type>.
1606      </para>
1607
1608     <note>
1609      <para>
1610       Prior to <productname>PostgreSQL</productname> 7.3, writing just
1611       <type>timestamp</type> was equivalent to <type>timestamp with time
1612       zone</type>.  This was changed for SQL spec compliance.
1613      </para>
1614     </note>
1615
1616      <para>
1617       Valid input for the time stamp types consists of a concatenation
1618       of a date and a time, followed by an optional
1619       <literal>AD</literal> or <literal>BC</literal>, followed by an
1620       optional time zone. (See <xref
1621       linkend="datatype-timezone-table">.)  Thus
1622
1623 <programlisting>
1624 1999-01-08 04:05:06
1625 </programlisting>
1626       and
1627 <programlisting>
1628 1999-01-08 04:05:06 -8:00
1629 </programlisting>
1630
1631       are valid values, which follow the <acronym>ISO</acronym> 8601
1632       standard.  In addition, the wide-spread format
1633
1634 <programlisting>
1635 January 8 04:05:06 1999 PST
1636 </programlisting>
1637       is supported.
1638      </para>
1639
1640      <para>
1641       The optional precision
1642       <replaceable>p</replaceable> should be between 0 and 6, and
1643       defaults to the precision of the input <type>timestamp</type> literal.
1644      </para>
1645
1646      <para>
1647       For <type>timestamp without time zone</type>, any explicit time
1648       zone specified in the input is silently ignored. That is, the
1649       resulting date/time value is derived from the explicit date/time
1650       fields in the input value, and is not adjusted for time zone.
1651      </para>
1652
1653      <para>
1654       For <type>timestamp with time zone</type>, the internally stored
1655       value is always in UTC (GMT).  An input value that has an explicit
1656       time zone specified is converted to UTC using the appropriate offset
1657       for that time zone.  If no time zone is stated in the input string,
1658       then it is assumed to be in the time zone indicated by the system's
1659       <varname>TimeZone</> parameter, and is converted to UTC using the
1660       offset for the <varname>TimeZone</> zone.
1661      </para>
1662
1663      <para>
1664       When a <type>timestamp with time
1665       zone</type> value is output, it is always converted from UTC to the
1666       current <varname>TimeZone</> zone, and displayed as local time in that
1667       zone.  To see the time in another time zone, either change
1668       <varname>TimeZone</> or use the <literal>AT TIME ZONE</> construct
1669       (see <xref linkend="functions-datetime-zoneconvert">).
1670      </para>
1671
1672      <para>
1673       Conversions between <type>timestamp without time zone</type> and
1674       <type>timestamp with time zone</type> normally assume that the
1675       <type>timestamp without time zone</type> value should be taken or given
1676       as <varname>TimeZone</> local time.  A different zone reference can
1677       be specified for the conversion using <literal>AT TIME ZONE</>.
1678      </para>
1679
1680       <table tocentry="1" id="datatype-timezone-table">
1681        <title>Time Zone Input</title>
1682        <tgroup cols="2">
1683         <thead>
1684          <row>
1685           <entry>Time Zone</entry>
1686           <entry>Description</entry>
1687          </row>
1688         </thead>
1689         <tbody>
1690          <row>
1691           <entry>PST</entry>
1692           <entry>Pacific Standard Time</entry>
1693          </row>
1694          <row>
1695           <entry>-8:00</entry>
1696           <entry>ISO-8601 offset for PST</entry>
1697          </row>
1698          <row>
1699           <entry>-800</entry>
1700           <entry>ISO-8601 offset for PST</entry>
1701          </row>
1702          <row>
1703           <entry>-8</entry>
1704           <entry>ISO-8601 offset for PST</entry>
1705          </row>
1706         </tbody>
1707        </tgroup>
1708       </table>
1709     </sect3>
1710
1711     <sect3>
1712      <title>Intervals</title>
1713
1714      <indexterm>
1715       <primary>interval</primary>
1716      </indexterm>
1717
1718       <para>
1719        <type>interval</type> values can be written with the following syntax:
1720
1721 <programlisting>
1722   Quantity Unit [Quantity Unit...] [Direction]
1723 @ Quantity Unit [Quantity Unit...] [Direction]
1724 </programlisting>
1725
1726       where: <literal>Quantity</literal> is a number (possibly signed),
1727       <literal>Unit</literal> is <literal>second</literal>,
1728       <literal>minute</literal>, <literal>hour</literal>, <literal>day</literal>,
1729       <literal>week</literal>, <literal>month</literal>, <literal>year</literal>,
1730       <literal>decade</literal>, <literal>century</literal>, <literal>millennium</literal>,
1731       or abbreviations or plurals of these units;
1732       <literal>Direction</literal> can be <literal>ago</literal> or
1733       empty.  The at sign (<literal>@</>) is optional noise.  The amounts
1734       of different units are implicitly added up with appropriate
1735       sign accounting.
1736      </para>
1737
1738      <para>
1739       Quantities of days, hours, minutes, and seconds can be specified without
1740       explicit unit markings.  For example, <literal>'1 12:59:10'</> is read
1741       the same as <literal>'1 day 12 hours 59 min 10 sec'</>.
1742      </para>
1743
1744      <para>
1745       The optional precision
1746       <replaceable>p</replaceable> should be between 0 and 6, and
1747       defaults to the precision of the input literal.
1748      </para>
1749     </sect3>
1750
1751     <sect3>
1752      <title>Special values</title>
1753
1754      <indexterm>
1755       <primary>time</primary>
1756       <secondary>constants</secondary>
1757      </indexterm>
1758
1759      <indexterm>
1760       <primary>date</primary>
1761       <secondary>constants</secondary>
1762      </indexterm>
1763
1764      <para>
1765       The following <acronym>SQL</acronym>-compatible functions can be
1766       used as date or time
1767       values for the corresponding data type: <literal>CURRENT_DATE</literal>,
1768       <literal>CURRENT_TIME</literal>,
1769       <literal>CURRENT_TIMESTAMP</literal>. The latter two accept an
1770       optional precision specification.  (See also <xref linkend="functions-datetime-current">.)
1771      </para>
1772
1773      <para>
1774       <productname>PostgreSQL</productname> also supports several
1775       special date/time input values for convenience, as shown in <xref
1776       linkend="datatype-datetime-special-table">.  The values
1777       <literal>infinity</literal> and <literal>-infinity</literal>
1778       are specially represented inside the system and will be displayed
1779       the same way; but the others are simply notational shorthands
1780       that will be converted to ordinary date/time values when read.
1781      </para>
1782
1783       <table id="datatype-datetime-special-table">
1784        <title>Special Date/Time Inputs</title>
1785        <tgroup cols="2">
1786         <thead>
1787          <row>
1788           <entry>Input string</entry>
1789           <entry>Description</entry>
1790          </row>
1791         </thead>
1792         <tbody>
1793          <row>
1794           <entry><literal>epoch</literal></entry>
1795           <entry>1970-01-01 00:00:00+00 (Unix system time zero)</entry>
1796          </row>
1797          <row>
1798           <entry><literal>infinity</literal></entry>
1799           <entry>later than all other timestamps (not available for
1800           type <type>date</>)</entry>
1801          </row>
1802          <row>
1803           <entry><literal>-infinity</literal></entry>
1804           <entry>earlier than all other timestamps (not available for
1805           type <type>date</>)</entry>
1806          </row>
1807          <row>
1808           <entry><literal>now</literal></entry>
1809           <entry>current transaction time</entry>
1810          </row>
1811          <row>
1812           <entry><literal>today</literal></entry>
1813           <entry>midnight today</entry>
1814          </row>
1815          <row>
1816           <entry><literal>tomorrow</literal></entry>
1817           <entry>midnight tomorrow</entry>
1818          </row>
1819          <row>
1820           <entry><literal>yesterday</literal></entry>
1821           <entry>midnight yesterday</entry>
1822          </row>
1823          <row>
1824           <entry><literal>zulu</>, <literal>allballs</>, <literal>z</></entry>
1825           <entry>00:00:00.00 GMT</entry>
1826          </row>
1827         </tbody>
1828        </tgroup>
1829       </table>
1830
1831     </sect3>
1832    </sect2>
1833
1834    <sect2 id="datatype-datetime-output">
1835     <title>Date/Time Output</title>
1836
1837     <indexterm>
1838      <primary>date</primary>
1839      <secondary>output format</secondary>
1840      <seealso>Formatting</seealso>
1841     </indexterm>
1842
1843     <indexterm>
1844      <primary>time</primary>
1845      <secondary>output format</secondary>
1846      <seealso>Formatting</seealso>
1847     </indexterm>
1848
1849     <para>
1850      Output formats can be set to one of the four styles ISO 8601,
1851      <acronym>SQL</acronym> (Ingres), traditional PostgreSQL, and
1852      German, using the <command>SET DateStyle</command>.  The default
1853      is the <acronym>ISO</acronym> format.  (The
1854      <acronym>SQL</acronym> standard requires the use of the ISO 8601
1855      format.  The name of the <quote>SQL</quote> output format is a
1856      historical accident.)  <xref
1857      linkend="datatype-datetime-output-table"> shows examples of each
1858      output style.  The output of the <type>date</type> and
1859      <type>time</type> types is of course only the date or time part
1860      in accordance with the given examples.
1861     </para>
1862
1863      <table id="datatype-datetime-output-table">
1864       <title>Date/Time Output Styles</title>
1865       <tgroup cols="3">
1866        <thead>
1867         <row>
1868          <entry>Style Specification</entry>
1869          <entry>Description</entry>
1870          <entry>Example</entry>
1871         </row>
1872        </thead>
1873        <tbody>
1874         <row>
1875          <entry>ISO</entry>
1876          <entry>ISO 8601/SQL standard</entry>
1877          <entry>1997-12-17 07:37:16-08</entry>
1878         </row>
1879         <row>
1880          <entry>SQL</entry>
1881          <entry>traditional style</entry>
1882          <entry>12/17/1997 07:37:16.00 PST</entry>
1883         </row>
1884         <row>
1885          <entry>PostgreSQL</entry>
1886          <entry>original style</entry>
1887          <entry>Wed Dec 17 07:37:16 1997 PST</entry>
1888         </row>
1889         <row>
1890          <entry>German</entry>
1891          <entry>regional style</entry>
1892          <entry>17.12.1997 07:37:16.00 PST</entry>
1893         </row>
1894        </tbody>
1895       </tgroup>
1896      </table>
1897
1898     <para>
1899      The <acronym>SQL</acronym> style has European and non-European
1900      (U.S.) variants, which determines whether month follows day or
1901      vice versa. (See <xref linkend="datatype-datetime-input">
1902      for how this setting also affects interpretation of input values.)
1903      <xref linkend="datatype-datetime-output2-table"> shows an
1904      example.
1905     </para>
1906
1907      <table id="datatype-datetime-output2-table">
1908       <title>Date Order Conventions</title>
1909       <tgroup cols="3">
1910        <thead>
1911         <row>
1912          <entry>Style Specification</entry>
1913          <entry>Description</entry>
1914          <entry>Example</entry>
1915         </row>
1916        </thead>
1917        <tbody>
1918         <row>
1919          <entry>European</entry>
1920          <entry><replaceable>day</replaceable>/<replaceable>month</replaceable>/<replaceable>year</replaceable></entry>
1921          <entry>17/12/1997 15:37:16.00 MET</entry>
1922         </row>
1923         <row>
1924          <entry>US</entry>
1925          <entry><replaceable>month</replaceable>/<replaceable>day</replaceable>/<replaceable>year</replaceable></entry>
1926          <entry>12/17/1997 07:37:16.00 PST</entry>
1927         </row>
1928        </tbody>
1929       </tgroup>
1930      </table>
1931
1932     <para>
1933     <type>interval</type> output looks like the input format, except that units like
1934     <literal>week</literal> or <literal>century</literal> are converted to years and days.
1935     In ISO mode the output looks like
1936
1937 <programlisting>
1938 [ Quantity Units [ ... ] ] [ Days ] Hours:Minutes [ ago ]
1939 </programlisting>
1940     </para>
1941
1942     <para>
1943      The date/time styles can be selected by the user using the
1944      <command>SET DATESTYLE</command> command, the
1945      <varname>datestyle</varname> parameter in the
1946      <filename>postgresql.conf</filename> configuration file, and the
1947      <envar>PGDATESTYLE</envar> environment variable on the server or
1948      client.  The formatting function <function>to_char</function>
1949      (see <xref linkend="functions-formatting">) is also available as
1950      a more flexible way to format the date/time output.
1951     </para>
1952    </sect2>
1953
1954    <sect2 id="datatype-timezones">
1955     <title>Time Zones</title>
1956
1957     <indexterm zone="datatype-timezones">
1958      <primary>time zones</primary>
1959     </indexterm>
1960
1961     <para>
1962      <productname>PostgreSQL</productname> endeavors to be compatible with
1963      the <acronym>SQL</acronym> standard definitions for typical usage.
1964      However, the <acronym>SQL</acronym> standard has an odd mix of date and
1965      time types and capabilities. Two obvious problems are:
1966
1967      <itemizedlist>
1968       <listitem>
1969        <para>
1970         Although the <type>date</type> type 
1971         does not have an associated time zone, the
1972         <type>time</type> type can.
1973         Time zones in the real world can have no meaning unless 
1974         associated with a date as well as a time
1975         since the offset may vary through the year with daylight-saving
1976         time boundaries.
1977        </para>
1978       </listitem>
1979
1980       <listitem>
1981        <para>
1982         The default time zone is specified as a constant integer offset 
1983         from <acronym>GMT</>/<acronym>UTC</>. It is not possible to adapt to daylight-saving
1984         time when doing date/time arithmetic across
1985         <acronym>DST</acronym> boundaries.
1986        </para>
1987       </listitem>
1988
1989      </itemizedlist>
1990     </para>
1991
1992     <para>
1993      To address these difficulties, we recommend using date/time types
1994      that contain both date and time when using time zones. We
1995      recommend <emphasis>not</emphasis> using the type <type>time with
1996      time zone</type> (though it is supported by
1997      <productname>PostgreSQL</productname> for legacy applications and
1998      for compatibility with other <acronym>SQL</acronym>
1999      implementations).  <productname>PostgreSQL</productname> assumes
2000      your local time zone for any type containing only date or
2001      time. Further, time zone support is derived from the underlying
2002      operating system time-zone capabilities, and hence can handle
2003      daylight-saving time and other expected behavior.
2004     </para>
2005
2006     <para>
2007      <productname>PostgreSQL</productname> obtains time-zone support
2008      from the underlying operating system for dates between 1902 and
2009      2038 (near the typical date limits for Unix-style
2010      systems). Outside of this range, all dates are assumed to be
2011      specified and used in Universal Coordinated Time
2012      (<acronym>UTC</acronym>).
2013     </para>
2014
2015     <para>
2016      All dates and times are stored internally in
2017      <acronym>UTC</acronym>, traditionally known as Greenwich Mean
2018      Time (<acronym>GMT</acronym>).  Times are converted to local time
2019      on the database server before being sent to the client frontend,
2020      hence by default are in the server time zone.
2021     </para>
2022
2023     <para>
2024      There are several ways to select the time zone used by the server:
2025
2026      <itemizedlist>
2027       <listitem>
2028        <para>
2029         The <envar>TZ</envar> environment variable on the server host
2030         is used by the server as the default time zone, if no other is
2031         specified.
2032        </para>
2033       </listitem>
2034
2035       <listitem>
2036        <para>
2037         The <varname>timezone</varname> configuration parameter can be
2038         set in <filename>postgresql.conf</>.
2039        </para>
2040       </listitem>
2041
2042       <listitem>
2043        <para>
2044         The <envar>PGTZ</envar> environment variable, if set at the
2045         client, is used by <application>libpq</application>
2046         applications to send a <command>SET TIME ZONE</command>
2047         command to the server upon connection.
2048        </para>
2049       </listitem>
2050
2051       <listitem>
2052        <para>
2053         The <acronym>SQL</acronym> command <command>SET TIME ZONE</command>
2054         sets the time zone for the session.
2055        </para>
2056       </listitem>
2057      </itemizedlist>
2058     </para>
2059
2060      <note>
2061       <para>
2062        If an invalid time zone is specified, the time zone becomes
2063        <acronym>UTC</acronym> (on most systems anyway).
2064       </para>
2065      </note>
2066
2067    <para>
2068     Refer to <xref linkend="datetime-appendix"> for a list of
2069     available time zones.
2070    </para>
2071
2072    </sect2>
2073
2074    <sect2 id="datatype-datetime-internals">
2075     <title>Internals</title>
2076
2077     <para>
2078      <productname>PostgreSQL</productname> uses Julian dates
2079      for all date/time calculations. They have the nice property of correctly
2080      predicting/calculating any date more recent than 4713 BC
2081      to far into the future, using the assumption that the length of the
2082      year is 365.2425 days.
2083     </para>
2084
2085     <para>
2086      Date conventions before the 19th century make for interesting reading,
2087      but are not consistent enough to warrant coding into a date/time handler.
2088     </para>
2089    </sect2>
2090
2091   </sect1>
2092
2093   <sect1 id="datatype-boolean">
2094    <title>Boolean Type</title>
2095
2096    <indexterm zone="datatype-boolean">
2097     <primary>Boolean</primary>
2098     <secondary>data type</secondary>
2099    </indexterm>
2100
2101    <indexterm zone="datatype-boolean">
2102     <primary>true</primary>
2103    </indexterm>
2104
2105    <indexterm zone="datatype-boolean">
2106     <primary>false</primary>
2107    </indexterm>
2108
2109    <para>
2110     <productname>PostgreSQL</productname> provides the
2111     standard <acronym>SQL</acronym> type <type>boolean</type>.
2112     <type>boolean</type> can have one of only two states:
2113     <quote>true</quote> or <quote>false</quote>.  A third state,
2114     <quote>unknown</quote>, is represented by the
2115     <acronym>SQL</acronym> null value.
2116    </para>
2117
2118    <para>
2119     Valid literal values for the <quote>true</quote> state are:
2120     <simplelist>
2121      <member><literal>TRUE</literal></member>
2122      <member><literal>'t'</literal></member>
2123      <member><literal>'true'</literal></member>
2124      <member><literal>'y'</literal></member>
2125      <member><literal>'yes'</literal></member>
2126      <member><literal>'1'</literal></member>
2127     </simplelist>
2128     For the <quote>false</quote> state, the following values can be
2129     used:
2130     <simplelist>
2131      <member><literal>FALSE</literal></member>
2132      <member><literal>'f'</literal></member>
2133      <member><literal>'false'</literal></member>
2134      <member><literal>'n'</literal></member>
2135      <member><literal>'no'</literal></member>
2136      <member><literal>'0'</literal></member>
2137     </simplelist>
2138     Using the key words <literal>TRUE</literal> and
2139     <literal>FALSE</literal> is preferred (and
2140     <acronym>SQL</acronym>-compliant).
2141    </para>
2142
2143    <example id="datatype-boolean-example">
2144     <title>Using the <type>boolean</type> type</title>
2145
2146 <programlisting>
2147 CREATE TABLE test1 (a boolean, b text);
2148 INSERT INTO test1 VALUES (TRUE, 'sic est');
2149 INSERT INTO test1 VALUES (FALSE, 'non est');
2150 SELECT * FROM test1;
2151  a |    b
2152 ---+---------
2153  t | sic est
2154  f | non est
2155
2156 SELECT * FROM test1 WHERE a;
2157  a |    b
2158 ---+---------
2159  t | sic est
2160 </programlisting>
2161    </example>
2162
2163    <para>
2164     <xref linkend="datatype-boolean-example"> shows that
2165     <type>boolean</type> values are output using the letters
2166     <literal>t</literal> and <literal>f</literal>.
2167    </para>
2168
2169    <tip>
2170     <para>
2171      Values of the <type>boolean</type> type cannot be cast directly
2172      to other types (e.g., <literal>CAST
2173      (<replaceable>boolval</replaceable> AS integer)</literal> does
2174      not work).  This can be accomplished using the
2175      <literal>CASE</literal> expression: <literal>CASE WHEN
2176      <replaceable>boolval</replaceable> THEN 'value if true' ELSE
2177      'value if false' END</literal>.  See also <xref
2178      linkend="functions-conditional">.
2179     </para>
2180    </tip>
2181
2182    <para>
2183     <type>boolean</type> uses 1 byte of storage.
2184    </para>
2185   </sect1>
2186
2187   <sect1 id="datatype-geometric">
2188    <title>Geometric Types</title>
2189
2190    <para>
2191     Geometric data types represent two-dimensional spatial
2192     objects. <xref linkend="datatype-geo-table"> shows the geometric
2193     types available in <productname>PostgreSQL</productname>.  The
2194     most fundamental type, the point, forms the basis for all of the
2195     other types.
2196    </para>
2197
2198     <table id="datatype-geo-table">
2199      <title>Geometric Types</title>
2200      <tgroup cols="4">
2201       <thead>
2202        <row>
2203         <entry>Geometric Type</entry>
2204         <entry>Storage</entry>
2205         <entry>Representation</entry>
2206         <entry>Description</entry>
2207        </row>
2208       </thead>
2209       <tbody>
2210        <row>
2211         <entry><type>point</type></entry>
2212         <entry>16 bytes</entry>
2213         <entry>(x,y)</entry>
2214         <entry>Point in space</entry>
2215        </row>
2216        <row>
2217         <entry><type>line</type></entry>
2218         <entry>32 bytes</entry>
2219         <entry>((x1,y1),(x2,y2))</entry>
2220         <entry>Infinite line (not fully implemented)</entry>
2221        </row>
2222        <row>
2223         <entry><type>lseg</type></entry>
2224         <entry>32 bytes</entry>
2225         <entry>((x1,y1),(x2,y2))</entry>
2226         <entry>Finite line segment</entry>
2227        </row>
2228        <row>
2229         <entry><type>box</type></entry>
2230         <entry>32 bytes</entry>
2231         <entry>((x1,y1),(x2,y2))</entry>
2232         <entry>Rectangular box</entry>
2233        </row>
2234        <row>
2235         <entry><type>path</type></entry>
2236         <entry>16+16n bytes</entry>
2237         <entry>((x1,y1),...)</entry>
2238         <entry>Closed path (similar to polygon)</entry>
2239        </row>
2240        <row>
2241         <entry><type>path</type></entry>
2242         <entry>16+16n bytes</entry>
2243         <entry>[(x1,y1),...]</entry>
2244         <entry>Open path</entry>
2245        </row>
2246        <row>
2247         <entry><type>polygon</type></entry>
2248         <entry>40+16n bytes</entry>
2249         <entry>((x1,y1),...)</entry>
2250         <entry>Polygon (similar to closed path)</entry>
2251        </row>
2252        <row>
2253         <entry><type>circle</type></entry>
2254         <entry>24 bytes</entry>
2255         <entry><(x,y),r></entry>
2256         <entry>Circle (center and radius)</entry>
2257        </row>
2258       </tbody>
2259      </tgroup>
2260     </table>
2261
2262    <para>
2263     A rich set of functions and operators is available to perform various geometric
2264     operations such as scaling, translation, rotation, and determining 
2265     intersections.  They are explained in <xref linkend="functions-geometry">.
2266    </para>
2267
2268    <sect2>
2269     <title>Point</title>
2270
2271     <indexterm>
2272      <primary>point</primary>
2273     </indexterm>
2274
2275     <para>
2276      Points are the fundamental two-dimensional building block for geometric types.
2277      <type>point</type> is specified using the following syntax:
2278
2279 <synopsis>
2280 ( <replaceable>x</replaceable> , <replaceable>y</replaceable> )
2281   <replaceable>x</replaceable> , <replaceable>y</replaceable>
2282 </synopsis>
2283
2284      where the arguments are
2285
2286      <variablelist>
2287       <varlistentry>
2288        <term><replaceable>x</replaceable></term>
2289        <listitem>
2290         <para>
2291          the x-axis coordinate as a floating-point number
2292         </para>
2293        </listitem>
2294       </varlistentry>
2295
2296       <varlistentry>
2297        <term><replaceable>y</replaceable></term>
2298        <listitem>
2299         <para>
2300          the y-axis coordinate as a floating-point number
2301         </para>
2302        </listitem>
2303       </varlistentry>
2304      </variablelist>
2305     </para>
2306    </sect2>
2307
2308    <sect2>
2309     <title>Line Segment</title>
2310
2311     <indexterm>
2312      <primary>line</primary>
2313     </indexterm>
2314
2315     <para>
2316      Line segments (<type>lseg</type>) are represented by pairs of points.
2317      <type>lseg</type> is specified using the following syntax:
2318
2319 <synopsis>
2320 ( ( <replaceable>x1</replaceable> , <replaceable>y1</replaceable> ) , ( <replaceable>x2</replaceable> , <replaceable>y2</replaceable> ) )
2321   ( <replaceable>x1</replaceable> , <replaceable>y1</replaceable> ) , ( <replaceable>x2</replaceable> , <replaceable>y2</replaceable> )  
2322     <replaceable>x1</replaceable> , <replaceable>y1</replaceable>   ,   <replaceable>x2</replaceable> , <replaceable>y2</replaceable>
2323 </synopsis>
2324
2325      where the arguments are
2326
2327      <variablelist>
2328       <varlistentry>
2329        <term>(<replaceable>x1</replaceable>,<replaceable>y1</replaceable>)</term>
2330        <term>(<replaceable>x2</replaceable>,<replaceable>y2</replaceable>)</term>
2331        <listitem>
2332         <para>
2333          the end points of the line segment
2334         </para>
2335        </listitem>
2336       </varlistentry>
2337      </variablelist>
2338     </para>
2339    </sect2>
2340
2341    <sect2>
2342     <title>Box</title>
2343
2344     <indexterm>
2345      <primary>box (data type)</primary>
2346     </indexterm>
2347
2348     <para>
2349      Boxes are represented by pairs of points that are opposite
2350      corners of the box.
2351      <type>box</type> is specified using the following syntax:
2352
2353 <synopsis>
2354 ( ( <replaceable>x1</replaceable> , <replaceable>y1</replaceable> ) , ( <replaceable>x2</replaceable> , <replaceable>y2</replaceable> ) )
2355   ( <replaceable>x1</replaceable> , <replaceable>y1</replaceable> ) , ( <replaceable>x2</replaceable> , <replaceable>y2</replaceable> )  
2356     <replaceable>x1</replaceable> , <replaceable>y1</replaceable>   ,   <replaceable>x2</replaceable> , <replaceable>y2</replaceable>
2357 </synopsis>
2358
2359      where the arguments are
2360
2361      <variablelist>
2362       <varlistentry>
2363        <term>(<replaceable>x1</replaceable>,<replaceable>y1</replaceable>)</term>
2364        <term>(<replaceable>x2</replaceable>,<replaceable>y2</replaceable>)</term>
2365        <listitem>
2366         <para>
2367          opposite corners of the box
2368         </para>
2369        </listitem>
2370       </varlistentry>
2371      </variablelist>
2372     </para>
2373
2374     <para>
2375      Boxes are output using the first syntax.
2376      The corners are reordered on input to store
2377      the upper right corner, then the lower left corner.
2378      Other corners of the box can be entered, but the lower
2379      left and upper right corners are determined from the input and stored corners.
2380     </para>
2381    </sect2>
2382
2383    <sect2>
2384     <title>Path</title>
2385
2386     <indexterm>
2387      <primary>path (data type)</primary>
2388     </indexterm>
2389
2390     <para>
2391      Paths are represented by connected sets of points. Paths can be
2392      <firstterm>open</firstterm>, where
2393      the first and last points in the set are not connected, and <firstterm>closed</firstterm>,
2394      where the first and last point are connected. Functions
2395      <function>popen(p)</function>
2396      and
2397      <function>pclose(p)</function>
2398      are supplied to force a path to be open or closed, and functions
2399      <function>isopen(p)</function>
2400      and
2401      <function>isclosed(p)</function>
2402      are supplied to test for either type in a query.
2403     </para>
2404
2405     <para>
2406      <type>path</type> is specified using the following syntax:
2407
2408 <synopsis>
2409 ( ( <replaceable>x1</replaceable> , <replaceable>y1</replaceable> ) , ... , ( <replaceable>xn</replaceable> , <replaceable>yn</replaceable> ) )
2410 [ ( <replaceable>x1</replaceable> , <replaceable>y1</replaceable> ) , ... , ( <replaceable>xn</replaceable> , <replaceable>yn</replaceable> ) ]
2411   ( <replaceable>x1</replaceable> , <replaceable>y1</replaceable> ) , ... , ( <replaceable>xn</replaceable> , <replaceable>yn</replaceable> )  
2412   ( <replaceable>x1</replaceable> , <replaceable>y1</replaceable>   , ... ,   <replaceable>xn</replaceable> , <replaceable>yn</replaceable> )  
2413     <replaceable>x1</replaceable> , <replaceable>y1</replaceable>   , ... ,   <replaceable>xn</replaceable> , <replaceable>yn</replaceable>    
2414 </synopsis>
2415
2416      where the arguments are
2417
2418      <variablelist>
2419       <varlistentry>
2420        <term>(<replaceable>x</replaceable>,<replaceable>y</replaceable>)</term>
2421        <listitem>
2422         <para>
2423          End points of the line segments comprising the path.
2424          A leading square bracket (<literal>[</>) indicates an open path, while
2425          a leading parenthesis (<literal>(</>) indicates a closed path.
2426         </para>
2427        </listitem>
2428       </varlistentry>
2429      </variablelist>
2430     </para>
2431
2432     <para>
2433      Paths are output using the first syntax.
2434     </para>
2435    </sect2>
2436
2437    <sect2>
2438     <title>Polygon</title>
2439
2440     <indexterm>
2441      <primary>polygon</primary>
2442     </indexterm>
2443
2444     <para>
2445      Polygons are represented by sets of points. Polygons should probably be
2446      considered equivalent to closed paths, but are stored differently 
2447      and have their own set of support routines.
2448     </para>
2449
2450     <para>
2451      <type>polygon</type> is specified using the following syntax:
2452
2453 <synopsis>
2454 ( ( <replaceable>x1</replaceable> , <replaceable>y1</replaceable> ) , ... , ( <replaceable>xn</replaceable> , <replaceable>yn</replaceable> ) )
2455   ( <replaceable>x1</replaceable> , <replaceable>y1</replaceable> ) , ... , ( <replaceable>xn</replaceable> , <replaceable>yn</replaceable> )  
2456   ( <replaceable>x1</replaceable> , <replaceable>y1</replaceable>   , ... ,   <replaceable>xn</replaceable> , <replaceable>yn</replaceable> )  
2457     <replaceable>x1</replaceable> , <replaceable>y1</replaceable>   , ... ,   <replaceable>xn</replaceable> , <replaceable>yn</replaceable>    
2458 </synopsis>
2459
2460      where the arguments are
2461
2462      <variablelist>
2463       <varlistentry>
2464        <term>(<replaceable>x</replaceable>,<replaceable>y</replaceable>)</term>
2465        <listitem>
2466         <para>
2467          End points of the line segments comprising the boundary of the
2468          polygon
2469         </para>
2470        </listitem>
2471       </varlistentry>
2472      </variablelist>
2473     </para>
2474
2475     <para>
2476      Polygons are output using the first syntax.
2477     </para>
2478    </sect2>
2479
2480    <sect2>
2481     <title>Circle</title>
2482
2483     <indexterm>
2484      <primary>circle</primary>
2485     </indexterm>
2486
2487     <para>
2488      Circles are represented by a center point and a radius.
2489      <type>circle</type> is specified using the following syntax:
2490
2491 <synopsis>
2492 &lt; ( <replaceable>x</replaceable> , <replaceable>y</replaceable> ) , <replaceable>r</replaceable> &gt;
2493 ( ( <replaceable>x</replaceable> , <replaceable>y</replaceable> ) , <replaceable>r</replaceable> )
2494   ( <replaceable>x</replaceable> , <replaceable>y</replaceable> ) , <replaceable>r</replaceable>  
2495     <replaceable>x</replaceable> , <replaceable>y</replaceable>   , <replaceable>r</replaceable>  
2496 </synopsis>
2497
2498      where the arguments are
2499
2500      <variablelist>
2501       <varlistentry>
2502        <term>(<replaceable>x</replaceable>,<replaceable>y</replaceable>)</term>
2503        <listitem>
2504         <para>
2505          center of the circle
2506         </para>
2507        </listitem>
2508       </varlistentry>
2509
2510       <varlistentry>
2511        <term><replaceable>r</replaceable></term>
2512        <listitem>
2513         <para>
2514          radius of the circle
2515         </para>
2516        </listitem>
2517       </varlistentry>
2518      </variablelist>
2519     </para>
2520
2521     <para>
2522      Circles are output using the first syntax.
2523     </para>
2524    </sect2>
2525
2526   </sect1>
2527
2528   <sect1 id="datatype-net-types">
2529    <title>Network Address Data Types</title>
2530
2531    <indexterm zone="datatype-net-types">
2532     <primary>network</primary>
2533     <secondary>addresses</secondary>
2534    </indexterm>
2535
2536    <para>
2537     <productname>PostgreSQL</> offers data types to store IP and MAC
2538     addresses, shown in <xref linkend="datatype-net-types-table">.  It
2539     is preferable to use these types over plain text types, because
2540     these types offer input error checking and several specialized
2541     operators and functions.
2542    </para>
2543
2544     <table tocentry="1" id="datatype-net-types-table">
2545      <title>Network Address Data Types</title>
2546      <tgroup cols="4">
2547       <thead>
2548        <row>
2549         <entry>Name</entry>
2550         <entry>Storage</entry>
2551         <entry>Description</entry>
2552         <entry>Range</entry>
2553        </row>
2554       </thead>
2555       <tbody>
2556
2557        <row>
2558         <entry><type>cidr</type></entry>
2559         <entry>12 bytes</entry>
2560         <entry>IP networks</entry>
2561         <entry>valid IPv4 networks</entry>
2562        </row>
2563
2564        <row>
2565         <entry><type>inet</type></entry>
2566         <entry>12 bytes</entry>
2567         <entry>IP hosts and networks</entry>
2568         <entry>valid IPv4 hosts or networks</entry>
2569        </row>
2570
2571        <row>
2572         <entry><type>macaddr</type></entry>
2573         <entry>6 bytes</entry>
2574         <entry>MAC addresses</entry>
2575         <entry>customary formats</entry>
2576        </row>
2577
2578       </tbody>
2579      </tgroup>
2580     </table>
2581
2582    <para>
2583     IPv6 is not yet supported.
2584    </para>
2585
2586
2587    <sect2 id="datatype-inet">
2588     <title><type>inet</type></title>
2589
2590     <indexterm>
2591      <primary>inet (data type)</primary>
2592     </indexterm>
2593
2594     <para>
2595      The <type>inet</type> type holds an IP host address, and
2596      optionally the identity of the subnet it is in, all in one field.
2597      The subnet identity is represented by the number of bits in the
2598      network part of the address (the <quote>netmask</quote>).  If the
2599      netmask is 32, 
2600      then the value does not indicate a subnet, only a single host.
2601      Note that if you want to accept networks only, you should use the
2602      <type>cidr</type> type rather than <type>inet</type>.
2603     </para>
2604
2605     <para>
2606      The input format for this type is <replaceable
2607      class="parameter">x.x.x.x/y</replaceable> where <replaceable
2608      class="parameter">x.x.x.x</replaceable> is an IP address and
2609      <replaceable class="parameter">y</replaceable> is the number of
2610      bits in the netmask.  If the <replaceable
2611      class="parameter">/y</replaceable> part is left off, then the
2612      netmask is 32, and the value represents just a single host.
2613      On display, the <replaceable class="parameter">/y</replaceable>
2614      portion is suppressed if the netmask is 32.
2615     </para>
2616    </sect2>
2617
2618    <sect2 id="datatype-cidr">
2619     <title><type>cidr</></title>
2620
2621     <indexterm>
2622      <primary>cidr</primary>
2623     </indexterm>
2624
2625     <para>
2626      The <type>cidr</type> type holds an IP network specification.
2627      Input and output formats follow Classless Internet Domain Routing
2628      conventions.
2629      The format for
2630      specifying classless networks is <replaceable
2631      class="parameter">x.x.x.x/y</> where <replaceable
2632      class="parameter">x.x.x.x</> is the network and <replaceable
2633      class="parameter">y</> is the number of bits in the netmask.  If
2634      <replaceable class="parameter">y</> is omitted, it is calculated
2635      using assumptions from the older classful numbering system, except
2636      that it will be at least large enough to include all of the octets
2637      written in the input.
2638     </para>
2639
2640     <para>
2641      <xref linkend="datatype-net-cidr-table"> shows some examples.
2642     </para>
2643
2644      <table id="datatype-net-cidr-table">
2645       <title><type>cidr</> Type Input Examples</title>
2646       <tgroup cols="3">
2647        <thead> 
2648         <row> 
2649          <entry><type>CIDR</type> Input</entry>
2650          <entry><type>CIDR</type> Displayed</entry>
2651          <entry><function>abbrev</function>(<type>CIDR</type>)</entry>
2652         </row>
2653        </thead>
2654        <tbody>
2655         <row>
2656          <entry>192.168.100.128/25</entry>
2657          <entry>192.168.100.128/25</entry>
2658          <entry>192.168.100.128/25</entry>
2659         </row>
2660         <row>
2661          <entry>192.168/24</entry>
2662          <entry>192.168.0.0/24</entry>
2663          <entry>192.168.0/24</entry>
2664         </row>
2665         <row>
2666          <entry>192.168/25</entry>
2667          <entry>192.168.0.0/25</entry>
2668          <entry>192.168.0.0/25</entry>
2669         </row>
2670         <row>
2671          <entry>192.168.1</entry>
2672          <entry>192.168.1.0/24</entry>
2673          <entry>192.168.1/24</entry>
2674         </row>
2675         <row>
2676          <entry>192.168</entry>
2677          <entry>192.168.0.0/24</entry>
2678          <entry>192.168.0/24</entry>
2679         </row>
2680         <row>
2681          <entry>128.1</entry>
2682          <entry>128.1.0.0/16</entry>
2683          <entry>128.1/16</entry>
2684         </row>
2685         <row>
2686          <entry>128</entry>
2687          <entry>128.0.0.0/16</entry>
2688          <entry>128.0/16</entry>
2689         </row>
2690         <row>
2691          <entry>128.1.2</entry>
2692          <entry>128.1.2.0/24</entry>
2693          <entry>128.1.2/24</entry>
2694         </row>
2695         <row>
2696          <entry>10.1.2</entry>
2697          <entry>10.1.2.0/24</entry>
2698          <entry>10.1.2/24</entry>
2699         </row>
2700         <row>
2701          <entry>10.1</entry>
2702          <entry>10.1.0.0/16</entry>
2703          <entry>10.1/16</entry>
2704         </row>
2705         <row>
2706          <entry>10</entry>
2707          <entry>10.0.0.0/8</entry>
2708          <entry>10/8</entry>
2709         </row>
2710        </tbody>
2711       </tgroup>
2712      </table>
2713    </sect2>
2714
2715    <sect2 id="datatype-inet-vs-cidr">
2716     <title><type>inet</type> vs <type>cidr</type></title>
2717
2718     <para>
2719     The essential difference between <type>inet</type> and <type>cidr</type>
2720     data types is that <type>inet</type> accepts values with nonzero bits to
2721     the right of the netmask, whereas <type>cidr</type> does not.
2722
2723       <tip>
2724         <para>
2725         If you do not like the output format for <type>inet</type> or
2726         <type>cidr</type> values, try the <function>host</>(),
2727         <function>text</>(), and <function>abbrev</>() functions.
2728         </para>
2729       </tip>
2730     </para>
2731    </sect2>
2732
2733    <sect2 id="datatype-macaddr">
2734     <title><type>macaddr</></>
2735
2736     <indexterm>
2737      <primary>macaddr (data type)</primary>
2738     </indexterm>
2739
2740     <indexterm>
2741      <primary>MAC address</primary>
2742      <see>macaddr</see>
2743     </indexterm>
2744
2745     <para>
2746      The <type>macaddr</> type stores MAC addresses, i.e., Ethernet
2747      card hardware addresses (although MAC addresses are used for
2748      other purposes as well).  Input is accepted in various customary
2749      formats, including
2750
2751      <simplelist>
2752       <member><literal>'08002b:010203'</></member>
2753       <member><literal>'08002b-010203'</></member>
2754       <member><literal>'0800.2b01.0203'</></member>
2755       <member><literal>'08-00-2b-01-02-03'</></member>
2756       <member><literal>'08:00:2b:01:02:03'</></member>
2757      </simplelist>
2758
2759      which would all specify the same
2760      address.  Upper and lower case is accepted for the digits
2761      <literal>a</> through <literal>f</>.  Output is always in the
2762      last of the shown forms.
2763     </para>
2764
2765     <para>
2766      The directory <filename class="directory">contrib/mac</filename>
2767      in the <productname>PostgreSQL</productname> source distribution
2768      contains tools that can be used to map MAC addresses to hardware
2769      manufacturer names.
2770     </para>
2771    </sect2>
2772
2773   </sect1>
2774
2775   <sect1 id="datatype-bit">
2776    <title>Bit String Types</title>
2777
2778    <indexterm zone="datatype-bit">
2779     <primary>bit strings</primary>
2780     <secondary>data type</secondary>
2781    </indexterm>
2782
2783    <para>
2784     Bit strings are strings of 1's and 0's.  They can be used to store
2785     or visualize bit masks.  There are two SQL bit types:
2786     <type>BIT(<replaceable>n</replaceable>)</type> and <type>BIT
2787     VARYING(<replaceable>n</replaceable>)</type>, where
2788     <replaceable>n</replaceable> is a positive integer.
2789    </para>
2790
2791    <para>
2792     <type>BIT</type> type data must match the length
2793     <replaceable>n</replaceable> exactly; it is an error to attempt to
2794     store shorter or longer bit strings.  <type>BIT VARYING</type> data is
2795     of variable length up to the maximum length
2796     <replaceable>n</replaceable>; longer strings will be rejected.
2797     Writing <type>BIT</type> without a length is equivalent to
2798     <literal>BIT(1)</literal>, while <type>BIT VARYING</type> without a length
2799     specification means unlimited length.
2800    </para>
2801
2802    <note>
2803     <para>
2804      If one explicitly casts a bit-string value to
2805      <type>BIT(<replaceable>n</>)</type>, it will be truncated or
2806      zero-padded on the right to be exactly <replaceable>n</> bits,
2807      without raising an error.  Similarly,
2808      if one explicitly casts a bit-string value to
2809      <type>BIT VARYING(<replaceable>n</>)</type>, it will be truncated
2810      on the right if it is more than <replaceable>n</> bits.
2811     </para>
2812    </note>
2813
2814    <note>
2815     <para>
2816      Prior to <productname>PostgreSQL</> 7.2, <type>BIT</type> data
2817      was always silently truncated or zero-padded on the right, with
2818      or without an explicit cast. This was changed to comply with the
2819      <acronym>SQL</acronym> standard.
2820     </para>
2821    </note>
2822
2823    <para>
2824     Refer to <xref
2825     linkend="sql-syntax-bit-strings"> for information about the syntax
2826     of bit string constants.  Bit-logical operators and string
2827     manipulation functions are available; see <xref
2828     linkend="functions">.
2829    </para>
2830
2831    <example>
2832     <title>Using the bit string types</title>
2833
2834 <programlisting>
2835 CREATE TABLE test (a BIT(3), b BIT VARYING(5));
2836 INSERT INTO test VALUES (B'101', B'00');
2837 INSERT INTO test VALUES (B'10', B'101');
2838 <computeroutput>
2839 ERROR:  Bit string length 2 does not match type BIT(3)
2840 </computeroutput>
2841 INSERT INTO test VALUES (B'10'::bit(3), B'101');
2842 SELECT * FROM test;
2843 <computeroutput>
2844   a  |  b
2845 -----+-----
2846  101 | 00
2847  100 | 101
2848 </computeroutput>
2849 </programlisting>
2850    </example>
2851
2852   </sect1>
2853
2854   <sect1 id="datatype-oid">
2855    <title>Object Identifier Types</title>
2856
2857    <indexterm zone="datatype-oid">
2858     <primary>object identifier</primary>
2859     <secondary>data type</secondary>
2860    </indexterm>
2861
2862    <indexterm zone="datatype-oid">
2863     <primary>oid</primary>
2864    </indexterm>
2865
2866    <indexterm zone="datatype-oid">
2867     <primary>regproc</primary>
2868    </indexterm>
2869
2870    <indexterm zone="datatype-oid">
2871     <primary>regprocedure</primary>
2872    </indexterm>
2873
2874    <indexterm zone="datatype-oid">
2875     <primary>regoper</primary>
2876    </indexterm>
2877
2878    <indexterm zone="datatype-oid">
2879     <primary>regoperator</primary>
2880    </indexterm>
2881
2882    <indexterm zone="datatype-oid">
2883     <primary>regclass</primary>
2884    </indexterm>
2885
2886    <indexterm zone="datatype-oid">
2887     <primary>regtype</primary>
2888    </indexterm>
2889
2890    <indexterm zone="datatype-oid">
2891     <primary>xid</primary>
2892    </indexterm>
2893
2894    <indexterm zone="datatype-oid">
2895     <primary>cid</primary>
2896    </indexterm>
2897
2898    <indexterm zone="datatype-oid">
2899     <primary>tid</primary>
2900    </indexterm>
2901
2902    <para>
2903     Object identifiers (OIDs) are used internally by
2904     <productname>PostgreSQL</productname> as primary keys for various system
2905     tables.  Also, an OID system column is added to user-created tables
2906     (unless <literal>WITHOUT OIDS</> is specified at table creation time).
2907     Type <type>oid</> represents an object identifier.  There are also
2908     several aliases for <type>oid</>: <type>regproc</>, <type>regprocedure</>,
2909     <type>regoper</>, <type>regoperator</>, <type>regclass</>,
2910     and <type>regtype</>. <xref linkend="datatype-oid-table"> shows an overview.
2911    </para>
2912
2913    <para>
2914     The <type>oid</> type is currently implemented as an unsigned four-byte
2915     integer.
2916     Therefore, it is not large enough to provide database-wide uniqueness
2917     in large databases, or even in large individual tables.  So, using a
2918     user-created table's OID column as a primary key is discouraged.
2919     OIDs are best used only for references to system tables.
2920    </para>
2921
2922    <para>
2923     The <type>oid</> type itself has few operations beyond comparison
2924     (which is implemented as unsigned comparison).  It can be cast to
2925     integer, however, and then manipulated using the standard integer
2926     operators.  (Beware of possible signed-versus-unsigned confusion
2927     if you do this.)
2928    </para>
2929
2930    <para>
2931     The  <type>oid</> alias types have no operations of their own except
2932     for specialized input and output routines.  These routines are able
2933     to accept and display symbolic names for system objects, rather than
2934     the raw numeric value that type <type>oid</> would use.  The alias
2935     types allow simplified lookup of OID values for objects: for example,
2936     one may write <literal>'mytable'::regclass</> to get the OID of table
2937     <literal>mytable</>, rather than <literal>SELECT oid FROM pg_class WHERE
2938     relname = 'mytable'</>.  (In reality, a much more complicated <command>SELECT</> would
2939     be needed to deal with selecting the right OID when there are multiple
2940     tables named <literal>mytable</> in different schemas.)
2941    </para>
2942
2943     <table id="datatype-oid-table">
2944      <title>Object Identifier Types</title>
2945      <tgroup cols="4">
2946       <thead>
2947        <row>
2948         <entry>Type name</entry>
2949         <entry>References</entry>
2950         <entry>Description</entry>
2951         <entry>Value example</entry>
2952        </row>
2953       </thead>
2954
2955       <tbody>
2956
2957        <row>
2958         <entry><type>oid</></entry>
2959         <entry>any</entry>
2960         <entry>numeric object identifier</entry>
2961         <entry><literal>564182</></entry>
2962        </row>
2963
2964        <row>
2965         <entry><type>regproc</></entry>
2966         <entry><structname>pg_proc</></entry>
2967         <entry>function name</entry>
2968         <entry><literal>sum</></entry>
2969        </row>
2970
2971        <row>
2972         <entry><type>regprocedure</></entry>
2973         <entry><structname>pg_proc</></entry>
2974         <entry>function with argument types</entry>
2975         <entry><literal>sum(int4)</></entry>
2976        </row>
2977
2978        <row>
2979         <entry><type>regoper</></entry>
2980         <entry><structname>pg_operator</></entry>
2981         <entry>operator name</entry>
2982         <entry><literal>+</></entry>
2983        </row>
2984
2985        <row>
2986         <entry><type>regoperator</></entry>
2987         <entry><structname>pg_operator</></entry>
2988         <entry>operator with argument types</entry>
2989         <entry><literal>*(integer,integer)</> or <literal>-(NONE,integer)</></entry>
2990        </row>
2991
2992        <row>
2993         <entry><type>regclass</></entry>
2994         <entry><structname>pg_class</></entry>
2995         <entry>relation name</entry>
2996         <entry><literal>pg_type</></entry>
2997        </row>
2998
2999        <row>
3000         <entry><type>regtype</></entry>
3001         <entry><structname>pg_type</></entry>
3002         <entry>type name</entry>
3003         <entry><literal>integer</></entry>
3004        </row>
3005       </tbody>
3006      </tgroup>
3007     </table>
3008
3009    <para>
3010     All of the OID alias types accept schema-qualified names, and will
3011     display schema-qualified names on output if the object would not
3012     be found in the current search path without being qualified.
3013     The <type>regproc</> and <type>regoper</> alias types will only
3014     accept input names that are unique (not overloaded), so they are
3015     of limited use; for most uses <type>regprocedure</> or
3016     <type>regoperator</> is more appropriate.  For <type>regoperator</>,
3017     unary operators are identified by writing <literal>NONE</> for the unused
3018     operand.
3019    </para>
3020
3021    <para>
3022     OIDs are 32-bit quantities and are assigned from a single cluster-wide
3023     counter.  In a large or long-lived database, it is possible for the
3024     counter to wrap around.  Hence, it is bad practice to assume that OIDs
3025     are unique, unless you take steps to ensure that they are unique.
3026     Recommended practice when using OIDs for row identification is to create
3027     a unique constraint on the OID column of each table for which the OID will
3028     be used.  Never assume that OIDs are unique across tables; use the
3029     combination of <structfield>tableoid</> and row OID if you need a
3030     database-wide identifier.  (Future releases of
3031     <productname>PostgreSQL</productname> are likely to use a separate
3032     OID counter for each table, so that <structfield>tableoid</>
3033     <emphasis>must</> be included to arrive at a globally unique identifier.)
3034    </para>
3035
3036    <para>
3037     Another identifier type used by the system is <type>xid</>, or transaction
3038     (abbreviated <abbrev>xact</>) identifier.  This is the data type of the system columns
3039     <structfield>xmin</> and <structfield>xmax</>.
3040     Transaction identifiers are 32-bit quantities.  In a long-lived
3041     database it is possible for transaction IDs to wrap around.  This
3042     is not a fatal problem given appropriate maintenance procedures;
3043     see the &cite-admin; for details.  However, it is
3044     unwise to depend on uniqueness of transaction IDs over the long term
3045     (more than one billion transactions).
3046    </para>
3047
3048    <para>
3049     A third identifier type used by the system is <type>cid</>, or
3050     command identifier.  This is the data type of the system columns
3051     <structfield>cmin</> and <structfield>cmax</>.  Command
3052     identifiers are also 32-bit quantities.  This creates a hard limit
3053     of 2<superscript>32</> (4 billion) <acronym>SQL</acronym> commands
3054     within a single transaction.  In practice this limit is not a
3055     problem --- note that the limit is on number of
3056     <acronym>SQL</acronym> commands, not number of tuples processed.
3057    </para>
3058
3059    <para>
3060     A final identifier type used by the system is <type>tid</>, or tuple
3061     identifier.  This is the data type of the system column
3062     <structfield>ctid</>.  A tuple ID is a pair
3063     (block number, tuple index within block) that identifies the
3064     physical location of the tuple within its table.
3065    </para>
3066
3067   </sect1>
3068
3069   <sect1 id="datatype-pseudo">
3070    <title>Pseudo-Types</title>
3071
3072    <indexterm zone="datatype-pseudo">
3073     <primary>record</primary>
3074    </indexterm>
3075
3076    <indexterm zone="datatype-pseudo">
3077     <primary>any</primary>
3078    </indexterm>
3079
3080    <indexterm zone="datatype-pseudo">
3081     <primary>anyarray</primary>
3082    </indexterm>
3083
3084    <indexterm zone="datatype-pseudo">
3085     <primary>void</primary>
3086    </indexterm>
3087
3088    <indexterm zone="datatype-pseudo">
3089     <primary>trigger</primary>
3090    </indexterm>
3091
3092    <indexterm zone="datatype-pseudo">
3093     <primary>language_handler</primary>
3094    </indexterm>
3095
3096    <indexterm zone="datatype-pseudo">
3097     <primary>cstring</primary>
3098    </indexterm>
3099
3100    <indexterm zone="datatype-pseudo">
3101     <primary>internal</primary>
3102    </indexterm>
3103
3104    <indexterm zone="datatype-pseudo">
3105     <primary>opaque</primary>
3106    </indexterm>
3107
3108    <para>
3109     The <productname>PostgreSQL</productname> type system contains a
3110     number of special-purpose entries that are collectively called
3111     <firstterm>pseudo-types</>.  A pseudo-type cannot be used as a
3112     column data type, but it can be used to declare a function's
3113     argument or result type.  Each of the available pseudo-types is
3114     useful in situations where a function's behavior does not
3115     correspond to simply taking or returning a value of a specific
3116     <acronym>SQL</acronym> data type.  <xref
3117     linkend="datatype-pseudotypes-table"> lists the existing
3118     pseudo-types.
3119    </para>
3120
3121     <table id="datatype-pseudotypes-table">
3122      <title>Pseudo-Types</title>
3123      <tgroup cols="2">
3124       <thead>
3125        <row>
3126         <entry>Type name</entry>
3127         <entry>Description</entry>
3128        </row>
3129       </thead>
3130
3131       <tbody>
3132
3133        <row>
3134         <entry><type>record</></entry>
3135         <entry>Identifies a function returning an unspecified row type</entry>
3136        </row>
3137
3138        <row>
3139         <entry><type>any</></entry>
3140         <entry>Indicates that a function accepts any input data type whatever</entry>
3141        </row>
3142
3143        <row>
3144         <entry><type>anyarray</></entry>
3145         <entry>Indicates that a function accepts any array data type</entry>
3146        </row>
3147
3148        <row>
3149         <entry><type>void</></entry>
3150         <entry>Indicates that a function returns no value</entry>
3151        </row>
3152
3153        <row>
3154         <entry><type>trigger</></entry>
3155         <entry>A trigger function is declared to return <type>trigger</></entry>
3156        </row>
3157
3158        <row>
3159         <entry><type>language_handler</></entry>
3160         <entry>A procedural language call handler is declared to return <type>language_handler</></entry>
3161        </row>
3162
3163        <row>
3164         <entry><type>cstring</></entry>
3165         <entry>Indicates that a function accepts or returns a null-terminated C string</entry>
3166        </row>
3167
3168        <row>
3169         <entry><type>internal</></entry>
3170         <entry>Indicates that a function accepts or returns a server-internal
3171         data type</entry>
3172        </row>
3173
3174        <row>
3175         <entry><type>opaque</></entry>
3176         <entry>An obsolete type name that formerly served all the above purposes</entry>
3177        </row>
3178       </tbody>
3179      </tgroup>
3180     </table>
3181
3182    <para>
3183     Functions coded in C (whether built-in or dynamically loaded) may be
3184     declared to accept or return any of these pseudo data types.  It is up to
3185     the function author to ensure that the function will behave safely
3186     when a pseudo-type is used as an argument type.
3187    </para>
3188
3189    <para>
3190     Functions coded in procedural languages may use pseudo-types only as
3191     allowed by their implementation languages.  At present the procedural
3192     languages all forbid use of a pseudo-type as argument type, and allow
3193     only <type>void</> as a result type (plus <type>trigger</> when the
3194     function is used as a trigger).
3195    </para>
3196
3197    <para>
3198     The <type>internal</> pseudo-type is used to declare functions
3199     that are meant only to be called internally by the database
3200     system, and not by direct invocation in a <acronym>SQL</acronym>
3201     query.  If a function has at least one <type>internal</>-type
3202     argument then it cannot be called from <acronym>SQL</acronym>.  To
3203     preserve the type safety of this restriction it is important to
3204     follow this coding rule: do not create any function that is
3205     declared to return <type>internal</> unless it has at least one
3206     <type>internal</> argument.
3207    </para>
3208
3209   </sect1>
3210
3211   &array;
3212
3213  </chapter>
3214
3215 <!-- Keep this comment at the end of the file
3216 Local variables:
3217 mode:sgml
3218 sgml-omittag:nil
3219 sgml-shorttag:t
3220 sgml-minimize-attributes:nil
3221 sgml-always-quote-attributes:t
3222 sgml-indent-step:1
3223 sgml-indent-tabs-mode:nil
3224 sgml-indent-data:t
3225 sgml-parent-document:nil
3226 sgml-default-dtd-file:"./reference.ced"
3227 sgml-exposed-tags:nil
3228 sgml-local-catalogs:("/usr/share/sgml/catalog")
3229 sgml-local-ecat-files:nil
3230 End:
3231 -->