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

49.2. ロジカルデコーディングのコンセプト #

<title>Logical Decoding Concepts</title>

49.2.1. ロジカルデコーディング #

<title>Logical Decoding</title>

Logical decoding is the process of extracting all persistent changes to a database's tables into a coherent, easy to understand format which can be interpreted without detailed knowledge of the database's internal state. ロジカルデコーディングは、データベースのテーブルへの恒久的な更新を、一貫性があって、データベース内部の状態に関する詳細な知識がなくても容易に理解できる形式として取得するプロセスです。

In <productname>PostgreSQL</productname>, logical decoding is implemented by decoding the contents of the <link linkend="wal">write-ahead log</link>, which describe changes on a storage level, into an application-specific form such as a stream of tuples or SQL statements. PostgreSQLにおいてロジカルデコーディングは、記憶装置のレベルで更新を記述する書き込み先行ログの内容を、タプルやSQL文のストリームといったアプリケーション固有の形式にデコードすることによって実装されています。

49.2.2. レプリケーションスロット #

<title>Replication Slots</title>

In the context of logical replication, a slot represents a stream of changes that can be replayed to a client in the order they were made on the origin server. Each slot streams a sequence of changes from a single database. ロジカルレプリケーションの文脈ではスロットは、元のサーバで行われた変更と同じ順序でクライアント上でリプレイできるようなストリームを表します。 それぞれのスロットは、単一のデータベース上の変更操作の連鎖をストリームとして流します。

注記

<para><productname>PostgreSQL</productname> also has streaming replication slots (see <xref linkend="streaming-replication"/>), but they are used somewhat differently there.

またPostgreSQLには、ストリーミングレプリケーションスロットがあります (27.2.5参照)。しかし、ここでの説明とは少し違う使い方がされています。

A replication slot has an identifier that is unique across all databases in a <productname>PostgreSQL</productname> cluster. Slots persist independently of the connection using them and are crash-safe. それぞれのレプリケーションスロットはPostgreSQLクラスタの中で一意な識別子を持っています。 スロットは、そのために使用される接続とは独立しており、クラッシュセーフです。

A logical slot will emit each change just once in normal operation. The current position of each slot is persisted only at checkpoint, so in the case of a crash the slot may return to an earlier LSN, which will then cause recent changes to be sent again when the server restarts. Logical decoding clients are responsible for avoiding ill effects from handling the same message more than once. Clients may wish to record the last LSN they saw when decoding and skip over any repeated data or (when using the replication protocol) request that decoding start from that LSN rather than letting the server determine the start point. The Replication Progress Tracking feature is designed for this purpose, refer to <link linkend="replication-origins">replication origins</link>. ロジカルスロットは、通常の操作においては、各々の変更操作を一度だけ送出します。 それぞれのスロットにおける現在位置は、チェックポイントのときにだけ永続的になります。 ですからクラッシュすると、スロットは以前のLSNに戻ってしまうかもしれませんし、サーバの再起動時には最近の変更が再送されることになります。 ロジカルデコーディングのクライアントは、同じメッセージを複数回扱うことによる好ましくない結果を避けることに対して責任を追っています。 クライアントはデコーディングの際に最後に確認したLSNを記録し、繰り返されるデータをスキップしたり、(レプリケーションプロトコルを使う場合に)サーバに開始時点を決めさせるのではなく、記録しておいたLSNからデコーディングを始めるように要求するかもしれません。 レプリケーション進捗追跡機能はこの目的のために設計されています。 replication originsを参照してください。

Multiple independent slots may exist for a single database. Each slot has its own state, allowing different consumers to receive changes from different points in the database change stream. For most applications, a separate slot will be required for each consumer. 単一のデータベース中に、お互いに独立した複数のスロットが存在しても構いません。 それぞれのスロットは自分自身の状態を持っており、データベース更新のストリーム上の別の場所から変更データを受信する異なる消費者があり得ます。 多くのアプリケーションにとっては、各消費者に対して個別のスロットが必要となるでしょう。

A logical replication slot knows nothing about the state of the receiver(s). It's even possible to have multiple different receivers using the same slot at different times; they'll just get the changes following on from when the last receiver stopped consuming them. Only one receiver may consume changes from a slot at any given time. ロジカルレプリケーションスロットは、受信者の状態については関知しません。 同時にでなければ、同じスロットを使う複数の異なる受信者を持つことさえできます。 その場合は、直近の受信者がストリームの消費を終了した時点から更新データを受信するだけです。 どの時点でも1つのスロットからの変更を消費できるのは1つの受信側だけです。

A logical replication slot can also be created on a hot standby. To prevent <command>VACUUM</command> from removing required rows from the system catalogs, <varname>hot_standby_feedback</varname> should be set on the standby. In spite of that, if any required rows get removed, the slot gets invalidated. It's highly recommended to use a physical slot between the primary and the standby. Otherwise, <varname>hot_standby_feedback</varname> will work but only while the connection is alive (for example a node restart would break it). Then, the primary may delete system catalog rows that could be needed by the logical decoding on the standby (as it does not know about the catalog_xmin on the standby). Existing logical slots on standby also get invalidated if <varname>wal_level</varname> on the primary is reduced to less than <literal>logical</literal>. This is done as soon as the standby detects such a change in the WAL stream. It means that, for walsenders which are lagging (if any), some WAL records up to the <varname>wal_level</varname> parameter change on the primary won't be decoded. 《機械翻訳》ロジカルレプリケーションスロットは、ホットスタンバイ上にも作成できます。 システムカタログから必要な行をVACUUMが削除するのを防ぐためには、スタンバイ上でhot_standby_feedbackを設定する必要があります。 それでも、必要な行が削除されるとスロットは無効になります。 プライマリとスタンバイの間に物理スロットを使用することを強くお勧めします。 そうしないと、hot_standby_feedbackは動作しますが、接続が生きている間だけです(例えばノードの再起動で破壊されます)。 その場合、プライマリはスタンバイ上のロジカルデコーディングが必要とするシステムカタログ行を削除するかもしれません(スタンバイ上のcatalog_xminについては知らないので)。 既存のスタンバイ上のロジカルスロットも、プライマリ上のwal_levellogicalよりも小さくなると無効になります。 これはスタンバイがWALストリームでそのような変更を検出したときにすぐに行われます。 これは、遅延しているWAL送信者がある場合に、プライマリ上のwal_levelパラメータの変更までの間に、一部のWALレコードがデコードされないことを意味します。

Creation of a logical slot requires information about all the currently running transactions. On the primary, this information is available directly, but on a standby, this information has to be obtained from primary. Thus, slot creation may need to wait for some activity to happen on the primary. If the primary is idle, creating a logical slot on standby may take noticeable time. This can be sped up by calling the <function>pg_log_standby_snapshot</function> function on the primary. 《機械翻訳》論理スロットの作成には、現在実行中のすべてのトランザクションに関する情報が必要です。 プライマリでは、この情報は直接使用できますが、スタンバイでは、この情報をプライマリから取得する必要があります。 したがって、スロットの作成は、プライマリで何らかのアクティビティが発生するまで待機する必要があります。 プライマリがアイドル状態の場合、スタンバイで論理スロットを作成するには、pg_log_standby_snapshot関数を呼び出すとかなり時間がかかることがあります。

注意

Replication slots persist across crashes and know nothing about the state of their consumer(s). They will prevent removal of required resources even when there is no connection using them. This consumes storage because neither required WAL nor required rows from the system catalogs can be removed by <command>VACUUM</command> as long as they are required by a replication slot. In extreme cases this could cause the database to shut down to prevent transaction ID wraparound (see <xref linkend="vacuum-for-wraparound"/>). So if a slot is no longer required it should be dropped. レプリケーションスロットは、クラッシュをまたがって永続し、消費者の状態については関知しません。 スロットを使う接続がない場合でも、消費者が必要としているリソースが削除されることを防ぎます。 これによりストレージが消費されます。何故ならば、関連するWALもシステムカタログの行も、レプリケーションスロットが必要とする限りVACUUMによって削除されないからです。 極端な場合、トランザクションIDの周回(25.1.5を参照)を防ぐためのデータベース停止をもたらす可能性があります。 したがって、必要でなくなったスロットは削除すべきです。

49.2.3. 出力プラグイン #

<title>Output Plugins</title>

Output plugins transform the data from the write-ahead log's internal representation into the format the consumer of a replication slot desires. 出力プラグインは、書き込み先行ログの内部データ表現を、レプリケーションスロットの消費者が必要とする形式に変換します。

49.2.4. スナップショットのエクスポート #

<title>Exported Snapshots</title>

When a new replication slot is created using the streaming replication interface (see <xref linkend="protocol-replication-create-replication-slot"/>), a snapshot is exported (see <xref linkend="functions-snapshot-synchronization"/>), which will show exactly the state of the database after which all changes will be included in the change stream. This can be used to create a new replica by using <link linkend="sql-set-transaction"><literal>SET TRANSACTION SNAPSHOT</literal></link> to read the state of the database at the moment the slot was created. This transaction can then be used to dump the database's state at that point in time, which afterwards can be updated using the slot's contents without losing any changes. ストリーミングレプリケーションのインタフェースを使って新しいスロットを作ると(CREATE_REPLICATION_SLOT参照)、スナップショットがエクスポートされます(9.27.5参照)。 このスナップショットはまさにその時点でのデータベースの状態を示しており、スナップショット以後のすべての変更は更新ストリームに含まれるようになります。 このことを利用して、スロットが作られた際のデータベースの状態をSET TRANSACTION SNAPSHOTを使って読み込むことにより、新しいレプリカを作ることができます。 このトランザクションは、その時点のデータベースの状態をダンプするために使用することができます。 また、スロットに含まれるデータを使って、ダンプした後で行われた更新を失うことなくデータベースを更新できます。

Creation of a snapshot is not always possible. In particular, it will fail when connected to a hot standby. Applications that do not require snapshot export may suppress it with the <literal>NOEXPORT_SNAPSHOT</literal> option. スナップショットの作成はいつでも可能なわけではありません。 とりわけ、ホットスタンバイに接続するときは失敗します。 スナップショットのエクスポートが必要ないアプリケーションは、NOEXPORT_SNAPSHOTオプションを使ってスナップショットのエクスポートを抑止できます。