summaryrefslogtreecommitdiffstats
path: root/doc/src/sgml/html/datatype-numeric.html
blob: 38dbe1f6fa49b1bc6da7bd8502b50ea4e84392fd (plain)
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
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>8.1. Numeric Types</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="datatype.html" title="Chapter 8. Data Types" /><link rel="next" href="datatype-money.html" title="8.2. Monetary Types" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">8.1. Numeric Types</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="datatype.html" title="Chapter 8. Data Types">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="datatype.html" title="Chapter 8. Data Types">Up</a></td><th width="60%" align="center">Chapter 8. Data Types</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 15.7 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="datatype-money.html" title="8.2. Monetary Types">Next</a></td></tr></table><hr /></div><div class="sect1" id="DATATYPE-NUMERIC"><div class="titlepage"><div><div><h2 class="title" style="clear: both">8.1. Numeric Types</h2></div></div></div><div class="toc"><dl class="toc"><dt><span class="sect2"><a href="datatype-numeric.html#DATATYPE-INT">8.1.1. Integer Types</a></span></dt><dt><span class="sect2"><a href="datatype-numeric.html#DATATYPE-NUMERIC-DECIMAL">8.1.2. Arbitrary Precision Numbers</a></span></dt><dt><span class="sect2"><a href="datatype-numeric.html#DATATYPE-FLOAT">8.1.3. Floating-Point Types</a></span></dt><dt><span class="sect2"><a href="datatype-numeric.html#DATATYPE-SERIAL">8.1.4. Serial Types</a></span></dt></dl></div><a id="id-1.5.7.9.2" class="indexterm"></a><p>
    Numeric types consist of two-, four-, and eight-byte integers,
    four- and eight-byte floating-point numbers, and selectable-precision
    decimals.  <a class="xref" href="datatype-numeric.html#DATATYPE-NUMERIC-TABLE" title="Table 8.2. Numeric Types">Table 8.2</a> lists the
    available types.
   </p><div class="table" id="DATATYPE-NUMERIC-TABLE"><p class="title"><strong>Table 8.2. Numeric Types</strong></p><div class="table-contents"><table class="table" summary="Numeric Types" border="1"><colgroup><col class="col1" /><col class="col2" /><col class="col3" /><col class="col4" /></colgroup><thead><tr><th>Name</th><th>Storage Size</th><th>Description</th><th>Range</th></tr></thead><tbody><tr><td><code class="type">smallint</code></td><td>2 bytes</td><td>small-range integer</td><td>-32768 to +32767</td></tr><tr><td><code class="type">integer</code></td><td>4 bytes</td><td>typical choice for integer</td><td>-2147483648 to +2147483647</td></tr><tr><td><code class="type">bigint</code></td><td>8 bytes</td><td>large-range integer</td><td>-9223372036854775808 to +9223372036854775807</td></tr><tr><td><code class="type">decimal</code></td><td>variable</td><td>user-specified precision, exact</td><td>up to 131072 digits before the decimal point; up to 16383 digits after the decimal point</td></tr><tr><td><code class="type">numeric</code></td><td>variable</td><td>user-specified precision, exact</td><td>up to 131072 digits before the decimal point; up to 16383 digits after the decimal point</td></tr><tr><td><code class="type">real</code></td><td>4 bytes</td><td>variable-precision, inexact</td><td>6 decimal digits precision</td></tr><tr><td><code class="type">double precision</code></td><td>8 bytes</td><td>variable-precision, inexact</td><td>15 decimal digits precision</td></tr><tr><td><code class="type">smallserial</code></td><td>2 bytes</td><td>small autoincrementing integer</td><td>1 to 32767</td></tr><tr><td><code class="type">serial</code></td><td>4 bytes</td><td>autoincrementing integer</td><td>1 to 2147483647</td></tr><tr><td><code class="type">bigserial</code></td><td>8 bytes</td><td>large autoincrementing integer</td><td>1 to 9223372036854775807</td></tr></tbody></table></div></div><br class="table-break" /><p>
    The syntax of constants for the numeric types is described in
    <a class="xref" href="sql-syntax-lexical.html#SQL-SYNTAX-CONSTANTS" title="4.1.2. Constants">Section 4.1.2</a>.  The numeric types have a
    full set of corresponding arithmetic operators and
    functions. Refer to <a class="xref" href="functions.html" title="Chapter 9. Functions and Operators">Chapter 9</a> for more
    information.  The following sections describe the types in detail.
   </p><div class="sect2" id="DATATYPE-INT"><div class="titlepage"><div><div><h3 class="title">8.1.1. Integer Types</h3></div></div></div><a id="id-1.5.7.9.6.2" class="indexterm"></a><a id="id-1.5.7.9.6.3" class="indexterm"></a><a id="id-1.5.7.9.6.4" class="indexterm"></a><a id="id-1.5.7.9.6.5" class="indexterm"></a><a id="id-1.5.7.9.6.6" class="indexterm"></a><a id="id-1.5.7.9.6.7" class="indexterm"></a><p>
     The types <code class="type">smallint</code>, <code class="type">integer</code>, and
     <code class="type">bigint</code> 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.
    </p><p>
     The type <code class="type">integer</code> is the common choice, as it offers
     the best balance between range, storage size, and performance.
     The <code class="type">smallint</code> type is generally only used if disk
     space is at a premium.  The <code class="type">bigint</code> type is designed to be
     used when the range of the <code class="type">integer</code> type is insufficient.
    </p><p>
     <acronym class="acronym">SQL</acronym> only specifies the integer types
     <code class="type">integer</code> (or <code class="type">int</code>),
     <code class="type">smallint</code>, and <code class="type">bigint</code>.  The
     type names <code class="type">int2</code>, <code class="type">int4</code>, and
     <code class="type">int8</code> are extensions, which are also used by some
     other <acronym class="acronym">SQL</acronym> database systems.
    </p></div><div class="sect2" id="DATATYPE-NUMERIC-DECIMAL"><div class="titlepage"><div><div><h3 class="title">8.1.2. Arbitrary Precision Numbers</h3></div></div></div><a id="id-1.5.7.9.7.2" class="indexterm"></a><a id="id-1.5.7.9.7.3" class="indexterm"></a><a id="id-1.5.7.9.7.4" class="indexterm"></a><p>
     The type <code class="type">numeric</code> 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 <code class="type">numeric</code> values yield exact
     results where possible, e.g.,  addition, subtraction, multiplication.
     However, calculations on <code class="type">numeric</code> values are very slow
     compared to the integer types, or to the floating-point types
     described in the next section.
    </p><p>
     We use the following terms below:  The
     <em class="firstterm">precision</em> of a <code class="type">numeric</code>
     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 <em class="firstterm">scale</em> of a <code class="type">numeric</code> 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.
    </p><p>
     Both the maximum precision and the maximum scale of a
     <code class="type">numeric</code> column can be
     configured.  To declare a column of type <code class="type">numeric</code> use
     the syntax:
</p><pre class="programlisting">
NUMERIC(<em class="replaceable"><code>precision</code></em>, <em class="replaceable"><code>scale</code></em>)
</pre><p>
     The precision must be positive, while the scale may be positive or
     negative (see below).  Alternatively:
</p><pre class="programlisting">
NUMERIC(<em class="replaceable"><code>precision</code></em>)
</pre><p>
     selects a scale of 0.  Specifying:
</p><pre class="programlisting">
NUMERIC
</pre><p>
     without any precision or scale creates an <span class="quote"><span class="quote">unconstrained
     numeric</span></span> 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
     <code class="type">numeric</code> columns with a declared scale will coerce
     input values to that scale.  (The <acronym class="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.)
    </p><div class="note"><h3 class="title">Note</h3><p>
      The maximum precision that can be explicitly specified in
      a <code class="type">numeric</code> type declaration is 1000.  An
      unconstrained <code class="type">numeric</code> column is subject to the limits
      described in <a class="xref" href="datatype-numeric.html#DATATYPE-NUMERIC-TABLE" title="Table 8.2. Numeric Types">Table 8.2</a>.
     </p></div><p>
     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.
     For example, a column declared as
</p><pre class="programlisting">
NUMERIC(3, 1)
</pre><p>
     will round values to 1 decimal place and can store values between
     -99.9 and 99.9, inclusive.
    </p><p>
     Beginning in <span class="productname">PostgreSQL</span> 15, it is allowed
     to declare a <code class="type">numeric</code> column with a negative scale.  Then
     values will be rounded to the left of the decimal point.  The
     precision still represents the maximum number of non-rounded digits.
     Thus, a column declared as
</p><pre class="programlisting">
NUMERIC(2, -3)
</pre><p>
     will round values to the nearest thousand and can store values
     between -99000 and 99000, inclusive.
     It is also allowed to declare a scale larger than the declared
     precision.  Such a column can only hold fractional values, and it
     requires the number of zero digits just to the right of the decimal
     point to be at least the declared scale minus the declared precision.
     For example, a column declared as
</p><pre class="programlisting">
NUMERIC(3, 5)
</pre><p>
     will round values to 5 decimal places and can store values between
     -0.00999 and 0.00999, inclusive.
    </p><div class="note"><h3 class="title">Note</h3><p>
      <span class="productname">PostgreSQL</span> permits the scale in a
      <code class="type">numeric</code> type declaration to be any value in the range
      -1000 to 1000.  However, the <acronym class="acronym">SQL</acronym> standard requires
      the scale to be in the range 0 to <em class="replaceable"><code>precision</code></em>.
      Using scales outside that range may not be portable to other database
      systems.
     </p></div><p>
     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 <code class="type">numeric</code>
     type is more akin to <code class="type">varchar(<em class="replaceable"><code>n</code></em>)</code>
     than to <code class="type">char(<em class="replaceable"><code>n</code></em>)</code>.)  The actual storage
     requirement is two bytes for each group of four decimal digits,
     plus three to eight bytes overhead.
    </p><a id="id-1.5.7.9.7.13" class="indexterm"></a><a id="id-1.5.7.9.7.14" class="indexterm"></a><a id="id-1.5.7.9.7.15" class="indexterm"></a><p>
     In addition to ordinary numeric values, the <code class="type">numeric</code> type
     has several special values:
</p><div class="literallayout"><p><br />
<code class="literal">Infinity</code><br />
<code class="literal">-Infinity</code><br />
<code class="literal">NaN</code><br />
</p></div><p>
     These are adapted from the IEEE 754 standard, and represent
     <span class="quote"><span class="quote">infinity</span></span>, <span class="quote"><span class="quote">negative infinity</span></span>, and
     <span class="quote"><span class="quote">not-a-number</span></span>, respectively. When writing these values
     as constants in an SQL command, you must put quotes around them,
     for example <code class="literal">UPDATE table SET x = '-Infinity'</code>.
     On input, these strings are recognized in a case-insensitive manner.
     The infinity values can alternatively be spelled <code class="literal">inf</code>
     and <code class="literal">-inf</code>.
    </p><p>
     The infinity values behave as per mathematical expectations.  For
     example, <code class="literal">Infinity</code> plus any finite value equals
     <code class="literal">Infinity</code>, as does <code class="literal">Infinity</code>
     plus <code class="literal">Infinity</code>; but <code class="literal">Infinity</code>
     minus <code class="literal">Infinity</code> yields <code class="literal">NaN</code> (not a
     number), because it has no well-defined interpretation.  Note that an
     infinity can only be stored in an unconstrained <code class="type">numeric</code>
     column, because it notionally exceeds any finite precision limit.
    </p><p>
     The <code class="literal">NaN</code> (not a number) value is used to represent
     undefined calculational results.  In general, any operation with
     a <code class="literal">NaN</code> input yields another <code class="literal">NaN</code>.
     The only exception is when the operation's other inputs are such that
     the same output would be obtained if the <code class="literal">NaN</code> were to
     be replaced by any finite or infinite numeric value; then, that output
     value is used for <code class="literal">NaN</code> too.  (An example of this
     principle is that <code class="literal">NaN</code> raised to the zero power
     yields one.)
    </p><div class="note"><h3 class="title">Note</h3><p>
      In most implementations of the <span class="quote"><span class="quote">not-a-number</span></span> concept,
      <code class="literal">NaN</code> is not considered equal to any other numeric
      value (including <code class="literal">NaN</code>).  In order to allow
      <code class="type">numeric</code> values to be sorted and used in tree-based
      indexes, <span class="productname">PostgreSQL</span> treats <code class="literal">NaN</code>
      values as equal, and greater than all non-<code class="literal">NaN</code>
      values.
     </p></div><p>
     The types <code class="type">decimal</code> and <code class="type">numeric</code> are
     equivalent.  Both types are part of the <acronym class="acronym">SQL</acronym>
     standard.
    </p><p>
     When rounding values, the <code class="type">numeric</code> type rounds ties away
     from zero, while (on most machines) the <code class="type">real</code>
     and <code class="type">double precision</code> types round ties to the nearest even
     number.  For example:

</p><pre class="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)
</pre><p>
    </p></div><div class="sect2" id="DATATYPE-FLOAT"><div class="titlepage"><div><div><h3 class="title">8.1.3. Floating-Point Types</h3></div></div></div><a id="id-1.5.7.9.8.2" class="indexterm"></a><a id="id-1.5.7.9.8.3" class="indexterm"></a><a id="id-1.5.7.9.8.4" class="indexterm"></a><a id="id-1.5.7.9.8.5" class="indexterm"></a><a id="id-1.5.7.9.8.6" class="indexterm"></a><p>
     The data types <code class="type">real</code> and <code class="type">double precision</code> are
     inexact, variable-precision numeric types. On all currently supported
     platforms, these types are implementations of <acronym class="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.
    </p><p>
     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:
     </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
        If you require exact storage and calculations (such as for
        monetary amounts), use the <code class="type">numeric</code> type instead.
       </p></li><li class="listitem"><p>
        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.
       </p></li><li class="listitem"><p>
        Comparing two floating-point values for equality might not
        always work as expected.
       </p></li></ul></div><p>
    </p><p>
     On all currently supported platforms, the <code class="type">real</code> type has a
     range of around 1E-37 to 1E+37 with a precision of at least 6 decimal
     digits. The <code class="type">double precision</code> 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.
    </p><p>
     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 <span class="emphasis"><em>exactly</em></span> 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 <code class="type">float8</code>
     values, and at most 9 digits for <code class="type">float4</code> values.
    </p><div class="note"><h3 class="title">Note</h3><p>
      This shortest-precise output format is much faster to generate than the
      historical rounded format.
     </p></div><p>
     For compatibility with output generated by older versions
     of <span class="productname">PostgreSQL</span>, and to allow the output
     precision to be reduced, the <a class="xref" href="runtime-config-client.html#GUC-EXTRA-FLOAT-DIGITS">extra_float_digits</a>
     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 <code class="type">float4</code>) or 15 (for <code class="type">float8</code>)
     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.
    </p><p>
     Any value of <a class="xref" href="runtime-config-client.html#GUC-EXTRA-FLOAT-DIGITS">extra_float_digits</a> greater than 0
     selects the shortest-precise format.
    </p><div class="note"><h3 class="title">Note</h3><p>
      Applications that wanted precise values have historically had to set
      <a class="xref" href="runtime-config-client.html#GUC-EXTRA-FLOAT-DIGITS">extra_float_digits</a> to 3 to obtain them. For
      maximum compatibility between versions, they should continue to do so.
     </p></div><a id="id-1.5.7.9.8.15" class="indexterm"></a><a id="id-1.5.7.9.8.16" class="indexterm"></a><p>
     In addition to ordinary numeric values, the floating-point types
     have several special values:
</p><div class="literallayout"><p><br />
<code class="literal">Infinity</code><br />
<code class="literal">-Infinity</code><br />
<code class="literal">NaN</code><br />
</p></div><p>
     These represent the IEEE 754 special values
     <span class="quote"><span class="quote">infinity</span></span>, <span class="quote"><span class="quote">negative infinity</span></span>, and
     <span class="quote"><span class="quote">not-a-number</span></span>, respectively. When writing these values
     as constants in an SQL command, you must put quotes around them,
     for example <code class="literal">UPDATE table SET x = '-Infinity'</code>.  On input,
     these strings are recognized in a case-insensitive manner.
     The infinity values can alternatively be spelled <code class="literal">inf</code>
     and <code class="literal">-inf</code>.
    </p><div class="note"><h3 class="title">Note</h3><p>
      IEEE 754 specifies that <code class="literal">NaN</code> should not compare equal
      to any other floating-point value (including <code class="literal">NaN</code>).
      In order to allow floating-point values to be sorted and used
      in tree-based indexes, <span class="productname">PostgreSQL</span> treats
      <code class="literal">NaN</code> values as equal, and greater than all
      non-<code class="literal">NaN</code> values.
     </p></div><p>
     <span class="productname">PostgreSQL</span> also supports the SQL-standard
     notations <code class="type">float</code> and
     <code class="type">float(<em class="replaceable"><code>p</code></em>)</code> for specifying
     inexact numeric types.  Here, <em class="replaceable"><code>p</code></em> specifies
     the minimum acceptable precision in <span class="emphasis"><em>binary</em></span> digits.
     <span class="productname">PostgreSQL</span> accepts
     <code class="type">float(1)</code> to <code class="type">float(24)</code> as selecting the
     <code class="type">real</code> type, while
     <code class="type">float(25)</code> to <code class="type">float(53)</code> select
     <code class="type">double precision</code>.  Values of <em class="replaceable"><code>p</code></em>
     outside the allowed range draw an error.
     <code class="type">float</code> with no precision specified is taken to mean
     <code class="type">double precision</code>.
    </p></div><div class="sect2" id="DATATYPE-SERIAL"><div class="titlepage"><div><div><h3 class="title">8.1.4. Serial Types</h3></div></div></div><a id="id-1.5.7.9.9.2" class="indexterm"></a><a id="id-1.5.7.9.9.3" class="indexterm"></a><a id="id-1.5.7.9.9.4" class="indexterm"></a><a id="id-1.5.7.9.9.5" class="indexterm"></a><a id="id-1.5.7.9.9.6" class="indexterm"></a><a id="id-1.5.7.9.9.7" class="indexterm"></a><a id="id-1.5.7.9.9.8" class="indexterm"></a><a id="id-1.5.7.9.9.9" class="indexterm"></a><div class="note"><h3 class="title">Note</h3><p>
      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 <a class="xref" href="sql-createtable.html" title="CREATE TABLE"><span class="refentrytitle">CREATE TABLE</span></a>.
     </p></div><p>
     The data types <code class="type">smallserial</code>, <code class="type">serial</code> and
     <code class="type">bigserial</code> are not true types, but merely
     a notational convenience for creating unique identifier columns
     (similar to the <code class="literal">AUTO_INCREMENT</code> property
     supported by some other databases). In the current
     implementation, specifying:

</p><pre class="programlisting">
CREATE TABLE <em class="replaceable"><code>tablename</code></em> (
    <em class="replaceable"><code>colname</code></em> SERIAL
);
</pre><p>

     is equivalent to specifying:

</p><pre class="programlisting">
CREATE SEQUENCE <em class="replaceable"><code>tablename</code></em>_<em class="replaceable"><code>colname</code></em>_seq AS integer;
CREATE TABLE <em class="replaceable"><code>tablename</code></em> (
    <em class="replaceable"><code>colname</code></em> integer NOT NULL DEFAULT nextval('<em class="replaceable"><code>tablename</code></em>_<em class="replaceable"><code>colname</code></em>_seq')
);
ALTER SEQUENCE <em class="replaceable"><code>tablename</code></em>_<em class="replaceable"><code>colname</code></em>_seq OWNED BY <em class="replaceable"><code>tablename</code></em>.<em class="replaceable"><code>colname</code></em>;
</pre><p>

     Thus, we have created an integer column and arranged for its default
     values to be assigned from a sequence generator.  A <code class="literal">NOT NULL</code>
     constraint is applied to ensure that a null value cannot be
     inserted.  (In most cases you would also want to attach a
     <code class="literal">UNIQUE</code> or <code class="literal">PRIMARY KEY</code> constraint to prevent
     duplicate values from being inserted by accident, but this is
     not automatic.)  Lastly, the sequence is marked as <span class="quote"><span class="quote">owned by</span></span>
     the column, so that it will be dropped if the column or table is dropped.
    </p><div class="note"><h3 class="title">Note</h3><p>
        Because <code class="type">smallserial</code>, <code class="type">serial</code> and
        <code class="type">bigserial</code> 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 <code class="literal">nextval()</code> in <a class="xref" href="functions-sequence.html" title="9.17. Sequence Manipulation Functions">Section 9.17</a>
        for details.
      </p></div><p>
     To insert the next value of the sequence into the <code class="type">serial</code>
     column, specify that the <code class="type">serial</code>
     column should be assigned its default value. This can be done
     either by excluding the column from the list of columns in
     the <code class="command">INSERT</code> statement, or through the use of
     the <code class="literal">DEFAULT</code> key word.
    </p><p>
     The type names <code class="type">serial</code> and <code class="type">serial4</code> are
     equivalent: both create <code class="type">integer</code> columns.  The type
     names <code class="type">bigserial</code> and <code class="type">serial8</code> work
     the same way, except that they create a <code class="type">bigint</code>
     column.  <code class="type">bigserial</code> should be used if you anticipate
     the use of more than 2<sup>31</sup> identifiers over the
     lifetime of the table. The type names <code class="type">smallserial</code> and
     <code class="type">serial2</code> also work the same way, except that they
     create a <code class="type">smallint</code> column.
    </p><p>
     The sequence created for a <code class="type">serial</code> 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.
    </p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="datatype.html" title="Chapter 8. Data Types">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="datatype.html" title="Chapter 8. Data Types">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="datatype-money.html" title="8.2. Monetary Types">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter 8. Data Types </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 15.7 Documentation">Home</a></td><td width="40%" align="right" valign="top"> 8.2. Monetary Types</td></tr></table></div></body></html>