We have a PostgreSQL cluster running on one master (db1
) and two async hot-standby servers (db2
and db3
) replicating from it. All the servers are working as a backend for a web service and all the read/write transactions connect to db1
and all read-only transactions are executed on hot-standby servers if possible. The clients are smart enough to fallback to talking to master if hot-standby servers are too delayed or down. All the servers have identical hardware so it doesn't matter which one is the master server.
We're planning to upgrade the PostgreSQL to newer major version and want to do that with minimal downtime.
We're currently using binary/physical streaming replication but that replication mode only supports running all servers on the same major version.
I know that logical streaming replication can be used to synchronize data between different major versios so to upgrade the whole system to more recent release, I've following idea:
- Start publishing changes on
db1
over logical replication: create a new logical replication slot calleddb1_to_db3
for this purpose. - Take
db3
down, upgrade the PostgreSQL to next major versio we want to use. - Clear the whole database and manually copy the database schema from
db1
todb3
. - Configure
db3
as hot-standby and subscribe to slowdb1_to_db3
. - Wait for
db3
to eventually synchronize. - Then do the same thing for
db2
except make it synchronize fromdb3
. - Then promote
db3
as the new master. Only do it now that we have at least two servers (db3
anddb2
) with the full database to avoid lots of downtime in case of server hardware failure. - Finally upgrade the PostgreSQL to the next major version on
db1
and add it as a hot-standby replica fordb3
.
However, I have following open questions:
- Does creating the logical replication slot on
db1
guarantee thatdb3
can be syncronized from scratch? - Does publishing the logical slot increase WAL log disk usage? If so, how much (e.g. +100%?)
- How to switch back from logical replication to binary replication in the future? I know that we don't want to use logical replication in long run because it doesn't handle DDL changes (schema changes).
- Is it possible to lose transactions with logical replication? How to verify the databases actually have the same data?
In case it makes a difference, we're planning to upgrade from version 12.x to 14.x.
pg_upgrade --link
. However, that doesn't have recovery plan in casepg_uprade
failed for any reason: dba.stackexchange.com/a/301517/29183pg_basebackup
. That's okay but simply takes more time than I was hoping for.