What Are Postgres Replication Slots?

A Postgres replication slot is a named, logical connection between a replication client and server. It ensures that only one client can connect to a given replication slot at any given time, and that each client receives all of the WAL data written to that slot, in the order it was written.

Replication slots were introduced in PostgreSQL 9.4.

 Exclusive Slots & Free Spins Offers: 

Prior to that, it was possible for multiple clients to connect to the same replication slot on the same server, which could lead to data loss if one of the clients disconnects unexpectedly. Now, with replication slots, each client has its own dedicated connection to the server, and can be sure that it will receive all of the WAL data.

There are two types of replication slots: physical and logical. Physical replication slots are created on the server when it initializes a streaming replication connection with a client.

A physical replication slot is tied to a specific database ID (OID) on the server, and can only be used by that database. A logical replication slot is not tied to any particular database, and can be used by any database on the server.

To create a physical replication slot, use the CREATE PHYSICAL REPLICATION SLOT command:

CREATE PHYSICAL REPLICATION SLOT myslot;

This will create a new physical replication slot named “myslot”. The next time you initialize a streaming replication connection with this server, you must use this slot name.

If you try to use a different slot name, you will get an error.

To create a logical replication slot, use the CREATE LOGICAL REPLICATION SLOT command:

CREATE LOGICAL REPLICATION SLOT myslot;

This will create a new logical replication slot named “myslot”. You can use this slot name when initializing streaming replicaiton with any database on the server.