PostgreSQL 8.1.4 Documentation
Prev | Fast Backward |  | Fast Forward | Next

# Chapter 8. Data Types

PostgreSQL has a rich set of native data types available to users. Users may add new types to PostgreSQL using the CREATE TYPE command.

Table 8-1 shows all the built-in general-purpose data types. Most of the alternative names listed in the "Aliases" column are the names used internally by PostgreSQL for historical reasons. In addition, some internally used or deprecated types are available, but they are not listed here.

Table 8-1. Data Types

Name | Aliases | Description
--- | --- | ---
`bigint` | `int8` | signed eight-byte integer
`bigserial` | `serial8` | autoincrementing eight-byte integer
`bit [ (`n`) ]` |  | fixed-length bit string
`bit varying [ (`n`) ]` | `varbit` | variable-length bit string
`boolean` | `bool` | logical Boolean (true/false)
`box` |  | rectangular box in the plane
`bytea` |  | binary data ("byte array")
`character varying [ (`n`) ]` | `varchar [ (`n`) ]` | variable-length character string
`character [ (`n`) ]` | `char [ (`n`) ]` | fixed-length character string
`cidr` |  | IPv4 or IPv6 network address
`circle` |  | circle in the plane
`date` |  | calendar date (year, month, day)
`double precision` | `float8` | double precision floating-point number
`inet` |  | IPv4 or IPv6 host address
`integer` | `int`, `int4` | signed four-byte integer
`interval [ (`p`) ]` |  | time span
`line` |  | infinite line in the plane
`lseg` |  | line segment in the plane
`macaddr` |  | MAC address
`money` |  | currency amount
`numeric [ (`p`, `s`) ]` | `decimal [ (`p`, `s`) ]` | exact numeric of selectable precision
`path` |  | geometric path in the plane
`point` |  | geometric point in the plane
`polygon` |  | closed geometric path in the plane
`real` | `float4` | single precision floating-point number
`smallint` | `int2` | signed two-byte integer
`serial` | `serial4` | autoincrementing four-byte integer
`text` |  | variable-length character string
`time [ (`p`) ] [ without time zone ]` |  | time of day
`time [ (`p`) ] with time zone` | `timetz` | time of day, including time zone
`timestamp [ (`p`) ] [ without time zone ]` |  | date and time
`timestamp [ (`p`) ] with time zone` | `timestamptz` | date and time, including time zone

> Compatibility: The following types (or spellings thereof) are specified by SQL: `bit`, `bit varying`, `boolean`, `char`, `character varying`, `character`, `varchar`, `date`, `double precision`, `integer`, `interval`, `numeric`, `decimal`, `real`, `smallint`, `time` (with or without time zone), `timestamp` (with or without time zone).

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 PostgreSQL, such as geometric paths, or have several possibilities for formats, such as the date and time types. Some of the input and output functions are not invertible. That is, the result of an output function may lose accuracy when compared to the original input.

# 8.1. Numeric Types

Numeric types consist of two-, four-, and eight-byte integers, four- and eight-byte floating-point numbers, and selectable-precision decimals. Table 8-2 lists the available types.

Table 8-2. Numeric Types

Name | Storage Size | Description | Range
--- | --- | --- | ---
`smallint` | 2 bytes | small-range integer | -32768 to +32767
`integer` | 4 bytes | usual choice for integer | -2147483648 to +2147483647
`bigint` | 8 bytes | large-range integer | -9223372036854775808 to 9223372036854775807
`decimal` | variable | user-specified precision, exact | no limit
`numeric` | variable | user-specified precision, exact | no limit
`real` | 4 bytes | variable-precision, inexact | 6 decimal digits precision
`double precision` | 8 bytes | variable-precision, inexact | 15 decimal digits precision
`serial` | 4 bytes | autoincrementing integer | 1 to 2147483647
`bigserial` | 8 bytes | large autoincrementing integer | 1 to 9223372036854775807

The syntax of constants for the numeric types is described in Section 4.1.2. The numeric types have a full set of corresponding arithmetic operators and functions. Refer to Chapter 9 for more information. The following sections describe the types in detail.

## 8.1.1. Integer Types

The types `smallint`, `integer`, and `bigint` 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.

The type `integer` is the usual choice, as it offers the best balance between range, storage size, and performance. The `smallint` type is generally only used if disk space is at a premium. The `bigint` type should only be used if the `integer` range is not sufficient, because the latter is definitely faster.

The `bigint` type may not function correctly on all platforms, since it relies on compiler support for eight-byte integers. On a machine without such support, `bigint` acts the same as `integer` (but still takes up eight bytes of storage). However, we are not aware of any reasonable platform where this is actually the case.

SQL only specifies the integer types `integer` (or `int`) and `smallint`. The type `bigint`, and the type names `int2`, `int4`, and `int8` are extensions, which are shared with various other SQL database systems.

## 8.1.2. Arbitrary Precision Numbers

The type `numeric` can store numbers with up to 1000 digits of precision and perform calculations exactly. It is especially recommended for storing monetary amounts and other quantities where exactness is required. However, arithmetic on `numeric` values is very slow compared to the integer types, or to the floating-point types described in the next section.

In what follows we use these terms: The scale of a `numeric` is the count of decimal digits in the fractional part, to the right of the decimal point. The precision of a `numeric` is the total count of significant digits in the whole number, that is, the number of digits to both sides 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.

Both the maximum precision and the maximum scale of a `numeric` column can be configured. To declare a column of type `numeric` use the syntax

    NUMERIC(precision, scale)

The precision must be positive, the scale zero or positive. Alternatively,

    NUMERIC(precision)

selects a scale of 0. Specifying

    NUMERIC

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 `numeric` columns with a declared scale will coerce input values to that scale. (The SQL 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.)

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.

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 `numeric` type is more akin to `varchar(`n`)` than to `char(`n`)`.) The actual storage requirement is two bytes for each group of four decimal digits, plus eight bytes overhead.

In addition to ordinary numeric values, the `numeric` type allows the special value `NaN`, meaning "not-a-number". Any operation on `NaN` yields another `NaN`. When writing this value as a constant in a SQL command, you must put quotes around it, for example `UPDATE table SET x = 'NaN'`. On input, the string `NaN` is recognized in a case-insensitive manner.

The types `decimal` and `numeric` are equivalent. Both types are part of the SQL standard.

## 8.1.3. Floating-Point Types

The data types `real` and `double precision` are inexact, variable-precision numeric types. In practice, these types are usually implementations of IEEE 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.

Inexact means that some values cannot be converted exactly to the internal format and are stored as approximations, so that storing and printing back out a value may 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 further here, except for the following points:

- If you require exact storage and calculations (such as for monetary amounts), use the `numeric` type instead.

- 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.

- Comparing two floating-point values for equality may or may not work as expected.

On most platforms, the `real` type has a range of at least 1E-37 to 1E+37 with a precision of at least 6 decimal digits. The `double precision` 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 may 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.

In addition to ordinary numeric values, the floating-point types have several special values:

`Infinity`
`-Infinity`
`NaN`

These represent the IEEE 754 special values "infinity", "negative infinity", and "not-a-number", 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 a SQL command, you must put quotes around them, for example `UPDATE table SET x = 'Infinity'`. On input, these strings are recognized in a case-insensitive manner.

PostgreSQL also supports the SQL-standard notations `float` and `float(`p`)` for specifying inexact numeric types. Here, `p` specifies the minimum acceptable precision in binary digits. PostgreSQL accepts `float(1)` to `float(24)` as selecting the `real` type, while `float(25)` to `float(53)` select `double precision`. Values of `p` outside the allowed range draw an error. `float` with no precision specified is taken to mean `double precision`.

> Note: Prior to PostgreSQL 7.4, the precision in `float(`p`)` was taken to mean so many decimal digits. This has been corrected to match the SQL standard, which specifies that the precision is measured in binary digits. The assumption that `real` and `double precision` have exactly 24 and 53 bits in the mantissa respectively is correct for IEEE-standard floating point implementations. On non-IEEE platforms it may be off a little, but for simplicity the same ranges of `p` are used on all platforms.

## 8.1.4. Serial Types

The data types `serial` and `bigserial` are not true types, but merely a notational convenience for setting up unique identifier columns (similar to the `AUTO_INCREMENT` property supported by some other databases). In the current implementation, specifying

    CREATE TABLE tablename (
        colname SERIAL
    );

is equivalent to specifying:

    CREATE SEQUENCE tablename_colname_seq;
    CREATE TABLE tablename (
        colname integer DEFAULT nextval('tablename_colname_seq') NOT NULL
    );

Thus, we have created an integer column and arranged for its default values to be assigned from a sequence generator. A `NOT NULL` constraint is applied to ensure that a null value cannot be explicitly inserted, either. In most cases you would also want to attach a `UNIQUE` or `PRIMARY KEY` constraint to prevent duplicate values from being inserted by accident, but this is not automatic.

> Note: Prior to PostgreSQL 7.3, `serial` implied `UNIQUE`. This is no longer automatic. If you wish a serial column to be in a unique constraint or a primary key, it must now be specified, same as with any other data type.

To insert the next value of the sequence into the `serial` column, specify that the `serial` column should be assigned its default value. This can be done either by excluding the column from the list of columns in the `INSERT` statement, or through the use of the `DEFAULT` key word.

The type names `serial` and `serial4` are equivalent: both create `integer` columns. The type names `bigserial` and `serial8` work just the same way, except that they create a `bigint` column. `bigserial` should be used if you anticipate the use of more than 231 identifiers over the lifetime of the table.

The sequence created for a `serial` column is automatically dropped when the owning column is dropped, and cannot be dropped otherwise. (This was not true in PostgreSQL releases before 7.3. Note that this automatic drop linkage will not occur for a sequence created by reloading a dump from a pre-7.3 database; the dump file does not contain the information needed to establish the dependency link.) Furthermore, this dependency between sequence and column is made only for the `serial` column itself. If any other columns reference the sequence (perhaps by manually calling the `nextval` function), they will be broken if the sequence is removed. Using a `serial` column's sequence in such a fashion is considered bad form; if you wish to feed several columns from the same sequence generator, create the sequence as an independent object.

Prev | Home | Next
`LIMIT` and `OFFSET` | Up | Monetary Types