1 <!-- doc/src/sgml/datatype.sgml -->
3 <chapter id="datatype">
4 <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"> 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 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 on a 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 on a 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 (8 bytes)</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>fields</replaceable> ] [ (<replaceable>p</replaceable>) ]</type></entry>
136 <entry>time span</entry>
140 <entry><type>line</type></entry>
142 <entry>infinite line on a plane</entry>
146 <entry><type>lseg</type></entry>
148 <entry>line segment on a plane</entry>
152 <entry><type>macaddr</type></entry>
154 <entry>MAC (Media Access Control) 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 on a plane</entry>
178 <entry><type>point</type></entry>
180 <entry>geometric point on a plane</entry>
184 <entry><type>polygon</type></entry>
186 <entry>closed geometric path on a plane</entry>
190 <entry><type>real</type></entry>
191 <entry><type>float4</type></entry>
192 <entry>single precision floating-point number (4 bytes)</entry>
196 <entry><type>smallint</type></entry>
197 <entry><type>int2</type></entry>
198 <entry>signed two-byte integer</entry>
202 <entry><type>smallserial</type></entry>
203 <entry><type>serial2</type></entry>
204 <entry>autoincrementing two-byte integer</entry>
208 <entry><type>serial</type></entry>
209 <entry><type>serial4</type></entry>
210 <entry>autoincrementing four-byte integer</entry>
214 <entry><type>text</type></entry>
216 <entry>variable-length character string</entry>
220 <entry><type>time [ (<replaceable>p</replaceable>) ] [ without time zone ]</type></entry>
222 <entry>time of day (no time zone)</entry>
226 <entry><type>time [ (<replaceable>p</replaceable>) ] with time zone</type></entry>
227 <entry><type>timetz</type></entry>
228 <entry>time of day, including time zone</entry>
232 <entry><type>timestamp [ (<replaceable>p</replaceable>) ] [ without time zone ]</type></entry>
234 <entry>date and time (no time zone)</entry>
238 <entry><type>timestamp [ (<replaceable>p</replaceable>) ] with time zone</type></entry>
239 <entry><type>timestamptz</type></entry>
240 <entry>date and time, including time zone</entry>
244 <entry><type>tsquery</type></entry>
246 <entry>text search query</entry>
250 <entry><type>tsvector</type></entry>
252 <entry>text search document</entry>
256 <entry><type>txid_snapshot</type></entry>
258 <entry>user-level transaction ID snapshot</entry>
262 <entry><type>uuid</type></entry>
264 <entry>universally unique identifier</entry>
268 <entry><type>xml</type></entry>
270 <entry>XML data</entry>
274 <entry><type>json</type></entry>
276 <entry>JSON data</entry>
283 <title>Compatibility</title>
285 The following types (or spellings thereof) are specified by
286 <acronym>SQL</acronym>: <type>bigint</type>, <type>bit</type>, <type>bit
287 varying</type>, <type>boolean</type>, <type>char</type>,
288 <type>character varying</type>, <type>character</type>,
289 <type>varchar</type>, <type>date</type>, <type>double
290 precision</type>, <type>integer</type>, <type>interval</type>,
291 <type>numeric</type>, <type>decimal</type>, <type>real</type>,
292 <type>smallint</type>, <type>time</type> (with or without time zone),
293 <type>timestamp</type> (with or without time zone),
299 Each data type has an external representation determined by its input
300 and output functions. Many of the built-in types have
301 obvious external formats. However, several types are either unique
302 to <productname>PostgreSQL</productname>, such as geometric
303 paths, or have several possible formats, such as the date
305 Some of the input and output functions are not invertible, i.e.,
306 the result of an output function might lose accuracy when compared to
310 <sect1 id="datatype-numeric">
311 <title>Numeric Types</title>
313 <indexterm zone="datatype-numeric">
314 <primary>data type</primary>
315 <secondary>numeric</secondary>
319 Numeric types consist of two-, four-, and eight-byte integers,
320 four- and eight-byte floating-point numbers, and selectable-precision
321 decimals. <xref linkend="datatype-numeric-table"> lists the
325 <table id="datatype-numeric-table">
326 <title>Numeric Types</title>
331 <entry>Storage Size</entry>
332 <entry>Description</entry>
339 <entry><type>smallint</></entry>
340 <entry>2 bytes</entry>
341 <entry>small-range integer</entry>
342 <entry>-32768 to +32767</entry>
345 <entry><type>integer</></entry>
346 <entry>4 bytes</entry>
347 <entry>typical choice for integer</entry>
348 <entry>-2147483648 to +2147483647</entry>
351 <entry><type>bigint</></entry>
352 <entry>8 bytes</entry>
353 <entry>large-range integer</entry>
354 <entry>-9223372036854775808 to +9223372036854775807</entry>
358 <entry><type>decimal</></entry>
359 <entry>variable</entry>
360 <entry>user-specified precision, exact</entry>
361 <entry>up to 131072 digits before the decimal point; up to 16383 digits after the decimal point</entry>
364 <entry><type>numeric</></entry>
365 <entry>variable</entry>
366 <entry>user-specified precision, exact</entry>
367 <entry>up to 131072 digits before the decimal point; up to 16383 digits after the decimal point</entry>
371 <entry><type>real</></entry>
372 <entry>4 bytes</entry>
373 <entry>variable-precision, inexact</entry>
374 <entry>6 decimal digits precision</entry>
377 <entry><type>double precision</></entry>
378 <entry>8 bytes</entry>
379 <entry>variable-precision, inexact</entry>
380 <entry>15 decimal digits precision</entry>
384 <entry><type>smallserial</type></entry>
385 <entry>2 bytes</entry>
386 <entry>small autoincrementing integer</entry>
387 <entry>1 to 32767</entry>
391 <entry><type>serial</></entry>
392 <entry>4 bytes</entry>
393 <entry>autoincrementing integer</entry>
394 <entry>1 to 2147483647</entry>
398 <entry><type>bigserial</type></entry>
399 <entry>8 bytes</entry>
400 <entry>large autoincrementing integer</entry>
401 <entry>1 to 9223372036854775807</entry>
408 The syntax of constants for the numeric types is described in
409 <xref linkend="sql-syntax-constants">. The numeric types have a
410 full set of corresponding arithmetic operators and
411 functions. Refer to <xref linkend="functions"> for more
412 information. The following sections describe the types in detail.
415 <sect2 id="datatype-int">
416 <title>Integer Types</title>
418 <indexterm zone="datatype-int">
419 <primary>integer</primary>
422 <indexterm zone="datatype-int">
423 <primary>smallint</primary>
426 <indexterm zone="datatype-int">
427 <primary>bigint</primary>
431 <primary>int4</primary>
436 <primary>int2</primary>
441 <primary>int8</primary>
446 The types <type>smallint</type>, <type>integer</type>, and
447 <type>bigint</type> store whole numbers, that is, numbers without
448 fractional components, of various ranges. Attempts to store
449 values outside of the allowed range will result in an error.
453 The type <type>integer</type> is the common choice, as it offers
454 the best balance between range, storage size, and performance.
455 The <type>smallint</type> type is generally only used if disk
456 space is at a premium. The <type>bigint</type> type is designed to be
457 used when the range of the <type>integer</type> type is insufficient.
461 <acronym>SQL</acronym> only specifies the integer types
462 <type>integer</type> (or <type>int</type>),
463 <type>smallint</type>, and <type>bigint</type>. The
464 type names <type>int2</type>, <type>int4</type>, and
465 <type>int8</type> are extensions, which are also used by some
466 other <acronym>SQL</acronym> database systems.
471 <sect2 id="datatype-numeric-decimal">
472 <title>Arbitrary Precision Numbers</title>
475 <primary>numeric (data type)</primary>
479 <primary>arbitrary precision numbers</primary>
483 <primary>decimal</primary>
488 The type <type>numeric</type> can store numbers with a
489 very large number of digits and perform calculations exactly. It is
490 especially recommended for storing monetary amounts and other
491 quantities where exactness is required. However, arithmetic on
492 <type>numeric</type> values is very slow compared to the integer
493 types, or to the floating-point types described in the next section.
497 We use the following terms below: The
498 <firstterm>scale</firstterm> of a <type>numeric</type> is the
499 count of decimal digits in the fractional part, to the right of
500 the decimal point. The <firstterm>precision</firstterm> of a
501 <type>numeric</type> is the total count of significant digits in
502 the whole number, that is, the number of digits to both sides of
503 the decimal point. So the number 23.5141 has a precision of 6
504 and a scale of 4. Integers can be considered to have a scale of
509 Both the maximum precision and the maximum scale of a
510 <type>numeric</type> column can be
511 configured. To declare a column of type <type>numeric</type> use
514 NUMERIC(<replaceable>precision</replaceable>, <replaceable>scale</replaceable>)
516 The precision must be positive, the scale zero or positive.
519 NUMERIC(<replaceable>precision</replaceable>)
521 selects a scale of 0. Specifying:
525 without any precision or scale creates a column in which numeric
526 values of any precision and scale can be stored, up to the
527 implementation limit on precision. A column of this kind will
528 not coerce input values to any particular scale, whereas
529 <type>numeric</type> columns with a declared scale will coerce
530 input values to that scale. (The <acronym>SQL</acronym> standard
531 requires a default scale of 0, i.e., coercion to integer
532 precision. We find this a bit useless. If you're concerned
533 about portability, always specify the precision and scale
539 The maximum allowed precision when explicitly specified in the
540 type declaration is 1000; <type>NUMERIC</type> without a specified
541 precision is subject to the limits described in <xref
542 linkend="datatype-numeric-table">.
547 If the scale of a value to be stored is greater than the declared
548 scale of the column, the system will round the value to the specified
549 number of fractional digits. Then, if the number of digits to the
550 left of the decimal point exceeds the declared precision minus the
551 declared scale, an error is raised.
555 Numeric values are physically stored without any extra leading or
556 trailing zeroes. Thus, the declared precision and scale of a column
557 are maximums, not fixed allocations. (In this sense the <type>numeric</>
558 type is more akin to <type>varchar(<replaceable>n</>)</type>
559 than to <type>char(<replaceable>n</>)</type>.) The actual storage
560 requirement is two bytes for each group of four decimal digits,
561 plus three to eight bytes overhead.
565 <primary>NaN</primary>
566 <see>not a number</see>
570 <primary>not a number</primary>
571 <secondary>numeric (data type)</secondary>
575 In addition to ordinary numeric values, the <type>numeric</type>
576 type allows the special value <literal>NaN</>, meaning
577 <quote>not-a-number</quote>. Any operation on <literal>NaN</>
578 yields another <literal>NaN</>. When writing this value
579 as a constant in an SQL command, you must put quotes around it,
580 for example <literal>UPDATE table SET x = 'NaN'</>. On input,
581 the string <literal>NaN</> is recognized in a case-insensitive manner.
586 In most implementations of the <quote>not-a-number</> concept,
587 <literal>NaN</> is not considered equal to any other numeric
588 value (including <literal>NaN</>). In order to allow
589 <type>numeric</> values to be sorted and used in tree-based
590 indexes, <productname>PostgreSQL</> treats <literal>NaN</>
591 values as equal, and greater than all non-<literal>NaN</>
597 The types <type>decimal</type> and <type>numeric</type> are
598 equivalent. Both types are part of the <acronym>SQL</acronym>
604 <sect2 id="datatype-float">
605 <title>Floating-Point Types</title>
607 <indexterm zone="datatype-float">
608 <primary>real</primary>
611 <indexterm zone="datatype-float">
612 <primary>double precision</primary>
616 <primary>float4</primary>
621 <primary>float8</primary>
622 <see>double precision</see>
625 <indexterm zone="datatype-float">
626 <primary>floating point</primary>
630 The data types <type>real</type> and <type>double
631 precision</type> are inexact, variable-precision numeric types.
632 In practice, these types are usually implementations of
633 <acronym>IEEE</acronym> Standard 754 for Binary Floating-Point
634 Arithmetic (single and double precision, respectively), to the
635 extent that the underlying processor, operating system, and
640 Inexact means that some values cannot be converted exactly to the
641 internal format and are stored as approximations, so that storing
642 and retrieving a value might show slight discrepancies.
643 Managing these errors and how they propagate through calculations
644 is the subject of an entire branch of mathematics and computer
645 science and will not be discussed here, except for the
650 If you require exact storage and calculations (such as for
651 monetary amounts), use the <type>numeric</type> type instead.
657 If you want to do complicated calculations with these types
658 for anything important, especially if you rely on certain
659 behavior in boundary cases (infinity, underflow), you should
660 evaluate the implementation carefully.
666 Comparing two floating-point values for equality might not
667 always work as expected.
674 On most platforms, the <type>real</type> type has a range of at least
675 1E-37 to 1E+37 with a precision of at least 6 decimal digits. The
676 <type>double precision</type> type typically has a range of around
677 1E-307 to 1E+308 with a precision of at least 15 digits. Values that
678 are too large or too small will cause an error. Rounding might
679 take place if the precision of an input number is too high.
680 Numbers too close to zero that are not representable as distinct
681 from zero will cause an underflow error.
685 <primary>not a number</primary>
686 <secondary>double precision</secondary>
690 In addition to ordinary numeric values, the floating-point types
691 have several special values:
693 <literal>Infinity</literal>
694 <literal>-Infinity</literal>
695 <literal>NaN</literal>
697 These represent the IEEE 754 special values
698 <quote>infinity</quote>, <quote>negative infinity</quote>, and
699 <quote>not-a-number</quote>, respectively. (On a machine whose
700 floating-point arithmetic does not follow IEEE 754, these values
701 will probably not work as expected.) When writing these values
702 as constants in an SQL command, you must put quotes around them,
703 for example <literal>UPDATE table SET x = 'Infinity'</>. On input,
704 these strings are recognized in a case-insensitive manner.
709 IEEE754 specifies that <literal>NaN</> should not compare equal
710 to any other floating-point value (including <literal>NaN</>).
711 In order to allow floating-point values to be sorted and used
712 in tree-based indexes, <productname>PostgreSQL</> treats
713 <literal>NaN</> values as equal, and greater than all
714 non-<literal>NaN</> values.
719 <productname>PostgreSQL</productname> also supports the SQL-standard
720 notations <type>float</type> and
721 <type>float(<replaceable>p</replaceable>)</type> for specifying
722 inexact numeric types. Here, <replaceable>p</replaceable> specifies
723 the minimum acceptable precision in <emphasis>binary</> digits.
724 <productname>PostgreSQL</productname> accepts
725 <type>float(1)</type> to <type>float(24)</type> as selecting the
726 <type>real</type> type, while
727 <type>float(25)</type> to <type>float(53)</type> select
728 <type>double precision</type>. Values of <replaceable>p</replaceable>
729 outside the allowed range draw an error.
730 <type>float</type> with no precision specified is taken to mean
731 <type>double precision</type>.
736 Prior to <productname>PostgreSQL</productname> 7.4, the precision in
737 <type>float(<replaceable>p</replaceable>)</type> was taken to mean
738 so many <emphasis>decimal</> digits. This has been corrected to match the SQL
739 standard, which specifies that the precision is measured in binary
740 digits. The assumption that <type>real</type> and
741 <type>double precision</type> have exactly 24 and 53 bits in the
742 mantissa respectively is correct for IEEE-standard floating point
743 implementations. On non-IEEE platforms it might be off a little, but
744 for simplicity the same ranges of <replaceable>p</replaceable> are used
751 <sect2 id="datatype-serial">
752 <title>Serial Types</title>
754 <indexterm zone="datatype-serial">
755 <primary>smallserial</primary>
758 <indexterm zone="datatype-serial">
759 <primary>serial</primary>
762 <indexterm zone="datatype-serial">
763 <primary>bigserial</primary>
766 <indexterm zone="datatype-serial">
767 <primary>serial2</primary>
770 <indexterm zone="datatype-serial">
771 <primary>serial4</primary>
774 <indexterm zone="datatype-serial">
775 <primary>serial8</primary>
779 <primary>auto-increment</primary>
784 <primary>sequence</primary>
785 <secondary>and serial type</secondary>
789 The data types <type>smallserial</type>, <type>serial</type> and
790 <type>bigserial</type> are not true types, but merely
791 a notational convenience for creating unique identifier columns
792 (similar to the <literal>AUTO_INCREMENT</literal> property
793 supported by some other databases). In the current
794 implementation, specifying:
797 CREATE TABLE <replaceable class="parameter">tablename</replaceable> (
798 <replaceable class="parameter">colname</replaceable> SERIAL
802 is equivalent to specifying:
805 CREATE SEQUENCE <replaceable class="parameter">tablename</replaceable>_<replaceable class="parameter">colname</replaceable>_seq;
806 CREATE TABLE <replaceable class="parameter">tablename</replaceable> (
807 <replaceable class="parameter">colname</replaceable> integer NOT NULL DEFAULT nextval('<replaceable class="parameter">tablename</replaceable>_<replaceable class="parameter">colname</replaceable>_seq')
809 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>;
812 Thus, we have created an integer column and arranged for its default
813 values to be assigned from a sequence generator. A <literal>NOT NULL</>
814 constraint is applied to ensure that a null value cannot be
815 inserted. (In most cases you would also want to attach a
816 <literal>UNIQUE</> or <literal>PRIMARY KEY</> constraint to prevent
817 duplicate values from being inserted by accident, but this is
818 not automatic.) Lastly, the sequence is marked as <quote>owned by</>
819 the column, so that it will be dropped if the column or table is dropped.
824 Because <type>smallserial</type>, <type>serial</type> and
825 <type>bigserial</type> are implemented using sequences, there may
826 be "holes" or gaps in the sequence of values which appears in the
827 column, even if no rows are ever deleted. A value allocated
828 from the sequence is still "used up" even if a row containing that
829 value is never successfully inserted into the table column. This
830 may happen, for example, if the inserting transaction rolls back.
831 See <literal>nextval()</literal> in <xref linkend="functions-sequence">
838 Prior to <productname>PostgreSQL</productname> 7.3, <type>serial</type>
839 implied <literal>UNIQUE</literal>. This is no longer automatic. If
840 you wish a serial column to have a unique constraint or be a
841 primary key, it must now be specified, just like
847 To insert the next value of the sequence into the <type>serial</type>
848 column, specify that the <type>serial</type>
849 column should be assigned its default value. This can be done
850 either by excluding the column from the list of columns in
851 the <command>INSERT</command> statement, or through the use of
852 the <literal>DEFAULT</literal> key word.
856 The type names <type>serial</type> and <type>serial4</type> are
857 equivalent: both create <type>integer</type> columns. The type
858 names <type>bigserial</type> and <type>serial8</type> work
859 the same way, except that they create a <type>bigint</type>
860 column. <type>bigserial</type> should be used if you anticipate
861 the use of more than 2<superscript>31</> identifiers over the
862 lifetime of the table. The type names <type>smallserial</type> and
863 <type>serial2</type> also work the same way, except that they
864 create a <type>smallint</type> column.
868 The sequence created for a <type>serial</type> column is
869 automatically dropped when the owning column is dropped.
870 You can drop the sequence without dropping the column, but this
871 will force removal of the column default expression.
876 <sect1 id="datatype-money">
877 <title>Monetary Types</title>
880 The <type>money</type> type stores a currency amount with a fixed
881 fractional precision; see <xref
882 linkend="datatype-money-table">. The fractional precision is
883 determined by the database's <xref linkend="guc-lc-monetary"> setting.
884 The range shown in the table assumes there are two fractional digits.
885 Input is accepted in a variety of formats, including integer and
886 floating-point literals, as well as typical
887 currency formatting, such as <literal>'$1,000.00'</literal>.
888 Output is generally in the latter form but depends on the locale.
891 <table id="datatype-money-table">
892 <title>Monetary Types</title>
897 <entry>Storage Size</entry>
898 <entry>Description</entry>
905 <entry>8 bytes</entry>
906 <entry>currency amount</entry>
907 <entry>-92233720368547758.08 to +92233720368547758.07</entry>
914 Since the output of this data type is locale-sensitive, it might not
915 work to load <type>money</> data into a database that has a different
916 setting of <varname>lc_monetary</>. To avoid problems, before
917 restoring a dump into a new database make sure <varname>lc_monetary</> has
918 the same or equivalent value as in the database that was dumped.
922 Values of the <type>numeric</type>, <type>int</type>, and
923 <type>bigint</type> data types can be cast to <type>money</type>.
924 Conversion from the <type>real</type> and <type>double precision</type>
925 data types can be done by casting to <type>numeric</type> first, for
928 SELECT '12.34'::float8::numeric::money;
930 However, this is not recommended. Floating point numbers should not be
931 used to handle money due to the potential for rounding errors.
935 A <type>money</type> value can be cast to <type>numeric</type> without
936 loss of precision. Conversion to other types could potentially lose
937 precision, and must also be done in two stages:
939 SELECT '52093.89'::money::numeric::float8;
944 When a <type>money</type> value is divided by another <type>money</type>
945 value, the result is <type>double precision</type> (i.e., a pure number,
946 not money); the currency units cancel each other out in the division.
951 <sect1 id="datatype-character">
952 <title>Character Types</title>
954 <indexterm zone="datatype-character">
955 <primary>character string</primary>
956 <secondary>data types</secondary>
960 <primary>string</primary>
961 <see>character string</see>
964 <indexterm zone="datatype-character">
965 <primary>character</primary>
968 <indexterm zone="datatype-character">
969 <primary>character varying</primary>
972 <indexterm zone="datatype-character">
973 <primary>text</primary>
976 <indexterm zone="datatype-character">
977 <primary>char</primary>
980 <indexterm zone="datatype-character">
981 <primary>varchar</primary>
984 <table id="datatype-character-table">
985 <title>Character Types</title>
990 <entry>Description</entry>
995 <entry><type>character varying(<replaceable>n</>)</type>, <type>varchar(<replaceable>n</>)</type></entry>
996 <entry>variable-length with limit</entry>
999 <entry><type>character(<replaceable>n</>)</type>, <type>char(<replaceable>n</>)</type></entry>
1000 <entry>fixed-length, blank padded</entry>
1003 <entry><type>text</type></entry>
1004 <entry>variable unlimited length</entry>
1011 <xref linkend="datatype-character-table"> shows the
1012 general-purpose character types available in
1013 <productname>PostgreSQL</productname>.
1017 <acronym>SQL</acronym> defines two primary character types:
1018 <type>character varying(<replaceable>n</>)</type> and
1019 <type>character(<replaceable>n</>)</type>, where <replaceable>n</>
1020 is a positive integer. Both of these types can store strings up to
1021 <replaceable>n</> characters (not bytes) in length. An attempt to store a
1022 longer string into a column of these types will result in an
1023 error, unless the excess characters are all spaces, in which case
1024 the string will be truncated to the maximum length. (This somewhat
1025 bizarre exception is required by the <acronym>SQL</acronym>
1026 standard.) If the string to be stored is shorter than the declared
1027 length, values of type <type>character</type> will be space-padded;
1028 values of type <type>character varying</type> will simply store the
1034 If one explicitly casts a value to <type>character
1035 varying(<replaceable>n</>)</type> or
1036 <type>character(<replaceable>n</>)</type>, then an over-length
1037 value will be truncated to <replaceable>n</> characters without
1038 raising an error. (This too is required by the
1039 <acronym>SQL</acronym> standard.)
1043 The notations <type>varchar(<replaceable>n</>)</type> and
1044 <type>char(<replaceable>n</>)</type> are aliases for <type>character
1045 varying(<replaceable>n</>)</type> and
1046 <type>character(<replaceable>n</>)</type>, respectively.
1047 <type>character</type> without length specifier is equivalent to
1048 <type>character(1)</type>. If <type>character varying</type> is used
1049 without length specifier, the type accepts strings of any size. The
1050 latter is a <productname>PostgreSQL</> extension.
1054 In addition, <productname>PostgreSQL</productname> provides the
1055 <type>text</type> type, which stores strings of any length.
1056 Although the type <type>text</type> is not in the
1057 <acronym>SQL</acronym> standard, several other SQL database
1058 management systems have it as well.
1062 Values of type <type>character</type> are physically padded
1063 with spaces to the specified width <replaceable>n</>, and are
1064 stored and displayed that way. However, the padding spaces are
1065 treated as semantically insignificant. Trailing spaces are
1066 disregarded when comparing two values of type <type>character</type>,
1067 and they will be removed when converting a <type>character</type> value
1068 to one of the other string types. Note that trailing spaces
1069 <emphasis>are</> semantically significant in
1070 <type>character varying</type> and <type>text</type> values, and
1071 when using pattern matching, e.g. <literal>LIKE</>,
1072 regular expressions.
1076 The storage requirement for a short string (up to 126 bytes) is 1 byte
1077 plus the actual string, which includes the space padding in the case of
1078 <type>character</type>. Longer strings have 4 bytes of overhead instead
1079 of 1. Long strings are compressed by the system automatically, so
1080 the physical requirement on disk might be less. Very long values are also
1081 stored in background tables so that they do not interfere with rapid
1082 access to shorter column values. In any case, the longest
1083 possible character string that can be stored is about 1 GB. (The
1084 maximum value that will be allowed for <replaceable>n</> in the data
1085 type declaration is less than that. It wouldn't be useful to
1086 change this because with multibyte character encodings the number of
1087 characters and bytes can be quite different. If you desire to
1088 store long strings with no specific upper limit, use
1089 <type>text</type> or <type>character varying</type> without a length
1090 specifier, rather than making up an arbitrary length limit.)
1095 There is no performance difference among these three types,
1096 apart from increased storage space when using the blank-padded
1097 type, and a few extra CPU cycles to check the length when storing into
1098 a length-constrained column. While
1099 <type>character(<replaceable>n</>)</type> has performance
1100 advantages in some other database systems, there is no such advantage in
1101 <productname>PostgreSQL</productname>; in fact
1102 <type>character(<replaceable>n</>)</type> is usually the slowest of
1103 the three because of its additional storage costs. In most situations
1104 <type>text</type> or <type>character varying</type> should be used
1110 Refer to <xref linkend="sql-syntax-strings"> for information about
1111 the syntax of string literals, and to <xref linkend="functions">
1112 for information about available operators and functions. The
1113 database character set determines the character set used to store
1114 textual values; for more information on character set support,
1115 refer to <xref linkend="multibyte">.
1119 <title>Using the Character Types</title>
1122 CREATE TABLE test1 (a character(4));
1123 INSERT INTO test1 VALUES ('ok');
1124 SELECT a, char_length(a) FROM test1; -- <co id="co.datatype-char">
1127 ------+-------------
1131 CREATE TABLE test2 (b varchar(5));
1132 INSERT INTO test2 VALUES ('ok');
1133 INSERT INTO test2 VALUES ('good ');
1134 INSERT INTO test2 VALUES ('too long');
1135 <computeroutput>ERROR: value too long for type character varying(5)</computeroutput>
1136 INSERT INTO test2 VALUES ('too long'::varchar(5)); -- explicit truncation
1137 SELECT b, char_length(b) FROM test2;
1140 -------+-------------
1147 <callout arearefs="co.datatype-char">
1149 The <function>char_length</function> function is discussed in
1150 <xref linkend="functions-string">.
1157 There are two other fixed-length character types in
1158 <productname>PostgreSQL</productname>, shown in <xref
1159 linkend="datatype-character-special-table">. The <type>name</type>
1160 type exists <emphasis>only</emphasis> for the storage of identifiers
1161 in the internal system catalogs and is not intended for use by the general user. Its
1162 length is currently defined as 64 bytes (63 usable characters plus
1163 terminator) but should be referenced using the constant
1164 <symbol>NAMEDATALEN</symbol> in <literal>C</> source code.
1165 The length is set at compile time (and
1166 is therefore adjustable for special uses); the default maximum
1167 length might change in a future release. The type <type>"char"</type>
1168 (note the quotes) is different from <type>char(1)</type> in that it
1169 only uses one byte of storage. It is internally used in the system
1170 catalogs as a simplistic enumeration type.
1173 <table id="datatype-character-special-table">
1174 <title>Special Character Types</title>
1179 <entry>Storage Size</entry>
1180 <entry>Description</entry>
1185 <entry><type>"char"</type></entry>
1186 <entry>1 byte</entry>
1187 <entry>single-byte internal type</entry>
1190 <entry><type>name</type></entry>
1191 <entry>64 bytes</entry>
1192 <entry>internal type for object names</entry>
1200 <sect1 id="datatype-binary">
1201 <title>Binary Data Types</title>
1203 <indexterm zone="datatype-binary">
1204 <primary>binary data</primary>
1207 <indexterm zone="datatype-binary">
1208 <primary>bytea</primary>
1212 The <type>bytea</type> data type allows storage of binary strings;
1213 see <xref linkend="datatype-binary-table">.
1216 <table id="datatype-binary-table">
1217 <title>Binary Data Types</title>
1222 <entry>Storage Size</entry>
1223 <entry>Description</entry>
1228 <entry><type>bytea</type></entry>
1229 <entry>1 or 4 bytes plus the actual binary string</entry>
1230 <entry>variable-length binary string</entry>
1237 A binary string is a sequence of octets (or bytes). Binary
1238 strings are distinguished from character strings in two
1239 ways. First, binary strings specifically allow storing
1240 octets of value zero and other <quote>non-printable</quote>
1241 octets (usually, octets outside the range 32 to 126).
1242 Character strings disallow zero octets, and also disallow any
1243 other octet values and sequences of octet values that are invalid
1244 according to the database's selected character set encoding.
1245 Second, operations on binary strings process the actual bytes,
1246 whereas the processing of character strings depends on locale settings.
1247 In short, binary strings are appropriate for storing data that the
1248 programmer thinks of as <quote>raw bytes</>, whereas character
1249 strings are appropriate for storing text.
1253 The <type>bytea</type> type supports two external formats for
1254 input and output: <productname>PostgreSQL</productname>'s historical
1255 <quote>escape</quote> format, and <quote>hex</quote> format. Both
1256 of these are always accepted on input. The output format depends
1257 on the configuration parameter <xref linkend="guc-bytea-output">;
1258 the default is hex. (Note that the hex format was introduced in
1259 <productname>PostgreSQL</productname> 9.0; earlier versions and some
1260 tools don't understand it.)
1264 The <acronym>SQL</acronym> standard defines a different binary
1265 string type, called <type>BLOB</type> or <type>BINARY LARGE
1266 OBJECT</type>. The input format is different from
1267 <type>bytea</type>, but the provided functions and operators are
1272 <title><type>bytea</> Hex Format</title>
1275 The <quote>hex</> format encodes binary data as 2 hexadecimal digits
1276 per byte, most significant nibble first. The entire string is
1277 preceded by the sequence <literal>\x</literal> (to distinguish it
1278 from the escape format). In some contexts, the initial backslash may
1279 need to be escaped by doubling it, in the same cases in which backslashes
1280 have to be doubled in escape format; details appear below.
1281 The hexadecimal digits can
1282 be either upper or lower case, and whitespace is permitted between
1283 digit pairs (but not within a digit pair nor in the starting
1284 <literal>\x</literal> sequence).
1285 The hex format is compatible with a wide
1286 range of external applications and protocols, and it tends to be
1287 faster to convert than the escape format, so its use is preferred.
1293 SELECT E'\\xDEADBEEF';
1299 <title><type>bytea</> Escape Format</title>
1302 The <quote>escape</quote> format is the traditional
1303 <productname>PostgreSQL</productname> format for the <type>bytea</type>
1305 takes the approach of representing a binary string as a sequence
1306 of ASCII characters, while converting those bytes that cannot be
1307 represented as an ASCII character into special escape sequences.
1308 If, from the point of view of the application, representing bytes
1309 as characters makes sense, then this representation can be
1310 convenient. But in practice it is usually confusing because it
1311 fuzzes up the distinction between binary strings and character
1312 strings, and also the particular escape mechanism that was chosen is
1313 somewhat unwieldy. So this format should probably be avoided
1314 for most new applications.
1318 When entering <type>bytea</type> values in escape format,
1320 values <emphasis>must</emphasis> be escaped, while all octet
1321 values <emphasis>can</emphasis> be escaped. In
1322 general, to escape an octet, convert it into its three-digit
1323 octal value and precede it
1324 by a backslash (or two backslashes, if writing the value as a
1325 literal using escape string syntax).
1326 Backslash itself (octet value 92) can alternatively be represented by
1328 <xref linkend="datatype-binary-sqlesc">
1329 shows the characters that must be escaped, and gives the alternative
1330 escape sequences where applicable.
1333 <table id="datatype-binary-sqlesc">
1334 <title><type>bytea</> Literal Escaped Octets</title>
1338 <entry>Decimal Octet Value</entry>
1339 <entry>Description</entry>
1340 <entry>Escaped Input Representation</entry>
1341 <entry>Example</entry>
1342 <entry>Output Representation</entry>
1349 <entry>zero octet</entry>
1350 <entry><literal>E'\\000'</literal></entry>
1351 <entry><literal>SELECT E'\\000'::bytea;</literal></entry>
1352 <entry><literal>\000</literal></entry>
1357 <entry>single quote</entry>
1358 <entry><literal>''''</literal> or <literal>E'\\047'</literal></entry>
1359 <entry><literal>SELECT E'\''::bytea;</literal></entry>
1360 <entry><literal>'</literal></entry>
1365 <entry>backslash</entry>
1366 <entry><literal>E'\\\\'</literal> or <literal>E'\\134'</literal></entry>
1367 <entry><literal>SELECT E'\\\\'::bytea;</literal></entry>
1368 <entry><literal>\\</literal></entry>
1372 <entry>0 to 31 and 127 to 255</entry>
1373 <entry><quote>non-printable</quote> octets</entry>
1374 <entry><literal>E'\\<replaceable>xxx'</></literal> (octal value)</entry>
1375 <entry><literal>SELECT E'\\001'::bytea;</literal></entry>
1376 <entry><literal>\001</literal></entry>
1384 The requirement to escape <emphasis>non-printable</emphasis> octets
1385 varies depending on locale settings. In some instances you can get away
1386 with leaving them unescaped. Note that the result in each of the examples
1387 in <xref linkend="datatype-binary-sqlesc"> was exactly one octet in
1388 length, even though the output representation is sometimes
1389 more than one character.
1393 The reason multiple backslashes are required, as shown
1394 in <xref linkend="datatype-binary-sqlesc">, is that an input
1395 string written as a string literal must pass through two parse
1396 phases in the <productname>PostgreSQL</productname> server.
1397 The first backslash of each pair is interpreted as an escape
1398 character by the string-literal parser (assuming escape string
1399 syntax is used) and is therefore consumed, leaving the second backslash of the
1400 pair. (Dollar-quoted strings can be used to avoid this level
1401 of escaping.) The remaining backslash is then recognized by the
1402 <type>bytea</type> input function as starting either a three
1403 digit octal value or escaping another backslash. For example,
1404 a string literal passed to the server as <literal>E'\\001'</literal>
1405 becomes <literal>\001</literal> after passing through the
1406 escape string parser. The <literal>\001</literal> is then sent
1407 to the <type>bytea</type> input function, where it is converted
1408 to a single octet with a decimal value of 1. Note that the
1409 single-quote character is not treated specially by <type>bytea</type>,
1410 so it follows the normal rules for string literals. (See also
1411 <xref linkend="sql-syntax-strings">.)
1415 <type>Bytea</type> octets are sometimes escaped when output. In general, each
1416 <quote>non-printable</quote> octet is converted into
1417 its equivalent three-digit octal value and preceded by one backslash.
1418 Most <quote>printable</quote> octets are represented by their standard
1419 representation in the client character set. The octet with decimal
1420 value 92 (backslash) is doubled in the output.
1421 Details are in <xref linkend="datatype-binary-resesc">.
1424 <table id="datatype-binary-resesc">
1425 <title><type>bytea</> Output Escaped Octets</title>
1429 <entry>Decimal Octet Value</entry>
1430 <entry>Description</entry>
1431 <entry>Escaped Output Representation</entry>
1432 <entry>Example</entry>
1433 <entry>Output Result</entry>
1441 <entry>backslash</entry>
1442 <entry><literal>\\</literal></entry>
1443 <entry><literal>SELECT E'\\134'::bytea;</literal></entry>
1444 <entry><literal>\\</literal></entry>
1448 <entry>0 to 31 and 127 to 255</entry>
1449 <entry><quote>non-printable</quote> octets</entry>
1450 <entry><literal>\<replaceable>xxx</></literal> (octal value)</entry>
1451 <entry><literal>SELECT E'\\001'::bytea;</literal></entry>
1452 <entry><literal>\001</literal></entry>
1456 <entry>32 to 126</entry>
1457 <entry><quote>printable</quote> octets</entry>
1458 <entry>client character set representation</entry>
1459 <entry><literal>SELECT E'\\176'::bytea;</literal></entry>
1460 <entry><literal>~</literal></entry>
1468 Depending on the front end to <productname>PostgreSQL</> you use,
1469 you might have additional work to do in terms of escaping and
1470 unescaping <type>bytea</type> strings. For example, you might also
1471 have to escape line feeds and carriage returns if your interface
1472 automatically translates these.
1478 <sect1 id="datatype-datetime">
1479 <title>Date/Time Types</title>
1481 <indexterm zone="datatype-datetime">
1482 <primary>date</primary>
1484 <indexterm zone="datatype-datetime">
1485 <primary>time</primary>
1487 <indexterm zone="datatype-datetime">
1488 <primary>time without time zone</primary>
1490 <indexterm zone="datatype-datetime">
1491 <primary>time with time zone</primary>
1493 <indexterm zone="datatype-datetime">
1494 <primary>timestamp</primary>
1496 <indexterm zone="datatype-datetime">
1497 <primary>timestamptz</primary>
1499 <indexterm zone="datatype-datetime">
1500 <primary>timestamp with time zone</primary>
1502 <indexterm zone="datatype-datetime">
1503 <primary>timestamp without time zone</primary>
1505 <indexterm zone="datatype-datetime">
1506 <primary>interval</primary>
1508 <indexterm zone="datatype-datetime">
1509 <primary>time span</primary>
1513 <productname>PostgreSQL</productname> supports the full set of
1514 <acronym>SQL</acronym> date and time types, shown in <xref
1515 linkend="datatype-datetime-table">. The operations available
1516 on these data types are described in
1517 <xref linkend="functions-datetime">.
1518 Dates are counted according to the Gregorian calendar, even in
1519 years before that calendar was introduced (see <xref
1520 linkend="datetime-units-history"> for more information).
1523 <table id="datatype-datetime-table">
1524 <title>Date/Time Types</title>
1529 <entry>Storage Size</entry>
1530 <entry>Description</entry>
1531 <entry>Low Value</entry>
1532 <entry>High Value</entry>
1533 <entry>Resolution</entry>
1538 <entry><type>timestamp [ (<replaceable>p</replaceable>) ] [ without time zone ]</type></entry>
1539 <entry>8 bytes</entry>
1540 <entry>both date and time (no time zone)</entry>
1541 <entry>4713 BC</entry>
1542 <entry>294276 AD</entry>
1543 <entry>1 microsecond / 14 digits</entry>
1546 <entry><type>timestamp [ (<replaceable>p</replaceable>) ] with time zone</type></entry>
1547 <entry>8 bytes</entry>
1548 <entry>both date and time, with time zone</entry>
1549 <entry>4713 BC</entry>
1550 <entry>294276 AD</entry>
1551 <entry>1 microsecond / 14 digits</entry>
1554 <entry><type>date</type></entry>
1555 <entry>4 bytes</entry>
1556 <entry>date (no time of day)</entry>
1557 <entry>4713 BC</entry>
1558 <entry>5874897 AD</entry>
1559 <entry>1 day</entry>
1562 <entry><type>time [ (<replaceable>p</replaceable>) ] [ without time zone ]</type></entry>
1563 <entry>8 bytes</entry>
1564 <entry>time of day (no date)</entry>
1565 <entry>00:00:00</entry>
1566 <entry>24:00:00</entry>
1567 <entry>1 microsecond / 14 digits</entry>
1570 <entry><type>time [ (<replaceable>p</replaceable>) ] with time zone</type></entry>
1571 <entry>12 bytes</entry>
1572 <entry>times of day only, with time zone</entry>
1573 <entry>00:00:00+1459</entry>
1574 <entry>24:00:00-1459</entry>
1575 <entry>1 microsecond / 14 digits</entry>
1578 <entry><type>interval [ <replaceable>fields</replaceable> ] [ (<replaceable>p</replaceable>) ]</type></entry>
1579 <entry>12 bytes</entry>
1580 <entry>time interval</entry>
1581 <entry>-178000000 years</entry>
1582 <entry>178000000 years</entry>
1583 <entry>1 microsecond / 14 digits</entry>
1591 The SQL standard requires that writing just <type>timestamp</type>
1592 be equivalent to <type>timestamp without time
1593 zone</type>, and <productname>PostgreSQL</productname> honors that
1594 behavior. (Releases prior to 7.3 treated it as <type>timestamp
1595 with time zone</type>.) <type>timestamptz</type> is accepted as an
1596 abbreviation for <type>timestamp with time zone</type>; this is a
1597 <productname>PostgreSQL</productname> extension.
1602 <type>time</type>, <type>timestamp</type>, and
1603 <type>interval</type> accept an optional precision value
1604 <replaceable>p</replaceable> which specifies the number of
1605 fractional digits retained in the seconds field. By default, there
1606 is no explicit bound on precision. The allowed range of
1607 <replaceable>p</replaceable> is from 0 to 6 for the
1608 <type>timestamp</type> and <type>interval</type> types.
1613 When <type>timestamp</> values are stored as eight-byte integers
1614 (currently the default), microsecond precision is available over
1615 the full range of values. When <type>timestamp</> values are
1616 stored as double precision floating-point numbers instead (a
1617 deprecated compile-time option), the effective limit of precision
1618 might be less than 6. <type>timestamp</type> values are stored as
1619 seconds before or after midnight 2000-01-01. When
1620 <type>timestamp</type> values are implemented using floating-point
1621 numbers, microsecond precision is achieved for dates within a few
1622 years of 2000-01-01, but the precision degrades for dates further
1623 away. Note that using floating-point datetimes allows a larger
1624 range of <type>timestamp</type> values to be represented than
1625 shown above: from 4713 BC up to 5874897 AD.
1629 The same compile-time option also determines whether
1630 <type>time</type> and <type>interval</type> values are stored as
1631 floating-point numbers or eight-byte integers. In the
1632 floating-point case, large <type>interval</type> values degrade in
1633 precision as the size of the interval increases.
1638 For the <type>time</type> types, the allowed range of
1639 <replaceable>p</replaceable> is from 0 to 6 when eight-byte integer
1640 storage is used, or from 0 to 10 when floating-point storage is used.
1644 The <type>interval</type> type has an additional option, which is
1645 to restrict the set of stored fields by writing one of these phrases:
1646 <literallayout class="monospaced">
1661 Note that if both <replaceable>fields</replaceable> and
1662 <replaceable>p</replaceable> are specified, the
1663 <replaceable>fields</replaceable> must include <literal>SECOND</>,
1664 since the precision applies only to the seconds.
1668 The type <type>time with time zone</type> is defined by the SQL
1669 standard, but the definition exhibits properties which lead to
1670 questionable usefulness. In most cases, a combination of
1671 <type>date</type>, <type>time</type>, <type>timestamp without time
1672 zone</type>, and <type>timestamp with time zone</type> should
1673 provide a complete range of date/time functionality required by
1678 The types <type>abstime</type>
1679 and <type>reltime</type> are lower precision types which are used internally.
1680 You are discouraged from using these types in
1681 applications; these internal types
1682 might disappear in a future release.
1685 <sect2 id="datatype-datetime-input">
1686 <title>Date/Time Input</title>
1689 Date and time input is accepted in almost any reasonable format, including
1690 ISO 8601, <acronym>SQL</acronym>-compatible,
1691 traditional <productname>POSTGRES</productname>, and others.
1692 For some formats, ordering of day, month, and year in date input is
1693 ambiguous and there is support for specifying the expected
1694 ordering of these fields. Set the <xref linkend="guc-datestyle"> parameter
1695 to <literal>MDY</> to select month-day-year interpretation,
1696 <literal>DMY</> to select day-month-year interpretation, or
1697 <literal>YMD</> to select year-month-day interpretation.
1701 <productname>PostgreSQL</productname> is more flexible in
1702 handling date/time input than the
1703 <acronym>SQL</acronym> standard requires.
1704 See <xref linkend="datetime-appendix">
1705 for the exact parsing rules of date/time input and for the
1706 recognized text fields including months, days of the week, and
1711 Remember that any date or time literal input needs to be enclosed
1712 in single quotes, like text strings. Refer to
1713 <xref linkend="sql-syntax-constants-generic"> for more
1715 <acronym>SQL</acronym> requires the following syntax
1717 <replaceable>type</replaceable> [ (<replaceable>p</replaceable>) ] '<replaceable>value</replaceable>'
1719 where <replaceable>p</replaceable> is an optional precision
1720 specification giving the number of
1721 fractional digits in the seconds field. Precision can be
1722 specified for <type>time</type>, <type>timestamp</type>, and
1723 <type>interval</type> types. The allowed values are mentioned
1724 above. If no precision is specified in a constant specification,
1725 it defaults to the precision of the literal value.
1729 <title>Dates</title>
1732 <primary>date</primary>
1736 <xref linkend="datatype-datetime-date-table"> shows some possible
1737 inputs for the <type>date</type> type.
1740 <table id="datatype-datetime-date-table">
1741 <title>Date Input</title>
1745 <entry>Example</entry>
1746 <entry>Description</entry>
1751 <entry>1999-01-08</entry>
1752 <entry>ISO 8601; January 8 in any mode
1753 (recommended format)</entry>
1756 <entry>January 8, 1999</entry>
1757 <entry>unambiguous in any <varname>datestyle</varname> input mode</entry>
1760 <entry>1/8/1999</entry>
1761 <entry>January 8 in <literal>MDY</> mode;
1762 August 1 in <literal>DMY</> mode</entry>
1765 <entry>1/18/1999</entry>
1766 <entry>January 18 in <literal>MDY</> mode;
1767 rejected in other modes</entry>
1770 <entry>01/02/03</entry>
1771 <entry>January 2, 2003 in <literal>MDY</> mode;
1772 February 1, 2003 in <literal>DMY</> mode;
1773 February 3, 2001 in <literal>YMD</> mode
1777 <entry>1999-Jan-08</entry>
1778 <entry>January 8 in any mode</entry>
1781 <entry>Jan-08-1999</entry>
1782 <entry>January 8 in any mode</entry>
1785 <entry>08-Jan-1999</entry>
1786 <entry>January 8 in any mode</entry>
1789 <entry>99-Jan-08</entry>
1790 <entry>January 8 in <literal>YMD</> mode, else error</entry>
1793 <entry>08-Jan-99</entry>
1794 <entry>January 8, except error in <literal>YMD</> mode</entry>
1797 <entry>Jan-08-99</entry>
1798 <entry>January 8, except error in <literal>YMD</> mode</entry>
1801 <entry>19990108</entry>
1802 <entry>ISO 8601; January 8, 1999 in any mode</entry>
1805 <entry>990108</entry>
1806 <entry>ISO 8601; January 8, 1999 in any mode</entry>
1809 <entry>1999.008</entry>
1810 <entry>year and day of year</entry>
1813 <entry>J2451187</entry>
1814 <entry>Julian date</entry>
1817 <entry>January 8, 99 BC</entry>
1818 <entry>year 99 BC</entry>
1826 <title>Times</title>
1829 <primary>time</primary>
1832 <primary>time without time zone</primary>
1835 <primary>time with time zone</primary>
1839 The time-of-day types are <type>time [
1840 (<replaceable>p</replaceable>) ] without time zone</type> and
1841 <type>time [ (<replaceable>p</replaceable>) ] with time
1842 zone</type>. <type>time</type> alone is equivalent to
1843 <type>time without time zone</type>.
1847 Valid input for these types consists of a time of day followed
1848 by an optional time zone. (See <xref
1849 linkend="datatype-datetime-time-table">
1850 and <xref linkend="datatype-timezone-table">.) If a time zone is
1851 specified in the input for <type>time without time zone</type>,
1852 it is silently ignored. You can also specify a date but it will
1853 be ignored, except when you use a time zone name that involves a
1854 daylight-savings rule, such as
1855 <literal>America/New_York</literal>. In this case specifying the date
1856 is required in order to determine whether standard or daylight-savings
1857 time applies. The appropriate time zone offset is recorded in the
1858 <type>time with time zone</type> value.
1861 <table id="datatype-datetime-time-table">
1862 <title>Time Input</title>
1866 <entry>Example</entry>
1867 <entry>Description</entry>
1872 <entry><literal>04:05:06.789</literal></entry>
1873 <entry>ISO 8601</entry>
1876 <entry><literal>04:05:06</literal></entry>
1877 <entry>ISO 8601</entry>
1880 <entry><literal>04:05</literal></entry>
1881 <entry>ISO 8601</entry>
1884 <entry><literal>040506</literal></entry>
1885 <entry>ISO 8601</entry>
1888 <entry><literal>04:05 AM</literal></entry>
1889 <entry>same as 04:05; AM does not affect value</entry>
1892 <entry><literal>04:05 PM</literal></entry>
1893 <entry>same as 16:05; input hour must be <= 12</entry>
1896 <entry><literal>04:05:06.789-8</literal></entry>
1897 <entry>ISO 8601</entry>
1900 <entry><literal>04:05:06-08:00</literal></entry>
1901 <entry>ISO 8601</entry>
1904 <entry><literal>04:05-08:00</literal></entry>
1905 <entry>ISO 8601</entry>
1908 <entry><literal>040506-08</literal></entry>
1909 <entry>ISO 8601</entry>
1912 <entry><literal>04:05:06 PST</literal></entry>
1913 <entry>time zone specified by abbreviation</entry>
1916 <entry><literal>2003-04-12 04:05:06 America/New_York</literal></entry>
1917 <entry>time zone specified by full name</entry>
1923 <table tocentry="1" id="datatype-timezone-table">
1924 <title>Time Zone Input</title>
1928 <entry>Example</entry>
1929 <entry>Description</entry>
1934 <entry><literal>PST</literal></entry>
1935 <entry>Abbreviation (for Pacific Standard Time)</entry>
1938 <entry><literal>America/New_York</literal></entry>
1939 <entry>Full time zone name</entry>
1942 <entry><literal>PST8PDT</literal></entry>
1943 <entry>POSIX-style time zone specification</entry>
1946 <entry><literal>-8:00</literal></entry>
1947 <entry>ISO-8601 offset for PST</entry>
1950 <entry><literal>-800</literal></entry>
1951 <entry>ISO-8601 offset for PST</entry>
1954 <entry><literal>-8</literal></entry>
1955 <entry>ISO-8601 offset for PST</entry>
1958 <entry><literal>zulu</literal></entry>
1959 <entry>Military abbreviation for UTC</entry>
1962 <entry><literal>z</literal></entry>
1963 <entry>Short form of <literal>zulu</literal></entry>
1970 Refer to <xref linkend="datatype-timezones"> for more information on how
1971 to specify time zones.
1976 <title>Time Stamps</title>
1979 <primary>timestamp</primary>
1983 <primary>timestamp with time zone</primary>
1987 <primary>timestamp without time zone</primary>
1991 Valid input for the time stamp types consists of the concatenation
1992 of a date and a time, followed by an optional time zone,
1993 followed by an optional <literal>AD</literal> or <literal>BC</literal>.
1994 (Alternatively, <literal>AD</literal>/<literal>BC</literal> can appear
1995 before the time zone, but this is not the preferred ordering.)
2003 1999-01-08 04:05:06 -8:00
2006 are valid values, which follow the <acronym>ISO</acronym> 8601
2007 standard. In addition, the common format:
2009 January 8 04:05:06 1999 PST
2015 The <acronym>SQL</acronym> standard differentiates
2016 <type>timestamp without time zone</type>
2017 and <type>timestamp with time zone</type> literals by the presence of a
2018 <quote>+</quote> or <quote>-</quote> symbol and time zone offset after
2019 the time. Hence, according to the standard,
2021 <programlisting>TIMESTAMP '2004-10-19 10:23:54'</programlisting>
2023 is a <type>timestamp without time zone</type>, while
2025 <programlisting>TIMESTAMP '2004-10-19 10:23:54+02'</programlisting>
2027 is a <type>timestamp with time zone</type>.
2028 <productname>PostgreSQL</productname> never examines the content of a
2029 literal string before determining its type, and therefore will treat
2030 both of the above as <type>timestamp without time zone</type>. To
2031 ensure that a literal is treated as <type>timestamp with time
2032 zone</type>, give it the correct explicit type:
2034 <programlisting>TIMESTAMP WITH TIME ZONE '2004-10-19 10:23:54+02'</programlisting>
2036 In a literal that has been determined to be <type>timestamp without time
2037 zone</type>, <productname>PostgreSQL</productname> will silently ignore
2038 any time zone indication.
2039 That is, the resulting value is derived from the date/time
2040 fields in the input value, and is not adjusted for time zone.
2044 For <type>timestamp with time zone</type>, the internally stored
2045 value is always in UTC (Universal
2046 Coordinated Time, traditionally known as Greenwich Mean Time,
2047 <acronym>GMT</>). An input value that has an explicit
2048 time zone specified is converted to UTC using the appropriate offset
2049 for that time zone. If no time zone is stated in the input string,
2050 then it is assumed to be in the time zone indicated by the system's
2051 <xref linkend="guc-timezone"> parameter, and is converted to UTC using the
2052 offset for the <varname>timezone</> zone.
2056 When a <type>timestamp with time
2057 zone</type> value is output, it is always converted from UTC to the
2058 current <varname>timezone</> zone, and displayed as local time in that
2059 zone. To see the time in another time zone, either change
2060 <varname>timezone</> or use the <literal>AT TIME ZONE</> construct
2061 (see <xref linkend="functions-datetime-zoneconvert">).
2065 Conversions between <type>timestamp without time zone</type> and
2066 <type>timestamp with time zone</type> normally assume that the
2067 <type>timestamp without time zone</type> value should be taken or given
2068 as <varname>timezone</> local time. A different time zone can
2069 be specified for the conversion using <literal>AT TIME ZONE</>.
2074 <title>Special Values</title>
2077 <primary>time</primary>
2078 <secondary>constants</secondary>
2082 <primary>date</primary>
2083 <secondary>constants</secondary>
2087 <productname>PostgreSQL</productname> supports several
2088 special date/time input values for convenience, as shown in <xref
2089 linkend="datatype-datetime-special-table">. The values
2090 <literal>infinity</literal> and <literal>-infinity</literal>
2091 are specially represented inside the system and will be displayed
2092 unchanged; but the others are simply notational shorthands
2093 that will be converted to ordinary date/time values when read.
2094 (In particular, <literal>now</> and related strings are converted
2095 to a specific time value as soon as they are read.)
2096 All of these values need to be enclosed in single quotes when used
2097 as constants in SQL commands.
2100 <table id="datatype-datetime-special-table">
2101 <title>Special Date/Time Inputs</title>
2105 <entry>Input String</entry>
2106 <entry>Valid Types</entry>
2107 <entry>Description</entry>
2112 <entry><literal>epoch</literal></entry>
2113 <entry><type>date</type>, <type>timestamp</type></entry>
2114 <entry>1970-01-01 00:00:00+00 (Unix system time zero)</entry>
2117 <entry><literal>infinity</literal></entry>
2118 <entry><type>date</type>, <type>timestamp</type></entry>
2119 <entry>later than all other time stamps</entry>
2122 <entry><literal>-infinity</literal></entry>
2123 <entry><type>date</type>, <type>timestamp</type></entry>
2124 <entry>earlier than all other time stamps</entry>
2127 <entry><literal>now</literal></entry>
2128 <entry><type>date</type>, <type>time</type>, <type>timestamp</type></entry>
2129 <entry>current transaction's start time</entry>
2132 <entry><literal>today</literal></entry>
2133 <entry><type>date</type>, <type>timestamp</type></entry>
2134 <entry>midnight today</entry>
2137 <entry><literal>tomorrow</literal></entry>
2138 <entry><type>date</type>, <type>timestamp</type></entry>
2139 <entry>midnight tomorrow</entry>
2142 <entry><literal>yesterday</literal></entry>
2143 <entry><type>date</type>, <type>timestamp</type></entry>
2144 <entry>midnight yesterday</entry>
2147 <entry><literal>allballs</literal></entry>
2148 <entry><type>time</type></entry>
2149 <entry>00:00:00.00 UTC</entry>
2156 The following <acronym>SQL</acronym>-compatible functions can also
2157 be used to obtain the current time value for the corresponding data
2159 <literal>CURRENT_DATE</literal>, <literal>CURRENT_TIME</literal>,
2160 <literal>CURRENT_TIMESTAMP</literal>, <literal>LOCALTIME</literal>,
2161 <literal>LOCALTIMESTAMP</literal>. The latter four accept an
2162 optional subsecond precision specification. (See <xref
2163 linkend="functions-datetime-current">.) Note that these are
2164 SQL functions and are <emphasis>not</> recognized in data input strings.
2170 <sect2 id="datatype-datetime-output">
2171 <title>Date/Time Output</title>
2174 <primary>date</primary>
2175 <secondary>output format</secondary>
2176 <seealso>formatting</seealso>
2180 <primary>time</primary>
2181 <secondary>output format</secondary>
2182 <seealso>formatting</seealso>
2186 The output format of the date/time types can be set to one of the four
2188 <acronym>SQL</acronym> (Ingres), traditional <productname>POSTGRES</>
2189 (Unix <application>date</> format), or
2191 is the <acronym>ISO</acronym> format. (The
2192 <acronym>SQL</acronym> standard requires the use of the ISO 8601
2193 format. The name of the <quote>SQL</quote> output format is a
2194 historical accident.) <xref
2195 linkend="datatype-datetime-output-table"> shows examples of each
2196 output style. The output of the <type>date</type> and
2197 <type>time</type> types is of course only the date or time part
2198 in accordance with the given examples.
2201 <table id="datatype-datetime-output-table">
2202 <title>Date/Time Output Styles</title>
2206 <entry>Style Specification</entry>
2207 <entry>Description</entry>
2208 <entry>Example</entry>
2213 <entry><literal>ISO</literal></entry>
2214 <entry>ISO 8601, SQL standard</entry>
2215 <entry><literal>1997-12-17 07:37:16-08</literal></entry>
2218 <entry><literal>SQL</literal></entry>
2219 <entry>traditional style</entry>
2220 <entry><literal>12/17/1997 07:37:16.00 PST</literal></entry>
2223 <entry><literal>Postgres</literal></entry>
2224 <entry>original style</entry>
2225 <entry><literal>Wed Dec 17 07:37:16 1997 PST</literal></entry>
2228 <entry><literal>German</literal></entry>
2229 <entry>regional style</entry>
2230 <entry><literal>17.12.1997 07:37:16.00 PST</literal></entry>
2238 ISO 8601 specifies the use of uppercase letter <literal>T</> to separate
2239 the date and time. <productname>PostgreSQL</> accepts that format on
2240 input, but on output it uses a space rather than <literal>T</>, as shown
2241 above. This is for readability and for consistency with RFC 3339 as
2242 well as some other database systems.
2247 In the <acronym>SQL</acronym> and POSTGRES styles, day appears before
2248 month if DMY field ordering has been specified, otherwise month appears
2250 (See <xref linkend="datatype-datetime-input">
2251 for how this setting also affects interpretation of input values.)
2252 <xref linkend="datatype-datetime-output2-table"> shows examples.
2255 <table id="datatype-datetime-output2-table">
2256 <title>Date Order Conventions</title>
2260 <entry><varname>datestyle</varname> Setting</entry>
2261 <entry>Input Ordering</entry>
2262 <entry>Example Output</entry>
2267 <entry><literal>SQL, DMY</></entry>
2268 <entry><replaceable>day</replaceable>/<replaceable>month</replaceable>/<replaceable>year</replaceable></entry>
2269 <entry><literal>17/12/1997 15:37:16.00 CET</literal></entry>
2272 <entry><literal>SQL, MDY</></entry>
2273 <entry><replaceable>month</replaceable>/<replaceable>day</replaceable>/<replaceable>year</replaceable></entry>
2274 <entry><literal>12/17/1997 07:37:16.00 PST</literal></entry>
2277 <entry><literal>Postgres, DMY</></entry>
2278 <entry><replaceable>day</replaceable>/<replaceable>month</replaceable>/<replaceable>year</replaceable></entry>
2279 <entry><literal>Wed 17 Dec 07:37:16 1997 PST</literal></entry>
2286 The date/time style can be selected by the user using the
2287 <command>SET datestyle</command> command, the <xref
2288 linkend="guc-datestyle"> parameter in the
2289 <filename>postgresql.conf</filename> configuration file, or the
2290 <envar>PGDATESTYLE</envar> environment variable on the server or
2295 The formatting function <function>to_char</function>
2296 (see <xref linkend="functions-formatting">) is also available as
2297 a more flexible way to format date/time output.
2301 <sect2 id="datatype-timezones">
2302 <title>Time Zones</title>
2304 <indexterm zone="datatype-timezones">
2305 <primary>time zone</primary>
2309 Time zones, and time-zone conventions, are influenced by
2310 political decisions, not just earth geometry. Time zones around the
2311 world became somewhat standardized during the 1900's,
2312 but continue to be prone to arbitrary changes, particularly with
2313 respect to daylight-savings rules.
2314 <productname>PostgreSQL</productname> uses the widely-used
2315 <literal>zoneinfo</> (Olson) time zone database for information about
2316 historical time zone rules. For times in the future, the assumption
2317 is that the latest known rules for a given time zone will
2318 continue to be observed indefinitely far into the future.
2322 <productname>PostgreSQL</productname> endeavors to be compatible with
2323 the <acronym>SQL</acronym> standard definitions for typical usage.
2324 However, the <acronym>SQL</acronym> standard has an odd mix of date and
2325 time types and capabilities. Two obvious problems are:
2330 Although the <type>date</type> type
2331 cannot have an associated time zone, the
2332 <type>time</type> type can.
2333 Time zones in the real world have little meaning unless
2334 associated with a date as well as a time,
2335 since the offset can vary through the year with daylight-saving
2342 The default time zone is specified as a constant numeric offset
2343 from <acronym>UTC</>. It is therefore impossible to adapt to
2344 daylight-saving time when doing date/time arithmetic across
2345 <acronym>DST</acronym> boundaries.
2353 To address these difficulties, we recommend using date/time types
2354 that contain both date and time when using time zones. We
2355 do <emphasis>not</> recommend using the type <type>time with
2356 time zone</type> (though it is supported by
2357 <productname>PostgreSQL</productname> for legacy applications and
2358 for compliance with the <acronym>SQL</acronym> standard).
2359 <productname>PostgreSQL</productname> assumes
2360 your local time zone for any type containing only date or time.
2364 All timezone-aware dates and times are stored internally in
2365 <acronym>UTC</acronym>. They are converted to local time
2366 in the zone specified by the <xref linkend="guc-timezone"> configuration
2367 parameter before being displayed to the client.
2371 <productname>PostgreSQL</productname> allows you to specify time zones in
2372 three different forms:
2376 A full time zone name, for example <literal>America/New_York</>.
2377 The recognized time zone names are listed in the
2378 <literal>pg_timezone_names</literal> view (see <xref
2379 linkend="view-pg-timezone-names">).
2380 <productname>PostgreSQL</productname> uses the widely-used
2381 <literal>zoneinfo</> time zone data for this purpose, so the same
2382 names are also recognized by much other software.
2387 A time zone abbreviation, for example <literal>PST</>. Such a
2388 specification merely defines a particular offset from UTC, in
2389 contrast to full time zone names which can imply a set of daylight
2390 savings transition-date rules as well. The recognized abbreviations
2391 are listed in the <literal>pg_timezone_abbrevs</> view (see <xref
2392 linkend="view-pg-timezone-abbrevs">). You cannot set the
2393 configuration parameters <xref linkend="guc-timezone"> or
2394 <xref linkend="guc-log-timezone"> to a time
2395 zone abbreviation, but you can use abbreviations in
2396 date/time input values and with the <literal>AT TIME ZONE</>
2402 In addition to the timezone names and abbreviations,
2403 <productname>PostgreSQL</productname> will accept POSIX-style time zone
2404 specifications of the form <replaceable>STD</><replaceable>offset</> or
2405 <replaceable>STD</><replaceable>offset</><replaceable>DST</>, where
2406 <replaceable>STD</> is a zone abbreviation, <replaceable>offset</> is a
2407 numeric offset in hours west from UTC, and <replaceable>DST</> is an
2408 optional daylight-savings zone abbreviation, assumed to stand for one
2409 hour ahead of the given offset. For example, if <literal>EST5EDT</>
2410 were not already a recognized zone name, it would be accepted and would
2411 be functionally equivalent to United States East Coast time. When a
2412 daylight-savings zone name is present, it is assumed to be used
2413 according to the same daylight-savings transition rules used in the
2414 <literal>zoneinfo</> time zone database's <filename>posixrules</> entry.
2415 In a standard <productname>PostgreSQL</productname> installation,
2416 <filename>posixrules</> is the same as <literal>US/Eastern</>, so
2417 that POSIX-style time zone specifications follow USA daylight-savings
2418 rules. If needed, you can adjust this behavior by replacing the
2419 <filename>posixrules</> file.
2424 In short, this is the difference between abbreviations
2425 and full names: abbreviations always represent a fixed offset from
2426 UTC, whereas most of the full names imply a local daylight-savings time
2427 rule, and so have two possible UTC offsets.
2431 One should be wary that the POSIX-style time zone feature can
2432 lead to silently accepting bogus input, since there is no check on the
2433 reasonableness of the zone abbreviations. For example, <literal>SET
2434 TIMEZONE TO FOOBAR0</> will work, leaving the system effectively using
2435 a rather peculiar abbreviation for UTC.
2436 Another issue to keep in mind is that in POSIX time zone names,
2437 positive offsets are used for locations <emphasis>west</> of Greenwich.
2438 Everywhere else, <productname>PostgreSQL</productname> follows the
2439 ISO-8601 convention that positive timezone offsets are <emphasis>east</>
2444 In all cases, timezone names are recognized case-insensitively.
2445 (This is a change from <productname>PostgreSQL</productname> versions
2446 prior to 8.2, which were case-sensitive in some contexts but not others.)
2450 Neither full names nor abbreviations are hard-wired into the server;
2451 they are obtained from configuration files stored under
2452 <filename>.../share/timezone/</> and <filename>.../share/timezonesets/</>
2453 of the installation directory
2454 (see <xref linkend="datetime-config-files">).
2458 The <xref linkend="guc-timezone"> configuration parameter can
2459 be set in the file <filename>postgresql.conf</>, or in any of the
2460 other standard ways described in <xref linkend="runtime-config">.
2461 There are also some special ways to set it:
2466 The <acronym>SQL</acronym> command <command>SET TIME ZONE</command>
2467 sets the time zone for the session. This is an alternative spelling
2468 of <command>SET TIMEZONE TO</> with a more SQL-spec-compatible syntax.
2474 The <envar>PGTZ</envar> environment variable is used by
2475 <application>libpq</application> clients
2476 to send a <command>SET TIME ZONE</command>
2477 command to the server upon connection.
2484 <sect2 id="datatype-interval-input">
2485 <title>Interval Input</title>
2488 <primary>interval</primary>
2492 <type>interval</type> values can be written using the following
2496 <optional>@</> <replaceable>quantity</> <replaceable>unit</> <optional><replaceable>quantity</> <replaceable>unit</>...</> <optional><replaceable>direction</></optional>
2499 where <replaceable>quantity</> is a number (possibly signed);
2500 <replaceable>unit</> is <literal>microsecond</literal>,
2501 <literal>millisecond</literal>, <literal>second</literal>,
2502 <literal>minute</literal>, <literal>hour</literal>, <literal>day</literal>,
2503 <literal>week</literal>, <literal>month</literal>, <literal>year</literal>,
2504 <literal>decade</literal>, <literal>century</literal>, <literal>millennium</literal>,
2505 or abbreviations or plurals of these units;
2506 <replaceable>direction</> can be <literal>ago</literal> or
2507 empty. The at sign (<literal>@</>) is optional noise. The amounts
2508 of the different units are implicitly added with appropriate
2509 sign accounting. <literal>ago</literal> negates all the fields.
2510 This syntax is also used for interval output, if
2511 <xref linkend="guc-intervalstyle"> is set to
2512 <literal>postgres_verbose</>.
2516 Quantities of days, hours, minutes, and seconds can be specified without
2517 explicit unit markings. For example, <literal>'1 12:59:10'</> is read
2518 the same as <literal>'1 day 12 hours 59 min 10 sec'</>. Also,
2519 a combination of years and months can be specified with a dash;
2520 for example <literal>'200-10'</> is read the same as <literal>'200 years
2521 10 months'</>. (These shorter forms are in fact the only ones allowed
2522 by the <acronym>SQL</acronym> standard, and are used for output when
2523 <varname>IntervalStyle</> is set to <literal>sql_standard</literal>.)
2527 Interval values can also be written as ISO 8601 time intervals, using
2528 either the <quote>format with designators</> of the standard's section
2529 4.4.3.2 or the <quote>alternative format</> of section 4.4.3.3. The
2530 format with designators looks like this:
2532 P <replaceable>quantity</> <replaceable>unit</> <optional> <replaceable>quantity</> <replaceable>unit</> ...</optional> <optional> T <optional> <replaceable>quantity</> <replaceable>unit</> ...</optional></optional>
2534 The string must start with a <literal>P</>, and may include a
2535 <literal>T</> that introduces the time-of-day units. The
2536 available unit abbreviations are given in <xref
2537 linkend="datatype-interval-iso8601-units">. Units may be
2538 omitted, and may be specified in any order, but units smaller than
2539 a day must appear after <literal>T</>. In particular, the meaning of
2540 <literal>M</> depends on whether it is before or after
2544 <table id="datatype-interval-iso8601-units">
2545 <title>ISO 8601 Interval Unit Abbreviations</title>
2549 <entry>Abbreviation</entry>
2550 <entry>Meaning</entry>
2556 <entry>Years</entry>
2560 <entry>Months (in the date part)</entry>
2564 <entry>Weeks</entry>
2572 <entry>Hours</entry>
2576 <entry>Minutes (in the time part)</entry>
2580 <entry>Seconds</entry>
2587 In the alternative format:
2589 P <optional> <replaceable>years</>-<replaceable>months</>-<replaceable>days</> </optional> <optional> T <replaceable>hours</>:<replaceable>minutes</>:<replaceable>seconds</> </optional>
2591 the string must begin with <literal>P</literal>, and a
2592 <literal>T</> separates the date and time parts of the interval.
2593 The values are given as numbers similar to ISO 8601 dates.
2597 When writing an interval constant with a <replaceable>fields</>
2598 specification, or when assigning a string to an interval column that was
2599 defined with a <replaceable>fields</> specification, the interpretation of
2600 unmarked quantities depends on the <replaceable>fields</>. For
2601 example <literal>INTERVAL '1' YEAR</> is read as 1 year, whereas
2602 <literal>INTERVAL '1'</> means 1 second. Also, field values
2603 <quote>to the right</> of the least significant field allowed by the
2604 <replaceable>fields</> specification are silently discarded. For
2605 example, writing <literal>INTERVAL '1 day 2:03:04' HOUR TO MINUTE</>
2606 results in dropping the seconds field, but not the day field.
2610 According to the <acronym>SQL</> standard all fields of an interval
2611 value must have the same sign, so a leading negative sign applies to all
2612 fields; for example the negative sign in the interval literal
2613 <literal>'-1 2:03:04'</> applies to both the days and hour/minute/second
2614 parts. <productname>PostgreSQL</> allows the fields to have different
2615 signs, and traditionally treats each field in the textual representation
2616 as independently signed, so that the hour/minute/second part is
2617 considered positive in this example. If <varname>IntervalStyle</> is
2618 set to <literal>sql_standard</literal> then a leading sign is considered
2619 to apply to all fields (but only if no additional signs appear).
2620 Otherwise the traditional <productname>PostgreSQL</> interpretation is
2621 used. To avoid ambiguity, it's recommended to attach an explicit sign
2622 to each field if any field is negative.
2626 Internally <type>interval</> values are stored as months, days,
2627 and seconds. This is done because the number of days in a month
2628 varies, and a day can have 23 or 25 hours if a daylight savings
2629 time adjustment is involved. The months and days fields are integers
2630 while the seconds field can store fractions. Because intervals are
2631 usually created from constant strings or <type>timestamp</> subtraction,
2632 this storage method works well in most cases. Functions
2633 <function>justify_days</> and <function>justify_hours</> are
2634 available for adjusting days and hours that overflow their normal
2639 In the verbose input format, and in some fields of the more compact
2640 input formats, field values can have fractional parts; for example
2641 <literal>'1.5 week'</> or <literal>'01:02:03.45'</>. Such input is
2642 converted to the appropriate number of months, days, and seconds
2643 for storage. When this would result in a fractional number of
2644 months or days, the fraction is added to the lower-order fields
2645 using the conversion factors 1 month = 30 days and 1 day = 24 hours.
2646 For example, <literal>'1.5 month'</> becomes 1 month and 15 days.
2647 Only seconds will ever be shown as fractional on output.
2651 <xref linkend="datatype-interval-input-examples"> shows some examples
2652 of valid <type>interval</> input.
2655 <table id="datatype-interval-input-examples">
2656 <title>Interval Input</title>
2660 <entry>Example</entry>
2661 <entry>Description</entry>
2667 <entry>SQL standard format: 1 year 2 months</entry>
2670 <entry>3 4:05:06</entry>
2671 <entry>SQL standard format: 3 days 4 hours 5 minutes 6 seconds</entry>
2674 <entry>1 year 2 months 3 days 4 hours 5 minutes 6 seconds</entry>
2675 <entry>Traditional Postgres format: 1 year 2 months 3 days 4 hours 5 minutes 6 seconds</entry>
2678 <entry>P1Y2M3DT4H5M6S</entry>
2679 <entry>ISO 8601 <quote>format with designators</>: same meaning as above</entry>
2682 <entry>P0001-02-03T04:05:06</entry>
2683 <entry>ISO 8601 <quote>alternative format</>: same meaning as above</entry>
2691 <sect2 id="datatype-interval-output">
2692 <title>Interval Output</title>
2695 <primary>interval</primary>
2696 <secondary>output format</secondary>
2697 <seealso>formatting</seealso>
2701 The output format of the interval type can be set to one of the
2702 four styles <literal>sql_standard</>, <literal>postgres</>,
2703 <literal>postgres_verbose</>, or <literal>iso_8601</>,
2704 using the command <literal>SET intervalstyle</literal>.
2705 The default is the <literal>postgres</> format.
2706 <xref linkend="interval-style-output-table"> shows examples of each
2711 The <literal>sql_standard</> style produces output that conforms to
2712 the SQL standard's specification for interval literal strings, if
2713 the interval value meets the standard's restrictions (either year-month
2714 only or day-time only, with no mixing of positive
2715 and negative components). Otherwise the output looks like a standard
2716 year-month literal string followed by a day-time literal string,
2717 with explicit signs added to disambiguate mixed-sign intervals.
2721 The output of the <literal>postgres</> style matches the output of
2722 <productname>PostgreSQL</> releases prior to 8.4 when the
2723 <xref linkend="guc-datestyle"> parameter was set to <literal>ISO</>.
2727 The output of the <literal>postgres_verbose</> style matches the output of
2728 <productname>PostgreSQL</> releases prior to 8.4 when the
2729 <varname>DateStyle</> parameter was set to non-<literal>ISO</> output.
2733 The output of the <literal>iso_8601</> style matches the <quote>format
2734 with designators</> described in section 4.4.3.2 of the
2738 <table id="interval-style-output-table">
2739 <title>Interval Output Style Examples</title>
2743 <entry>Style Specification</entry>
2744 <entry>Year-Month Interval</entry>
2745 <entry>Day-Time Interval</entry>
2746 <entry>Mixed Interval</entry>
2751 <entry><literal>sql_standard</></entry>
2753 <entry>3 4:05:06</entry>
2754 <entry>-1-2 +3 -4:05:06</entry>
2757 <entry><literal>postgres</></entry>
2758 <entry>1 year 2 mons</entry>
2759 <entry>3 days 04:05:06</entry>
2760 <entry>-1 year -2 mons +3 days -04:05:06</entry>
2763 <entry><literal>postgres_verbose</></entry>
2764 <entry>@ 1 year 2 mons</entry>
2765 <entry>@ 3 days 4 hours 5 mins 6 secs</entry>
2766 <entry>@ 1 year 2 mons -3 days 4 hours 5 mins 6 secs ago</entry>
2769 <entry><literal>iso_8601</></entry>
2770 <entry>P1Y2M</entry>
2771 <entry>P3DT4H5M6S</entry>
2772 <entry>P-1Y-2M3DT-4H-5M-6S</entry>
2782 <sect1 id="datatype-boolean">
2783 <title>Boolean Type</title>
2785 <indexterm zone="datatype-boolean">
2786 <primary>Boolean</primary>
2787 <secondary>data type</secondary>
2790 <indexterm zone="datatype-boolean">
2791 <primary>true</primary>
2794 <indexterm zone="datatype-boolean">
2795 <primary>false</primary>
2799 <productname>PostgreSQL</productname> provides the
2800 standard <acronym>SQL</acronym> type <type>boolean</type>;
2801 see <xref linkend="datatype-boolean-table">.
2802 The <type>boolean</type> type can have several states:
2803 <quote>true</quote>, <quote>false</quote>, and a third state,
2804 <quote>unknown</quote>, which is represented by the
2805 <acronym>SQL</acronym> null value.
2808 <table id="datatype-boolean-table">
2809 <title>Boolean Data Type</title>
2814 <entry>Storage Size</entry>
2815 <entry>Description</entry>
2820 <entry><type>boolean</type></entry>
2821 <entry>1 byte</entry>
2822 <entry>state of true or false</entry>
2829 Valid literal values for the <quote>true</quote> state are:
2831 <member><literal>TRUE</literal></member>
2832 <member><literal>'t'</literal></member>
2833 <member><literal>'true'</literal></member>
2834 <member><literal>'y'</literal></member>
2835 <member><literal>'yes'</literal></member>
2836 <member><literal>'on'</literal></member>
2837 <member><literal>'1'</literal></member>
2839 For the <quote>false</quote> state, the following values can be
2842 <member><literal>FALSE</literal></member>
2843 <member><literal>'f'</literal></member>
2844 <member><literal>'false'</literal></member>
2845 <member><literal>'n'</literal></member>
2846 <member><literal>'no'</literal></member>
2847 <member><literal>'off'</literal></member>
2848 <member><literal>'0'</literal></member>
2850 Leading or trailing whitespace is ignored, and case does not matter.
2852 <literal>TRUE</literal> and <literal>FALSE</literal> are the preferred
2853 (<acronym>SQL</acronym>-compliant) usage.
2857 <xref linkend="datatype-boolean-example"> shows that
2858 <type>boolean</type> values are output using the letters
2859 <literal>t</literal> and <literal>f</literal>.
2862 <example id="datatype-boolean-example">
2863 <title>Using the <type>boolean</type> Type</title>
2866 CREATE TABLE test1 (a boolean, b text);
2867 INSERT INTO test1 VALUES (TRUE, 'sic est');
2868 INSERT INTO test1 VALUES (FALSE, 'non est');
2869 SELECT * FROM test1;
2875 SELECT * FROM test1 WHERE a;
2883 <sect1 id="datatype-enum">
2884 <title>Enumerated Types</title>
2886 <indexterm zone="datatype-enum">
2887 <primary>data type</primary>
2888 <secondary>enumerated (enum)</secondary>
2891 <indexterm zone="datatype-enum">
2892 <primary>enumerated types</primary>
2896 Enumerated (enum) types are data types that
2897 comprise a static, ordered set of values.
2898 They are equivalent to the <type>enum</type>
2899 types supported in a number of programming languages. An example of an enum
2900 type might be the days of the week, or a set of status values for
2905 <title>Declaration of Enumerated Types</title>
2908 Enum types are created using the <xref
2909 linkend="sql-createtype"> command,
2913 CREATE TYPE mood AS ENUM ('sad', 'ok', 'happy');
2916 Once created, the enum type can be used in table and function
2917 definitions much like any other type:
2919 CREATE TYPE mood AS ENUM ('sad', 'ok', 'happy');
2920 CREATE TABLE person (
2924 INSERT INTO person VALUES ('Moe', 'happy');
2925 SELECT * FROM person WHERE current_mood = 'happy';
2927 ------+--------------
2935 <title>Ordering</title>
2938 The ordering of the values in an enum type is the
2939 order in which the values were listed when the type was created.
2940 All standard comparison operators and related
2941 aggregate functions are supported for enums. For example:
2944 INSERT INTO person VALUES ('Larry', 'sad');
2945 INSERT INTO person VALUES ('Curly', 'ok');
2946 SELECT * FROM person WHERE current_mood > 'sad';
2948 -------+--------------
2953 SELECT * FROM person WHERE current_mood > 'sad' ORDER BY current_mood;
2955 -------+--------------
2962 WHERE current_mood = (SELECT MIN(current_mood) FROM person);
2972 <title>Type Safety</title>
2975 Each enumerated data type is separate and cannot
2976 be compared with other enumerated types. See this example:
2979 CREATE TYPE happiness AS ENUM ('happy', 'very happy', 'ecstatic');
2980 CREATE TABLE holidays (
2984 INSERT INTO holidays(num_weeks,happiness) VALUES (4, 'happy');
2985 INSERT INTO holidays(num_weeks,happiness) VALUES (6, 'very happy');
2986 INSERT INTO holidays(num_weeks,happiness) VALUES (8, 'ecstatic');
2987 INSERT INTO holidays(num_weeks,happiness) VALUES (2, 'sad');
2988 ERROR: invalid input value for enum happiness: "sad"
2989 SELECT person.name, holidays.num_weeks FROM person, holidays
2990 WHERE person.current_mood = holidays.happiness;
2991 ERROR: operator does not exist: mood = happiness
2996 If you really need to do something like that, you can either
2997 write a custom operator or add explicit casts to your query:
3000 SELECT person.name, holidays.num_weeks FROM person, holidays
3001 WHERE person.current_mood::text = holidays.happiness::text;
3012 <title>Implementation Details</title>
3015 An enum value occupies four bytes on disk. The length of an enum
3016 value's textual label is limited by the <symbol>NAMEDATALEN</symbol>
3017 setting compiled into <productname>PostgreSQL</productname>; in standard
3018 builds this means at most 63 bytes.
3022 Enum labels are case sensitive, so
3023 <type>'happy'</type> is not the same as <type>'HAPPY'</type>.
3024 White space in the labels is significant too.
3028 The translations from internal enum values to textual labels are
3029 kept in the system catalog
3030 <link linkend="catalog-pg-enum"><structname>pg_enum</structname></link>.
3031 Querying this catalog directly can be useful.
3037 <sect1 id="datatype-geometric">
3038 <title>Geometric Types</title>
3041 Geometric data types represent two-dimensional spatial
3042 objects. <xref linkend="datatype-geo-table"> shows the geometric
3043 types available in <productname>PostgreSQL</productname>. The
3044 most fundamental type, the point, forms the basis for all of the
3048 <table id="datatype-geo-table">
3049 <title>Geometric Types</title>
3054 <entry>Storage Size</entry>
3055 <entry>Representation</entry>
3056 <entry>Description</entry>
3061 <entry><type>point</type></entry>
3062 <entry>16 bytes</entry>
3063 <entry>Point on a plane</entry>
3064 <entry>(x,y)</entry>
3067 <entry><type>line</type></entry>
3068 <entry>32 bytes</entry>
3069 <entry>Infinite line (not fully implemented)</entry>
3070 <entry>((x1,y1),(x2,y2))</entry>
3073 <entry><type>lseg</type></entry>
3074 <entry>32 bytes</entry>
3075 <entry>Finite line segment</entry>
3076 <entry>((x1,y1),(x2,y2))</entry>
3079 <entry><type>box</type></entry>
3080 <entry>32 bytes</entry>
3081 <entry>Rectangular box</entry>
3082 <entry>((x1,y1),(x2,y2))</entry>
3085 <entry><type>path</type></entry>
3086 <entry>16+16n bytes</entry>
3087 <entry>Closed path (similar to polygon)</entry>
3088 <entry>((x1,y1),...)</entry>
3091 <entry><type>path</type></entry>
3092 <entry>16+16n bytes</entry>
3093 <entry>Open path</entry>
3094 <entry>[(x1,y1),...]</entry>
3097 <entry><type>polygon</type></entry>
3098 <entry>40+16n bytes</entry>
3099 <entry>Polygon (similar to closed path)</entry>
3100 <entry>((x1,y1),...)</entry>
3103 <entry><type>circle</type></entry>
3104 <entry>24 bytes</entry>
3105 <entry>Circle</entry>
3106 <entry><(x,y),r> (center point and radius)</entry>
3113 A rich set of functions and operators is available to perform various geometric
3114 operations such as scaling, translation, rotation, and determining
3115 intersections. They are explained in <xref linkend="functions-geometry">.
3119 <title>Points</title>
3122 <primary>point</primary>
3126 Points are the fundamental two-dimensional building block for geometric
3127 types. Values of type <type>point</type> are specified using either of
3128 the following syntaxes:
3131 ( <replaceable>x</replaceable> , <replaceable>y</replaceable> )
3132 <replaceable>x</replaceable> , <replaceable>y</replaceable>
3135 where <replaceable>x</> and <replaceable>y</> are the respective
3136 coordinates, as floating-point numbers.
3140 Points are output using the first syntax.
3145 <title>Line Segments</title>
3148 <primary>lseg</primary>
3152 <primary>line segment</primary>
3156 Line segments (<type>lseg</type>) are represented by pairs of points.
3157 Values of type <type>lseg</type> are specified using any of the following
3161 [ ( <replaceable>x1</replaceable> , <replaceable>y1</replaceable> ) , ( <replaceable>x2</replaceable> , <replaceable>y2</replaceable> ) ]
3162 ( ( <replaceable>x1</replaceable> , <replaceable>y1</replaceable> ) , ( <replaceable>x2</replaceable> , <replaceable>y2</replaceable> ) )
3163 ( <replaceable>x1</replaceable> , <replaceable>y1</replaceable> ) , ( <replaceable>x2</replaceable> , <replaceable>y2</replaceable> )
3164 <replaceable>x1</replaceable> , <replaceable>y1</replaceable> , <replaceable>x2</replaceable> , <replaceable>y2</replaceable>
3168 <literal>(<replaceable>x1</replaceable>,<replaceable>y1</replaceable>)</literal>
3170 <literal>(<replaceable>x2</replaceable>,<replaceable>y2</replaceable>)</literal>
3171 are the end points of the line segment.
3175 Line segments are output using the first syntax.
3180 <title>Boxes</title>
3183 <primary>box (data type)</primary>
3187 <primary>rectangle</primary>
3191 Boxes are represented by pairs of points that are opposite
3193 Values of type <type>box</type> are specified using any of the following
3197 ( ( <replaceable>x1</replaceable> , <replaceable>y1</replaceable> ) , ( <replaceable>x2</replaceable> , <replaceable>y2</replaceable> ) )
3198 ( <replaceable>x1</replaceable> , <replaceable>y1</replaceable> ) , ( <replaceable>x2</replaceable> , <replaceable>y2</replaceable> )
3199 <replaceable>x1</replaceable> , <replaceable>y1</replaceable> , <replaceable>x2</replaceable> , <replaceable>y2</replaceable>
3203 <literal>(<replaceable>x1</replaceable>,<replaceable>y1</replaceable>)</literal>
3205 <literal>(<replaceable>x2</replaceable>,<replaceable>y2</replaceable>)</literal>
3206 are any two opposite corners of the box.
3210 Boxes are output using the second syntax.
3214 Any two opposite corners can be supplied on input, but the values
3215 will be reordered as needed to store the
3216 upper right and lower left corners, in that order.
3221 <title>Paths</title>
3224 <primary>path (data type)</primary>
3228 Paths are represented by lists of connected points. Paths can be
3229 <firstterm>open</firstterm>, where
3230 the first and last points in the list are considered not connected, or
3231 <firstterm>closed</firstterm>,
3232 where the first and last points are considered connected.
3236 Values of type <type>path</type> are specified using any of the following
3240 [ ( <replaceable>x1</replaceable> , <replaceable>y1</replaceable> ) , ... , ( <replaceable>xn</replaceable> , <replaceable>yn</replaceable> ) ]
3241 ( ( <replaceable>x1</replaceable> , <replaceable>y1</replaceable> ) , ... , ( <replaceable>xn</replaceable> , <replaceable>yn</replaceable> ) )
3242 ( <replaceable>x1</replaceable> , <replaceable>y1</replaceable> ) , ... , ( <replaceable>xn</replaceable> , <replaceable>yn</replaceable> )
3243 ( <replaceable>x1</replaceable> , <replaceable>y1</replaceable> , ... , <replaceable>xn</replaceable> , <replaceable>yn</replaceable> )
3244 <replaceable>x1</replaceable> , <replaceable>y1</replaceable> , ... , <replaceable>xn</replaceable> , <replaceable>yn</replaceable>
3247 where the points are the end points of the line segments
3248 comprising the path. Square brackets (<literal>[]</>) indicate
3249 an open path, while parentheses (<literal>()</>) indicate a
3250 closed path. When the outermost parentheses are omitted, as
3251 in the third through fifth syntaxes, a closed path is assumed.
3255 Paths are output using the first or second syntax, as appropriate.
3260 <title>Polygons</title>
3263 <primary>polygon</primary>
3267 Polygons are represented by lists of points (the vertexes of the
3268 polygon). Polygons are very similar to closed paths, but are
3269 stored differently and have their own set of support routines.
3273 Values of type <type>polygon</type> are specified using any of the
3277 ( ( <replaceable>x1</replaceable> , <replaceable>y1</replaceable> ) , ... , ( <replaceable>xn</replaceable> , <replaceable>yn</replaceable> ) )
3278 ( <replaceable>x1</replaceable> , <replaceable>y1</replaceable> ) , ... , ( <replaceable>xn</replaceable> , <replaceable>yn</replaceable> )
3279 ( <replaceable>x1</replaceable> , <replaceable>y1</replaceable> , ... , <replaceable>xn</replaceable> , <replaceable>yn</replaceable> )
3280 <replaceable>x1</replaceable> , <replaceable>y1</replaceable> , ... , <replaceable>xn</replaceable> , <replaceable>yn</replaceable>
3283 where the points are the end points of the line segments
3284 comprising the boundary of the polygon.
3288 Polygons are output using the first syntax.
3293 <title>Circles</title>
3296 <primary>circle</primary>
3300 Circles are represented by a center point and radius.
3301 Values of type <type>circle</type> are specified using any of the
3305 < ( <replaceable>x</replaceable> , <replaceable>y</replaceable> ) , <replaceable>r</replaceable> >
3306 ( ( <replaceable>x</replaceable> , <replaceable>y</replaceable> ) , <replaceable>r</replaceable> )
3307 ( <replaceable>x</replaceable> , <replaceable>y</replaceable> ) , <replaceable>r</replaceable>
3308 <replaceable>x</replaceable> , <replaceable>y</replaceable> , <replaceable>r</replaceable>
3312 <literal>(<replaceable>x</replaceable>,<replaceable>y</replaceable>)</>
3313 is the center point and <replaceable>r</replaceable> is the radius of the
3318 Circles are output using the first syntax.
3324 <sect1 id="datatype-net-types">
3325 <title>Network Address Types</title>
3327 <indexterm zone="datatype-net-types">
3328 <primary>network</primary>
3329 <secondary>data types</secondary>
3333 <productname>PostgreSQL</> offers data types to store IPv4, IPv6, and MAC
3334 addresses, as shown in <xref linkend="datatype-net-types-table">. It
3335 is better to use these types instead of plain text types to store
3336 network addresses, because
3337 these types offer input error checking and specialized
3338 operators and functions (see <xref linkend="functions-net">).
3341 <table tocentry="1" id="datatype-net-types-table">
3342 <title>Network Address Types</title>
3347 <entry>Storage Size</entry>
3348 <entry>Description</entry>
3354 <entry><type>cidr</type></entry>
3355 <entry>7 or 19 bytes</entry>
3356 <entry>IPv4 and IPv6 networks</entry>
3360 <entry><type>inet</type></entry>
3361 <entry>7 or 19 bytes</entry>
3362 <entry>IPv4 and IPv6 hosts and networks</entry>
3366 <entry><type>macaddr</type></entry>
3367 <entry>6 bytes</entry>
3368 <entry>MAC addresses</entry>
3376 When sorting <type>inet</type> or <type>cidr</type> data types,
3377 IPv4 addresses will always sort before IPv6 addresses, including
3378 IPv4 addresses encapsulated or mapped to IPv6 addresses, such as
3379 ::10.2.3.4 or ::ffff:10.4.3.2.
3383 <sect2 id="datatype-inet">
3384 <title><type>inet</type></title>
3387 <primary>inet (data type)</primary>
3391 The <type>inet</type> type holds an IPv4 or IPv6 host address, and
3392 optionally its subnet, all in one field.
3393 The subnet is represented by the number of network address bits
3394 present in the host address (the
3395 <quote>netmask</quote>). If the netmask is 32 and the address is IPv4,
3396 then the value does not indicate a subnet, only a single host.
3397 In IPv6, the address length is 128 bits, so 128 bits specify a
3398 unique host address. Note that if you
3399 want to accept only networks, you should use the
3400 <type>cidr</type> type rather than <type>inet</type>.
3404 The input format for this type is
3405 <replaceable class="parameter">address/y</replaceable>
3407 <replaceable class="parameter">address</replaceable>
3408 is an IPv4 or IPv6 address and
3409 <replaceable class="parameter">y</replaceable>
3410 is the number of bits in the netmask. If the
3411 <replaceable class="parameter">/y</replaceable>
3412 portion is missing, the
3413 netmask is 32 for IPv4 and 128 for IPv6, so the value represents
3414 just a single host. On display, the
3415 <replaceable class="parameter">/y</replaceable>
3416 portion is suppressed if the netmask specifies a single host.
3420 <sect2 id="datatype-cidr">
3421 <title><type>cidr</></title>
3424 <primary>cidr</primary>
3428 The <type>cidr</type> type holds an IPv4 or IPv6 network specification.
3429 Input and output formats follow Classless Internet Domain Routing
3431 The format for specifying networks is <replaceable
3432 class="parameter">address/y</> where <replaceable
3433 class="parameter">address</> is the network represented as an
3434 IPv4 or IPv6 address, and <replaceable
3435 class="parameter">y</> is the number of bits in the netmask. If
3436 <replaceable class="parameter">y</> is omitted, it is calculated
3437 using assumptions from the older classful network numbering system, except
3438 it will be at least large enough to include all of the octets
3439 written in the input. It is an error to specify a network address
3440 that has bits set to the right of the specified netmask.
3444 <xref linkend="datatype-net-cidr-table"> shows some examples.
3447 <table id="datatype-net-cidr-table">
3448 <title><type>cidr</> Type Input Examples</title>
3452 <entry><type>cidr</type> Input</entry>
3453 <entry><type>cidr</type> Output</entry>
3454 <entry><literal><function>abbrev(<type>cidr</type>)</function></literal></entry>
3459 <entry>192.168.100.128/25</entry>
3460 <entry>192.168.100.128/25</entry>
3461 <entry>192.168.100.128/25</entry>
3464 <entry>192.168/24</entry>
3465 <entry>192.168.0.0/24</entry>
3466 <entry>192.168.0/24</entry>
3469 <entry>192.168/25</entry>
3470 <entry>192.168.0.0/25</entry>
3471 <entry>192.168.0.0/25</entry>
3474 <entry>192.168.1</entry>
3475 <entry>192.168.1.0/24</entry>
3476 <entry>192.168.1/24</entry>
3479 <entry>192.168</entry>
3480 <entry>192.168.0.0/24</entry>
3481 <entry>192.168.0/24</entry>
3484 <entry>128.1</entry>
3485 <entry>128.1.0.0/16</entry>
3486 <entry>128.1/16</entry>
3490 <entry>128.0.0.0/16</entry>
3491 <entry>128.0/16</entry>
3494 <entry>128.1.2</entry>
3495 <entry>128.1.2.0/24</entry>
3496 <entry>128.1.2/24</entry>
3499 <entry>10.1.2</entry>
3500 <entry>10.1.2.0/24</entry>
3501 <entry>10.1.2/24</entry>
3505 <entry>10.1.0.0/16</entry>
3506 <entry>10.1/16</entry>
3510 <entry>10.0.0.0/8</entry>
3514 <entry>10.1.2.3/32</entry>
3515 <entry>10.1.2.3/32</entry>
3516 <entry>10.1.2.3/32</entry>
3519 <entry>2001:4f8:3:ba::/64</entry>
3520 <entry>2001:4f8:3:ba::/64</entry>
3521 <entry>2001:4f8:3:ba::/64</entry>
3524 <entry>2001:4f8:3:ba:2e0:81ff:fe22:d1f1/128</entry>
3525 <entry>2001:4f8:3:ba:2e0:81ff:fe22:d1f1/128</entry>
3526 <entry>2001:4f8:3:ba:2e0:81ff:fe22:d1f1</entry>
3529 <entry>::ffff:1.2.3.0/120</entry>
3530 <entry>::ffff:1.2.3.0/120</entry>
3531 <entry>::ffff:1.2.3/120</entry>
3534 <entry>::ffff:1.2.3.0/128</entry>
3535 <entry>::ffff:1.2.3.0/128</entry>
3536 <entry>::ffff:1.2.3.0/128</entry>
3543 <sect2 id="datatype-inet-vs-cidr">
3544 <title><type>inet</type> vs. <type>cidr</type></title>
3547 The essential difference between <type>inet</type> and <type>cidr</type>
3548 data types is that <type>inet</type> accepts values with nonzero bits to
3549 the right of the netmask, whereas <type>cidr</type> does not.
3554 If you do not like the output format for <type>inet</type> or
3555 <type>cidr</type> values, try the functions <function>host</>,
3556 <function>text</>, and <function>abbrev</>.
3561 <sect2 id="datatype-macaddr">
3562 <title><type>macaddr</type></title>
3565 <primary>macaddr (data type)</primary>
3569 <primary>MAC address</primary>
3574 The <type>macaddr</> type stores MAC addresses, known for example
3575 from Ethernet card hardware addresses (although MAC addresses are
3576 used for other purposes as well). Input is accepted in the
3580 <member><literal>'08:00:2b:01:02:03'</></member>
3581 <member><literal>'08-00-2b-01-02-03'</></member>
3582 <member><literal>'08002b:010203'</></member>
3583 <member><literal>'08002b-010203'</></member>
3584 <member><literal>'0800.2b01.0203'</></member>
3585 <member><literal>'08002b010203'</></member>
3588 These examples would all specify the same address. Upper and
3589 lower case is accepted for the digits
3590 <literal>a</> through <literal>f</>. Output is always in the
3591 first of the forms shown.
3595 IEEE Std 802-2001 specifies the second shown form (with hyphens)
3596 as the canonical form for MAC addresses, and specifies the first
3597 form (with colons) as the bit-reversed notation, so that
3598 08-00-2b-01-02-03 = 01:00:4D:08:04:0C. This convention is widely
3599 ignored nowadays, and it is relevant only for obsolete network
3600 protocols (such as Token Ring). PostgreSQL makes no provisions
3601 for bit reversal, and all accepted formats use the canonical LSB
3606 The remaining four input formats are not part of any standard.
3612 <sect1 id="datatype-bit">
3613 <title>Bit String Types</title>
3615 <indexterm zone="datatype-bit">
3616 <primary>bit string</primary>
3617 <secondary>data type</secondary>
3621 Bit strings are strings of 1's and 0's. They can be used to store
3622 or visualize bit masks. There are two SQL bit types:
3623 <type>bit(<replaceable>n</replaceable>)</type> and <type>bit
3624 varying(<replaceable>n</replaceable>)</type>, where
3625 <replaceable>n</replaceable> is a positive integer.
3629 <type>bit</type> type data must match the length
3630 <replaceable>n</replaceable> exactly; it is an error to attempt to
3631 store shorter or longer bit strings. <type>bit varying</type> data is
3632 of variable length up to the maximum length
3633 <replaceable>n</replaceable>; longer strings will be rejected.
3634 Writing <type>bit</type> without a length is equivalent to
3635 <literal>bit(1)</literal>, while <type>bit varying</type> without a length
3636 specification means unlimited length.
3641 If one explicitly casts a bit-string value to
3642 <type>bit(<replaceable>n</>)</type>, it will be truncated or
3643 zero-padded on the right to be exactly <replaceable>n</> bits,
3644 without raising an error. Similarly,
3645 if one explicitly casts a bit-string value to
3646 <type>bit varying(<replaceable>n</>)</type>, it will be truncated
3647 on the right if it is more than <replaceable>n</> bits.
3653 linkend="sql-syntax-bit-strings"> for information about the syntax
3654 of bit string constants. Bit-logical operators and string
3655 manipulation functions are available; see <xref
3656 linkend="functions-bitstring">.
3660 <title>Using the Bit String Types</title>
3663 CREATE TABLE test (a BIT(3), b BIT VARYING(5));
3664 INSERT INTO test VALUES (B'101', B'00');
3665 INSERT INTO test VALUES (B'10', B'101');
3667 ERROR: bit string length 2 does not match type bit(3)
3669 INSERT INTO test VALUES (B'10'::bit(3), B'101');
3681 A bit string value requires 1 byte for each group of 8 bits, plus
3682 5 or 8 bytes overhead depending on the length of the string
3683 (but long values may be compressed or moved out-of-line, as explained
3684 in <xref linkend="datatype-character"> for character strings).
3688 <sect1 id="datatype-textsearch">
3689 <title>Text Search Types</title>
3691 <indexterm zone="datatype-textsearch">
3692 <primary>full text search</primary>
3693 <secondary>data types</secondary>
3696 <indexterm zone="datatype-textsearch">
3697 <primary>text search</primary>
3698 <secondary>data types</secondary>
3702 <productname>PostgreSQL</productname> provides two data types that
3703 are designed to support full text search, which is the activity of
3704 searching through a collection of natural-language <firstterm>documents</>
3705 to locate those that best match a <firstterm>query</>.
3706 The <type>tsvector</type> type represents a document in a form optimized
3707 for text search; the <type>tsquery</type> type similarly represents
3709 <xref linkend="textsearch"> provides a detailed explanation of this
3710 facility, and <xref linkend="functions-textsearch"> summarizes the
3711 related functions and operators.
3714 <sect2 id="datatype-tsvector">
3715 <title><type>tsvector</type></title>
3718 <primary>tsvector (data type)</primary>
3722 A <type>tsvector</type> value is a sorted list of distinct
3723 <firstterm>lexemes</>, which are words that have been
3724 <firstterm>normalized</> to merge different variants of the same word
3725 (see <xref linkend="textsearch"> for details). Sorting and
3726 duplicate-elimination are done automatically during input, as shown in
3730 SELECT 'a fat cat sat on a mat and ate a fat rat'::tsvector;
3732 ----------------------------------------------------
3733 'a' 'and' 'ate' 'cat' 'fat' 'mat' 'on' 'rat' 'sat'
3737 lexemes containing whitespace or punctuation, surround them with quotes:
3740 SELECT $$the lexeme ' ' contains spaces$$::tsvector;
3742 -------------------------------------------
3743 ' ' 'contains' 'lexeme' 'spaces' 'the'
3746 (We use dollar-quoted string literals in this example and the next one
3747 to avoid the confusion of having to double quote marks within the
3748 literals.) Embedded quotes and backslashes must be doubled:
3751 SELECT $$the lexeme 'Joe''s' contains a quote$$::tsvector;
3753 ------------------------------------------------
3754 'Joe''s' 'a' 'contains' 'lexeme' 'quote' 'the'
3757 Optionally, integer <firstterm>positions</>
3758 can be attached to lexemes:
3761 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;
3763 -------------------------------------------------------------------------------
3764 'a':1,6,10 'and':8 'ate':9 'cat':3 'fat':2,11 'mat':7 'on':5 'rat':12 'sat':4
3767 A position normally indicates the source word's location in the
3768 document. Positional information can be used for
3769 <firstterm>proximity ranking</firstterm>. Position values can
3770 range from 1 to 16383; larger numbers are silently set to 16383.
3771 Duplicate positions for the same lexeme are discarded.
3775 Lexemes that have positions can further be labeled with a
3776 <firstterm>weight</>, which can be <literal>A</literal>,
3777 <literal>B</literal>, <literal>C</literal>, or <literal>D</literal>.
3778 <literal>D</literal> is the default and hence is not shown on output:
3781 SELECT 'a:1A fat:2B,4C cat:5D'::tsvector;
3783 ----------------------------
3784 'a':1A 'cat':5 'fat':2B,4C
3787 Weights are typically used to reflect document structure, for example
3788 by marking title words differently from body words. Text search
3789 ranking functions can assign different priorities to the different
3794 It is important to understand that the
3795 <type>tsvector</type> type itself does not perform any normalization;
3796 it assumes the words it is given are normalized appropriately
3797 for the application. For example,
3800 select 'The Fat Rats'::tsvector;
3802 --------------------
3806 For most English-text-searching applications the above words would
3807 be considered non-normalized, but <type>tsvector</type> doesn't care.
3808 Raw document text should usually be passed through
3809 <function>to_tsvector</> to normalize the words appropriately
3813 SELECT to_tsvector('english', 'The Fat Rats');
3819 Again, see <xref linkend="textsearch"> for more detail.
3824 <sect2 id="datatype-tsquery">
3825 <title><type>tsquery</type></title>
3828 <primary>tsquery (data type)</primary>
3832 A <type>tsquery</type> value stores lexemes that are to be
3833 searched for, and combines them honoring the Boolean operators
3834 <literal>&</literal> (AND), <literal>|</literal> (OR), and
3835 <literal>!</> (NOT). Parentheses can be used to enforce grouping
3839 SELECT 'fat & rat'::tsquery;
3844 SELECT 'fat & (rat | cat)'::tsquery;
3846 ---------------------------
3847 'fat' & ( 'rat' | 'cat' )
3849 SELECT 'fat & rat & ! cat'::tsquery;
3851 ------------------------
3852 'fat' & 'rat' & !'cat'
3855 In the absence of parentheses, <literal>!</> (NOT) binds most tightly,
3856 and <literal>&</literal> (AND) binds more tightly than
3857 <literal>|</literal> (OR).
3861 Optionally, lexemes in a <type>tsquery</type> can be labeled with
3862 one or more weight letters, which restricts them to match only
3863 <type>tsvector</> lexemes with matching weights:
3866 SELECT 'fat:ab & cat'::tsquery;
3869 'fat':AB & 'cat'
3874 Also, lexemes in a <type>tsquery</type> can be labeled with <literal>*</>
3875 to specify prefix matching:
3877 SELECT 'super:*'::tsquery;
3882 This query will match any word in a <type>tsvector</> that begins
3883 with <quote>super</>. Note that prefixes are first processed by
3884 text search configurations, which means this comparison returns
3887 SELECT to_tsvector( 'postgraduate' ) @@ to_tsquery( 'postgres:*' );
3893 because <literal>postgres</> gets stemmed to <literal>postgr</>:
3895 SELECT to_tsquery('postgres:*');
3901 which then matches <literal>postgraduate</>.
3905 Quoting rules for lexemes are the same as described previously for
3906 lexemes in <type>tsvector</>; and, as with <type>tsvector</>,
3907 any required normalization of words must be done before converting
3908 to the <type>tsquery</> type. The <function>to_tsquery</>
3909 function is convenient for performing such normalization:
3912 SELECT to_tsquery('Fat:ab & Cats');
3915 'fat':AB & 'cat'
3923 <sect1 id="datatype-uuid">
3924 <title><acronym>UUID</acronym> Type</title>
3926 <indexterm zone="datatype-uuid">
3927 <primary>UUID</primary>
3931 The data type <type>uuid</type> stores Universally Unique Identifiers
3932 (UUID) as defined by RFC 4122, ISO/IEC 9834-8:2005, and related standards.
3933 (Some systems refer to this data type as a globally unique identifier, or
3934 GUID,<indexterm><primary>GUID</primary></indexterm> instead.) This
3935 identifier is a 128-bit quantity that is generated by an algorithm chosen
3936 to make it very unlikely that the same identifier will be generated by
3937 anyone else in the known universe using the same algorithm. Therefore,
3938 for distributed systems, these identifiers provide a better uniqueness
3939 guarantee than sequence generators, which
3940 are only unique within a single database.
3944 A UUID is written as a sequence of lower-case hexadecimal digits,
3945 in several groups separated by hyphens, specifically a group of 8
3946 digits followed by three groups of 4 digits followed by a group of
3947 12 digits, for a total of 32 digits representing the 128 bits. An
3948 example of a UUID in this standard form is:
3950 a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11
3952 <productname>PostgreSQL</productname> also accepts the following
3953 alternative forms for input:
3954 use of upper-case digits, the standard format surrounded by
3955 braces, omitting some or all hyphens, adding a hyphen after any
3956 group of four digits. Examples are:
3958 A0EEBC99-9C0B-4EF8-BB6D-6BB9BD380A11
3959 {a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11}
3960 a0eebc999c0b4ef8bb6d6bb9bd380a11
3961 a0ee-bc99-9c0b-4ef8-bb6d-6bb9-bd38-0a11
3962 {a0eebc99-9c0b4ef8-bb6d6bb9-bd380a11}
3964 Output is always in the standard form.
3968 <productname>PostgreSQL</productname> provides storage and comparison
3969 functions for UUIDs, but the core database does not include any
3970 function for generating UUIDs, because no single algorithm is well
3971 suited for every application. The <xref
3972 linkend="uuid-ossp"> module
3973 provides functions that implement several standard algorithms.
3974 Alternatively, UUIDs could be generated by client applications or
3975 other libraries invoked through a server-side function.
3979 <sect1 id="datatype-xml">
3980 <title><acronym>XML</> Type</title>
3982 <indexterm zone="datatype-xml">
3983 <primary>XML</primary>
3987 The <type>xml</type> data type can be used to store XML data. Its
3988 advantage over storing XML data in a <type>text</type> field is that it
3989 checks the input values for well-formedness, and there are support
3990 functions to perform type-safe operations on it; see <xref
3991 linkend="functions-xml">. Use of this data type requires the
3992 installation to have been built with <command>configure
3997 The <type>xml</type> type can store well-formed
3998 <quote>documents</quote>, as defined by the XML standard, as well
3999 as <quote>content</quote> fragments, which are defined by the
4000 production <literal>XMLDecl? content</literal> in the XML
4001 standard. Roughly, this means that content fragments can have
4002 more than one top-level element or character node. The expression
4003 <literal><replaceable>xmlvalue</replaceable> IS DOCUMENT</literal>
4004 can be used to evaluate whether a particular <type>xml</type>
4005 value is a full document or only a content fragment.
4009 <title>Creating XML Values</title>
4011 To produce a value of type <type>xml</type> from character data,
4013 <function>xmlparse</function>:<indexterm><primary>xmlparse</primary></indexterm>
4015 XMLPARSE ( { DOCUMENT | CONTENT } <replaceable>value</replaceable>)
4018 <programlisting><![CDATA[
4019 XMLPARSE (DOCUMENT '<?xml version="1.0"?><book><title>Manual</title><chapter>...</chapter></book>')
4020 XMLPARSE (CONTENT 'abc<foo>bar</foo><bar>foo</bar>')
4021 ]]></programlisting>
4022 While this is the only way to convert character strings into XML
4023 values according to the SQL standard, the PostgreSQL-specific
4025 <programlisting><![CDATA[
4026 xml '<foo>bar</foo>'
4027 '<foo>bar</foo>'::xml
4028 ]]></programlisting>
4033 The <type>xml</type> type does not validate input values
4034 against a document type declaration
4035 (DTD),<indexterm><primary>DTD</primary></indexterm>
4036 even when the input value specifies a DTD.
4037 There is also currently no built-in support for validating against
4038 other XML schema languages such as XML Schema.
4042 The inverse operation, producing a character string value from
4043 <type>xml</type>, uses the function
4044 <function>xmlserialize</function>:<indexterm><primary>xmlserialize</primary></indexterm>
4046 XMLSERIALIZE ( { DOCUMENT | CONTENT } <replaceable>value</replaceable> AS <replaceable>type</replaceable> )
4048 <replaceable>type</replaceable> can be
4049 <type>character</type>, <type>character varying</type>, or
4050 <type>text</type> (or an alias for one of those). Again, according
4051 to the SQL standard, this is the only way to convert between type
4052 <type>xml</type> and character types, but PostgreSQL also allows
4053 you to simply cast the value.
4057 When a character string value is cast to or from type
4058 <type>xml</type> without going through <type>XMLPARSE</type> or
4059 <type>XMLSERIALIZE</type>, respectively, the choice of
4060 <literal>DOCUMENT</literal> versus <literal>CONTENT</literal> is
4061 determined by the <quote>XML option</quote>
4062 <indexterm><primary>XML option</primary></indexterm>
4063 session configuration parameter, which can be set using the
4066 SET XML OPTION { DOCUMENT | CONTENT };
4068 or the more PostgreSQL-like syntax
4070 SET xmloption TO { DOCUMENT | CONTENT };
4072 The default is <literal>CONTENT</literal>, so all forms of XML
4078 With the default XML option setting, you cannot directly cast
4079 character strings to type <type>xml</type> if they contain a
4080 document type declaration, because the definition of XML content
4081 fragment does not accept them. If you need to do that, either
4082 use <literal>XMLPARSE</literal> or change the XML option.
4089 <title>Encoding Handling</title>
4091 Care must be taken when dealing with multiple character encodings
4092 on the client, server, and in the XML data passed through them.
4093 When using the text mode to pass queries to the server and query
4094 results to the client (which is the normal mode), PostgreSQL
4095 converts all character data passed between the client and the
4096 server and vice versa to the character encoding of the respective
4097 end; see <xref linkend="multibyte">. This includes string
4098 representations of XML values, such as in the above examples.
4099 This would ordinarily mean that encoding declarations contained in
4100 XML data can become invalid as the character data is converted
4101 to other encodings while traveling between client and server,
4102 because the embedded encoding declaration is not changed. To cope
4103 with this behavior, encoding declarations contained in
4104 character strings presented for input to the <type>xml</type> type
4105 are <emphasis>ignored</emphasis>, and content is assumed
4106 to be in the current server encoding. Consequently, for correct
4107 processing, character strings of XML data must be sent
4108 from the client in the current client encoding. It is the
4109 responsibility of the client to either convert documents to the
4110 current client encoding before sending them to the server, or to
4111 adjust the client encoding appropriately. On output, values of
4112 type <type>xml</type> will not have an encoding declaration, and
4113 clients should assume all data is in the current client
4118 When using binary mode to pass query parameters to the server
4119 and query results back to the client, no character set conversion
4120 is performed, so the situation is different. In this case, an
4121 encoding declaration in the XML data will be observed, and if it
4122 is absent, the data will be assumed to be in UTF-8 (as required by
4123 the XML standard; note that PostgreSQL does not support UTF-16).
4124 On output, data will have an encoding declaration
4125 specifying the client encoding, unless the client encoding is
4126 UTF-8, in which case it will be omitted.
4130 Needless to say, processing XML data with PostgreSQL will be less
4131 error-prone and more efficient if the XML data encoding, client encoding,
4132 and server encoding are the same. Since XML data is internally
4133 processed in UTF-8, computations will be most efficient if the
4134 server encoding is also UTF-8.
4139 Some XML-related functions may not work at all on non-ASCII data
4140 when the server encoding is not UTF-8. This is known to be an
4141 issue for <function>xpath()</> in particular.
4147 <title>Accessing XML Values</title>
4150 The <type>xml</type> data type is unusual in that it does not
4151 provide any comparison operators. This is because there is no
4152 well-defined and universally useful comparison algorithm for XML
4153 data. One consequence of this is that you cannot retrieve rows by
4154 comparing an <type>xml</type> column against a search value. XML
4155 values should therefore typically be accompanied by a separate key
4156 field such as an ID. An alternative solution for comparing XML
4157 values is to convert them to character strings first, but note
4158 that character string comparison has little to do with a useful
4159 XML comparison method.
4163 Since there are no comparison operators for the <type>xml</type>
4164 data type, it is not possible to create an index directly on a
4165 column of this type. If speedy searches in XML data are desired,
4166 possible workarounds include casting the expression to a
4167 character string type and indexing that, or indexing an XPath
4168 expression. Of course, the actual query would have to be adjusted
4169 to search by the indexed expression.
4173 The text-search functionality in PostgreSQL can also be used to speed
4174 up full-document searches of XML data. The necessary
4175 preprocessing support is, however, not yet available in the PostgreSQL
4181 <sect1 id="datatype-json">
4182 <title><acronym>JSON</> Type</title>
4184 <indexterm zone="datatype-json">
4185 <primary>JSON</primary>
4189 The <type>json</type> data type can be used to store JSON (JavaScript
4190 Object Notation) data, as specified in <ulink
4191 url="http://www.ietf.org/rfc/rfc4627.txt">RFC 4627</ulink>. Such
4192 data can also be stored as <type>text</type>, but the
4193 <type>json</type> data type has the advantage of checking that each
4194 stored value is a valid JSON value. There are also related support
4195 functions available; see <xref linkend="functions-json">.
4199 <productname>PostgreSQL</productname> allows only one server encoding
4200 per database. It is therefore not possible for JSON to conform rigidly
4201 to the specification unless the server encoding is UTF-8. Attempts to
4202 directly include characters which cannot be represented in the server
4203 encoding will fail; conversely, characters which can be represented in
4204 the server encoding but not in UTF-8 will be allowed.
4205 <literal>\uXXXX</literal> escapes are allowed regardless of the server
4206 encoding, and are checked only for syntactic correctness.
4216 <sect1 id="datatype-oid">
4217 <title>Object Identifier Types</title>
4219 <indexterm zone="datatype-oid">
4220 <primary>object identifier</primary>
4221 <secondary>data type</secondary>
4224 <indexterm zone="datatype-oid">
4225 <primary>oid</primary>
4228 <indexterm zone="datatype-oid">
4229 <primary>regproc</primary>
4232 <indexterm zone="datatype-oid">
4233 <primary>regprocedure</primary>
4236 <indexterm zone="datatype-oid">
4237 <primary>regoper</primary>
4240 <indexterm zone="datatype-oid">
4241 <primary>regoperator</primary>
4244 <indexterm zone="datatype-oid">
4245 <primary>regclass</primary>
4248 <indexterm zone="datatype-oid">
4249 <primary>regtype</primary>
4252 <indexterm zone="datatype-oid">
4253 <primary>regconfig</primary>
4256 <indexterm zone="datatype-oid">
4257 <primary>regdictionary</primary>
4260 <indexterm zone="datatype-oid">
4261 <primary>xid</primary>
4264 <indexterm zone="datatype-oid">
4265 <primary>cid</primary>
4268 <indexterm zone="datatype-oid">
4269 <primary>tid</primary>
4273 Object identifiers (OIDs) are used internally by
4274 <productname>PostgreSQL</productname> as primary keys for various
4275 system tables. OIDs are not added to user-created tables, unless
4276 <literal>WITH OIDS</literal> is specified when the table is
4277 created, or the <xref linkend="guc-default-with-oids">
4278 configuration variable is enabled. Type <type>oid</> represents
4279 an object identifier. There are also several alias types for
4280 <type>oid</>: <type>regproc</>, <type>regprocedure</>,
4281 <type>regoper</>, <type>regoperator</>, <type>regclass</>,
4282 <type>regtype</>, <type>regconfig</>, and <type>regdictionary</>.
4283 <xref linkend="datatype-oid-table"> shows an overview.
4287 The <type>oid</> type is currently implemented as an unsigned
4288 four-byte integer. Therefore, it is not large enough to provide
4289 database-wide uniqueness in large databases, or even in large
4290 individual tables. So, using a user-created table's OID column as
4291 a primary key is discouraged. OIDs are best used only for
4292 references to system tables.
4296 The <type>oid</> type itself has few operations beyond comparison.
4297 It can be cast to integer, however, and then manipulated using the
4298 standard integer operators. (Beware of possible
4299 signed-versus-unsigned confusion if you do this.)
4303 The OID alias types have no operations of their own except
4304 for specialized input and output routines. These routines are able
4305 to accept and display symbolic names for system objects, rather than
4306 the raw numeric value that type <type>oid</> would use. The alias
4307 types allow simplified lookup of OID values for objects. For example,
4308 to examine the <structname>pg_attribute</> rows related to a table
4309 <literal>mytable</>, one could write:
4311 SELECT * FROM pg_attribute WHERE attrelid = 'mytable'::regclass;
4315 SELECT * FROM pg_attribute
4316 WHERE attrelid = (SELECT oid FROM pg_class WHERE relname = 'mytable');
4318 While that doesn't look all that bad by itself, it's still oversimplified.
4319 A far more complicated sub-select would be needed to
4320 select the right OID if there are multiple tables named
4321 <literal>mytable</> in different schemas.
4322 The <type>regclass</> input converter handles the table lookup according
4323 to the schema path setting, and so it does the <quote>right thing</>
4324 automatically. Similarly, casting a table's OID to
4325 <type>regclass</> is handy for symbolic display of a numeric OID.
4328 <table id="datatype-oid-table">
4329 <title>Object Identifier Types</title>
4334 <entry>References</entry>
4335 <entry>Description</entry>
4336 <entry>Value Example</entry>
4343 <entry><type>oid</></entry>
4345 <entry>numeric object identifier</entry>
4346 <entry><literal>564182</></entry>
4350 <entry><type>regproc</></entry>
4351 <entry><structname>pg_proc</></entry>
4352 <entry>function name</entry>
4353 <entry><literal>sum</></entry>
4357 <entry><type>regprocedure</></entry>
4358 <entry><structname>pg_proc</></entry>
4359 <entry>function with argument types</entry>
4360 <entry><literal>sum(int4)</></entry>
4364 <entry><type>regoper</></entry>
4365 <entry><structname>pg_operator</></entry>
4366 <entry>operator name</entry>
4367 <entry><literal>+</></entry>
4371 <entry><type>regoperator</></entry>
4372 <entry><structname>pg_operator</></entry>
4373 <entry>operator with argument types</entry>
4374 <entry><literal>*(integer,integer)</> or <literal>-(NONE,integer)</></entry>
4378 <entry><type>regclass</></entry>
4379 <entry><structname>pg_class</></entry>
4380 <entry>relation name</entry>
4381 <entry><literal>pg_type</></entry>
4385 <entry><type>regtype</></entry>
4386 <entry><structname>pg_type</></entry>
4387 <entry>data type name</entry>
4388 <entry><literal>integer</></entry>
4392 <entry><type>regconfig</></entry>
4393 <entry><structname>pg_ts_config</></entry>
4394 <entry>text search configuration</entry>
4395 <entry><literal>english</></entry>
4399 <entry><type>regdictionary</></entry>
4400 <entry><structname>pg_ts_dict</></entry>
4401 <entry>text search dictionary</entry>
4402 <entry><literal>simple</></entry>
4409 All of the OID alias types accept schema-qualified names, and will
4410 display schema-qualified names on output if the object would not
4411 be found in the current search path without being qualified.
4412 The <type>regproc</> and <type>regoper</> alias types will only
4413 accept input names that are unique (not overloaded), so they are
4414 of limited use; for most uses <type>regprocedure</> or
4415 <type>regoperator</> are more appropriate. For <type>regoperator</>,
4416 unary operators are identified by writing <literal>NONE</> for the unused
4421 An additional property of the OID alias types is the creation of
4423 constant of one of these types appears in a stored expression
4424 (such as a column default expression or view), it creates a dependency
4425 on the referenced object. For example, if a column has a default
4426 expression <literal>nextval('my_seq'::regclass)</>,
4427 <productname>PostgreSQL</productname>
4428 understands that the default expression depends on the sequence
4429 <literal>my_seq</>; the system will not let the sequence be dropped
4430 without first removing the default expression.
4434 Another identifier type used by the system is <type>xid</>, or transaction
4435 (abbreviated <abbrev>xact</>) identifier. This is the data type of the system columns
4436 <structfield>xmin</> and <structfield>xmax</>. Transaction identifiers are 32-bit quantities.
4440 A third identifier type used by the system is <type>cid</>, or
4441 command identifier. This is the data type of the system columns
4442 <structfield>cmin</> and <structfield>cmax</>. Command identifiers are also 32-bit quantities.
4446 A final identifier type used by the system is <type>tid</>, or tuple
4447 identifier (row identifier). This is the data type of the system column
4448 <structfield>ctid</>. A tuple ID is a pair
4449 (block number, tuple index within block) that identifies the
4450 physical location of the row within its table.
4454 (The system columns are further explained in <xref
4455 linkend="ddl-system-columns">.)
4459 <sect1 id="datatype-pseudo">
4460 <title>Pseudo-Types</title>
4462 <indexterm zone="datatype-pseudo">
4463 <primary>record</primary>
4466 <indexterm zone="datatype-pseudo">
4467 <primary>any</primary>
4470 <indexterm zone="datatype-pseudo">
4471 <primary>anyelement</primary>
4474 <indexterm zone="datatype-pseudo">
4475 <primary>anyarray</primary>
4478 <indexterm zone="datatype-pseudo">
4479 <primary>anynonarray</primary>
4482 <indexterm zone="datatype-pseudo">
4483 <primary>anyenum</primary>
4486 <indexterm zone="datatype-pseudo">
4487 <primary>anyrange</primary>
4490 <indexterm zone="datatype-pseudo">
4491 <primary>void</primary>
4494 <indexterm zone="datatype-pseudo">
4495 <primary>trigger</primary>
4498 <indexterm zone="datatype-pseudo">
4499 <primary>language_handler</primary>
4502 <indexterm zone="datatype-pseudo">
4503 <primary>fdw_handler</primary>
4506 <indexterm zone="datatype-pseudo">
4507 <primary>cstring</primary>
4510 <indexterm zone="datatype-pseudo">
4511 <primary>internal</primary>
4514 <indexterm zone="datatype-pseudo">
4515 <primary>opaque</primary>
4519 The <productname>PostgreSQL</productname> type system contains a
4520 number of special-purpose entries that are collectively called
4521 <firstterm>pseudo-types</>. A pseudo-type cannot be used as a
4522 column data type, but it can be used to declare a function's
4523 argument or result type. Each of the available pseudo-types is
4524 useful in situations where a function's behavior does not
4525 correspond to simply taking or returning a value of a specific
4526 <acronym>SQL</acronym> data type. <xref
4527 linkend="datatype-pseudotypes-table"> lists the existing
4531 <table id="datatype-pseudotypes-table">
4532 <title>Pseudo-Types</title>
4537 <entry>Description</entry>
4543 <entry><type>any</></entry>
4544 <entry>Indicates that a function accepts any input data type.</entry>
4548 <entry><type>anyelement</></entry>
4549 <entry>Indicates that a function accepts any data type
4550 (see <xref linkend="extend-types-polymorphic">).</entry>
4554 <entry><type>anyarray</></entry>
4555 <entry>Indicates that a function accepts any array data type
4556 (see <xref linkend="extend-types-polymorphic">).</entry>
4560 <entry><type>anynonarray</></entry>
4561 <entry>Indicates that a function accepts any non-array data type
4562 (see <xref linkend="extend-types-polymorphic">).</entry>
4566 <entry><type>anyenum</></entry>
4567 <entry>Indicates that a function accepts any enum data type
4568 (see <xref linkend="extend-types-polymorphic"> and
4569 <xref linkend="datatype-enum">).</entry>
4573 <entry><type>anyrange</></entry>
4574 <entry>Indicates that a function accepts any range data type
4575 (see <xref linkend="extend-types-polymorphic"> and
4576 <xref linkend="rangetypes">).</entry>
4580 <entry><type>cstring</></entry>
4581 <entry>Indicates that a function accepts or returns a null-terminated C string.</entry>
4585 <entry><type>internal</></entry>
4586 <entry>Indicates that a function accepts or returns a server-internal
4591 <entry><type>language_handler</></entry>
4592 <entry>A procedural language call handler is declared to return <type>language_handler</>.</entry>
4596 <entry><type>fdw_handler</></entry>
4597 <entry>A foreign-data wrapper handler is declared to return <type>fdw_handler</>.</entry>
4601 <entry><type>record</></entry>
4602 <entry>Identifies a function returning an unspecified row type.</entry>
4606 <entry><type>trigger</></entry>
4607 <entry>A trigger function is declared to return <type>trigger.</></entry>
4611 <entry><type>void</></entry>
4612 <entry>Indicates that a function returns no value.</entry>
4616 <entry><type>opaque</></entry>
4617 <entry>An obsolete type name that formerly served all the above purposes.</entry>
4624 Functions coded in C (whether built-in or dynamically loaded) can be
4625 declared to accept or return any of these pseudo data types. It is up to
4626 the function author to ensure that the function will behave safely
4627 when a pseudo-type is used as an argument type.
4631 Functions coded in procedural languages can use pseudo-types only as
4632 allowed by their implementation languages. At present the procedural
4633 languages all forbid use of a pseudo-type as argument type, and allow
4634 only <type>void</> and <type>record</> as a result type (plus
4635 <type>trigger</> when the function is used as a trigger). Some also
4636 support polymorphic functions using the types <type>anyelement</>,
4637 <type>anyarray</>, <type>anynonarray</>, <type>anyenum</>, and
4642 The <type>internal</> pseudo-type is used to declare functions
4643 that are meant only to be called internally by the database
4644 system, and not by direct invocation in an <acronym>SQL</acronym>
4645 query. If a function has at least one <type>internal</>-type
4646 argument then it cannot be called from <acronym>SQL</acronym>. To
4647 preserve the type safety of this restriction it is important to
4648 follow this coding rule: do not create any function that is
4649 declared to return <type>internal</> unless it has at least one
4650 <type>internal</> argument.