<?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><div xmlns="http://www.w3.org/TR/xhtml1/transitional" 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 11.4 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></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 /><col /><col /><col /></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, the scale zero or positive. 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 a column in which numeric values of any precision and scale can be stored, up to the implementation limit on precision. 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 allowed precision when explicitly specified in the type declaration is 1000; <code class="type">NUMERIC</code> without a specified precision 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. </p><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.11" class="indexterm"></a><a id="id-1.5.7.9.7.12" class="indexterm"></a><p> In addition to ordinary numeric values, the <code class="type">numeric</code> type allows the special value <code class="literal">NaN</code>, meaning <span class="quote">“<span class="quote">not-a-number</span>”</span>. Any operation on <code class="literal">NaN</code> yields another <code class="literal">NaN</code>. When writing this value as a constant in an SQL command, you must put quotes around it, for example <code class="literal">UPDATE table SET x = 'NaN'</code>. On input, the string <code class="literal">NaN</code> is recognized in a case-insensitive manner. </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. In practice, these types are usually 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 most platforms, the <code class="type">real</code> type has a range of at least 1E-37 to 1E+37 with a precision of at least 6 decimal digits. The <code class="type">double precision</code> type typically 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><div class="note"><h3 class="title">Note</h3><p> The <a class="xref" href="runtime-config-client.html#GUC-EXTRA-FLOAT-DIGITS">extra_float_digits</a> setting controls the number of extra significant digits included when a floating point value is converted to text for output. With the default value of <code class="literal">0</code>, the output is the same on every platform supported by PostgreSQL. Increasing it will produce output that more accurately represents the stored value, but may be unportable. </p></div><a id="id-1.5.7.9.8.11" 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. (On a machine whose floating-point arithmetic does not follow IEEE 754, these values will probably not work as expected.) 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. </p><div class="note"><h3 class="title">Note</h3><p> IEEE754 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 class="note"><h3 class="title">Note</h3><p> The assumption that <code class="type">real</code> and <code class="type">double precision</code> have exactly 24 and 53 bits in the mantissa respectively is correct for IEEE-standard floating point implementations. On non-IEEE platforms it might be off a little, but for simplicity the same ranges of <em class="replaceable"><code>p</code></em> are used on all platforms. </p></div></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.16. Sequence Manipulation Functions">Section 9.16</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">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="datatype.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="datatype-money.html">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">Home</a></td><td width="40%" align="right" valign="top"> 8.2. Monetary Types</td></tr></table></div></body></html>