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

ALTER SEQUENCE

ALTER SEQUENCE — change the definition of a sequence generator シーケンスジェネレータの定義を変更する

概要

ALTER SEQUENCE [ IF EXISTS ] name
    [ AS data_type ]
    [ INCREMENT [ BY ] increment ]
    [ MINVALUE minvalue | NO MINVALUE ] [ MAXVALUE maxvalue | NO MAXVALUE ]
    [ START [ WITH ] start ]
    [ RESTART [ [ WITH ] restart ] ]
    [ CACHE cache ] [ [ NO ] CYCLE ]
    [ OWNED BY { table_name.column_name | NONE } ]
ALTER SEQUENCE [ IF EXISTS ] name SET { LOGGED | UNLOGGED }
ALTER SEQUENCE [ IF EXISTS ] name OWNER TO { new_owner | CURRENT_ROLE | CURRENT_USER | SESSION_USER }
ALTER SEQUENCE [ IF EXISTS ] name RENAME TO new_name
ALTER SEQUENCE [ IF EXISTS ] name SET SCHEMA new_schema

説明

<title>Description</title>

<command>ALTER SEQUENCE</command> changes the parameters of an existing sequence generator. Any parameters not specifically set in the <command>ALTER SEQUENCE</command> command retain their prior settings. ALTER SEQUENCEは、既存のシーケンスジェネレータのパラメータを変更します。 ALTER SEQUENCEで指定されなかったパラメータについては、以前の設定が保持されます。

You must own the sequence to use <command>ALTER SEQUENCE</command>. To change a sequence's schema, you must also have <literal>CREATE</literal> privilege on the new schema. To alter the owner, you must be able to <literal>SET ROLE</literal> to the new owning role, and that role must have <literal>CREATE</literal> privilege on the sequence's schema. (These restrictions enforce that altering the owner doesn't do anything you couldn't do by dropping and recreating the sequence. However, a superuser can alter ownership of any sequence anyway.) ALTER SEQUENCEを使用するには、シーケンスの所有者でなければなりません。 シーケンスのスキーマを変更するには、新しいスキーマにおけるCREATE権限も持たなければなりません。 所有者を変更するには、新しい所有者ロールに対してSET ROLEができなければなりません。またそのロールはシーケンスのスキーマ上にCREATE権限を持たなければなりません。 (これらの制限は、シーケンスの削除および再作成によりユーザが実行できないことを、所有者の変更により実行されないようにするためのものです。 しかし、スーパーユーザはすべてのシーケンスの所有者を変更できます。)

パラメータ

<title>Parameters</title>

name

The name (optionally schema-qualified) of a sequence to be altered. 変更するシーケンスの名前です(スキーマ修飾名も可)。

IF EXISTS

Do not throw an error if the sequence does not exist. A notice is issued in this case. シーケンスが存在しない場合にエラーとしません。 この場合、注意メッセージが発生します。

data_type

The optional clause <literal>AS <replaceable class="parameter">data_type</replaceable></literal> changes the data type of the sequence. Valid types are <literal>smallint</literal>, <literal>integer</literal>, and <literal>bigint</literal>. オプション句AS data_typeはシーケンスのデータ型を変更します。 有効な型はsmallintintegerbigintです。

Changing the data type automatically changes the minimum and maximum values of the sequence if and only if the previous minimum and maximum values were the minimum or maximum value of the old data type (in other words, if the sequence had been created using <literal>NO MINVALUE</literal> or <literal>NO MAXVALUE</literal>, implicitly or explicitly). Otherwise, the minimum and maximum values are preserved, unless new values are given as part of the same command. If the minimum and maximum values do not fit into the new data type, an error will be generated. データ型を変更したとき、以前の最小値と最大値は、古いデータ型の最小値と最大値に一致している場合に限り(別の言い方をすれば、暗示的にせよ明示的にせよ、シーケンスがNO MINVALUEまたはNO MAXVALUEで作られていた場合に)、シーケンスの最小値および最大値が自動的に変更されます。 そうでない場合、新しい値が同じコマンドの一部として指定されているのでなければ、最小値と最大値は保存されます。 最小値と最大値が新しいデータ型に適合しない場合は、エラーが生成されます。

increment

The clause <literal>INCREMENT BY <replaceable class="parameter">increment</replaceable></literal> is optional. A positive value will make an ascending sequence, a negative one a descending sequence. If unspecified, the old increment value will be maintained. INCREMENT BY increment句は省略可能です。 正の値が指定された時は昇順のシーケンス、負の値が指定された時は降順のシーケンスにします。 指定がない場合、以前の増分値が保持されます。

minvalue
NO MINVALUE

The optional clause <literal>MINVALUE <replaceable class="parameter">minvalue</replaceable></literal> determines the minimum value a sequence can generate. If <literal>NO MINVALUE</literal> is specified, the defaults of 1 and the minimum value of the data type for ascending and descending sequences, respectively, will be used. If neither option is specified, the current minimum value will be maintained. MINVALUE minvalue句はシーケンスジェネレータが生成する最小値を決定します。 NO MINVALUEが指定された場合、昇順の時は1、降順の時はデータ型の最小値がデフォルトになります。 どちらのオプションも指定されていなければ、現在の最小値が保持されます。

maxvalue
NO MAXVALUE

The optional clause <literal>MAXVALUE <replaceable class="parameter">maxvalue</replaceable></literal> determines the maximum value for the sequence. If <literal>NO MAXVALUE</literal> is specified, the defaults of the maximum value of the data type and -1 for ascending and descending sequences, respectively, will be used. If neither option is specified, the current maximum value will be maintained. MAXVALUE maxvalue句はシーケンスが生成する最大値を決定します。 NO MAXVALUEが指定された場合、昇順の時はデータ型の最大値、降順の時は-1がデフォルトになります。 どちらのオプションも指定されていなければ、現在の最大値が保持されます。

start

The optional clause <literal>START WITH <replaceable class="parameter">start</replaceable></literal> changes the recorded start value of the sequence. This has no effect on the <emphasis>current</emphasis> sequence value; it simply sets the value that future <command>ALTER SEQUENCE RESTART</command> commands will use. START WITH start句(省略可能)は、記録されているシーケンスの開始値を変更します。 これは現在のシーケンス値に影響しません。 単に将来実行されるALTER SEQUENCE RESTARTコマンドが使用する値を設定するだけです。

restart

The optional clause <literal>RESTART [ WITH <replaceable class="parameter">restart</replaceable> ]</literal> changes the current value of the sequence. This is similar to calling the <function>setval</function> function with <literal>is_called</literal> = <literal>false</literal>: the specified value will be returned by the <emphasis>next</emphasis> call of <function>nextval</function>. Writing <literal>RESTART</literal> with no <replaceable class="parameter">restart</replaceable> value is equivalent to supplying the start value that was recorded by <command>CREATE SEQUENCE</command> or last set by <command>ALTER SEQUENCE START WITH</command>. RESTART WITH restart句(省略可能)は、シーケンスの現在値を変更します。 これはis_called = falseとしてsetval関数を呼び出すことと似ています。 指定した値は次のnextval呼出時に返されます。 restartを付けずにRESTARTと記述することは、CREATE SEQUENCEで記録、または前回ALTER SEQUENCE START WITHで設定された開始値を指定することと同じです。

In contrast to a <function>setval</function> call, a <literal>RESTART</literal> operation on a sequence is transactional and blocks concurrent transactions from obtaining numbers from the same sequence. If that's not the desired mode of operation, <function>setval</function> should be used. setvalの呼び出しとは異なり、シーケンスに対するRESTARTの操作はトランザクション的で、同時実行中のトランザクションが同じシーケンスから値を取得するのをブロックします。 それが期待する動作でないときは、setvalを使用してください。

cache

The clause <literal>CACHE <replaceable class="parameter">cache</replaceable></literal> enables sequence numbers to be preallocated and stored in memory for faster access. The minimum value is 1 (only one value can be generated at a time, i.e., no cache). If unspecified, the old cache value will be maintained. CACHE cache句を使用すると、アクセスを高速化するために、シーケンス番号を事前に割り当て、メモリに保存しておくことができます。 最小値は1です(一度に生成する値が1つだけなので、キャッシュがない状態になります)。 指定がなければ、以前のキャッシュ値が保持されます。

CYCLE

The optional <literal>CYCLE</literal> key word can be used to enable the sequence to wrap around when the <replaceable class="parameter">maxvalue</replaceable> or <replaceable class="parameter">minvalue</replaceable> has been reached by an ascending or descending sequence respectively. If the limit is reached, the next number generated will be the <replaceable class="parameter">minvalue</replaceable> or <replaceable class="parameter">maxvalue</replaceable>, respectively. CYCLEキーワードを使用すると、シーケンスが限界値(昇順の場合はmaxvalue、降順の場合はminvalue)に達した時、そのシーケンスを周回させることができます。 限界値に達した時、次に生成される番号は、昇順の場合はminvalue、降順の場合はmaxvalueになります。

NO CYCLE

If the optional <literal>NO CYCLE</literal> key word is specified, any calls to <function>nextval</function> after the sequence has reached its maximum value will return an error. If neither <literal>CYCLE</literal> or <literal>NO CYCLE</literal> are specified, the old cycle behavior will be maintained. NO CYCLEキーワードが指定されると、シーケンスの限界値に達した後のnextval呼び出しは全てエラーとなります。 CYCLENO CYCLEも指定されていない場合は、以前の周回動作が保持されます。

SET { LOGGED | UNLOGGED }

This form changes the sequence from unlogged to logged or vice-versa (see <xref linkend="sql-createsequence"/>). It cannot be applied to a temporary sequence. この構文は、シーケンスをログを取らないものからログを取るものに、またはその逆に変更します(CREATE SEQUENCEを参照してください)。 一時シーケンスには適用できません。

OWNED BY table_name.column_name
OWNED BY NONE

The <literal>OWNED BY</literal> option causes the sequence to be associated with a specific table column, such that if that column (or its whole table) is dropped, the sequence will be automatically dropped as well. If specified, this association replaces any previously specified association for the sequence. The specified table must have the same owner and be in the same schema as the sequence. Specifying <literal>OWNED BY NONE</literal> removes any existing association, making the sequence <quote>free-standing</quote>. OWNED BYオプションにより、シーケンスは指定されたテーブル列に関連付けられ、その列(やテーブル全体)が削除されると、自動的にシーケンスも一緒に削除されるようになります。 指定があると、以前に指定されたシーケンスの関連は、指定された関連に置き換えられます。 指定するテーブルは、シーケンスと同一所有者でなければならず、また、同一のスキーマ内に存在しなければなりません。 OWNED BY NONEを指定することで、既存の関連は削除され、シーケンスは独立したものになります。

new_owner

The user name of the new owner of the sequence. シーケンスの新しい所有者のユーザ名です。

new_name

The new name for the sequence. シーケンスの新しい名称です。

new_schema

The new schema for the sequence. シーケンスの新しいスキーマです。

注釈

<title>Notes</title>

<command>ALTER SEQUENCE</command> will not immediately affect <function>nextval</function> results in backends, other than the current one, that have preallocated (cached) sequence values. They will use up all cached values prior to noticing the changed sequence generation parameters. The current backend will be affected immediately. ALTER SEQUENCEは、コマンドを実行したバックエンド以外のバックエンドにおけるnextvalに対しては、すぐには効力を発揮しません。 これらのバックエンドに事前に割り当てられた(キャッシュされた)シーケンス値がある場合、この値を全て使い果たした後に、変更されたシーケンス生成パラメータを検知します。 コマンドを実行したバックエンドには、即座に変更が反映されます。

<command>ALTER SEQUENCE</command> does not affect the <function>currval</function> status for the sequence. (Before <productname>PostgreSQL</productname> 8.3, it sometimes did.) ALTER SEQUENCEはシーケンスのcurrval状態には影響しません。 (8.3より前のPostgreSQLでは影響を与える場合がありました。)

<command>ALTER SEQUENCE</command> blocks concurrent <function>nextval</function>, <function>currval</function>, <function>lastval</function>, and <command>setval</command> calls. ALTER SEQUENCEは、同時に実行されるnextvalcurrvallastvalsetvalの呼び出しをブロックします。

For historical reasons, <command>ALTER TABLE</command> can be used with sequences too; but the only variants of <command>ALTER TABLE</command> that are allowed with sequences are equivalent to the forms shown above. 歴史的な理由によりALTER TABLEはシーケンスにも使用することができます。 しかし、シーケンスに対して許されるALTER TABLEの構文は、上で示した構文と等価なものだけです。

<title>Examples</title>

Restart a sequence called <literal>serial</literal>, at 105: serialというシーケンスを105から再開します。

ALTER SEQUENCE serial RESTART WITH 105;

互換性

<title>Compatibility</title>

<command>ALTER SEQUENCE</command> conforms to the <acronym>SQL</acronym> standard, except for the <literal>AS</literal>, <literal>START WITH</literal>, <literal>OWNED BY</literal>, <literal>OWNER TO</literal>, <literal>RENAME TO</literal>, and <literal>SET SCHEMA</literal> clauses, which are <productname>PostgreSQL</productname> extensions. ALTER SEQUENCEは、PostgreSQLの拡張であるASSTART WITHOWNED BYOWNER TORENAME TOSET SCHEMA構文を除いて、標準SQLに従っています。

関連項目

<title>See Also</title> CREATE SEQUENCE, DROP SEQUENCE