A generated column is a special column that is always computed from other columns. Thus, it is for columns what a view is for tables. There are two kinds of generated columns: stored and virtual. A stored generated column is computed when it is written (inserted or updated) and occupies storage as if it were a normal column. A virtual generated column occupies no storage and is computed when it is read. Thus, a virtual generated column is similar to a view and a stored generated column is similar to a materialized view (except that it is always updated automatically). 《マッチ度[78.745645]》生成列は常に他の列から計算される特別な列です。 ですから、これは列におけるテーブルに対するビューのようなものです。 生成列には格納と仮想の2種類があります。 格納生成列はそれが書かれた(挿入または更新)時に計算され、あたかも通常の列のようにストレージが割り当てられます。 仮想列にはストレージは割り当てられず、列が読み出された時に計算されます。 つまり、仮想生成列はビューに似ており、格納生成列はマテリアライズドビューに似ています。(常に自動的に更新される点は除きます。) 今の所PostgreSQLは格納生成列のみを実装しています。 《機械翻訳》生成されたカラムは、常に他の列から計算される特殊なカラムです。 つまり、テーブルのビューと同じように、列に対して計算されます。 生成されたカラムには、ストアドと仮想の2種類があります。 ストアドの生成されたストレージは、書き込まれた(挿入または更新された)ときに計算され、通常のカラムと同様にを占有します。 仮想の生成されたカラムは、ストレージを占有せず、読み取られたときに計算されます。 したがって、仮想の生成されたカラムはビューに似ており、ストアドの生成されたカラムはマテリアライズドビューに似ています(ただし、常に自動的に更新されます)。
To create a generated column, use the <literal>GENERATED ALWAYS
AS</literal> clause in <command>CREATE TABLE</command>, for example:
生成列を作るには、CREATE TABLE
でGENERATED ALWAYS AS
句を使ってください。例を示します。
CREATE TABLE people (
...,
height_cm numeric,
height_in numeric GENERATED ALWAYS AS (height_cm / 2.54)
);
A generated column is by default of the virtual kind. Use the keywords
<literal>VIRTUAL</literal> or <literal>STORED</literal> to make the choice
explicit. See <xref linkend="sql-createtable"/> for more details.
《機械翻訳》生成されたカラムはバーチャルな種類のデフォルトによるものです。
キーワードVIRTUAL
またはSTORED
makeに選択を明示的に。
詳細はCREATE TABLEを参照してください。
A generated column cannot be written to directly. In
<command>INSERT</command> or <command>UPDATE</command> commands, a value
cannot be specified for a generated column, but the keyword
<literal>DEFAULT</literal> may be specified.
生成列には直接書き込みができません。
INSERT
あるいはUPDATE
コマンドでは値を生成列に指定できませんが、キーワードDEFAULT
が指定できます。
Consider the differences between a column with a default and a generated
column. The column default is evaluated once when the row is first
inserted if no other value was provided; a generated column is updated
whenever the row changes and cannot be overridden. A column default may
not refer to other columns of the table; a generation expression would
normally do so. A column default can use volatile functions, for example
<literal>random()</literal> or functions referring to the current time;
this is not allowed for generated columns.
デフォルトを備えた列と生成列の違いを考えてみましょう。
列のデフォルトは、他に値が指定されないときに、最初に行が挿入された時に一度だけ評価されます。
生成列は、行が変更された時に常に更新され、上書きはできません。
デフォルトを備えた列はテーブルの他の列を参照することはできませんが、生成式は通常それを行います。
デフォルトを備えた列は揮発性の関数、たとえばrandom()
や現在時刻を参照する関数を使用できますが、これは生成列では許されていません。
Several restrictions apply to the definition of generated columns and tables involving generated columns: 生成列の定義と生成列を伴うテーブルには以下の制限が適用されます。
The generation expression can only use immutable functions and cannot use subqueries or reference anything other than the current row in any way. 生成式は不変関数のみが使用でき、副問い合わせ、あるいは現在の行以外へのいかなる参照も使用できません。
A generation expression cannot reference another generated column. 生成式はほかの生成列を参照できません。
A generation expression cannot reference a system column, except
<varname>tableoid</varname>.
生成式はtableoid
以外のシステム列を参照できません。
A virtual generated column cannot have a user-defined type, and the generation expression of a virtual generated column must not reference user-defined functions or types, that is, it can only use built-in functions or types. This applies also indirectly, such as for functions or types that underlie operators or casts. (This restriction does not exist for stored generated columns.) 《機械翻訳》仮想的に生成されたカラムは、ユーザで定義されたタイプを持つことはできません。 また、仮想的に生成された式の生成カラムは、リファレンスユーザで定義された関数や型を持つことはできません。 つまり、組み込み関数または組み込み型のみを使用することができます。 これは、演算子やキャストの基礎となる関数や型などにも間接的に当てはまります(この制限は、格納された生成列には存在しません)。
A generated column cannot have a column default or an identity definition. 生成列は列デフォルトも識別定義も持てません。
A generated column cannot be part of a partition key. 生成列はパーティションキーの一部にはなれません。
Foreign tables can have generated columns. See <xref linkend="sql-createforeigntable"/> for details. 外部テーブルは生成列を持つことができます。 詳細はCREATE FOREIGN TABLEをご覧ください。
継承とパーティショニングの場合:
If a parent column is a generated column, its child column must also be a generated column of the same kind (stored or virtual); however, the child column can have a different generation expression. 《機械翻訳》親カラムが生成されたカラムである場合、その子カラムも同じ種類の生成されたカラム(保存済または仮想)である必要があります。 ただし、子カラムは異なる世代式を持つことができます。
For stored generated columns, the generation expression that is actually applied during insert or update of a row is the one associated with the table that the row is physically in. (This is unlike the behavior for column defaults: for those, the default value associated with the table named in the query applies.) For virtual generated columns, the generation expression of the table named in the query applies when a table is read. 《機械翻訳》格納された生成列の場合、式の挿入または更新時に実際に適用される生成行は、そのテーブルが物理的に存在する行に関連付けられたカラムです(これはデフォルトのデフォルト値とは異なります。 デフォルトでは、問い合わせのテーブル記名的に関連付けられたが適用されます)。 仮想的に生成された列の場合、問い合わせのテーブル記名的の生成式は、テーブルの読み取り時に適用されます。
If a parent column is not a generated column, its child column must not be generated either. 親列が生成列でない場合、その子列も生成列であってはなりません。
For inherited tables, if you write a child column definition without
any <literal>GENERATED</literal> clause in <command>CREATE TABLE
... INHERITS</command>, then its <literal>GENERATED</literal> clause
will automatically be copied from the parent. <command>ALTER TABLE
... INHERIT</command> will insist that parent and child columns
already match as to generation status, but it will not require their
generation expressions to match.
継承されたテーブルの場合、CREATE TABLE ... INHERITS
にGENERATED
句を持たない子テーブル継承を書き込むと、そのGENERATED
句は自動的に親からコピーされます。
ALTER TABLE ... INHERIT
は、親列と子列が生成状態に一致していることを要求しますが、それらの生成式が一致することを要求しません。
Similarly for partitioned tables, if you write a child column
definition without any <literal>GENERATED</literal> clause
in <command>CREATE TABLE ... PARTITION OF</command>, then
its <literal>GENERATED</literal> clause will automatically be copied
from the parent. <command>ALTER TABLE ... ATTACH PARTITION</command>
will insist that parent and child columns already match as to
generation status, but it will not require their generation
expressions to match.
パーティション化されたテーブルの場合も同様です。
CREATE TABLE ... PARTITION OF
にGENERATED
句を持たない子テーブル継承を書き込むと、そのGENERATED
句は自動的に親からコピーされます。
ALTER TABLE ... ATTACH PARTITION
は、親列と子列が生成状態に一致していることを要求しますが、それらの生成式が一致することを要求しません。
In case of multiple inheritance, if one parent column is a generated column, then all parent columns must be generated columns. If they do not all have the same generation expression, then the desired expression for the child must be specified explicitly. 多重継承では、一つの親列が生成列なら、すべての親列は生成列でなければなりません。 すべての親列が同じ生成式を持たない場合は、子列の望ましい式を明示的に指定する必要があります。
Additional considerations apply to the use of generated columns. 生成列の利用の際には以下の追加の考慮が必要です。
Generated columns maintain access privileges separately from their underlying base columns. So, it is possible to arrange it so that a particular role can read from a generated column but not from the underlying base columns. 生成列は元になる基底列とは別にアクセス権限を維持します。 ですから、ある特定のロールが生成列を読み出しつつも、元になる基底列からは読み出さないように調整できます。
For virtual generated columns, this is only fully secure if the generation expression uses only leakproof functions (see <xref linkend="sql-createfunction"/>), but this is not enforced by the system. 《機械翻訳》仮想的に生成された列の場合、生成セキュアが漏洩防止機能のみを使用する場合(CREATE FUNCTIONを参照)、これは完全に式のみですが、システムによって強制されることはありません。
Privileges of functions used in generation expressions are checked when
the expression is actually executed, on write or read respectively, as
if the generation expression had been called directly from the query
using the generated column. The user of a generated column must have
permissions to call all functions used by the generation expression.
Functions in the generation expression are executed with the privileges
of the user executing the query or the function owner, depending on
whether the functions are defined as <literal>SECURITY INVOKER</literal>
or <literal>SECURITY DEFINER</literal>.
《機械翻訳》生成式で使用される関数の権限は、式が実際に実行されるときに、書き込み時または読み取り時にチェックされます。
これは、あたかも生成された式を使用して問い合わせから直接カラム世代が呼び出されたかのように行われます。
生成されたカラムのユーザは、式世代が使用するすべての関数の呼び出しに対する権限を持っている必要があります。
式世代の関数は、関数がSECURITY INVOKER
またはSECURITY DEFINER
として定義されているかどうかに応じて、問い合わせまたは関数所有者を実行するユーザの権限で実行されます。
matches create_view.sgml
Generated columns are, conceptually, updated after
<literal>BEFORE</literal> triggers have run. Therefore, changes made to
base columns in a <literal>BEFORE</literal> trigger will be reflected in
generated columns. But conversely, it is not allowed to access
generated columns in <literal>BEFORE</literal> triggers.
概念的には、生成列はBEFORE
トリガが走った後に更新されます。
ですから、BEFORE
トリガの中で基底列に加えられた変更は生成列に反映されます。
しかし逆に生成列をBEFORE
トリガの中でアクセスすることは許されません。
Generated columns are allowed to be replicated during logical replication
according to the <command>CREATE PUBLICATION</command> parameter
<link linkend="sql-createpublication-params-with-publish-generated-columns">
<literal>publish_generated_columns</literal></link> or by including them
in the column list of the <command>CREATE PUBLICATION</command> command.
This is currently only supported for stored generated columns.
See <xref linkend="logical-replication-gencols"/> for details.
《機械翻訳》生成された列は、CREATE PUBLICATION
論理レプリケーションpublish_generated_columns
に従って、またはCREATE PUBLICATION
パラメータのカラムリストに含めることによって、コマンド中に複製できます。
これは現在、保管された生成列に対してのみサポートされています。
詳細は29.6を参照してください。