doc/user/content/ingest-data/postgres/replication-slot-active.md
This guide helps you troubleshoot and resolve the "replication slot... is active" error that can occur with PostgreSQL sources in Materialize.
When you see an error like:
postgres: ERROR replication slot "materialize_1002f37c6eeb4c28b052fa3805d46baa" is active for PID 610
This means that PostgreSQL has detected an attempt to use a replication slot that is already in use by another active connection. Each replication slot can only have one active consumer at a time, and this error occurs when Materialize tries to connect to a slot that PostgreSQL considers already active. This error is generally transient and indicates a race condition during connection management between Materialize and PostgreSQL.
Connect to your PostgreSQL database and run:
-- Replace <slot_name> with the replication slot name shown in the error message
SELECT
slot_name,
active,
active_pid,
restart_lsn,
confirmed_flush_lsn
FROM pg_replication_slots
WHERE slot_name = '<slot_name>';
Look for:
Check which connections are using the replication slot:
-- Replace <slot_name> with the replication slot name shown in the error message
SELECT
pid,
usename,
application_name,
client_addr,
state,
backend_start
FROM pg_stat_activity
WHERE pid IN (
SELECT active_pid
FROM pg_replication_slots
WHERE slot_name = '<slot_name>'
);
In most cases, Materialize will automatically reconnect and recover from this error. The error is typically transient and resolves itself within a few minutes as PostgreSQL releases the previous connection.
If the error persists and you've verified that there are no legitimate active connections, you can terminate the stale connection in PostgreSQL:
{{< warning >}} Only terminate connections if you're certain they are stale. Terminating an active replication connection will interrupt data ingestion. {{</ warning >}}
-- First, identify the PID from the error message or from pg_replication_slots
-- Replace <slot_name> with the replication slot name shown in the error message
SELECT
slot_name,
active_pid
FROM pg_replication_slots
WHERE slot_name = '<slot_name>' AND active = true;
-- Terminate the connection (replace ### with the actual PID)
SELECT pg_terminate_backend(###);
After terminating the connection, Materialize should be able to reconnect to the replication slot.
Best practices to avoid this error:
SELECT *
FROM mz_internal.mz_source_statuses;