/
datatype.sgml
5191 lines (4558 loc) · 180 KB
/
datatype.sgml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!-- doc/src/sgml/datatype.sgml -->
<chapter id="datatype">
<title>Data Types</title>
<indexterm zone="datatype">
<primary>data type</primary>
</indexterm>
<indexterm>
<primary>type</primary>
<see>data type</see>
</indexterm>
<para>
<productname>PostgreSQL</productname> has a rich set of native data
types available to users. Users can add new types to
<productname>PostgreSQL</productname> using the <xref
linkend="sql-createtype"/> command.
</para>
<para>
<xref linkend="datatype-table"/> shows all the built-in general-purpose data
types. Most of the alternative names listed in the
<quote>Aliases</quote> column are the names used internally by
<productname>PostgreSQL</productname> for historical reasons. In
addition, some internally used or deprecated types are available,
but are not listed here.
</para>
<table id="datatype-table">
<title>Data Types</title>
<tgroup cols="3">
<colspec colname="col1" colwidth="2*"/>
<colspec colname="col2" colwidth="1*"/>
<colspec colname="col3" colwidth="2*"/>
<thead>
<row>
<entry>Name</entry>
<entry>Aliases</entry>
<entry>Description</entry>
</row>
</thead>
<tbody>
<row>
<entry><type>bigint</type></entry>
<entry><type>int8</type></entry>
<entry>signed eight-byte integer</entry>
</row>
<row>
<entry><type>bigserial</type></entry>
<entry><type>serial8</type></entry>
<entry>autoincrementing eight-byte integer</entry>
</row>
<row>
<entry><type>bit [ (<replaceable>n</replaceable>) ]</type></entry>
<entry></entry>
<entry>fixed-length bit string</entry>
</row>
<row>
<entry><type>bit varying [ (<replaceable>n</replaceable>) ]</type></entry>
<entry><type>varbit [ (<replaceable>n</replaceable>) ]</type></entry>
<entry>variable-length bit string</entry>
</row>
<row>
<entry><type>boolean</type></entry>
<entry><type>bool</type></entry>
<entry>logical Boolean (true/false)</entry>
</row>
<row>
<entry><type>box</type></entry>
<entry></entry>
<entry>rectangular box on a plane</entry>
</row>
<row>
<entry><type>bytea</type></entry>
<entry></entry>
<entry>binary data (<quote>byte array</quote>)</entry>
</row>
<row>
<entry><type>character [ (<replaceable>n</replaceable>) ]</type></entry>
<entry><type>char [ (<replaceable>n</replaceable>) ]</type></entry>
<entry>fixed-length character string</entry>
</row>
<row>
<entry><type>character varying [ (<replaceable>n</replaceable>) ]</type></entry>
<entry><type>varchar [ (<replaceable>n</replaceable>) ]</type></entry>
<entry>variable-length character string</entry>
</row>
<row>
<entry><type>cidr</type></entry>
<entry></entry>
<entry>IPv4 or IPv6 network address</entry>
</row>
<row>
<entry><type>circle</type></entry>
<entry></entry>
<entry>circle on a plane</entry>
</row>
<row>
<entry><type>date</type></entry>
<entry></entry>
<entry>calendar date (year, month, day)</entry>
</row>
<row>
<entry><type>double precision</type></entry>
<entry><type>float8</type></entry>
<entry>double precision floating-point number (8 bytes)</entry>
</row>
<row>
<entry><type>inet</type></entry>
<entry></entry>
<entry>IPv4 or IPv6 host address</entry>
</row>
<row>
<entry><type>integer</type></entry>
<entry><type>int</type>, <type>int4</type></entry>
<entry>signed four-byte integer</entry>
</row>
<row>
<entry><type>interval [ <replaceable>fields</replaceable> ] [ (<replaceable>p</replaceable>) ]</type></entry>
<entry></entry>
<entry>time span</entry>
</row>
<row>
<entry><type>json</type></entry>
<entry></entry>
<entry>textual JSON data</entry>
</row>
<row>
<entry><type>jsonb</type></entry>
<entry></entry>
<entry>binary JSON data, decomposed</entry>
</row>
<row>
<entry><type>line</type></entry>
<entry></entry>
<entry>infinite line on a plane</entry>
</row>
<row>
<entry><type>lseg</type></entry>
<entry></entry>
<entry>line segment on a plane</entry>
</row>
<row>
<entry><type>macaddr</type></entry>
<entry></entry>
<entry>MAC (Media Access Control) address</entry>
</row>
<row>
<entry><type>macaddr8</type></entry>
<entry></entry>
<entry>MAC (Media Access Control) address (EUI-64 format)</entry>
</row>
<row>
<entry><type>money</type></entry>
<entry></entry>
<entry>currency amount</entry>
</row>
<row>
<entry><type>numeric [ (<replaceable>p</replaceable>,
<replaceable>s</replaceable>) ]</type></entry>
<entry><type>decimal [ (<replaceable>p</replaceable>,
<replaceable>s</replaceable>) ]</type></entry>
<entry>exact numeric of selectable precision</entry>
</row>
<row>
<entry><type>path</type></entry>
<entry></entry>
<entry>geometric path on a plane</entry>
</row>
<row>
<entry><type>pg_lsn</type></entry>
<entry></entry>
<entry><productname>PostgreSQL</productname> Log Sequence Number</entry>
</row>
<row>
<entry><type>pg_snapshot</type></entry>
<entry></entry>
<entry>user-level transaction ID snapshot</entry>
</row>
<row>
<entry><type>point</type></entry>
<entry></entry>
<entry>geometric point on a plane</entry>
</row>
<row>
<entry><type>polygon</type></entry>
<entry></entry>
<entry>closed geometric path on a plane</entry>
</row>
<row>
<entry><type>real</type></entry>
<entry><type>float4</type></entry>
<entry>single precision floating-point number (4 bytes)</entry>
</row>
<row>
<entry><type>smallint</type></entry>
<entry><type>int2</type></entry>
<entry>signed two-byte integer</entry>
</row>
<row>
<entry><type>smallserial</type></entry>
<entry><type>serial2</type></entry>
<entry>autoincrementing two-byte integer</entry>
</row>
<row>
<entry><type>serial</type></entry>
<entry><type>serial4</type></entry>
<entry>autoincrementing four-byte integer</entry>
</row>
<row>
<entry><type>text</type></entry>
<entry></entry>
<entry>variable-length character string</entry>
</row>
<row>
<entry><type>time [ (<replaceable>p</replaceable>) ] [ without time zone ]</type></entry>
<entry></entry>
<entry>time of day (no time zone)</entry>
</row>
<row>
<entry><type>time [ (<replaceable>p</replaceable>) ] with time zone</type></entry>
<entry><type>timetz</type></entry>
<entry>time of day, including time zone</entry>
</row>
<row>
<entry><type>timestamp [ (<replaceable>p</replaceable>) ] [ without time zone ]</type></entry>
<entry></entry>
<entry>date and time (no time zone)</entry>
</row>
<row>
<entry><type>timestamp [ (<replaceable>p</replaceable>) ] with time zone</type></entry>
<entry><type>timestamptz</type></entry>
<entry>date and time, including time zone</entry>
</row>
<row>
<entry><type>tsquery</type></entry>
<entry></entry>
<entry>text search query</entry>
</row>
<row>
<entry><type>tsvector</type></entry>
<entry></entry>
<entry>text search document</entry>
</row>
<row>
<entry><type>txid_snapshot</type></entry>
<entry></entry>
<entry>user-level transaction ID snapshot (deprecated; see <type>pg_snapshot</type>)</entry>
</row>
<row>
<entry><type>uuid</type></entry>
<entry></entry>
<entry>universally unique identifier</entry>
</row>
<row>
<entry><type>xml</type></entry>
<entry></entry>
<entry>XML data</entry>
</row>
</tbody>
</tgroup>
</table>
<note>
<title>Compatibility</title>
<para>
The following types (or spellings thereof) are specified by
<acronym>SQL</acronym>: <type>bigint</type>, <type>bit</type>, <type>bit
varying</type>, <type>boolean</type>, <type>char</type>,
<type>character varying</type>, <type>character</type>,
<type>varchar</type>, <type>date</type>, <type>double
precision</type>, <type>integer</type>, <type>interval</type>,
<type>numeric</type>, <type>decimal</type>, <type>real</type>,
<type>smallint</type>, <type>time</type> (with or without time zone),
<type>timestamp</type> (with or without time zone),
<type>xml</type>.
</para>
</note>
<para>
Each data type has an external representation determined by its input
and output functions. Many of the built-in types have
obvious external formats. However, several types are either unique
to <productname>PostgreSQL</productname>, such as geometric
paths, or have several possible formats, such as the date
and time types.
Some of the input and output functions are not invertible, i.e.,
the result of an output function might lose accuracy when compared to
the original input.
</para>
<sect1 id="datatype-numeric">
<title>Numeric Types</title>
<indexterm zone="datatype-numeric">
<primary>data type</primary>
<secondary>numeric</secondary>
</indexterm>
<para>
Numeric types consist of two-, four-, and eight-byte integers,
four- and eight-byte floating-point numbers, and selectable-precision
decimals. <xref linkend="datatype-numeric-table"/> lists the
available types.
</para>
<table id="datatype-numeric-table">
<title>Numeric Types</title>
<tgroup cols="4">
<colspec colname="col1" colwidth="2*"/>
<colspec colname="col2" colwidth="1*"/>
<colspec colname="col3" colwidth="2*"/>
<colspec colname="col4" colwidth="2*"/>
<thead>
<row>
<entry>Name</entry>
<entry>Storage Size</entry>
<entry>Description</entry>
<entry>Range</entry>
</row>
</thead>
<tbody>
<row>
<entry><type>smallint</type></entry>
<entry>2 bytes</entry>
<entry>small-range integer</entry>
<entry>-32768 to +32767</entry>
</row>
<row>
<entry><type>integer</type></entry>
<entry>4 bytes</entry>
<entry>typical choice for integer</entry>
<entry>-2147483648 to +2147483647</entry>
</row>
<row>
<entry><type>bigint</type></entry>
<entry>8 bytes</entry>
<entry>large-range integer</entry>
<entry>-9223372036854775808 to +9223372036854775807</entry>
</row>
<row>
<entry><type>decimal</type></entry>
<entry>variable</entry>
<entry>user-specified precision, exact</entry>
<entry>up to 131072 digits before the decimal point; up to 16383 digits after the decimal point</entry>
</row>
<row>
<entry><type>numeric</type></entry>
<entry>variable</entry>
<entry>user-specified precision, exact</entry>
<entry>up to 131072 digits before the decimal point; up to 16383 digits after the decimal point</entry>
</row>
<row>
<entry><type>real</type></entry>
<entry>4 bytes</entry>
<entry>variable-precision, inexact</entry>
<entry>6 decimal digits precision</entry>
</row>
<row>
<entry><type>double precision</type></entry>
<entry>8 bytes</entry>
<entry>variable-precision, inexact</entry>
<entry>15 decimal digits precision</entry>
</row>
<row>
<entry><type>smallserial</type></entry>
<entry>2 bytes</entry>
<entry>small autoincrementing integer</entry>
<entry>1 to 32767</entry>
</row>
<row>
<entry><type>serial</type></entry>
<entry>4 bytes</entry>
<entry>autoincrementing integer</entry>
<entry>1 to 2147483647</entry>
</row>
<row>
<entry><type>bigserial</type></entry>
<entry>8 bytes</entry>
<entry>large autoincrementing integer</entry>
<entry>1 to 9223372036854775807</entry>
</row>
</tbody>
</tgroup>
</table>
<para>
The syntax of constants for the numeric types is described in
<xref linkend="sql-syntax-constants"/>. The numeric types have a
full set of corresponding arithmetic operators and
functions. Refer to <xref linkend="functions"/> for more
information. The following sections describe the types in detail.
</para>
<sect2 id="datatype-int">
<title>Integer Types</title>
<indexterm zone="datatype-int">
<primary>integer</primary>
</indexterm>
<indexterm zone="datatype-int">
<primary>smallint</primary>
</indexterm>
<indexterm zone="datatype-int">
<primary>bigint</primary>
</indexterm>
<indexterm>
<primary>int4</primary>
<see>integer</see>
</indexterm>
<indexterm>
<primary>int2</primary>
<see>smallint</see>
</indexterm>
<indexterm>
<primary>int8</primary>
<see>bigint</see>
</indexterm>
<para>
The types <type>smallint</type>, <type>integer</type>, and
<type>bigint</type> store whole numbers, that is, numbers without
fractional components, of various ranges. Attempts to store
values outside of the allowed range will result in an error.
</para>
<para>
The type <type>integer</type> is the common choice, as it offers
the best balance between range, storage size, and performance.
The <type>smallint</type> type is generally only used if disk
space is at a premium. The <type>bigint</type> type is designed to be
used when the range of the <type>integer</type> type is insufficient.
</para>
<para>
<acronym>SQL</acronym> only specifies the integer types
<type>integer</type> (or <type>int</type>),
<type>smallint</type>, and <type>bigint</type>. The
type names <type>int2</type>, <type>int4</type>, and
<type>int8</type> are extensions, which are also used by some
other <acronym>SQL</acronym> database systems.
</para>
</sect2>
<sect2 id="datatype-numeric-decimal">
<title>Arbitrary Precision Numbers</title>
<indexterm>
<primary>numeric (data type)</primary>
</indexterm>
<indexterm>
<primary>arbitrary precision numbers</primary>
</indexterm>
<indexterm>
<primary>decimal</primary>
<see>numeric</see>
</indexterm>
<para>
The type <type>numeric</type> can store numbers with a
very large number of digits. It is especially recommended for
storing monetary amounts and other quantities where exactness is
required. Calculations with <type>numeric</type> values yield exact
results where possible, e.g., addition, subtraction, multiplication.
However, calculations on <type>numeric</type> values are very slow
compared to the integer types, or to the floating-point types
described in the next section.
</para>
<para>
We use the following terms below: The
<firstterm>precision</firstterm> of a <type>numeric</type>
is the total count of significant digits in the whole number,
that is, the number of digits to both sides of the decimal point.
The <firstterm>scale</firstterm> of a <type>numeric</type> is the
count of decimal digits in the fractional part, to the right of the
decimal point. So the number 23.5141 has a precision of 6 and a
scale of 4. Integers can be considered to have a scale of zero.
</para>
<para>
Both the maximum precision and the maximum scale of a
<type>numeric</type> column can be
configured. To declare a column of type <type>numeric</type> use
the syntax:
<programlisting>
NUMERIC(<replaceable>precision</replaceable>, <replaceable>scale</replaceable>)
</programlisting>
The precision must be positive, the scale zero or positive.
Alternatively:
<programlisting>
NUMERIC(<replaceable>precision</replaceable>)
</programlisting>
selects a scale of 0. Specifying:
<programlisting>
NUMERIC
</programlisting>
without any precision or scale creates an <quote>unconstrained
numeric</quote> column in which numeric values of any length can be
stored, up to the implementation limits. A column of this kind will
not coerce input values to any particular scale, whereas
<type>numeric</type> columns with a declared scale will coerce
input values to that scale. (The <acronym>SQL</acronym> standard
requires a default scale of 0, i.e., coercion to integer
precision. We find this a bit useless. If you're concerned
about portability, always specify the precision and scale
explicitly.)
</para>
<note>
<para>
The maximum precision that can be explicitly specified in
a <type>NUMERIC</type> type declaration is 1000. An
unconstrained <type>NUMERIC</type> column is subject to the limits
described in <xref linkend="datatype-numeric-table"/>.
</para>
</note>
<para>
If the scale of a value to be stored is greater than the declared
scale of the column, the system will round the value to the specified
number of fractional digits. Then, if the number of digits to the
left of the decimal point exceeds the declared precision minus the
declared scale, an error is raised.
</para>
<para>
Numeric values are physically stored without any extra leading or
trailing zeroes. Thus, the declared precision and scale of a column
are maximums, not fixed allocations. (In this sense the <type>numeric</type>
type is more akin to <type>varchar(<replaceable>n</replaceable>)</type>
than to <type>char(<replaceable>n</replaceable>)</type>.) The actual storage
requirement is two bytes for each group of four decimal digits,
plus three to eight bytes overhead.
</para>
<indexterm>
<primary>infinity</primary>
<secondary>numeric (data type)</secondary>
</indexterm>
<indexterm>
<primary>NaN</primary>
<see>not a number</see>
</indexterm>
<indexterm>
<primary>not a number</primary>
<secondary>numeric (data type)</secondary>
</indexterm>
<para>
In addition to ordinary numeric values, the <type>numeric</type> type
has several special values:
<literallayout>
<literal>Infinity</literal>
<literal>-Infinity</literal>
<literal>NaN</literal>
</literallayout>
These are adapted from the IEEE 754 standard, and represent
<quote>infinity</quote>, <quote>negative infinity</quote>, and
<quote>not-a-number</quote>, respectively. When writing these values
as constants in an SQL command, you must put quotes around them,
for example <literal>UPDATE table SET x = '-Infinity'</literal>.
On input, these strings are recognized in a case-insensitive manner.
The infinity values can alternatively be spelled <literal>inf</literal>
and <literal>-inf</literal>.
</para>
<para>
The infinity values behave as per mathematical expectations. For
example, <literal>Infinity</literal> plus any finite value equals
<literal>Infinity</literal>, as does <literal>Infinity</literal>
plus <literal>Infinity</literal>; but <literal>Infinity</literal>
minus <literal>Infinity</literal> yields <literal>NaN</literal> (not a
number), because it has no well-defined interpretation. Note that an
infinity can only be stored in an unconstrained <type>numeric</type>
column, because it notionally exceeds any finite precision limit.
</para>
<para>
The <literal>NaN</literal> (not a number) value is used to represent
undefined calculational results. In general, any operation with
a <literal>NaN</literal> input yields another <literal>NaN</literal>.
The only exception is when the operation's other inputs are such that
the same output would be obtained if the <literal>NaN</literal> were to
be replaced by any finite or infinite numeric value; then, that output
value is used for <literal>NaN</literal> too. (An example of this
principle is that <literal>NaN</literal> raised to the zero power
yields one.)
</para>
<note>
<para>
In most implementations of the <quote>not-a-number</quote> concept,
<literal>NaN</literal> is not considered equal to any other numeric
value (including <literal>NaN</literal>). In order to allow
<type>numeric</type> values to be sorted and used in tree-based
indexes, <productname>PostgreSQL</productname> treats <literal>NaN</literal>
values as equal, and greater than all non-<literal>NaN</literal>
values.
</para>
</note>
<para>
The types <type>decimal</type> and <type>numeric</type> are
equivalent. Both types are part of the <acronym>SQL</acronym>
standard.
</para>
<para>
When rounding values, the <type>numeric</type> type rounds ties away
from zero, while (on most machines) the <type>real</type>
and <type>double precision</type> types round ties to the nearest even
number. For example:
<programlisting>
SELECT x,
round(x::numeric) AS num_round,
round(x::double precision) AS dbl_round
FROM generate_series(-3.5, 3.5, 1) as x;
x | num_round | dbl_round
------+-----------+-----------
-3.5 | -4 | -4
-2.5 | -3 | -2
-1.5 | -2 | -2
-0.5 | -1 | -0
0.5 | 1 | 0
1.5 | 2 | 2
2.5 | 3 | 2
3.5 | 4 | 4
(8 rows)
</programlisting>
</para>
</sect2>
<sect2 id="datatype-float">
<title>Floating-Point Types</title>
<indexterm zone="datatype-float">
<primary>real</primary>
</indexterm>
<indexterm zone="datatype-float">
<primary>double precision</primary>
</indexterm>
<indexterm>
<primary>float4</primary>
<see>real</see>
</indexterm>
<indexterm>
<primary>float8</primary>
<see>double precision</see>
</indexterm>
<indexterm zone="datatype-float">
<primary>floating point</primary>
</indexterm>
<para>
The data types <type>real</type> and <type>double precision</type> are
inexact, variable-precision numeric types. On all currently supported
platforms, these types are implementations of <acronym>IEEE</acronym>
Standard 754 for Binary Floating-Point Arithmetic (single and double
precision, respectively), to the extent that the underlying processor,
operating system, and compiler support it.
</para>
<para>
Inexact means that some values cannot be converted exactly to the
internal format and are stored as approximations, so that storing
and retrieving a value might show slight discrepancies.
Managing these errors and how they propagate through calculations
is the subject of an entire branch of mathematics and computer
science and will not be discussed here, except for the
following points:
<itemizedlist>
<listitem>
<para>
If you require exact storage and calculations (such as for
monetary amounts), use the <type>numeric</type> type instead.
</para>
</listitem>
<listitem>
<para>
If you want to do complicated calculations with these types
for anything important, especially if you rely on certain
behavior in boundary cases (infinity, underflow), you should
evaluate the implementation carefully.
</para>
</listitem>
<listitem>
<para>
Comparing two floating-point values for equality might not
always work as expected.
</para>
</listitem>
</itemizedlist>
</para>
<para>
On all currently supported platforms, the <type>real</type> type has a
range of around 1E-37 to 1E+37 with a precision of at least 6 decimal
digits. The <type>double precision</type> type has a range of around
1E-307 to 1E+308 with a precision of at least 15 digits. Values that are
too large or too small will cause an error. Rounding might take place if
the precision of an input number is too high. Numbers too close to zero
that are not representable as distinct from zero will cause an underflow
error.
</para>
<para>
By default, floating point values are output in text form in their
shortest precise decimal representation; the decimal value produced is
closer to the true stored binary value than to any other value
representable in the same binary precision. (However, the output value is
currently never <emphasis>exactly</emphasis> midway between two
representable values, in order to avoid a widespread bug where input
routines do not properly respect the round-to-nearest-even rule.) This value will
use at most 17 significant decimal digits for <type>float8</type>
values, and at most 9 digits for <type>float4</type> values.
</para>
<note>
<para>
This shortest-precise output format is much faster to generate than the
historical rounded format.
</para>
</note>
<para>
For compatibility with output generated by older versions
of <productname>PostgreSQL</productname>, and to allow the output
precision to be reduced, the <xref linkend="guc-extra-float-digits"/>
parameter can be used to select rounded decimal output instead. Setting a
value of 0 restores the previous default of rounding the value to 6
(for <type>float4</type>) or 15 (for <type>float8</type>)
significant decimal digits. Setting a negative value reduces the number
of digits further; for example -2 would round output to 4 or 13 digits
respectively.
</para>
<para>
Any value of <xref linkend="guc-extra-float-digits"/> greater than 0
selects the shortest-precise format.
</para>
<note>
<para>
Applications that wanted precise values have historically had to set
<xref linkend="guc-extra-float-digits"/> to 3 to obtain them. For
maximum compatibility between versions, they should continue to do so.
</para>
</note>
<indexterm>
<primary>infinity</primary>
<secondary>floating point</secondary>
</indexterm>
<indexterm>
<primary>not a number</primary>
<secondary>floating point</secondary>
</indexterm>
<para>
In addition to ordinary numeric values, the floating-point types
have several special values:
<literallayout>
<literal>Infinity</literal>
<literal>-Infinity</literal>
<literal>NaN</literal>
</literallayout>
These represent the IEEE 754 special values
<quote>infinity</quote>, <quote>negative infinity</quote>, and
<quote>not-a-number</quote>, respectively. When writing these values
as constants in an SQL command, you must put quotes around them,
for example <literal>UPDATE table SET x = '-Infinity'</literal>. On input,
these strings are recognized in a case-insensitive manner.
The infinity values can alternatively be spelled <literal>inf</literal>
and <literal>-inf</literal>.
</para>
<note>
<para>
IEEE 754 specifies that <literal>NaN</literal> should not compare equal
to any other floating-point value (including <literal>NaN</literal>).
In order to allow floating-point values to be sorted and used
in tree-based indexes, <productname>PostgreSQL</productname> treats
<literal>NaN</literal> values as equal, and greater than all
non-<literal>NaN</literal> values.
</para>
</note>
<para>
<productname>PostgreSQL</productname> also supports the SQL-standard
notations <type>float</type> and
<type>float(<replaceable>p</replaceable>)</type> for specifying
inexact numeric types. Here, <replaceable>p</replaceable> specifies
the minimum acceptable precision in <emphasis>binary</emphasis> digits.
<productname>PostgreSQL</productname> accepts
<type>float(1)</type> to <type>float(24)</type> as selecting the
<type>real</type> type, while
<type>float(25)</type> to <type>float(53)</type> select
<type>double precision</type>. Values of <replaceable>p</replaceable>
outside the allowed range draw an error.
<type>float</type> with no precision specified is taken to mean
<type>double precision</type>.
</para>
</sect2>
<sect2 id="datatype-serial">
<title>Serial Types</title>
<indexterm zone="datatype-serial">
<primary>smallserial</primary>
</indexterm>
<indexterm zone="datatype-serial">
<primary>serial</primary>
</indexterm>
<indexterm zone="datatype-serial">
<primary>bigserial</primary>
</indexterm>
<indexterm zone="datatype-serial">
<primary>serial2</primary>
</indexterm>
<indexterm zone="datatype-serial">
<primary>serial4</primary>
</indexterm>
<indexterm zone="datatype-serial">
<primary>serial8</primary>
</indexterm>
<indexterm>
<primary>auto-increment</primary>
<see>serial</see>
</indexterm>
<indexterm>
<primary>sequence</primary>
<secondary>and serial type</secondary>
</indexterm>
<note>
<para>
This section describes a PostgreSQL-specific way to create an
autoincrementing column. Another way is to use the SQL-standard
identity column feature, described at <xref linkend="sql-createtable"/>.
</para>
</note>
<para>
The data types <type>smallserial</type>, <type>serial</type> and
<type>bigserial</type> are not true types, but merely
a notational convenience for creating unique identifier columns
(similar to the <literal>AUTO_INCREMENT</literal> property
supported by some other databases). In the current
implementation, specifying:
<programlisting>
CREATE TABLE <replaceable class="parameter">tablename</replaceable> (
<replaceable class="parameter">colname</replaceable> SERIAL
);
</programlisting>
is equivalent to specifying:
<programlisting>
CREATE SEQUENCE <replaceable class="parameter">tablename</replaceable>_<replaceable class="parameter">colname</replaceable>_seq AS integer;
CREATE TABLE <replaceable class="parameter">tablename</replaceable> (
<replaceable class="parameter">colname</replaceable> integer NOT NULL DEFAULT nextval('<replaceable class="parameter">tablename</replaceable>_<replaceable class="parameter">colname</replaceable>_seq')
);
ALTER SEQUENCE <replaceable class="parameter">tablename</replaceable>_<replaceable class="parameter">colname</replaceable>_seq OWNED BY <replaceable class="parameter">tablename</replaceable>.<replaceable class="parameter">colname</replaceable>;
</programlisting>
Thus, we have created an integer column and arranged for its default
values to be assigned from a sequence generator. A <literal>NOT NULL</literal>
constraint is applied to ensure that a null value cannot be
inserted. (In most cases you would also want to attach a
<literal>UNIQUE</literal> or <literal>PRIMARY KEY</literal> constraint to prevent
duplicate values from being inserted by accident, but this is
not automatic.) Lastly, the sequence is marked as <quote>owned by</quote>
the column, so that it will be dropped if the column or table is dropped.
</para>
<note>
<para>
Because <type>smallserial</type>, <type>serial</type> and
<type>bigserial</type> are implemented using sequences, there may
be "holes" or gaps in the sequence of values which appears in the
column, even if no rows are ever deleted. A value allocated
from the sequence is still "used up" even if a row containing that
value is never successfully inserted into the table column. This
may happen, for example, if the inserting transaction rolls back.
See <literal>nextval()</literal> in <xref linkend="functions-sequence"/>
for details.
</para>
</note>
<para>
To insert the next value of the sequence into the <type>serial</type>
column, specify that the <type>serial</type>
column should be assigned its default value. This can be done
either by excluding the column from the list of columns in
the <command>INSERT</command> statement, or through the use of
the <literal>DEFAULT</literal> key word.
</para>
<para>
The type names <type>serial</type> and <type>serial4</type> are
equivalent: both create <type>integer</type> columns. The type
names <type>bigserial</type> and <type>serial8</type> work
the same way, except that they create a <type>bigint</type>
column. <type>bigserial</type> should be used if you anticipate
the use of more than 2<superscript>31</superscript> identifiers over the
lifetime of the table. The type names <type>smallserial</type> and
<type>serial2</type> also work the same way, except that they
create a <type>smallint</type> column.
</para>
<para>
The sequence created for a <type>serial</type> column is
automatically dropped when the owning column is dropped.
You can drop the sequence without dropping the column, but this
will force removal of the column default expression.
</para>
</sect2>
</sect1>
<sect1 id="datatype-money">