Typically, a table at the subscriber will be defined the same as the
publisher table, so if the publisher table has a <link linkend="ddl-generated-columns">
<literal>GENERATED column</literal></link> then the subscriber table will
have a matching generated column. In this case, it is always the subscriber
table generated column value that is used.
通常、サブスクライバーのテーブルはパブリッシャーのテーブルと同じように定義されます。そのため、パブリッシャーテーブルにGENERATED(生成)列が存在する場合、サブスクライバーテーブルにも対応する生成列が存在します。
この場合、常にサブスクライバーテーブルの生成列の値が使用されます。
For example, note below that subscriber table generated column value comes from the subscriber column's calculation. 例えば以下の場合、サブスクライバーテーブルの生成列の値は、サブスクライバー列の計算から取得されることに注意してください。
/* pub # */ CREATE TABLE tab_gen_to_gen (a int, b int GENERATED ALWAYS AS (a + 1) STORED); /* pub # */ INSERT INTO tab_gen_to_gen VALUES (1),(2),(3); /* pub # */ CREATE PUBLICATION pub1 FOR TABLE tab_gen_to_gen; /* pub # */ SELECT * FROM tab_gen_to_gen; a | b ---+--- 1 | 2 2 | 3 3 | 4 (3 rows) /* sub # */ CREATE TABLE tab_gen_to_gen (a int, b int GENERATED ALWAYS AS (a * 100) STORED); /* sub # */ CREATE SUBSCRIPTION sub1 CONNECTION 'dbname=test_pub' PUBLICATION pub1; /* sub # */ SELECT * from tab_gen_to_gen; a | b ---+---- 1 | 100 2 | 200 3 | 300 (3 rows)
In fact, prior to version 18.0, logical replication does not publish
<literal>GENERATED</literal> columns at all.
実際、バージョン18.0より前では、論理レプリケーションは生成列を全くパブリッシュしません。
But, replicating a generated column to a regular column can sometimes be desirable. しかし、生成列の値を通常の列に複製することが望ましい場合もあります。
This feature may be useful when replicating data to a non-PostgreSQL database via output plugin, especially if the target database does not support generated columns. この機能は、出力プラグインを介してPostgreSQL以外のデータベースにデータを複製する場合、特に対象データベースが生成列をサポートしていない場合に有用です。
Generated columns are not published by default, but users can opt to publish stored generated columns just like regular ones. 生成列はデフォルトではパブリッシュされませんが、ユーザは格納生成列を通常の列と同様にパブリッシュすることを選択できます。
There are two ways to do this: これには2つの方法があります。
Set the <command>PUBLICATION</command> parameter
<link linkend="sql-createpublication-params-with-publish-generated-columns">
<literal>publish_generated_columns</literal></link> to <literal>stored</literal>.
This instructs PostgreSQL logical replication to publish current and
future stored generated columns of the publication's tables.
PUBLICATIONパラメータのpublish_generated_columnsをstoredに設定します。
これは、PostgreSQL論理レプリケーションに、パブリケーションのテーブルが持つ現在または将来の格納生成列をパブリッシュするように指示します。
Specify a table <link linkend="logical-replication-col-lists">column list</link> to explicitly nominate which stored generated columns will be published. どの格納生成列がパブリッシュされるかを明示的に指定する、テーブルの列リストを設定します。
When determining which table columns will be published, a column list
takes precedence, overriding the effect of the
<literal>publish_generated_columns</literal> parameter.
どのテーブル列をパブリッシュするかを決定する際には、列リストが優先され、publish_generated_columnsパラメータの効果を上書きします。
The following table summarizes behavior when there are generated columns involved in the logical replication. Results are shown for when publishing generated columns is not enabled, and for when it is enabled. 以下の表は、論理レプリケーションに生成列が含まれる場合の動作をまとめたものです。 生成列のパブリッシュが有効になっていない場合と、有効になっている場合の結果を示しています。
表29.2 レプリケーション結果の概要
| 生成列をパブリッシュするか否か? | パブリッシャーテーブルの列 | サブスクライバーテーブルの列 | 結果 |
|---|---|---|---|
| いいえ | 生成列 | 生成列 | パブリッシャーテーブルの列はレプリケーションされません。サブスクライバーテーブルの生成列の値を使用します。 |
| いいえ | 生成列 | 通常の列 | パブリッシャーテーブルの列はレプリケーションされません。サブスクライバーテーブルの通常の列の値を使用します。 |
| いいえ | 生成列 | --存在しない-- | パブリッシャーテーブルの列はレプリケーションされません。何も起こりません。 |
| はい | 生成列 | 生成列 | エラー。サポートされていません。 |
| はい | 生成列 | 通常の列 | パブリッシャーテーブルの列がサブスクライバーテーブルの列にレプリケーションされます。 |
| はい | 生成列 | --存在しない-- | エラー。サブスクライバーテーブルに列が存在しないことが報告されます。 |
There's currently no support for subscriptions comprising several publications where the same table has been published with different column lists. See <xref linkend="logical-replication-col-lists"/>. 現在のところ、同じテーブルが異なる列リストでパブリッシュされている複数のパブリケーションで構成されるサブスクリプションはサポートされていません。 29.5を参照してください。
This same situation can occur if one publication is publishing generated columns, while another publication in the same subscription is not publishing generated columns for the same table. 同様の状況は、あるパブリケーションが生成列をパブリッシュしている一方で、同じサブスクリプション内の別のパブリケーションが同じテーブルに対して生成列をパブリッシュしていない場合にも発生します。
If the subscriber is from a release prior to 18, then initial table synchronization won't copy generated columns even if they are defined in the publisher. サブスクライバーが18より前のリリースの場合、生成列のパブリッシュがパブリッシャーで規定されている場合でも、初期テーブル同期ではそれらの列はコピーされません。