1 <!-- $PostgreSQL: pgsql/doc/src/sgml/datatype.sgml,v 1.218 2007/11/27 05:49:58 momjian Exp $ -->
3 <chapter id="datatype">
4 <title id="datatype-title">Data Types</title>
6 <indexterm zone="datatype">
7 <primary>data type</primary>
11 <primary>type</primary>
16 <productname>PostgreSQL</productname> has a rich set of native data
17 types available to users. Users can add new types to
18 <productname>PostgreSQL</productname> using the <xref
19 linkend="sql-createtype" endterm="sql-createtype-title"> command.
23 <xref linkend="datatype-table"> shows all the built-in general-purpose data
24 types. Most of the alternative names listed in the
25 <quote>Aliases</quote> column are the names used internally by
26 <productname>PostgreSQL</productname> for historical reasons. In
27 addition, some internally used or deprecated types are available,
28 but they are not listed here.
31 <table id="datatype-table">
32 <title>Data Types</title>
37 <entry>Aliases</entry>
38 <entry>Description</entry>
44 <entry><type>bigint</type></entry>
45 <entry><type>int8</type></entry>
46 <entry>signed eight-byte integer</entry>
50 <entry><type>bigserial</type></entry>
51 <entry><type>serial8</type></entry>
52 <entry>autoincrementing eight-byte integer</entry>
56 <entry><type>bit [ (<replaceable>n</replaceable>) ]</type></entry>
58 <entry>fixed-length bit string</entry>
62 <entry><type>bit varying [ (<replaceable>n</replaceable>) ]</type></entry>
63 <entry><type>varbit</type></entry>
64 <entry>variable-length bit string</entry>
68 <entry><type>boolean</type></entry>
69 <entry><type>bool</type></entry>
70 <entry>logical Boolean (true/false)</entry>
74 <entry><type>box</type></entry>
76 <entry>rectangular box in the plane</entry>
80 <entry><type>bytea</type></entry>
82 <entry>binary data (<quote>byte array</>)</entry>
86 <entry><type>character varying [ (<replaceable>n</replaceable>) ]</type></entry>
87 <entry><type>varchar [ (<replaceable>n</replaceable>) ]</type></entry>
88 <entry>variable-length character string</entry>
92 <entry><type>character [ (<replaceable>n</replaceable>) ]</type></entry>
93 <entry><type>char [ (<replaceable>n</replaceable>) ]</type></entry>
94 <entry>fixed-length character string</entry>
98 <entry><type>cidr</type></entry>
100 <entry>IPv4 or IPv6 network address</entry>
104 <entry><type>circle</type></entry>
106 <entry>circle in the plane</entry>
110 <entry><type>date</type></entry>
112 <entry>calendar date (year, month, day)</entry>
116 <entry><type>double precision</type></entry>
117 <entry><type>float8</type></entry>
118 <entry>double precision floating-point number</entry>
122 <entry><type>inet</type></entry>
124 <entry>IPv4 or IPv6 host address</entry>
128 <entry><type>integer</type></entry>
129 <entry><type>int</type>, <type>int4</type></entry>
130 <entry>signed four-byte integer</entry>
134 <entry><type>interval [ (<replaceable>p</replaceable>) ]</type></entry>
136 <entry>time span</entry>
140 <entry><type>line</type></entry>
142 <entry>infinite line in the plane</entry>
146 <entry><type>lseg</type></entry>
148 <entry>line segment in the plane</entry>
152 <entry><type>macaddr</type></entry>
154 <entry>MAC address</entry>
158 <entry><type>money</type></entry>
160 <entry>currency amount</entry>
164 <entry><type>numeric [ (<replaceable>p</replaceable>,
165 <replaceable>s</replaceable>) ]</type></entry>
166 <entry><type>decimal [ (<replaceable>p</replaceable>,
167 <replaceable>s</replaceable>) ]</type></entry>
168 <entry>exact numeric of selectable precision</entry>
172 <entry><type>path</type></entry>
174 <entry>geometric path in the plane</entry>
178 <entry><type>point</type></entry>
180 <entry>geometric point in the plane</entry>
184 <entry><type>polygon</type></entry>
186 <entry>closed geometric path in the plane</entry>
190 <entry><type>real</type></entry>
191 <entry><type>float4</type></entry>
192 <entry>single precision floating-point number</entry>
196 <entry><type>smallint</type></entry>
197 <entry><type>int2</type></entry>
198 <entry>signed two-byte integer</entry>
202 <entry><type>serial</type></entry>
203 <entry><type>serial4</type></entry>
204 <entry>autoincrementing four-byte integer</entry>
208 <entry><type>text</type></entry>
210 <entry>variable-length character string</entry>
214 <entry><type>time [ (<replaceable>p</replaceable>) ] [ without time zone ]</type></entry>
216 <entry>time of day</entry>
220 <entry><type>time [ (<replaceable>p</replaceable>) ] with time zone</type></entry>
221 <entry><type>timetz</type></entry>
222 <entry>time of day, including time zone</entry>
226 <entry><type>timestamp [ (<replaceable>p</replaceable>) ] [ without time zone ]</type></entry>
228 <entry>date and time</entry>
232 <entry><type>timestamp [ (<replaceable>p</replaceable>) ] with time zone</type></entry>
233 <entry><type>timestamptz</type></entry>
234 <entry>date and time, including time zone</entry>
238 <entry><type>tsquery</type></entry>
240 <entry>text search query</entry>
244 <entry><type>tsvector</type></entry>
246 <entry>text search document</entry>
250 <entry><type>txid_snapshot</type></entry>
252 <entry>user-level transaction ID snapshot</entry>
256 <entry><type>uuid</type></entry>
258 <entry>universally unique identifier</entry>
262 <entry><type>xml</type></entry>
264 <entry>XML data</entry>
271 <title>Compatibility</title>
273 The following types (or spellings thereof) are specified by
274 <acronym>SQL</acronym>: <type>bigint</type>, <type>bit</type>, <type>bit
275 varying</type>, <type>boolean</type>, <type>char</type>,
276 <type>character varying</type>, <type>character</type>,
277 <type>varchar</type>, <type>date</type>, <type>double
278 precision</type>, <type>integer</type>, <type>interval</type>,
279 <type>numeric</type>, <type>decimal</type>, <type>real</type>,
280 <type>smallint</type>, <type>time</type> (with or without time zone),
281 <type>timestamp</type> (with or without time zone),
287 Each data type has an external representation determined by its input
288 and output functions. Many of the built-in types have
289 obvious external formats. However, several types are either unique
290 to <productname>PostgreSQL</productname>, such as geometric
291 paths, or have several possibilities for formats, such as the date
293 Some of the input and output functions are not invertible. That is,
294 the result of an output function might lose accuracy when compared to
298 <sect1 id="datatype-numeric">
299 <title>Numeric Types</title>
301 <indexterm zone="datatype-numeric">
302 <primary>data type</primary>
303 <secondary>numeric</secondary>
307 Numeric types consist of two-, four-, and eight-byte integers,
308 four- and eight-byte floating-point numbers, and selectable-precision
309 decimals. <xref linkend="datatype-numeric-table"> lists the
313 <table id="datatype-numeric-table">
314 <title>Numeric Types</title>
319 <entry>Storage Size</entry>
320 <entry>Description</entry>
327 <entry><type>smallint</></entry>
328 <entry>2 bytes</entry>
329 <entry>small-range integer</entry>
330 <entry>-32768 to +32767</entry>
333 <entry><type>integer</></entry>
334 <entry>4 bytes</entry>
335 <entry>usual choice for integer</entry>
336 <entry>-2147483648 to +2147483647</entry>
339 <entry><type>bigint</></entry>
340 <entry>8 bytes</entry>
341 <entry>large-range integer</entry>
342 <entry>-9223372036854775808 to 9223372036854775807</entry>
346 <entry><type>decimal</></entry>
347 <entry>variable</entry>
348 <entry>user-specified precision, exact</entry>
349 <entry>no limit</entry>
352 <entry><type>numeric</></entry>
353 <entry>variable</entry>
354 <entry>user-specified precision, exact</entry>
355 <entry>no limit</entry>
359 <entry><type>real</></entry>
360 <entry>4 bytes</entry>
361 <entry>variable-precision, inexact</entry>
362 <entry>6 decimal digits precision</entry>
365 <entry><type>double precision</></entry>
366 <entry>8 bytes</entry>
367 <entry>variable-precision, inexact</entry>
368 <entry>15 decimal digits precision</entry>
372 <entry><type>serial</></entry>
373 <entry>4 bytes</entry>
374 <entry>autoincrementing integer</entry>
375 <entry>1 to 2147483647</entry>
379 <entry><type>bigserial</type></entry>
380 <entry>8 bytes</entry>
381 <entry>large autoincrementing integer</entry>
382 <entry>1 to 9223372036854775807</entry>
389 The syntax of constants for the numeric types is described in
390 <xref linkend="sql-syntax-constants">. The numeric types have a
391 full set of corresponding arithmetic operators and
392 functions. Refer to <xref linkend="functions"> for more
393 information. The following sections describe the types in detail.
396 <sect2 id="datatype-int">
397 <title>Integer Types</title>
399 <indexterm zone="datatype-int">
400 <primary>integer</primary>
403 <indexterm zone="datatype-int">
404 <primary>smallint</primary>
407 <indexterm zone="datatype-int">
408 <primary>bigint</primary>
412 <primary>int4</primary>
417 <primary>int2</primary>
422 <primary>int8</primary>
427 The types <type>smallint</type>, <type>integer</type>, and
428 <type>bigint</type> store whole numbers, that is, numbers without
429 fractional components, of various ranges. Attempts to store
430 values outside of the allowed range will result in an error.
434 The type <type>integer</type> is the usual choice, as it offers
435 the best balance between range, storage size, and performance.
436 The <type>smallint</type> type is generally only used if disk
437 space is at a premium. The <type>bigint</type> type should only
438 be used if the <type>integer</type> range is not sufficient,
439 because the latter is definitely faster.
443 The <type>bigint</type> type might not function correctly on all
444 platforms, since it relies on compiler support for eight-byte
445 integers. On a machine without such support, <type>bigint</type>
446 acts the same as <type>integer</type> (but still takes up eight
447 bytes of storage). However, we are not aware of any reasonable
448 platform where this is actually the case.
452 <acronym>SQL</acronym> only specifies the integer types
453 <type>integer</type> (or <type>int</type>),
454 <type>smallint</type>, and <type>bigint</type>. The
455 type names <type>int2</type>, <type>int4</type>, and
456 <type>int8</type> are extensions, which are shared with various
457 other <acronym>SQL</acronym> database systems.
462 <sect2 id="datatype-numeric-decimal">
463 <title>Arbitrary Precision Numbers</title>
466 <primary>numeric (data type)</primary>
470 <primary>arbitrary precision numbers</primary>
474 <primary>decimal</primary>
479 The type <type>numeric</type> can store numbers with up to 1000
480 digits of precision and perform calculations exactly. It is
481 especially recommended for storing monetary amounts and other
482 quantities where exactness is required. However, arithmetic on
483 <type>numeric</type> values is very slow compared to the integer
484 types, or to the floating-point types described in the next section.
488 In what follows we use these terms: The
489 <firstterm>scale</firstterm> of a <type>numeric</type> is the
490 count of decimal digits in the fractional part, to the right of
491 the decimal point. The <firstterm>precision</firstterm> of a
492 <type>numeric</type> is the total count of significant digits in
493 the whole number, that is, the number of digits to both sides of
494 the decimal point. So the number 23.5141 has a precision of 6
495 and a scale of 4. Integers can be considered to have a scale of
500 Both the maximum precision and the maximum scale of a
501 <type>numeric</type> column can be
502 configured. To declare a column of type <type>numeric</type> use
505 NUMERIC(<replaceable>precision</replaceable>, <replaceable>scale</replaceable>)
507 The precision must be positive, the scale zero or positive.
510 NUMERIC(<replaceable>precision</replaceable>)
512 selects a scale of 0. Specifying:
516 without any precision or scale creates a column in which numeric
517 values of any precision and scale can be stored, up to the
518 implementation limit on precision. A column of this kind will
519 not coerce input values to any particular scale, whereas
520 <type>numeric</type> columns with a declared scale will coerce
521 input values to that scale. (The <acronym>SQL</acronym> standard
522 requires a default scale of 0, i.e., coercion to integer
523 precision. We find this a bit useless. If you're concerned
524 about portability, always specify the precision and scale
529 If the scale of a value to be stored is greater than the declared
530 scale of the column, the system will round the value to the specified
531 number of fractional digits. Then, if the number of digits to the
532 left of the decimal point exceeds the declared precision minus the
533 declared scale, an error is raised.
537 Numeric values are physically stored without any extra leading or
538 trailing zeroes. Thus, the declared precision and scale of a column
539 are maximums, not fixed allocations. (In this sense the <type>numeric</>
540 type is more akin to <type>varchar(<replaceable>n</>)</type>
541 than to <type>char(<replaceable>n</>)</type>.) The actual storage
542 requirement is two bytes for each group of four decimal digits,
543 plus five to eight bytes overhead.
547 <primary>NaN</primary>
548 <see>not a number</see>
552 <primary>not a number</primary>
553 <secondary>numeric (data type)</secondary>
557 In addition to ordinary numeric values, the <type>numeric</type>
558 type allows the special value <literal>NaN</>, meaning
559 <quote>not-a-number</quote>. Any operation on <literal>NaN</>
560 yields another <literal>NaN</>. When writing this value
561 as a constant in a SQL command, you must put quotes around it,
562 for example <literal>UPDATE table SET x = 'NaN'</>. On input,
563 the string <literal>NaN</> is recognized in a case-insensitive manner.
568 In most implementations of the <quote>not-a-number</> concept,
569 <literal>NaN</> is not considered equal to any other numeric
570 value (including <literal>NaN</>). In order to allow
571 <type>numeric</> values to be sorted and used in tree-based
572 indexes, <productname>PostgreSQL</> treats <literal>NaN</>
573 values as equal, and greater than all non-<literal>NaN</>
579 The types <type>decimal</type> and <type>numeric</type> are
580 equivalent. Both types are part of the <acronym>SQL</acronym>
586 <sect2 id="datatype-float">
587 <title>Floating-Point Types</title>
589 <indexterm zone="datatype-float">
590 <primary>real</primary>
593 <indexterm zone="datatype-float">
594 <primary>double precision</primary>
598 <primary>float4</primary>
603 <primary>float8</primary>
604 <see>double precision</see>
607 <indexterm zone="datatype-float">
608 <primary>floating point</primary>
612 The data types <type>real</type> and <type>double
613 precision</type> are inexact, variable-precision numeric types.
614 In practice, these types are usually implementations of
615 <acronym>IEEE</acronym> Standard 754 for Binary Floating-Point
616 Arithmetic (single and double precision, respectively), to the
617 extent that the underlying processor, operating system, and
622 Inexact means that some values cannot be converted exactly to the
623 internal format and are stored as approximations, so that storing
624 and printing back out a value might show slight discrepancies.
625 Managing these errors and how they propagate through calculations
626 is the subject of an entire branch of mathematics and computer
627 science and will not be discussed further here, except for the
632 If you require exact storage and calculations (such as for
633 monetary amounts), use the <type>numeric</type> type instead.
639 If you want to do complicated calculations with these types
640 for anything important, especially if you rely on certain
641 behavior in boundary cases (infinity, underflow), you should
642 evaluate the implementation carefully.
648 Comparing two floating-point values for equality might or might
649 not work as expected.
656 On most platforms, the <type>real</type> type has a range of at least
657 1E-37 to 1E+37 with a precision of at least 6 decimal digits. The
658 <type>double precision</type> type typically has a range of around
659 1E-307 to 1E+308 with a precision of at least 15 digits. Values that
660 are too large or too small will cause an error. Rounding might
661 take place if the precision of an input number is too high.
662 Numbers too close to zero that are not representable as distinct
663 from zero will cause an underflow error.
667 <primary>not a number</primary>
668 <secondary>double precision</secondary>
672 In addition to ordinary numeric values, the floating-point types
673 have several special values:
675 <literal>Infinity</literal>
676 <literal>-Infinity</literal>
677 <literal>NaN</literal>
679 These represent the IEEE 754 special values
680 <quote>infinity</quote>, <quote>negative infinity</quote>, and
681 <quote>not-a-number</quote>, respectively. (On a machine whose
682 floating-point arithmetic does not follow IEEE 754, these values
683 will probably not work as expected.) When writing these values
684 as constants in a SQL command, you must put quotes around them,
685 for example <literal>UPDATE table SET x = 'Infinity'</>. On input,
686 these strings are recognized in a case-insensitive manner.
691 IEEE754 specifies that <literal>NaN</> should not compare equal
692 to any other floating-point value (including <literal>NaN</>).
693 In order to allow floating-point values to be sorted and used
694 in tree-based indexes, <productname>PostgreSQL</> treats
695 <literal>NaN</> values as equal, and greater than all
696 non-<literal>NaN</> values.
701 <productname>PostgreSQL</productname> also supports the SQL-standard
702 notations <type>float</type> and
703 <type>float(<replaceable>p</replaceable>)</type> for specifying
704 inexact numeric types. Here, <replaceable>p</replaceable> specifies
705 the minimum acceptable precision in binary digits.
706 <productname>PostgreSQL</productname> accepts
707 <type>float(1)</type> to <type>float(24)</type> as selecting the
708 <type>real</type> type, while
709 <type>float(25)</type> to <type>float(53)</type> select
710 <type>double precision</type>. Values of <replaceable>p</replaceable>
711 outside the allowed range draw an error.
712 <type>float</type> with no precision specified is taken to mean
713 <type>double precision</type>.
718 Prior to <productname>PostgreSQL</productname> 7.4, the precision in
719 <type>float(<replaceable>p</replaceable>)</type> was taken to mean
720 so many decimal digits. This has been corrected to match the SQL
721 standard, which specifies that the precision is measured in binary
722 digits. The assumption that <type>real</type> and
723 <type>double precision</type> have exactly 24 and 53 bits in the
724 mantissa respectively is correct for IEEE-standard floating point
725 implementations. On non-IEEE platforms it might be off a little, but
726 for simplicity the same ranges of <replaceable>p</replaceable> are used
733 <sect2 id="datatype-serial">
734 <title>Serial Types</title>
736 <indexterm zone="datatype-serial">
737 <primary>serial</primary>
740 <indexterm zone="datatype-serial">
741 <primary>bigserial</primary>
744 <indexterm zone="datatype-serial">
745 <primary>serial4</primary>
748 <indexterm zone="datatype-serial">
749 <primary>serial8</primary>
753 <primary>auto-increment</primary>
758 <primary>sequence</primary>
759 <secondary>and serial type</secondary>
763 The data types <type>serial</type> and <type>bigserial</type>
764 are not true types, but merely
765 a notational convenience for setting up unique identifier columns
766 (similar to the <literal>AUTO_INCREMENT</literal> property
767 supported by some other databases). In the current
768 implementation, specifying:
771 CREATE TABLE <replaceable class="parameter">tablename</replaceable> (
772 <replaceable class="parameter">colname</replaceable> SERIAL
776 is equivalent to specifying:
779 CREATE SEQUENCE <replaceable class="parameter">tablename</replaceable>_<replaceable class="parameter">colname</replaceable>_seq;
780 CREATE TABLE <replaceable class="parameter">tablename</replaceable> (
781 <replaceable class="parameter">colname</replaceable> integer NOT NULL DEFAULT nextval('<replaceable class="parameter">tablename</replaceable>_<replaceable class="parameter">colname</replaceable>_seq')
783 ALTER SEQUENCE <replaceable class="parameter">tablename</replaceable>_<replaceable class="parameter">colname</replaceable>_seq OWNED BY <replaceable class="parameter">tablename</replaceable>.<replaceable class="parameter">colname</replaceable>;
786 Thus, we have created an integer column and arranged for its default
787 values to be assigned from a sequence generator. A <literal>NOT NULL</>
788 constraint is applied to ensure that a null value cannot be explicitly
789 inserted, either. (In most cases you would also want to attach a
790 <literal>UNIQUE</> or <literal>PRIMARY KEY</> constraint to prevent
791 duplicate values from being inserted by accident, but this is
792 not automatic.) Lastly, the sequence is marked as <quote>owned by</>
793 the column, so that it will be dropped if the column or table is dropped.
798 Prior to <productname>PostgreSQL</productname> 7.3, <type>serial</type>
799 implied <literal>UNIQUE</literal>. This is no longer automatic. If
800 you wish a serial column to be in a unique constraint or a
801 primary key, it must now be specified, same as with
807 To insert the next value of the sequence into the <type>serial</type>
808 column, specify that the <type>serial</type>
809 column should be assigned its default value. This can be done
810 either by excluding the column from the list of columns in
811 the <command>INSERT</command> statement, or through the use of
812 the <literal>DEFAULT</literal> key word.
816 The type names <type>serial</type> and <type>serial4</type> are
817 equivalent: both create <type>integer</type> columns. The type
818 names <type>bigserial</type> and <type>serial8</type> work just
819 the same way, except that they create a <type>bigint</type>
820 column. <type>bigserial</type> should be used if you anticipate
821 the use of more than 2<superscript>31</> identifiers over the
822 lifetime of the table.
826 The sequence created for a <type>serial</type> column is
827 automatically dropped when the owning column is dropped.
828 You can drop the sequence without dropping the column, but this
829 will force removal of the column default expression.
834 <sect1 id="datatype-money">
835 <title>Monetary Types</title>
838 The <type>money</type> type stores a currency amount with a fixed
839 fractional precision; see <xref
840 linkend="datatype-money-table">.
841 Input is accepted in a variety of formats, including integer and
842 floating-point literals, as well as <quote>typical</quote>
843 currency formatting, such as <literal>'$1,000.00'</literal>.
844 Output is generally in the latter form but depends on the locale.
845 Non-quoted numeric values can be converted to <type>money</type> by
846 casting the numeric value to <type>text</type> and then
849 SELECT 1234::text::money;
851 There is no simple way of doing the reverse; casting a <type>money</type> value to a
856 Since the output of this data type is locale-sensitive, it may not
857 work to load <type>money</> data into a database that has a different
858 setting of <varname>lc_monetary</>. To avoid problems, before
859 restoring a dump make sure <varname>lc_monetary</> has the same or
860 equivalent value as in the database that was dumped.
863 <table id="datatype-money-table">
864 <title>Monetary Types</title>
869 <entry>Storage Size</entry>
870 <entry>Description</entry>
877 <entry>8 bytes</entry>
878 <entry>currency amount</entry>
879 <entry>-92233720368547758.08 to +92233720368547758.07</entry>
887 <sect1 id="datatype-character">
888 <title>Character Types</title>
890 <indexterm zone="datatype-character">
891 <primary>character string</primary>
892 <secondary>data types</secondary>
896 <primary>string</primary>
897 <see>character string</see>
900 <indexterm zone="datatype-character">
901 <primary>character</primary>
904 <indexterm zone="datatype-character">
905 <primary>character varying</primary>
908 <indexterm zone="datatype-character">
909 <primary>text</primary>
912 <indexterm zone="datatype-character">
913 <primary>char</primary>
916 <indexterm zone="datatype-character">
917 <primary>varchar</primary>
920 <table id="datatype-character-table">
921 <title>Character Types</title>
926 <entry>Description</entry>
931 <entry><type>character varying(<replaceable>n</>)</type>, <type>varchar(<replaceable>n</>)</type></entry>
932 <entry>variable-length with limit</entry>
935 <entry><type>character(<replaceable>n</>)</type>, <type>char(<replaceable>n</>)</type></entry>
936 <entry>fixed-length, blank padded</entry>
939 <entry><type>text</type></entry>
940 <entry>variable unlimited length</entry>
947 <xref linkend="datatype-character-table"> shows the
948 general-purpose character types available in
949 <productname>PostgreSQL</productname>.
953 <acronym>SQL</acronym> defines two primary character types:
954 <type>character varying(<replaceable>n</>)</type> and
955 <type>character(<replaceable>n</>)</type>, where <replaceable>n</>
956 is a positive integer. Both of these types can store strings up to
957 <replaceable>n</> characters in length. An attempt to store a
958 longer string into a column of these types will result in an
959 error, unless the excess characters are all spaces, in which case
960 the string will be truncated to the maximum length. (This somewhat
961 bizarre exception is required by the <acronym>SQL</acronym>
962 standard.) If the string to be stored is shorter than the declared
963 length, values of type <type>character</type> will be space-padded;
964 values of type <type>character varying</type> will simply store the
970 If one explicitly casts a value to <type>character
971 varying(<replaceable>n</>)</type> or
972 <type>character(<replaceable>n</>)</type>, then an over-length
973 value will be truncated to <replaceable>n</> characters without
974 raising an error. (This too is required by the
975 <acronym>SQL</acronym> standard.)
979 The notations <type>varchar(<replaceable>n</>)</type> and
980 <type>char(<replaceable>n</>)</type> are aliases for <type>character
981 varying(<replaceable>n</>)</type> and
982 <type>character(<replaceable>n</>)</type>, respectively.
983 <type>character</type> without length specifier is equivalent to
984 <type>character(1)</type>. If <type>character varying</type> is used
985 without length specifier, the type accepts strings of any size. The
986 latter is a <productname>PostgreSQL</> extension.
990 In addition, <productname>PostgreSQL</productname> provides the
991 <type>text</type> type, which stores strings of any length.
992 Although the type <type>text</type> is not in the
993 <acronym>SQL</acronym> standard, several other SQL database
994 management systems have it as well.
998 Values of type <type>character</type> are physically padded
999 with spaces to the specified width <replaceable>n</>, and are
1000 stored and displayed that way. However, the padding spaces are
1001 treated as semantically insignificant. Trailing spaces are
1002 disregarded when comparing two values of type <type>character</type>,
1003 and they will be removed when converting a <type>character</type> value
1004 to one of the other string types. Note that trailing spaces
1005 <emphasis>are</> semantically significant in
1006 <type>character varying</type> and <type>text</type> values.
1010 The storage requirement for a short string (up to 126 bytes) is 1 byte
1011 plus the actual string, which includes the space padding in the case of
1012 <type>character</type>. Longer strings have 4 bytes overhead instead
1013 of 1. Long strings are compressed by the system automatically, so
1014 the physical requirement on disk might be less. Very long values are also
1015 stored in background tables so that they do not interfere with rapid
1016 access to shorter column values. In any case, the longest
1017 possible character string that can be stored is about 1 GB. (The
1018 maximum value that will be allowed for <replaceable>n</> in the data
1019 type declaration is less than that. It wouldn't be very useful to
1020 change this because with multibyte character encodings the number of
1021 characters and bytes can be quite different anyway. If you desire to
1022 store long strings with no specific upper limit, use
1023 <type>text</type> or <type>character varying</type> without a length
1024 specifier, rather than making up an arbitrary length limit.)
1029 There are no performance differences between these three types,
1030 apart from increased storage size when using the blank-padded
1031 type, and a few extra cycles to check the length when storing into
1032 a length-constrained column. While
1033 <type>character(<replaceable>n</>)</type> has performance
1034 advantages in some other database systems, it has no such advantages in
1035 <productname>PostgreSQL</productname>. In most situations
1036 <type>text</type> or <type>character varying</type> should be used
1042 Refer to <xref linkend="sql-syntax-strings"> for information about
1043 the syntax of string literals, and to <xref linkend="functions">
1044 for information about available operators and functions. The
1045 database character set determines the character set used to store
1046 textual values; for more information on character set support,
1047 refer to <xref linkend="multibyte">.
1051 <title>Using the character types</title>
1054 CREATE TABLE test1 (a character(4));
1055 INSERT INTO test1 VALUES ('ok');
1056 SELECT a, char_length(a) FROM test1; -- <co id="co.datatype-char">
1059 ------+-------------
1063 CREATE TABLE test2 (b varchar(5));
1064 INSERT INTO test2 VALUES ('ok');
1065 INSERT INTO test2 VALUES ('good ');
1066 INSERT INTO test2 VALUES ('too long');
1067 <computeroutput>ERROR: value too long for type character varying(5)</computeroutput>
1068 INSERT INTO test2 VALUES ('too long'::varchar(5)); -- explicit truncation
1069 SELECT b, char_length(b) FROM test2;
1072 -------+-------------
1079 <callout arearefs="co.datatype-char">
1081 The <function>char_length</function> function is discussed in
1082 <xref linkend="functions-string">.
1089 There are two other fixed-length character types in
1090 <productname>PostgreSQL</productname>, shown in <xref
1091 linkend="datatype-character-special-table">. The <type>name</type>
1092 type exists <emphasis>only</emphasis> for storage of identifiers
1093 in the internal system catalogs and is not intended for use by the general user. Its
1094 length is currently defined as 64 bytes (63 usable characters plus
1095 terminator) but should be referenced using the constant
1096 <symbol>NAMEDATALEN</symbol>. The length is set at compile time (and
1097 is therefore adjustable for special uses); the default maximum
1098 length might change in a future release. The type <type>"char"</type>
1099 (note the quotes) is different from <type>char(1)</type> in that it
1100 only uses one byte of storage. It is internally used in the system
1101 catalogs as a poor-man's enumeration type.
1104 <table id="datatype-character-special-table">
1105 <title>Special Character Types</title>
1110 <entry>Storage Size</entry>
1111 <entry>Description</entry>
1116 <entry><type>"char"</type></entry>
1117 <entry>1 byte</entry>
1118 <entry>single-byte internal type</entry>
1121 <entry><type>name</type></entry>
1122 <entry>64 bytes</entry>
1123 <entry>internal type for object names</entry>
1131 <sect1 id="datatype-binary">
1132 <title>Binary Data Types</title>
1134 <indexterm zone="datatype-binary">
1135 <primary>binary data</primary>
1138 <indexterm zone="datatype-binary">
1139 <primary>bytea</primary>
1143 The <type>bytea</type> data type allows storage of binary strings;
1144 see <xref linkend="datatype-binary-table">.
1147 <table id="datatype-binary-table">
1148 <title>Binary Data Types</title>
1153 <entry>Storage Size</entry>
1154 <entry>Description</entry>
1159 <entry><type>bytea</type></entry>
1160 <entry>1 or 4 bytes plus the actual binary string</entry>
1161 <entry>variable-length binary string</entry>
1168 A binary string is a sequence of octets (or bytes). Binary
1169 strings are distinguished from character strings by two
1170 characteristics: First, binary strings specifically allow storing
1171 octets of value zero and other <quote>non-printable</quote>
1172 octets (usually, octets outside the range 32 to 126).
1173 Character strings disallow zero octets, and also disallow any
1174 other octet values and sequences of octet values that are invalid
1175 according to the database's selected character set encoding.
1176 Second, operations on binary strings process the actual bytes,
1177 whereas the processing of character strings depends on locale settings.
1178 In short, binary strings are appropriate for storing data that the
1179 programmer thinks of as <quote>raw bytes</>, whereas character
1180 strings are appropriate for storing text.
1184 When entering <type>bytea</type> values, octets of certain
1185 values <emphasis>must</emphasis> be escaped (but all octet
1186 values <emphasis>can</emphasis> be escaped) when used as part
1187 of a string literal in an <acronym>SQL</acronym> statement. In
1188 general, to escape an octet, it is converted into the three-digit
1189 octal number equivalent of its decimal octet value, and preceded
1190 by two backslashes. <xref linkend="datatype-binary-sqlesc">
1191 shows the characters that must be escaped, and gives the alternative
1192 escape sequences where applicable.
1195 <table id="datatype-binary-sqlesc">
1196 <title><type>bytea</> Literal Escaped Octets</title>
1200 <entry>Decimal Octet Value</entry>
1201 <entry>Description</entry>
1202 <entry>Escaped Input Representation</entry>
1203 <entry>Example</entry>
1204 <entry>Output Representation</entry>
1211 <entry>zero octet</entry>
1212 <entry><literal>E'\\000'</literal></entry>
1213 <entry><literal>SELECT E'\\000'::bytea;</literal></entry>
1214 <entry><literal>\000</literal></entry>
1219 <entry>single quote</entry>
1220 <entry><literal>''''</literal> or <literal>E'\\047'</literal></entry>
1221 <entry><literal>SELECT E'\''::bytea;</literal></entry>
1222 <entry><literal>'</literal></entry>
1227 <entry>backslash</entry>
1228 <entry><literal>E'\\\\'</literal> or <literal>E'\\134'</literal></entry>
1229 <entry><literal>SELECT E'\\\\'::bytea;</literal></entry>
1230 <entry><literal>\\</literal></entry>
1234 <entry>0 to 31 and 127 to 255</entry>
1235 <entry><quote>non-printable</quote> octets</entry>
1236 <entry><literal>E'\\<replaceable>xxx'</></literal> (octal value)</entry>
1237 <entry><literal>SELECT E'\\001'::bytea;</literal></entry>
1238 <entry><literal>\001</literal></entry>
1246 The requirement to escape <quote>non-printable</quote> octets actually
1247 varies depending on locale settings. In some instances you can get away
1248 with leaving them unescaped. Note that the result in each of the examples
1249 in <xref linkend="datatype-binary-sqlesc"> was exactly one octet in
1250 length, even though the output representation of the zero octet and
1251 backslash are more than one character.
1255 The reason that you have to write so many backslashes, as shown
1256 in <xref linkend="datatype-binary-sqlesc">, is that an input
1257 string written as a string literal must pass through two parse
1258 phases in the <productname>PostgreSQL</productname> server.
1259 The first backslash of each pair is interpreted as an escape
1260 character by the string-literal parser (assuming escape string
1261 syntax is used) and is therefore consumed, leaving the second backslash of the
1262 pair. (Dollar-quoted strings can be used to avoid this level
1263 of escaping.) The remaining backslash is then recognized by the
1264 <type>bytea</type> input function as starting either a three
1265 digit octal value or escaping another backslash. For example,
1266 a string literal passed to the server as <literal>E'\\001'</literal>
1267 becomes <literal>\001</literal> after passing through the
1268 escape string parser. The <literal>\001</literal> is then sent
1269 to the <type>bytea</type> input function, where it is converted
1270 to a single octet with a decimal value of 1. Note that the
1271 single-quote character is not treated specially by <type>bytea</type>,
1272 so it follows the normal rules for string literals. (See also
1273 <xref linkend="sql-syntax-strings">.)
1277 <type>Bytea</type> octets are also escaped in the output. In general, each
1278 <quote>non-printable</quote> octet is converted into
1279 its equivalent three-digit octal value and preceded by one backslash.
1280 Most <quote>printable</quote> octets are represented by their standard
1281 representation in the client character set. The octet with decimal
1282 value 92 (backslash) has a special alternative output representation.
1283 Details are in <xref linkend="datatype-binary-resesc">.
1286 <table id="datatype-binary-resesc">
1287 <title><type>bytea</> Output Escaped Octets</title>
1291 <entry>Decimal Octet Value</entry>
1292 <entry>Description</entry>
1293 <entry>Escaped Output Representation</entry>
1294 <entry>Example</entry>
1295 <entry>Output Result</entry>
1303 <entry>backslash</entry>
1304 <entry><literal>\\</literal></entry>
1305 <entry><literal>SELECT E'\\134'::bytea;</literal></entry>
1306 <entry><literal>\\</literal></entry>
1310 <entry>0 to 31 and 127 to 255</entry>
1311 <entry><quote>non-printable</quote> octets</entry>
1312 <entry><literal>\<replaceable>xxx</></literal> (octal value)</entry>
1313 <entry><literal>SELECT E'\\001'::bytea;</literal></entry>
1314 <entry><literal>\001</literal></entry>
1318 <entry>32 to 126</entry>
1319 <entry><quote>printable</quote> octets</entry>
1320 <entry>client character set representation</entry>
1321 <entry><literal>SELECT E'\\176'::bytea;</literal></entry>
1322 <entry><literal>~</literal></entry>
1330 Depending on the front end to <productname>PostgreSQL</> you use,
1331 you might have additional work to do in terms of escaping and
1332 unescaping <type>bytea</type> strings. For example, you might also
1333 have to escape line feeds and carriage returns if your interface
1334 automatically translates these.
1338 The <acronym>SQL</acronym> standard defines a different binary
1339 string type, called <type>BLOB</type> or <type>BINARY LARGE
1340 OBJECT</type>. The input format is different from
1341 <type>bytea</type>, but the provided functions and operators are
1347 <sect1 id="datatype-datetime">
1348 <title>Date/Time Types</title>
1350 <indexterm zone="datatype-datetime">
1351 <primary>date</primary>
1353 <indexterm zone="datatype-datetime">
1354 <primary>time</primary>
1356 <indexterm zone="datatype-datetime">
1357 <primary>time without time zone</primary>
1359 <indexterm zone="datatype-datetime">
1360 <primary>time with time zone</primary>
1362 <indexterm zone="datatype-datetime">
1363 <primary>timestamp</primary>
1365 <indexterm zone="datatype-datetime">
1366 <primary>timestamp with time zone</primary>
1368 <indexterm zone="datatype-datetime">
1369 <primary>timestamp without time zone</primary>
1371 <indexterm zone="datatype-datetime">
1372 <primary>interval</primary>
1374 <indexterm zone="datatype-datetime">
1375 <primary>time span</primary>
1379 <productname>PostgreSQL</productname> supports the full set of
1380 <acronym>SQL</acronym> date and time types, shown in <xref
1381 linkend="datatype-datetime-table">. The operations available
1382 on these data types are described in
1383 <xref linkend="functions-datetime">.
1386 <table id="datatype-datetime-table">
1387 <title>Date/Time Types</title>
1392 <entry>Storage Size</entry>
1393 <entry>Description</entry>
1394 <entry>Low Value</entry>
1395 <entry>High Value</entry>
1396 <entry>Resolution</entry>
1401 <entry><type>timestamp [ (<replaceable>p</replaceable>) ] [ without time zone ]</type></entry>
1402 <entry>8 bytes</entry>
1403 <entry>both date and time</entry>
1404 <entry>4713 BC</entry>
1405 <entry>5874897 AD</entry>
1406 <entry>1 microsecond / 14 digits</entry>
1409 <entry><type>timestamp [ (<replaceable>p</replaceable>) ] with time zone</type></entry>
1410 <entry>8 bytes</entry>
1411 <entry>both date and time, with time zone</entry>
1412 <entry>4713 BC</entry>
1413 <entry>5874897 AD</entry>
1414 <entry>1 microsecond / 14 digits</entry>
1417 <entry><type>interval [ (<replaceable>p</replaceable>) ]</type></entry>
1418 <entry>12 bytes</entry>
1419 <entry>time intervals</entry>
1420 <entry>-178000000 years</entry>
1421 <entry>178000000 years</entry>
1422 <entry>1 microsecond / 14 digits</entry>
1425 <entry><type>date</type></entry>
1426 <entry>4 bytes</entry>
1427 <entry>dates only</entry>
1428 <entry>4713 BC</entry>
1429 <entry>5874897 AD</entry>
1430 <entry>1 day</entry>
1433 <entry><type>time [ (<replaceable>p</replaceable>) ] [ without time zone ]</type></entry>
1434 <entry>8 bytes</entry>
1435 <entry>times of day only</entry>
1436 <entry>00:00:00</entry>
1437 <entry>24:00:00</entry>
1438 <entry>1 microsecond / 14 digits</entry>
1441 <entry><type>time [ (<replaceable>p</replaceable>) ] with time zone</type></entry>
1442 <entry>12 bytes</entry>
1443 <entry>times of day only, with time zone</entry>
1444 <entry>00:00:00+1459</entry>
1445 <entry>24:00:00-1459</entry>
1446 <entry>1 microsecond / 14 digits</entry>
1454 Prior to <productname>PostgreSQL</productname> 7.3, writing just
1455 <type>timestamp</type> was equivalent to <type>timestamp with
1456 time zone</type>. This was changed for SQL compliance.
1461 <type>time</type>, <type>timestamp</type>, and
1462 <type>interval</type> accept an optional precision value
1463 <replaceable>p</replaceable> which specifies the number of
1464 fractional digits retained in the seconds field. By default, there
1465 is no explicit bound on precision. The allowed range of
1466 <replaceable>p</replaceable> is from 0 to 6 for the
1467 <type>timestamp</type> and <type>interval</type> types.
1472 When <type>timestamp</> values are stored as double precision floating-point
1473 numbers (currently the default), the effective limit of precision
1474 might be less than 6. <type>timestamp</type> values are stored as seconds
1475 before or after midnight 2000-01-01. Microsecond precision is achieved for
1476 dates within a few years of 2000-01-01, but the precision degrades for
1477 dates further away. When <type>timestamp</type> values are stored as
1478 eight-byte integers (a compile-time
1479 option), microsecond precision is available over the full range of
1480 values. However eight-byte integer timestamps have a more limited range of
1481 dates than shown above: from 4713 BC up to 294276 AD. The same
1482 compile-time option also determines whether <type>time</type> and
1483 <type>interval</type> values are stored as floating-point or eight-byte
1484 integers. In the floating-point case, large <type>interval</type> values
1485 degrade in precision as the size of the interval increases.
1490 For the <type>time</type> types, the allowed range of
1491 <replaceable>p</replaceable> is from 0 to 6 when eight-byte integer
1492 storage is used, or from 0 to 10 when floating-point storage is used.
1496 The type <type>time with time zone</type> is defined by the SQL
1497 standard, but the definition exhibits properties which lead to
1498 questionable usefulness. In most cases, a combination of
1499 <type>date</type>, <type>time</type>, <type>timestamp without time
1500 zone</type>, and <type>timestamp with time zone</type> should
1501 provide a complete range of date/time functionality required by
1506 The types <type>abstime</type>
1507 and <type>reltime</type> are lower precision types which are used internally.
1508 You are discouraged from using these types in new
1509 applications and are encouraged to move any old
1510 ones over when appropriate. Any or all of these internal types
1511 might disappear in a future release.
1514 <sect2 id="datatype-datetime-input">
1515 <title>Date/Time Input</title>
1518 Date and time input is accepted in almost any reasonable format, including
1519 ISO 8601, <acronym>SQL</acronym>-compatible,
1520 traditional <productname>POSTGRES</productname>, and others.
1521 For some formats, ordering of month, day, and year in date input is
1522 ambiguous and there is support for specifying the expected
1523 ordering of these fields. Set the <xref linkend="guc-datestyle"> parameter
1524 to <literal>MDY</> to select month-day-year interpretation,
1525 <literal>DMY</> to select day-month-year interpretation, or
1526 <literal>YMD</> to select year-month-day interpretation.
1530 <productname>PostgreSQL</productname> is more flexible in
1531 handling date/time input than the
1532 <acronym>SQL</acronym> standard requires.
1533 See <xref linkend="datetime-appendix">
1534 for the exact parsing rules of date/time input and for the
1535 recognized text fields including months, days of the week, and
1540 Remember that any date or time literal input needs to be enclosed
1541 in single quotes, like text strings. Refer to
1542 <xref linkend="sql-syntax-constants-generic"> for more
1544 <acronym>SQL</acronym> requires the following syntax
1546 <replaceable>type</replaceable> [ (<replaceable>p</replaceable>) ] '<replaceable>value</replaceable>'
1548 where <replaceable>p</replaceable> in the optional precision
1549 specification is an integer corresponding to the number of
1550 fractional digits in the seconds field. Precision can be
1551 specified for <type>time</type>, <type>timestamp</type>, and
1552 <type>interval</type> types. The allowed values are mentioned
1553 above. If no precision is specified in a constant specification,
1554 it defaults to the precision of the literal value.
1558 <title>Dates</title>
1561 <primary>date</primary>
1565 <xref linkend="datatype-datetime-date-table"> shows some possible
1566 inputs for the <type>date</type> type.
1569 <table id="datatype-datetime-date-table">
1570 <title>Date Input</title>
1574 <entry>Example</entry>
1575 <entry>Description</entry>
1580 <entry>January 8, 1999</entry>
1581 <entry>unambiguous in any <varname>datestyle</varname> input mode</entry>
1584 <entry>1999-01-08</entry>
1585 <entry>ISO 8601; January 8 in any mode
1586 (recommended format)</entry>
1589 <entry>1/8/1999</entry>
1590 <entry>January 8 in <literal>MDY</> mode;
1591 August 1 in <literal>DMY</> mode</entry>
1594 <entry>1/18/1999</entry>
1595 <entry>January 18 in <literal>MDY</> mode;
1596 rejected in other modes</entry>
1599 <entry>01/02/03</entry>
1600 <entry>January 2, 2003 in <literal>MDY</> mode;
1601 February 1, 2003 in <literal>DMY</> mode;
1602 February 3, 2001 in <literal>YMD</> mode
1606 <entry>1999-Jan-08</entry>
1607 <entry>January 8 in any mode</entry>
1610 <entry>Jan-08-1999</entry>
1611 <entry>January 8 in any mode</entry>
1614 <entry>08-Jan-1999</entry>
1615 <entry>January 8 in any mode</entry>
1618 <entry>99-Jan-08</entry>
1619 <entry>January 8 in <literal>YMD</> mode, else error</entry>
1622 <entry>08-Jan-99</entry>
1623 <entry>January 8, except error in <literal>YMD</> mode</entry>
1626 <entry>Jan-08-99</entry>
1627 <entry>January 8, except error in <literal>YMD</> mode</entry>
1630 <entry>19990108</entry>
1631 <entry>ISO 8601; January 8, 1999 in any mode</entry>
1634 <entry>990108</entry>
1635 <entry>ISO 8601; January 8, 1999 in any mode</entry>
1638 <entry>1999.008</entry>
1639 <entry>year and day of year</entry>
1642 <entry>J2451187</entry>
1643 <entry>Julian day</entry>
1646 <entry>January 8, 99 BC</entry>
1647 <entry>year 99 before the Common Era</entry>
1655 <title>Times</title>
1658 <primary>time</primary>
1661 <primary>time without time zone</primary>
1664 <primary>time with time zone</primary>
1668 The time-of-day types are <type>time [
1669 (<replaceable>p</replaceable>) ] without time zone</type> and
1670 <type>time [ (<replaceable>p</replaceable>) ] with time
1671 zone</type>. Writing just <type>time</type> is equivalent to
1672 <type>time without time zone</type>.
1676 Valid input for these types consists of a time of day followed
1677 by an optional time zone. (See <xref
1678 linkend="datatype-datetime-time-table">
1679 and <xref linkend="datatype-timezone-table">.) If a time zone is
1680 specified in the input for <type>time without time zone</type>,
1681 it is silently ignored. You can also specify a date but it will
1682 be ignored, except when you use a time zone name that involves a
1683 daylight-savings rule, such as
1684 <literal>America/New_York</literal>. In this case specifying the date
1685 is required in order to determine whether standard or daylight-savings
1686 time applies. The appropriate time zone offset is recorded in the
1687 <type>time with time zone</type> value.
1690 <table id="datatype-datetime-time-table">
1691 <title>Time Input</title>
1695 <entry>Example</entry>
1696 <entry>Description</entry>
1701 <entry><literal>04:05:06.789</literal></entry>
1702 <entry>ISO 8601</entry>
1705 <entry><literal>04:05:06</literal></entry>
1706 <entry>ISO 8601</entry>
1709 <entry><literal>04:05</literal></entry>
1710 <entry>ISO 8601</entry>
1713 <entry><literal>040506</literal></entry>
1714 <entry>ISO 8601</entry>
1717 <entry><literal>04:05 AM</literal></entry>
1718 <entry>same as 04:05; AM does not affect value</entry>
1721 <entry><literal>04:05 PM</literal></entry>
1722 <entry>same as 16:05; input hour must be <= 12</entry>
1725 <entry><literal>04:05:06.789-8</literal></entry>
1726 <entry>ISO 8601</entry>
1729 <entry><literal>04:05:06-08:00</literal></entry>
1730 <entry>ISO 8601</entry>
1733 <entry><literal>04:05-08:00</literal></entry>
1734 <entry>ISO 8601</entry>
1737 <entry><literal>040506-08</literal></entry>
1738 <entry>ISO 8601</entry>
1741 <entry><literal>04:05:06 PST</literal></entry>
1742 <entry>time zone specified by abbreviation</entry>
1745 <entry><literal>2003-04-12 04:05:06 America/New_York</literal></entry>
1746 <entry>time zone specified by full name</entry>
1752 <table tocentry="1" id="datatype-timezone-table">
1753 <title>Time Zone Input</title>
1757 <entry>Example</entry>
1758 <entry>Description</entry>
1763 <entry><literal>PST</literal></entry>
1764 <entry>Abbreviation (for Pacific Standard Time)</entry>
1767 <entry><literal>America/New_York</literal></entry>
1768 <entry>Full time zone name</entry>
1771 <entry><literal>PST8PDT</literal></entry>
1772 <entry>POSIX-style time zone specification</entry>
1775 <entry><literal>-8:00</literal></entry>
1776 <entry>ISO-8601 offset for PST</entry>
1779 <entry><literal>-800</literal></entry>
1780 <entry>ISO-8601 offset for PST</entry>
1783 <entry><literal>-8</literal></entry>
1784 <entry>ISO-8601 offset for PST</entry>
1787 <entry><literal>zulu</literal></entry>
1788 <entry>Military abbreviation for UTC</entry>
1791 <entry><literal>z</literal></entry>
1792 <entry>Short form of <literal>zulu</literal></entry>
1799 Refer to <xref linkend="datatype-timezones"> for more information on how
1800 to specify time zones.
1805 <title>Time Stamps</title>
1808 <primary>timestamp</primary>
1812 <primary>timestamp with time zone</primary>
1816 <primary>timestamp without time zone</primary>
1820 Valid input for the time stamp types consists of a concatenation
1821 of a date and a time, followed by an optional time zone,
1822 followed by an optional <literal>AD</literal> or <literal>BC</literal>.
1823 (Alternatively, <literal>AD</literal>/<literal>BC</literal> can appear
1824 before the time zone, but this is not the preferred ordering.)
1832 1999-01-08 04:05:06 -8:00
1835 are valid values, which follow the <acronym>ISO</acronym> 8601
1836 standard. In addition, the wide-spread format:
1838 January 8 04:05:06 1999 PST
1844 The <acronym>SQL</acronym> standard differentiates <type>timestamp without time zone</type>
1845 and <type>timestamp with time zone</type> literals by the presence of a
1846 <quote>+</quote> or <quote>-</quote>. Hence, according to the standard,
1847 <programlisting>TIMESTAMP '2004-10-19 10:23:54'</programlisting>
1848 is a <type>timestamp without time zone</type>, while
1849 <programlisting>TIMESTAMP '2004-10-19 10:23:54+02'</programlisting>
1850 is a <type>timestamp with time zone</type>.
1851 <productname>PostgreSQL</productname> never examines the content of a
1852 literal string before determining its type, and therefore will treat
1853 both of the above as <type>timestamp without time zone</type>. To
1854 ensure that a literal is treated as <type>timestamp with time
1855 zone</type>, give it the correct explicit type:
1856 <programlisting>TIMESTAMP WITH TIME ZONE '2004-10-19 10:23:54+02'</programlisting>
1857 In a literal that has been decided to be <type>timestamp without time
1858 zone</type>, <productname>PostgreSQL</productname> will silently ignore
1859 any time zone indication.
1860 That is, the resulting value is derived from the date/time
1861 fields in the input value, and is not adjusted for time zone.
1865 For <type>timestamp with time zone</type>, the internally stored
1866 value is always in UTC (Universal
1867 Coordinated Time, traditionally known as Greenwich Mean Time,
1868 <acronym>GMT</>). An input value that has an explicit
1869 time zone specified is converted to UTC using the appropriate offset
1870 for that time zone. If no time zone is stated in the input string,
1871 then it is assumed to be in the time zone indicated by the system's
1872 <xref linkend="guc-timezone"> parameter, and is converted to UTC using the
1873 offset for the <varname>timezone</> zone.
1877 When a <type>timestamp with time
1878 zone</type> value is output, it is always converted from UTC to the
1879 current <varname>timezone</> zone, and displayed as local time in that
1880 zone. To see the time in another time zone, either change
1881 <varname>timezone</> or use the <literal>AT TIME ZONE</> construct
1882 (see <xref linkend="functions-datetime-zoneconvert">).
1886 Conversions between <type>timestamp without time zone</type> and
1887 <type>timestamp with time zone</type> normally assume that the
1888 <type>timestamp without time zone</type> value should be taken or given
1889 as <varname>timezone</> local time. A different zone reference can
1890 be specified for the conversion using <literal>AT TIME ZONE</>.
1895 <title>Intervals</title>
1898 <primary>interval</primary>
1902 <type>interval</type> values can be written with the following syntax:
1905 <optional>@</> <replaceable>quantity</> <replaceable>unit</> <optional><replaceable>quantity</> <replaceable>unit</>...</> <optional><replaceable>direction</></optional>
1908 Where: <replaceable>quantity</> is a number (possibly signed);
1909 <replaceable>unit</> is <literal>microsecond</literal>,
1910 <literal>millisecond</literal>, <literal>second</literal>,
1911 <literal>minute</literal>, <literal>hour</literal>, <literal>day</literal>,
1912 <literal>week</literal>, <literal>month</literal>, <literal>year</literal>,
1913 <literal>decade</literal>, <literal>century</literal>, <literal>millennium</literal>,
1914 or abbreviations or plurals of these units;
1915 <replaceable>direction</> can be <literal>ago</literal> or
1916 empty. The at sign (<literal>@</>) is optional noise. The amounts
1917 of different units are implicitly added up with appropriate
1922 Quantities of days, hours, minutes, and seconds can be specified without
1923 explicit unit markings. For example, <literal>'1 12:59:10'</> is read
1924 the same as <literal>'1 day 12 hours 59 min 10 sec'</>.
1928 The optional subsecond precision <replaceable>p</replaceable> should
1929 be between 0 and 6, and defaults to the precision of the input literal.
1933 Internally <type>interval</> values are stored as months, days,
1934 and seconds. This is done because the number of days in a month
1935 varies, and a day can have 23 or 25 hours if a daylight savings
1936 time adjustment is involved. Because intervals are usually created
1937 from constant strings or <type>timestamp</> subtraction, this
1938 storage method works well in most cases. Functions
1939 <function>justify_days</> and <function>justify_hours</> are
1940 available for adjusting days and hours that overflow their normal
1946 <title>Special Values</title>
1949 <primary>time</primary>
1950 <secondary>constants</secondary>
1954 <primary>date</primary>
1955 <secondary>constants</secondary>
1959 <productname>PostgreSQL</productname> supports several
1960 special date/time input values for convenience, as shown in <xref
1961 linkend="datatype-datetime-special-table">. The values
1962 <literal>infinity</literal> and <literal>-infinity</literal>
1963 are specially represented inside the system and will be displayed
1964 the same way; but the others are simply notational shorthands
1965 that will be converted to ordinary date/time values when read.
1966 (In particular, <literal>now</> and related strings are converted
1967 to a specific time value as soon as they are read.)
1968 All of these values need to be written in single quotes when used
1969 as constants in SQL commands.
1972 <table id="datatype-datetime-special-table">
1973 <title>Special Date/Time Inputs</title>
1977 <entry>Input String</entry>
1978 <entry>Valid Types</entry>
1979 <entry>Description</entry>
1984 <entry><literal>epoch</literal></entry>
1985 <entry><type>date</type>, <type>timestamp</type></entry>
1986 <entry>1970-01-01 00:00:00+00 (Unix system time zero)</entry>
1989 <entry><literal>infinity</literal></entry>
1990 <entry><type>timestamp</type></entry>
1991 <entry>later than all other time stamps</entry>
1994 <entry><literal>-infinity</literal></entry>
1995 <entry><type>timestamp</type></entry>
1996 <entry>earlier than all other time stamps</entry>
1999 <entry><literal>now</literal></entry>
2000 <entry><type>date</type>, <type>time</type>, <type>timestamp</type></entry>
2001 <entry>current transaction's start time</entry>
2004 <entry><literal>today</literal></entry>
2005 <entry><type>date</type>, <type>timestamp</type></entry>
2006 <entry>midnight today</entry>
2009 <entry><literal>tomorrow</literal></entry>
2010 <entry><type>date</type>, <type>timestamp</type></entry>
2011 <entry>midnight tomorrow</entry>
2014 <entry><literal>yesterday</literal></entry>
2015 <entry><type>date</type>, <type>timestamp</type></entry>
2016 <entry>midnight yesterday</entry>
2019 <entry><literal>allballs</literal></entry>
2020 <entry><type>time</type></entry>
2021 <entry>00:00:00.00 UTC</entry>
2028 The following <acronym>SQL</acronym>-compatible functions can also
2029 be used to obtain the current time value for the corresponding data
2031 <literal>CURRENT_DATE</literal>, <literal>CURRENT_TIME</literal>,
2032 <literal>CURRENT_TIMESTAMP</literal>, <literal>LOCALTIME</literal>,
2033 <literal>LOCALTIMESTAMP</literal>. The latter four accept an
2034 optional subsecond precision specification. (See <xref
2035 linkend="functions-datetime-current">.) Note however that these are
2036 SQL functions and are <emphasis>not</> recognized as data input strings.
2042 <sect2 id="datatype-datetime-output">
2043 <title>Date/Time Output</title>
2046 <primary>date</primary>
2047 <secondary>output format</secondary>
2048 <seealso>formatting</seealso>
2052 <primary>time</primary>
2053 <secondary>output format</secondary>
2054 <seealso>formatting</seealso>
2058 The output format of the date/time types can be set to one of the four
2060 <acronym>SQL</acronym> (Ingres), traditional POSTGRES, and
2061 German, using the command <literal>SET datestyle</literal>. The default
2062 is the <acronym>ISO</acronym> format. (The
2063 <acronym>SQL</acronym> standard requires the use of the ISO 8601
2064 format. The name of the <quote>SQL</quote> output format is a
2065 historical accident.) <xref
2066 linkend="datatype-datetime-output-table"> shows examples of each
2067 output style. The output of the <type>date</type> and
2068 <type>time</type> types is of course only the date or time part
2069 in accordance with the given examples.
2072 <table id="datatype-datetime-output-table">
2073 <title>Date/Time Output Styles</title>
2077 <entry>Style Specification</entry>
2078 <entry>Description</entry>
2079 <entry>Example</entry>
2085 <entry>ISO 8601/SQL standard</entry>
2086 <entry>1997-12-17 07:37:16-08</entry>
2090 <entry>traditional style</entry>
2091 <entry>12/17/1997 07:37:16.00 PST</entry>
2094 <entry>POSTGRES</entry>
2095 <entry>original style</entry>
2096 <entry>Wed Dec 17 07:37:16 1997 PST</entry>
2099 <entry>German</entry>
2100 <entry>regional style</entry>
2101 <entry>17.12.1997 07:37:16.00 PST</entry>
2108 In the <acronym>SQL</acronym> and POSTGRES styles, day appears before
2109 month if DMY field ordering has been specified, otherwise month appears
2111 (See <xref linkend="datatype-datetime-input">
2112 for how this setting also affects interpretation of input values.)
2113 <xref linkend="datatype-datetime-output2-table"> shows an
2117 <table id="datatype-datetime-output2-table">
2118 <title>Date Order Conventions</title>
2122 <entry><varname>datestyle</varname> Setting</entry>
2123 <entry>Input Ordering</entry>
2124 <entry>Example Output</entry>
2129 <entry><literal>SQL, DMY</></entry>
2130 <entry><replaceable>day</replaceable>/<replaceable>month</replaceable>/<replaceable>year</replaceable></entry>
2131 <entry>17/12/1997 15:37:16.00 CET</entry>
2134 <entry><literal>SQL, MDY</></entry>
2135 <entry><replaceable>month</replaceable>/<replaceable>day</replaceable>/<replaceable>year</replaceable></entry>
2136 <entry>12/17/1997 07:37:16.00 PST</entry>
2139 <entry><literal>Postgres, DMY</></entry>
2140 <entry><replaceable>day</replaceable>/<replaceable>month</replaceable>/<replaceable>year</replaceable></entry>
2141 <entry>Wed 17 Dec 07:37:16 1997 PST</entry>
2148 <type>interval</type> output looks like the input format, except
2149 that units like <literal>century</literal> or
2150 <literal>week</literal> are converted to years and days and
2151 <literal>ago</literal> is converted to an appropriate sign. In
2152 ISO mode the output looks like:
2155 <optional> <replaceable>quantity</> <replaceable>unit</> <optional> ... </> </> <optional> <replaceable>days</> </> <optional> <replaceable>hours</>:<replaceable>minutes</>:<replaceable>seconds</> </optional>
2160 The date/time styles can be selected by the user using the
2161 <command>SET datestyle</command> command, the <xref
2162 linkend="guc-datestyle"> parameter in the
2163 <filename>postgresql.conf</filename> configuration file, or the
2164 <envar>PGDATESTYLE</envar> environment variable on the server or
2165 client. The formatting function <function>to_char</function>
2166 (see <xref linkend="functions-formatting">) is also available as
2167 a more flexible way to format the date/time output.
2171 <sect2 id="datatype-timezones">
2172 <title>Time Zones</title>
2174 <indexterm zone="datatype-timezones">
2175 <primary>time zone</primary>
2179 Time zones, and time-zone conventions, are influenced by
2180 political decisions, not just earth geometry. Time zones around the
2181 world became somewhat standardized during the 1900's,
2182 but continue to be prone to arbitrary changes, particularly with
2183 respect to daylight-savings rules.
2184 <productname>PostgreSQL</productname> currently supports daylight-savings
2185 rules over the time period 1902 through 2038 (corresponding to the full
2186 range of conventional Unix system time). Times outside that range are
2187 taken to be in <quote>standard time</> for the selected time zone, no
2188 matter what part of the year they fall in.
2192 <productname>PostgreSQL</productname> endeavors to be compatible with
2193 the <acronym>SQL</acronym> standard definitions for typical usage.
2194 However, the <acronym>SQL</acronym> standard has an odd mix of date and
2195 time types and capabilities. Two obvious problems are:
2200 Although the <type>date</type> type
2201 does not have an associated time zone, the
2202 <type>time</type> type can.
2203 Time zones in the real world have little meaning unless
2204 associated with a date as well as a time,
2205 since the offset can vary through the year with daylight-saving
2212 The default time zone is specified as a constant numeric offset
2213 from <acronym>UTC</>. It is therefore not possible to adapt to
2214 daylight-saving time when doing date/time arithmetic across
2215 <acronym>DST</acronym> boundaries.
2223 To address these difficulties, we recommend using date/time types
2224 that contain both date and time when using time zones. We
2225 recommend <emphasis>not</emphasis> using the type <type>time with
2226 time zone</type> (though it is supported by
2227 <productname>PostgreSQL</productname> for legacy applications and
2228 for compliance with the <acronym>SQL</acronym> standard).
2229 <productname>PostgreSQL</productname> assumes
2230 your local time zone for any type containing only date or time.
2234 All timezone-aware dates and times are stored internally in
2235 <acronym>UTC</acronym>. They are converted to local time
2236 in the zone specified by the <xref linkend="guc-timezone"> configuration
2237 parameter before being displayed to the client.
2241 <productname>PostgreSQL</productname> allows you to specify time zones in
2242 three different forms:
2246 A full time zone name, for example <literal>America/New_York</>.
2247 The recognized time zone names are listed in the
2248 <literal>pg_timezone_names</literal> view (see <xref
2249 linkend="view-pg-timezone-names">).
2250 <productname>PostgreSQL</productname> uses the widely-used
2251 <literal>zic</> time zone data for this purpose, so the same
2252 names are also recognized by much other software.
2257 A time zone abbreviation, for example <literal>PST</>. Such a
2258 specification merely defines a particular offset from UTC, in
2259 contrast to full time zone names which might imply a set of daylight
2260 savings transition-date rules as well. The recognized abbreviations
2261 are listed in the <literal>pg_timezone_abbrevs</> view (see <xref
2262 linkend="view-pg-timezone-abbrevs">). You cannot set the
2263 configuration parameters <xref linkend="guc-timezone"> or
2264 <xref linkend="guc-log-timezone"> using a time
2265 zone abbreviation, but you can use abbreviations in
2266 date/time input values and with the <literal>AT TIME ZONE</>
2272 In addition to the timezone names and abbreviations,
2273 <productname>PostgreSQL</productname> will accept POSIX-style time zone
2274 specifications of the form <replaceable>STD</><replaceable>offset</> or
2275 <replaceable>STD</><replaceable>offset</><replaceable>DST</>, where
2276 <replaceable>STD</> is a zone abbreviation, <replaceable>offset</> is a
2277 numeric offset in hours west from UTC, and <replaceable>DST</> is an
2278 optional daylight-savings zone abbreviation, assumed to stand for one
2279 hour ahead of the given offset. For example, if <literal>EST5EDT</>
2280 were not already a recognized zone name, it would be accepted and would
2281 be functionally equivalent to USA East Coast time. When a
2282 daylight-savings zone name is present, it is assumed to be used
2283 according to the same daylight-savings transition rules used in the
2284 <literal>zic</> time zone database's <filename>posixrules</> entry.
2285 In a standard <productname>PostgreSQL</productname> installation,
2286 <filename>posixrules</> is the same as <literal>US/Eastern</>, so
2287 that POSIX-style time zone specifications follow USA daylight-savings
2288 rules. If needed, you can adjust this behavior by replacing the
2289 <filename>posixrules</> file.
2294 There is a conceptual and practical difference between the abbreviations
2295 and the full names: abbreviations always represent a fixed offset from
2296 UTC, whereas most of the full names imply a local daylight-savings time
2297 rule and so have two possible UTC offsets.
2301 One should be wary that the POSIX-style time zone feature can
2302 lead to silently accepting bogus input, since there is no check on the
2303 reasonableness of the zone abbreviations. For example, <literal>SET
2304 TIMEZONE TO FOOBAR0</> will work, leaving the system effectively using
2305 a rather peculiar abbreviation for UTC.
2306 Another issue to keep in mind is that in POSIX time zone names,
2307 positive offsets are used for locations <emphasis>west</> of Greenwich.
2308 Everywhere else, <productname>PostgreSQL</productname> follows the
2309 ISO-8601 convention that positive timezone offsets are <emphasis>east</>
2314 In all cases, timezone names are recognized case-insensitively.
2315 (This is a change from <productname>PostgreSQL</productname> versions
2316 prior to 8.2, which were case-sensitive in some contexts and not others.)
2320 Neither full names nor abbreviations are hard-wired into the server;
2321 they are obtained from configuration files stored under
2322 <filename>.../share/timezone/</> and <filename>.../share/timezonesets/</>
2323 of the installation directory
2324 (see <xref linkend="datetime-config-files">).
2328 The <xref linkend="guc-timezone"> configuration parameter can
2329 be set in the file <filename>postgresql.conf</>, or in any of the
2330 other standard ways described in <xref linkend="runtime-config">.
2331 There are also several special ways to set it:
2336 If <varname>timezone</> is not specified in
2337 <filename>postgresql.conf</> nor as a server command-line option,
2338 the server attempts to use the value of the <envar>TZ</envar>
2339 environment variable as the default time zone. If <envar>TZ</envar>
2340 is not defined or is not any of the time zone names known to
2341 <productname>PostgreSQL</productname>, the server attempts to
2342 determine the operating system's default time zone by checking the
2343 behavior of the C library function <literal>localtime()</>. The
2344 default time zone is selected as the closest match among
2345 <productname>PostgreSQL</productname>'s known time zones.
2346 (These rules are also used to choose the default value of
2347 <xref linkend="guc-log-timezone">, if it is not specified.)
2353 The <acronym>SQL</acronym> command <command>SET TIME ZONE</command>
2354 sets the time zone for the session. This is an alternative spelling
2355 of <command>SET TIMEZONE TO</> with a more SQL-spec-compatible syntax.
2361 The <envar>PGTZ</envar> environment variable, if set at the
2362 client, is used by <application>libpq</application>
2363 applications to send a <command>SET TIME ZONE</command>
2364 command to the server upon connection.
2371 <sect2 id="datatype-datetime-internals">
2372 <title>Internals</title>
2375 <productname>PostgreSQL</productname> uses Julian dates
2376 for all date/time calculations. They have the nice property of correctly
2377 predicting/calculating any date more recent than 4713 BC
2378 to far into the future, using the assumption that the length of the
2379 year is 365.2425 days.
2383 Date conventions before the 19th century make for interesting reading,
2384 but are not consistent enough to warrant coding into a date/time handler.
2390 <sect1 id="datatype-boolean">
2391 <title>Boolean Type</title>
2393 <indexterm zone="datatype-boolean">
2394 <primary>Boolean</primary>
2395 <secondary>data type</secondary>
2398 <indexterm zone="datatype-boolean">
2399 <primary>true</primary>
2402 <indexterm zone="datatype-boolean">
2403 <primary>false</primary>
2407 <productname>PostgreSQL</productname> provides the
2408 standard <acronym>SQL</acronym> type <type>boolean</type>.
2409 <type>boolean</type> can have one of only two states:
2410 <quote>true</quote> or <quote>false</quote>. A third state,
2411 <quote>unknown</quote>, is represented by the
2412 <acronym>SQL</acronym> null value.
2416 Valid literal values for the <quote>true</quote> state are:
2418 <member><literal>TRUE</literal></member>
2419 <member><literal>'t'</literal></member>
2420 <member><literal>'true'</literal></member>
2421 <member><literal>'y'</literal></member>
2422 <member><literal>'yes'</literal></member>
2423 <member><literal>'1'</literal></member>
2425 For the <quote>false</quote> state, the following values can be
2428 <member><literal>FALSE</literal></member>
2429 <member><literal>'f'</literal></member>
2430 <member><literal>'false'</literal></member>
2431 <member><literal>'n'</literal></member>
2432 <member><literal>'no'</literal></member>
2433 <member><literal>'0'</literal></member>
2435 Leading and trailing whitespace is ignored. Using the key words
2436 <literal>TRUE</literal> and <literal>FALSE</literal> is preferred
2437 (and <acronym>SQL</acronym>-compliant).
2440 <example id="datatype-boolean-example">
2441 <title>Using the <type>boolean</type> type</title>
2444 CREATE TABLE test1 (a boolean, b text);
2445 INSERT INTO test1 VALUES (TRUE, 'sic est');
2446 INSERT INTO test1 VALUES (FALSE, 'non est');
2447 SELECT * FROM test1;
2453 SELECT * FROM test1 WHERE a;
2461 <xref linkend="datatype-boolean-example"> shows that
2462 <type>boolean</type> values are output using the letters
2463 <literal>t</literal> and <literal>f</literal>.
2467 <type>boolean</type> uses 1 byte of storage.
2471 <sect1 id="datatype-enum">
2472 <title>Enumerated Types</title>
2474 <indexterm zone="datatype-enum">
2475 <primary>data type</primary>
2476 <secondary>enumerated (enum)</secondary>
2480 Enumerated (enum) types are data types that
2481 are comprised of a static, predefined set of values with a
2482 specific order. They are equivalent to the <type>enum</type>
2483 types in a number of programming languages. An example of an enum
2484 type might be the days of the week, or a set of status values for
2489 <title>Declaration of Enumerated Types</title>
2492 Enum types are created using the <xref
2493 linkend="sql-createtype" endterm="sql-createtype-title"> command,
2497 CREATE TYPE mood AS ENUM ('sad', 'ok', 'happy');
2500 Once created, the enum type can be used in table and function
2501 definitions much like any other type:
2505 <title>Basic Enum Usage</title>
2507 CREATE TYPE mood AS ENUM ('sad', 'ok', 'happy');
2508 CREATE TABLE person (
2512 INSERT INTO person VALUES ('Moe', 'happy');
2513 SELECT * FROM person WHERE current_mood = 'happy';
2515 ------+--------------
2523 <title>Ordering</title>
2526 The ordering of the values in an enum type is the
2527 order in which the values were listed when the type was declared.
2528 All standard comparison operators and related
2529 aggregate functions are supported for enums. For example:
2533 <title>Enum Ordering</title>
2535 INSERT INTO person VALUES ('Larry', 'sad');
2536 INSERT INTO person VALUES ('Curly', 'ok');
2537 SELECT * FROM person WHERE current_mood > 'sad';
2539 -------+--------------
2544 SELECT * FROM person WHERE current_mood > 'sad' ORDER BY current_mood;
2546 -------+--------------
2551 SELECT name FROM person
2552 WHERE current_mood = (SELECT MIN(current_mood) FROM person);
2562 <title>Type Safety</title>
2565 Enumerated types are completely separate data types and may not
2566 be compared with each other.
2570 <title>Lack of Casting</title>
2572 CREATE TYPE happiness AS ENUM ('happy', 'very happy', 'ecstatic');
2573 CREATE TABLE holidays (
2577 INSERT INTO holidays(num_weeks,happiness) VALUES (4, 'happy');
2578 INSERT INTO holidays(num_weeks,happiness) VALUES (6, 'very happy');
2579 INSERT INTO holidays(num_weeks,happiness) VALUES (8, 'ecstatic');
2580 INSERT INTO holidays(num_weeks,happiness) VALUES (2, 'sad');
2581 ERROR: invalid input value for enum happiness: "sad"
2582 SELECT person.name, holidays.num_weeks FROM person, holidays
2583 WHERE person.current_mood = holidays.happiness;
2584 ERROR: operator does not exist: mood = happiness
2589 If you really need to do something like that, you can either
2590 write a custom operator or add explicit casts to your query:
2594 <title>Comparing Different Enums by Casting to Text</title>
2596 SELECT person.name, holidays.num_weeks FROM person, holidays
2597 WHERE person.current_mood::text = holidays.happiness::text;
2608 <title>Implementation Details</title>
2611 An enum value occupies four bytes on disk. The length of an enum
2612 value's textual label is limited by the <symbol>NAMEDATALEN</symbol>
2613 setting compiled into <productname>PostgreSQL</productname>; in standard
2614 builds this means at most 63 bytes.
2618 Enum labels are case sensitive, so
2619 <type>'happy'</type> is not the same as <type>'HAPPY'</type>.
2620 Spaces in the labels are significant, too.
2626 <sect1 id="datatype-geometric">
2627 <title>Geometric Types</title>
2630 Geometric data types represent two-dimensional spatial
2631 objects. <xref linkend="datatype-geo-table"> shows the geometric
2632 types available in <productname>PostgreSQL</productname>. The
2633 most fundamental type, the point, forms the basis for all of the
2637 <table id="datatype-geo-table">
2638 <title>Geometric Types</title>
2643 <entry>Storage Size</entry>
2644 <entry>Representation</entry>
2645 <entry>Description</entry>
2650 <entry><type>point</type></entry>
2651 <entry>16 bytes</entry>
2652 <entry>Point on the plane</entry>
2653 <entry>(x,y)</entry>
2656 <entry><type>line</type></entry>
2657 <entry>32 bytes</entry>
2658 <entry>Infinite line (not fully implemented)</entry>
2659 <entry>((x1,y1),(x2,y2))</entry>
2662 <entry><type>lseg</type></entry>
2663 <entry>32 bytes</entry>
2664 <entry>Finite line segment</entry>
2665 <entry>((x1,y1),(x2,y2))</entry>
2668 <entry><type>box</type></entry>
2669 <entry>32 bytes</entry>
2670 <entry>Rectangular box</entry>
2671 <entry>((x1,y1),(x2,y2))</entry>
2674 <entry><type>path</type></entry>
2675 <entry>16+16n bytes</entry>
2676 <entry>Closed path (similar to polygon)</entry>
2677 <entry>((x1,y1),...)</entry>
2680 <entry><type>path</type></entry>
2681 <entry>16+16n bytes</entry>
2682 <entry>Open path</entry>
2683 <entry>[(x1,y1),...]</entry>
2686 <entry><type>polygon</type></entry>
2687 <entry>40+16n bytes</entry>
2688 <entry>Polygon (similar to closed path)</entry>
2689 <entry>((x1,y1),...)</entry>
2692 <entry><type>circle</type></entry>
2693 <entry>24 bytes</entry>
2694 <entry>Circle</entry>
2695 <entry><(x,y),r> (center and radius)</entry>
2702 A rich set of functions and operators is available to perform various geometric
2703 operations such as scaling, translation, rotation, and determining
2704 intersections. They are explained in <xref linkend="functions-geometry">.
2708 <title>Points</title>
2711 <primary>point</primary>
2715 Points are the fundamental two-dimensional building block for geometric types.
2716 Values of type <type>point</type> are specified using the following syntax:
2719 ( <replaceable>x</replaceable> , <replaceable>y</replaceable> )
2720 <replaceable>x</replaceable> , <replaceable>y</replaceable>
2723 where <replaceable>x</> and <replaceable>y</> are the respective
2724 coordinates as floating-point numbers.
2729 <title>Line Segments</title>
2732 <primary>lseg</primary>
2736 <primary>line segment</primary>
2740 Line segments (<type>lseg</type>) are represented by pairs of points.
2741 Values of type <type>lseg</type> are specified using the following syntax:
2744 ( ( <replaceable>x1</replaceable> , <replaceable>y1</replaceable> ) , ( <replaceable>x2</replaceable> , <replaceable>y2</replaceable> ) )
2745 ( <replaceable>x1</replaceable> , <replaceable>y1</replaceable> ) , ( <replaceable>x2</replaceable> , <replaceable>y2</replaceable> )
2746 <replaceable>x1</replaceable> , <replaceable>y1</replaceable> , <replaceable>x2</replaceable> , <replaceable>y2</replaceable>
2750 <literal>(<replaceable>x1</replaceable>,<replaceable>y1</replaceable>)</literal>
2752 <literal>(<replaceable>x2</replaceable>,<replaceable>y2</replaceable>)</literal>
2753 are the end points of the line segment.
2758 <title>Boxes</title>
2761 <primary>box (data type)</primary>
2765 <primary>rectangle</primary>
2769 Boxes are represented by pairs of points that are opposite
2771 Values of type <type>box</type> are specified using the following syntax:
2774 ( ( <replaceable>x1</replaceable> , <replaceable>y1</replaceable> ) , ( <replaceable>x2</replaceable> , <replaceable>y2</replaceable> ) )
2775 ( <replaceable>x1</replaceable> , <replaceable>y1</replaceable> ) , ( <replaceable>x2</replaceable> , <replaceable>y2</replaceable> )
2776 <replaceable>x1</replaceable> , <replaceable>y1</replaceable> , <replaceable>x2</replaceable> , <replaceable>y2</replaceable>
2780 <literal>(<replaceable>x1</replaceable>,<replaceable>y1</replaceable>)</literal>
2782 <literal>(<replaceable>x2</replaceable>,<replaceable>y2</replaceable>)</literal>
2783 are any two opposite corners of the box.
2787 Boxes are output using the first syntax.
2788 The corners are reordered on input to store
2789 the upper right corner, then the lower left corner.
2790 Other corners of the box can be entered, but the lower
2791 left and upper right corners are determined from the input and stored.
2796 <title>Paths</title>
2799 <primary>path (data type)</primary>
2803 Paths are represented by lists of connected points. Paths can be
2804 <firstterm>open</firstterm>, where
2805 the first and last points in the list are not considered connected, or
2806 <firstterm>closed</firstterm>,
2807 where the first and last points are considered connected.
2811 Values of type <type>path</type> are specified using the following syntax:
2814 ( ( <replaceable>x1</replaceable> , <replaceable>y1</replaceable> ) , ... , ( <replaceable>xn</replaceable> , <replaceable>yn</replaceable> ) )
2815 [ ( <replaceable>x1</replaceable> , <replaceable>y1</replaceable> ) , ... , ( <replaceable>xn</replaceable> , <replaceable>yn</replaceable> ) ]
2816 ( <replaceable>x1</replaceable> , <replaceable>y1</replaceable> ) , ... , ( <replaceable>xn</replaceable> , <replaceable>yn</replaceable> )
2817 ( <replaceable>x1</replaceable> , <replaceable>y1</replaceable> , ... , <replaceable>xn</replaceable> , <replaceable>yn</replaceable> )
2818 <replaceable>x1</replaceable> , <replaceable>y1</replaceable> , ... , <replaceable>xn</replaceable> , <replaceable>yn</replaceable>
2821 where the points are the end points of the line segments
2822 comprising the path. Square brackets (<literal>[]</>) indicate
2823 an open path, while parentheses (<literal>()</>) indicate a
2828 Paths are output using the first syntax.
2833 <title>Polygons</title>
2836 <primary>polygon</primary>
2840 Polygons are represented by lists of points (the vertexes of the
2841 polygon). Polygons should probably be
2842 considered equivalent to closed paths, but are stored differently
2843 and have their own set of support routines.
2847 Values of type <type>polygon</type> are specified using the following syntax:
2850 ( ( <replaceable>x1</replaceable> , <replaceable>y1</replaceable> ) , ... , ( <replaceable>xn</replaceable> , <replaceable>yn</replaceable> ) )
2851 ( <replaceable>x1</replaceable> , <replaceable>y1</replaceable> ) , ... , ( <replaceable>xn</replaceable> , <replaceable>yn</replaceable> )
2852 ( <replaceable>x1</replaceable> , <replaceable>y1</replaceable> , ... , <replaceable>xn</replaceable> , <replaceable>yn</replaceable> )
2853 <replaceable>x1</replaceable> , <replaceable>y1</replaceable> , ... , <replaceable>xn</replaceable> , <replaceable>yn</replaceable>
2856 where the points are the end points of the line segments
2857 comprising the boundary of the polygon.
2861 Polygons are output using the first syntax.
2866 <title>Circles</title>
2869 <primary>circle</primary>
2873 Circles are represented by a center point and a radius.
2874 Values of type <type>circle</type> are specified using the following syntax:
2877 < ( <replaceable>x</replaceable> , <replaceable>y</replaceable> ) , <replaceable>r</replaceable> >
2878 ( ( <replaceable>x</replaceable> , <replaceable>y</replaceable> ) , <replaceable>r</replaceable> )
2879 ( <replaceable>x</replaceable> , <replaceable>y</replaceable> ) , <replaceable>r</replaceable>
2880 <replaceable>x</replaceable> , <replaceable>y</replaceable> , <replaceable>r</replaceable>
2884 <literal>(<replaceable>x</replaceable>,<replaceable>y</replaceable>)</literal>
2885 is the center and <replaceable>r</replaceable> is the radius of the circle.
2889 Circles are output using the first syntax.
2895 <sect1 id="datatype-net-types">
2896 <title>Network Address Types</title>
2898 <indexterm zone="datatype-net-types">
2899 <primary>network</primary>
2900 <secondary>data types</secondary>
2904 <productname>PostgreSQL</> offers data types to store IPv4, IPv6, and MAC
2905 addresses, as shown in <xref linkend="datatype-net-types-table">. It
2906 is preferable to use these types instead of plain text types to store
2907 network addresses, because
2908 these types offer input error checking and several specialized
2909 operators and functions (see <xref linkend="functions-net">).
2912 <table tocentry="1" id="datatype-net-types-table">
2913 <title>Network Address Types</title>
2918 <entry>Storage Size</entry>
2919 <entry>Description</entry>
2925 <entry><type>cidr</type></entry>
2926 <entry>7 or 19 bytes</entry>
2927 <entry>IPv4 and IPv6 networks</entry>
2931 <entry><type>inet</type></entry>
2932 <entry>7 or 19 bytes</entry>
2933 <entry>IPv4 and IPv6 hosts and networks</entry>
2937 <entry><type>macaddr</type></entry>
2938 <entry>6 bytes</entry>
2939 <entry>MAC addresses</entry>
2947 When sorting <type>inet</type> or <type>cidr</type> data types,
2948 IPv4 addresses will always sort before IPv6 addresses, including
2949 IPv4 addresses encapsulated or mapped into IPv6 addresses, such as
2950 ::10.2.3.4 or ::ffff::10.4.3.2.
2954 <sect2 id="datatype-inet">
2955 <title><type>inet</type></title>
2958 <primary>inet (data type)</primary>
2962 The <type>inet</type> type holds an IPv4 or IPv6 host address, and
2963 optionally the identity of the subnet it is in, all in one field.
2964 The subnet identity is represented by stating how many bits of
2965 the host address represent the network address (the
2966 <quote>netmask</quote>). If the netmask is 32 and the address is IPv4,
2967 then the value does not indicate a subnet, only a single host.
2968 In IPv6, the address length is 128 bits, so 128 bits specify a
2969 unique host address. Note that if you
2970 want to accept networks only, you should use the
2971 <type>cidr</type> type rather than <type>inet</type>.
2975 The input format for this type is
2976 <replaceable class="parameter">address/y</replaceable>
2978 <replaceable class="parameter">address</replaceable>
2979 is an IPv4 or IPv6 address and
2980 <replaceable class="parameter">y</replaceable>
2981 is the number of bits in the netmask. If the
2982 <replaceable class="parameter">/y</replaceable>
2983 part is left off, then the
2984 netmask is 32 for IPv4 and 128 for IPv6, so the value represents
2985 just a single host. On display, the
2986 <replaceable class="parameter">/y</replaceable>
2987 portion is suppressed if the netmask specifies a single host.
2991 <sect2 id="datatype-cidr">
2992 <title><type>cidr</></title>
2995 <primary>cidr</primary>
2999 The <type>cidr</type> type holds an IPv4 or IPv6 network specification.
3000 Input and output formats follow Classless Internet Domain Routing
3002 The format for specifying networks is <replaceable
3003 class="parameter">address/y</> where <replaceable
3004 class="parameter">address</> is the network represented as an
3005 IPv4 or IPv6 address, and <replaceable
3006 class="parameter">y</> is the number of bits in the netmask. If
3007 <replaceable class="parameter">y</> is omitted, it is calculated
3008 using assumptions from the older classful network numbering system, except
3009 that it will be at least large enough to include all of the octets
3010 written in the input. It is an error to specify a network address
3011 that has bits set to the right of the specified netmask.
3015 <xref linkend="datatype-net-cidr-table"> shows some examples.
3018 <table id="datatype-net-cidr-table">
3019 <title><type>cidr</> Type Input Examples</title>
3023 <entry><type>cidr</type> Input</entry>
3024 <entry><type>cidr</type> Output</entry>
3025 <entry><literal><function>abbrev</function>(<type>cidr</type>)</literal></entry>
3030 <entry>192.168.100.128/25</entry>
3031 <entry>192.168.100.128/25</entry>
3032 <entry>192.168.100.128/25</entry>
3035 <entry>192.168/24</entry>
3036 <entry>192.168.0.0/24</entry>
3037 <entry>192.168.0/24</entry>
3040 <entry>192.168/25</entry>
3041 <entry>192.168.0.0/25</entry>
3042 <entry>192.168.0.0/25</entry>
3045 <entry>192.168.1</entry>
3046 <entry>192.168.1.0/24</entry>
3047 <entry>192.168.1/24</entry>
3050 <entry>192.168</entry>
3051 <entry>192.168.0.0/24</entry>
3052 <entry>192.168.0/24</entry>
3055 <entry>128.1</entry>
3056 <entry>128.1.0.0/16</entry>
3057 <entry>128.1/16</entry>
3061 <entry>128.0.0.0/16</entry>
3062 <entry>128.0/16</entry>
3065 <entry>128.1.2</entry>
3066 <entry>128.1.2.0/24</entry>
3067 <entry>128.1.2/24</entry>
3070 <entry>10.1.2</entry>
3071 <entry>10.1.2.0/24</entry>
3072 <entry>10.1.2/24</entry>
3076 <entry>10.1.0.0/16</entry>
3077 <entry>10.1/16</entry>
3081 <entry>10.0.0.0/8</entry>
3085 <entry>10.1.2.3/32</entry>
3086 <entry>10.1.2.3/32</entry>
3087 <entry>10.1.2.3/32</entry>
3090 <entry>2001:4f8:3:ba::/64</entry>
3091 <entry>2001:4f8:3:ba::/64</entry>
3092 <entry>2001:4f8:3:ba::/64</entry>
3095 <entry>2001:4f8:3:ba:2e0:81ff:fe22:d1f1/128</entry>
3096 <entry>2001:4f8:3:ba:2e0:81ff:fe22:d1f1/128</entry>
3097 <entry>2001:4f8:3:ba:2e0:81ff:fe22:d1f1</entry>
3100 <entry>::ffff:1.2.3.0/120</entry>
3101 <entry>::ffff:1.2.3.0/120</entry>
3102 <entry>::ffff:1.2.3/120</entry>
3105 <entry>::ffff:1.2.3.0/128</entry>
3106 <entry>::ffff:1.2.3.0/128</entry>
3107 <entry>::ffff:1.2.3.0/128</entry>
3114 <sect2 id="datatype-inet-vs-cidr">
3115 <title><type>inet</type> vs. <type>cidr</type></title>
3118 The essential difference between <type>inet</type> and <type>cidr</type>
3119 data types is that <type>inet</type> accepts values with nonzero bits to
3120 the right of the netmask, whereas <type>cidr</type> does not.
3125 If you do not like the output format for <type>inet</type> or
3126 <type>cidr</type> values, try the functions <function>host</>,
3127 <function>text</>, and <function>abbrev</>.
3132 <sect2 id="datatype-macaddr">
3133 <title><type>macaddr</></>
3136 <primary>macaddr (data type)</primary>
3140 <primary>MAC address</primary>
3145 The <type>macaddr</> type stores MAC addresses, i.e., Ethernet
3146 card hardware addresses (although MAC addresses are used for
3147 other purposes as well). Input is accepted in various customary
3151 <member><literal>'08002b:010203'</></member>
3152 <member><literal>'08002b-010203'</></member>
3153 <member><literal>'0800.2b01.0203'</></member>
3154 <member><literal>'08-00-2b-01-02-03'</></member>
3155 <member><literal>'08:00:2b:01:02:03'</></member>
3158 which would all specify the same
3159 address. Upper and lower case is accepted for the digits
3160 <literal>a</> through <literal>f</>. Output is always in the
3161 last of the forms shown.
3167 <sect1 id="datatype-bit">
3168 <title>Bit String Types</title>
3170 <indexterm zone="datatype-bit">
3171 <primary>bit string</primary>
3172 <secondary>data type</secondary>
3176 Bit strings are strings of 1's and 0's. They can be used to store
3177 or visualize bit masks. There are two SQL bit types:
3178 <type>bit(<replaceable>n</replaceable>)</type> and <type>bit
3179 varying(<replaceable>n</replaceable>)</type>, where
3180 <replaceable>n</replaceable> is a positive integer.
3184 <type>bit</type> type data must match the length
3185 <replaceable>n</replaceable> exactly; it is an error to attempt to
3186 store shorter or longer bit strings. <type>bit varying</type> data is
3187 of variable length up to the maximum length
3188 <replaceable>n</replaceable>; longer strings will be rejected.
3189 Writing <type>bit</type> without a length is equivalent to
3190 <literal>bit(1)</literal>, while <type>bit varying</type> without a length
3191 specification means unlimited length.
3196 If one explicitly casts a bit-string value to
3197 <type>bit(<replaceable>n</>)</type>, it will be truncated or
3198 zero-padded on the right to be exactly <replaceable>n</> bits,
3199 without raising an error. Similarly,
3200 if one explicitly casts a bit-string value to
3201 <type>bit varying(<replaceable>n</>)</type>, it will be truncated
3202 on the right if it is more than <replaceable>n</> bits.
3208 linkend="sql-syntax-bit-strings"> for information about the syntax
3209 of bit string constants. Bit-logical operators and string
3210 manipulation functions are available; see <xref
3211 linkend="functions-bitstring">.
3215 <title>Using the bit string types</title>
3218 CREATE TABLE test (a BIT(3), b BIT VARYING(5));
3219 INSERT INTO test VALUES (B'101', B'00');
3220 INSERT INTO test VALUES (B'10', B'101');
3222 ERROR: bit string length 2 does not match type bit(3)
3224 INSERT INTO test VALUES (B'10'::bit(3), B'101');
3236 A bit string value requires 1 byte for each group of 8 bits, plus
3237 5 or 8 bytes overhead depending on the length of the string
3238 (but long values may be compressed or moved out-of-line, as explained
3239 in <xref linkend="datatype-character"> for character strings).
3243 <sect1 id="datatype-textsearch">
3244 <title>Text Search Types</title>
3246 <indexterm zone="datatype-textsearch">
3247 <primary>full text search</primary>
3248 <secondary>data types</secondary>
3251 <indexterm zone="datatype-textsearch">
3252 <primary>text search</primary>
3253 <secondary>data types</secondary>
3257 <productname>PostgreSQL</productname> provides two data types that
3258 are designed to support full text search, which is the activity of
3259 searching through a collection of natural-language <firstterm>documents</>
3260 to locate those that best match a <firstterm>query</>.
3261 The <type>tsvector</type> type represents a document in a form suited
3262 for text search, while the <type>tsquery</type> type similarly represents
3264 <xref linkend="textsearch"> provides a detailed explanation of this
3265 facility, and <xref linkend="functions-textsearch"> summarizes the
3266 related functions and operators.
3269 <sect2 id="datatype-tsvector">
3270 <title><type>tsvector</type></title>
3273 <primary>tsvector (data type)</primary>
3277 A <type>tsvector</type> value is a sorted list of distinct
3278 <firstterm>lexemes</>, which are words that have been
3279 <firstterm>normalized</> to make different variants of the same word look
3280 alike (see <xref linkend="textsearch"> for details). Sorting and
3281 duplicate-elimination are done automatically during input, as shown in
3285 SELECT 'a fat cat sat on a mat and ate a fat rat'::tsvector;
3287 ----------------------------------------------------
3288 'a' 'on' 'and' 'ate' 'cat' 'fat' 'mat' 'rat' 'sat'
3291 (As the example shows, the sorting is first by length and then
3292 alphabetically, but that detail is seldom important.) To represent
3293 lexemes containing whitespace or punctuation, surround them with quotes:
3296 SELECT $$the lexeme ' ' contains spaces$$::tsvector;
3298 -------------------------------------------
3299 'the' ' ' 'lexeme' 'spaces' 'contains'
3302 (We use dollar-quoted string literals in this example and the next one,
3303 to avoid confusing matters by having to double quote marks within the
3304 literals.) Embedded quotes and backslashes must be doubled:
3307 SELECT $$the lexeme 'Joe''s' contains a quote$$::tsvector;
3309 ------------------------------------------------
3310 'a' 'the' 'Joe''s' 'quote' 'lexeme' 'contains'
3313 Optionally, integer <firstterm>position(s)</>
3314 can be attached to any or all of the lexemes:
3317 SELECT 'a:1 fat:2 cat:3 sat:4 on:5 a:6 mat:7 and:8 ate:9 a:10 fat:11 rat:12'::tsvector;
3319 -------------------------------------------------------------------------------
3320 'a':1,6,10 'on':5 'and':8 'ate':9 'cat':3 'fat':2,11 'mat':7 'rat':12 'sat':4
3323 A position normally indicates the source word's location in the
3324 document. Positional information can be used for
3325 <firstterm>proximity ranking</firstterm>. Position values can
3326 range from 1 to 16383; larger numbers are silently clamped to 16383.
3327 Duplicate position entries are discarded.
3331 Lexemes that have positions can further be labeled with a
3332 <firstterm>weight</>, which can be <literal>A</literal>,
3333 <literal>B</literal>, <literal>C</literal>, or <literal>D</literal>.
3334 <literal>D</literal> is the default and hence is not shown on output:
3337 SELECT 'a:1A fat:2B,4C cat:5D'::tsvector;
3339 ----------------------------
3340 'a':1A 'cat':5 'fat':2B,4C
3343 Weights are typically used to reflect document structure, for example
3344 by marking title words differently from body words. Text search
3345 ranking functions can assign different priorities to the different
3350 It is important to understand that the
3351 <type>tsvector</type> type itself does not perform any normalization;
3352 it assumes that the words it is given are normalized appropriately
3353 for the application. For example,
3356 select 'The Fat Rats'::tsvector;
3358 --------------------
3362 For most English-text-searching applications the above words would
3363 be considered non-normalized, but <type>tsvector</type> doesn't care.
3364 Raw document text should usually be passed through
3365 <function>to_tsvector</> to normalize the words appropriately
3369 SELECT to_tsvector('english', 'The Fat Rats');
3375 Again, see <xref linkend="textsearch"> for more detail.
3380 <sect2 id="datatype-tsquery">
3381 <title><type>tsquery</type></title>
3384 <primary>tsquery (data type)</primary>
3388 A <type>tsquery</type> value stores lexemes that are to be
3389 searched for, and combines them using the boolean operators
3390 <literal>&</literal> (AND), <literal>|</literal> (OR), and
3391 <literal>!</> (NOT). Parentheses can be used to enforce grouping
3395 SELECT 'fat & rat'::tsquery;
3400 SELECT 'fat & (rat | cat)'::tsquery;
3402 ---------------------------
3403 'fat' & ( 'rat' | 'cat' )
3405 SELECT 'fat & rat & ! cat'::tsquery;
3407 ------------------------
3408 'fat' & 'rat' & !'cat'
3411 In the absence of parentheses, <literal>!</> (NOT) binds most tightly,
3412 and <literal>&</literal> (AND) binds more tightly than
3413 <literal>|</literal> (OR).
3417 Optionally, lexemes in a <type>tsquery</type> can be labeled with
3418 one or more weight letters, which restricts them to match only
3419 <type>tsvector</> lexemes with one of those weights:
3422 SELECT 'fat:ab & cat'::tsquery;
3425 'fat':AB & 'cat'
3430 Quoting rules for lexemes are the same as described above for
3431 lexemes in <type>tsvector</>; and, as with <type>tsvector</>,
3432 any required normalization of words must be done before putting
3433 them into the <type>tsquery</> type. The <function>to_tsquery</>
3434 function is convenient for performing such normalization:
3437 SELECT to_tsquery('Fat:ab & Cats');
3448 <sect1 id="datatype-uuid">
3449 <title><acronym>UUID</acronym> Type</title>
3451 <indexterm zone="datatype-uuid">
3452 <primary>UUID</primary>
3456 The data type <type>uuid</type> stores Universally Unique Identifiers
3457 (UUID) as defined by RFC 4122, ISO/IEC 9834-8:2005, and related standards.
3458 (Some systems refer to this data type as globally unique identifier, or
3459 GUID,<indexterm><primary>GUID</primary></indexterm> instead.) Such an
3460 identifier is a 128-bit quantity that is generated by an algorithm chosen
3461 to make it very unlikely that the same identifier will be generated by
3462 anyone else in the known universe using the same algorithm. Therefore,
3463 for distributed systems, these identifiers provide a better uniqueness
3464 guarantee than that which can be achieved using sequence generators, which
3465 are only unique within a single database.
3469 A UUID is written as a sequence of lower-case hexadecimal digits,
3470 in several groups separated by hyphens, specifically a group of 8
3471 digits followed by three groups of 4 digits followed by a group of
3472 12 digits, for a total of 32 digits representing the 128 bits. An
3473 example of a UUID in this standard form is:
3475 a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11
3477 <productname>PostgreSQL</productname> also accepts the following
3478 alternative forms for input:
3479 use of upper-case digits, the standard format surrounded by
3480 braces, and omitting the hyphens. Examples are:
3482 A0EEBC99-9C0B-4EF8-BB6D-6BB9BD380A11
3483 {a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11}
3484 a0eebc999c0b4ef8bb6d6bb9bd380a11
3486 Output is always in the standard form.
3490 <productname>PostgreSQL</productname> provides storage and comparison
3491 functions for UUIDs, but the core database does not include any
3492 function for generating UUIDs, because no single algorithm is well
3493 suited for every application. The contrib module
3494 <filename>contrib/uuid-ossp</filename> provides functions that implement
3495 several standard algorithms.
3496 Alternatively, UUIDs could be generated by client applications or
3497 other libraries invoked through a server-side function.
3501 <sect1 id="datatype-xml">
3502 <title><acronym>XML</> Type</title>
3504 <indexterm zone="datatype-xml">
3505 <primary>XML</primary>
3509 The data type <type>xml</type> can be used to store XML data. Its
3510 advantage over storing XML data in a <type>text</type> field is that it
3511 checks the input values for well-formedness, and there are support
3512 functions to perform type-safe operations on it; see <xref
3513 linkend="functions-xml">. Use of this data type requires the
3514 installation to have been built with <command>configure
3519 The <type>xml</type> type can store well-formed
3520 <quote>documents</quote>, as defined by the XML standard, as well
3521 as <quote>content</quote> fragments, which are defined by the
3522 production <literal>XMLDecl? content</literal> in the XML
3523 standard. Roughly, this means that content fragments can have
3524 more than one top-level element or character node. The expression
3525 <literal><replaceable>xmlvalue</replaceable> IS DOCUMENT</literal>
3526 can be used to evaluate whether a particular <type>xml</type>
3527 value is a full document or only a content fragment.
3531 <title>Creating XML Values</title>
3533 To produce a value of type <type>xml</type> from character data,
3535 <function>xmlparse</function>:<indexterm><primary>xmlparse</primary></indexterm>
3537 XMLPARSE ( { DOCUMENT | CONTENT } <replaceable>value</replaceable>)
3540 <programlisting><![CDATA[
3541 XMLPARSE (DOCUMENT '<?xml version="1.0"?><book><title>Manual</title><chapter>...</chapter><book>')
3542 XMLPARSE (CONTENT 'abc<foo>bar</foo><bar>foo</bar>')
3543 ]]></programlisting>
3544 While this is the only way to convert character strings into XML
3545 values according to the SQL standard, the PostgreSQL-specific
3547 <programlisting><![CDATA[
3548 xml '<foo>bar</foo>'
3549 '<foo>bar</foo>'::xml
3550 ]]></programlisting>
3555 The <type>xml</type> type does not validate its input values
3556 against a possibly included document type declaration
3557 (DTD).<indexterm><primary>DTD</primary></indexterm>
3561 The inverse operation, producing character string type values from
3562 <type>xml</type>, uses the function
3563 <function>xmlserialize</function>:<indexterm><primary>xmlserialize</primary></indexterm>
3565 XMLSERIALIZE ( { DOCUMENT | CONTENT } <replaceable>value</replaceable> AS <replaceable>type</replaceable> )
3567 <replaceable>type</replaceable> can be one of
3568 <type>character</type>, <type>character varying</type>, or
3569 <type>text</type> (or an alias name for those). Again, according
3570 to the SQL standard, this is the only way to convert between type
3571 <type>xml</type> and character types, but PostgreSQL also allows
3572 you to simply cast the value.
3576 When character string values are cast to or from type
3577 <type>xml</type> without going through <type>XMLPARSE</type> or
3578 <type>XMLSERIALIZE</type>, respectively, the choice of
3579 <literal>DOCUMENT</literal> versus <literal>CONTENT</literal> is
3580 determined by the <quote>XML option</quote>
3581 <indexterm><primary>XML option</primary></indexterm>
3582 session configuration parameter, which can be set using the
3585 SET XML OPTION { DOCUMENT | CONTENT };
3587 or the more PostgreSQL-like syntax
3589 SET xmloption TO { DOCUMENT | CONTENT };
3591 The default is <literal>CONTENT</literal>, so all forms of XML
3597 <title>Encoding Handling</title>
3599 Care must be taken when dealing with multiple character encodings
3600 on the client, server, and in the XML data passed through them.
3601 When using the text mode to pass queries to the server and query
3602 results to the client (which is the normal mode), PostgreSQL
3603 converts all character data passed between the client and the
3604 server and vice versa to the character encoding of the respective
3605 end; see <xref linkend="multibyte">. This includes string
3606 representations of XML values, such as in the above examples.
3607 This would ordinarily mean that encoding declarations contained in
3608 XML data might become invalid as the character data is converted
3609 to other encodings while travelling between client and server,
3610 while the embedded encoding declaration is not changed. To cope
3611 with this behavior, an encoding declaration contained in a
3612 character string presented for input to the <type>xml</type> type
3613 is <emphasis>ignored</emphasis>, and the content is always assumed
3614 to be in the current server encoding. Consequently, for correct
3615 processing, such character strings of XML data must be sent off
3616 from the client in the current client encoding. It is the
3617 responsibility of the client to either convert the document to the
3618 current client encoding before sending it off to the server or to
3619 adjust the client encoding appropriately. On output, values of
3620 type <type>xml</type> will not have an encoding declaration, and
3621 clients must assume that the data is in the current client
3626 When using the binary mode to pass query parameters to the server
3627 and query results back to the client, no character set conversion
3628 is performed, so the situation is different. In this case, an
3629 encoding declaration in the XML data will be observed, and if it
3630 is absent, the data will be assumed to be in UTF-8 (as required by
3631 the XML standard; note that PostgreSQL does not support UTF-16 at
3632 all). On output, data will have an encoding declaration
3633 specifying the client encoding, unless the client encoding is
3634 UTF-8, in which case it will be omitted.
3638 Needless to say, processing XML data with PostgreSQL will be less
3639 error-prone and more efficient if data encoding, client encoding,
3640 and server encoding are the same. Since XML data is internally
3641 processed in UTF-8, computations will be most efficient if the
3642 server encoding is also UTF-8.
3647 <title>Accessing XML Values</title>
3650 The <type>xml</type> data type is unusual in that it does not
3651 provide any comparison operators. This is because there is no
3652 well-defined and universally useful comparison algorithm for XML
3653 data. One consequence of this is that you cannot retrieve rows by
3654 comparing an <type>xml</type> column against a search value. XML
3655 values should therefore typically be accompanied by a separate key
3656 field such as an ID. An alternative solution for comparing XML
3657 values is to convert them to character strings first, but note
3658 that character string comparison has little to do with a useful
3659 XML comparison method.
3663 Since there are no comparison operators for the <type>xml</type>
3664 data type, it is not possible to create an index directly on a
3665 column of this type. If speedy searches in XML data are desired,
3666 possible workarounds would be casting the expression to a
3667 character string type and indexing that, or indexing an XPath
3668 expression. The actual query would of course have to be adjusted
3669 to search by the indexed expression.
3673 The text-search functionality in PostgreSQL could also be used to speed
3674 up full-document searches in XML data. The necessary
3675 preprocessing support is, however, not available in the PostgreSQL
3676 distribution in this release.
3685 <sect1 id="datatype-oid">
3686 <title>Object Identifier Types</title>
3688 <indexterm zone="datatype-oid">
3689 <primary>object identifier</primary>
3690 <secondary>data type</secondary>
3693 <indexterm zone="datatype-oid">
3694 <primary>oid</primary>
3697 <indexterm zone="datatype-oid">
3698 <primary>regproc</primary>
3701 <indexterm zone="datatype-oid">
3702 <primary>regprocedure</primary>
3705 <indexterm zone="datatype-oid">
3706 <primary>regoper</primary>
3709 <indexterm zone="datatype-oid">
3710 <primary>regoperator</primary>
3713 <indexterm zone="datatype-oid">
3714 <primary>regclass</primary>
3717 <indexterm zone="datatype-oid">
3718 <primary>regtype</primary>
3721 <indexterm zone="datatype-oid">
3722 <primary>regconfig</primary>
3725 <indexterm zone="datatype-oid">
3726 <primary>regdictionary</primary>
3729 <indexterm zone="datatype-oid">
3730 <primary>xid</primary>
3733 <indexterm zone="datatype-oid">
3734 <primary>cid</primary>
3737 <indexterm zone="datatype-oid">
3738 <primary>tid</primary>
3742 Object identifiers (OIDs) are used internally by
3743 <productname>PostgreSQL</productname> as primary keys for various
3744 system tables. OIDs are not added to user-created tables, unless
3745 <literal>WITH OIDS</literal> is specified when the table is
3746 created, or the <xref linkend="guc-default-with-oids">
3747 configuration variable is enabled. Type <type>oid</> represents
3748 an object identifier. There are also several alias types for
3749 <type>oid</>: <type>regproc</>, <type>regprocedure</>,
3750 <type>regoper</>, <type>regoperator</>, <type>regclass</>,
3751 <type>regtype</>, <type>regconfig</>, and <type>regdictionary</>.
3752 <xref linkend="datatype-oid-table"> shows an overview.
3756 The <type>oid</> type is currently implemented as an unsigned
3757 four-byte integer. Therefore, it is not large enough to provide
3758 database-wide uniqueness in large databases, or even in large
3759 individual tables. So, using a user-created table's OID column as
3760 a primary key is discouraged. OIDs are best used only for
3761 references to system tables.
3765 The <type>oid</> type itself has few operations beyond comparison.
3766 It can be cast to integer, however, and then manipulated using the
3767 standard integer operators. (Beware of possible
3768 signed-versus-unsigned confusion if you do this.)
3772 The OID alias types have no operations of their own except
3773 for specialized input and output routines. These routines are able
3774 to accept and display symbolic names for system objects, rather than
3775 the raw numeric value that type <type>oid</> would use. The alias
3776 types allow simplified lookup of OID values for objects. For example,
3777 to examine the <structname>pg_attribute</> rows related to a table
3778 <literal>mytable</>, one could write:
3780 SELECT * FROM pg_attribute WHERE attrelid = 'mytable'::regclass;
3784 SELECT * FROM pg_attribute
3785 WHERE attrelid = (SELECT oid FROM pg_class WHERE relname = 'mytable');
3787 While that doesn't look all that bad by itself, it's still oversimplified.
3788 A far more complicated sub-select would be needed to
3789 select the right OID if there are multiple tables named
3790 <literal>mytable</> in different schemas.
3791 The <type>regclass</> input converter handles the table lookup according
3792 to the schema path setting, and so it does the <quote>right thing</>
3793 automatically. Similarly, casting a table's OID to
3794 <type>regclass</> is handy for symbolic display of a numeric OID.
3797 <table id="datatype-oid-table">
3798 <title>Object Identifier Types</title>
3803 <entry>References</entry>
3804 <entry>Description</entry>
3805 <entry>Value Example</entry>
3812 <entry><type>oid</></entry>
3814 <entry>numeric object identifier</entry>
3815 <entry><literal>564182</></entry>
3819 <entry><type>regproc</></entry>
3820 <entry><structname>pg_proc</></entry>
3821 <entry>function name</entry>
3822 <entry><literal>sum</></entry>
3826 <entry><type>regprocedure</></entry>
3827 <entry><structname>pg_proc</></entry>
3828 <entry>function with argument types</entry>
3829 <entry><literal>sum(int4)</></entry>
3833 <entry><type>regoper</></entry>
3834 <entry><structname>pg_operator</></entry>
3835 <entry>operator name</entry>
3836 <entry><literal>+</></entry>
3840 <entry><type>regoperator</></entry>
3841 <entry><structname>pg_operator</></entry>
3842 <entry>operator with argument types</entry>
3843 <entry><literal>*(integer,integer)</> or <literal>-(NONE,integer)</></entry>
3847 <entry><type>regclass</></entry>
3848 <entry><structname>pg_class</></entry>
3849 <entry>relation name</entry>
3850 <entry><literal>pg_type</></entry>
3854 <entry><type>regtype</></entry>
3855 <entry><structname>pg_type</></entry>
3856 <entry>data type name</entry>
3857 <entry><literal>integer</></entry>
3861 <entry><type>regconfig</></entry>
3862 <entry><structname>pg_ts_config</></entry>
3863 <entry>text search configuration</entry>
3864 <entry><literal>english</></entry>
3868 <entry><type>regdictionary</></entry>
3869 <entry><structname>pg_ts_dict</></entry>
3870 <entry>text search dictionary</entry>
3871 <entry><literal>simple</></entry>
3878 All of the OID alias types accept schema-qualified names, and will
3879 display schema-qualified names on output if the object would not
3880 be found in the current search path without being qualified.
3881 The <type>regproc</> and <type>regoper</> alias types will only
3882 accept input names that are unique (not overloaded), so they are
3883 of limited use; for most uses <type>regprocedure</> or
3884 <type>regoperator</> is more appropriate. For <type>regoperator</>,
3885 unary operators are identified by writing <literal>NONE</> for the unused
3890 An additional property of the OID alias types is that if a
3891 constant of one of these types appears in a stored expression
3892 (such as a column default expression or view), it creates a dependency
3893 on the referenced object. For example, if a column has a default
3894 expression <literal>nextval('my_seq'::regclass)</>,
3895 <productname>PostgreSQL</productname>
3896 understands that the default expression depends on the sequence
3897 <literal>my_seq</>; the system will not let the sequence be dropped
3898 without first removing the default expression.
3902 Another identifier type used by the system is <type>xid</>, or transaction
3903 (abbreviated <abbrev>xact</>) identifier. This is the data type of the system columns
3904 <structfield>xmin</> and <structfield>xmax</>. Transaction identifiers are 32-bit quantities.
3908 A third identifier type used by the system is <type>cid</>, or
3909 command identifier. This is the data type of the system columns
3910 <structfield>cmin</> and <structfield>cmax</>. Command identifiers are also 32-bit quantities.
3914 A final identifier type used by the system is <type>tid</>, or tuple
3915 identifier (row identifier). This is the data type of the system column
3916 <structfield>ctid</>. A tuple ID is a pair
3917 (block number, tuple index within block) that identifies the
3918 physical location of the row within its table.
3922 (The system columns are further explained in <xref
3923 linkend="ddl-system-columns">.)
3927 <sect1 id="datatype-pseudo">
3928 <title>Pseudo-Types</title>
3930 <indexterm zone="datatype-pseudo">
3931 <primary>record</primary>
3934 <indexterm zone="datatype-pseudo">
3935 <primary>any</primary>
3938 <indexterm zone="datatype-pseudo">
3939 <primary>anyelement</primary>
3942 <indexterm zone="datatype-pseudo">
3943 <primary>anyarray</primary>
3946 <indexterm zone="datatype-pseudo">
3947 <primary>anynonarray</primary>
3950 <indexterm zone="datatype-pseudo">
3951 <primary>anyenum</primary>
3954 <indexterm zone="datatype-pseudo">
3955 <primary>void</primary>
3958 <indexterm zone="datatype-pseudo">
3959 <primary>trigger</primary>
3962 <indexterm zone="datatype-pseudo">
3963 <primary>language_handler</primary>
3966 <indexterm zone="datatype-pseudo">
3967 <primary>cstring</primary>
3970 <indexterm zone="datatype-pseudo">
3971 <primary>internal</primary>
3974 <indexterm zone="datatype-pseudo">
3975 <primary>opaque</primary>
3979 The <productname>PostgreSQL</productname> type system contains a
3980 number of special-purpose entries that are collectively called
3981 <firstterm>pseudo-types</>. A pseudo-type cannot be used as a
3982 column data type, but it can be used to declare a function's
3983 argument or result type. Each of the available pseudo-types is
3984 useful in situations where a function's behavior does not
3985 correspond to simply taking or returning a value of a specific
3986 <acronym>SQL</acronym> data type. <xref
3987 linkend="datatype-pseudotypes-table"> lists the existing
3991 <table id="datatype-pseudotypes-table">
3992 <title>Pseudo-Types</title>
3997 <entry>Description</entry>
4003 <entry><type>any</></entry>
4004 <entry>Indicates that a function accepts any input data type whatever.</entry>
4008 <entry><type>anyarray</></entry>
4009 <entry>Indicates that a function accepts any array data type
4010 (see <xref linkend="extend-types-polymorphic">).</entry>
4014 <entry><type>anyelement</></entry>
4015 <entry>Indicates that a function accepts any data type
4016 (see <xref linkend="extend-types-polymorphic">).</entry>
4020 <entry><type>anyenum</></entry>
4021 <entry>Indicates that a function accepts any enum data type
4022 (see <xref linkend="extend-types-polymorphic"> and
4023 <xref linkend="datatype-enum">).</entry>
4027 <entry><type>anynonarray</></entry>
4028 <entry>Indicates that a function accepts any non-array data type
4029 (see <xref linkend="extend-types-polymorphic">).</entry>
4033 <entry><type>cstring</></entry>
4034 <entry>Indicates that a function accepts or returns a null-terminated C string.</entry>
4038 <entry><type>internal</></entry>
4039 <entry>Indicates that a function accepts or returns a server-internal
4044 <entry><type>language_handler</></entry>
4045 <entry>A procedural language call handler is declared to return <type>language_handler</>.</entry>
4049 <entry><type>record</></entry>
4050 <entry>Identifies a function returning an unspecified row type.</entry>
4054 <entry><type>trigger</></entry>
4055 <entry>A trigger function is declared to return <type>trigger.</></entry>
4059 <entry><type>void</></entry>
4060 <entry>Indicates that a function returns no value.</entry>
4064 <entry><type>opaque</></entry>
4065 <entry>An obsolete type name that formerly served all the above purposes.</entry>
4072 Functions coded in C (whether built-in or dynamically loaded) can be
4073 declared to accept or return any of these pseudo data types. It is up to
4074 the function author to ensure that the function will behave safely
4075 when a pseudo-type is used as an argument type.
4079 Functions coded in procedural languages can use pseudo-types only as
4080 allowed by their implementation languages. At present the procedural
4081 languages all forbid use of a pseudo-type as argument type, and allow
4082 only <type>void</> and <type>record</> as a result type (plus
4083 <type>trigger</> when the function is used as a trigger). Some also
4084 support polymorphic functions using the types <type>anyarray</>,
4085 <type>anyelement</>, <type>anyenum</>, and <type>anynonarray</>.
4089 The <type>internal</> pseudo-type is used to declare functions
4090 that are meant only to be called internally by the database
4091 system, and not by direct invocation in a <acronym>SQL</acronym>
4092 query. If a function has at least one <type>internal</>-type
4093 argument then it cannot be called from <acronym>SQL</acronym>. To
4094 preserve the type safety of this restriction it is important to
4095 follow this coding rule: do not create any function that is
4096 declared to return <type>internal</> unless it has at least one
4097 <type>internal</> argument.