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

27.1. 様々な解法の比較 #

<title>Comparison of Different Solutions</title>
共有ディスクを用いたフェイルオーバー

Shared disk failover avoids synchronization overhead by having only one copy of the database. It uses a single disk array that is shared by multiple servers. If the main database server fails, the standby server is able to mount and start the database as though it were recovering from a database crash. This allows rapid failover with no data loss. データベースのコピーを 1つだけ保有すればよいため、共有ディスクを用いたフェイルオーバーは同期によるオーバーヘッドを回避できます。 本解法では、複数のサーバが単一のディスクアレイを共有します。 主データベースサーバが故障したとき、まるでデータベースの破損から復旧したように、スタンバイサーバが元のデータベースを実装して稼働できます。 これはデータの消失がない高速なフェイルオーバーを行うことができます。

Shared hardware functionality is common in network storage devices. Using a network file system is also possible, though care must be taken that the file system has full <acronym>POSIX</acronym> behavior (see <xref linkend="creating-cluster-nfs"/>). One significant limitation of this method is that if the shared disk array fails or becomes corrupt, the primary and standby servers are both nonfunctional. Another issue is that the standby server should never access the shared storage while the primary server is running. ハードウェアを共有するという機能は、ネットワーク上の記憶装置では一般的です。 ネットワークファイルシステムの利用も可能ですが、そのファイルシステムがPOSIX仕様を満たしているか注意してください。 ( 19.2.2.1を見てください)。 本解法には重大な制約があり、共有ディスクアレイが故障または破損したとき、プライマリサーバもスタンバイサーバも機能しなくなります。 また、プライマリサーバが稼働している間は、スタンバイサーバが共有記憶装置にアクセスしてはなりません。

ファイルシステム(ブロックデバイス)レプリケーション

A modified version of shared hardware functionality is file system replication, where all changes to a file system are mirrored to a file system residing on another computer. The only restriction is that the mirroring must be done in a way that ensures the standby server has a consistent copy of the file system &mdash; specifically, writes to the standby must be done in the same order as those on the primary. <productname>DRBD</productname> is a popular file system replication solution for Linux. ハードウェア共有機能の改善の一つとしてファイルシステムのレプリケーションをあげることができます。 それは、あるファイルシステムに対して行われたすべての変更を他のコンピュータに存在するファイルシステムにミラーリングします。 制約はただ一つであり、スタンバイサーバがファイルシステムの一貫したコピーを自身の領域に持つようにミラーリングしなければなりません。具体的には、スタンバイサーバへの書き込みがプライマリサーバへの書き込みと同じ順序でおこなわれなければなりません。 LinuxにおけるDRBDは、ファイルシステムレプリケーションで広く受けいれられている手法です。

https://forge.continuent.org/pipermail/sequoia/2006-November/004070.html Oracle RAC is a shared disk approach and just send cache invalidations to other nodes but not actual data. As the disk is shared, data is only committed once to disk and there is a distributed locking protocol to make nodes agree on a serializable transactional order.
先行書き込みログシッピング

Warm and hot standby servers can be kept current by reading a stream of write-ahead log (<acronym>WAL</acronym>) records. If the main server fails, the standby contains almost all of the data of the main server, and can be quickly made the new primary database server. This can be synchronous or asynchronous and can only be done for the entire database server. ウォームスタンバイおよびホットスタンバイサーバは、ログ先行書き込み(WAL)のレコードを解読して最新の状態を保持できます。 プライマリサーバが故障したとき、スタンバイサーバがプライマリサーバのほぼすべてのデータを保存して、速やかに新しいプライマリデータベースサーバを作成できます。 本解法は同期、非同期で行うことができ、データベース全体だけを範囲として処理できます。

A standby server can be implemented using file-based log shipping (<xref linkend="warm-standby"/>) or streaming replication (see <xref linkend="streaming-replication"/>), or a combination of both. For information on hot standby, see <xref linkend="hot-standby"/>. スタンバイサーバは、ファイル単位のログシッピング(27.2参照)またはストリーミングレプリケーション(27.2.5参照)または両者の併用を使用して実装できます。 ホットスタンバイの情報は 27.4 を参照してください。

論理レプリケーション

Logical replication allows a database server to send a stream of data modifications to another server. <productname>PostgreSQL</productname> logical replication constructs a stream of logical data modifications from the WAL. Logical replication allows replication of data changes on a per-table basis. In addition, a server that is publishing its own changes can also subscribe to changes from another server, allowing data to flow in multiple directions. For more information on logical replication, see <xref linkend="logical-replication"/>. Through the logical decoding interface (<xref linkend="logicaldecoding"/>), third-party extensions can also provide similar functionality. 論理レプリケーションにより、データベースサーバが他のサーバに、データ更新のストリームを送ることができます。 PostgreSQLの論理レプリケーションは、WALから論理的なデータ更新のストリームを構築します。 論理レプリケーションでは、テーブル単位でデータ変更をレプリケーションすることができます。 さらに自分の変更をパブリッシュしているサーバは同時に他のサーバから変更をサブスクライブできるので、複数の方向にデータを流すことができます。 論理レプリケーションの更なる情報については、第31章をご覧ください。 なお、ロジカルデコーディングインタフェース(第49章)を使って、サードパーティー拡張は同様の機能を提供できます。

トリガベースのプライマリ・スタンバイレプリケーション

A trigger-based replication setup typically funnels data modification queries to a designated primary server. Operating on a per-table basis, the primary server sends data changes (typically) asynchronously to the standby servers. Standby servers can answer queries while the primary is running, and may allow some local data changes or write activity. This form of replication is often used for offloading large analytical or data warehouse queries. トリガベースのレプリケーションの構成では、通常はデータを変更する問い合わせを指定されたプライマリサーバに送り込みます。 テーブル単位で処理を行い、プライマリサーバはデータの変更を(典型的には)非同期でスタンバイサーバに送信します。 スタンバイサーバは、プライマリが処理中に問い合わせに応答し、ローカルでのデータ変更あるいは書き込み処理を行うことができます。 この形式のレプリケーションは、大量のデータ分析の負荷軽減や、データウェアハウスの問い合わせにしばしば利用されます。

<productname>Slony-I</productname> is an example of this type of replication, with per-table granularity, and support for multiple standby servers. Because it updates the standby server asynchronously (in batches), there is possible data loss during fail over. この種類のレプリケーションの一例はSlony-Iであり、テーブル単位の粒度を持ち、複数のスタンバイサーバが稼働できます。 (バッチ処理によって)スタンバイサーバのデータを非同期で更新するため、フェイルオーバーにおけるデータ消失の可能性があります。

SQLに基づいたレプリケーションのミドルウェア

With SQL-based replication middleware, a program intercepts every SQL query and sends it to one or all servers. Each server operates independently. Read-write queries must be sent to all servers, so that every server receives any changes. But read-only queries can be sent to just one server, allowing the read workload to be distributed among them. SQLに基づいたレプリケーションのミドルウェアでは、プログラムがすべてのSQL問い合わせを採取して、1つまたはすべてのサーバに送付します。 なお、各々のサーバは独立して稼働します。 読み書き問い合わせは、すべてのサーバがすべての変更を受け取るように全サーバに送付されなければなりません。 しかし、読み取り専用の問い合わせはサーバ全体の読み取り負荷を分散させるために、1つのサーバだけに送付することができます。

If queries are simply broadcast unmodified, functions like <function>random()</function>, <function>CURRENT_TIMESTAMP</function>, and sequences can have different values on different servers. This is because each server operates independently, and because SQL queries are broadcast rather than actual data changes. If this is unacceptable, either the middleware or the application must determine such values from a single source and then use those values in write queries. Care must also be taken that all transactions either commit or abort on all servers, perhaps using two-phase commit (<xref linkend="sql-prepare-transaction"/> and <xref linkend="sql-commit-prepared"/>). <productname>Pgpool-II</productname> and <productname>Continuent Tungsten</productname> are examples of this type of replication. 問い合わせを修正しないで送付した場合、random()関数による乱数値とCURRENT_TIMESTAMP関数による現在時刻およびシーケンス値が、サーバごとに異なることがあります。 その理由は、各サーバが独立して稼働しているため、および、実際のデータ変更ではなくSQL問い合わせが送信されるからです。 これが許容できない場合は、ミドルウェアかアプリケーションで単一のソースからそのような値を確定し、その結果を書き込み問い合わせで使用しなければなりません。 トランザクションをコミットするか中断するかについても、全サーバが同一となるよう注意しなければなりません。 これには2相コミット(PREPARE TRANSACTIONおよびCOMMIT PREPARED)を使用することになるでしょう。 Pgpool-IIContinuent Tungstenがこのレプリケーションの一例です。

非同期マルチマスタレプリケーション

For servers that are not regularly connected or have slow communication links, like laptops or remote servers, keeping data consistent among servers is a challenge. Using asynchronous multimaster replication, each server works independently, and periodically communicates with the other servers to identify conflicting transactions. The conflicts can be resolved by users or conflict resolution rules. Bucardo is an example of this type of replication. ラップトップやリモートマシンのように、通常は接続されていない、あるいは遅い通信リンクで接続されているサーバ間において、データの一貫性を保持することは挑戦的な課題です。 非同期マルチマスタレプリケーションの使用により、全サーバの独立した稼働、およびトランザクションの衝突を識別するための定期的な通信を実現します。 トランザクションの衝突は、利用者および衝突回避法によって解決できるでしょう。 Bucardoはこの種のレプリケーションの一例です。

同期マルチマスタレプリケーション

In synchronous multimaster replication, each server can accept write requests, and modified data is transmitted from the original server to every other server before each transaction commits. Heavy write activity can cause excessive locking and commit delays, leading to poor performance. Read requests can be sent to any server. Some implementations use shared disk to reduce the communication overhead. Synchronous multimaster replication is best for mostly read workloads, though its big advantage is that any server can accept write requests &mdash; there is no need to partition workloads between primary and standby servers, and because the data changes are sent from one server to another, there is no problem with non-deterministic functions like <function>random()</function>. 同期マルチマスタレプリケーションでは全てのサーバが書き込み要求を受理できます。 受理したサーバは更新したデータを、トランザクションをコミットする前に、他の全サーバへ配布します。 書き込み負荷が重いとき、ロックの掛かり過ぎやコミットの遅延による作業効率の低下の原因となりえます。 読み取り要求はどのサーバにも送付できます。 通信による負荷を減らすには、共有ディスクが実装されます。 同期マルチマスタレプリケーションは、主に読み取り作業負荷の低減に最適ですが、全てのサーバが書き込み要求を受理できることも大きな利点です。 その利点とは、プライマリとスタンバイ間で作業負荷を分けなくてよいこと、および更新データが1つのサーバから他のサーバに送付されるため、出力が確定しないrandom()関数などによる問題が起こらないことです。

<productname>PostgreSQL</productname> does not offer this type of replication, though <productname>PostgreSQL</productname> two-phase commit (<xref linkend="sql-prepare-transaction"/> and <xref linkend="sql-commit-prepared"/>) can be used to implement this in application code or middleware. PostgreSQL では、この種類のレプリケーションを提供しません。 しかし、PostgreSQL の 2相コミット(PREPARE TRANSACTIONおよびCOMMIT PREPARED)を使用すれば、アプリケーションのコードまたはミドルウェアにおいて本解法を実装できます。

<xref linkend="high-availability-matrix"/> summarizes the capabilities of the various solutions listed above. 表 27.1は上述した種々の解法の機能を要約したものです。

表27.1 高可用性、負荷分散およびレプリケーションの特徴

<title>High Availability, Load Balancing, and Replication Feature Matrix</title>
特徴共有ディスクファイルシステムのレプリケーション先行書き込みログシッピング論理レプリケーショントリガに基づいたレプリケーションSQLに基づいたレプリケーションのミドルウェア非同期マルチマスタレプリケーション同期マルチマスタレプリケーション
一般的な例NASDRBD組み込みストリーミングレプリケーション組み込み論理レプリケーション、pglogicalLondiste、Slonypgpool-IIBucardo 
通信方法共有ディスクディスクブロックWALロジカルデコーディングテーブル行SQLテーブル行テーブル行および行ロック
特別なハードウェアが不要 
複数のプライマリサーバが可能    
プライマリサーバにオーバーヘッドがない    
複数のサーバを待たない 同期が無効の場合同期が無効の場合  
プライマリの故障によるデータ損失がない同期が有効の場合同期が有効の場合  
レプリカは読み取り専用問い合わせを受理可能  ホットスタンバイ使用時
テーブルごとの粒度    
コンフリクトの回避が不要  

There are a few solutions that do not fit into the above categories: 上の分類に該当しない解法もあります。

データの分割

Data partitioning splits tables into data sets. Each set can be modified by only one server. For example, data can be partitioned by offices, e.g., London and Paris, with a server in each office. If queries combining London and Paris data are necessary, an application can query both servers, or primary/standby replication can be used to keep a read-only copy of the other office's data on each server. データの分割とは、同じテーブルのデータを複数部分に分けることです。 各部分に書き込むことができるのは、1つのサーバだけです。 例えば、データをロンドンとパリの営業所用に分割でき、サーバをロンドンとパリのどちらにも設置できた状態を考えます。 問い合わせにロンドンとパリのデータが混在した場合、アプリケーションは両方のサーバに問い合わせることができます。 または、プライマリ/スタンバイレプリケーションを使用して、他の営業所のデータを読み取り専用コピーとして保持できます。

複数サーバによる問い合わせの並列実行

Many of the above solutions allow multiple servers to handle multiple queries, but none allow a single query to use multiple servers to complete faster. This solution allows multiple servers to work concurrently on a single query. It is usually accomplished by splitting the data among servers and having each server execute its part of the query and return results to a central server where they are combined and returned to the user. This can be implemented using the <productname>PL/Proxy</productname> tool set. 上述した多くの解法は、複数のサーバが複数の問い合わせを処理するものです。 処理速度の向上のために、単一の問い合わせに複数のサーバを使用するものはありません。 本解法は複数のサーバが単一の問い合わせを共同して実行するものです。 その方法は、データをサーバ間で分割し、各サーバが部分的に問い合わせを実行し、各々の結果をプライマリサーバに送付し、プライマリサーバが合体して利用者に返送するものです。 これはPL/Proxyツールセットを使用して実装できます。

It should also be noted that because <productname>PostgreSQL</productname> is open source and easily extended, a number of companies have taken <productname>PostgreSQL</productname> and created commercial closed-source solutions with unique failover, replication, and load balancing capabilities. These are not discussed here. また、PostgreSQLはオープンソースで、容易に拡張できるので、多くの企業がPostgreSQLをもとにして、独自のフェイルオーバー、レプリケーション、負荷分散機能を備えたクローズドソースの製品を開発していることに注意してください。 これらについては、ここでは説明しません。