Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to limit WAL size when using Postgres Logical Replication Slot?

I am creating replication slot and streaming changes from AWS Postgres RDS to java process through JDBC driver.

My replication slot creation code looks like this.

final ReplicationSlotInfo replicationSlotInfo = pgConnection.getReplicationAPI()
                    .createReplicationSlot()
                    .logical()
                    .withSlotName(replicationSlotName)
                    .withOutputPlugin("wal2json")
                    .make();

and I get replication stream using following code.

pgConnection.getReplicationAPI()
                .replicationStream()
                .logical()
                .withSlotName(replicationSlotName)
                .withSlotOption("include-xids", true)
                .withSlotOption("include-timestamp", true)
                .withSlotOption("pretty-print", false)
                .withSlotOption("add-tables", "public.users")
                .withStatusInterval(10, TimeUnit.SECONDS)
                .start()

When replicator java process is not running, the WAL size gets increased. Here is the query I use to find replication lag.

SELECT
    slot_name,
    pg_size_pretty(pg_xlog_location_diff(pg_current_xlog_location(), restart_lsn)) AS replicationSlotLag,
    active
FROM
    pg_replication_slots;

Output:

slot_name   replicationslotlag  active
data_stream_slot    100 GB  f

This replication lag gets increased beyond RDS Disk, which shuts RDS down.

I thought wal_keep_segments will take care of this, which was set to 32. But it did not work. Is there any other property which I have to set to avoid this situation, even when Java Replication process is not running.

like image 697
dnsh Avatar asked Mar 04 '20 13:03

dnsh


People also ask

What is logical replication slots in Postgres?

A replication slot has an identifier that is unique across all databases in a PostgreSQL cluster. Slots persist independently of the connection using them and are crash-safe. A logical slot will emit each change just once in normal operation.

How does Postgres logical replication work?

Logical replication uses a publish and subscribe model with one or more subscribers subscribing to one or more publications on a publisher node. Subscribers pull data from the publications they subscribe to and may subsequently re-publish data to allow cascading replication or more complex configurations.

What is the difference between physical replication and logical replication?

Logical replication is a method of replicating data objects and their changes based upon their replication identity (usually a primary key). We use the term logical in contrast to physical replication, which uses exact block addresses and byte-by-byte replication.

What causes replication lag Postgres?

The most common reasons for increase in the replica lag are the following: Configuration differences between the primary and replica instances. Heavy write workload on the primary instance. Transactions that are running for a long time.


2 Answers

There is a proposal to allow a logical replication slots WAL retention to be limited. I think that that is just what you need, but it is not clear when/if it will become available.

In the meantime, all you can do is monitor the situation, then then drop the slot if it starts to fall behind too far. Of course this does mean you will have a problem re-establishing synchronization later, but there is no way around that (other than fixing whatever it is that is causing the replication process to go away and/or fall behind).

Since you say the java process is not running, dropping the slot is easy to do. If it were running, but just not keeping up, then you would have to do the sad little dance where you kill the wal sender, then try to drop the slot before it gets restarted (and I don't know how you do that on RDS)

wal_keep_segments is only applicable to physical replication, not logical. And it is for use instead of slots, not in addition to them. If you have both, then WAL is retained until both criteria are met. Indeed that is the problem you are facing; logical replication cannot be done without use of slots the way physical replication can.

like image 120
jjanes Avatar answered Jan 04 '23 02:01

jjanes


wal_keep_segments is irrelevant for logical decoding.

With logical decoding, you always have to use a logical replication slot, which is a data structure which marks a position in the transaction log (WAL), so that the server never discards old WAL segments that logical decoding might still need.

That is why your WAL directory grows if you don't consume the changes.

wal_keep_segments specifies a minimum number of old WAL segments to retain. It is used for purposes like streaming replication, pg_receivewal or pg_rewind.

like image 23
Laurenz Albe Avatar answered Jan 04 '23 00:01

Laurenz Albe