Set up logical replication to Aiven for PostgreSQL®
Aiven for PostgreSQL® represents an ideal managed solution for a variety of use cases; remote production systems can be completely migrated to Aiven using different methods including using Aiven-db-migrate or the standard dump and restore method.
Whether you are migrating or have another use case to keep an existing system in sync with an Aiven for PostgreSQL service, you can address that by setting up a logical replica and replicating tables from a self-managed PostgreSQL cluster to Aiven.
This content also works with AWS RDS PostgreSQL 10+ and Google CloudSQL PostgreSQL.
Variables
These are the placeholders you will need to replace in the code sample:
| Variable | Description |
|---|---|
SRC_HOST | Hostname of the source PostgreSQL database |
SRC_PORT | Port of the source PostgreSQL database |
SRC_DATABASE | Database Name of the source PostgreSQL database |
SRC_USER | Username of the source PostgreSQL database |
SRC_PASSWORD | Password of the source PostgreSQL database |
SRC_CONN_URI | Connection URI of the source PostgreSQL database |
Requirements
- PostgreSQL version 10 or newer
- Connection between the source cluster's PostgreSQL port and Aiven for PostgreSQL cluster
- Access to an superuser role on the source cluster
wal_levelsetting tologicalon the source cluster. To verify and change thewal_levelsetting, see the instructions on setting this configuration.
If you are using an AWS RDS PostgreSQL cluster as source, the
rds.logical_replication parameter must be set to 1 (true) in the
parameter group.
Set up the replication
To create a logical replication, there is no need to install any extensions on the source cluster, but a superuser account is required.
The aiven_extras extension enables the creation of a
publish/subscribe-style logical replication without a superuser account,
and it is preinstalled on Aiven for PostgreSQL servers. For more information on
aiven_extras, see the dedicated GitHub
repository. The following
example assumes the aiven_extras extension is not available in the
source PostgreSQL database.
This example assumes a source database called origin_database on a
self-managed PostgreSQL cluster. The replication will mirror three
tables, named test_table, test_table_2, and test_table_3, to the
defaultdb database on Aiven for PostgreSQL. The process to setup the
logical replication is the following:
-
On the source cluster, connect to the
origin_databasewithpsql. -
Create the
PUBLICATIONentry, namedpub_source_tables, for the test tables:CREATE PUBLICATION pub_source_tables
FOR TABLE test_table,test_table_2,test_table_3
WITH (publish='insert,update,delete');tipIn PostgreSQL 10 and above,
PUBLICATIONentries define the tables to be replicated, which are in turnSUBSCRIBEDto by the receiving database.When creating a publication entry, the
publishparameter defines the operations to transfer. In this example, all theINSERT,UPDATE, orDELETEoperations will be transferred. -
PostgreSQL's logical replication doesn't copy table definitions, that can be extracted from the
origin_databasewithpg_dumpand included in aorigin-database-schema.sqlfile with:pg_dump --schema-only --no-publications \
SRC_CONN_URI \
-t test_table -t test_table_2 -t test_table_3 > origin-database-schema.sql -
Connect via
psqlto the destination Aiven for PostgreSQL database and create the newaiven_extrasextension:CREATE EXTENSION aiven_extras CASCADE; -
Create the table definitions in the Aiven for PostgreSQL destination database within
psql:\i origin-database-schema.sql -
Create a
SUBSCRIPTIONentry, nameddest_subscription, in the Aiven for PostgreSQL destination database to start replicating changes from the sourcepub_source_tablespublication:SELECT * FROM
aiven_extras.pg_create_subscription(
'dest_subscription',
'host=SRC_HOST password=SRC_PASSWORD port=SRC_PORT dbname=SRC_DATABASE user=SRC_USER',
'pub_source_tables',
'dest_slot',
TRUE,
TRUE); -
Verify that the subscription has been created successfully. As the
pg_subscriptioncatalog is superuser-only, you can use theaiven_extras.pg_list_all_subscriptions()function from theaiven_extrasextension:SELECT subdbid, subname, subowner, subenabled, subslotname
FROM aiven_extras.pg_list_all_subscriptions();
subdbid | subname | subowner | subenabled | subslotname
---------+-------------------+----------+------------+-------------
16401 | dest_subscription | 10 | t | dest_slot
(1 row) -
Verify the subscription status:
SELECT * FROM pg_stat_subscription;
subid | subname | pid | relid | received_lsn | last_msg_send_time | last_msg_receipt_time | latest_end_lsn | latest_end_time
-------+-------------------+-----+-------+--------------+-------------------------------+-------------------------------+----------------+-------------------------------
16444 | dest_subscription | 869 | | 0/C002360 | 2021-06-25 12:06:59.570865+00 | 2021-06-25 12:06:59.571295+00 | 0/C002360 | 2021-06-25 12:06:59.570865+00
(1 row) -
Verify the data is correctly copied over the Aiven for PostgreSQL target tables.
Remove unused replication setup
It is important to remove unused replication setups since the underlying replication slots in PostgreSQL forces the server to keep all the data needed to replicate since the publication creation time. If the data stream has no readers, there will be an ever-growing amount of data on disk until it becomes full.
To remove an unused subscription, essentially stopping the replication, run the following command in the Aiven for PostgreSQL target database:
-
When the source database is accessible
SELECT * FROM aiven_extras.pg_drop_subscription('dest_subscription'); -
When there is no access to the source database
SELECT * FROM aiven_extras.pg_drop_subscription('dest_subscription', FALSE);
Verify the replication removal with:
SELECT * FROM aiven_extras.pg_list_all_subscriptions();
subdbid | subname | subowner | subenabled | subconninfo | subslotname | subsynccommit | subpublications
---------+---------+----------+------------+-------------+-------------+---------------+-----------------
(0 rows)