mirror of
https://github.com/outbackdingo/patroni.git
synced 2026-08-25 14:53:37 +00:00
Effectively, this PR consists of a few changes: 1. The easy part: In case of permanent logical slots are defined in the global configuration, Patroni on the primary will not only create them, but also periodically update DCS with the current values of `confirmed_flush_lsn` for all these slots. In order to reduce the number of interactions with DCS the new `/status` key was introduced. It will contain the json object with `optime` and `slots` keys. For backward compatibility the `/optime/leader` will be updated if there are members with old Patroni in the cluster. 2. The tricky part: On replicas that are eligible for a failover, Patroni creates the logical replication slot by copying the slot file from the primary and restarting the replica. In order to copy the slot file Patroni opens a connection to the primary with `rewind` or `superuser` credentials and calls `pg_read_binary_file()` function. When the logical slot already exists on the replica Patroni periodically calls `pg_replication_slot_advance()` function, which allows moving the slot forward. 3. Additional requirements: In order to ensure that primary doesn't cleanup tuples from pg_catalog that are required for logical decoding, Patroni enables `hot_standby_feedback` on replicas with logical slots and on cascading replicas if they are used for streaming by replicas with logical slots. 4. When logical slots are copied from to the replica there is a timeframe when it could be not safe to use them after promotion. Right now there is no protection from promoting such a replica. But, Patroni will show the warning with names of the slots that might be not safe to use. Compatibility. The `pg_replication_slot_advance()` function is only available starting from PostgreSQL 11. For older Postgres versions Patroni will refuse to create the logical slot on the primary. The old "permanent slots" feature, which creates logical slots right after promotion and before allowing connections, was removed. Close: https://github.com/zalando/patroni/issues/1749
61 lines
3.1 KiB
Python
61 lines
3.1 KiB
Python
import time
|
|
import psycopg2
|
|
|
|
from behave import step, then
|
|
|
|
|
|
@step('I create a logical replication slot {slot_name} on {pg_name:w} with the {plugin:w} plugin')
|
|
def create_logical_replication_slot(context, slot_name, pg_name, plugin):
|
|
try:
|
|
output = context.pctl.query(pg_name, ("SELECT pg_create_logical_replication_slot('{0}', '{1}'),"
|
|
" current_database()").format(slot_name, plugin))
|
|
print(output.fetchone())
|
|
except psycopg2.Error as e:
|
|
print(e)
|
|
assert False, "Error creating slot {0} on {1} with plugin {2}".format(slot_name, pg_name, plugin)
|
|
|
|
|
|
@then('{pg_name:w} has a logical replication slot named {slot_name} with the {plugin:w} plugin')
|
|
def has_logical_replication_slot(context, pg_name, slot_name, plugin):
|
|
try:
|
|
row = context.pctl.query(pg_name, ("SELECT slot_type, plugin FROM pg_replication_slots"
|
|
" WHERE slot_name = '{0}'").format(slot_name)).fetchone()
|
|
assert row, "Couldn't find replication slot named {0}".format(slot_name)
|
|
assert row[0] == "logical", "Found replication slot named {0} but wasn't a logical slot".format(slot_name)
|
|
assert row[1] == plugin, ("Found replication slot named {0} but was using plugin "
|
|
"{1} rather than {2}").format(slot_name, row[1], plugin)
|
|
except psycopg2.Error:
|
|
assert False, "Error looking for slot {0} on {1} with plugin {2}".format(slot_name, pg_name, plugin)
|
|
|
|
|
|
@then('{pg_name:w} does not have a logical replication slot named {slot_name}')
|
|
def does_not_have_logical_replication_slot(context, pg_name, slot_name):
|
|
try:
|
|
row = context.pctl.query(pg_name, ("SELECT 1 FROM pg_replication_slots"
|
|
" WHERE slot_name = '{0}'").format(slot_name)).fetchone()
|
|
assert not row, "Found unexpected replication slot named {0}".format(slot_name)
|
|
except psycopg2.Error:
|
|
assert False, "Error looking for slot {0} on {1}".format(slot_name, pg_name)
|
|
|
|
|
|
@step('Logical slot {slot_name:w} is in sync between {pg_name1:w} and {pg_name2:w} after {time_limit:d} seconds')
|
|
def logical_slots_in_sync(context, slot_name, pg_name1, pg_name2, time_limit):
|
|
time_limit *= context.timeout_multiplier
|
|
max_time = time.time() + int(time_limit)
|
|
while time.time() < max_time:
|
|
try:
|
|
query = "SELECT confirmed_flush_lsn FROM pg_replication_slots WHERE slot_name = '{0}'".format(slot_name)
|
|
slot1 = context.pctl.query(pg_name1, query).fetchone()
|
|
slot2 = context.pctl.query(pg_name2, query).fetchone()
|
|
if slot1[0] == slot2[0]:
|
|
return
|
|
except Exception:
|
|
pass
|
|
time.sleep(1)
|
|
assert False, "Logical slot {0} is not in sync between {1} and {2}".format(slot_name, pg_name1, pg_name2)
|
|
|
|
|
|
@step('I get all changes from logical slot {slot_name:w} on {pg_name:w}')
|
|
def logical_slot_get_changes(context, slot_name, pg_name):
|
|
context.pctl.query(pg_name, "SELECT * FROM pg_logical_slot_get_changes('{0}', NULL, NULL)".format(slot_name))
|