Skip to content

Commit 2ee8caa

Browse files
RTLSfazzone
andcommitted
⚙️ Only consider physical replicas as replicas
Co-authored-by: Russell McQueeney <rmcq@utexas.edu>
1 parent ebb458c commit 2ee8caa

File tree

3 files changed

+25
-12
lines changed

3 files changed

+25
-12
lines changed

docker/replica-cdc-dev/init-primary.sh

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,17 @@ psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-E
2727
CREATE PUBLICATION sequin_pub FOR TABLE test_table;
2828
2929
-- Drop existing replication slot if it exists
30-
SELECT pg_drop_replication_slot('sequin_slot') WHERE EXISTS (SELECT 1 FROM pg_replication_slots WHERE slot_name = 'sequin_slot');
30+
SELECT pg_drop_replication_slot(slot_name)
31+
FROM pg_replication_slots
32+
WHERE slot_name LIKE 'pg_%_sync_%' OR slot_name = 'sequin_slot';
3133
32-
-- Create replication slot
34+
-- Create primary replication slot
3335
SELECT pg_create_logical_replication_slot('sequin_slot', 'pgoutput');
3436
EOSQL
3537

3638
# Configure pg_hba.conf to allow replication connections
3739
echo "host replication replicator all md5" >> "$PGDATA/pg_hba.conf"
38-
echo "host all replicator all md5" >> "$PGDATA/pg_hba.conf"
40+
echo "host all replicator all md5" >> "$PGDATA/pg_hba.conf"
41+
42+
# Set synchronous_commit to local to improve performance
43+
echo "synchronous_commit = local" >> "$PGDATA/postgresql.conf"

docker/replica-cdc-dev/init-replica.sh

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,17 @@ psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-E
2626
CREATE SUBSCRIPTION sequin_sub
2727
CONNECTION 'host=postgres-primary port=5432 user=replicator password=replicator_password dbname=postgres'
2828
PUBLICATION sequin_pub
29-
WITH (copy_data = true);
29+
WITH (copy_data = true, create_slot = true, enabled = true, connect = true, slot_name = 'sequin_sub', disable_on_error = false);
30+
EOSQL
31+
32+
# Allow time for the subscription to be established
33+
sleep 5
34+
35+
# Restart subscription if needed
36+
psql -v ON_ERROR_STOP=0 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
37+
-- Restart subscription if not active
38+
ALTER SUBSCRIPTION sequin_sub DISABLE;
39+
ALTER SUBSCRIPTION sequin_sub ENABLE;
3040
EOSQL
3141

3242
# Wait for initial data copy to complete

lib/sequin/databases/databases.ex

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ defmodule Sequin.Databases do
433433

434434
def test_maybe_replica(%PostgresDatabase{} = db, db_primary) do
435435
cond do
436-
not replica?(db) ->
436+
not is_physical_replica?(db) ->
437437
:ok
438438

439439
is_nil(db_primary) ->
@@ -627,18 +627,16 @@ defmodule Sequin.Databases do
627627
end
628628
end
629629

630-
@spec replica?(%PostgresDatabase{}) :: boolean()
631-
def replica?(%PostgresDatabase{} = db) do
630+
@spec is_physical_replica?(%PostgresDatabase{}) :: boolean()
631+
def is_physical_replica?(%PostgresDatabase{} = db) do
632632
with_uncached_connection(db, fn conn ->
633633
query = """
634-
SELECT
635-
pg_is_in_recovery() AS is_physical_replica,
636-
EXISTS (SELECT 1 FROM pg_subscription) AS has_logical_replication
634+
SELECT pg_is_in_recovery() AS is_physical_replica
637635
"""
638636

639637
case Postgres.query(conn, query, []) do
640-
{:ok, %{rows: [[is_physical, has_logical]]}} ->
641-
is_physical or has_logical
638+
{:ok, %{rows: [[is_physical]]}} ->
639+
is_physical
642640

643641
_ ->
644642
false

0 commit comments

Comments
 (0)