Using PeerDB
Peer Creation
The building block of a PeerDB deployment is a peer, which could represent a database from a wide variety of providers. We can query these peers using the same Postgres dialect of SQL and also run mirrors to migrate data between two peers. To begin with, let us create two Postgres peers on the same cluster that PeerDB uses for internal state management. If you have another Postgres cluster, you can use that as well! Just be sure to substitute the connection details wherever needed. Let’s begin by connecting to the PeerDB internal cluster [which we will call catalog from now on]:peerone
and create some sample tables.
peerone
.
CDC Mirror
Now imagine the two tables we created inpeerone
have a lot of records being inserted, updated and deleted concurrently. We want a live copy of these tables in peertwo
. We don’t want to change the data in any way, just Extract it from peerone
and Load it in peertwo
.
PeerDB supports this via CDC mirrors, which builds on Postgres’ logical replication capablities and allows efficient synchronization between the state of two tables.
peertwo
.
pgbench
which is a standard tool for Postgres testing. It usually comes bundled with psql
, so it should already be present on your system.
pgbench
to run each script on 8 parallel connections on 2 threads, for a time period of 120 seconds and a progress report every 60 seconds.]
[NOTE: Windows Powershell or Command Prompt users cannot run commands in parallel like this, there are other ways but it would probably be easier to run them in 3 seperate terminals.]
As soon as the rows are inserted into the source tables, PeerDB will start receiving logical replication messages and perform the same operations on the destination tables. We can observe the process from psql in the other terminal.
QRep Mirror
After some brainstorming, we realize that having a copy of both tables is unnecessary when all we do is run this one query against them.peerone
and peertwo
in the table we created.
pgbench
scripts from earlier and the changes should reflect in qrep_dst
. Also since our CDC mirror from before is still running, the new rows will reflect there as well.