packagemain.tech

packagemain.tech

Real-Time database change tracking in Go: Implementing PostgreSQL CDC

Julien Singler's avatar
Julien Singler
May 07, 2025
∙ Paid

Introduction

Change Data Capture (CDC) enables real-time tracking of database changes, critical for event-driven systems, analytics pipelines, and synchronizing microservices. This guide walks through implementing PostgreSQL CDC in Go using native logical replication and the pgx driver.

1. Understanding PostgreSQL CDC

PostgreSQL provides CDC via logical replication, which decodes changes from the write-ahead log (WAL) into consumable events (inserts/updates/deletes). Key concepts:

  • Replication Slots: Persistent channels for streaming changes.

  • Publications: Define which tables to monitor.

  • Logical Decoding Plugins: Convert WAL entries to readable formats (e.g., pgoutput, wal2json).

2. Prerequisites

a) PostgreSQL configured for replication

# postgresql.conf  
wal_level = logical  
max_replication_slots = 5

In GCP with cloudSQL, you have to enable some flags:

cloudsql.logical_decoding = on 

b) Replication user

CREATE ROLE repl_user WITH LOGIN REPLICATION PASSWORD 'password';  
GRANT SELECT ON ALL TABLES IN SCHEMA public TO repl_user;

User's avatar

Continue reading this post for free, courtesy of Alex Pliutau.

Or purchase a paid subscription.
© 2026 Aliaksandr Pliutau · Privacy ∙ Terms ∙ Collection notice
Start your SubstackGet the app
Substack is the home for great culture