Skip to main content

risingwave_connector/source/cdc/
postgres.rs

1// Copyright 2026 RisingWave Labs
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::collections::BTreeMap;
16
17use risingwave_common::bail;
18use risingwave_pb::secret::PbSecretRef;
19
20use crate::error::ConnectorResult;
21use crate::source::cdc::{
22    CdcSourceDdlValidator, CdcSourceExternalTableIdentifier, CdcSourceRuntimeFlags,
23    CdcSourceTypeTrait, Postgres, check_alter_with_options_inner, check_common_with_options,
24};
25
26// ------- Runtime flags -------
27
28impl CdcSourceRuntimeFlags for Postgres {
29    const CAN_AUTO_SCHEMA_CHANGE: bool = true;
30    const CAN_BACKFILL: bool = true;
31    const CAN_BACKFILL_IN_PARALLEL: bool = true;
32    const CAN_USE_TRANSACTION_METADATA: bool = true;
33    const NEEDS_OFFSET_COMMIT_ON_CHECKPOINT: bool = true;
34}
35
36// ------- DDL validation -------
37
38impl CdcSourceDdlValidator for Postgres {
39    fn check_and_fill_create_with_options(
40        props: &mut BTreeMap<String, String>,
41        secret_refs: &BTreeMap<String, PbSecretRef>,
42    ) -> ConnectorResult<()> {
43        check_common_with_options::<Self>(props, secret_refs)?;
44        match props.get("slot.name") {
45            None => {
46                // Build a random slot name with UUID
47                // e.g. "rw_cdc_f9a3567e6dd54bf5900444c8b1c03815"
48                let uuid = uuid::Uuid::new_v4();
49                props.insert("slot.name".into(), format!("rw_cdc_{}", uuid.simple()));
50            }
51            Some(slot_name) => {
52                // please refer to
53                // - https://github.com/debezium/debezium/blob/97956ce25b7612e3413d363658661896b7d2e0a2/debezium-connector-postgres/src/main/java/io/debezium/connector/postgresql/PostgresConnectorConfig.java#L1179
54                // - https://doxygen.postgresql.org/slot_8c.html#afac399f07320b9adfd2c599cf822aaa3
55                if !slot_name
56                    .chars()
57                    .all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '_')
58                    || slot_name.len() > 63
59                {
60                    bail!(
61                        "Invalid replication slot name: {:?}. Valid replication slot name must contain only digits, lowercase characters and underscores with length <= 63",
62                        slot_name
63                    );
64                }
65            }
66        }
67
68        if !props.contains_key("schema.name") {
69            // Default schema name is "public"
70            props.insert("schema.name".into(), "public".into());
71        }
72        if !props.contains_key("publication.name") {
73            // Build a random publication name with UUID to avoid conflicts between sources
74            // e.g. "rw_publication_f9a3567e6dd54bf5900444c8b1c03815"
75            let uuid = uuid::Uuid::new_v4();
76            props.insert(
77                "publication.name".into(),
78                format!("rw_publication_{}", uuid.simple()),
79            );
80        }
81        if !props.contains_key("publication.create.enable") {
82            // Default auto create publication if doesn't exist
83            props.insert("publication.create.enable".into(), "true".into());
84        }
85        Ok(())
86    }
87
88    fn check_alter_with_options(
89        props: &BTreeMap<String, String>,
90        secret_refs: &BTreeMap<String, PbSecretRef>,
91    ) -> ConnectorResult<()> {
92        check_common_with_options::<Self>(props, secret_refs)?;
93        check_alter_with_options_inner(
94            Self::CDC_CONNECTOR_NAME,
95            props,
96            secret_refs,
97            &[
98                "cdc.source.wait.streaming.start.timeout",
99                "debezium.max.queue.size",
100                "debezium.queue.memory.ratio",
101                "debezium.heartbeat.interval.ms",
102                "hostname",
103                "port",
104                "password",
105            ],
106        )
107    }
108}
109
110// ------- External table identifier -------
111
112impl CdcSourceExternalTableIdentifier for Postgres {}