PostgreSQL
CDC source setup for PostgreSQL
Sling supports Change Data Capture from PostgreSQL by reading the Write-Ahead Log (WAL) via logical replication. Each run reads row-level inserts, updates, and deletes from the WAL and merges them into the target table.
For general CDC concepts, the two-phase process, and all available options, see the Change Capture overview.
Prerequisites
CDC needs two things: logical replication turned on, and a Sling role with the replication privilege plus SELECT on the source tables.
Verify the WAL level first:
SHOW wal_level; -- Must be 'logical'How you set wal_level and grant replication depends on where PostgreSQL runs. Pick your platform below.
Sling connects with a least-privilege, read-only role: replication rights plus SELECT on the source tables, and nothing more. It runs no DDL against your source — the publication and replica identity are provisioned once by a DBA (see CDC Setup below).
CDC Setup (DBA-owned)
Sling reads from a pre-provisioned publication; it never creates one. A DBA (or a role that owns the tables) runs this setup once, then Sling references it via the change_feed option.
1. Create a publication scoped to the tables you want to capture:
Scope the publication to exactly the streams you replicate — avoid FOR ALL TABLES unless you truly want every table in scope (on self-managed PostgreSQL it also requires superuser).
2. Ensure each captured table has a CDC-usable replica identity. PostgreSQL only includes old-row data in UPDATE and DELETE WAL messages when the table has a replica identity — without one, those changes carry no key and cannot be applied to the target. Tables with a primary key already satisfy this under the default replica identity (PostgreSQL logs the key — sufficient for a PK-based merge), so no DDL is needed.
Tables without a primary key need one set explicitly. Either form works:
Sling accepts either, and validates it before streaming. Note that REPLICA IDENTITY NOTHING is never usable for CDC — including on tables that have a primary key.
3. Point Sling at the publication with change_feed:
Sling validates the publication exists and covers each configured stream, then creates and manages its own replication slot (which needs only the REPLICATION attribute). If the publication is missing, does not cover a table, or a table lacks a usable replica identity, Sling stops with a clear error containing the exact DDL for a DBA to run.
Quick Start
Examples
Large Tables with Custom Chunk Size
For very large tables, adjust the chunk size to control memory usage and checkpointing frequency during the initial snapshot.
Time-Bounded Snapshots for Very Large Tables
For tables with hundreds of millions of rows, the initial snapshot can take hours. Use snapshot_run_duration to cap how long each run spends on the snapshot. The next run automatically resumes from the last completed chunk.
High-Throughput Workloads
For tables with heavy write activity, increase run_max_events and run_max_duration so each run captures more changes.
Soft Deletes
Keep deleted rows in the target instead of physically removing them. Deleted rows are marked with _sling_synced_op = 'D'. Useful for audit trails or when downstream queries need to detect deletions.
When a row is deleted in the source, the target row is preserved with _sling_synced_op set to 'D' and _sling_synced_at updated to the current timestamp. A subsequent re-insert of the same primary key restores the row with the appropriate operation type.
Mixed Streams with Per-Stream Overrides
Different tables can have different CDC options.
Replay / Backfill from a Point in Time
If target data becomes inconsistent, you can replay changes from an earlier position. The replay_from value is applied exactly once per unique value.
After the replay run completes, remove or change the replay_from value. Leaving it unchanged has no effect (it is only applied once).
Replay Formats
The replay_from option accepts these PostgreSQL-specific position formats:
RFC 3339 timestamp:
2025-06-01T00:00:00Z— resolves to the replication slot'srestart_lsn(best-effort; PostgreSQL WAL does not support direct timestamp seeking)PostgreSQL LSN:
0/16B3748— a Write-Ahead Log position in hex format
Replication Slots and Publications
The publication is DBA-provisioned (see CDC Setup) and shared by all streams that reference the same change_feed. The replication slot is created and managed by Sling — it needs only the REPLICATION attribute, never ownership. How the slot is scoped depends on the slot_level option, which defaults to shared for PostgreSQL.
slot_level: shared (default)
All streams in the replication share one replication slot (and read from the one change_feed publication):
Replication slot: Created once with a deterministic name (
sling_cdc_<hash>) derived from the host, port, and database — independent of the table list. Every stream reads from this single slot, so they all converge to the same unified WAL position (LSN), giving a point-in-time-consistent view across tables. The WAL is decoded only once per run regardless of how many tables are captured.Publication: The one named by
change_feed. It must cover every configured stream; Sling validates this and errors withALTER PUBLICATION … ADD TABLEif a table is missing.
Because the slot is shared across streams, it is not dropped automatically when you remove a single stream — removing it would break the other streams reading from it. To tear down a shared slot manually:
slot_level: stream
Each captured table gets its own slot (all still reading from the DBA's publication):
Replication slot: Named
sling_cdc_<hash>where the hash is derived from the host, port, and that table's name, so each stream is fully isolated and advances independently.Publication: The
change_feedpublication, which must include the table.
Per-table slots are cleaned up automatically when you remove the corresponding CDC stream. Use this mode when you want each table to advance fully independently rather than sharing a unified position.
WAL Retention
Replication slots prevent PostgreSQL from recycling WAL segments that haven't been consumed yet. If CDC runs are paused for an extended period, WAL can accumulate and consume significant disk space.
To protect against unbounded WAL growth, set a safety limit:
Troubleshooting
"wal_level must be 'logical'"
The PostgreSQL instance is not configured for logical replication.
On self-managed PostgreSQL, set wal_level and restart:
On AWS RDS, ALTER SYSTEM is blocked. Set rds.logical_replication = 1 in a custom parameter group and reboot the instance — the parameter is static, so it does not apply until then. On Cloud SQL, set the cloudsql.logical_decoding flag to on. On Azure, set the wal_level server parameter to logical and restart. See Prerequisites.
"current user does not have REPLICATION privilege"
Grant the REPLICATION attribute to the CDC user:
On AWS RDS / Aurora that statement always fails, because it requires superuser and RDS does not grant it. Use role membership instead, as the RDS master user:
"could not open replication connection"
On self-managed PostgreSQL, ensure pg_hba.conf allows replication connections for the user and database. Add a line like:
Then reload PostgreSQL configuration.
On managed platforms there is no pg_hba.conf to edit. Check instead that:
the network path is open (RDS security group, Cloud SQL authorized networks, or Azure firewall rules) for port 5432
sslmodeis set on the connection — RDS rejects plaintext on most instances, and Sling defaults todisable
"permission denied for database" when creating a publication
CREATE PUBLICATION needs CREATE on the database, which the least-privilege Sling role deliberately lacks. Create it as an administrative role instead — the RDS master user, the Cloud SQL postgres user, or the Azure admin — then point Sling at it with change_feed:
On self-managed PostgreSQL, FOR ALL TABLES additionally requires a true superuser. Scoping the publication to named tables avoids that and keeps unrelated tables out of replication scope.
"CDC publication … does not exist"
The publication named by change_feed (or the derived default) has not been created. Sling does not create it — a DBA must, once:
Then set change_feed: sling_cdc_pub in change_capture_options.
"table … is not a member of publication"
A configured stream is not covered by the publication. A DBA adds it:
"table … has no CDC-usable replica identity"
PostgreSQL is not logging a key for UPDATE/DELETE on the table, so those changes cannot be applied to the target. The error reports the table's current relreplident value. Causes:
No primary key, replica identity left at DEFAULT (d)
ALTER TABLE … REPLICA IDENTITY FULL; or nominate a unique index (see below)
Replica identity explicitly set to NOTHING (n) — applies even if the table has a primary key
ALTER TABLE … REPLICA IDENTITY DEFAULT; (PK tables) or FULL
USING INDEX (i) whose nominated index was dropped, invalidated, or is partial/deferrable/over nullable columns
Re-nominate a usable index, or switch to FULL
Tables that have a primary key and are left at the default replica identity do not need any of this — PostgreSQL already logs the key for a PK-based merge.
"replication slot is already active"
Another consumer (another Sling process or a different tool) is connected to the same replication slot. Ensure only one CDC process is running per stream group at a time.
Initial snapshot keeps restarting
Ensure SLING_STATE is configured. Without state persistence, Sling cannot track that the snapshot completed and will restart it on every run.
Large WAL lag warning
If Sling warns about WAL lag exceeding 5 GB, the CDC consumer is falling behind. Increase the run frequency or raise run_max_events / run_max_duration to process more changes per run. Consider setting max_slot_wal_keep_size as a safety net to prevent unbounded disk usage.
Last updated
Was this helpful?