postgresql High Availability solutions summary
Shared Disk Failover
Only one copy of the database on shared file system.
- No data loss if failover happens.
- If the shared disk array fails or becomes corrupt, all database servers are nonfunctional.
- Another issue is that the standby server should never access the shared storage while the primary server is running.
NAS
could be the solution.
File System Replication
Upgraded version of Shared Disk Failover
solution. File system are mirrored to a file system residing on another computer.
- Writes to the standby must be done in the same order as those on the master.
DRBD
is a popular file system replication solution for Linux
Transaction Log Shipping
Keep current by reading a stream of WAL 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 master database server. - This can be synchronous or asynchronous and can only be done for the entire database server.
- A standby server can also be used for read-only queries, in which case it is called a Hot Standby server
File-based log shipping
- Transferring WAL records one file (WAL segment) at a time.
- The bandwidth required for this technique varies according to the transaction rate of the primary server.
- Log shipping is
asynchronous
, i.e., the WAL records are shipped after transaction commit. As a result, there is a window for data loss should the primary server suffer a catastrophic failure; transactions not yet shipped will be lost. The size of the data loss window in file-based log shipping can be limited by use of thearchive_timeout
parameter, which can be set as low as a few seconds. - However such a low setting will substantially increase the bandwidth required for file shipping.
Streaming replication
- Streaming replication allows a standby server to stay more up-to-date than is possible with file-based log shipping.
- The standby connects to the primary, which streams WAL records to the standby as they are generated, without waiting for the WAL file to be filled.
- Streams WAL changes incrementally over a network connection.
- Record-based log shipping is more granular.
- Streaming replication is
asynchronous
bydefault
, in which case there is a small delay between committing a transaction in the primary and the changes becoming visible in the standby. - This delay is however much smaller than with file-based log shipping, typically under one second.
- With streaming replication,
archive_timeout
is not required to reduce the data loss window
Trigger-Based Master-Standby Replication
Master-standby replication setup sends all data modification queries to the master server.
- The master server
asynchronously
sends data changes to the standby server. - The standby can answer read-only queries while the master server is running.
- The standby server is ideal for data warehouse queries.
Slony-I
is an example of this type of replication.- Because it updates the standby server asynchronously (in batches), there is possible data loss during failover.
Statement-Based Replication Middleware
With statement-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.
- If query encounter situation such as
RANDOM()
which each server will generate different results, this might cause problem. Either themiddleware
or the application must query such values from a single server and then use those values in write queries. Another option is to use this replication option with a traditional master-standby setup. - Care must also be taken that all transactions either commit or abort on all servers, perhaps using two-phase commit.
Pgpool-II
and Continuent Tungsten are examples of this type of replication.
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.
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, leading to poor performance, often worse than that of a single server.
- Read requests can be sent to any server.
- Synchronous multimaster replication is best for mostly read workloads.
- though its big advantage is that any server can accept write requests — there is no need to partition workloads between master and standby servers.
- Because the data changes are sent from one server to another, there is no problem with non-deterministic functions like random().
- PostgreSQL does not offer this type of replication, though PostgreSQL two-phase commit (PREPARE TRANSACTION and COMMIT PREPARED) can be used to implement this in application code or middleware.
Data Partitioning
- Data partitioning splits tables into data sets.
- Each set can be modified by only one server.
- If queries combining data from different servers are necessary, an application can query both servers, or master/standby replication can be used to keep a read-only copy of the other data of server on each server.
Multiple-Server Parallel Query Execution
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.
Pgpool-II
has this capability.- Also, this can be implemented using the
PL/Proxy
tool set.
postgresql High Availability solutions summary
https://rug.al/2015/2015-08-02-postgresql-high-availability-solutions-summary/