Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patroni: Add permanent replication slots #614

Merged
merged 3 commits into from
Mar 28, 2024
Merged

Patroni: Add permanent replication slots #614

merged 3 commits into from
Mar 28, 2024

Conversation

vitabaks
Copy link
Owner

@vitabaks vitabaks commented Mar 28, 2024

Add the ability to define permanent replication slots.

  • Variable: patroni_slots (by default, the value is not defined)

Example:

patroni_slots:
  - slot: "<logical_replication_slot_name>" # the name of the permanent replication slot.
    type: "logical" # the type of slot. Could be physical or logical (if the slot is logical, you have to define 'database' and 'plugin').
    plugin: "pgoutput" # the plugin name for the logical slot.
    database: "<my_db_name>" # the database name where logical slots should be created.

These slots will be preserved during switchover/failover. Permanent slots that don’t exist will be created by Patroni.

How it works:

With PostgreSQL 11 onwards permanent physical slots are created on all nodes and their position is advanced every loop_wait seconds. For PostgreSQL versions older than 11 permanent physical replication slots are maintained only on the current primary. The logical slots are copied from the primary to a standby with restart, and after that their position advanced every loop_wait seconds (if necessary). Copying logical slot files performed via libpq connection and using either rewind or superuser credentials.

There is always a chance that the logical slot position on the replica is a bit behind the former primary, therefore application should be prepared that some messages could be received the second time after the failover.

Permanent replication slots are synchronized only from the primary/standby_leader to replica nodes. That means, applications are supposed to be using them only from the leader node. Using them on replica nodes will cause indefinite growth of pg_wal on all other nodes in the cluster.

Doc: https://patroni.readthedocs.io/en/latest/dynamic_configuration.html

Additionally

  • Set 'wal_level=logical' by default

@vitabaks vitabaks added the enhancement Improvement of the current functionality label Mar 28, 2024
@vitabaks vitabaks self-assigned this Mar 28, 2024
@vitabaks
Copy link
Owner Author

vitabaks commented Mar 28, 2024

Test

Vars:

patroni_slots:
  - slot: "logical_replication_slot"
    type: "logical"
    plugin: "pgoutput"
    database: "postgres"

Result:

--check slot on Primary
postgres=# select * from pg_replication_slots where slot_type = 'logical'\gx
-[ RECORD 1 ]-------+-------------------------
slot_name           | logical_replication_slot
plugin              | pgoutput
slot_type           | logical
datoid              | 5
database            | postgres
temporary           | f
active              | f
active_pid          | 
xmin                | 
catalog_xmin        | 733
restart_lsn         | 0/172B808
confirmed_flush_lsn | 0/172B840
wal_status          | reserved
safe_wal_size       | 
two_phase           | f
conflicting         | f

--check slot on Replica
postgres=# select * from pg_replication_slots where slot_type = 'logical'\gx
-[ RECORD 1 ]-------+-------------------------
slot_name           | logical_replication_slot
plugin              | pgoutput
slot_type           | logical
datoid              | 5
database            | postgres
temporary           | f
active              | f
active_pid          | 
xmin                | 
catalog_xmin        | 733
restart_lsn         | 0/172B808
confirmed_flush_lsn | 0/172B840
wal_status          | reserved
safe_wal_size       | 
two_phase           | f
conflicting         | f

patronictl show-config

loop_wait: 10
master_start_timeout: 300
maximum_lag_on_failover: 1048576
postgresql:
  parameters:
    ...
  use_pg_rewind: true
  use_slots: true
retry_timeout: 10
slots:
  logical_replication_slot:
    database: postgres
    plugin: pgoutput
    type: logical
synchronous_mode: false
synchronous_mode_strict: false
synchronous_node_count: 1
ttl: 30

passed

@vitabaks vitabaks changed the title Patroni: Add permanent replication slots. Patroni: Add permanent replication slots Mar 28, 2024
@vitabaks vitabaks merged commit f3cfc01 into master Mar 28, 2024
17 checks passed
@vitabaks vitabaks deleted the patroni-slots branch March 28, 2024 15:02
christofalpo pushed a commit to christofalpo/postgresql_cluster that referenced this pull request Apr 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Improvement of the current functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant