Easily setup logical replication and switchover to new database with minimal downtime
Easily setup logical replication and switchover to new database with minimal downtime
pg_easy_replicate is a CLI orchestrator tool that simplifies the process of setting up logical replication between two PostgreSQL databases. pg_easy_replicate also supports switchover. After the source (primary database) is fully replicated, pg_easy_replicate puts it into read-only mode and via logical replication flushes all data to the new target database. This ensures zero data loss and minimal downtime for the application. This method can be useful for performing minimal downtime (up to [-l ]
This command will display a list of DDL changes in JSON format;
[ { "id": 1, "group_name": "cluster-1", "event_type": "ddl_command_end", "object_type": "table", "object_identity": "public.pgbench_accounts", "ddl_command": "ALTER TABLE public.pgbench_accounts ADD COLUMN test_column VARCHAR(255)", "created_at": "2024-08-31 15:42:33 UTC" } ]
#### Applying DDL Changes
`pg_easy_replicate` won't automatically apply the changes for you. To apply the tracked DDL changes to the target database:
```bash
$ pg_easy_replicate apply_ddl_change -g [-i ]If you specify a change ID with the -i option, only that specific change will be applied. If you don't specify an ID, you'll be prompted to apply all pending changes.
$ pg_easy_replicate apply_ddl_change -g cluster-1
The following DDL changes will be applied:
ID: 1, Type: table, Command: ALTER TABLE public.pgbench_accounts ADD COLUMN test_column VARCHAR(255)...
Do you want to apply all these changes? (y/n): y
...
All pending DDL changes applied successfully.You can inspect or watch stats any time during the sync process. The stats give you an idea of when the sync started, current flush/write lag, how many tables are in replicating, copying or other stages, and more.
You can poll these stats to perform any other after the switchover is done. The stats include a switchover_completed_at which is updated once the switch over is complete.
$ pg_easy_replicate stats --group-name database-cluster-1
{
"lag_stats": [
{
"pid": 66,
"client_addr": "192.168.128.2",
"user_name": "jamesbond",
"application_name": "pger_subscription_database_cluster_1",
"state": "streaming",
"sync_state": "async",
"write_lag": "0.0",
"flush_lag": "0.0",
"replay_lag": "0.0"
}
],
"message_lsn_receipts": [
{
"received_lsn": "0/1674688",
"last_msg_send_time": "2023-06-19 19:56:35 UTC",
"last_msg_receipt_time": "2023-06-19 19:56:35 UTC",
"latest_end_lsn": "0/1674688",
"latest_end_time": "2023-06-19 19:56:35 UTC"
}
],
"sync_started_at": "2023-06-19 19:54:54 UTC",
"sync_failed_at": null,
"switchover_completed_at": null
....You can send stats to an endpoint on an interval using notify. This can be configured to receieve the stats to this url on a frequency (default 10s). A timeout can also be configured for the request to the endpoint (default 10s). This gives you greater control over processing different events in the replication cycle in your workflow.
$ pg_easy_replicate notify --group-name database-cluster-1 --url https://example.com/webhook --frequency 10 --timeout 10
…
bash
$ pg_easy_replicate switchover --group-name database-cluster-1
{"name":"pg_easy_replicate","hostname":"PKHXQVK6DW","pid":24192,"level":30,"time":"2023-06-19T16:05:23.033-04:00","v":0,"msg":"Watching lag stats","version":"0.1.0"}
...By default all tables are added for replication but you can create multiple groups with custom tables for the same database. Example
$ pg_easy_replicate bootstrap --group-name database-cluster-1 --copy-schema
$ pg_easy_replicate start_sync --group-name database-cluster-1 --schema-name public --tables "users,posts,events"
...
$ pg_easy_replicate bootstrap --group-name database-cluster-2 --copy-schema
$ pg_easy_replicate start_sync --group-name database-cluster-2 --schema-name public --tables "comments,views"
...
$ pg_easy_replicate switchover --group-name database-cluster-1
$ pg_easy_replicate switchover --group-name database-cluster-2
...By default all tables are added for replication but you can exclude tables if necessary. Example
...
$ pg_easy_replicate bootstrap --group-name database-cluster-1 --copy-schema
$ pg_easy_replicate start_sync --group-name database-cluster-1 --schema-name public --exclude_tables "events"
...Use cleanup if you want to remove all bootstrapped data for the specified group. Additionally you can pass -e or --everything in order to clean up all schema changes for bootstrapped tables, users and any publication/subscription data.
$ pg_easy_replicate cleanup --group-name database-cluster-1 --everything
{"name":"pg_easy_replicate","hostname":"PKHXQVK6DW","pid":24192,"level":30,"time":"2023-06-19T16:05:23.033-04:00","v":0,"msg":"Dropping groups table","version":"0.1.0"}
{"name":"pg_easy_replicate","hostname":"PKHXQVK6DW","pid":24192,"level":30,"time":"2023-06-19T16:05:23.033-04:00","v":0,"msg":"Dropping schema","version":"0.1.0"}
...For minimal downtime, it'd be best to watch/tail the stats and wait until switchover_completed_at is updated with a timestamp. Once that happens you can perform any of the following strategies. Note: These are just suggestions and pg_easy_replicate doesn't provide any functionalities for this.
In this strategy, you have a change ready to go which instructs your application to start connecting to the new database. Either using an environment variable or similar. Depending on the application type, it may or may not require a rolling restart.
Next, you can set up a program that watches the stats and waits until switchover_completed_at is reporting as true. Once that happens it kicks off a rolling restart of your application containers so they can start making connections to the DNS of the new database.
In this strategy, you have a weighted based DNS system (example AWS Route53 weighted records) where 100% of traffic goes to a primary origin and 0% to a secondary origin. The primary origin here is the DNS host for your source database and secondary origin is the DNS host for your target database. You can set up your application ahead of time to interact with the database using DNS from the weighted group.
Next, you can set up a program that watches the stats and waits until switchover_completed_at is reporting as true. Once that happens it updates the weight in the DNS weighted group where 100% of the requests now go to the new/target database. Note: Keeping a low ttl is recommended.
pg_hba or pgBouncer userlistpg_easy_replicate sets up a designated user for managing the replication process. In case you handle user permissions through pg_hba, it's necessary to modify this list to permit sessions from pger_su_h1a4fb. Similarly, with pgBouncer, you'll need to authorize pger_su_h1a4fb for login access by including it in the userlist.
PRs most welcome. You can get started locally by
docker compose down -v && docker compose up --remove-orphans --build3.4.4 using RVM (instruction)bundle exec rspec for specsNo open issues yet, or sync has not completed.