2
$PostgreSQL: pgsql/doc/src/sgml/datatype.sgml,v 1.154.4.1 2005-01-22 23:05:47 momjian Exp $
5
<chapter id="datatype">
6
<title id="datatype-title">Data Types</title>
8
<indexterm zone="datatype">
9
<primary>data type</primary>
13
<primary>type</primary>
18
<productname>PostgreSQL</productname> has a rich set of native data
19
types available to users.
20
Users may add new types to <productname>PostgreSQL</productname> using the
21
<command>CREATE TYPE</command> command.
25
<xref linkend="datatype-table"> shows all the built-in general-purpose data
26
types. Most of the alternative names listed in the
27
<quote>Aliases</quote> column are the names used internally by
28
<productname>PostgreSQL</productname> for historical reasons. In
29
addition, some internally used or deprecated types are available,
30
but they are not listed here.
33
<table id="datatype-table">
34
<title>Data Types</title>
39
<entry>Aliases</entry>
40
<entry>Description</entry>
46
<entry><type>bigint</type></entry>
47
<entry><type>int8</type></entry>
48
<entry>signed eight-byte integer</entry>
52
<entry><type>bigserial</type></entry>
53
<entry><type>serial8</type></entry>
54
<entry>autoincrementing eight-byte integer</entry>
58
<entry><type>bit [ (<replaceable>n</replaceable>) ]</type></entry>
60
<entry>fixed-length bit string</entry>
64
<entry><type>bit varying [ (<replaceable>n</replaceable>) ]</type></entry>
65
<entry><type>varbit</type></entry>
66
<entry>variable-length bit string</entry>
70
<entry><type>boolean</type></entry>
71
<entry><type>bool</type></entry>
72
<entry>logical Boolean (true/false)</entry>
76
<entry><type>box</type></entry>
78
<entry>rectangular box in the plane</entry>
82
<entry><type>bytea</type></entry>
84
<entry>binary data (<quote>byte array</>)</entry>
88
<entry><type>character varying [ (<replaceable>n</replaceable>) ]</type></entry>
89
<entry><type>varchar [ (<replaceable>n</replaceable>) ]</type></entry>
90
<entry>variable-length character string</entry>
94
<entry><type>character [ (<replaceable>n</replaceable>) ]</type></entry>
95
<entry><type>char [ (<replaceable>n</replaceable>) ]</type></entry>
96
<entry>fixed-length character string</entry>
100
<entry><type>cidr</type></entry>
102
<entry>IPv4 or IPv6 network address</entry>
106
<entry><type>circle</type></entry>
108
<entry>circle in the plane</entry>
112
<entry><type>date</type></entry>
114
<entry>calendar date (year, month, day)</entry>
118
<entry><type>double precision</type></entry>
119
<entry><type>float8</type></entry>
120
<entry>double precision floating-point number</entry>
124
<entry><type>inet</type></entry>
126
<entry>IPv4 or IPv6 host address</entry>
130
<entry><type>integer</type></entry>
131
<entry><type>int</type>, <type>int4</type></entry>
132
<entry>signed four-byte integer</entry>
136
<entry><type>interval [ (<replaceable>p</replaceable>) ]</type></entry>
138
<entry>time span</entry>
142
<entry><type>line</type></entry>
144
<entry>infinite line in the plane</entry>
148
<entry><type>lseg</type></entry>
150
<entry>line segment in the plane</entry>
154
<entry><type>macaddr</type></entry>
156
<entry>MAC address</entry>
160
<entry><type>money</type></entry>
162
<entry>currency amount</entry>
166
<entry><type>numeric [ (<replaceable>p</replaceable>,
167
<replaceable>s</replaceable>) ]</type></entry>
168
<entry><type>decimal [ (<replaceable>p</replaceable>,
169
<replaceable>s</replaceable>) ]</type></entry>
170
<entry>exact numeric of selectable precision</entry>
174
<entry><type>path</type></entry>
176
<entry>geometric path in the plane</entry>
180
<entry><type>point</type></entry>
182
<entry>geometric point in the plane</entry>
186
<entry><type>polygon</type></entry>
188
<entry>closed geometric path in the plane</entry>
192
<entry><type>real</type></entry>
193
<entry><type>float4</type></entry>
194
<entry>single precision floating-point number</entry>
198
<entry><type>smallint</type></entry>
199
<entry><type>int2</type></entry>
200
<entry>signed two-byte integer</entry>
204
<entry><type>serial</type></entry>
205
<entry><type>serial4</type></entry>
206
<entry>autoincrementing four-byte integer</entry>
210
<entry><type>text</type></entry>
212
<entry>variable-length character string</entry>
216
<entry><type>time [ (<replaceable>p</replaceable>) ] [ without time zone ]</type></entry>
218
<entry>time of day</entry>
222
<entry><type>time [ (<replaceable>p</replaceable>) ] with time zone</type></entry>
223
<entry><type>timetz</type></entry>
224
<entry>time of day, including time zone</entry>
228
<entry><type>timestamp [ (<replaceable>p</replaceable>) ] [ without time zone ]</type></entry>
230
<entry>date and time</entry>
234
<entry><type>timestamp [ (<replaceable>p</replaceable>) ] with time zone</type></entry>
235
<entry><type>timestamptz</type></entry>
236
<entry>date and time, including time zone</entry>
243
<title>Compatibility</title>
245
The following types (or spellings thereof) are specified by
246
<acronym>SQL</acronym>: <type>bit</type>, <type>bit
247
varying</type>, <type>boolean</type>, <type>char</type>,
248
<type>character varying</type>, <type>character</type>,
249
<type>varchar</type>, <type>date</type>, <type>double
250
precision</type>, <type>integer</type>, <type>interval</type>,
251
<type>numeric</type>, <type>decimal</type>, <type>real</type>,
252
<type>smallint</type>, <type>time</type> (with or without time zone),
253
<type>timestamp</type> (with or without time zone).
258
Each data type has an external representation determined by its input
259
and output functions. Many of the built-in types have
260
obvious external formats. However, several types are either unique
261
to <productname>PostgreSQL</productname>, such as geometric
262
paths, or have several possibilities for formats, such as the date
264
Some of the input and output functions are not invertible. That is,
265
the result of an output function may lose accuracy when compared to
269
<sect1 id="datatype-numeric">
270
<title>Numeric Types</title>
272
<indexterm zone="datatype-numeric">
273
<primary>data type</primary>
274
<secondary>numeric</secondary>
278
Numeric types consist of two-, four-, and eight-byte integers,
279
four- and eight-byte floating-point numbers, and selectable-precision
280
decimals. <xref linkend="datatype-numeric-table"> lists the
284
<table id="datatype-numeric-table">
285
<title>Numeric Types</title>
290
<entry>Storage Size</entry>
291
<entry>Description</entry>
298
<entry><type>smallint</></entry>
299
<entry>2 bytes</entry>
300
<entry>small-range integer</entry>
301
<entry>-32768 to +32767</entry>
304
<entry><type>integer</></entry>
305
<entry>4 bytes</entry>
306
<entry>usual choice for integer</entry>
307
<entry>-2147483648 to +2147483647</entry>
310
<entry><type>bigint</></entry>
311
<entry>8 bytes</entry>
312
<entry>large-range integer</entry>
313
<entry>-9223372036854775808 to 9223372036854775807</entry>
317
<entry><type>decimal</></entry>
318
<entry>variable</entry>
319
<entry>user-specified precision, exact</entry>
320
<entry>no limit</entry>
323
<entry><type>numeric</></entry>
324
<entry>variable</entry>
325
<entry>user-specified precision, exact</entry>
326
<entry>no limit</entry>
330
<entry><type>real</></entry>
331
<entry>4 bytes</entry>
332
<entry>variable-precision, inexact</entry>
333
<entry>6 decimal digits precision</entry>
336
<entry><type>double precision</></entry>
337
<entry>8 bytes</entry>
338
<entry>variable-precision, inexact</entry>
339
<entry>15 decimal digits precision</entry>
343
<entry><type>serial</></entry>
344
<entry>4 bytes</entry>
345
<entry>autoincrementing integer</entry>
346
<entry>1 to 2147483647</entry>
350
<entry><type>bigserial</type></entry>
351
<entry>8 bytes</entry>
352
<entry>large autoincrementing integer</entry>
353
<entry>1 to 9223372036854775807</entry>
360
The syntax of constants for the numeric types is described in
361
<xref linkend="sql-syntax-constants">. The numeric types have a
362
full set of corresponding arithmetic operators and
363
functions. Refer to <xref linkend="functions"> for more
364
information. The following sections describe the types in detail.
367
<sect2 id="datatype-int">
368
<title>Integer Types</title>
370
<indexterm zone="datatype-int">
371
<primary>integer</primary>
374
<indexterm zone="datatype-int">
375
<primary>smallint</primary>
378
<indexterm zone="datatype-int">
379
<primary>bigint</primary>
383
<primary>int4</primary>
388
<primary>int2</primary>
393
<primary>int8</primary>
398
The types <type>smallint</type>, <type>integer</type>, and
399
<type>bigint</type> store whole numbers, that is, numbers without
400
fractional components, of various ranges. Attempts to store
401
values outside of the allowed range will result in an error.
405
The type <type>integer</type> is the usual choice, as it offers
406
the best balance between range, storage size, and performance.
407
The <type>smallint</type> type is generally only used if disk
408
space is at a premium. The <type>bigint</type> type should only
409
be used if the <type>integer</type> range is not sufficient,
410
because the latter is definitely faster.
414
The <type>bigint</type> type may not function correctly on all
415
platforms, since it relies on compiler support for eight-byte
416
integers. On a machine without such support, <type>bigint</type>
417
acts the same as <type>integer</type> (but still takes up eight
418
bytes of storage). However, we are not aware of any reasonable
419
platform where this is actually the case.
423
<acronym>SQL</acronym> only specifies the integer types
424
<type>integer</type> (or <type>int</type>) and
425
<type>smallint</type>. The type <type>bigint</type>, and the
426
type names <type>int2</type>, <type>int4</type>, and
427
<type>int8</type> are extensions, which are shared with various
428
other <acronym>SQL</acronym> database systems.
433
<sect2 id="datatype-numeric-decimal">
434
<title>Arbitrary Precision Numbers</title>
436
<indexterm zone="datatype-numeric-decimal">
437
<primary>numeric (data type)</primary>
441
<primary>decimal</primary>
446
The type <type>numeric</type> can store numbers with up to 1000
447
digits of precision and perform calculations exactly. It is
448
especially recommended for storing monetary amounts and other
449
quantities where exactness is required. However, arithmetic on
450
<type>numeric</type> values is very slow compared to the integer
451
types, or to the floating-point types described in the next section.
455
In what follows we use these terms: The
456
<firstterm>scale</firstterm> of a <type>numeric</type> is the
457
count of decimal digits in the fractional part, to the right of
458
the decimal point. The <firstterm>precision</firstterm> of a
459
<type>numeric</type> is the total count of significant digits in
460
the whole number, that is, the number of digits to both sides of
461
the decimal point. So the number 23.5141 has a precision of 6
462
and a scale of 4. Integers can be considered to have a scale of
467
Both the maximum precision and the maximum scale of a
468
<type>numeric</type> column can be
469
configured. To declare a column of type <type>numeric</type> use
472
NUMERIC(<replaceable>precision</replaceable>, <replaceable>scale</replaceable>)
474
The precision must be positive, the scale zero or positive.
477
NUMERIC(<replaceable>precision</replaceable>)
479
selects a scale of 0. Specifying
483
without any precision or scale creates a column in which numeric
484
values of any precision and scale can be stored, up to the
485
implementation limit on precision. A column of this kind will
486
not coerce input values to any particular scale, whereas
487
<type>numeric</type> columns with a declared scale will coerce
488
input values to that scale. (The <acronym>SQL</acronym> standard
489
requires a default scale of 0, i.e., coercion to integer
490
precision. We find this a bit useless. If you're concerned
491
about portability, always specify the precision and scale
496
If the scale of a value to be stored is greater than the declared
497
scale of the column, the system will round the value to the specified
498
number of fractional digits. Then, if the number of digits to the
499
left of the decimal point exceeds the declared precision minus the
500
declared scale, an error is raised.
504
Numeric values are physically stored without any extra leading or
505
trailing zeroes. Thus, the declared precision and scale of a column
506
are maximums, not fixed allocations. (In this sense the <type>numeric</>
507
type is more akin to <type>varchar(<replaceable>n</>)</type>
508
than to <type>char(<replaceable>n</>)</type>.)
512
In addition to ordinary numeric values, the <type>numeric</type>
513
type allows the special value <literal>NaN</>, meaning
514
<quote>not-a-number</quote>. Any operation on <literal>NaN</>
515
yields another <literal>NaN</>. When writing this value
516
as a constant in a SQL command, you must put quotes around it,
517
for example <literal>UPDATE table SET x = 'NaN'</>. On input,
518
the string <literal>NaN</> is recognized in a case-insensitive manner.
522
The types <type>decimal</type> and <type>numeric</type> are
523
equivalent. Both types are part of the <acronym>SQL</acronym>
529
<sect2 id="datatype-float">
530
<title>Floating-Point Types</title>
532
<indexterm zone="datatype-float">
533
<primary>real</primary>
536
<indexterm zone="datatype-float">
537
<primary>double precision</primary>
541
<primary>float4</primary>
546
<primary>float8</primary>
547
<see>double precision</see>
550
<indexterm zone="datatype-float">
551
<primary>floating point</primary>
555
The data types <type>real</type> and <type>double
556
precision</type> are inexact, variable-precision numeric types.
557
In practice, these types are usually implementations of
558
<acronym>IEEE</acronym> Standard 754 for Binary Floating-Point
559
Arithmetic (single and double precision, respectively), to the
560
extent that the underlying processor, operating system, and
565
Inexact means that some values cannot be converted exactly to the
566
internal format and are stored as approximations, so that storing
567
and printing back out a value may show slight discrepancies.
568
Managing these errors and how they propagate through calculations
569
is the subject of an entire branch of mathematics and computer
570
science and will not be discussed further here, except for the
575
If you require exact storage and calculations (such as for
576
monetary amounts), use the <type>numeric</type> type instead.
582
If you want to do complicated calculations with these types
583
for anything important, especially if you rely on certain
584
behavior in boundary cases (infinity, underflow), you should
585
evaluate the implementation carefully.
591
Comparing two floating-point values for equality may or may
592
not work as expected.
599
On most platforms, the <type>real</type> type has a range of at least
600
1E-37 to 1E+37 with a precision of at least 6 decimal digits. The
601
<type>double precision</type> type typically has a range of around
602
1E-307 to 1E+308 with a precision of at least 15 digits. Values that
603
are too large or too small will cause an error. Rounding may
604
take place if the precision of an input number is too high.
605
Numbers too close to zero that are not representable as distinct
606
from zero will cause an underflow error.
610
In addition to ordinary numeric values, the floating-point types
611
have several special values:
613
<literal>Infinity</literal>
614
<literal>-Infinity</literal>
615
<literal>NaN</literal>
617
These represent the IEEE 754 special values
618
<quote>infinity</quote>, <quote>negative infinity</quote>, and
619
<quote>not-a-number</quote>, respectively. (On a machine whose
620
floating-point arithmetic does not follow IEEE 754, these values
621
will probably not work as expected.) When writing these values
622
as constants in a SQL command, you must put quotes around them,
623
for example <literal>UPDATE table SET x = 'Infinity'</>. On input,
624
these strings are recognized in a case-insensitive manner.
628
<productname>PostgreSQL</productname> also supports the SQL-standard
629
notations <type>float</type> and
630
<type>float(<replaceable>p</replaceable>)</type> for specifying
631
inexact numeric types. Here, <replaceable>p</replaceable> specifies
632
the minimum acceptable precision in binary digits.
633
<productname>PostgreSQL</productname> accepts
634
<type>float(1)</type> to <type>float(24)</type> as selecting the
635
<type>real</type> type, while
636
<type>float(25)</type> to <type>float(53)</type> select
637
<type>double precision</type>. Values of <replaceable>p</replaceable>
638
outside the allowed range draw an error.
639
<type>float</type> with no precision specified is taken to mean
640
<type>double precision</type>.
645
Prior to <productname>PostgreSQL</productname> 7.4, the precision in
646
<type>float(<replaceable>p</replaceable>)</type> was taken to mean
647
so many decimal digits. This has been corrected to match the SQL
648
standard, which specifies that the precision is measured in binary
649
digits. The assumption that <type>real</type> and
650
<type>double precision</type> have exactly 24 and 53 bits in the
651
mantissa respectively is correct for IEEE-standard floating point
652
implementations. On non-IEEE platforms it may be off a little, but
653
for simplicity the same ranges of <replaceable>p</replaceable> are used
660
<sect2 id="datatype-serial">
661
<title>Serial Types</title>
663
<indexterm zone="datatype-serial">
664
<primary>serial</primary>
667
<indexterm zone="datatype-serial">
668
<primary>bigserial</primary>
671
<indexterm zone="datatype-serial">
672
<primary>serial4</primary>
675
<indexterm zone="datatype-serial">
676
<primary>serial8</primary>
680
<primary>auto-increment</primary>
685
<primary>sequence</primary>
686
<secondary>and serial type</secondary>
690
The data types <type>serial</type> and <type>bigserial</type>
691
are not true types, but merely
692
a notational convenience for setting up unique identifier columns
693
(similar to the <literal>AUTO_INCREMENT</literal> property
694
supported by some other databases). In the current
695
implementation, specifying
698
CREATE TABLE <replaceable class="parameter">tablename</replaceable> (
699
<replaceable class="parameter">colname</replaceable> SERIAL
703
is equivalent to specifying:
706
CREATE SEQUENCE <replaceable class="parameter">tablename</replaceable>_<replaceable class="parameter">colname</replaceable>_seq;
707
CREATE TABLE <replaceable class="parameter">tablename</replaceable> (
708
<replaceable class="parameter">colname</replaceable> integer DEFAULT nextval('<replaceable class="parameter">tablename</replaceable>_<replaceable class="parameter">colname</replaceable>_seq') NOT NULL
712
Thus, we have created an integer column and arranged for its default
713
values to be assigned from a sequence generator. A <literal>NOT NULL</>
714
constraint is applied to ensure that a null value cannot be explicitly
715
inserted, either. In most cases you would also want to attach a
716
<literal>UNIQUE</> or <literal>PRIMARY KEY</> constraint to prevent
717
duplicate values from being inserted by accident, but this is
723
Prior to <productname>PostgreSQL</productname> 7.3, <type>serial</type>
724
implied <literal>UNIQUE</literal>. This is no longer automatic. If
725
you wish a serial column to be in a unique constraint or a
726
primary key, it must now be specified, same as with
732
To insert the next value of the sequence into the <type>serial</type>
733
column, specify that the <type>serial</type>
734
column should be assigned its default value. This can be done
735
either by excluding the column from the list of columns in
736
the <command>INSERT</command> statement, or through the use of
737
the <literal>DEFAULT</literal> key word.
741
The type names <type>serial</type> and <type>serial4</type> are
742
equivalent: both create <type>integer</type> columns. The type
743
names <type>bigserial</type> and <type>serial8</type> work just
744
the same way, except that they create a <type>bigint</type>
745
column. <type>bigserial</type> should be used if you anticipate
746
the use of more than 2<superscript>31</> identifiers over the
747
lifetime of the table.
751
The sequence created for a <type>serial</type> column is
752
automatically dropped when the owning column is dropped, and
753
cannot be dropped otherwise. (This was not true in
754
<productname>PostgreSQL</productname> releases before 7.3. Note
755
that this automatic drop linkage will not occur for a sequence
756
created by reloading a dump from a pre-7.3 database; the dump
757
file does not contain the information needed to establish the
758
dependency link.) Furthermore, this dependency between sequence
759
and column is made only for the <type>serial</> column itself. If
760
any other columns reference the sequence (perhaps by manually
761
calling the <function>nextval</> function), they will be broken
762
if the sequence is removed. Using a <type>serial</> column's sequence
763
in such a fashion is considered bad form; if you wish to feed several
764
columns from the same sequence generator, create the sequence as an
770
<sect1 id="datatype-money">
771
<title>Monetary Types</title>
775
The <type>money</type> type is deprecated. Use
776
<type>numeric</type> or <type>decimal</type> instead, in
777
combination with the <function>to_char</function> function.
782
The <type>money</type> type stores a currency amount with a fixed
783
fractional precision; see <xref
784
linkend="datatype-money-table">.
785
Input is accepted in a variety of formats, including integer and
786
floating-point literals, as well as <quote>typical</quote>
787
currency formatting, such as <literal>'$1,000.00'</literal>.
788
Output is generally in the latter form but depends on the locale.
791
<table id="datatype-money-table">
792
<title>Monetary Types</title>
797
<entry>Storage Size</entry>
798
<entry>Description</entry>
805
<entry>4 bytes</entry>
806
<entry>currency amount</entry>
807
<entry>-21474836.48 to +21474836.47</entry>
815
<sect1 id="datatype-character">
816
<title>Character Types</title>
818
<indexterm zone="datatype-character">
819
<primary>character string</primary>
820
<secondary>data types</secondary>
824
<primary>string</primary>
825
<see>character string</see>
828
<indexterm zone="datatype-character">
829
<primary>character</primary>
832
<indexterm zone="datatype-character">
833
<primary>character varying</primary>
836
<indexterm zone="datatype-character">
837
<primary>text</primary>
840
<indexterm zone="datatype-character">
841
<primary>char</primary>
844
<indexterm zone="datatype-character">
845
<primary>varchar</primary>
848
<table id="datatype-character-table">
849
<title>Character Types</title>
854
<entry>Description</entry>
859
<entry><type>character varying(<replaceable>n</>)</type>, <type>varchar(<replaceable>n</>)</type></entry>
860
<entry>variable-length with limit</entry>
863
<entry><type>character(<replaceable>n</>)</type>, <type>char(<replaceable>n</>)</type></entry>
864
<entry>fixed-length, blank padded</entry>
867
<entry><type>text</type></entry>
868
<entry>variable unlimited length</entry>
875
<xref linkend="datatype-character-table"> shows the
876
general-purpose character types available in
877
<productname>PostgreSQL</productname>.
881
<acronym>SQL</acronym> defines two primary character types:
882
<type>character varying(<replaceable>n</>)</type> and
883
<type>character(<replaceable>n</>)</type>, where <replaceable>n</>
884
is a positive integer. Both of these types can store strings up to
885
<replaceable>n</> characters in length. An attempt to store a
886
longer string into a column of these types will result in an
887
error, unless the excess characters are all spaces, in which case
888
the string will be truncated to the maximum length. (This somewhat
889
bizarre exception is required by the <acronym>SQL</acronym>
890
standard.) If the string to be stored is shorter than the declared
891
length, values of type <type>character</type> will be space-padded;
892
values of type <type>character varying</type> will simply store the
898
If one explicitly casts a value to <type>character
899
varying(<replaceable>n</>)</type> or
900
<type>character(<replaceable>n</>)</type>, then an over-length
901
value will be truncated to <replaceable>n</> characters without
902
raising an error. (This too is required by the
903
<acronym>SQL</acronym> standard.)
908
Prior to <productname>PostgreSQL</> 7.2, strings that were too long were
909
always truncated without raising an error, in either explicit or
910
implicit casting contexts.
915
The notations <type>varchar(<replaceable>n</>)</type> and
916
<type>char(<replaceable>n</>)</type> are aliases for <type>character
917
varying(<replaceable>n</>)</type> and
918
<type>character(<replaceable>n</>)</type>, respectively.
919
<type>character</type> without length specifier is equivalent to
920
<type>character(1)</type>. If <type>character varying</type> is used
921
without length specifier, the type accepts strings of any size. The
922
latter is a <productname>PostgreSQL</> extension.
926
In addition, <productname>PostgreSQL</productname> provides the
927
<type>text</type> type, which stores strings of any length.
928
Although the type <type>text</type> is not in the
929
<acronym>SQL</acronym> standard, several other SQL database
930
management systems have it as well.
934
Values of type <type>character</type> are physically padded
935
with spaces to the specified width <replaceable>n</>, and are
936
stored and displayed that way. However, the padding spaces are
937
treated as semantically insignificant. Trailing spaces are
938
disregarded when comparing two values of type <type>character</type>,
939
and they will be removed when converting a <type>character</type> value
940
to one of the other string types. Note that trailing spaces
941
<emphasis>are</> semantically significant in
942
<type>character varying</type> and <type>text</type> values.
946
The storage requirement for data of these types is 4 bytes plus the
947
actual string, and in case of <type>character</type> plus the
948
padding. Long strings are compressed by the system automatically, so
949
the physical requirement on disk may be less. Long values are also
950
stored in background tables so they do not interfere with rapid
951
access to the shorter column values. In any case, the longest
952
possible character string that can be stored is about 1 GB. (The
953
maximum value that will be allowed for <replaceable>n</> in the data
954
type declaration is less than that. It wouldn't be very useful to
955
change this because with multibyte character encodings the number of
956
characters and bytes can be quite different anyway. If you desire to
957
store long strings with no specific upper limit, use
958
<type>text</type> or <type>character varying</type> without a length
959
specifier, rather than making up an arbitrary length limit.)
964
There are no performance differences between these three types,
965
apart from the increased storage size when using the blank-padded
966
type. While <type>character(<replaceable>n</>)</type> has performance
967
advantages in some other database systems, it has no such advantages in
968
<productname>PostgreSQL</productname>. In most situations
969
<type>text</type> or <type>character varying</type> should be used
975
Refer to <xref linkend="sql-syntax-strings"> for information about
976
the syntax of string literals, and to <xref linkend="functions">
977
for information about available operators and functions. The
978
database character set determines the character set used to store
979
textual values; for more information on character set support,
980
refer to <xref linkend="multibyte">.
984
<title>Using the character types</title>
987
CREATE TABLE test1 (a character(4));
988
INSERT INTO test1 VALUES ('ok');
989
SELECT a, char_length(a) FROM test1; -- <co id="co.datatype-char">
996
CREATE TABLE test2 (b varchar(5));
997
INSERT INTO test2 VALUES ('ok');
998
INSERT INTO test2 VALUES ('good ');
999
INSERT INTO test2 VALUES ('too long');
1000
<computeroutput>ERROR: value too long for type character varying(5)</computeroutput>
1001
INSERT INTO test2 VALUES ('too long'::varchar(5)); -- explicit truncation
1002
SELECT b, char_length(b) FROM test2;
1005
-------+-------------
1012
<callout arearefs="co.datatype-char">
1014
The <function>char_length</function> function is discussed in
1015
<xref linkend="functions-string">.
1022
There are two other fixed-length character types in
1023
<productname>PostgreSQL</productname>, shown in <xref
1024
linkend="datatype-character-special-table">. The <type>name</type>
1025
type exists <emphasis>only</emphasis> for storage of identifiers
1026
in the internal system catalogs and is not intended for use by the general user. Its
1027
length is currently defined as 64 bytes (63 usable characters plus
1028
terminator) but should be referenced using the constant
1029
<symbol>NAMEDATALEN</symbol>. The length is set at compile time (and
1030
is therefore adjustable for special uses); the default maximum
1031
length may change in a future release. The type <type>"char"</type>
1032
(note the quotes) is different from <type>char(1)</type> in that it
1033
only uses one byte of storage. It is internally used in the system
1034
catalogs as a poor-man's enumeration type.
1037
<table id="datatype-character-special-table">
1038
<title>Special Character Types</title>
1043
<entry>Storage Size</entry>
1044
<entry>Description</entry>
1049
<entry><type>"char"</type></entry>
1050
<entry>1 byte</entry>
1051
<entry>single-character internal type</entry>
1054
<entry><type>name</type></entry>
1055
<entry>64 bytes</entry>
1056
<entry>internal type for object names</entry>
1064
<sect1 id="datatype-binary">
1065
<title>Binary Data Types</title>
1067
<indexterm zone="datatype-binary">
1068
<primary>binary data</primary>
1071
<indexterm zone="datatype-binary">
1072
<primary>bytea</primary>
1076
The <type>bytea</type> data type allows storage of binary strings;
1077
see <xref linkend="datatype-binary-table">.
1080
<table id="datatype-binary-table">
1081
<title>Binary Data Types</title>
1086
<entry>Storage Size</entry>
1087
<entry>Description</entry>
1092
<entry><type>bytea</type></entry>
1093
<entry>4 bytes plus the actual binary string</entry>
1094
<entry>variable-length binary string</entry>
1101
A binary string is a sequence of octets (or bytes). Binary
1102
strings are distinguished from character strings by two
1103
characteristics: First, binary strings specifically allow storing
1104
octets of value zero and other <quote>non-printable</quote>
1105
octets (usually, octets outside the range 32 to 126).
1106
Character strings disallow zero octets, and also disallow any
1107
other octet values and sequences of octet values that are invalid
1108
according to the database's selected character set encoding.
1109
Second, operations on binary strings process the actual bytes,
1110
whereas the processing of character strings depends on locale settings.
1111
In short, binary strings are appropriate for storing data that the
1112
programmer thinks of as <quote>raw bytes</>, whereas character
1113
strings are appropriate for storing text.
1117
When entering <type>bytea</type> values, octets of certain values
1118
<emphasis>must</emphasis> be escaped (but all octet values
1119
<emphasis>may</emphasis> be escaped) when used as part of a string
1120
literal in an <acronym>SQL</acronym> statement. In general, to
1121
escape an octet, it is converted into the three-digit octal number
1122
equivalent of its decimal octet value, and preceded by two
1123
backslashes. <xref linkend="datatype-binary-sqlesc"> shows the
1124
characters that must be escaped, and gives the alternate escape
1125
sequences where applicable.
1128
<table id="datatype-binary-sqlesc">
1129
<title><type>bytea</> Literal Escaped Octets</title>
1133
<entry>Decimal Octet Value</entry>
1134
<entry>Description</entry>
1135
<entry>Escaped Input Representation</entry>
1136
<entry>Example</entry>
1137
<entry>Output Representation</entry>
1144
<entry>zero octet</entry>
1145
<entry><literal>'\\000'</literal></entry>
1146
<entry><literal>SELECT '\\000'::bytea;</literal></entry>
1147
<entry><literal>\000</literal></entry>
1152
<entry>single quote</entry>
1153
<entry><literal>'\''</literal> or <literal>'\\047'</literal></entry>
1154
<entry><literal>SELECT '\''::bytea;</literal></entry>
1155
<entry><literal>'</literal></entry>
1160
<entry>backslash</entry>
1161
<entry><literal>'\\\\'</literal> or <literal>'\\134'</literal></entry>
1162
<entry><literal>SELECT '\\\\'::bytea;</literal></entry>
1163
<entry><literal>\\</literal></entry>
1167
<entry>0 to 31 and 127 to 255</entry>
1168
<entry><quote>non-printable</quote> octets</entry>
1169
<entry><literal>'\\<replaceable>xxx'</></literal> (octal value)</entry>
1170
<entry><literal>SELECT '\\001'::bytea;</literal></entry>
1171
<entry><literal>\001</literal></entry>
1179
The requirement to escape <quote>non-printable</quote> octets actually
1180
varies depending on locale settings. In some instances you can get away
1181
with leaving them unescaped. Note that the result in each of the examples
1182
in <xref linkend="datatype-binary-sqlesc"> was exactly one octet in
1183
length, even though the output representation of the zero octet and
1184
backslash are more than one character.
1188
The reason that you have to write so many backslashes, as shown in
1189
<xref linkend="datatype-binary-sqlesc">, is that an input string
1190
written as a string literal must pass through two parse phases in
1191
the <productname>PostgreSQL</productname> server. The first
1192
backslash of each pair is interpreted as an escape character by
1193
the string-literal parser and is therefore consumed, leaving the
1194
second backslash of the pair. The remaining backslash is then
1195
recognized by the <type>bytea</type> input function as starting
1196
either a three digit octal value or escaping another backslash.
1197
For example, a string literal passed to the server as
1198
<literal>'\\001'</literal> becomes <literal>\001</literal> after
1199
passing through the string-literal parser. The
1200
<literal>\001</literal> is then sent to the <type>bytea</type>
1201
input function, where it is converted to a single octet with a
1202
decimal value of 1. Note that the apostrophe character is not
1203
treated specially by <type>bytea</type>, so it follows the normal
1204
rules for string literals. (See also <xref
1205
linkend="sql-syntax-strings">.)
1209
<type>Bytea</type> octets are also escaped in the output. In general, each
1210
<quote>non-printable</quote> octet is converted into
1211
its equivalent three-digit octal value and preceded by one backslash.
1212
Most <quote>printable</quote> octets are represented by their standard
1213
representation in the client character set. The octet with decimal
1214
value 92 (backslash) has a special alternative output representation.
1215
Details are in <xref linkend="datatype-binary-resesc">.
1218
<table id="datatype-binary-resesc">
1219
<title><type>bytea</> Output Escaped Octets</title>
1223
<entry>Decimal Octet Value</entry>
1224
<entry>Description</entry>
1225
<entry>Escaped Output Representation</entry>
1226
<entry>Example</entry>
1227
<entry>Output Result</entry>
1235
<entry>backslash</entry>
1236
<entry><literal>\\</literal></entry>
1237
<entry><literal>SELECT '\\134'::bytea;</literal></entry>
1238
<entry><literal>\\</literal></entry>
1242
<entry>0 to 31 and 127 to 255</entry>
1243
<entry><quote>non-printable</quote> octets</entry>
1244
<entry><literal>\<replaceable>xxx</></literal> (octal value)</entry>
1245
<entry><literal>SELECT '\\001'::bytea;</literal></entry>
1246
<entry><literal>\001</literal></entry>
1250
<entry>32 to 126</entry>
1251
<entry><quote>printable</quote> octets</entry>
1252
<entry>client character set representation</entry>
1253
<entry><literal>SELECT '\\176'::bytea;</literal></entry>
1254
<entry><literal>~</literal></entry>
1262
Depending on the front end to <productname>PostgreSQL</> you use,
1263
you may have additional work to do in terms of escaping and
1264
unescaping <type>bytea</type> strings. For example, you may also
1265
have to escape line feeds and carriage returns if your interface
1266
automatically translates these.
1270
The <acronym>SQL</acronym> standard defines a different binary
1271
string type, called <type>BLOB</type> or <type>BINARY LARGE
1272
OBJECT</type>. The input format is different from
1273
<type>bytea</type>, but the provided functions and operators are
1279
<sect1 id="datatype-datetime">
1280
<title>Date/Time Types</title>
1282
<indexterm zone="datatype-datetime">
1283
<primary>date</primary>
1285
<indexterm zone="datatype-datetime">
1286
<primary>time</primary>
1288
<indexterm zone="datatype-datetime">
1289
<primary>time without time zone</primary>
1291
<indexterm zone="datatype-datetime">
1292
<primary>time with time zone</primary>
1294
<indexterm zone="datatype-datetime">
1295
<primary>timestamp</primary>
1297
<indexterm zone="datatype-datetime">
1298
<primary>timestamp with time zone</primary>
1300
<indexterm zone="datatype-datetime">
1301
<primary>timestamp without time zone</primary>
1303
<indexterm zone="datatype-datetime">
1304
<primary>interval</primary>
1306
<indexterm zone="datatype-datetime">
1307
<primary>time span</primary>
1311
<productname>PostgreSQL</productname> supports the full set of
1312
<acronym>SQL</acronym> date and time types, shown in <xref
1313
linkend="datatype-datetime-table">. The operations available
1314
on these data types are described in
1315
<xref linkend="functions-datetime">.
1318
<table id="datatype-datetime-table">
1319
<title>Date/Time Types</title>
1324
<entry>Storage Size</entry>
1325
<entry>Description</entry>
1326
<entry>Low Value</entry>
1327
<entry>High Value</entry>
1328
<entry>Resolution</entry>
1333
<entry><type>timestamp [ (<replaceable>p</replaceable>) ] [ without time zone ]</type></entry>
1334
<entry>8 bytes</entry>
1335
<entry>both date and time</entry>
1336
<entry>4713 BC</entry>
1337
<entry>5874897 AD</entry>
1338
<entry>1 microsecond / 14 digits</entry>
1341
<entry><type>timestamp [ (<replaceable>p</replaceable>) ] with time zone</type></entry>
1342
<entry>8 bytes</entry>
1343
<entry>both date and time, with time zone</entry>
1344
<entry>4713 BC</entry>
1345
<entry>5874897 AD</entry>
1346
<entry>1 microsecond / 14 digits</entry>
1349
<entry><type>interval [ (<replaceable>p</replaceable>) ]</type></entry>
1350
<entry>12 bytes</entry>
1351
<entry>time intervals</entry>
1352
<entry>-178000000 years</entry>
1353
<entry>178000000 years</entry>
1354
<entry>1 microsecond / 14 digits</entry>
1357
<entry><type>date</type></entry>
1358
<entry>4 bytes</entry>
1359
<entry>dates only</entry>
1360
<entry>4713 BC</entry>
1361
<entry>32767 AD</entry>
1362
<entry>1 day</entry>
1365
<entry><type>time [ (<replaceable>p</replaceable>) ] [ without time zone ]</type></entry>
1366
<entry>8 bytes</entry>
1367
<entry>times of day only</entry>
1368
<entry>00:00:00.00</entry>
1369
<entry>23:59:59.99</entry>
1370
<entry>1 microsecond / 14 digits</entry>
1373
<entry><type>time [ (<replaceable>p</replaceable>) ] with time zone</type></entry>
1374
<entry>12 bytes</entry>
1375
<entry>times of day only, with time zone</entry>
1376
<entry>00:00:00.00+12</entry>
1377
<entry>23:59:59.99-12</entry>
1378
<entry>1 microsecond / 14 digits</entry>
1386
Prior to <productname>PostgreSQL</productname> 7.3, writing just
1387
<type>timestamp</type> was equivalent to <type>timestamp with
1388
time zone</type>. This was changed for SQL compliance.
1393
<type>time</type>, <type>timestamp</type>, and
1394
<type>interval</type> accept an optional precision value
1395
<replaceable>p</replaceable> which specifies the number of
1396
fractional digits retained in the seconds field. By default, there
1397
is no explicit bound on precision. The allowed range of
1398
<replaceable>p</replaceable> is from 0 to 6 for the
1399
<type>timestamp</type> and <type>interval</type> types.
1404
When <type>timestamp</> values are stored as double precision floating-point
1405
numbers (currently the default), the effective limit of precision
1406
may be less than 6. <type>timestamp</type> values are stored as seconds
1407
before or after midnight 2000-01-01. Microsecond precision is achieved for
1408
dates within a few years of 2000-01-01, but the precision degrades for
1409
dates further away. When <type>timestamp</type> values are stored as
1410
eight-byte integers (a compile-time
1411
option), microsecond precision is available over the full range of
1412
values. However eight-byte integer timestamps have a more limited range of
1413
dates than shown above: from 4713 BC up to 294276 AD. The same
1414
compile-time option also determines whether <type>time</type> and
1415
<type>interval</type> values are stored as floating-point or eight-byte
1416
integers. In the floating-point case, large <type>interval</type> values
1417
degrade in precision as the size of the interval increases.
1422
For the <type>time</type> types, the allowed range of
1423
<replaceable>p</replaceable> is from 0 to 6 when eight-byte integer
1424
storage is used, or from 0 to 10 when floating-point storage is used.
1428
The type <type>time with time zone</type> is defined by the SQL
1429
standard, but the definition exhibits properties which lead to
1430
questionable usefulness. In most cases, a combination of
1431
<type>date</type>, <type>time</type>, <type>timestamp without time
1432
zone</type>, and <type>timestamp with time zone</type> should
1433
provide a complete range of date/time functionality required by
1438
The types <type>abstime</type>
1439
and <type>reltime</type> are lower precision types which are used internally.
1440
You are discouraged from using these types in new
1441
applications and are encouraged to move any old
1442
ones over when appropriate. Any or all of these internal types
1443
might disappear in a future release.
1446
<sect2 id="datatype-datetime-input">
1447
<title>Date/Time Input</title>
1450
Date and time input is accepted in almost any reasonable format, including
1451
ISO 8601, <acronym>SQL</acronym>-compatible,
1452
traditional <productname>POSTGRES</productname>, and others.
1453
For some formats, ordering of month, day, and year in date input is
1454
ambiguous and there is support for specifying the expected
1455
ordering of these fields. Set the <xref linkend="guc-datestyle"> parameter
1456
to <literal>MDY</> to select month-day-year interpretation,
1457
<literal>DMY</> to select day-month-year interpretation, or
1458
<literal>YMD</> to select year-month-day interpretation.
1462
<productname>PostgreSQL</productname> is more flexible in
1463
handling date/time input than the
1464
<acronym>SQL</acronym> standard requires.
1465
See <xref linkend="datetime-appendix">
1466
for the exact parsing rules of date/time input and for the
1467
recognized text fields including months, days of the week, and
1472
Remember that any date or time literal input needs to be enclosed
1473
in single quotes, like text strings. Refer to
1474
<xref linkend="sql-syntax-constants-generic"> for more
1476
<acronym>SQL</acronym> requires the following syntax
1478
<replaceable>type</replaceable> [ (<replaceable>p</replaceable>) ] '<replaceable>value</replaceable>'
1480
where <replaceable>p</replaceable> in the optional precision
1481
specification is an integer corresponding to the number of
1482
fractional digits in the seconds field. Precision can be
1483
specified for <type>time</type>, <type>timestamp</type>, and
1484
<type>interval</type> types. The allowed values are mentioned
1485
above. If no precision is specified in a constant specification,
1486
it defaults to the precision of the literal value.
1490
<title>Dates</title>
1493
<primary>date</primary>
1497
<xref linkend="datatype-datetime-date-table"> shows some possible
1498
inputs for the <type>date</type> type.
1501
<table id="datatype-datetime-date-table">
1502
<title>Date Input</title>
1506
<entry>Example</entry>
1507
<entry>Description</entry>
1512
<entry>January 8, 1999</entry>
1513
<entry>unambiguous in any <varname>datestyle</varname> input mode</entry>
1516
<entry>1999-01-08</entry>
1517
<entry>ISO 8601; January 8 in any mode
1518
(recommended format)</entry>
1521
<entry>1/8/1999</entry>
1522
<entry>January 8 in <literal>MDY</> mode;
1523
August 1 in <literal>DMY</> mode</entry>
1526
<entry>1/18/1999</entry>
1527
<entry>January 18 in <literal>MDY</> mode;
1528
rejected in other modes</entry>
1531
<entry>01/02/03</entry>
1532
<entry>January 2, 2003 in <literal>MDY</> mode;
1533
February 1, 2003 in <literal>DMY</> mode;
1534
February 3, 2001 in <literal>YMD</> mode
1538
<entry>1999-Jan-08</entry>
1539
<entry>January 8 in any mode</entry>
1542
<entry>Jan-08-1999</entry>
1543
<entry>January 8 in any mode</entry>
1546
<entry>08-Jan-1999</entry>
1547
<entry>January 8 in any mode</entry>
1550
<entry>99-Jan-08</entry>
1551
<entry>January 8 in <literal>YMD</> mode, else error</entry>
1554
<entry>08-Jan-99</entry>
1555
<entry>January 8, except error in <literal>YMD</> mode</entry>
1558
<entry>Jan-08-99</entry>
1559
<entry>January 8, except error in <literal>YMD</> mode</entry>
1562
<entry>19990108</entry>
1563
<entry>ISO 8601; January 8, 1999 in any mode</entry>
1566
<entry>990108</entry>
1567
<entry>ISO 8601; January 8, 1999 in any mode</entry>
1570
<entry>1999.008</entry>
1571
<entry>year and day of year</entry>
1574
<entry>J2451187</entry>
1575
<entry>Julian day</entry>
1578
<entry>January 8, 99 BC</entry>
1579
<entry>year 99 before the Common Era</entry>
1587
<title>Times</title>
1590
<primary>time</primary>
1593
<primary>time without time zone</primary>
1596
<primary>time with time zone</primary>
1600
The time-of-day types are <type>time [
1601
(<replaceable>p</replaceable>) ] without time zone</type> and
1602
<type>time [ (<replaceable>p</replaceable>) ] with time
1603
zone</type>. Writing just <type>time</type> is equivalent to
1604
<type>time without time zone</type>.
1608
Valid input for these types consists of a time of day followed
1609
by an optional time zone. (See <xref
1610
linkend="datatype-datetime-time-table">
1611
and <xref linkend="datatype-timezone-table">.) If a time zone is
1612
specified in the input for <type>time without time zone</type>,
1613
it is silently ignored.
1616
<table id="datatype-datetime-time-table">
1617
<title>Time Input</title>
1621
<entry>Example</entry>
1622
<entry>Description</entry>
1627
<entry><literal>04:05:06.789</literal></entry>
1628
<entry>ISO 8601</entry>
1631
<entry><literal>04:05:06</literal></entry>
1632
<entry>ISO 8601</entry>
1635
<entry><literal>04:05</literal></entry>
1636
<entry>ISO 8601</entry>
1639
<entry><literal>040506</literal></entry>
1640
<entry>ISO 8601</entry>
1643
<entry><literal>04:05 AM</literal></entry>
1644
<entry>same as 04:05; AM does not affect value</entry>
1647
<entry><literal>04:05 PM</literal></entry>
1648
<entry>same as 16:05; input hour must be <= 12</entry>
1651
<entry><literal>04:05:06.789-8</literal></entry>
1652
<entry>ISO 8601</entry>
1655
<entry><literal>04:05:06-08:00</literal></entry>
1656
<entry>ISO 8601</entry>
1659
<entry><literal>04:05-08:00</literal></entry>
1660
<entry>ISO 8601</entry>
1663
<entry><literal>040506-08</literal></entry>
1664
<entry>ISO 8601</entry>
1667
<entry><literal>04:05:06 PST</literal></entry>
1668
<entry>time zone specified by name</entry>
1674
<table tocentry="1" id="datatype-timezone-table">
1675
<title>Time Zone Input</title>
1679
<entry>Example</entry>
1680
<entry>Description</entry>
1685
<entry><literal>PST</literal></entry>
1686
<entry>Pacific Standard Time</entry>
1689
<entry><literal>-8:00</literal></entry>
1690
<entry>ISO-8601 offset for PST</entry>
1693
<entry><literal>-800</literal></entry>
1694
<entry>ISO-8601 offset for PST</entry>
1697
<entry><literal>-8</literal></entry>
1698
<entry>ISO-8601 offset for PST</entry>
1701
<entry><literal>zulu</literal></entry>
1702
<entry>Military abbreviation for UTC</entry>
1705
<entry><literal>z</literal></entry>
1706
<entry>Short form of <literal>zulu</literal></entry>
1713
Refer to <xref linkend="datetime-appendix"> for a list of
1714
time zone names that are recognized for input.
1719
<title>Time Stamps</title>
1722
<primary>timestamp</primary>
1726
<primary>timestamp with time zone</primary>
1730
<primary>timestamp without time zone</primary>
1734
Valid input for the time stamp types consists of a concatenation
1735
of a date and a time, followed by an optional time zone,
1736
followed by an optional <literal>AD</literal> or <literal>BC</literal>.
1737
(Alternatively, <literal>AD</literal>/<literal>BC</literal> can appear
1738
before the time zone, but this is not the preferred ordering.)
1746
1999-01-08 04:05:06 -8:00
1749
are valid values, which follow the <acronym>ISO</acronym> 8601
1750
standard. In addition, the wide-spread format
1753
January 8 04:05:06 1999 PST
1759
The <acronym>SQL</acronym> standard differentiates <type>timestamp without time zone</type>
1760
and <type>timestamp with time zone</type> literals by the existence of a
1761
<quote>+</quote>; or <quote>-</quote>. Hence, according to the standard,
1762
<programlisting>TIMESTAMP '2004-10-19 10:23:54'</programlisting>
1763
is a <type>timestamp without time zone</type>, while
1764
<programlisting>TIMESTAMP '2004-10-19 10:23:54+02'</programlisting>
1765
is a <type>timestamp with time zone</type>.
1766
<productname>PostgreSQL</productname>
1767
differs from the standard by requiring that <type>timestamp with time zone</type>
1768
literals be explicitly typed:
1769
<programlisting>TIMESTAMP WITH TIME ZONE '2004-10-19 10:23:54+02'</programlisting>
1770
If a literal is not explicitly indicated as being of <type>timestamp with time zone</type>,
1771
PostgreSQL will silently ignore any time zone indication in the literal.
1772
That is, the resulting date/time value is derived from the date/time
1773
fields in the input value, and is not adjusted for time zone.
1777
For <type>timestamp with time zone</type>, the internally stored
1778
value is always in UTC (Universal
1779
Coordinated Time, traditionally known as Greenwich Mean Time,
1780
<acronym>GMT</>). An input value that has an explicit
1781
time zone specified is converted to UTC using the appropriate offset
1782
for that time zone. If no time zone is stated in the input string,
1783
then it is assumed to be in the time zone indicated by the system's
1784
<xref linkend="guc-timezone"> parameter, and is converted to UTC using the
1785
offset for the <varname>timezone</> zone.
1789
When a <type>timestamp with time
1790
zone</type> value is output, it is always converted from UTC to the
1791
current <varname>timezone</> zone, and displayed as local time in that
1792
zone. To see the time in another time zone, either change
1793
<varname>timezone</> or use the <literal>AT TIME ZONE</> construct
1794
(see <xref linkend="functions-datetime-zoneconvert">).
1798
Conversions between <type>timestamp without time zone</type> and
1799
<type>timestamp with time zone</type> normally assume that the
1800
<type>timestamp without time zone</type> value should be taken or given
1801
as <varname>timezone</> local time. A different zone reference can
1802
be specified for the conversion using <literal>AT TIME ZONE</>.
1807
<title>Intervals</title>
1810
<primary>interval</primary>
1814
<type>interval</type> values can be written with the following syntax:
1817
<optional>@</> <replaceable>quantity</> <replaceable>unit</> <optional><replaceable>quantity</> <replaceable>unit</>...</> <optional><replaceable>direction</></optional>
1820
Where: <replaceable>quantity</> is a number (possibly signed);
1821
<replaceable>unit</> is <literal>second</literal>,
1822
<literal>minute</literal>, <literal>hour</literal>, <literal>day</literal>,
1823
<literal>week</literal>, <literal>month</literal>, <literal>year</literal>,
1824
<literal>decade</literal>, <literal>century</literal>, <literal>millennium</literal>,
1825
or abbreviations or plurals of these units;
1826
<replaceable>direction</> can be <literal>ago</literal> or
1827
empty. The at sign (<literal>@</>) is optional noise. The amounts
1828
of different units are implicitly added up with appropriate
1833
Quantities of days, hours, minutes, and seconds can be specified without
1834
explicit unit markings. For example, <literal>'1 12:59:10'</> is read
1835
the same as <literal>'1 day 12 hours 59 min 10 sec'</>.
1839
The optional precision
1840
<replaceable>p</replaceable> should be between 0 and 6, and
1841
defaults to the precision of the input literal.
1846
<title>Special Values</title>
1849
<primary>time</primary>
1850
<secondary>constants</secondary>
1854
<primary>date</primary>
1855
<secondary>constants</secondary>
1859
<productname>PostgreSQL</productname> supports several
1860
special date/time input values for convenience, as shown in <xref
1861
linkend="datatype-datetime-special-table">. The values
1862
<literal>infinity</literal> and <literal>-infinity</literal>
1863
are specially represented inside the system and will be displayed
1864
the same way; but the others are simply notational shorthands
1865
that will be converted to ordinary date/time values when read.
1866
(In particular, <literal>now</> and related strings are converted
1867
to a specific time value as soon as they are read.)
1868
All of these values need to be written in single quotes when used
1869
as constants in SQL commands.
1872
<table id="datatype-datetime-special-table">
1873
<title>Special Date/Time Inputs</title>
1877
<entry>Input String</entry>
1878
<entry>Valid Types</entry>
1879
<entry>Description</entry>
1884
<entry><literal>epoch</literal></entry>
1885
<entry><type>date</type>, <type>timestamp</type></entry>
1886
<entry>1970-01-01 00:00:00+00 (Unix system time zero)</entry>
1889
<entry><literal>infinity</literal></entry>
1890
<entry><type>timestamp</type></entry>
1891
<entry>later than all other time stamps</entry>
1894
<entry><literal>-infinity</literal></entry>
1895
<entry><type>timestamp</type></entry>
1896
<entry>earlier than all other time stamps</entry>
1899
<entry><literal>now</literal></entry>
1900
<entry><type>date</type>, <type>time</type>, <type>timestamp</type></entry>
1901
<entry>current transaction's start time</entry>
1904
<entry><literal>today</literal></entry>
1905
<entry><type>date</type>, <type>timestamp</type></entry>
1906
<entry>midnight today</entry>
1909
<entry><literal>tomorrow</literal></entry>
1910
<entry><type>date</type>, <type>timestamp</type></entry>
1911
<entry>midnight tomorrow</entry>
1914
<entry><literal>yesterday</literal></entry>
1915
<entry><type>date</type>, <type>timestamp</type></entry>
1916
<entry>midnight yesterday</entry>
1919
<entry><literal>allballs</literal></entry>
1920
<entry><type>time</type></entry>
1921
<entry>00:00:00.00 UTC</entry>
1928
The following <acronym>SQL</acronym>-compatible functions can also
1929
be used to obtain the current time value for the corresponding data
1931
<literal>CURRENT_DATE</literal>, <literal>CURRENT_TIME</literal>,
1932
<literal>CURRENT_TIMESTAMP</literal>, <literal>LOCALTIME</literal>,
1933
<literal>LOCALTIMESTAMP</literal>. The latter four accept an
1934
optional precision specification. (See <xref
1935
linkend="functions-datetime-current">.) Note however that these are
1936
SQL functions and are <emphasis>not</> recognized as data input strings.
1942
<sect2 id="datatype-datetime-output">
1943
<title>Date/Time Output</title>
1946
<primary>date</primary>
1947
<secondary>output format</secondary>
1948
<seealso>formatting</seealso>
1952
<primary>time</primary>
1953
<secondary>output format</secondary>
1954
<seealso>formatting</seealso>
1958
The output format of the date/time types can be set to one of the four
1960
<acronym>SQL</acronym> (Ingres), traditional POSTGRES, and
1961
German, using the command <literal>SET datestyle</literal>. The default
1962
is the <acronym>ISO</acronym> format. (The
1963
<acronym>SQL</acronym> standard requires the use of the ISO 8601
1964
format. The name of the <quote>SQL</quote> output format is a
1965
historical accident.) <xref
1966
linkend="datatype-datetime-output-table"> shows examples of each
1967
output style. The output of the <type>date</type> and
1968
<type>time</type> types is of course only the date or time part
1969
in accordance with the given examples.
1972
<table id="datatype-datetime-output-table">
1973
<title>Date/Time Output Styles</title>
1977
<entry>Style Specification</entry>
1978
<entry>Description</entry>
1979
<entry>Example</entry>
1985
<entry>ISO 8601/SQL standard</entry>
1986
<entry>1997-12-17 07:37:16-08</entry>
1990
<entry>traditional style</entry>
1991
<entry>12/17/1997 07:37:16.00 PST</entry>
1994
<entry>POSTGRES</entry>
1995
<entry>original style</entry>
1996
<entry>Wed Dec 17 07:37:16 1997 PST</entry>
1999
<entry>German</entry>
2000
<entry>regional style</entry>
2001
<entry>17.12.1997 07:37:16.00 PST</entry>
2008
In the <acronym>SQL</acronym> and POSTGRES styles, day appears before
2009
month if DMY field ordering has been specified, otherwise month appears
2011
(See <xref linkend="datatype-datetime-input">
2012
for how this setting also affects interpretation of input values.)
2013
<xref linkend="datatype-datetime-output2-table"> shows an
2017
<table id="datatype-datetime-output2-table">
2018
<title>Date Order Conventions</title>
2022
<entry><varname>datestyle</varname> Setting</entry>
2023
<entry>Input Ordering</entry>
2024
<entry>Example Output</entry>
2029
<entry><literal>SQL, DMY</></entry>
2030
<entry><replaceable>day</replaceable>/<replaceable>month</replaceable>/<replaceable>year</replaceable></entry>
2031
<entry>17/12/1997 15:37:16.00 CET</entry>
2034
<entry><literal>SQL, MDY</></entry>
2035
<entry><replaceable>month</replaceable>/<replaceable>day</replaceable>/<replaceable>year</replaceable></entry>
2036
<entry>12/17/1997 07:37:16.00 PST</entry>
2039
<entry><literal>Postgres, DMY</></entry>
2040
<entry><replaceable>day</replaceable>/<replaceable>month</replaceable>/<replaceable>year</replaceable></entry>
2041
<entry>Wed 17 Dec 07:37:16 1997 PST</entry>
2048
<type>interval</type> output looks like the input format, except
2049
that units like <literal>century</literal> or
2050
<literal>week</literal> are converted to years and days and
2051
<literal>ago</literal> is converted to an appropriate sign. In
2052
ISO mode the output looks like
2055
<optional> <replaceable>quantity</> <replaceable>unit</> <optional> ... </> </> <optional> <replaceable>days</> </> <optional> <replaceable>hours</>:<replaceable>minutes</>:<replaceable>seconds</> </optional>
2060
The date/time styles can be selected by the user using the
2061
<command>SET datestyle</command> command, the <xref
2062
linkend="guc-datestyle"> parameter in the
2063
<filename>postgresql.conf</filename> configuration file, or the
2064
<envar>PGDATESTYLE</envar> environment variable on the server or
2065
client. The formatting function <function>to_char</function>
2066
(see <xref linkend="functions-formatting">) is also available as
2067
a more flexible way to format the date/time output.
2071
<sect2 id="datatype-timezones">
2072
<title>Time Zones</title>
2074
<indexterm zone="datatype-timezones">
2075
<primary>time zone</primary>
2079
Time zones, and time-zone conventions, are influenced by
2080
political decisions, not just earth geometry. Time zones around the
2081
world became somewhat standardized during the 1900's,
2082
but continue to be prone to arbitrary changes, particularly with
2083
respect to daylight-savings rules.
2084
<productname>PostgreSQL</productname> currently supports daylight-savings
2085
rules over the time period 1902 through 2038 (corresponding to the full
2086
range of conventional Unix system time). Times outside that range are
2087
taken to be in <quote>standard time</> for the selected time zone, no
2088
matter what part of the year they fall in.
2092
<productname>PostgreSQL</productname> endeavors to be compatible with
2093
the <acronym>SQL</acronym> standard definitions for typical usage.
2094
However, the <acronym>SQL</acronym> standard has an odd mix of date and
2095
time types and capabilities. Two obvious problems are:
2100
Although the <type>date</type> type
2101
does not have an associated time zone, the
2102
<type>time</type> type can.
2103
Time zones in the real world have little meaning unless
2104
associated with a date as well as a time,
2105
since the offset may vary through the year with daylight-saving
2112
The default time zone is specified as a constant numeric offset
2113
from <acronym>UTC</>. It is therefore not possible to adapt to
2114
daylight-saving time when doing date/time arithmetic across
2115
<acronym>DST</acronym> boundaries.
2123
To address these difficulties, we recommend using date/time types
2124
that contain both date and time when using time zones. We
2125
recommend <emphasis>not</emphasis> using the type <type>time with
2126
time zone</type> (though it is supported by
2127
<productname>PostgreSQL</productname> for legacy applications and
2128
for compliance with the <acronym>SQL</acronym> standard).
2129
<productname>PostgreSQL</productname> assumes
2130
your local time zone for any type containing only date or time.
2134
All timezone-aware dates and times are stored internally in
2135
<acronym>UTC</acronym>. They are converted to local time
2136
in the zone specified by the <xref linkend="guc-timezone"> configuration
2137
parameter before being displayed to the client.
2141
The <xref linkend="guc-timezone"> configuration parameter can
2142
be set in the file <filename>postgresql.conf</>, or in any of the
2143
other standard ways described in <xref linkend="runtime-config">.
2144
There are also several special ways to set it:
2149
If <varname>timezone</> is not specified in
2150
<filename>postgresql.conf</> nor as a postmaster command-line switch,
2151
the server attempts to use the value of the <envar>TZ</envar>
2152
environment variable as the default time zone. If <envar>TZ</envar>
2153
is not defined or is not any of the time zone names known to
2154
<productname>PostgreSQL</productname>, the server attempts to
2155
determine the operating system's default time zone by checking the
2156
behavior of the C library function <literal>localtime()</>. The
2157
default time zone is selected as the closest match among
2158
<productname>PostgreSQL</productname>'s known time zones.
2164
The <acronym>SQL</acronym> command <command>SET TIME ZONE</command>
2165
sets the time zone for the session. This is an alternative spelling
2166
of <command>SET TIMEZONE TO</> with a more SQL-spec-compatible syntax.
2172
The <envar>PGTZ</envar> environment variable, if set at the
2173
client, is used by <application>libpq</application>
2174
applications to send a <command>SET TIME ZONE</command>
2175
command to the server upon connection.
2182
Refer to <xref linkend="datetime-appendix"> for a list of
2183
available time zones.
2188
<sect2 id="datatype-datetime-internals">
2189
<title>Internals</title>
2192
<productname>PostgreSQL</productname> uses Julian dates
2193
for all date/time calculations. They have the nice property of correctly
2194
predicting/calculating any date more recent than 4713 BC
2195
to far into the future, using the assumption that the length of the
2196
year is 365.2425 days.
2200
Date conventions before the 19th century make for interesting reading,
2201
but are not consistent enough to warrant coding into a date/time handler.
2207
<sect1 id="datatype-boolean">
2208
<title>Boolean Type</title>
2210
<indexterm zone="datatype-boolean">
2211
<primary>Boolean</primary>
2212
<secondary>data type</secondary>
2215
<indexterm zone="datatype-boolean">
2216
<primary>true</primary>
2219
<indexterm zone="datatype-boolean">
2220
<primary>false</primary>
2224
<productname>PostgreSQL</productname> provides the
2225
standard <acronym>SQL</acronym> type <type>boolean</type>.
2226
<type>boolean</type> can have one of only two states:
2227
<quote>true</quote> or <quote>false</quote>. A third state,
2228
<quote>unknown</quote>, is represented by the
2229
<acronym>SQL</acronym> null value.
2233
Valid literal values for the <quote>true</quote> state are:
2235
<member><literal>TRUE</literal></member>
2236
<member><literal>'t'</literal></member>
2237
<member><literal>'true'</literal></member>
2238
<member><literal>'y'</literal></member>
2239
<member><literal>'yes'</literal></member>
2240
<member><literal>'1'</literal></member>
2242
For the <quote>false</quote> state, the following values can be
2245
<member><literal>FALSE</literal></member>
2246
<member><literal>'f'</literal></member>
2247
<member><literal>'false'</literal></member>
2248
<member><literal>'n'</literal></member>
2249
<member><literal>'no'</literal></member>
2250
<member><literal>'0'</literal></member>
2252
Using the key words <literal>TRUE</literal> and
2253
<literal>FALSE</literal> is preferred (and
2254
<acronym>SQL</acronym>-compliant).
2257
<example id="datatype-boolean-example">
2258
<title>Using the <type>boolean</type> type</title>
2261
CREATE TABLE test1 (a boolean, b text);
2262
INSERT INTO test1 VALUES (TRUE, 'sic est');
2263
INSERT INTO test1 VALUES (FALSE, 'non est');
2264
SELECT * FROM test1;
2270
SELECT * FROM test1 WHERE a;
2278
<xref linkend="datatype-boolean-example"> shows that
2279
<type>boolean</type> values are output using the letters
2280
<literal>t</literal> and <literal>f</literal>.
2285
Values of the <type>boolean</type> type cannot be cast directly
2286
to other types (e.g., <literal>CAST
2287
(<replaceable>boolval</replaceable> AS integer)</literal> does
2288
not work). This can be accomplished using the
2289
<literal>CASE</literal> expression: <literal>CASE WHEN
2290
<replaceable>boolval</replaceable> THEN 'value if true' ELSE
2291
'value if false' END</literal>. See <xref
2292
linkend="functions-conditional">.
2297
<type>boolean</type> uses 1 byte of storage.
2301
<sect1 id="datatype-geometric">
2302
<title>Geometric Types</title>
2305
Geometric data types represent two-dimensional spatial
2306
objects. <xref linkend="datatype-geo-table"> shows the geometric
2307
types available in <productname>PostgreSQL</productname>. The
2308
most fundamental type, the point, forms the basis for all of the
2312
<table id="datatype-geo-table">
2313
<title>Geometric Types</title>
2318
<entry>Storage Size</entry>
2319
<entry>Representation</entry>
2320
<entry>Description</entry>
2325
<entry><type>point</type></entry>
2326
<entry>16 bytes</entry>
2327
<entry>Point on the plane</entry>
2328
<entry>(x,y)</entry>
2331
<entry><type>line</type></entry>
2332
<entry>32 bytes</entry>
2333
<entry>Infinite line (not fully implemented)</entry>
2334
<entry>((x1,y1),(x2,y2))</entry>
2337
<entry><type>lseg</type></entry>
2338
<entry>32 bytes</entry>
2339
<entry>Finite line segment</entry>
2340
<entry>((x1,y1),(x2,y2))</entry>
2343
<entry><type>box</type></entry>
2344
<entry>32 bytes</entry>
2345
<entry>Rectangular box</entry>
2346
<entry>((x1,y1),(x2,y2))</entry>
2349
<entry><type>path</type></entry>
2350
<entry>16+16n bytes</entry>
2351
<entry>Closed path (similar to polygon)</entry>
2352
<entry>((x1,y1),...)</entry>
2355
<entry><type>path</type></entry>
2356
<entry>16+16n bytes</entry>
2357
<entry>Open path</entry>
2358
<entry>[(x1,y1),...]</entry>
2361
<entry><type>polygon</type></entry>
2362
<entry>40+16n bytes</entry>
2363
<entry>Polygon (similar to closed path)</entry>
2364
<entry>((x1,y1),...)</entry>
2367
<entry><type>circle</type></entry>
2368
<entry>24 bytes</entry>
2369
<entry>Circle</entry>
2370
<entry><(x,y),r> (center and radius)</entry>
2377
A rich set of functions and operators is available to perform various geometric
2378
operations such as scaling, translation, rotation, and determining
2379
intersections. They are explained in <xref linkend="functions-geometry">.
2383
<title>Points</title>
2386
<primary>point</primary>
2390
Points are the fundamental two-dimensional building block for geometric types.
2391
Values of type <type>point</type> are specified using the following syntax:
2394
( <replaceable>x</replaceable> , <replaceable>y</replaceable> )
2395
<replaceable>x</replaceable> , <replaceable>y</replaceable>
2398
where <replaceable>x</> and <replaceable>y</> are the respective
2399
coordinates as floating-point numbers.
2404
<title>Line Segments</title>
2407
<primary>lseg</primary>
2411
<primary>line segment</primary>
2415
Line segments (<type>lseg</type>) are represented by pairs of points.
2416
Values of type <type>lseg</type> are specified using the following syntax:
2419
( ( <replaceable>x1</replaceable> , <replaceable>y1</replaceable> ) , ( <replaceable>x2</replaceable> , <replaceable>y2</replaceable> ) )
2420
( <replaceable>x1</replaceable> , <replaceable>y1</replaceable> ) , ( <replaceable>x2</replaceable> , <replaceable>y2</replaceable> )
2421
<replaceable>x1</replaceable> , <replaceable>y1</replaceable> , <replaceable>x2</replaceable> , <replaceable>y2</replaceable>
2425
<literal>(<replaceable>x1</replaceable>,<replaceable>y1</replaceable>)</literal>
2427
<literal>(<replaceable>x2</replaceable>,<replaceable>y2</replaceable>)</literal>
2428
are the end points of the line segment.
2433
<title>Boxes</title>
2436
<primary>box (data type)</primary>
2440
<primary>rectangle</primary>
2444
Boxes are represented by pairs of points that are opposite
2446
Values of type <type>box</type> are specified using the following syntax:
2449
( ( <replaceable>x1</replaceable> , <replaceable>y1</replaceable> ) , ( <replaceable>x2</replaceable> , <replaceable>y2</replaceable> ) )
2450
( <replaceable>x1</replaceable> , <replaceable>y1</replaceable> ) , ( <replaceable>x2</replaceable> , <replaceable>y2</replaceable> )
2451
<replaceable>x1</replaceable> , <replaceable>y1</replaceable> , <replaceable>x2</replaceable> , <replaceable>y2</replaceable>
2455
<literal>(<replaceable>x1</replaceable>,<replaceable>y1</replaceable>)</literal>
2457
<literal>(<replaceable>x2</replaceable>,<replaceable>y2</replaceable>)</literal>
2458
are any two opposite corners of the box.
2462
Boxes are output using the first syntax.
2463
The corners are reordered on input to store
2464
the upper right corner, then the lower left corner.
2465
Other corners of the box can be entered, but the lower
2466
left and upper right corners are determined from the input and stored.
2471
<title>Paths</title>
2474
<primary>path (data type)</primary>
2478
Paths are represented by lists of connected points. Paths can be
2479
<firstterm>open</firstterm>, where
2480
the first and last points in the list are not considered connected, or
2481
<firstterm>closed</firstterm>,
2482
where the first and last points are considered connected.
2486
Values of type <type>path</type> are specified using the following syntax:
2489
( ( <replaceable>x1</replaceable> , <replaceable>y1</replaceable> ) , ... , ( <replaceable>xn</replaceable> , <replaceable>yn</replaceable> ) )
2490
[ ( <replaceable>x1</replaceable> , <replaceable>y1</replaceable> ) , ... , ( <replaceable>xn</replaceable> , <replaceable>yn</replaceable> ) ]
2491
( <replaceable>x1</replaceable> , <replaceable>y1</replaceable> ) , ... , ( <replaceable>xn</replaceable> , <replaceable>yn</replaceable> )
2492
( <replaceable>x1</replaceable> , <replaceable>y1</replaceable> , ... , <replaceable>xn</replaceable> , <replaceable>yn</replaceable> )
2493
<replaceable>x1</replaceable> , <replaceable>y1</replaceable> , ... , <replaceable>xn</replaceable> , <replaceable>yn</replaceable>
2496
where the points are the end points of the line segments
2497
comprising the path. Square brackets (<literal>[]</>) indicate
2498
an open path, while parentheses (<literal>()</>) indicate a
2503
Paths are output using the first syntax.
2508
<title>Polygons</title>
2511
<primary>polygon</primary>
2515
Polygons are represented by lists of points (the vertexes of the
2516
polygon). Polygons should probably be
2517
considered equivalent to closed paths, but are stored differently
2518
and have their own set of support routines.
2522
Values of type <type>polygon</type> are specified using the following syntax:
2525
( ( <replaceable>x1</replaceable> , <replaceable>y1</replaceable> ) , ... , ( <replaceable>xn</replaceable> , <replaceable>yn</replaceable> ) )
2526
( <replaceable>x1</replaceable> , <replaceable>y1</replaceable> ) , ... , ( <replaceable>xn</replaceable> , <replaceable>yn</replaceable> )
2527
( <replaceable>x1</replaceable> , <replaceable>y1</replaceable> , ... , <replaceable>xn</replaceable> , <replaceable>yn</replaceable> )
2528
<replaceable>x1</replaceable> , <replaceable>y1</replaceable> , ... , <replaceable>xn</replaceable> , <replaceable>yn</replaceable>
2531
where the points are the end points of the line segments
2532
comprising the boundary of the polygon.
2536
Polygons are output using the first syntax.
2541
<title>Circles</title>
2544
<primary>circle</primary>
2548
Circles are represented by a center point and a radius.
2549
Values of type <type>circle</type> are specified using the following syntax:
2552
< ( <replaceable>x</replaceable> , <replaceable>y</replaceable> ) , <replaceable>r</replaceable> >
2553
( ( <replaceable>x</replaceable> , <replaceable>y</replaceable> ) , <replaceable>r</replaceable> )
2554
( <replaceable>x</replaceable> , <replaceable>y</replaceable> ) , <replaceable>r</replaceable>
2555
<replaceable>x</replaceable> , <replaceable>y</replaceable> , <replaceable>r</replaceable>
2559
<literal>(<replaceable>x</replaceable>,<replaceable>y</replaceable>)</literal>
2560
is the center and <replaceable>r</replaceable> is the radius of the circle.
2564
Circles are output using the first syntax.
2570
<sect1 id="datatype-net-types">
2571
<title>Network Address Types</title>
2573
<indexterm zone="datatype-net-types">
2574
<primary>network</primary>
2575
<secondary>data types</secondary>
2579
<productname>PostgreSQL</> offers data types to store IPv4, IPv6, and MAC
2580
addresses, as shown in <xref linkend="datatype-net-types-table">. It
2581
is preferable to use these types instead of plain text types to store
2582
network addresses, because
2583
these types offer input error checking and several specialized
2584
operators and functions (see <xref linkend="functions-net">).
2587
<table tocentry="1" id="datatype-net-types-table">
2588
<title>Network Address Types</title>
2593
<entry>Storage Size</entry>
2594
<entry>Description</entry>
2600
<entry><type>cidr</type></entry>
2601
<entry>12 or 24 bytes</entry>
2602
<entry>IPv4 and IPv6 networks</entry>
2606
<entry><type>inet</type></entry>
2607
<entry>12 or 24 bytes</entry>
2608
<entry>IPv4 and IPv6 hosts and networks</entry>
2612
<entry><type>macaddr</type></entry>
2613
<entry>6 bytes</entry>
2614
<entry>MAC addresses</entry>
2622
When sorting <type>inet</type> or <type>cidr</type> data types,
2623
IPv4 addresses will always sort before IPv6 addresses, including
2624
IPv4 addresses encapsulated or mapped into IPv6 addresses, such as
2625
::10.2.3.4 or ::ffff::10.4.3.2.
2629
<sect2 id="datatype-inet">
2630
<title><type>inet</type></title>
2633
<primary>inet (data type)</primary>
2637
The <type>inet</type> type holds an IPv4 or IPv6 host address, and
2638
optionally the identity of the subnet it is in, all in one field.
2639
The subnet identity is represented by stating how many bits of
2640
the host address represent the network address (the
2641
<quote>netmask</quote>). If the netmask is 32 and the address is IPv4,
2642
then the value does not indicate a subnet, only a single host.
2643
In IPv6, the address length is 128 bits, so 128 bits specify a
2644
unique host address. Note that if you
2645
want to accept networks only, you should use the
2646
<type>cidr</type> type rather than <type>inet</type>.
2650
The input format for this type is
2651
<replaceable class="parameter">address/y</replaceable>
2653
<replaceable class="parameter">address</replaceable>
2654
is an IPv4 or IPv6 address and
2655
<replaceable class="parameter">y</replaceable>
2656
is the number of bits in the netmask. If the
2657
<replaceable class="parameter">/y</replaceable>
2658
part is left off, then the
2659
netmask is 32 for IPv4 and 128 for IPv6, so the value represents
2660
just a single host. On display, the
2661
<replaceable class="parameter">/y</replaceable>
2662
portion is suppressed if the netmask specifies a single host.
2666
<sect2 id="datatype-cidr">
2667
<title><type>cidr</></title>
2670
<primary>cidr</primary>
2674
The <type>cidr</type> type holds an IPv4 or IPv6 network specification.
2675
Input and output formats follow Classless Internet Domain Routing
2677
The format for specifying networks is <replaceable
2678
class="parameter">address/y</> where <replaceable
2679
class="parameter">address</> is the network represented as an
2680
IPv4 or IPv6 address, and <replaceable
2681
class="parameter">y</> is the number of bits in the netmask. If
2682
<replaceable class="parameter">y</> is omitted, it is calculated
2683
using assumptions from the older classful network numbering system, except
2684
that it will be at least large enough to include all of the octets
2685
written in the input. It is an error to specify a network address
2686
that has bits set to the right of the specified netmask.
2690
<xref linkend="datatype-net-cidr-table"> shows some examples.
2693
<table id="datatype-net-cidr-table">
2694
<title><type>cidr</> Type Input Examples</title>
2698
<entry><type>cidr</type> Input</entry>
2699
<entry><type>cidr</type> Output</entry>
2700
<entry><literal><function>abbrev</function>(<type>cidr</type>)</literal></entry>
2705
<entry>192.168.100.128/25</entry>
2706
<entry>192.168.100.128/25</entry>
2707
<entry>192.168.100.128/25</entry>
2710
<entry>192.168/24</entry>
2711
<entry>192.168.0.0/24</entry>
2712
<entry>192.168.0/24</entry>
2715
<entry>192.168/25</entry>
2716
<entry>192.168.0.0/25</entry>
2717
<entry>192.168.0.0/25</entry>
2720
<entry>192.168.1</entry>
2721
<entry>192.168.1.0/24</entry>
2722
<entry>192.168.1/24</entry>
2725
<entry>192.168</entry>
2726
<entry>192.168.0.0/24</entry>
2727
<entry>192.168.0/24</entry>
2730
<entry>128.1</entry>
2731
<entry>128.1.0.0/16</entry>
2732
<entry>128.1/16</entry>
2736
<entry>128.0.0.0/16</entry>
2737
<entry>128.0/16</entry>
2740
<entry>128.1.2</entry>
2741
<entry>128.1.2.0/24</entry>
2742
<entry>128.1.2/24</entry>
2745
<entry>10.1.2</entry>
2746
<entry>10.1.2.0/24</entry>
2747
<entry>10.1.2/24</entry>
2751
<entry>10.1.0.0/16</entry>
2752
<entry>10.1/16</entry>
2756
<entry>10.0.0.0/8</entry>
2760
<entry>10.1.2.3/32</entry>
2761
<entry>10.1.2.3/32</entry>
2762
<entry>10.1.2.3/32</entry>
2765
<entry>2001:4f8:3:ba::/64</entry>
2766
<entry>2001:4f8:3:ba::/64</entry>
2767
<entry>2001:4f8:3:ba::/64</entry>
2770
<entry>2001:4f8:3:ba:2e0:81ff:fe22:d1f1/128</entry>
2771
<entry>2001:4f8:3:ba:2e0:81ff:fe22:d1f1/128</entry>
2772
<entry>2001:4f8:3:ba:2e0:81ff:fe22:d1f1</entry>
2775
<entry>::ffff:1.2.3.0/120</entry>
2776
<entry>::ffff:1.2.3.0/120</entry>
2777
<entry>::ffff:1.2.3/120</entry>
2780
<entry>::ffff:1.2.3.0/128</entry>
2781
<entry>::ffff:1.2.3.0/128</entry>
2782
<entry>::ffff:1.2.3.0/128</entry>
2789
<sect2 id="datatype-inet-vs-cidr">
2790
<title><type>inet</type> vs. <type>cidr</type></title>
2793
The essential difference between <type>inet</type> and <type>cidr</type>
2794
data types is that <type>inet</type> accepts values with nonzero bits to
2795
the right of the netmask, whereas <type>cidr</type> does not.
2800
If you do not like the output format for <type>inet</type> or
2801
<type>cidr</type> values, try the functions <function>host</>,
2802
<function>text</>, and <function>abbrev</>.
2807
<sect2 id="datatype-macaddr">
2808
<title><type>macaddr</></>
2811
<primary>macaddr (data type)</primary>
2815
<primary>MAC address</primary>
2820
The <type>macaddr</> type stores MAC addresses, i.e., Ethernet
2821
card hardware addresses (although MAC addresses are used for
2822
other purposes as well). Input is accepted in various customary
2826
<member><literal>'08002b:010203'</></member>
2827
<member><literal>'08002b-010203'</></member>
2828
<member><literal>'0800.2b01.0203'</></member>
2829
<member><literal>'08-00-2b-01-02-03'</></member>
2830
<member><literal>'08:00:2b:01:02:03'</></member>
2833
which would all specify the same
2834
address. Upper and lower case is accepted for the digits
2835
<literal>a</> through <literal>f</>. Output is always in the
2836
last of the forms shown.
2840
The directory <filename class="directory">contrib/mac</filename>
2841
in the <productname>PostgreSQL</productname> source distribution
2842
contains tools that can be used to map MAC addresses to hardware
2849
<sect1 id="datatype-bit">
2850
<title>Bit String Types</title>
2852
<indexterm zone="datatype-bit">
2853
<primary>bit string</primary>
2854
<secondary>data type</secondary>
2858
Bit strings are strings of 1's and 0's. They can be used to store
2859
or visualize bit masks. There are two SQL bit types:
2860
<type>bit(<replaceable>n</replaceable>)</type> and <type>bit
2861
varying(<replaceable>n</replaceable>)</type>, where
2862
<replaceable>n</replaceable> is a positive integer.
2866
<type>bit</type> type data must match the length
2867
<replaceable>n</replaceable> exactly; it is an error to attempt to
2868
store shorter or longer bit strings. <type>bit varying</type> data is
2869
of variable length up to the maximum length
2870
<replaceable>n</replaceable>; longer strings will be rejected.
2871
Writing <type>bit</type> without a length is equivalent to
2872
<literal>bit(1)</literal>, while <type>bit varying</type> without a length
2873
specification means unlimited length.
2878
If one explicitly casts a bit-string value to
2879
<type>bit(<replaceable>n</>)</type>, it will be truncated or
2880
zero-padded on the right to be exactly <replaceable>n</> bits,
2881
without raising an error. Similarly,
2882
if one explicitly casts a bit-string value to
2883
<type>bit varying(<replaceable>n</>)</type>, it will be truncated
2884
on the right if it is more than <replaceable>n</> bits.
2890
Prior to <productname>PostgreSQL</> 7.2, <type>bit</type> data
2891
was always silently truncated or zero-padded on the right, with
2892
or without an explicit cast. This was changed to comply with the
2893
<acronym>SQL</acronym> standard.
2899
linkend="sql-syntax-bit-strings"> for information about the syntax
2900
of bit string constants. Bit-logical operators and string
2901
manipulation functions are available; see <xref
2902
linkend="functions-bitstring">.
2906
<title>Using the bit string types</title>
2909
CREATE TABLE test (a BIT(3), b BIT VARYING(5));
2910
INSERT INTO test VALUES (B'101', B'00');
2911
INSERT INTO test VALUES (B'10', B'101');
2913
ERROR: bit string length 2 does not match type bit(3)
2915
INSERT INTO test VALUES (B'10'::bit(3), B'101');
2932
<sect1 id="datatype-oid">
2933
<title>Object Identifier Types</title>
2935
<indexterm zone="datatype-oid">
2936
<primary>object identifier</primary>
2937
<secondary>data type</secondary>
2940
<indexterm zone="datatype-oid">
2941
<primary>oid</primary>
2944
<indexterm zone="datatype-oid">
2945
<primary>regproc</primary>
2948
<indexterm zone="datatype-oid">
2949
<primary>regprocedure</primary>
2952
<indexterm zone="datatype-oid">
2953
<primary>regoper</primary>
2956
<indexterm zone="datatype-oid">
2957
<primary>regoperator</primary>
2960
<indexterm zone="datatype-oid">
2961
<primary>regclass</primary>
2964
<indexterm zone="datatype-oid">
2965
<primary>regtype</primary>
2968
<indexterm zone="datatype-oid">
2969
<primary>xid</primary>
2972
<indexterm zone="datatype-oid">
2973
<primary>cid</primary>
2976
<indexterm zone="datatype-oid">
2977
<primary>tid</primary>
2981
Object identifiers (OIDs) are used internally by
2982
<productname>PostgreSQL</productname> as primary keys for various
2983
system tables. An OID system column is also added to user-created
2984
tables, unless <literal>WITHOUT OIDS</literal> is specified when
2985
the table is created, or the <xref linkend="guc-default-with-oids">
2986
configuration variable is set to false. Type <type>oid</>
2987
represents an object identifier. There are also several alias
2988
types for <type>oid</>: <type>regproc</>, <type>regprocedure</>,
2989
<type>regoper</>, <type>regoperator</>, <type>regclass</>, and
2990
<type>regtype</>. <xref linkend="datatype-oid-table"> shows an
2995
The <type>oid</> type is currently implemented as an unsigned
2996
four-byte integer. Therefore, it is not large enough to provide
2997
database-wide uniqueness in large databases, or even in large
2998
individual tables. So, using a user-created table's OID column as
2999
a primary key is discouraged. OIDs are best used only for
3000
references to system tables.
3005
OIDs are included by default in user-created tables in
3006
<productname>PostgreSQL</productname> &version;. However, this
3007
behavior is likely to change in a future version of
3008
<productname>PostgreSQL</productname>. Eventually, user-created
3009
tables will not include an OID system column unless <literal>WITH
3010
OIDS</literal> is specified when the table is created, or the
3011
<varname>default_with_oids</varname> configuration variable is set
3012
to true. If your application requires the presence of an OID
3013
system column in a table, it should specify <literal>WITH
3014
OIDS</literal> when that table is created to ensure compatibility
3015
with future releases of <productname>PostgreSQL</productname>.
3020
The <type>oid</> type itself has few operations beyond comparison.
3022
integer, however, and then manipulated using the standard integer
3023
operators. (Beware of possible signed-versus-unsigned confusion
3028
The OID alias types have no operations of their own except
3029
for specialized input and output routines. These routines are able
3030
to accept and display symbolic names for system objects, rather than
3031
the raw numeric value that type <type>oid</> would use. The alias
3032
types allow simplified lookup of OID values for objects. For example,
3033
to examine the <structname>pg_attribute</> rows related to a table
3034
<literal>mytable</>, one could write
3036
SELECT * FROM pg_attribute WHERE attrelid = 'mytable'::regclass;
3040
SELECT * FROM pg_attribute
3041
WHERE attrelid = (SELECT oid FROM pg_class WHERE relname = 'mytable');
3043
While that doesn't look all that bad by itself, it's still oversimplified.
3044
A far more complicated sub-select would be needed to
3045
select the right OID if there are multiple tables named
3046
<literal>mytable</> in different schemas.
3047
The <type>regclass</> input converter handles the table lookup according
3048
to the schema path setting, and so it does the <quote>right thing</>
3049
automatically. Similarly, casting a table's OID to
3050
<type>regclass</> is handy for symbolic display of a numeric OID.
3053
<table id="datatype-oid-table">
3054
<title>Object Identifier Types</title>
3059
<entry>References</entry>
3060
<entry>Description</entry>
3061
<entry>Value Example</entry>
3068
<entry><type>oid</></entry>
3070
<entry>numeric object identifier</entry>
3071
<entry><literal>564182</></entry>
3075
<entry><type>regproc</></entry>
3076
<entry><structname>pg_proc</></entry>
3077
<entry>function name</entry>
3078
<entry><literal>sum</></entry>
3082
<entry><type>regprocedure</></entry>
3083
<entry><structname>pg_proc</></entry>
3084
<entry>function with argument types</entry>
3085
<entry><literal>sum(int4)</></entry>
3089
<entry><type>regoper</></entry>
3090
<entry><structname>pg_operator</></entry>
3091
<entry>operator name</entry>
3092
<entry><literal>+</></entry>
3096
<entry><type>regoperator</></entry>
3097
<entry><structname>pg_operator</></entry>
3098
<entry>operator with argument types</entry>
3099
<entry><literal>*(integer,integer)</> or <literal>-(NONE,integer)</></entry>
3103
<entry><type>regclass</></entry>
3104
<entry><structname>pg_class</></entry>
3105
<entry>relation name</entry>
3106
<entry><literal>pg_type</></entry>
3110
<entry><type>regtype</></entry>
3111
<entry><structname>pg_type</></entry>
3112
<entry>data type name</entry>
3113
<entry><literal>integer</></entry>
3120
All of the OID alias types accept schema-qualified names, and will
3121
display schema-qualified names on output if the object would not
3122
be found in the current search path without being qualified.
3123
The <type>regproc</> and <type>regoper</> alias types will only
3124
accept input names that are unique (not overloaded), so they are
3125
of limited use; for most uses <type>regprocedure</> or
3126
<type>regoperator</> is more appropriate. For <type>regoperator</>,
3127
unary operators are identified by writing <literal>NONE</> for the unused
3132
Another identifier type used by the system is <type>xid</>, or transaction
3133
(abbreviated <abbrev>xact</>) identifier. This is the data type of the system columns
3134
<structfield>xmin</> and <structfield>xmax</>. Transaction identifiers are 32-bit quantities.
3138
A third identifier type used by the system is <type>cid</>, or
3139
command identifier. This is the data type of the system columns
3140
<structfield>cmin</> and <structfield>cmax</>. Command identifiers are also 32-bit quantities.
3144
A final identifier type used by the system is <type>tid</>, or tuple
3145
identifier (row identifier). This is the data type of the system column
3146
<structfield>ctid</>. A tuple ID is a pair
3147
(block number, tuple index within block) that identifies the
3148
physical location of the row within its table.
3152
(The system columns are further explained in <xref
3153
linkend="ddl-system-columns">.)
3157
<sect1 id="datatype-pseudo">
3158
<title>Pseudo-Types</title>
3160
<indexterm zone="datatype-pseudo">
3161
<primary>record</primary>
3164
<indexterm zone="datatype-pseudo">
3165
<primary>any</primary>
3168
<indexterm zone="datatype-pseudo">
3169
<primary>anyarray</primary>
3172
<indexterm zone="datatype-pseudo">
3173
<primary>anyelement</primary>
3176
<indexterm zone="datatype-pseudo">
3177
<primary>void</primary>
3180
<indexterm zone="datatype-pseudo">
3181
<primary>trigger</primary>
3184
<indexterm zone="datatype-pseudo">
3185
<primary>language_handler</primary>
3188
<indexterm zone="datatype-pseudo">
3189
<primary>cstring</primary>
3192
<indexterm zone="datatype-pseudo">
3193
<primary>internal</primary>
3196
<indexterm zone="datatype-pseudo">
3197
<primary>opaque</primary>
3201
The <productname>PostgreSQL</productname> type system contains a
3202
number of special-purpose entries that are collectively called
3203
<firstterm>pseudo-types</>. A pseudo-type cannot be used as a
3204
column data type, but it can be used to declare a function's
3205
argument or result type. Each of the available pseudo-types is
3206
useful in situations where a function's behavior does not
3207
correspond to simply taking or returning a value of a specific
3208
<acronym>SQL</acronym> data type. <xref
3209
linkend="datatype-pseudotypes-table"> lists the existing
3213
<table id="datatype-pseudotypes-table">
3214
<title>Pseudo-Types</title>
3219
<entry>Description</entry>
3225
<entry><type>any</></entry>
3226
<entry>Indicates that a function accepts any input data type whatever.</entry>
3230
<entry><type>anyarray</></entry>
3231
<entry>Indicates that a function accepts any array data type
3232
(see <xref linkend="extend-types-polymorphic">).</entry>
3236
<entry><type>anyelement</></entry>
3237
<entry>Indicates that a function accepts any data type
3238
(see <xref linkend="extend-types-polymorphic">).</entry>
3242
<entry><type>cstring</></entry>
3243
<entry>Indicates that a function accepts or returns a null-terminated C string.</entry>
3247
<entry><type>internal</></entry>
3248
<entry>Indicates that a function accepts or returns a server-internal
3253
<entry><type>language_handler</></entry>
3254
<entry>A procedural language call handler is declared to return <type>language_handler</>.</entry>
3258
<entry><type>record</></entry>
3259
<entry>Identifies a function returning an unspecified row type.</entry>
3263
<entry><type>trigger</></entry>
3264
<entry>A trigger function is declared to return <type>trigger.</></entry>
3268
<entry><type>void</></entry>
3269
<entry>Indicates that a function returns no value.</entry>
3273
<entry><type>opaque</></entry>
3274
<entry>An obsolete type name that formerly served all the above purposes.</entry>
3281
Functions coded in C (whether built-in or dynamically loaded) may be
3282
declared to accept or return any of these pseudo data types. It is up to
3283
the function author to ensure that the function will behave safely
3284
when a pseudo-type is used as an argument type.
3288
Functions coded in procedural languages may use pseudo-types only as
3289
allowed by their implementation languages. At present the procedural
3290
languages all forbid use of a pseudo-type as argument type, and allow
3291
only <type>void</> and <type>record</> as a result type (plus
3292
<type>trigger</> when the function is used as a trigger). Some also
3293
support polymorphic functions using the types <type>anyarray</> and
3294
<type>anyelement</>.
3298
The <type>internal</> pseudo-type is used to declare functions
3299
that are meant only to be called internally by the database
3300
system, and not by direct invocation in a <acronym>SQL</acronym>
3301
query. If a function has at least one <type>internal</>-type
3302
argument then it cannot be called from <acronym>SQL</acronym>. To
3303
preserve the type safety of this restriction it is important to
3304
follow this coding rule: do not create any function that is
3305
declared to return <type>internal</> unless it has at least one
3306
<type>internal</> argument.
3313
<!-- Keep this comment at the end of the file
3318
sgml-minimize-attributes:nil
3319
sgml-always-quote-attributes:t
3321
sgml-indent-tabs-mode:nil
3323
sgml-parent-document:nil
3324
sgml-default-dtd-file:"./reference.ced"
3325
sgml-exposed-tags:nil
3326
sgml-local-catalogs:("/usr/share/sgml/catalog")
3327
sgml-local-ecat-files:nil