3
<chapter id="datatype">
4
<title id="datatype-title">Data Types</title>
6
<indexterm zone="datatype">
7
<primary>data type</primary>
11
<primary>type</primary>
16
<productname>PostgreSQL</productname> has a rich set of native data
17
types available to users. Users can add new types to
18
<productname>PostgreSQL</productname> using the <xref
19
linkend="sql-createtype" endterm="sql-createtype-title"> command.
23
<xref linkend="datatype-table"> shows all the built-in general-purpose data
24
types. Most of the alternative names listed in the
25
<quote>Aliases</quote> column are the names used internally by
26
<productname>PostgreSQL</productname> for historical reasons. In
27
addition, some internally used or deprecated types are available,
28
but they are not listed here.
31
<table id="datatype-table">
32
<title>Data Types</title>
37
<entry>Aliases</entry>
38
<entry>Description</entry>
44
<entry><type>bigint</type></entry>
45
<entry><type>int8</type></entry>
46
<entry>signed eight-byte integer</entry>
50
<entry><type>bigserial</type></entry>
51
<entry><type>serial8</type></entry>
52
<entry>autoincrementing eight-byte integer</entry>
56
<entry><type>bit [ (<replaceable>n</replaceable>) ]</type></entry>
58
<entry>fixed-length bit string</entry>
62
<entry><type>bit varying [ (<replaceable>n</replaceable>) ]</type></entry>
63
<entry><type>varbit</type></entry>
64
<entry>variable-length bit string</entry>
68
<entry><type>boolean</type></entry>
69
<entry><type>bool</type></entry>
70
<entry>logical Boolean (true/false)</entry>
74
<entry><type>box</type></entry>
76
<entry>rectangular box in the plane</entry>
80
<entry><type>bytea</type></entry>
82
<entry>binary data (<quote>byte array</>)</entry>
86
<entry><type>character varying [ (<replaceable>n</replaceable>) ]</type></entry>
87
<entry><type>varchar [ (<replaceable>n</replaceable>) ]</type></entry>
88
<entry>variable-length character string</entry>
92
<entry><type>character [ (<replaceable>n</replaceable>) ]</type></entry>
93
<entry><type>char [ (<replaceable>n</replaceable>) ]</type></entry>
94
<entry>fixed-length character string</entry>
98
<entry><type>cidr</type></entry>
100
<entry>IPv4 or IPv6 network address</entry>
104
<entry><type>circle</type></entry>
106
<entry>circle in the plane</entry>
110
<entry><type>date</type></entry>
112
<entry>calendar date (year, month, day)</entry>
116
<entry><type>double precision</type></entry>
117
<entry><type>float8</type></entry>
118
<entry>double precision floating-point number</entry>
122
<entry><type>inet</type></entry>
124
<entry>IPv4 or IPv6 host address</entry>
128
<entry><type>integer</type></entry>
129
<entry><type>int</type>, <type>int4</type></entry>
130
<entry>signed four-byte integer</entry>
134
<entry><type>interval [ <replaceable>fields</replaceable> ] [ (<replaceable>p</replaceable>) ]</type></entry>
136
<entry>time span</entry>
140
<entry><type>line</type></entry>
142
<entry>infinite line in the plane</entry>
146
<entry><type>lseg</type></entry>
148
<entry>line segment in the plane</entry>
152
<entry><type>macaddr</type></entry>
154
<entry>MAC address</entry>
158
<entry><type>money</type></entry>
160
<entry>currency amount</entry>
164
<entry><type>numeric [ (<replaceable>p</replaceable>,
165
<replaceable>s</replaceable>) ]</type></entry>
166
<entry><type>decimal [ (<replaceable>p</replaceable>,
167
<replaceable>s</replaceable>) ]</type></entry>
168
<entry>exact numeric of selectable precision</entry>
172
<entry><type>path</type></entry>
174
<entry>geometric path in the plane</entry>
178
<entry><type>point</type></entry>
180
<entry>geometric point in the plane</entry>
184
<entry><type>polygon</type></entry>
186
<entry>closed geometric path in the plane</entry>
190
<entry><type>real</type></entry>
191
<entry><type>float4</type></entry>
192
<entry>single precision floating-point number</entry>
196
<entry><type>smallint</type></entry>
197
<entry><type>int2</type></entry>
198
<entry>signed two-byte integer</entry>
202
<entry><type>serial</type></entry>
203
<entry><type>serial4</type></entry>
204
<entry>autoincrementing four-byte integer</entry>
208
<entry><type>text</type></entry>
210
<entry>variable-length character string</entry>
214
<entry><type>time [ (<replaceable>p</replaceable>) ] [ without time zone ]</type></entry>
216
<entry>time of day</entry>
220
<entry><type>time [ (<replaceable>p</replaceable>) ] with time zone</type></entry>
221
<entry><type>timetz</type></entry>
222
<entry>time of day, including time zone</entry>
226
<entry><type>timestamp [ (<replaceable>p</replaceable>) ] [ without time zone ]</type></entry>
228
<entry>date and time</entry>
232
<entry><type>timestamp [ (<replaceable>p</replaceable>) ] with time zone</type></entry>
233
<entry><type>timestamptz</type></entry>
234
<entry>date and time, including time zone</entry>
238
<entry><type>tsquery</type></entry>
240
<entry>text search query</entry>
244
<entry><type>tsvector</type></entry>
246
<entry>text search document</entry>
250
<entry><type>txid_snapshot</type></entry>
252
<entry>user-level transaction ID snapshot</entry>
256
<entry><type>uuid</type></entry>
258
<entry>universally unique identifier</entry>
262
<entry><type>xml</type></entry>
264
<entry>XML data</entry>
271
<title>Compatibility</title>
273
The following types (or spellings thereof) are specified by
274
<acronym>SQL</acronym>: <type>bigint</type>, <type>bit</type>, <type>bit
275
varying</type>, <type>boolean</type>, <type>char</type>,
276
<type>character varying</type>, <type>character</type>,
277
<type>varchar</type>, <type>date</type>, <type>double
278
precision</type>, <type>integer</type>, <type>interval</type>,
279
<type>numeric</type>, <type>decimal</type>, <type>real</type>,
280
<type>smallint</type>, <type>time</type> (with or without time zone),
281
<type>timestamp</type> (with or without time zone),
287
Each data type has an external representation determined by its input
288
and output functions. Many of the built-in types have
289
obvious external formats. However, several types are either unique
290
to <productname>PostgreSQL</productname>, such as geometric
291
paths, or have several possibilities for formats, such as the date
293
Some of the input and output functions are not invertible. That is,
294
the result of an output function might lose accuracy when compared to
298
<sect1 id="datatype-numeric">
299
<title>Numeric Types</title>
301
<indexterm zone="datatype-numeric">
302
<primary>data type</primary>
303
<secondary>numeric</secondary>
307
Numeric types consist of two-, four-, and eight-byte integers,
308
four- and eight-byte floating-point numbers, and selectable-precision
309
decimals. <xref linkend="datatype-numeric-table"> lists the
313
<table id="datatype-numeric-table">
314
<title>Numeric Types</title>
319
<entry>Storage Size</entry>
320
<entry>Description</entry>
327
<entry><type>smallint</></entry>
328
<entry>2 bytes</entry>
329
<entry>small-range integer</entry>
330
<entry>-32768 to +32767</entry>
333
<entry><type>integer</></entry>
334
<entry>4 bytes</entry>
335
<entry>usual choice for integer</entry>
336
<entry>-2147483648 to +2147483647</entry>
339
<entry><type>bigint</></entry>
340
<entry>8 bytes</entry>
341
<entry>large-range integer</entry>
342
<entry>-9223372036854775808 to 9223372036854775807</entry>
346
<entry><type>decimal</></entry>
347
<entry>variable</entry>
348
<entry>user-specified precision, exact</entry>
349
<entry>no limit</entry>
352
<entry><type>numeric</></entry>
353
<entry>variable</entry>
354
<entry>user-specified precision, exact</entry>
355
<entry>no limit</entry>
359
<entry><type>real</></entry>
360
<entry>4 bytes</entry>
361
<entry>variable-precision, inexact</entry>
362
<entry>6 decimal digits precision</entry>
365
<entry><type>double precision</></entry>
366
<entry>8 bytes</entry>
367
<entry>variable-precision, inexact</entry>
368
<entry>15 decimal digits precision</entry>
372
<entry><type>serial</></entry>
373
<entry>4 bytes</entry>
374
<entry>autoincrementing integer</entry>
375
<entry>1 to 2147483647</entry>
379
<entry><type>bigserial</type></entry>
380
<entry>8 bytes</entry>
381
<entry>large autoincrementing integer</entry>
382
<entry>1 to 9223372036854775807</entry>
389
The syntax of constants for the numeric types is described in
390
<xref linkend="sql-syntax-constants">. The numeric types have a
391
full set of corresponding arithmetic operators and
392
functions. Refer to <xref linkend="functions"> for more
393
information. The following sections describe the types in detail.
396
<sect2 id="datatype-int">
397
<title>Integer Types</title>
399
<indexterm zone="datatype-int">
400
<primary>integer</primary>
403
<indexterm zone="datatype-int">
404
<primary>smallint</primary>
407
<indexterm zone="datatype-int">
408
<primary>bigint</primary>
412
<primary>int4</primary>
417
<primary>int2</primary>
422
<primary>int8</primary>
427
The types <type>smallint</type>, <type>integer</type>, and
428
<type>bigint</type> store whole numbers, that is, numbers without
429
fractional components, of various ranges. Attempts to store
430
values outside of the allowed range will result in an error.
434
The type <type>integer</type> is the usual choice, as it offers
435
the best balance between range, storage size, and performance.
436
The <type>smallint</type> type is generally only used if disk
437
space is at a premium. The <type>bigint</type> type should only
438
be used if the <type>integer</type> range is not sufficient,
439
because the latter is definitely faster.
443
The <type>bigint</type> type might not function correctly on all
444
platforms, since it relies on compiler support for eight-byte
445
integers. On a machine without such support, <type>bigint</type>
446
acts the same as <type>integer</type> (but still takes up eight
447
bytes of storage). However, we are not aware of any reasonable
448
platform where this is actually the case.
452
<acronym>SQL</acronym> only specifies the integer types
453
<type>integer</type> (or <type>int</type>),
454
<type>smallint</type>, and <type>bigint</type>. The
455
type names <type>int2</type>, <type>int4</type>, and
456
<type>int8</type> are extensions, which are shared with various
457
other <acronym>SQL</acronym> database systems.
462
<sect2 id="datatype-numeric-decimal">
463
<title>Arbitrary Precision Numbers</title>
466
<primary>numeric (data type)</primary>
470
<primary>arbitrary precision numbers</primary>
474
<primary>decimal</primary>
479
The type <type>numeric</type> can store numbers with up to 1000
480
digits of precision and perform calculations exactly. It is
481
especially recommended for storing monetary amounts and other
482
quantities where exactness is required. However, arithmetic on
483
<type>numeric</type> values is very slow compared to the integer
484
types, or to the floating-point types described in the next section.
488
In what follows we use these terms: The
489
<firstterm>scale</firstterm> of a <type>numeric</type> is the
490
count of decimal digits in the fractional part, to the right of
491
the decimal point. The <firstterm>precision</firstterm> of a
492
<type>numeric</type> is the total count of significant digits in
493
the whole number, that is, the number of digits to both sides of
494
the decimal point. So the number 23.5141 has a precision of 6
495
and a scale of 4. Integers can be considered to have a scale of
500
Both the maximum precision and the maximum scale of a
501
<type>numeric</type> column can be
502
configured. To declare a column of type <type>numeric</type> use
505
NUMERIC(<replaceable>precision</replaceable>, <replaceable>scale</replaceable>)
507
The precision must be positive, the scale zero or positive.
510
NUMERIC(<replaceable>precision</replaceable>)
512
selects a scale of 0. Specifying:
516
without any precision or scale creates a column in which numeric
517
values of any precision and scale can be stored, up to the
518
implementation limit on precision. A column of this kind will
519
not coerce input values to any particular scale, whereas
520
<type>numeric</type> columns with a declared scale will coerce
521
input values to that scale. (The <acronym>SQL</acronym> standard
522
requires a default scale of 0, i.e., coercion to integer
523
precision. We find this a bit useless. If you're concerned
524
about portability, always specify the precision and scale
529
If the scale of a value to be stored is greater than the declared
530
scale of the column, the system will round the value to the specified
531
number of fractional digits. Then, if the number of digits to the
532
left of the decimal point exceeds the declared precision minus the
533
declared scale, an error is raised.
537
Numeric values are physically stored without any extra leading or
538
trailing zeroes. Thus, the declared precision and scale of a column
539
are maximums, not fixed allocations. (In this sense the <type>numeric</>
540
type is more akin to <type>varchar(<replaceable>n</>)</type>
541
than to <type>char(<replaceable>n</>)</type>.) The actual storage
542
requirement is two bytes for each group of four decimal digits,
543
plus five to eight bytes overhead.
547
<primary>NaN</primary>
548
<see>not a number</see>
552
<primary>not a number</primary>
553
<secondary>numeric (data type)</secondary>
557
In addition to ordinary numeric values, the <type>numeric</type>
558
type allows the special value <literal>NaN</>, meaning
559
<quote>not-a-number</quote>. Any operation on <literal>NaN</>
560
yields another <literal>NaN</>. When writing this value
561
as a constant in a SQL command, you must put quotes around it,
562
for example <literal>UPDATE table SET x = 'NaN'</>. On input,
563
the string <literal>NaN</> is recognized in a case-insensitive manner.
568
In most implementations of the <quote>not-a-number</> concept,
569
<literal>NaN</> is not considered equal to any other numeric
570
value (including <literal>NaN</>). In order to allow
571
<type>numeric</> values to be sorted and used in tree-based
572
indexes, <productname>PostgreSQL</> treats <literal>NaN</>
573
values as equal, and greater than all non-<literal>NaN</>
579
The types <type>decimal</type> and <type>numeric</type> are
580
equivalent. Both types are part of the <acronym>SQL</acronym>
586
<sect2 id="datatype-float">
587
<title>Floating-Point Types</title>
589
<indexterm zone="datatype-float">
590
<primary>real</primary>
593
<indexterm zone="datatype-float">
594
<primary>double precision</primary>
598
<primary>float4</primary>
603
<primary>float8</primary>
604
<see>double precision</see>
607
<indexterm zone="datatype-float">
608
<primary>floating point</primary>
612
The data types <type>real</type> and <type>double
613
precision</type> are inexact, variable-precision numeric types.
614
In practice, these types are usually implementations of
615
<acronym>IEEE</acronym> Standard 754 for Binary Floating-Point
616
Arithmetic (single and double precision, respectively), to the
617
extent that the underlying processor, operating system, and
622
Inexact means that some values cannot be converted exactly to the
623
internal format and are stored as approximations, so that storing
624
and printing back out a value might show slight discrepancies.
625
Managing these errors and how they propagate through calculations
626
is the subject of an entire branch of mathematics and computer
627
science and will not be discussed further here, except for the
632
If you require exact storage and calculations (such as for
633
monetary amounts), use the <type>numeric</type> type instead.
639
If you want to do complicated calculations with these types
640
for anything important, especially if you rely on certain
641
behavior in boundary cases (infinity, underflow), you should
642
evaluate the implementation carefully.
648
Comparing two floating-point values for equality might or might
649
not work as expected.
656
On most platforms, the <type>real</type> type has a range of at least
657
1E-37 to 1E+37 with a precision of at least 6 decimal digits. The
658
<type>double precision</type> type typically has a range of around
659
1E-307 to 1E+308 with a precision of at least 15 digits. Values that
660
are too large or too small will cause an error. Rounding might
661
take place if the precision of an input number is too high.
662
Numbers too close to zero that are not representable as distinct
663
from zero will cause an underflow error.
667
<primary>not a number</primary>
668
<secondary>double precision</secondary>
672
In addition to ordinary numeric values, the floating-point types
673
have several special values:
675
<literal>Infinity</literal>
676
<literal>-Infinity</literal>
677
<literal>NaN</literal>
679
These represent the IEEE 754 special values
680
<quote>infinity</quote>, <quote>negative infinity</quote>, and
681
<quote>not-a-number</quote>, respectively. (On a machine whose
682
floating-point arithmetic does not follow IEEE 754, these values
683
will probably not work as expected.) When writing these values
684
as constants in a SQL command, you must put quotes around them,
685
for example <literal>UPDATE table SET x = 'Infinity'</>. On input,
686
these strings are recognized in a case-insensitive manner.
691
IEEE754 specifies that <literal>NaN</> should not compare equal
692
to any other floating-point value (including <literal>NaN</>).
693
In order to allow floating-point values to be sorted and used
694
in tree-based indexes, <productname>PostgreSQL</> treats
695
<literal>NaN</> values as equal, and greater than all
696
non-<literal>NaN</> values.
701
<productname>PostgreSQL</productname> also supports the SQL-standard
702
notations <type>float</type> and
703
<type>float(<replaceable>p</replaceable>)</type> for specifying
704
inexact numeric types. Here, <replaceable>p</replaceable> specifies
705
the minimum acceptable precision in binary digits.
706
<productname>PostgreSQL</productname> accepts
707
<type>float(1)</type> to <type>float(24)</type> as selecting the
708
<type>real</type> type, while
709
<type>float(25)</type> to <type>float(53)</type> select
710
<type>double precision</type>. Values of <replaceable>p</replaceable>
711
outside the allowed range draw an error.
712
<type>float</type> with no precision specified is taken to mean
713
<type>double precision</type>.
718
Prior to <productname>PostgreSQL</productname> 7.4, the precision in
719
<type>float(<replaceable>p</replaceable>)</type> was taken to mean
720
so many decimal digits. This has been corrected to match the SQL
721
standard, which specifies that the precision is measured in binary
722
digits. The assumption that <type>real</type> and
723
<type>double precision</type> have exactly 24 and 53 bits in the
724
mantissa respectively is correct for IEEE-standard floating point
725
implementations. On non-IEEE platforms it might be off a little, but
726
for simplicity the same ranges of <replaceable>p</replaceable> are used
733
<sect2 id="datatype-serial">
734
<title>Serial Types</title>
736
<indexterm zone="datatype-serial">
737
<primary>serial</primary>
740
<indexterm zone="datatype-serial">
741
<primary>bigserial</primary>
744
<indexterm zone="datatype-serial">
745
<primary>serial4</primary>
748
<indexterm zone="datatype-serial">
749
<primary>serial8</primary>
753
<primary>auto-increment</primary>
758
<primary>sequence</primary>
759
<secondary>and serial type</secondary>
763
The data types <type>serial</type> and <type>bigserial</type>
764
are not true types, but merely
765
a notational convenience for setting up unique identifier columns
766
(similar to the <literal>AUTO_INCREMENT</literal> property
767
supported by some other databases). In the current
768
implementation, specifying:
771
CREATE TABLE <replaceable class="parameter">tablename</replaceable> (
772
<replaceable class="parameter">colname</replaceable> SERIAL
776
is equivalent to specifying:
779
CREATE SEQUENCE <replaceable class="parameter">tablename</replaceable>_<replaceable class="parameter">colname</replaceable>_seq;
780
CREATE TABLE <replaceable class="parameter">tablename</replaceable> (
781
<replaceable class="parameter">colname</replaceable> integer NOT NULL DEFAULT nextval('<replaceable class="parameter">tablename</replaceable>_<replaceable class="parameter">colname</replaceable>_seq')
783
ALTER SEQUENCE <replaceable class="parameter">tablename</replaceable>_<replaceable class="parameter">colname</replaceable>_seq OWNED BY <replaceable class="parameter">tablename</replaceable>.<replaceable class="parameter">colname</replaceable>;
786
Thus, we have created an integer column and arranged for its default
787
values to be assigned from a sequence generator. A <literal>NOT NULL</>
788
constraint is applied to ensure that a null value cannot be explicitly
789
inserted, either. (In most cases you would also want to attach a
790
<literal>UNIQUE</> or <literal>PRIMARY KEY</> constraint to prevent
791
duplicate values from being inserted by accident, but this is
792
not automatic.) Lastly, the sequence is marked as <quote>owned by</>
793
the column, so that it will be dropped if the column or table is dropped.
798
Prior to <productname>PostgreSQL</productname> 7.3, <type>serial</type>
799
implied <literal>UNIQUE</literal>. This is no longer automatic. If
800
you wish a serial column to be in a unique constraint or a
801
primary key, it must now be specified, same as with
807
To insert the next value of the sequence into the <type>serial</type>
808
column, specify that the <type>serial</type>
809
column should be assigned its default value. This can be done
810
either by excluding the column from the list of columns in
811
the <command>INSERT</command> statement, or through the use of
812
the <literal>DEFAULT</literal> key word.
816
The type names <type>serial</type> and <type>serial4</type> are
817
equivalent: both create <type>integer</type> columns. The type
818
names <type>bigserial</type> and <type>serial8</type> work just
819
the same way, except that they create a <type>bigint</type>
820
column. <type>bigserial</type> should be used if you anticipate
821
the use of more than 2<superscript>31</> identifiers over the
822
lifetime of the table.
826
The sequence created for a <type>serial</type> column is
827
automatically dropped when the owning column is dropped.
828
You can drop the sequence without dropping the column, but this
829
will force removal of the column default expression.
834
<sect1 id="datatype-money">
835
<title>Monetary Types</title>
838
The <type>money</type> type stores a currency amount with a fixed
839
fractional precision; see <xref
840
linkend="datatype-money-table">.
841
Input is accepted in a variety of formats, including integer and
842
floating-point literals, as well as <quote>typical</quote>
843
currency formatting, such as <literal>'$1,000.00'</literal>.
844
Output is generally in the latter form but depends on the locale.
845
Non-quoted numeric values can be converted to <type>money</type> by
846
casting the numeric value to <type>text</type> and then
849
SELECT 1234::text::money;
851
There is no simple way of doing the reverse in a locale-independent
852
manner, namely casting a <type>money</type> value to a numeric type.
853
If you know the currency symbol and thousands separator you can use
854
<function>regexp_replace()</>:
856
SELECT regexp_replace('52093.89'::money::text, '[$,]', '', 'g')::numeric;
862
Since the output of this data type is locale-sensitive, it may not
863
work to load <type>money</> data into a database that has a different
864
setting of <varname>lc_monetary</>. To avoid problems, before
865
restoring a dump make sure <varname>lc_monetary</> has the same or
866
equivalent value as in the database that was dumped.
869
<table id="datatype-money-table">
870
<title>Monetary Types</title>
875
<entry>Storage Size</entry>
876
<entry>Description</entry>
883
<entry>8 bytes</entry>
884
<entry>currency amount</entry>
885
<entry>-92233720368547758.08 to +92233720368547758.07</entry>
893
<sect1 id="datatype-character">
894
<title>Character Types</title>
896
<indexterm zone="datatype-character">
897
<primary>character string</primary>
898
<secondary>data types</secondary>
902
<primary>string</primary>
903
<see>character string</see>
906
<indexterm zone="datatype-character">
907
<primary>character</primary>
910
<indexterm zone="datatype-character">
911
<primary>character varying</primary>
914
<indexterm zone="datatype-character">
915
<primary>text</primary>
918
<indexterm zone="datatype-character">
919
<primary>char</primary>
922
<indexterm zone="datatype-character">
923
<primary>varchar</primary>
926
<table id="datatype-character-table">
927
<title>Character Types</title>
932
<entry>Description</entry>
937
<entry><type>character varying(<replaceable>n</>)</type>, <type>varchar(<replaceable>n</>)</type></entry>
938
<entry>variable-length with limit</entry>
941
<entry><type>character(<replaceable>n</>)</type>, <type>char(<replaceable>n</>)</type></entry>
942
<entry>fixed-length, blank padded</entry>
945
<entry><type>text</type></entry>
946
<entry>variable unlimited length</entry>
953
<xref linkend="datatype-character-table"> shows the
954
general-purpose character types available in
955
<productname>PostgreSQL</productname>.
959
<acronym>SQL</acronym> defines two primary character types:
960
<type>character varying(<replaceable>n</>)</type> and
961
<type>character(<replaceable>n</>)</type>, where <replaceable>n</>
962
is a positive integer. Both of these types can store strings up to
963
<replaceable>n</> characters in length. An attempt to store a
964
longer string into a column of these types will result in an
965
error, unless the excess characters are all spaces, in which case
966
the string will be truncated to the maximum length. (This somewhat
967
bizarre exception is required by the <acronym>SQL</acronym>
968
standard.) If the string to be stored is shorter than the declared
969
length, values of type <type>character</type> will be space-padded;
970
values of type <type>character varying</type> will simply store the
976
If one explicitly casts a value to <type>character
977
varying(<replaceable>n</>)</type> or
978
<type>character(<replaceable>n</>)</type>, then an over-length
979
value will be truncated to <replaceable>n</> characters without
980
raising an error. (This too is required by the
981
<acronym>SQL</acronym> standard.)
985
The notations <type>varchar(<replaceable>n</>)</type> and
986
<type>char(<replaceable>n</>)</type> are aliases for <type>character
987
varying(<replaceable>n</>)</type> and
988
<type>character(<replaceable>n</>)</type>, respectively.
989
<type>character</type> without length specifier is equivalent to
990
<type>character(1)</type>. If <type>character varying</type> is used
991
without length specifier, the type accepts strings of any size. The
992
latter is a <productname>PostgreSQL</> extension.
996
In addition, <productname>PostgreSQL</productname> provides the
997
<type>text</type> type, which stores strings of any length.
998
Although the type <type>text</type> is not in the
999
<acronym>SQL</acronym> standard, several other SQL database
1000
management systems have it as well.
1004
Values of type <type>character</type> are physically padded
1005
with spaces to the specified width <replaceable>n</>, and are
1006
stored and displayed that way. However, the padding spaces are
1007
treated as semantically insignificant. Trailing spaces are
1008
disregarded when comparing two values of type <type>character</type>,
1009
and they will be removed when converting a <type>character</type> value
1010
to one of the other string types. Note that trailing spaces
1011
<emphasis>are</> semantically significant in
1012
<type>character varying</type> and <type>text</type> values.
1016
The storage requirement for a short string (up to 126 bytes) is 1 byte
1017
plus the actual string, which includes the space padding in the case of
1018
<type>character</type>. Longer strings have 4 bytes overhead instead
1019
of 1. Long strings are compressed by the system automatically, so
1020
the physical requirement on disk might be less. Very long values are also
1021
stored in background tables so that they do not interfere with rapid
1022
access to shorter column values. In any case, the longest
1023
possible character string that can be stored is about 1 GB. (The
1024
maximum value that will be allowed for <replaceable>n</> in the data
1025
type declaration is less than that. It wouldn't be very useful to
1026
change this because with multibyte character encodings the number of
1027
characters and bytes can be quite different anyway. If you desire to
1028
store long strings with no specific upper limit, use
1029
<type>text</type> or <type>character varying</type> without a length
1030
specifier, rather than making up an arbitrary length limit.)
1035
There are no performance differences between these three types,
1036
apart from increased storage size when using the blank-padded
1037
type, and a few extra cycles to check the length when storing into
1038
a length-constrained column. While
1039
<type>character(<replaceable>n</>)</type> has performance
1040
advantages in some other database systems, it has no such advantages in
1041
<productname>PostgreSQL</productname>. In most situations
1042
<type>text</type> or <type>character varying</type> should be used
1048
Refer to <xref linkend="sql-syntax-strings"> for information about
1049
the syntax of string literals, and to <xref linkend="functions">
1050
for information about available operators and functions. The
1051
database character set determines the character set used to store
1052
textual values; for more information on character set support,
1053
refer to <xref linkend="multibyte">.
1057
<title>Using the character types</title>
1060
CREATE TABLE test1 (a character(4));
1061
INSERT INTO test1 VALUES ('ok');
1062
SELECT a, char_length(a) FROM test1; -- <co id="co.datatype-char">
1065
------+-------------
1069
CREATE TABLE test2 (b varchar(5));
1070
INSERT INTO test2 VALUES ('ok');
1071
INSERT INTO test2 VALUES ('good ');
1072
INSERT INTO test2 VALUES ('too long');
1073
<computeroutput>ERROR: value too long for type character varying(5)</computeroutput>
1074
INSERT INTO test2 VALUES ('too long'::varchar(5)); -- explicit truncation
1075
SELECT b, char_length(b) FROM test2;
1078
-------+-------------
1085
<callout arearefs="co.datatype-char">
1087
The <function>char_length</function> function is discussed in
1088
<xref linkend="functions-string">.
1095
There are two other fixed-length character types in
1096
<productname>PostgreSQL</productname>, shown in <xref
1097
linkend="datatype-character-special-table">. The <type>name</type>
1098
type exists <emphasis>only</emphasis> for storage of identifiers
1099
in the internal system catalogs and is not intended for use by the general user. Its
1100
length is currently defined as 64 bytes (63 usable characters plus
1101
terminator) but should be referenced using the constant
1102
<symbol>NAMEDATALEN</symbol>. The length is set at compile time (and
1103
is therefore adjustable for special uses); the default maximum
1104
length might change in a future release. The type <type>"char"</type>
1105
(note the quotes) is different from <type>char(1)</type> in that it
1106
only uses one byte of storage. It is internally used in the system
1107
catalogs as a poor-man's enumeration type.
1110
<table id="datatype-character-special-table">
1111
<title>Special Character Types</title>
1116
<entry>Storage Size</entry>
1117
<entry>Description</entry>
1122
<entry><type>"char"</type></entry>
1123
<entry>1 byte</entry>
1124
<entry>single-byte internal type</entry>
1127
<entry><type>name</type></entry>
1128
<entry>64 bytes</entry>
1129
<entry>internal type for object names</entry>
1137
<sect1 id="datatype-binary">
1138
<title>Binary Data Types</title>
1140
<indexterm zone="datatype-binary">
1141
<primary>binary data</primary>
1144
<indexterm zone="datatype-binary">
1145
<primary>bytea</primary>
1149
The <type>bytea</type> data type allows storage of binary strings;
1150
see <xref linkend="datatype-binary-table">.
1153
<table id="datatype-binary-table">
1154
<title>Binary Data Types</title>
1159
<entry>Storage Size</entry>
1160
<entry>Description</entry>
1165
<entry><type>bytea</type></entry>
1166
<entry>1 or 4 bytes plus the actual binary string</entry>
1167
<entry>variable-length binary string</entry>
1174
A binary string is a sequence of octets (or bytes). Binary
1175
strings are distinguished from character strings by two
1176
characteristics: First, binary strings specifically allow storing
1177
octets of value zero and other <quote>non-printable</quote>
1178
octets (usually, octets outside the range 32 to 126).
1179
Character strings disallow zero octets, and also disallow any
1180
other octet values and sequences of octet values that are invalid
1181
according to the database's selected character set encoding.
1182
Second, operations on binary strings process the actual bytes,
1183
whereas the processing of character strings depends on locale settings.
1184
In short, binary strings are appropriate for storing data that the
1185
programmer thinks of as <quote>raw bytes</>, whereas character
1186
strings are appropriate for storing text.
1190
When entering <type>bytea</type> values, octets of certain
1191
values <emphasis>must</emphasis> be escaped (but all octet
1192
values <emphasis>can</emphasis> be escaped) when used as part
1193
of a string literal in an <acronym>SQL</acronym> statement. In
1194
general, to escape an octet, it is converted into the three-digit
1195
octal number equivalent of its decimal octet value, and preceded
1196
by two backslashes. <xref linkend="datatype-binary-sqlesc">
1197
shows the characters that must be escaped, and gives the alternative
1198
escape sequences where applicable.
1201
<table id="datatype-binary-sqlesc">
1202
<title><type>bytea</> Literal Escaped Octets</title>
1206
<entry>Decimal Octet Value</entry>
1207
<entry>Description</entry>
1208
<entry>Escaped Input Representation</entry>
1209
<entry>Example</entry>
1210
<entry>Output Representation</entry>
1217
<entry>zero octet</entry>
1218
<entry><literal>E'\\000'</literal></entry>
1219
<entry><literal>SELECT E'\\000'::bytea;</literal></entry>
1220
<entry><literal>\000</literal></entry>
1225
<entry>single quote</entry>
1226
<entry><literal>''''</literal> or <literal>E'\\047'</literal></entry>
1227
<entry><literal>SELECT E'\''::bytea;</literal></entry>
1228
<entry><literal>'</literal></entry>
1233
<entry>backslash</entry>
1234
<entry><literal>E'\\\\'</literal> or <literal>E'\\134'</literal></entry>
1235
<entry><literal>SELECT E'\\\\'::bytea;</literal></entry>
1236
<entry><literal>\\</literal></entry>
1240
<entry>0 to 31 and 127 to 255</entry>
1241
<entry><quote>non-printable</quote> octets</entry>
1242
<entry><literal>E'\\<replaceable>xxx'</></literal> (octal value)</entry>
1243
<entry><literal>SELECT E'\\001'::bytea;</literal></entry>
1244
<entry><literal>\001</literal></entry>
1252
The requirement to escape <quote>non-printable</quote> octets actually
1253
varies depending on locale settings. In some instances you can get away
1254
with leaving them unescaped. Note that the result in each of the examples
1255
in <xref linkend="datatype-binary-sqlesc"> was exactly one octet in
1256
length, even though the output representation of the zero octet and
1257
backslash are more than one character.
1261
The reason that you have to write so many backslashes, as shown
1262
in <xref linkend="datatype-binary-sqlesc">, is that an input
1263
string written as a string literal must pass through two parse
1264
phases in the <productname>PostgreSQL</productname> server.
1265
The first backslash of each pair is interpreted as an escape
1266
character by the string-literal parser (assuming escape string
1267
syntax is used) and is therefore consumed, leaving the second backslash of the
1268
pair. (Dollar-quoted strings can be used to avoid this level
1269
of escaping.) The remaining backslash is then recognized by the
1270
<type>bytea</type> input function as starting either a three
1271
digit octal value or escaping another backslash. For example,
1272
a string literal passed to the server as <literal>E'\\001'</literal>
1273
becomes <literal>\001</literal> after passing through the
1274
escape string parser. The <literal>\001</literal> is then sent
1275
to the <type>bytea</type> input function, where it is converted
1276
to a single octet with a decimal value of 1. Note that the
1277
single-quote character is not treated specially by <type>bytea</type>,
1278
so it follows the normal rules for string literals. (See also
1279
<xref linkend="sql-syntax-strings">.)
1283
<type>Bytea</type> octets are also escaped in the output. In general, each
1284
<quote>non-printable</quote> octet is converted into
1285
its equivalent three-digit octal value and preceded by one backslash.
1286
Most <quote>printable</quote> octets are represented by their standard
1287
representation in the client character set. The octet with decimal
1288
value 92 (backslash) has a special alternative output representation.
1289
Details are in <xref linkend="datatype-binary-resesc">.
1292
<table id="datatype-binary-resesc">
1293
<title><type>bytea</> Output Escaped Octets</title>
1297
<entry>Decimal Octet Value</entry>
1298
<entry>Description</entry>
1299
<entry>Escaped Output Representation</entry>
1300
<entry>Example</entry>
1301
<entry>Output Result</entry>
1309
<entry>backslash</entry>
1310
<entry><literal>\\</literal></entry>
1311
<entry><literal>SELECT E'\\134'::bytea;</literal></entry>
1312
<entry><literal>\\</literal></entry>
1316
<entry>0 to 31 and 127 to 255</entry>
1317
<entry><quote>non-printable</quote> octets</entry>
1318
<entry><literal>\<replaceable>xxx</></literal> (octal value)</entry>
1319
<entry><literal>SELECT E'\\001'::bytea;</literal></entry>
1320
<entry><literal>\001</literal></entry>
1324
<entry>32 to 126</entry>
1325
<entry><quote>printable</quote> octets</entry>
1326
<entry>client character set representation</entry>
1327
<entry><literal>SELECT E'\\176'::bytea;</literal></entry>
1328
<entry><literal>~</literal></entry>
1336
Depending on the front end to <productname>PostgreSQL</> you use,
1337
you might have additional work to do in terms of escaping and
1338
unescaping <type>bytea</type> strings. For example, you might also
1339
have to escape line feeds and carriage returns if your interface
1340
automatically translates these.
1344
The <acronym>SQL</acronym> standard defines a different binary
1345
string type, called <type>BLOB</type> or <type>BINARY LARGE
1346
OBJECT</type>. The input format is different from
1347
<type>bytea</type>, but the provided functions and operators are
1353
<sect1 id="datatype-datetime">
1354
<title>Date/Time Types</title>
1356
<indexterm zone="datatype-datetime">
1357
<primary>date</primary>
1359
<indexterm zone="datatype-datetime">
1360
<primary>time</primary>
1362
<indexterm zone="datatype-datetime">
1363
<primary>time without time zone</primary>
1365
<indexterm zone="datatype-datetime">
1366
<primary>time with time zone</primary>
1368
<indexterm zone="datatype-datetime">
1369
<primary>timestamp</primary>
1371
<indexterm zone="datatype-datetime">
1372
<primary>timestamp with time zone</primary>
1374
<indexterm zone="datatype-datetime">
1375
<primary>timestamp without time zone</primary>
1377
<indexterm zone="datatype-datetime">
1378
<primary>interval</primary>
1380
<indexterm zone="datatype-datetime">
1381
<primary>time span</primary>
1385
<productname>PostgreSQL</productname> supports the full set of
1386
<acronym>SQL</acronym> date and time types, shown in <xref
1387
linkend="datatype-datetime-table">. The operations available
1388
on these data types are described in
1389
<xref linkend="functions-datetime">.
1392
<table id="datatype-datetime-table">
1393
<title>Date/Time Types</title>
1398
<entry>Storage Size</entry>
1399
<entry>Description</entry>
1400
<entry>Low Value</entry>
1401
<entry>High Value</entry>
1402
<entry>Resolution</entry>
1407
<entry><type>timestamp [ (<replaceable>p</replaceable>) ] [ without time zone ]</type></entry>
1408
<entry>8 bytes</entry>
1409
<entry>both date and time</entry>
1410
<entry>4713 BC</entry>
1411
<entry>294276 AD</entry>
1412
<entry>1 microsecond / 14 digits</entry>
1415
<entry><type>timestamp [ (<replaceable>p</replaceable>) ] with time zone</type></entry>
1416
<entry>8 bytes</entry>
1417
<entry>both date and time, with time zone</entry>
1418
<entry>4713 BC</entry>
1419
<entry>294276 AD</entry>
1420
<entry>1 microsecond / 14 digits</entry>
1423
<entry><type>date</type></entry>
1424
<entry>4 bytes</entry>
1425
<entry>dates only</entry>
1426
<entry>4713 BC</entry>
1427
<entry>5874897 AD</entry>
1428
<entry>1 day</entry>
1431
<entry><type>time [ (<replaceable>p</replaceable>) ] [ without time zone ]</type></entry>
1432
<entry>8 bytes</entry>
1433
<entry>times of day only</entry>
1434
<entry>00:00:00</entry>
1435
<entry>24:00:00</entry>
1436
<entry>1 microsecond / 14 digits</entry>
1439
<entry><type>time [ (<replaceable>p</replaceable>) ] with time zone</type></entry>
1440
<entry>12 bytes</entry>
1441
<entry>times of day only, with time zone</entry>
1442
<entry>00:00:00+1459</entry>
1443
<entry>24:00:00-1459</entry>
1444
<entry>1 microsecond / 14 digits</entry>
1447
<entry><type>interval [ <replaceable>fields</replaceable> ] [ (<replaceable>p</replaceable>) ]</type></entry>
1448
<entry>12 bytes</entry>
1449
<entry>time intervals</entry>
1450
<entry>-178000000 years</entry>
1451
<entry>178000000 years</entry>
1452
<entry>1 microsecond / 14 digits</entry>
1460
Prior to <productname>PostgreSQL</productname> 7.3, writing just
1461
<type>timestamp</type> was equivalent to <type>timestamp with
1462
time zone</type>. This was changed for SQL compliance.
1467
<type>time</type>, <type>timestamp</type>, and
1468
<type>interval</type> accept an optional precision value
1469
<replaceable>p</replaceable> which specifies the number of
1470
fractional digits retained in the seconds field. By default, there
1471
is no explicit bound on precision. The allowed range of
1472
<replaceable>p</replaceable> is from 0 to 6 for the
1473
<type>timestamp</type> and <type>interval</type> types.
1478
When <type>timestamp</> values are stored as eight-byte integers
1479
(currently the default), microsecond precision is available over
1480
the full range of values. When <type>timestamp</> values are
1481
stored as double precision floating-point numbers instead (a
1482
deprecated compile-time option), the effective limit of precision
1483
might be less than 6. <type>timestamp</type> values are stored as
1484
seconds before or after midnight 2000-01-01. When
1485
<type>timestamp</type> values are implemented using floating-point
1486
numbers, microsecond precision is achieved for dates within a few
1487
years of 2000-01-01, but the precision degrades for dates further
1488
away. Note that using floating-point datetimes allows a larger
1489
range of <type>timestamp</type> values to be represented than
1490
shown above: from 4713 BC up to 5874897 AD.
1494
The same compile-time option also determines whether
1495
<type>time</type> and <type>interval</type> values are stored as
1496
floating-point numbers or eight-byte integers. In the
1497
floating-point case, large <type>interval</type> values degrade in
1498
precision as the size of the interval increases.
1503
For the <type>time</type> types, the allowed range of
1504
<replaceable>p</replaceable> is from 0 to 6 when eight-byte integer
1505
storage is used, or from 0 to 10 when floating-point storage is used.
1509
The <type>interval</type> type has an additional option, which is
1510
to restrict the set of stored fields by writing one of these phrases:
1525
Input falling outside the specified set of fields is silently discarded.
1526
Note that if both <replaceable>fields</replaceable> and
1527
<replaceable>precision</replaceable> are specified, the
1528
<replaceable>fields</replaceable> must include <literal>SECOND</>,
1529
since the precision applies only to the seconds.
1533
The type <type>time with time zone</type> is defined by the SQL
1534
standard, but the definition exhibits properties which lead to
1535
questionable usefulness. In most cases, a combination of
1536
<type>date</type>, <type>time</type>, <type>timestamp without time
1537
zone</type>, and <type>timestamp with time zone</type> should
1538
provide a complete range of date/time functionality required by
1543
The types <type>abstime</type>
1544
and <type>reltime</type> are lower precision types which are used internally.
1545
You are discouraged from using these types in new
1546
applications and are encouraged to move any old
1547
ones over when appropriate. Any or all of these internal types
1548
might disappear in a future release.
1551
<sect2 id="datatype-datetime-input">
1552
<title>Date/Time Input</title>
1555
Date and time input is accepted in almost any reasonable format, including
1556
ISO 8601, <acronym>SQL</acronym>-compatible,
1557
traditional <productname>POSTGRES</productname>, and others.
1558
For some formats, ordering of month, day, and year in date input is
1559
ambiguous and there is support for specifying the expected
1560
ordering of these fields. Set the <xref linkend="guc-datestyle"> parameter
1561
to <literal>MDY</> to select month-day-year interpretation,
1562
<literal>DMY</> to select day-month-year interpretation, or
1563
<literal>YMD</> to select year-month-day interpretation.
1567
<productname>PostgreSQL</productname> is more flexible in
1568
handling date/time input than the
1569
<acronym>SQL</acronym> standard requires.
1570
See <xref linkend="datetime-appendix">
1571
for the exact parsing rules of date/time input and for the
1572
recognized text fields including months, days of the week, and
1577
Remember that any date or time literal input needs to be enclosed
1578
in single quotes, like text strings. Refer to
1579
<xref linkend="sql-syntax-constants-generic"> for more
1581
<acronym>SQL</acronym> requires the following syntax
1583
<replaceable>type</replaceable> [ (<replaceable>p</replaceable>) ] '<replaceable>value</replaceable>'
1585
where <replaceable>p</replaceable> in the optional precision
1586
specification is an integer corresponding to the number of
1587
fractional digits in the seconds field. Precision can be
1588
specified for <type>time</type>, <type>timestamp</type>, and
1589
<type>interval</type> types. The allowed values are mentioned
1590
above. If no precision is specified in a constant specification,
1591
it defaults to the precision of the literal value.
1595
<title>Dates</title>
1598
<primary>date</primary>
1602
<xref linkend="datatype-datetime-date-table"> shows some possible
1603
inputs for the <type>date</type> type.
1606
<table id="datatype-datetime-date-table">
1607
<title>Date Input</title>
1611
<entry>Example</entry>
1612
<entry>Description</entry>
1617
<entry>January 8, 1999</entry>
1618
<entry>unambiguous in any <varname>datestyle</varname> input mode</entry>
1621
<entry>1999-01-08</entry>
1622
<entry>ISO 8601; January 8 in any mode
1623
(recommended format)</entry>
1626
<entry>1/8/1999</entry>
1627
<entry>January 8 in <literal>MDY</> mode;
1628
August 1 in <literal>DMY</> mode</entry>
1631
<entry>1/18/1999</entry>
1632
<entry>January 18 in <literal>MDY</> mode;
1633
rejected in other modes</entry>
1636
<entry>01/02/03</entry>
1637
<entry>January 2, 2003 in <literal>MDY</> mode;
1638
February 1, 2003 in <literal>DMY</> mode;
1639
February 3, 2001 in <literal>YMD</> mode
1643
<entry>1999-Jan-08</entry>
1644
<entry>January 8 in any mode</entry>
1647
<entry>Jan-08-1999</entry>
1648
<entry>January 8 in any mode</entry>
1651
<entry>08-Jan-1999</entry>
1652
<entry>January 8 in any mode</entry>
1655
<entry>99-Jan-08</entry>
1656
<entry>January 8 in <literal>YMD</> mode, else error</entry>
1659
<entry>08-Jan-99</entry>
1660
<entry>January 8, except error in <literal>YMD</> mode</entry>
1663
<entry>Jan-08-99</entry>
1664
<entry>January 8, except error in <literal>YMD</> mode</entry>
1667
<entry>19990108</entry>
1668
<entry>ISO 8601; January 8, 1999 in any mode</entry>
1671
<entry>990108</entry>
1672
<entry>ISO 8601; January 8, 1999 in any mode</entry>
1675
<entry>1999.008</entry>
1676
<entry>year and day of year</entry>
1679
<entry>J2451187</entry>
1680
<entry>Julian day</entry>
1683
<entry>January 8, 99 BC</entry>
1684
<entry>year 99 before the Common Era</entry>
1692
<title>Times</title>
1695
<primary>time</primary>
1698
<primary>time without time zone</primary>
1701
<primary>time with time zone</primary>
1705
The time-of-day types are <type>time [
1706
(<replaceable>p</replaceable>) ] without time zone</type> and
1707
<type>time [ (<replaceable>p</replaceable>) ] with time
1708
zone</type>. Writing just <type>time</type> is equivalent to
1709
<type>time without time zone</type>.
1713
Valid input for these types consists of a time of day followed
1714
by an optional time zone. (See <xref
1715
linkend="datatype-datetime-time-table">
1716
and <xref linkend="datatype-timezone-table">.) If a time zone is
1717
specified in the input for <type>time without time zone</type>,
1718
it is silently ignored. You can also specify a date but it will
1719
be ignored, except when you use a time zone name that involves a
1720
daylight-savings rule, such as
1721
<literal>America/New_York</literal>. In this case specifying the date
1722
is required in order to determine whether standard or daylight-savings
1723
time applies. The appropriate time zone offset is recorded in the
1724
<type>time with time zone</type> value.
1727
<table id="datatype-datetime-time-table">
1728
<title>Time Input</title>
1732
<entry>Example</entry>
1733
<entry>Description</entry>
1738
<entry><literal>04:05:06.789</literal></entry>
1739
<entry>ISO 8601</entry>
1742
<entry><literal>04:05:06</literal></entry>
1743
<entry>ISO 8601</entry>
1746
<entry><literal>04:05</literal></entry>
1747
<entry>ISO 8601</entry>
1750
<entry><literal>040506</literal></entry>
1751
<entry>ISO 8601</entry>
1754
<entry><literal>04:05 AM</literal></entry>
1755
<entry>same as 04:05; AM does not affect value</entry>
1758
<entry><literal>04:05 PM</literal></entry>
1759
<entry>same as 16:05; input hour must be <= 12</entry>
1762
<entry><literal>04:05:06.789-8</literal></entry>
1763
<entry>ISO 8601</entry>
1766
<entry><literal>04:05:06-08:00</literal></entry>
1767
<entry>ISO 8601</entry>
1770
<entry><literal>04:05-08:00</literal></entry>
1771
<entry>ISO 8601</entry>
1774
<entry><literal>040506-08</literal></entry>
1775
<entry>ISO 8601</entry>
1778
<entry><literal>04:05:06 PST</literal></entry>
1779
<entry>time zone specified by abbreviation</entry>
1782
<entry><literal>2003-04-12 04:05:06 America/New_York</literal></entry>
1783
<entry>time zone specified by full name</entry>
1789
<table tocentry="1" id="datatype-timezone-table">
1790
<title>Time Zone Input</title>
1794
<entry>Example</entry>
1795
<entry>Description</entry>
1800
<entry><literal>PST</literal></entry>
1801
<entry>Abbreviation (for Pacific Standard Time)</entry>
1804
<entry><literal>America/New_York</literal></entry>
1805
<entry>Full time zone name</entry>
1808
<entry><literal>PST8PDT</literal></entry>
1809
<entry>POSIX-style time zone specification</entry>
1812
<entry><literal>-8:00</literal></entry>
1813
<entry>ISO-8601 offset for PST</entry>
1816
<entry><literal>-800</literal></entry>
1817
<entry>ISO-8601 offset for PST</entry>
1820
<entry><literal>-8</literal></entry>
1821
<entry>ISO-8601 offset for PST</entry>
1824
<entry><literal>zulu</literal></entry>
1825
<entry>Military abbreviation for UTC</entry>
1828
<entry><literal>z</literal></entry>
1829
<entry>Short form of <literal>zulu</literal></entry>
1836
Refer to <xref linkend="datatype-timezones"> for more information on how
1837
to specify time zones.
1842
<title>Time Stamps</title>
1845
<primary>timestamp</primary>
1849
<primary>timestamp with time zone</primary>
1853
<primary>timestamp without time zone</primary>
1857
Valid input for the time stamp types consists of a concatenation
1858
of a date and a time, followed by an optional time zone,
1859
followed by an optional <literal>AD</literal> or <literal>BC</literal>.
1860
(Alternatively, <literal>AD</literal>/<literal>BC</literal> can appear
1861
before the time zone, but this is not the preferred ordering.)
1869
1999-01-08 04:05:06 -8:00
1872
are valid values, which follow the <acronym>ISO</acronym> 8601
1873
standard. In addition, the wide-spread format:
1875
January 8 04:05:06 1999 PST
1881
The <acronym>SQL</acronym> standard differentiates <type>timestamp without time zone</type>
1882
and <type>timestamp with time zone</type> literals by the presence of a
1883
<quote>+</quote> or <quote>-</quote>. Hence, according to the standard,
1884
<programlisting>TIMESTAMP '2004-10-19 10:23:54'</programlisting>
1885
is a <type>timestamp without time zone</type>, while
1886
<programlisting>TIMESTAMP '2004-10-19 10:23:54+02'</programlisting>
1887
is a <type>timestamp with time zone</type>.
1888
<productname>PostgreSQL</productname> never examines the content of a
1889
literal string before determining its type, and therefore will treat
1890
both of the above as <type>timestamp without time zone</type>. To
1891
ensure that a literal is treated as <type>timestamp with time
1892
zone</type>, give it the correct explicit type:
1893
<programlisting>TIMESTAMP WITH TIME ZONE '2004-10-19 10:23:54+02'</programlisting>
1894
In a literal that has been decided to be <type>timestamp without time
1895
zone</type>, <productname>PostgreSQL</productname> will silently ignore
1896
any time zone indication.
1897
That is, the resulting value is derived from the date/time
1898
fields in the input value, and is not adjusted for time zone.
1902
For <type>timestamp with time zone</type>, the internally stored
1903
value is always in UTC (Universal
1904
Coordinated Time, traditionally known as Greenwich Mean Time,
1905
<acronym>GMT</>). An input value that has an explicit
1906
time zone specified is converted to UTC using the appropriate offset
1907
for that time zone. If no time zone is stated in the input string,
1908
then it is assumed to be in the time zone indicated by the system's
1909
<xref linkend="guc-timezone"> parameter, and is converted to UTC using the
1910
offset for the <varname>timezone</> zone.
1914
When a <type>timestamp with time
1915
zone</type> value is output, it is always converted from UTC to the
1916
current <varname>timezone</> zone, and displayed as local time in that
1917
zone. To see the time in another time zone, either change
1918
<varname>timezone</> or use the <literal>AT TIME ZONE</> construct
1919
(see <xref linkend="functions-datetime-zoneconvert">).
1923
Conversions between <type>timestamp without time zone</type> and
1924
<type>timestamp with time zone</type> normally assume that the
1925
<type>timestamp without time zone</type> value should be taken or given
1926
as <varname>timezone</> local time. A different zone reference can
1927
be specified for the conversion using <literal>AT TIME ZONE</>.
1932
<title>Special Values</title>
1935
<primary>time</primary>
1936
<secondary>constants</secondary>
1940
<primary>date</primary>
1941
<secondary>constants</secondary>
1945
<productname>PostgreSQL</productname> supports several
1946
special date/time input values for convenience, as shown in <xref
1947
linkend="datatype-datetime-special-table">. The values
1948
<literal>infinity</literal> and <literal>-infinity</literal>
1949
are specially represented inside the system and will be displayed
1950
the same way; but the others are simply notational shorthands
1951
that will be converted to ordinary date/time values when read.
1952
(In particular, <literal>now</> and related strings are converted
1953
to a specific time value as soon as they are read.)
1954
All of these values need to be written in single quotes when used
1955
as constants in SQL commands.
1958
<table id="datatype-datetime-special-table">
1959
<title>Special Date/Time Inputs</title>
1963
<entry>Input String</entry>
1964
<entry>Valid Types</entry>
1965
<entry>Description</entry>
1970
<entry><literal>epoch</literal></entry>
1971
<entry><type>date</type>, <type>timestamp</type></entry>
1972
<entry>1970-01-01 00:00:00+00 (Unix system time zero)</entry>
1975
<entry><literal>infinity</literal></entry>
1976
<entry><type>date</type>, <type>timestamp</type></entry>
1977
<entry>later than all other time stamps</entry>
1980
<entry><literal>-infinity</literal></entry>
1981
<entry><type>date</type>, <type>timestamp</type></entry>
1982
<entry>earlier than all other time stamps</entry>
1985
<entry><literal>now</literal></entry>
1986
<entry><type>date</type>, <type>time</type>, <type>timestamp</type></entry>
1987
<entry>current transaction's start time</entry>
1990
<entry><literal>today</literal></entry>
1991
<entry><type>date</type>, <type>timestamp</type></entry>
1992
<entry>midnight today</entry>
1995
<entry><literal>tomorrow</literal></entry>
1996
<entry><type>date</type>, <type>timestamp</type></entry>
1997
<entry>midnight tomorrow</entry>
2000
<entry><literal>yesterday</literal></entry>
2001
<entry><type>date</type>, <type>timestamp</type></entry>
2002
<entry>midnight yesterday</entry>
2005
<entry><literal>allballs</literal></entry>
2006
<entry><type>time</type></entry>
2007
<entry>00:00:00.00 UTC</entry>
2014
The following <acronym>SQL</acronym>-compatible functions can also
2015
be used to obtain the current time value for the corresponding data
2017
<literal>CURRENT_DATE</literal>, <literal>CURRENT_TIME</literal>,
2018
<literal>CURRENT_TIMESTAMP</literal>, <literal>LOCALTIME</literal>,
2019
<literal>LOCALTIMESTAMP</literal>. The latter four accept an
2020
optional subsecond precision specification. (See <xref
2021
linkend="functions-datetime-current">.) Note however that these are
2022
SQL functions and are <emphasis>not</> recognized as data input strings.
2028
<sect2 id="datatype-datetime-output">
2029
<title>Date/Time Output</title>
2032
<primary>date</primary>
2033
<secondary>output format</secondary>
2034
<seealso>formatting</seealso>
2038
<primary>time</primary>
2039
<secondary>output format</secondary>
2040
<seealso>formatting</seealso>
2044
The output format of the date/time types can be set to one of the four
2046
<acronym>SQL</acronym> (Ingres), traditional POSTGRES, and
2047
German, using the command <literal>SET datestyle</literal>. The default
2048
is the <acronym>ISO</acronym> format. (The
2049
<acronym>SQL</acronym> standard requires the use of the ISO 8601
2050
format. The name of the <quote>SQL</quote> output format is a
2051
historical accident.) <xref
2052
linkend="datatype-datetime-output-table"> shows examples of each
2053
output style. The output of the <type>date</type> and
2054
<type>time</type> types is of course only the date or time part
2055
in accordance with the given examples.
2058
<table id="datatype-datetime-output-table">
2059
<title>Date/Time Output Styles</title>
2063
<entry>Style Specification</entry>
2064
<entry>Description</entry>
2065
<entry>Example</entry>
2071
<entry>ISO 8601/SQL standard</entry>
2072
<entry>1997-12-17 07:37:16-08</entry>
2076
<entry>traditional style</entry>
2077
<entry>12/17/1997 07:37:16.00 PST</entry>
2080
<entry>POSTGRES</entry>
2081
<entry>original style</entry>
2082
<entry>Wed Dec 17 07:37:16 1997 PST</entry>
2085
<entry>German</entry>
2086
<entry>regional style</entry>
2087
<entry>17.12.1997 07:37:16.00 PST</entry>
2094
In the <acronym>SQL</acronym> and POSTGRES styles, day appears before
2095
month if DMY field ordering has been specified, otherwise month appears
2097
(See <xref linkend="datatype-datetime-input">
2098
for how this setting also affects interpretation of input values.)
2099
<xref linkend="datatype-datetime-output2-table"> shows an
2103
<table id="datatype-datetime-output2-table">
2104
<title>Date Order Conventions</title>
2108
<entry><varname>datestyle</varname> Setting</entry>
2109
<entry>Input Ordering</entry>
2110
<entry>Example Output</entry>
2115
<entry><literal>SQL, DMY</></entry>
2116
<entry><replaceable>day</replaceable>/<replaceable>month</replaceable>/<replaceable>year</replaceable></entry>
2117
<entry>17/12/1997 15:37:16.00 CET</entry>
2120
<entry><literal>SQL, MDY</></entry>
2121
<entry><replaceable>month</replaceable>/<replaceable>day</replaceable>/<replaceable>year</replaceable></entry>
2122
<entry>12/17/1997 07:37:16.00 PST</entry>
2125
<entry><literal>Postgres, DMY</></entry>
2126
<entry><replaceable>day</replaceable>/<replaceable>month</replaceable>/<replaceable>year</replaceable></entry>
2127
<entry>Wed 17 Dec 07:37:16 1997 PST</entry>
2134
The date/time styles can be selected by the user using the
2135
<command>SET datestyle</command> command, the <xref
2136
linkend="guc-datestyle"> parameter in the
2137
<filename>postgresql.conf</filename> configuration file, or the
2138
<envar>PGDATESTYLE</envar> environment variable on the server or
2139
client. The formatting function <function>to_char</function>
2140
(see <xref linkend="functions-formatting">) is also available as
2141
a more flexible way to format date/time output.
2145
<sect2 id="datatype-timezones">
2146
<title>Time Zones</title>
2148
<indexterm zone="datatype-timezones">
2149
<primary>time zone</primary>
2153
Time zones, and time-zone conventions, are influenced by
2154
political decisions, not just earth geometry. Time zones around the
2155
world became somewhat standardized during the 1900's,
2156
but continue to be prone to arbitrary changes, particularly with
2157
respect to daylight-savings rules.
2158
<productname>PostgreSQL</productname> uses the widely-used
2159
<literal>zoneinfo</> time zone database for information about
2160
historical time zone rules. For times in the future, the assumption
2161
is that the latest known rules for a given time zone will
2162
continue to be observed indefinitely far into the future.
2166
<productname>PostgreSQL</productname> endeavors to be compatible with
2167
the <acronym>SQL</acronym> standard definitions for typical usage.
2168
However, the <acronym>SQL</acronym> standard has an odd mix of date and
2169
time types and capabilities. Two obvious problems are:
2174
Although the <type>date</type> type
2175
does not have an associated time zone, the
2176
<type>time</type> type can.
2177
Time zones in the real world have little meaning unless
2178
associated with a date as well as a time,
2179
since the offset can vary through the year with daylight-saving
2186
The default time zone is specified as a constant numeric offset
2187
from <acronym>UTC</>. It is therefore not possible to adapt to
2188
daylight-saving time when doing date/time arithmetic across
2189
<acronym>DST</acronym> boundaries.
2197
To address these difficulties, we recommend using date/time types
2198
that contain both date and time when using time zones. We
2199
recommend <emphasis>not</emphasis> using the type <type>time with
2200
time zone</type> (though it is supported by
2201
<productname>PostgreSQL</productname> for legacy applications and
2202
for compliance with the <acronym>SQL</acronym> standard).
2203
<productname>PostgreSQL</productname> assumes
2204
your local time zone for any type containing only date or time.
2208
All timezone-aware dates and times are stored internally in
2209
<acronym>UTC</acronym>. They are converted to local time
2210
in the zone specified by the <xref linkend="guc-timezone"> configuration
2211
parameter before being displayed to the client.
2215
<productname>PostgreSQL</productname> allows you to specify time zones in
2216
three different forms:
2220
A full time zone name, for example <literal>America/New_York</>.
2221
The recognized time zone names are listed in the
2222
<literal>pg_timezone_names</literal> view (see <xref
2223
linkend="view-pg-timezone-names">).
2224
<productname>PostgreSQL</productname> uses the widely-used
2225
<literal>zoneinfo</> time zone data for this purpose, so the same
2226
names are also recognized by much other software.
2231
A time zone abbreviation, for example <literal>PST</>. Such a
2232
specification merely defines a particular offset from UTC, in
2233
contrast to full time zone names which might imply a set of daylight
2234
savings transition-date rules as well. The recognized abbreviations
2235
are listed in the <literal>pg_timezone_abbrevs</> view (see <xref
2236
linkend="view-pg-timezone-abbrevs">). You cannot set the
2237
configuration parameters <xref linkend="guc-timezone"> or
2238
<xref linkend="guc-log-timezone"> using a time
2239
zone abbreviation, but you can use abbreviations in
2240
date/time input values and with the <literal>AT TIME ZONE</>
2246
In addition to the timezone names and abbreviations,
2247
<productname>PostgreSQL</productname> will accept POSIX-style time zone
2248
specifications of the form <replaceable>STD</><replaceable>offset</> or
2249
<replaceable>STD</><replaceable>offset</><replaceable>DST</>, where
2250
<replaceable>STD</> is a zone abbreviation, <replaceable>offset</> is a
2251
numeric offset in hours west from UTC, and <replaceable>DST</> is an
2252
optional daylight-savings zone abbreviation, assumed to stand for one
2253
hour ahead of the given offset. For example, if <literal>EST5EDT</>
2254
were not already a recognized zone name, it would be accepted and would
2255
be functionally equivalent to USA East Coast time. When a
2256
daylight-savings zone name is present, it is assumed to be used
2257
according to the same daylight-savings transition rules used in the
2258
<literal>zoneinfo</> time zone database's <filename>posixrules</> entry.
2259
In a standard <productname>PostgreSQL</productname> installation,
2260
<filename>posixrules</> is the same as <literal>US/Eastern</>, so
2261
that POSIX-style time zone specifications follow USA daylight-savings
2262
rules. If needed, you can adjust this behavior by replacing the
2263
<filename>posixrules</> file.
2268
There is a conceptual and practical difference between the abbreviations
2269
and the full names: abbreviations always represent a fixed offset from
2270
UTC, whereas most of the full names imply a local daylight-savings time
2271
rule and so have two possible UTC offsets.
2275
One should be wary that the POSIX-style time zone feature can
2276
lead to silently accepting bogus input, since there is no check on the
2277
reasonableness of the zone abbreviations. For example, <literal>SET
2278
TIMEZONE TO FOOBAR0</> will work, leaving the system effectively using
2279
a rather peculiar abbreviation for UTC.
2280
Another issue to keep in mind is that in POSIX time zone names,
2281
positive offsets are used for locations <emphasis>west</> of Greenwich.
2282
Everywhere else, <productname>PostgreSQL</productname> follows the
2283
ISO-8601 convention that positive timezone offsets are <emphasis>east</>
2288
In all cases, timezone names are recognized case-insensitively.
2289
(This is a change from <productname>PostgreSQL</productname> versions
2290
prior to 8.2, which were case-sensitive in some contexts and not others.)
2294
Neither full names nor abbreviations are hard-wired into the server;
2295
they are obtained from configuration files stored under
2296
<filename>.../share/timezone/</> and <filename>.../share/timezonesets/</>
2297
of the installation directory
2298
(see <xref linkend="datetime-config-files">).
2302
The <xref linkend="guc-timezone"> configuration parameter can
2303
be set in the file <filename>postgresql.conf</>, or in any of the
2304
other standard ways described in <xref linkend="runtime-config">.
2305
There are also several special ways to set it:
2310
If <varname>timezone</> is not specified in
2311
<filename>postgresql.conf</> nor as a server command-line option,
2312
the server attempts to use the value of the <envar>TZ</envar>
2313
environment variable as the default time zone. If <envar>TZ</envar>
2314
is not defined or is not any of the time zone names known to
2315
<productname>PostgreSQL</productname>, the server attempts to
2316
determine the operating system's default time zone by checking the
2317
behavior of the C library function <literal>localtime()</>. The
2318
default time zone is selected as the closest match among
2319
<productname>PostgreSQL</productname>'s known time zones.
2320
(These rules are also used to choose the default value of
2321
<xref linkend="guc-log-timezone">, if it is not specified.)
2327
The <acronym>SQL</acronym> command <command>SET TIME ZONE</command>
2328
sets the time zone for the session. This is an alternative spelling
2329
of <command>SET TIMEZONE TO</> with a more SQL-spec-compatible syntax.
2335
The <envar>PGTZ</envar> environment variable, if set at the
2336
client, is used by <application>libpq</application>
2337
applications to send a <command>SET TIME ZONE</command>
2338
command to the server upon connection.
2345
<sect2 id="datatype-interval-input">
2346
<title>Interval Input</title>
2349
<primary>interval</primary>
2353
<type>interval</type> values can be written with the following
2357
<optional>@</> <replaceable>quantity</> <replaceable>unit</> <optional><replaceable>quantity</> <replaceable>unit</>...</> <optional><replaceable>direction</></optional>
2360
where <replaceable>quantity</> is a number (possibly signed);
2361
<replaceable>unit</> is <literal>microsecond</literal>,
2362
<literal>millisecond</literal>, <literal>second</literal>,
2363
<literal>minute</literal>, <literal>hour</literal>, <literal>day</literal>,
2364
<literal>week</literal>, <literal>month</literal>, <literal>year</literal>,
2365
<literal>decade</literal>, <literal>century</literal>, <literal>millennium</literal>,
2366
or abbreviations or plurals of these units;
2367
<replaceable>direction</> can be <literal>ago</literal> or
2368
empty. The at sign (<literal>@</>) is optional noise. The amounts
2369
of different units are implicitly added up with appropriate
2370
sign accounting. <literal>ago</literal> negates all the fields.
2371
This syntax is also used for interval output, if
2372
<xref linkend="guc-intervalstyle"> is set to
2373
<literal>postgres_verbose</>.
2377
Quantities of days, hours, minutes, and seconds can be specified without
2378
explicit unit markings. For example, <literal>'1 12:59:10'</> is read
2379
the same as <literal>'1 day 12 hours 59 min 10 sec'</>. Also,
2380
a combination of years and months can be specified with a dash;
2381
for example <literal>'200-10'</> is read the same as <literal>'200 years
2382
10 months'</>. (These shorter forms are in fact the only ones allowed
2383
by the <acronym>SQL</acronym> standard, and are used for output when
2384
<varname>IntervalStyle</> is set to <literal>sql_standard</literal>.)
2388
Interval values can also be written as ISO 8601 time intervals, using
2389
either the <quote>format with designators</> of the standard's section
2390
4.4.3.2 or the <quote>alternative format</> of section 4.4.3.3. The
2391
format with designators looks like this:
2393
P <replaceable>quantity</> <replaceable>unit</> <optional> <replaceable>quantity</> <replaceable>unit</> ...</optional> <optional> T <optional> <replaceable>quantity</> <replaceable>unit</> ...</optional></optional>
2395
The string must start with a <literal>P</>, and may include a
2396
<literal>T</> that introduces the time-of-day units. The
2397
available unit abbreviations are given in <xref
2398
linkend="datatype-interval-iso8601-units">. Units may be
2399
omitted, and may be specified in any order, but units smaller than
2400
a day must appear after <literal>T</>. In particular, the meaning of
2401
<literal>M</> depends on whether it is before or after
2405
<table id="datatype-interval-iso8601-units">
2406
<title>ISO 8601 interval unit abbreviations</title>
2410
<entry>Abbreviation</entry>
2411
<entry>Meaning</entry>
2417
<entry>Years</entry>
2421
<entry>Months (in the date part)</entry>
2425
<entry>Weeks</entry>
2433
<entry>Hours</entry>
2437
<entry>Minutes (in the time part)</entry>
2441
<entry>Seconds</entry>
2448
In the alternative format:
2450
P <optional> <replaceable>years</>-<replaceable>months</>-<replaceable>days</> </optional> <optional> T <replaceable>hours</>:<replaceable>minutes</>:<replaceable>seconds</> </optional>
2452
the string must begin with <literal>P</literal>, and a
2453
<literal>T</> separates the date and time parts of the interval.
2454
The values are given as numbers similar to ISO 8601 dates.
2458
When writing an interval constant with a <replaceable>fields</>
2459
specification, or when assigning to an interval column that was defined
2460
with a <replaceable>fields</> specification, the interpretation of
2461
unmarked quantities depends on the <replaceable>fields</>. For
2462
example <literal>INTERVAL '1' YEAR</> is read as 1 year, whereas
2463
<literal>INTERVAL '1'</> means 1 second.
2467
According to the <acronym>SQL</> standard all fields of an interval
2468
value must have the same sign, so a leading negative sign applies to all
2469
fields; for example the negative sign in the interval literal
2470
<literal>'-1 2:03:04'</> applies to both the days and hour/minute/second
2471
parts. <productname>PostgreSQL</> allows the fields to have different
2472
signs, and traditionally treats each field in the textual representation
2473
as independently signed, so that the hour/minute/second part is
2474
considered positive in this example. If <varname>IntervalStyle</> is
2475
set to <literal>sql_standard</literal> then a leading sign is considered
2476
to apply to all fields (but only if no additional signs appear).
2477
Otherwise the traditional <productname>PostgreSQL</> interpretation is
2478
used. To avoid ambiguity, it's recommended to attach an explicit sign
2479
to each field if any field is negative.
2483
Internally <type>interval</> values are stored as months, days,
2484
and seconds. This is done because the number of days in a month
2485
varies, and a day can have 23 or 25 hours if a daylight savings
2486
time adjustment is involved. The months and days fields are integers
2487
while the seconds field can store fractions. Because intervals are
2488
usually created from constant strings or <type>timestamp</> subtraction,
2489
this storage method works well in most cases. Functions
2490
<function>justify_days</> and <function>justify_hours</> are
2491
available for adjusting days and hours that overflow their normal
2496
In the verbose input format, and in some fields of the more compact
2497
input formats, field values can have fractional parts; for example
2498
<literal>'1.5 week'</> or <literal>'01:02:03.45'</>. Such input is
2499
converted to the appropriate number of months, days, and seconds
2500
for storage. When this would result in a fractional number of
2501
months or days, the fraction is added to the lower-order fields
2502
using the conversion factors 1 month = 30 days and 1 day = 24 hours.
2503
For example, <literal>'1.5 month'</> becomes 1 month and 15 days.
2504
Only seconds will ever be shown as fractional on output.
2508
<xref linkend="datatype-interval-input-examples"> shows some examples
2509
of valid <type>interval</> input.
2512
<table id="datatype-interval-input-examples">
2513
<title>Interval Input</title>
2517
<entry>Example</entry>
2518
<entry>Description</entry>
2524
<entry>SQL standard format: 1 year 2 months</entry>
2527
<entry>3 4:05:06</entry>
2528
<entry>SQL standard format: 3 days 4 hours 5 minutes 6 seconds</entry>
2531
<entry>1 year 2 months 3 days 4 hours 5 minutes 6 seconds</entry>
2532
<entry>Traditional Postgres format: 1 year 2 months 3 days 4 hours 5 minutes 6 seconds</entry>
2535
<entry>P1Y2M3DT4H5M6S</entry>
2536
<entry>ISO 8601 <quote>format with designators</>: same meaning as above</entry>
2539
<entry>P0001-02-03T04:05:06</entry>
2540
<entry>ISO 8601 <quote>alternative format</>: same meaning as above</entry>
2548
<sect2 id="datatype-interval-output">
2549
<title>Interval Output</title>
2552
<primary>interval</primary>
2553
<secondary>output format</secondary>
2554
<seealso>formatting</seealso>
2558
The output format of the interval type can be set to one of the
2559
four styles <literal>sql_standard</>, <literal>postgres</>,
2560
<literal>postgres_verbose</>, or <literal>iso_8601</>,
2561
using the command <literal>SET intervalstyle</literal>.
2562
The default is the <literal>postgres</> format.
2563
<xref linkend="interval-style-output-table"> shows examples of each
2568
The <literal>sql_standard</> style produces output that conforms to
2569
the SQL standard's specification for interval literal strings, if
2570
the interval value meets the standard's restrictions (either year-month
2571
only or day-time only, with no mixing of positive
2572
and negative components). Otherwise the output looks like a standard
2573
year-month literal string followed by a day-time literal string,
2574
with explicit signs added to disambiguate mixed-sign intervals.
2578
The output of the <literal>postgres</> style matches the output of
2579
<productname>PostgreSQL</> releases prior to 8.4 when the
2580
<xref linkend="guc-datestyle"> parameter was set to <literal>ISO</>.
2584
The output of the <literal>postgres_verbose</> style matches the output of
2585
<productname>PostgreSQL</> releases prior to 8.4 when the
2586
<varname>DateStyle</> parameter was set to non-<literal>ISO</> output.
2590
The output of the <literal>iso_8601</> style matches the <quote>format
2591
with designators</> described in section 4.4.3.2 of the
2595
<table id="interval-style-output-table">
2596
<title>Interval Output Style Examples</title>
2600
<entry>Style Specification</entry>
2601
<entry>Year-Month Interval</entry>
2602
<entry>Day-Time Interval</entry>
2603
<entry>Mixed Interval</entry>
2608
<entry><literal>sql_standard</></entry>
2610
<entry>3 4:05:06</entry>
2611
<entry>-1-2 +3 -4:05:06</entry>
2614
<entry><literal>postgres</></entry>
2615
<entry>1 year 2 mons</entry>
2616
<entry>3 days 04:05:06</entry>
2617
<entry>-1 year -2 mons +3 days -04:05:06</entry>
2620
<entry><literal>postgres_verbose</></entry>
2621
<entry>@ 1 year 2 mons</entry>
2622
<entry>@ 3 days 4 hours 5 mins 6 secs</entry>
2623
<entry>@ 1 year 2 mons -3 days 4 hours 5 mins 6 secs ago</entry>
2626
<entry><literal>iso_8601</></entry>
2627
<entry>P1Y2M</entry>
2628
<entry>P3DT4H5M6S</entry>
2629
<entry>P-1Y-2M3DT-4H-5M-6S</entry>
2637
<sect2 id="datatype-datetime-internals">
2638
<title>Internals</title>
2641
<productname>PostgreSQL</productname> uses Julian dates
2642
for all date/time calculations. They have the nice property of correctly
2643
predicting/calculating any date more recent than 4713 BC
2644
to far into the future, using the assumption that the length of the
2645
year is 365.2425 days.
2649
Date conventions before the 19th century make for interesting reading,
2650
but are not consistent enough to warrant coding into a date/time handler.
2656
<sect1 id="datatype-boolean">
2657
<title>Boolean Type</title>
2659
<indexterm zone="datatype-boolean">
2660
<primary>Boolean</primary>
2661
<secondary>data type</secondary>
2664
<indexterm zone="datatype-boolean">
2665
<primary>true</primary>
2668
<indexterm zone="datatype-boolean">
2669
<primary>false</primary>
2673
<productname>PostgreSQL</productname> provides the
2674
standard <acronym>SQL</acronym> type <type>boolean</type>.
2675
<type>boolean</type> can have one of only two states:
2676
<quote>true</quote> or <quote>false</quote>. A third state,
2677
<quote>unknown</quote>, is represented by the
2678
<acronym>SQL</acronym> null value.
2682
Valid literal values for the <quote>true</quote> state are:
2684
<member><literal>TRUE</literal></member>
2685
<member><literal>'t'</literal></member>
2686
<member><literal>'true'</literal></member>
2687
<member><literal>'y'</literal></member>
2688
<member><literal>'yes'</literal></member>
2689
<member><literal>'on'</literal></member>
2690
<member><literal>'1'</literal></member>
2692
For the <quote>false</quote> state, the following values can be
2695
<member><literal>FALSE</literal></member>
2696
<member><literal>'f'</literal></member>
2697
<member><literal>'false'</literal></member>
2698
<member><literal>'n'</literal></member>
2699
<member><literal>'no'</literal></member>
2700
<member><literal>'off'</literal></member>
2701
<member><literal>'0'</literal></member>
2703
Leading and trailing whitespace is ignored. Using the key words
2704
<literal>TRUE</literal> and <literal>FALSE</literal> is preferred
2705
(and <acronym>SQL</acronym>-compliant).
2708
<example id="datatype-boolean-example">
2709
<title>Using the <type>boolean</type> type</title>
2712
CREATE TABLE test1 (a boolean, b text);
2713
INSERT INTO test1 VALUES (TRUE, 'sic est');
2714
INSERT INTO test1 VALUES (FALSE, 'non est');
2715
SELECT * FROM test1;
2721
SELECT * FROM test1 WHERE a;
2729
<xref linkend="datatype-boolean-example"> shows that
2730
<type>boolean</type> values are output using the letters
2731
<literal>t</literal> and <literal>f</literal>.
2735
<type>boolean</type> uses 1 byte of storage.
2739
<sect1 id="datatype-enum">
2740
<title>Enumerated Types</title>
2742
<indexterm zone="datatype-enum">
2743
<primary>data type</primary>
2744
<secondary>enumerated (enum)</secondary>
2747
<indexterm zone="datatype-enum">
2748
<primary>enumerated types</primary>
2752
Enumerated (enum) types are data types that
2753
are comprised of a static, predefined set of values with a
2754
specific order. They are equivalent to the <type>enum</type>
2755
types in a number of programming languages. An example of an enum
2756
type might be the days of the week, or a set of status values for
2761
<title>Declaration of Enumerated Types</title>
2764
Enum types are created using the <xref
2765
linkend="sql-createtype" endterm="sql-createtype-title"> command,
2769
CREATE TYPE mood AS ENUM ('sad', 'ok', 'happy');
2772
Once created, the enum type can be used in table and function
2773
definitions much like any other type:
2777
<title>Basic Enum Usage</title>
2779
CREATE TYPE mood AS ENUM ('sad', 'ok', 'happy');
2780
CREATE TABLE person (
2784
INSERT INTO person VALUES ('Moe', 'happy');
2785
SELECT * FROM person WHERE current_mood = 'happy';
2787
------+--------------
2795
<title>Ordering</title>
2798
The ordering of the values in an enum type is the
2799
order in which the values were listed when the type was declared.
2800
All standard comparison operators and related
2801
aggregate functions are supported for enums. For example:
2805
<title>Enum Ordering</title>
2807
INSERT INTO person VALUES ('Larry', 'sad');
2808
INSERT INTO person VALUES ('Curly', 'ok');
2809
SELECT * FROM person WHERE current_mood > 'sad';
2811
-------+--------------
2816
SELECT * FROM person WHERE current_mood > 'sad' ORDER BY current_mood;
2818
-------+--------------
2823
SELECT name FROM person
2824
WHERE current_mood = (SELECT MIN(current_mood) FROM person);
2834
<title>Type Safety</title>
2837
Enumerated types are completely separate data types and may not
2838
be compared with each other.
2842
<title>Lack of Casting</title>
2844
CREATE TYPE happiness AS ENUM ('happy', 'very happy', 'ecstatic');
2845
CREATE TABLE holidays (
2849
INSERT INTO holidays(num_weeks,happiness) VALUES (4, 'happy');
2850
INSERT INTO holidays(num_weeks,happiness) VALUES (6, 'very happy');
2851
INSERT INTO holidays(num_weeks,happiness) VALUES (8, 'ecstatic');
2852
INSERT INTO holidays(num_weeks,happiness) VALUES (2, 'sad');
2853
ERROR: invalid input value for enum happiness: "sad"
2854
SELECT person.name, holidays.num_weeks FROM person, holidays
2855
WHERE person.current_mood = holidays.happiness;
2856
ERROR: operator does not exist: mood = happiness
2861
If you really need to do something like that, you can either
2862
write a custom operator or add explicit casts to your query:
2866
<title>Comparing Different Enums by Casting to Text</title>
2868
SELECT person.name, holidays.num_weeks FROM person, holidays
2869
WHERE person.current_mood::text = holidays.happiness::text;
2880
<title>Implementation Details</title>
2883
An enum value occupies four bytes on disk. The length of an enum
2884
value's textual label is limited by the <symbol>NAMEDATALEN</symbol>
2885
setting compiled into <productname>PostgreSQL</productname>; in standard
2886
builds this means at most 63 bytes.
2890
Enum labels are case sensitive, so
2891
<type>'happy'</type> is not the same as <type>'HAPPY'</type>.
2892
Spaces in the labels are significant, too.
2896
The translations from internal enum values to textual labels are
2897
kept in the system catalog
2898
<link linkend="catalog-pg-enum"><structname>pg_enum</structname></link>.
2899
Querying this catalog directly can be useful.
2905
<sect1 id="datatype-geometric">
2906
<title>Geometric Types</title>
2909
Geometric data types represent two-dimensional spatial
2910
objects. <xref linkend="datatype-geo-table"> shows the geometric
2911
types available in <productname>PostgreSQL</productname>. The
2912
most fundamental type, the point, forms the basis for all of the
2916
<table id="datatype-geo-table">
2917
<title>Geometric Types</title>
2922
<entry>Storage Size</entry>
2923
<entry>Representation</entry>
2924
<entry>Description</entry>
2929
<entry><type>point</type></entry>
2930
<entry>16 bytes</entry>
2931
<entry>Point on the plane</entry>
2932
<entry>(x,y)</entry>
2935
<entry><type>line</type></entry>
2936
<entry>32 bytes</entry>
2937
<entry>Infinite line (not fully implemented)</entry>
2938
<entry>((x1,y1),(x2,y2))</entry>
2941
<entry><type>lseg</type></entry>
2942
<entry>32 bytes</entry>
2943
<entry>Finite line segment</entry>
2944
<entry>((x1,y1),(x2,y2))</entry>
2947
<entry><type>box</type></entry>
2948
<entry>32 bytes</entry>
2949
<entry>Rectangular box</entry>
2950
<entry>((x1,y1),(x2,y2))</entry>
2953
<entry><type>path</type></entry>
2954
<entry>16+16n bytes</entry>
2955
<entry>Closed path (similar to polygon)</entry>
2956
<entry>((x1,y1),...)</entry>
2959
<entry><type>path</type></entry>
2960
<entry>16+16n bytes</entry>
2961
<entry>Open path</entry>
2962
<entry>[(x1,y1),...]</entry>
2965
<entry><type>polygon</type></entry>
2966
<entry>40+16n bytes</entry>
2967
<entry>Polygon (similar to closed path)</entry>
2968
<entry>((x1,y1),...)</entry>
2971
<entry><type>circle</type></entry>
2972
<entry>24 bytes</entry>
2973
<entry>Circle</entry>
2974
<entry><(x,y),r> (center and radius)</entry>
2981
A rich set of functions and operators is available to perform various geometric
2982
operations such as scaling, translation, rotation, and determining
2983
intersections. They are explained in <xref linkend="functions-geometry">.
2987
<title>Points</title>
2990
<primary>point</primary>
2994
Points are the fundamental two-dimensional building block for geometric types.
2995
Values of type <type>point</type> are specified using the following syntax:
2998
( <replaceable>x</replaceable> , <replaceable>y</replaceable> )
2999
<replaceable>x</replaceable> , <replaceable>y</replaceable>
3002
where <replaceable>x</> and <replaceable>y</> are the respective
3003
coordinates as floating-point numbers.
3008
<title>Line Segments</title>
3011
<primary>lseg</primary>
3015
<primary>line segment</primary>
3019
Line segments (<type>lseg</type>) are represented by pairs of points.
3020
Values of type <type>lseg</type> are specified using the following syntax:
3023
( ( <replaceable>x1</replaceable> , <replaceable>y1</replaceable> ) , ( <replaceable>x2</replaceable> , <replaceable>y2</replaceable> ) )
3024
( <replaceable>x1</replaceable> , <replaceable>y1</replaceable> ) , ( <replaceable>x2</replaceable> , <replaceable>y2</replaceable> )
3025
<replaceable>x1</replaceable> , <replaceable>y1</replaceable> , <replaceable>x2</replaceable> , <replaceable>y2</replaceable>
3029
<literal>(<replaceable>x1</replaceable>,<replaceable>y1</replaceable>)</literal>
3031
<literal>(<replaceable>x2</replaceable>,<replaceable>y2</replaceable>)</literal>
3032
are the end points of the line segment.
3037
<title>Boxes</title>
3040
<primary>box (data type)</primary>
3044
<primary>rectangle</primary>
3048
Boxes are represented by pairs of points that are opposite
3050
Values of type <type>box</type> are specified using the following syntax:
3053
( ( <replaceable>x1</replaceable> , <replaceable>y1</replaceable> ) , ( <replaceable>x2</replaceable> , <replaceable>y2</replaceable> ) )
3054
( <replaceable>x1</replaceable> , <replaceable>y1</replaceable> ) , ( <replaceable>x2</replaceable> , <replaceable>y2</replaceable> )
3055
<replaceable>x1</replaceable> , <replaceable>y1</replaceable> , <replaceable>x2</replaceable> , <replaceable>y2</replaceable>
3059
<literal>(<replaceable>x1</replaceable>,<replaceable>y1</replaceable>)</literal>
3061
<literal>(<replaceable>x2</replaceable>,<replaceable>y2</replaceable>)</literal>
3062
are any two opposite corners of the box.
3066
Boxes are output using the first syntax.
3067
The corners are reordered on input to store
3068
the upper right corner, then the lower left corner.
3069
Other corners of the box can be entered, but the lower
3070
left and upper right corners are determined from the input and stored.
3075
<title>Paths</title>
3078
<primary>path (data type)</primary>
3082
Paths are represented by lists of connected points. Paths can be
3083
<firstterm>open</firstterm>, where
3084
the first and last points in the list are not considered connected, or
3085
<firstterm>closed</firstterm>,
3086
where the first and last points are considered connected.
3090
Values of type <type>path</type> are specified using the following syntax:
3093
( ( <replaceable>x1</replaceable> , <replaceable>y1</replaceable> ) , ... , ( <replaceable>xn</replaceable> , <replaceable>yn</replaceable> ) )
3094
[ ( <replaceable>x1</replaceable> , <replaceable>y1</replaceable> ) , ... , ( <replaceable>xn</replaceable> , <replaceable>yn</replaceable> ) ]
3095
( <replaceable>x1</replaceable> , <replaceable>y1</replaceable> ) , ... , ( <replaceable>xn</replaceable> , <replaceable>yn</replaceable> )
3096
( <replaceable>x1</replaceable> , <replaceable>y1</replaceable> , ... , <replaceable>xn</replaceable> , <replaceable>yn</replaceable> )
3097
<replaceable>x1</replaceable> , <replaceable>y1</replaceable> , ... , <replaceable>xn</replaceable> , <replaceable>yn</replaceable>
3100
where the points are the end points of the line segments
3101
comprising the path. Square brackets (<literal>[]</>) indicate
3102
an open path, while parentheses (<literal>()</>) indicate a
3107
Paths are output using the first syntax.
3112
<title>Polygons</title>
3115
<primary>polygon</primary>
3119
Polygons are represented by lists of points (the vertexes of the
3120
polygon). Polygons should probably be
3121
considered equivalent to closed paths, but are stored differently
3122
and have their own set of support routines.
3126
Values of type <type>polygon</type> are specified using the following syntax:
3129
( ( <replaceable>x1</replaceable> , <replaceable>y1</replaceable> ) , ... , ( <replaceable>xn</replaceable> , <replaceable>yn</replaceable> ) )
3130
( <replaceable>x1</replaceable> , <replaceable>y1</replaceable> ) , ... , ( <replaceable>xn</replaceable> , <replaceable>yn</replaceable> )
3131
( <replaceable>x1</replaceable> , <replaceable>y1</replaceable> , ... , <replaceable>xn</replaceable> , <replaceable>yn</replaceable> )
3132
<replaceable>x1</replaceable> , <replaceable>y1</replaceable> , ... , <replaceable>xn</replaceable> , <replaceable>yn</replaceable>
3135
where the points are the end points of the line segments
3136
comprising the boundary of the polygon.
3140
Polygons are output using the first syntax.
3145
<title>Circles</title>
3148
<primary>circle</primary>
3152
Circles are represented by a center point and a radius.
3153
Values of type <type>circle</type> are specified using the following syntax:
3156
< ( <replaceable>x</replaceable> , <replaceable>y</replaceable> ) , <replaceable>r</replaceable> >
3157
( ( <replaceable>x</replaceable> , <replaceable>y</replaceable> ) , <replaceable>r</replaceable> )
3158
( <replaceable>x</replaceable> , <replaceable>y</replaceable> ) , <replaceable>r</replaceable>
3159
<replaceable>x</replaceable> , <replaceable>y</replaceable> , <replaceable>r</replaceable>
3163
<literal>(<replaceable>x</replaceable>,<replaceable>y</replaceable>)</literal>
3164
is the center and <replaceable>r</replaceable> is the radius of the circle.
3168
Circles are output using the first syntax.
3174
<sect1 id="datatype-net-types">
3175
<title>Network Address Types</title>
3177
<indexterm zone="datatype-net-types">
3178
<primary>network</primary>
3179
<secondary>data types</secondary>
3183
<productname>PostgreSQL</> offers data types to store IPv4, IPv6, and MAC
3184
addresses, as shown in <xref linkend="datatype-net-types-table">. It
3185
is preferable to use these types instead of plain text types to store
3186
network addresses, because
3187
these types offer input error checking and several specialized
3188
operators and functions (see <xref linkend="functions-net">).
3191
<table tocentry="1" id="datatype-net-types-table">
3192
<title>Network Address Types</title>
3197
<entry>Storage Size</entry>
3198
<entry>Description</entry>
3204
<entry><type>cidr</type></entry>
3205
<entry>7 or 19 bytes</entry>
3206
<entry>IPv4 and IPv6 networks</entry>
3210
<entry><type>inet</type></entry>
3211
<entry>7 or 19 bytes</entry>
3212
<entry>IPv4 and IPv6 hosts and networks</entry>
3216
<entry><type>macaddr</type></entry>
3217
<entry>6 bytes</entry>
3218
<entry>MAC addresses</entry>
3226
When sorting <type>inet</type> or <type>cidr</type> data types,
3227
IPv4 addresses will always sort before IPv6 addresses, including
3228
IPv4 addresses encapsulated or mapped into IPv6 addresses, such as
3229
::10.2.3.4 or ::ffff:10.4.3.2.
3233
<sect2 id="datatype-inet">
3234
<title><type>inet</type></title>
3237
<primary>inet (data type)</primary>
3241
The <type>inet</type> type holds an IPv4 or IPv6 host address, and
3242
optionally the identity of the subnet it is in, all in one field.
3243
The subnet identity is represented by stating how many bits of
3244
the host address represent the network address (the
3245
<quote>netmask</quote>). If the netmask is 32 and the address is IPv4,
3246
then the value does not indicate a subnet, only a single host.
3247
In IPv6, the address length is 128 bits, so 128 bits specify a
3248
unique host address. Note that if you
3249
want to accept networks only, you should use the
3250
<type>cidr</type> type rather than <type>inet</type>.
3254
The input format for this type is
3255
<replaceable class="parameter">address/y</replaceable>
3257
<replaceable class="parameter">address</replaceable>
3258
is an IPv4 or IPv6 address and
3259
<replaceable class="parameter">y</replaceable>
3260
is the number of bits in the netmask. If the
3261
<replaceable class="parameter">/y</replaceable>
3262
part is left off, then the
3263
netmask is 32 for IPv4 and 128 for IPv6, so the value represents
3264
just a single host. On display, the
3265
<replaceable class="parameter">/y</replaceable>
3266
portion is suppressed if the netmask specifies a single host.
3270
<sect2 id="datatype-cidr">
3271
<title><type>cidr</></title>
3274
<primary>cidr</primary>
3278
The <type>cidr</type> type holds an IPv4 or IPv6 network specification.
3279
Input and output formats follow Classless Internet Domain Routing
3281
The format for specifying networks is <replaceable
3282
class="parameter">address/y</> where <replaceable
3283
class="parameter">address</> is the network represented as an
3284
IPv4 or IPv6 address, and <replaceable
3285
class="parameter">y</> is the number of bits in the netmask. If
3286
<replaceable class="parameter">y</> is omitted, it is calculated
3287
using assumptions from the older classful network numbering system, except
3288
that it will be at least large enough to include all of the octets
3289
written in the input. It is an error to specify a network address
3290
that has bits set to the right of the specified netmask.
3294
<xref linkend="datatype-net-cidr-table"> shows some examples.
3297
<table id="datatype-net-cidr-table">
3298
<title><type>cidr</> Type Input Examples</title>
3302
<entry><type>cidr</type> Input</entry>
3303
<entry><type>cidr</type> Output</entry>
3304
<entry><literal><function>abbrev</function>(<type>cidr</type>)</literal></entry>
3309
<entry>192.168.100.128/25</entry>
3310
<entry>192.168.100.128/25</entry>
3311
<entry>192.168.100.128/25</entry>
3314
<entry>192.168/24</entry>
3315
<entry>192.168.0.0/24</entry>
3316
<entry>192.168.0/24</entry>
3319
<entry>192.168/25</entry>
3320
<entry>192.168.0.0/25</entry>
3321
<entry>192.168.0.0/25</entry>
3324
<entry>192.168.1</entry>
3325
<entry>192.168.1.0/24</entry>
3326
<entry>192.168.1/24</entry>
3329
<entry>192.168</entry>
3330
<entry>192.168.0.0/24</entry>
3331
<entry>192.168.0/24</entry>
3334
<entry>128.1</entry>
3335
<entry>128.1.0.0/16</entry>
3336
<entry>128.1/16</entry>
3340
<entry>128.0.0.0/16</entry>
3341
<entry>128.0/16</entry>
3344
<entry>128.1.2</entry>
3345
<entry>128.1.2.0/24</entry>
3346
<entry>128.1.2/24</entry>
3349
<entry>10.1.2</entry>
3350
<entry>10.1.2.0/24</entry>
3351
<entry>10.1.2/24</entry>
3355
<entry>10.1.0.0/16</entry>
3356
<entry>10.1/16</entry>
3360
<entry>10.0.0.0/8</entry>
3364
<entry>10.1.2.3/32</entry>
3365
<entry>10.1.2.3/32</entry>
3366
<entry>10.1.2.3/32</entry>
3369
<entry>2001:4f8:3:ba::/64</entry>
3370
<entry>2001:4f8:3:ba::/64</entry>
3371
<entry>2001:4f8:3:ba::/64</entry>
3374
<entry>2001:4f8:3:ba:2e0:81ff:fe22:d1f1/128</entry>
3375
<entry>2001:4f8:3:ba:2e0:81ff:fe22:d1f1/128</entry>
3376
<entry>2001:4f8:3:ba:2e0:81ff:fe22:d1f1</entry>
3379
<entry>::ffff:1.2.3.0/120</entry>
3380
<entry>::ffff:1.2.3.0/120</entry>
3381
<entry>::ffff:1.2.3/120</entry>
3384
<entry>::ffff:1.2.3.0/128</entry>
3385
<entry>::ffff:1.2.3.0/128</entry>
3386
<entry>::ffff:1.2.3.0/128</entry>
3393
<sect2 id="datatype-inet-vs-cidr">
3394
<title><type>inet</type> vs. <type>cidr</type></title>
3397
The essential difference between <type>inet</type> and <type>cidr</type>
3398
data types is that <type>inet</type> accepts values with nonzero bits to
3399
the right of the netmask, whereas <type>cidr</type> does not.
3404
If you do not like the output format for <type>inet</type> or
3405
<type>cidr</type> values, try the functions <function>host</>,
3406
<function>text</>, and <function>abbrev</>.
3411
<sect2 id="datatype-macaddr">
3412
<title><type>macaddr</></>
3415
<primary>macaddr (data type)</primary>
3419
<primary>MAC address</primary>
3424
The <type>macaddr</> type stores MAC addresses, known for example
3425
from Ethernet card hardware addresses (although MAC addresses are
3426
used for other purposes as well). Input is accepted in the
3430
<member><literal>'08:00:2b:01:02:03'</></member>
3431
<member><literal>'08-00-2b-01-02-03'</></member>
3432
<member><literal>'08002b:010203'</></member>
3433
<member><literal>'08002b-010203'</></member>
3434
<member><literal>'0800.2b01.0203'</></member>
3435
<member><literal>'08002b010203'</></member>
3438
These examples would all specify the same address. Upper and
3439
lower case is accepted for the digits
3440
<literal>a</> through <literal>f</>. Output is always in the
3441
first of the forms shown.
3445
IEEE Std 802-2001 specifies the second shown form (with hyphens)
3446
as the canonical form for MAC addresses, and specifies the first
3447
form (with colons) as the bit-reversed notation, so that
3448
08-00-2b-01-02-03 = 01:00:4D:08:04:0C. This convention is widely
3449
ignored nowadays, and it is only relevant for obsolete network
3450
protocols (such as Token Ring). PostgreSQL makes no provisions
3451
for bit reversal, and all accepted formats use the canonical LSB
3456
The remaining four input formats are not part of any standard.
3462
<sect1 id="datatype-bit">
3463
<title>Bit String Types</title>
3465
<indexterm zone="datatype-bit">
3466
<primary>bit string</primary>
3467
<secondary>data type</secondary>
3471
Bit strings are strings of 1's and 0's. They can be used to store
3472
or visualize bit masks. There are two SQL bit types:
3473
<type>bit(<replaceable>n</replaceable>)</type> and <type>bit
3474
varying(<replaceable>n</replaceable>)</type>, where
3475
<replaceable>n</replaceable> is a positive integer.
3479
<type>bit</type> type data must match the length
3480
<replaceable>n</replaceable> exactly; it is an error to attempt to
3481
store shorter or longer bit strings. <type>bit varying</type> data is
3482
of variable length up to the maximum length
3483
<replaceable>n</replaceable>; longer strings will be rejected.
3484
Writing <type>bit</type> without a length is equivalent to
3485
<literal>bit(1)</literal>, while <type>bit varying</type> without a length
3486
specification means unlimited length.
3491
If one explicitly casts a bit-string value to
3492
<type>bit(<replaceable>n</>)</type>, it will be truncated or
3493
zero-padded on the right to be exactly <replaceable>n</> bits,
3494
without raising an error. Similarly,
3495
if one explicitly casts a bit-string value to
3496
<type>bit varying(<replaceable>n</>)</type>, it will be truncated
3497
on the right if it is more than <replaceable>n</> bits.
3503
linkend="sql-syntax-bit-strings"> for information about the syntax
3504
of bit string constants. Bit-logical operators and string
3505
manipulation functions are available; see <xref
3506
linkend="functions-bitstring">.
3510
<title>Using the bit string types</title>
3513
CREATE TABLE test (a BIT(3), b BIT VARYING(5));
3514
INSERT INTO test VALUES (B'101', B'00');
3515
INSERT INTO test VALUES (B'10', B'101');
3517
ERROR: bit string length 2 does not match type bit(3)
3519
INSERT INTO test VALUES (B'10'::bit(3), B'101');
3531
A bit string value requires 1 byte for each group of 8 bits, plus
3532
5 or 8 bytes overhead depending on the length of the string
3533
(but long values may be compressed or moved out-of-line, as explained
3534
in <xref linkend="datatype-character"> for character strings).
3538
<sect1 id="datatype-textsearch">
3539
<title>Text Search Types</title>
3541
<indexterm zone="datatype-textsearch">
3542
<primary>full text search</primary>
3543
<secondary>data types</secondary>
3546
<indexterm zone="datatype-textsearch">
3547
<primary>text search</primary>
3548
<secondary>data types</secondary>
3552
<productname>PostgreSQL</productname> provides two data types that
3553
are designed to support full text search, which is the activity of
3554
searching through a collection of natural-language <firstterm>documents</>
3555
to locate those that best match a <firstterm>query</>.
3556
The <type>tsvector</type> type represents a document in a form suited
3557
for text search, while the <type>tsquery</type> type similarly represents
3559
<xref linkend="textsearch"> provides a detailed explanation of this
3560
facility, and <xref linkend="functions-textsearch"> summarizes the
3561
related functions and operators.
3564
<sect2 id="datatype-tsvector">
3565
<title><type>tsvector</type></title>
3568
<primary>tsvector (data type)</primary>
3572
A <type>tsvector</type> value is a sorted list of distinct
3573
<firstterm>lexemes</>, which are words that have been
3574
<firstterm>normalized</> to make different variants of the same word look
3575
alike (see <xref linkend="textsearch"> for details). Sorting and
3576
duplicate-elimination are done automatically during input, as shown in
3580
SELECT 'a fat cat sat on a mat and ate a fat rat'::tsvector;
3582
----------------------------------------------------
3583
'a' 'and' 'ate' 'cat' 'fat' 'mat' 'on' 'rat' 'sat'
3587
lexemes containing whitespace or punctuation, surround them with quotes:
3590
SELECT $$the lexeme ' ' contains spaces$$::tsvector;
3592
-------------------------------------------
3593
' ' 'contains' 'lexeme' 'spaces' 'the'
3596
(We use dollar-quoted string literals in this example and the next one,
3597
to avoid confusing matters by having to double quote marks within the
3598
literals.) Embedded quotes and backslashes must be doubled:
3601
SELECT $$the lexeme 'Joe''s' contains a quote$$::tsvector;
3603
------------------------------------------------
3604
'Joe''s' 'a' 'contains' 'lexeme' 'quote' 'the'
3607
Optionally, integer <firstterm>position(s)</>
3608
can be attached to any or all of the lexemes:
3611
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;
3613
-------------------------------------------------------------------------------
3614
'a':1,6,10 'and':8 'ate':9 'cat':3 'fat':2,11 'mat':7 'on':5 'rat':12 'sat':4
3617
A position normally indicates the source word's location in the
3618
document. Positional information can be used for
3619
<firstterm>proximity ranking</firstterm>. Position values can
3620
range from 1 to 16383; larger numbers are silently clamped to 16383.
3621
Duplicate positions for the same lexeme are discarded.
3625
Lexemes that have positions can further be labeled with a
3626
<firstterm>weight</>, which can be <literal>A</literal>,
3627
<literal>B</literal>, <literal>C</literal>, or <literal>D</literal>.
3628
<literal>D</literal> is the default and hence is not shown on output:
3631
SELECT 'a:1A fat:2B,4C cat:5D'::tsvector;
3633
----------------------------
3634
'a':1A 'cat':5 'fat':2B,4C
3637
Weights are typically used to reflect document structure, for example
3638
by marking title words differently from body words. Text search
3639
ranking functions can assign different priorities to the different
3644
It is important to understand that the
3645
<type>tsvector</type> type itself does not perform any normalization;
3646
it assumes that the words it is given are normalized appropriately
3647
for the application. For example,
3650
select 'The Fat Rats'::tsvector;
3652
--------------------
3656
For most English-text-searching applications the above words would
3657
be considered non-normalized, but <type>tsvector</type> doesn't care.
3658
Raw document text should usually be passed through
3659
<function>to_tsvector</> to normalize the words appropriately
3663
SELECT to_tsvector('english', 'The Fat Rats');
3669
Again, see <xref linkend="textsearch"> for more detail.
3674
<sect2 id="datatype-tsquery">
3675
<title><type>tsquery</type></title>
3678
<primary>tsquery (data type)</primary>
3682
A <type>tsquery</type> value stores lexemes that are to be
3683
searched for, and combines them using the boolean operators
3684
<literal>&</literal> (AND), <literal>|</literal> (OR), and
3685
<literal>!</> (NOT). Parentheses can be used to enforce grouping
3689
SELECT 'fat & rat'::tsquery;
3694
SELECT 'fat & (rat | cat)'::tsquery;
3696
---------------------------
3697
'fat' & ( 'rat' | 'cat' )
3699
SELECT 'fat & rat & ! cat'::tsquery;
3701
------------------------
3702
'fat' & 'rat' & !'cat'
3705
In the absence of parentheses, <literal>!</> (NOT) binds most tightly,
3706
and <literal>&</literal> (AND) binds more tightly than
3707
<literal>|</literal> (OR).
3711
Optionally, lexemes in a <type>tsquery</type> can be labeled with
3712
one or more weight letters, which restricts them to match only
3713
<type>tsvector</> lexemes with one of those weights:
3716
SELECT 'fat:ab & cat'::tsquery;
3719
'fat':AB & 'cat'
3724
Also, lexemes in a <type>tsquery</type> can be labeled with <literal>*</>
3725
to specify prefix matching:
3727
SELECT 'super:*'::tsquery;
3732
This query will match any word in a <type>tsvector</> that begins
3733
with <quote>super</>.
3737
Quoting rules for lexemes are the same as described above for
3738
lexemes in <type>tsvector</>; and, as with <type>tsvector</>,
3739
any required normalization of words must be done before putting
3740
them into the <type>tsquery</> type. The <function>to_tsquery</>
3741
function is convenient for performing such normalization:
3744
SELECT to_tsquery('Fat:ab & Cats');
3747
'fat':AB & 'cat'
3755
<sect1 id="datatype-uuid">
3756
<title><acronym>UUID</acronym> Type</title>
3758
<indexterm zone="datatype-uuid">
3759
<primary>UUID</primary>
3763
The data type <type>uuid</type> stores Universally Unique Identifiers
3764
(UUID) as defined by RFC 4122, ISO/IEC 9834-8:2005, and related standards.
3765
(Some systems refer to this data type as globally unique identifier, or
3766
GUID,<indexterm><primary>GUID</primary></indexterm> instead.) Such an
3767
identifier is a 128-bit quantity that is generated by an algorithm chosen
3768
to make it very unlikely that the same identifier will be generated by
3769
anyone else in the known universe using the same algorithm. Therefore,
3770
for distributed systems, these identifiers provide a better uniqueness
3771
guarantee than that which can be achieved using sequence generators, which
3772
are only unique within a single database.
3776
A UUID is written as a sequence of lower-case hexadecimal digits,
3777
in several groups separated by hyphens, specifically a group of 8
3778
digits followed by three groups of 4 digits followed by a group of
3779
12 digits, for a total of 32 digits representing the 128 bits. An
3780
example of a UUID in this standard form is:
3782
a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11
3784
<productname>PostgreSQL</productname> also accepts the following
3785
alternative forms for input:
3786
use of upper-case digits, the standard format surrounded by
3787
braces, omitting some or all hyphens, adding a hyphen after any
3788
group of four digits. Examples are:
3790
A0EEBC99-9C0B-4EF8-BB6D-6BB9BD380A11
3791
{a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11}
3792
a0eebc999c0b4ef8bb6d6bb9bd380a11
3793
a0ee-bc99-9c0b-4ef8-bb6d-6bb9-bd38-0a11
3794
{a0eebc99-9c0b4ef8-bb6d6bb9-bd380a11}
3796
Output is always in the standard form.
3800
<productname>PostgreSQL</productname> provides storage and comparison
3801
functions for UUIDs, but the core database does not include any
3802
function for generating UUIDs, because no single algorithm is well
3803
suited for every application. The contrib module
3804
<filename>contrib/uuid-ossp</filename> provides functions that implement
3805
several standard algorithms.
3806
Alternatively, UUIDs could be generated by client applications or
3807
other libraries invoked through a server-side function.
3811
<sect1 id="datatype-xml">
3812
<title><acronym>XML</> Type</title>
3814
<indexterm zone="datatype-xml">
3815
<primary>XML</primary>
3819
The data type <type>xml</type> can be used to store XML data. Its
3820
advantage over storing XML data in a <type>text</type> field is that it
3821
checks the input values for well-formedness, and there are support
3822
functions to perform type-safe operations on it; see <xref
3823
linkend="functions-xml">. Use of this data type requires the
3824
installation to have been built with <command>configure
3829
The <type>xml</type> type can store well-formed
3830
<quote>documents</quote>, as defined by the XML standard, as well
3831
as <quote>content</quote> fragments, which are defined by the
3832
production <literal>XMLDecl? content</literal> in the XML
3833
standard. Roughly, this means that content fragments can have
3834
more than one top-level element or character node. The expression
3835
<literal><replaceable>xmlvalue</replaceable> IS DOCUMENT</literal>
3836
can be used to evaluate whether a particular <type>xml</type>
3837
value is a full document or only a content fragment.
3841
<title>Creating XML Values</title>
3843
To produce a value of type <type>xml</type> from character data,
3845
<function>xmlparse</function>:<indexterm><primary>xmlparse</primary></indexterm>
3847
XMLPARSE ( { DOCUMENT | CONTENT } <replaceable>value</replaceable>)
3850
<programlisting><![CDATA[
3851
XMLPARSE (DOCUMENT '<?xml version="1.0"?><book><title>Manual</title><chapter>...</chapter></book>')
3852
XMLPARSE (CONTENT 'abc<foo>bar</foo><bar>foo</bar>')
3853
]]></programlisting>
3854
While this is the only way to convert character strings into XML
3855
values according to the SQL standard, the PostgreSQL-specific
3857
<programlisting><![CDATA[
3858
xml '<foo>bar</foo>'
3859
'<foo>bar</foo>'::xml
3860
]]></programlisting>
3865
The <type>xml</type> type does not validate its input values
3866
against a possibly included document type declaration
3867
(DTD).<indexterm><primary>DTD</primary></indexterm>
3871
The inverse operation, producing character string type values from
3872
<type>xml</type>, uses the function
3873
<function>xmlserialize</function>:<indexterm><primary>xmlserialize</primary></indexterm>
3875
XMLSERIALIZE ( { DOCUMENT | CONTENT } <replaceable>value</replaceable> AS <replaceable>type</replaceable> )
3877
<replaceable>type</replaceable> can be one of
3878
<type>character</type>, <type>character varying</type>, or
3879
<type>text</type> (or an alias name for those). Again, according
3880
to the SQL standard, this is the only way to convert between type
3881
<type>xml</type> and character types, but PostgreSQL also allows
3882
you to simply cast the value.
3886
When character string values are cast to or from type
3887
<type>xml</type> without going through <type>XMLPARSE</type> or
3888
<type>XMLSERIALIZE</type>, respectively, the choice of
3889
<literal>DOCUMENT</literal> versus <literal>CONTENT</literal> is
3890
determined by the <quote>XML option</quote>
3891
<indexterm><primary>XML option</primary></indexterm>
3892
session configuration parameter, which can be set using the
3895
SET XML OPTION { DOCUMENT | CONTENT };
3897
or the more PostgreSQL-like syntax
3899
SET xmloption TO { DOCUMENT | CONTENT };
3901
The default is <literal>CONTENT</literal>, so all forms of XML
3907
<title>Encoding Handling</title>
3909
Care must be taken when dealing with multiple character encodings
3910
on the client, server, and in the XML data passed through them.
3911
When using the text mode to pass queries to the server and query
3912
results to the client (which is the normal mode), PostgreSQL
3913
converts all character data passed between the client and the
3914
server and vice versa to the character encoding of the respective
3915
end; see <xref linkend="multibyte">. This includes string
3916
representations of XML values, such as in the above examples.
3917
This would ordinarily mean that encoding declarations contained in
3918
XML data might become invalid as the character data is converted
3919
to other encodings while travelling between client and server,
3920
while the embedded encoding declaration is not changed. To cope
3921
with this behavior, an encoding declaration contained in a
3922
character string presented for input to the <type>xml</type> type
3923
is <emphasis>ignored</emphasis>, and the content is always assumed
3924
to be in the current server encoding. Consequently, for correct
3925
processing, such character strings of XML data must be sent off
3926
from the client in the current client encoding. It is the
3927
responsibility of the client to either convert the document to the
3928
current client encoding before sending it off to the server or to
3929
adjust the client encoding appropriately. On output, values of
3930
type <type>xml</type> will not have an encoding declaration, and
3931
clients must assume that the data is in the current client
3936
When using the binary mode to pass query parameters to the server
3937
and query results back to the client, no character set conversion
3938
is performed, so the situation is different. In this case, an
3939
encoding declaration in the XML data will be observed, and if it
3940
is absent, the data will be assumed to be in UTF-8 (as required by
3941
the XML standard; note that PostgreSQL does not support UTF-16 at
3942
all). On output, data will have an encoding declaration
3943
specifying the client encoding, unless the client encoding is
3944
UTF-8, in which case it will be omitted.
3948
Needless to say, processing XML data with PostgreSQL will be less
3949
error-prone and more efficient if data encoding, client encoding,
3950
and server encoding are the same. Since XML data is internally
3951
processed in UTF-8, computations will be most efficient if the
3952
server encoding is also UTF-8.
3957
<title>Accessing XML Values</title>
3960
The <type>xml</type> data type is unusual in that it does not
3961
provide any comparison operators. This is because there is no
3962
well-defined and universally useful comparison algorithm for XML
3963
data. One consequence of this is that you cannot retrieve rows by
3964
comparing an <type>xml</type> column against a search value. XML
3965
values should therefore typically be accompanied by a separate key
3966
field such as an ID. An alternative solution for comparing XML
3967
values is to convert them to character strings first, but note
3968
that character string comparison has little to do with a useful
3969
XML comparison method.
3973
Since there are no comparison operators for the <type>xml</type>
3974
data type, it is not possible to create an index directly on a
3975
column of this type. If speedy searches in XML data are desired,
3976
possible workarounds would be casting the expression to a
3977
character string type and indexing that, or indexing an XPath
3978
expression. The actual query would of course have to be adjusted
3979
to search by the indexed expression.
3983
The text-search functionality in PostgreSQL could also be used to speed
3984
up full-document searches in XML data. The necessary
3985
preprocessing support is, however, not available in the PostgreSQL
3986
distribution in this release.
3995
<sect1 id="datatype-oid">
3996
<title>Object Identifier Types</title>
3998
<indexterm zone="datatype-oid">
3999
<primary>object identifier</primary>
4000
<secondary>data type</secondary>
4003
<indexterm zone="datatype-oid">
4004
<primary>oid</primary>
4007
<indexterm zone="datatype-oid">
4008
<primary>regproc</primary>
4011
<indexterm zone="datatype-oid">
4012
<primary>regprocedure</primary>
4015
<indexterm zone="datatype-oid">
4016
<primary>regoper</primary>
4019
<indexterm zone="datatype-oid">
4020
<primary>regoperator</primary>
4023
<indexterm zone="datatype-oid">
4024
<primary>regclass</primary>
4027
<indexterm zone="datatype-oid">
4028
<primary>regtype</primary>
4031
<indexterm zone="datatype-oid">
4032
<primary>regconfig</primary>
4035
<indexterm zone="datatype-oid">
4036
<primary>regdictionary</primary>
4039
<indexterm zone="datatype-oid">
4040
<primary>xid</primary>
4043
<indexterm zone="datatype-oid">
4044
<primary>cid</primary>
4047
<indexterm zone="datatype-oid">
4048
<primary>tid</primary>
4052
Object identifiers (OIDs) are used internally by
4053
<productname>PostgreSQL</productname> as primary keys for various
4054
system tables. OIDs are not added to user-created tables, unless
4055
<literal>WITH OIDS</literal> is specified when the table is
4056
created, or the <xref linkend="guc-default-with-oids">
4057
configuration variable is enabled. Type <type>oid</> represents
4058
an object identifier. There are also several alias types for
4059
<type>oid</>: <type>regproc</>, <type>regprocedure</>,
4060
<type>regoper</>, <type>regoperator</>, <type>regclass</>,
4061
<type>regtype</>, <type>regconfig</>, and <type>regdictionary</>.
4062
<xref linkend="datatype-oid-table"> shows an overview.
4066
The <type>oid</> type is currently implemented as an unsigned
4067
four-byte integer. Therefore, it is not large enough to provide
4068
database-wide uniqueness in large databases, or even in large
4069
individual tables. So, using a user-created table's OID column as
4070
a primary key is discouraged. OIDs are best used only for
4071
references to system tables.
4075
The <type>oid</> type itself has few operations beyond comparison.
4076
It can be cast to integer, however, and then manipulated using the
4077
standard integer operators. (Beware of possible
4078
signed-versus-unsigned confusion if you do this.)
4082
The OID alias types have no operations of their own except
4083
for specialized input and output routines. These routines are able
4084
to accept and display symbolic names for system objects, rather than
4085
the raw numeric value that type <type>oid</> would use. The alias
4086
types allow simplified lookup of OID values for objects. For example,
4087
to examine the <structname>pg_attribute</> rows related to a table
4088
<literal>mytable</>, one could write:
4090
SELECT * FROM pg_attribute WHERE attrelid = 'mytable'::regclass;
4094
SELECT * FROM pg_attribute
4095
WHERE attrelid = (SELECT oid FROM pg_class WHERE relname = 'mytable');
4097
While that doesn't look all that bad by itself, it's still oversimplified.
4098
A far more complicated sub-select would be needed to
4099
select the right OID if there are multiple tables named
4100
<literal>mytable</> in different schemas.
4101
The <type>regclass</> input converter handles the table lookup according
4102
to the schema path setting, and so it does the <quote>right thing</>
4103
automatically. Similarly, casting a table's OID to
4104
<type>regclass</> is handy for symbolic display of a numeric OID.
4107
<table id="datatype-oid-table">
4108
<title>Object Identifier Types</title>
4113
<entry>References</entry>
4114
<entry>Description</entry>
4115
<entry>Value Example</entry>
4122
<entry><type>oid</></entry>
4124
<entry>numeric object identifier</entry>
4125
<entry><literal>564182</></entry>
4129
<entry><type>regproc</></entry>
4130
<entry><structname>pg_proc</></entry>
4131
<entry>function name</entry>
4132
<entry><literal>sum</></entry>
4136
<entry><type>regprocedure</></entry>
4137
<entry><structname>pg_proc</></entry>
4138
<entry>function with argument types</entry>
4139
<entry><literal>sum(int4)</></entry>
4143
<entry><type>regoper</></entry>
4144
<entry><structname>pg_operator</></entry>
4145
<entry>operator name</entry>
4146
<entry><literal>+</></entry>
4150
<entry><type>regoperator</></entry>
4151
<entry><structname>pg_operator</></entry>
4152
<entry>operator with argument types</entry>
4153
<entry><literal>*(integer,integer)</> or <literal>-(NONE,integer)</></entry>
4157
<entry><type>regclass</></entry>
4158
<entry><structname>pg_class</></entry>
4159
<entry>relation name</entry>
4160
<entry><literal>pg_type</></entry>
4164
<entry><type>regtype</></entry>
4165
<entry><structname>pg_type</></entry>
4166
<entry>data type name</entry>
4167
<entry><literal>integer</></entry>
4171
<entry><type>regconfig</></entry>
4172
<entry><structname>pg_ts_config</></entry>
4173
<entry>text search configuration</entry>
4174
<entry><literal>english</></entry>
4178
<entry><type>regdictionary</></entry>
4179
<entry><structname>pg_ts_dict</></entry>
4180
<entry>text search dictionary</entry>
4181
<entry><literal>simple</></entry>
4188
All of the OID alias types accept schema-qualified names, and will
4189
display schema-qualified names on output if the object would not
4190
be found in the current search path without being qualified.
4191
The <type>regproc</> and <type>regoper</> alias types will only
4192
accept input names that are unique (not overloaded), so they are
4193
of limited use; for most uses <type>regprocedure</> or
4194
<type>regoperator</> is more appropriate. For <type>regoperator</>,
4195
unary operators are identified by writing <literal>NONE</> for the unused
4200
An additional property of the OID alias types is that if a
4201
constant of one of these types appears in a stored expression
4202
(such as a column default expression or view), it creates a dependency
4203
on the referenced object. For example, if a column has a default
4204
expression <literal>nextval('my_seq'::regclass)</>,
4205
<productname>PostgreSQL</productname>
4206
understands that the default expression depends on the sequence
4207
<literal>my_seq</>; the system will not let the sequence be dropped
4208
without first removing the default expression.
4212
Another identifier type used by the system is <type>xid</>, or transaction
4213
(abbreviated <abbrev>xact</>) identifier. This is the data type of the system columns
4214
<structfield>xmin</> and <structfield>xmax</>. Transaction identifiers are 32-bit quantities.
4218
A third identifier type used by the system is <type>cid</>, or
4219
command identifier. This is the data type of the system columns
4220
<structfield>cmin</> and <structfield>cmax</>. Command identifiers are also 32-bit quantities.
4224
A final identifier type used by the system is <type>tid</>, or tuple
4225
identifier (row identifier). This is the data type of the system column
4226
<structfield>ctid</>. A tuple ID is a pair
4227
(block number, tuple index within block) that identifies the
4228
physical location of the row within its table.
4232
(The system columns are further explained in <xref
4233
linkend="ddl-system-columns">.)
4237
<sect1 id="datatype-pseudo">
4238
<title>Pseudo-Types</title>
4240
<indexterm zone="datatype-pseudo">
4241
<primary>record</primary>
4244
<indexterm zone="datatype-pseudo">
4245
<primary>any</primary>
4248
<indexterm zone="datatype-pseudo">
4249
<primary>anyelement</primary>
4252
<indexterm zone="datatype-pseudo">
4253
<primary>anyarray</primary>
4256
<indexterm zone="datatype-pseudo">
4257
<primary>anynonarray</primary>
4260
<indexterm zone="datatype-pseudo">
4261
<primary>anyenum</primary>
4264
<indexterm zone="datatype-pseudo">
4265
<primary>void</primary>
4268
<indexterm zone="datatype-pseudo">
4269
<primary>trigger</primary>
4272
<indexterm zone="datatype-pseudo">
4273
<primary>language_handler</primary>
4276
<indexterm zone="datatype-pseudo">
4277
<primary>cstring</primary>
4280
<indexterm zone="datatype-pseudo">
4281
<primary>internal</primary>
4284
<indexterm zone="datatype-pseudo">
4285
<primary>opaque</primary>
4289
The <productname>PostgreSQL</productname> type system contains a
4290
number of special-purpose entries that are collectively called
4291
<firstterm>pseudo-types</>. A pseudo-type cannot be used as a
4292
column data type, but it can be used to declare a function's
4293
argument or result type. Each of the available pseudo-types is
4294
useful in situations where a function's behavior does not
4295
correspond to simply taking or returning a value of a specific
4296
<acronym>SQL</acronym> data type. <xref
4297
linkend="datatype-pseudotypes-table"> lists the existing
4301
<table id="datatype-pseudotypes-table">
4302
<title>Pseudo-Types</title>
4307
<entry>Description</entry>
4313
<entry><type>any</></entry>
4314
<entry>Indicates that a function accepts any input data type whatever.</entry>
4318
<entry><type>anyarray</></entry>
4319
<entry>Indicates that a function accepts any array data type
4320
(see <xref linkend="extend-types-polymorphic">).</entry>
4324
<entry><type>anyelement</></entry>
4325
<entry>Indicates that a function accepts any data type
4326
(see <xref linkend="extend-types-polymorphic">).</entry>
4330
<entry><type>anyenum</></entry>
4331
<entry>Indicates that a function accepts any enum data type
4332
(see <xref linkend="extend-types-polymorphic"> and
4333
<xref linkend="datatype-enum">).</entry>
4337
<entry><type>anynonarray</></entry>
4338
<entry>Indicates that a function accepts any non-array data type
4339
(see <xref linkend="extend-types-polymorphic">).</entry>
4343
<entry><type>cstring</></entry>
4344
<entry>Indicates that a function accepts or returns a null-terminated C string.</entry>
4348
<entry><type>internal</></entry>
4349
<entry>Indicates that a function accepts or returns a server-internal
4354
<entry><type>language_handler</></entry>
4355
<entry>A procedural language call handler is declared to return <type>language_handler</>.</entry>
4359
<entry><type>record</></entry>
4360
<entry>Identifies a function returning an unspecified row type.</entry>
4364
<entry><type>trigger</></entry>
4365
<entry>A trigger function is declared to return <type>trigger.</></entry>
4369
<entry><type>void</></entry>
4370
<entry>Indicates that a function returns no value.</entry>
4374
<entry><type>opaque</></entry>
4375
<entry>An obsolete type name that formerly served all the above purposes.</entry>
4382
Functions coded in C (whether built-in or dynamically loaded) can be
4383
declared to accept or return any of these pseudo data types. It is up to
4384
the function author to ensure that the function will behave safely
4385
when a pseudo-type is used as an argument type.
4389
Functions coded in procedural languages can use pseudo-types only as
4390
allowed by their implementation languages. At present the procedural
4391
languages all forbid use of a pseudo-type as argument type, and allow
4392
only <type>void</> and <type>record</> as a result type (plus
4393
<type>trigger</> when the function is used as a trigger). Some also
4394
support polymorphic functions using the types <type>anyarray</>,
4395
<type>anyelement</>, <type>anyenum</>, and <type>anynonarray</>.
4399
The <type>internal</> pseudo-type is used to declare functions
4400
that are meant only to be called internally by the database
4401
system, and not by direct invocation in a <acronym>SQL</acronym>
4402
query. If a function has at least one <type>internal</>-type
4403
argument then it cannot be called from <acronym>SQL</acronym>. To
4404
preserve the type safety of this restriction it is important to
4405
follow this coding rule: do not create any function that is
4406
declared to return <type>internal</> unless it has at least one
4407
<type>internal</> argument.