バージョンごとのドキュメント一覧

8.1. 数値データ型 #

<title>Numeric Types</title>

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. 数値データ型には2、4、8バイト整数と、4、8バイト浮動小数点および精度設定が可能な数があります。 表 8.2に使用可能な型を列挙します。

表8.2 数値データ型

<title>Numeric Types</title>
型名格納サイズ説明範囲
smallint2バイト狭範囲の整数-32768から+32767
integer4バイト典型的に使用する整数-2147483648から+2147483647
bigint8バイト広範囲整数-9223372036854775808から+9223372036854775807
decimal可変長ユーザ指定精度、正確小数点より上は131072桁まで、小数点より下は16383桁まで
numeric可変長ユーザ指定精度、正確小数点より上は131072桁まで、小数点より下は16383桁まで
real4バイト可変精度、不正確6桁精度
double precision8バイト可変精度、不正確15桁精度
smallserial2バイト狭範囲自動整数1から32767
serial4バイト自動増分整数1から2147483647
bigserial8バイト広範囲自動増分整数1から9223372036854775807

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. 数値データ型に対する定数の構文は4.1.2で説明しています。 数値データ型には対応する算術演算子と関数の一式が揃っています。 詳細は第9章を参照してください。 次節でデータ型について詳しく説明します。

8.1.1. 整数データ型 #

<title>Integer Types</title>

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. smallintintegerbigintは各種範囲の整数、つまり小数点以下の端数がない数を保持します。 許容範囲から外れた値を保存しようとするとエラーになります。

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. integer型は数値の範囲、格納サイズおよび性能において最も釣合いが取れていますので、一般的に使用されます。 smallint型は通常はディスク容量に制限が付いている場合にのみ使用します。 bigint型はintegerの許容範囲では十分ではない場合に使用されるよう設計されています。

<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. SQLでは整数の型としてinteger(またはint)とsmallintbigintのみを規定しています。 int2int4およびint8は拡張ですが、いくつか他のSQLデータベースシステムでも使われています。

8.1.2. 任意の精度を持つ数 #

<title>Arbitrary Precision Numbers</title>

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. numeric型は、非常に大きな桁数で数値を格納できます。 通貨金額やその他正確性が求められる数量を保存する時は特に、この型を推奨します。 numericの値での計算は、可能なところ、例えば、足し算、引算、掛け算では、正確な結果(訳注:10進の小数で誤差が生じない、ということ)になります。 とは言っても、numericの値に対する計算は整数型、もしくは次節で説明する浮動小数点データ型に比較し非常に遅くなります。

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. この後の説明では、次の用語を使用します。 numeric精度(precision)とは数字全体の有効桁数です。 すなわち、小数点をはさんでいる両側の桁数の合計です。 numeric位取り(scale)とは、小数点の右側の小数部分の桁数をいいます。 そのため、23.5141という数値の精度は6で位取りは4となります。 整数の位取りは、ゼロであるとみなすことができます。

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: numeric列の数値の最大精度と最大位取りの両方を設定することができます。 numeric型の列を宣言するには次の構文を使います。

NUMERIC(precision, scale)

The precision must be positive, while the scale may be positive or negative (see below). Alternatively: 精度は正でなければならず、位取りは正または負であることができます(以下を参照)。 または、次のように指定します

NUMERIC(precision)

selects a scale of 0. Specifying: は位取りが0であることを選択します。 精度も位取りも指定せず、

NUMERIC

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.) と記述すると、実装されている限界の精度まで、いかなる精度あるいは位取りの値も格納できる制約の無い数値列が作られます。 この類の列は入力値をいかなる特定の位取りにも変換しませんが、宣言された位取りを持つnumeric列は入力値をその位取りに変換します。 (標準SQLはデフォルトとして位取り0を要求していて、つまり、整数の精度に変換されます。 しかし、この方法はあまり役に立たないと思われます。 もし移植性を心配するなら、常に精度と位取りを明示的に設定してください。)

注記

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"/>. 明示的にnumeric型宣言で指定される場合の最大精度は1000です。 制約の無いnumeric列は表 8.2で説明する制限に従います。

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 格納される値の位取りが宣言された列の位取りより大きかった場合、システムは指定された小数部の桁まで値を丸めます。 そして、小数点の左側の桁数が、宣言された精度から宣言された位取りを差し引いた数を超える場合にエラーとなります。 例えば、

NUMERIC(3, 1)

will round values to 1 decimal place and can store values between -99.9 and 99.9, inclusive. として宣言された列は、値を小数第1位に丸め、-99.9から99.9までの値を格納できます。

Beginning in <productname>PostgreSQL</productname> 15, it is allowed to declare a <type>numeric</type> 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 PostgreSQL 15以降では、負の位取りを持つnumeric列を宣言することが許可されました。 その場合、値は小数点の左側に丸められます。 精度は、依然として丸められない最大桁数を表します。 したがって、

NUMERIC(2, -3)

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 として宣言された列は、千に近い値に丸められ、-99000から99000までの値を格納できます。 また、宣言された精度よりも大きな位取りを宣言することも許可されます。 このような列は小数値しか保持できず、小数点のすぐ右の0桁は、少なくとも宣言された位取りから宣言された精度を引いた値である必要があります。 例えば、

NUMERIC(3, 5)

will round values to 5 decimal places and can store values between -0.00999 and 0.00999, inclusive. として宣言された列は、小数点以下5桁に丸められ、-0.00999から0.00999までの値を格納できます。

注記

<productname>PostgreSQL</productname> permits the scale in a <type>numeric</type> type declaration to be any value in the range -1000 to 1000. However, the <acronym>SQL</acronym> standard requires the scale to be in the range 0 to <replaceable>precision</replaceable>. Using scales outside that range may not be portable to other database systems. PostgreSQLでは、numeric型宣言の位取りを-1000~1000の範囲の任意の値にすることができます。 しかし、SQL標準では位取りを0~精度の範囲にする必要があります。 この範囲外の位取りを使用すると、他のデータベースシステムに移植できない可能性があります。

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. 数値は物理的に先頭や末尾に0を付与されることなく格納されます。 したがって、列の宣言された精度と位取りは最大であり、固定的に割り当てられていません。 (この意味ではnumericchar(n)よりもvarchar(n)に似ています。) 実際の格納に必要な容量は、10進数4桁のそれぞれのグループに対して2バイトと、3から8バイトのオーバーヘッドです。

In addition to ordinary numeric values, the <type>numeric</type> type has several special values: 通常の数値に加え、numeric型はいくつかの特別な値を取ることができます。


Infinity
-Infinity
NaN

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>. これらはIEEE 754標準から引用されたもので、それぞれ無限大マイナス無限大そして非数値を表します。 SQLコマンドの定数として記述する場合は、例えばUPDATE table SET x = '-Infinity'のように、引用符でくくらなければなりません。 入力の際、これらの文字列は大文字小文字の区別なく認識されます。 無限の値は代わりにinf-infと綴ることもできます。

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. 無限の値は数学的に期待されたとおりに振る舞います。 例えば、Infinityに有限な値を加算した場合やInfinityInfinityを加算した場合はInfinityになります。しかし、InfinityからInfinityを減算した場合は、解釈が定まらないためNaN(数値では無い)になります。 無限大は制約が無いnumeric列にのみ格納できることに注意してください。これは理論上、いかなる有限な精度も超えているためです。

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.) NaN(数値では無い)値は定義されていない計算の結果を表現するために使用されます。 通常、NaN入力を伴う操作は別のNaNを出力します。 その操作の入力がNaNを他の有限もしくは無限の数値型の値に置き換えられた場合に同じ出力が得られる時に限り例外があります。 この時はNaNの代わりにその操作の出力が使われます(この概念では例えば、NaNの0乗は1を出力します)。

注記

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. ほとんどの非数の概念の実装において、NaNは(NaNを含む)他の数値と等価にならないとみなされています。 numeric値をソートできる、また、ツリーを基にしたインデックスで使用できるように、PostgreSQLNaN同士は等しく、すべてのNaN以外の値よりも大きな値となるものとして扱います。

The types <type>decimal</type> and <type>numeric</type> are equivalent. Both types are part of the <acronym>SQL</acronym> standard. decimalnumeric型は等価です。 2つのデータ型はともに標準SQLに含まれます。

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: 値を丸める際、numeric型は0から離れるように丸めますが、一方で(ほとんどのマシンでは)realdouble precision型ではその値に最も近い偶数に丸めます。 以下に例を示します。:

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)

8.1.3. 浮動小数点データ型 #

<title>Floating-Point Types</title>

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. realdouble precisionは不正確な(訳注:10進の小数で誤差が生じる、ということ)可変精度の数値データ型です。 現在サポートされている全てのプラットフォーム上では、これらのデータ型は、使用しているプロセッサ、オペレーティングシステムおよびコンパイラがサポートしていれば、通常は(それぞれ単精度および倍精度の)バイナリ浮動小数点演算用のIEEE規格754の実装です。

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: 不正確というのは、ある値はそのままで内部形式に変換されずに近似値として保存されるということです。 ですから、保存しようとする値と抽出しようとする値の間に多少の差異が認められます。 これらのエラーを管理し計算によって補正をどうするかについては、数学とコンピュータ科学の系統すべてに関わることで、以下の点を除き触れません。

  • If you require exact storage and calculations (such as for monetary amounts), use the <type>numeric</type> type instead. (金銭金額など)正確な記録と計算が必要な時は代わりにnumericを使用してください。

  • 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 might not always work as expected. 2つの浮動小数点値が等価であるのかどうかの比較は予想通りに行かない時もあります。

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. 現在サポートされている全てのプラットフォームでは、real型は最低6桁の精度を持ち、1E-37から1E+37までの範囲です。 double precision型は最低15桁の精度でおよそ1E-307から1E+308までの範囲です。 大き過ぎたり小さ過ぎる値はエラーの原因になります。 入力値の精度が高過ぎる場合は丸められることがあります。 ゼロに限りなく近い値で、しかもゼロと異なる値として表現できない数値はアンダーフローエラーになります。

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. デフォルトでは、浮動小数の値は最も短い正確な10進数のテキスト形式で出力されます。 生成される10進値は、同じバイナリ精度で表現できる他の値よりも、実際に格納されているバイナリ値に近い値になります。 (ただし、出力値が2つの表現可能な値の厳密な中間になることはありません。これは、入力ルーチンが最も近い偶数に丸める規則を適切に考慮しないという広範囲にわたる不具合を避けるためです。) この値はfloat8型の値には最大17桁の10進数、float4型の値には最大9桁の10進数を使用します。

注記

This shortest-precise output format is much faster to generate than the historical rounded format. この最も短く正確な出力フォーマットは従来の丸められた形式よりもはるかに速く値を生成します。

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. PostgreSQLの古いバージョンで生成された出力との互換性を確保し、出力精度を低くするために、代わりにextra_float_digitsパラメータを使用して丸めた10進数の出力を選択することができます。 値を0に設定した場合は、以前のデフォルト値である6(float4の場合)か15(float8の場合)の有効桁に丸めた値を戻します。 負の値を設定すると、桁数がさらに減少します。たとえば、-2を設定すると、出力はそれぞれ4桁または13桁に丸められます。

Any value of <xref linkend="guc-extra-float-digits"/> greater than 0 selects the shortest-precise format. 0より大きいextra_float_digitsの値は、最短の正確なフォーマットを選択します。

注記

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. 精密な値を必要とするアプリケーションでは、従来、extra_float_digitsを3に設定して値を取得する必要がありました。 バージョン間の互換性を最大にするためには継続してそのように設定する必要があります。

In addition to ordinary numeric values, the floating-point types have several special values: 通常の数値に加え、浮動小数点型では以下の特殊な値を取ります。


Infinity
-Infinity
NaN

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>. これらはそれぞれ、IEEE 754の特殊な値、無限大負の無限大非数値を表します。 これらの値をSQLコマンドの定数として記述する場合、例えばUPDATE table SET x = '-Infinity'のように引用符でくくる必要があります。 入力の際、これらの文字列は大文字小文字の区別なく認識されます。 無限の値は代わりにinf-infと綴ることもできます。

注記

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. IEEE 754では、NaNは(NaNを含む)他のすべての浮動小数点値と比べた時に不等でなければならないと規定しています。 浮動小数点値をソートできる、また、ツリーを基にしたインデックスで使用できるように、PostgreSQLNaN同士は等しく、すべてのNaN以外の値よりも大きな値となるものとして扱います。

<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>. また、PostgreSQLでは不正確な数値型についての標準SQLの表記であるfloatfloat(p)をサポートしています。 ここで、p2進数桁数で最低限、許容可能な精度を指定します。 PostgreSQLfloat(1)からfloat(24)realを選択するものとして受け付け、float(25)からfloat(53)double precisionを選択するものとして受け付けます。 許容範囲外のpの値はエラーになります。 精度指定のないfloatdouble precisionとして解釈されます。

8.1.4. 連番型 #

<title>Serial Types</title>

注記

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"/>. このセクションではPostgreSQL固有の自動増分列の作成方法について記述します。 SQL標準の識別列を作成する方法は、CREATE TABLEに記述されています。

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: smallserialserialおよびbigserialデータ型は正確にはデータ型ではなく、テーブルの列に一意の識別子を作成する簡便な表記法です (他のデータベースでサポートされるAUTO_INCREMENTプロパティに似ています)。 現在の実装では、

CREATE TABLE tablename (
    colname SERIAL
);

is equivalent to specifying: は以下を指定することと同じです。

CREATE SEQUENCE tablename_colname_seq AS integer;
CREATE TABLE tablename (
    colname integer NOT NULL DEFAULT nextval('tablename_colname_seq')
);
ALTER SEQUENCE tablename_colname_seq OWNED BY tablename.colname;

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. このように整数列を作成し、その列のデフォルト値が連番ジェネレータから割り当てられるようにしました。 また、NOT NULL制約を適用することによって、NULL値が挿入されないようにします。 (たいていの場合は、重複する値を間違って挿入しないように、UNIQUE制約またはPRIMARY KEY制約も追加することになるでしょうが、これは自動的には行われません。) 最後に、シーケンスは列により所有されるものと印が付きます。 したがって、テーブルの列が削除された場合にシーケンスは削除されます。

注記

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. smallserialserialおよびbigserialはシーケンスを使って実装されているため、行の削除が行われていなくとも、列に"穴"や連番の抜けが発生するかもしれません。 また、テーブルへ正常に挿入されていないにも関わらず、シーケンスの値を"消費してしまう"こともあります。 これは、例えば挿入したトランザクションがロールバックされた時に発生することがあります。 詳細は9.17nextval()を参照してください。

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. serial列にシーケンスの次の値を挿入するには、serial列にそのデフォルト値を割り当てるよう指定してください。 これは、INSERT文の列リストからその列を除外する、もしくはDEFAULTキーワードを使用することで行うことができます。

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. serialserial4という型の名称は等価です。 ともにinteger列を作成します。 bigserialserial8という型の名称もbigint列を作成することを除いて同じ振舞いをします。 もしテーブルを使用する期間で231以上の識別子を使用すると予測される場合、bigserialを使用すべきです。 smallserialserial2という型の名称もまた、smallint列を作成することを除いて同じ振舞いをします。

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. serial列用に作成されたシーケンスは、それを所有する列が削除された時に自動的に削除されます。 列を削除せずにシーケンスを削除することができますが、これにより強制的に列のデフォルト式が削除されます。