go-pq-cdc 是一款轻量级、高效的变更数据捕获 (CDC) 系统,专为 PostgreSQL 设计。
go-pq-cdc is designed to provide efficient and lightweight Change Data Capture (CDC) for PostgreSQL databases. The architecture leverages PostgreSQL's built-in logical replication capabilities to capture changes in the database and stream these changes to downstream systems, such as Kafka, Elasticsearch etc. The entire system is written in Golang, ensuring low resource consumption and high performance.
Debezium vs go-pq-cdc benchmark
Capture existing data before starting CDC! The new snapshot feature enables initial data synchronization, ensuring downstream systems receive both historical and real-time data.
✨ Key Highlights:
pg_export_snapshot()pgoutput proto_version 1 and 2 support Read Full Documentation for detailed architecture, configuration, and best practices.
Protocol Version & Streaming Transactions for proto_version behavior and migration guidance.
CDC systems are crucial for real-time data synchronization, analytics, and event-driven architectures. Our main goal is to build a cdc base library for faster and stateful systems.
go get github.com/Trendyol/go-pq-cdc…go-pq-cdc connects to PostgreSQL with a regular role. For most deployments you do not need a superuser. A dedicated role with the right grants is enough. The exact privileges depend on which features you enable.
Minimum role for streaming CDC against a fixed table list:
CREATE ROLE cdc_user WITH LOGIN REPLICATION PASSWORD 'change_me';
GRANT USAGE ON SCHEMA public TO cdc_user;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO cdc_user;
ALTER DEFAULT PRIVILEGES IN SCHEMA public
GRANT SELECT ON TABLES TO cdc_user;Add these grants when the corresponding feature is enabled:
| Feature | Extra grants needed |
|---|---|
publication.createIfNotExists: true |
GRANT CREATE ON DATABASE TO cdc_user; so the publication can be created. |
slot.createIfNotExists: true |
REPLICATION role attribute (already in the example above). |
| Heartbeat with auto-created table | GRANT CREATE ON SCHEMA TO cdc_user; for the initial CREATE TABLE, plus GRANT INSERT, UPDATE ON for the auto-managed singleton row. |
| Snapshot mode (initial / only) | SELECT on the source tables (already covered above). |
| Adding tables to an existing publication | The role must own the publication, e.g. ALTER PUBLICATION cdc_publication OWNER TO cdc_user;. |
Common failure modes when grants are missing:
must be superuser or replication role to start walsender — role missing REPLICATION.permission denied for schema — missing USAGE on the schema.permission denied to create publication — missing CREATE on the database.permission denied for table — heartbeat enabled but the role cannot INSERT/UPDATE the singleton row.pg_hba.conf must also allow replication connections from the CDC host, for example:
host replication cdc_user 10.0.0.0/8 scram-sha-256The go-pq-cdc operates in passive/active modes for PostgreSQL change data capture (CDC). Here's how it ensures availability:
This setup ensures continuous data synchronization and minimal downtime in capturing database changes.
PostgreSQL does not send TOASTed column values during an update unless the TOASTed column itself is modified. For example, if only a small field changes, large columns such as TEXT, JSONB, or BYTEA are sent as TOAST placeholders instead of real values.
To guarantee that UPDATE messages include full, correct data, go-pq-cdc automatically merges old and new tuples, restoring any missing TOASTed fields:
if m.OldTupleData != nil {
for i, col := range m.NewTupleData.Columns {
// Toasted columns are not sent unless the column itself changed
if col.DataType == tuple.DataTypeToast {
m.NewTupleData.Columns[i] = m.OldTupleData.Columns[i]
}
}
}Logical replication slots in PostgreSQL retain WAL segments until all consumers have confirmed they no longer need them.
In low-traffic databases, this can cause WAL bloat when other databases on the same Postgres instance generate a lot of
traffic while the CDC database itself remains mostly idle. In this situation the replication slot's restart_lsn and
confirmed_flush_lsn may lag far behind pg_current_wal_lsn(), preventing WAL segments from being recycled.
go-pq-cdc provides a table-based heartbeat mechanism for this scenario.
When heartbeat.table.name is configured, the connector:
UPDATE to generate small committed WAL records.Each heartbeat:
confirmed_flush_lsn and, over time, restart_lsn, allowing PostgreSQL to safely recycle old WAL.This makes WAL retention predictable even when application traffic on the CDC database is very low, while other databases on the same instance are generating heavy write load.
Heartbeat only cleans WAL when its writes flow through the replication slot.
The heartbeat table must satisfy both conditions below — otherwise the auto-
UPDATEs commit successfully but the slot never decodes them,confirmed_flush_lsnstays frozen, and WAL keeps growing as if heartbeat were disabled:
- The heartbeat table is listed in
publication.tables. If it is not part of the publication the change is filtered out before reaching the slot. The connector validates this at startup for selective publications and returns an error if the heartbeat table is missing.- The heartbeat table has a
REPLICA IDENTITYthat allows the auto-UPDATEto be decoded —DEFAULT(the auto-created table already has a primary key, so this works out of the box) orFULL.REPLICA IDENTITY NOTHINGwill silently break WAL advancement because theUPDATEcannot be decoded bypgoutput.Use a dedicated heartbeat table — never reuse a business table.
heartbeat:
table:
name: "my_heartbeat"
schema: "public"
interval: 100msMake sure the heartbeat table is included in the publication, for example:
Publication: publication.Config{
Name: "cdc_publication",
CreateIfNotExists: true,
Tables: publication.Tables{
publication.Table{Name: "users"},
publication.Table{Name: "my_heartbeat"}, // must be present
},
},Before:
heartbeat:
enabled: true
query: "INSERT INTO heartbeat_table(txt) VALUES ('hb')"
interval: 5sAfter:
heartbeat:
table:
name: "heartbeat_table"
schema: "public"
interval: 100msYou can run Simple With Heartbeat example.
If you use heartbeat with a selective publication (publication.tables is non-empty and the publication is not FOR ALL TABLES), the connector now fails at startup when the heartbeat table is missing from publication.tables.
Previously, this misconfiguration could still start successfully, but heartbeat writes would not reach the replication slot and WAL retention would not improve.
Before upgrading, add the heartbeat table to your publication config (or switch to a FOR ALL TABLES publication):
publication:
name: cdc_publication
createIfNotExists: true
tables:
- name:暂无开放 Issues,或尚未同步最近议题。