Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ All notable changes to dbmazz will be documented here.

## [Unreleased]

### Added

- **SQL Server sink (GA).** New sink connector that streams PostgreSQL changes to
Microsoft SQL Server 2017+. Uses batch INSERT to a raw staging table followed by
an async MERGE normalizer; supports hard deletes, schema evolution via dynamic
DDL, and TOAST-like unchanged value detection. Configurable via `SINK_TYPE=sql_server`
and `SINK_SQL_SERVER_*` environment variables.

## [2.5.0] - 2026-05-16

### Changed
Expand Down
72 changes: 72 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ sink-snowflake = [
"dep:jsonwebtoken", "dep:rsa", "dep:pem", "dep:sha2",
"dep:aws-sigv4", "dep:aws-credential-types", "dep:uuid",
]
sink-sqlserver = ["dep:tiberius", "dep:tokio-util"]
sink-sql_server = ["sink-sqlserver"]

[dependencies]
# Materialize fork with full support for logical replication
Expand Down Expand Up @@ -72,6 +74,10 @@ aws-sigv4 = { version = "1.2", optional = true }
aws-credential-types = { version = "1.2", optional = true }
uuid = { version = "1", features = ["v4"], optional = true }

# --- Optional: SQL Server sink ---
tiberius = { version = "0.12", default-features = false, features = ["tds73", "chrono"], optional = true }
tokio-util = { version = "0.7", features = ["compat"], optional = true }

# --- Optional: MySQL source ---
mysql_common = { version = "=0.32.4", features = ["binlog"], optional = true }

Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

**Real-time PostgreSQL CDC in ~11 MB of RAM. One binary. No Kafka.**

A single Rust daemon that streams PostgreSQL changes to StarRocks, Snowflake, or another PostgreSQL with sub-second replication lag — **23–360× lighter** than [Debezium standard deployments][deb-faq] and **727× lighter** than [Airbyte's minimum recommendation][air-deploy]. ([How we measured ↓](#-performance))
A single Rust daemon that streams PostgreSQL changes to StarRocks, Snowflake, SQL Server, or another PostgreSQL with sub-second replication lag — **23–360× lighter** than [Debezium standard deployments][deb-faq] and **727× lighter** than [Airbyte's minimum recommendation][air-deploy]. ([How we measured ↓](#-performance))

Built and maintained by **[EZ-CDC](https://ez-cdc.com)**.

Expand Down Expand Up @@ -122,6 +122,7 @@ Full Docker reference (env vars, healthcheck, persisting state, build-from-sourc
| PostgreSQL 12+ | **StarRocks 3.2+** | ✅ Stable | JSON Stream Load, partial-update for TOAST columns, audit columns auto-managed, schema evolution requires per-table `fast_schema_evolution=true` |
| PostgreSQL 12+ | **PostgreSQL 15+** | ✅ Stable | Binary `COPY` → raw table → `MERGE` normalizer; supports hard delete |
| PostgreSQL 12+ | **Snowflake** | ✅ Stable | Parquet → PUT (stage) → `COPY INTO` → background `MERGE`; JWT key-pair auth supported; requires `ALTER TABLE` privilege on target schema |
| PostgreSQL 12+ | **SQL Server 2017+** (`sql_server`) | ✅ Stable | T-SQL bulk insert with batching via `INSERT` + `MERGE` normalizer; schema evolution via dynamic DDL; TOAST-like unchanged values detected and excluded from MERGE |
| PostgreSQL 12+ | S3 / Iceberg | 🚧 Roadmap | Tracked in [issues](https://github.com/ez-cdc/dbmazz/issues) |
| MySQL 5.7+ / 8.0+ | All sinks | ✅ Stable | Binlog-based with GTID-aware checkpointing, BIGINT UNSIGNED, microsecond DATETIME, non-integer PK support, cursor-based snapshot chunker. See [`docs/mysql-source.md`](docs/mysql-source.md). |

Expand All @@ -137,7 +138,7 @@ PostgreSQL (source) dbmazz Sink (target)
│ WAL │ logical │ WAL Handler │ │ StarRocks │
│ (INSERT, │ replication │ │ │ │ PostgreSQL │
│ UPDATE, │ ────────────▶ │ ▼ │ │ Snowflake │
│ DELETE) │ (pgoutput) │ source/converter │ │ │
│ DELETE) │ (pgoutput) │ source/converter │ │ SQL Server │
│ │ │ │ │ │ │
│ │ │ ▼ │ │ │
│ │ │ Pipeline │ write │ │
Expand Down
18 changes: 17 additions & 1 deletion docs/architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ PostgreSQL (source) dbmazz Sink (target)
│ WAL │ logical │ WAL Handler │ │ StarRocks │
│ (INSERT, │ replication│ │ │ │ PostgreSQL │
│ UPDATE, │ ──────────▶ │ ▼ │ │ Snowflake │
│ DELETE) │ (pgoutput) │ source/converter │ │ (+ future) │
│ DELETE) │ (pgoutput) │ source/converter │ │ SQL Server │
│ │ │ │ │ │ │
│ │ │ ▼ │ │ │
│ │ │ Pipeline │ write │ │
Expand All @@ -23,6 +23,18 @@ PostgreSQL (source) dbmazz Sink (target)
<1s latency
```

### Sink connector modules

Each sink is implemented as a submodule under `src/connectors/sinks/`:

| Sink | Module | Feature flag | Type key |
|---|---|---|---|
| StarRocks | `starrocks` | (default) | `starrocks` |
| PostgreSQL | `postgres` | `sink-postgres` | `postgres` |
| Snowflake | `snowflake` | `sink-snowflake` | `snowflake` |
| SQL Server | `sqlserver` | `sink-sqlserver` | `sql_server` |
```

## Data Flow — Step by Step

### 1. Setup Phase
Expand Down Expand Up @@ -200,6 +212,10 @@ src/
│ ├── merge_generator.rs MERGE SQL with VARIANT extraction + TOAST
│ ├── normalizer.rs Async MERGE loop (raw table → target)
│ └── types.rs PG → Snowflake type mapping
│ └── sqlserver/ SQL Server sink
│ ├── mod.rs SqlServerSink (batch INSERT → raw table → MERGE)
│ ├── config.rs SqlServerSinkConfig
│ └── setup.rs DDL: schema, raw table, target tables
├── control/ HTTP control plane (axum)
│ ├── mod.rs Router + server startup
│ ├── state.rs SharedState (metrics, dedup, control)
Expand Down
21 changes: 19 additions & 2 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ every option documented inline. The default location is

| Variable | Default | Description |
|---|---|---|
| `SINK_TYPE` | `starrocks` | Sink connector type: `starrocks`, `postgres`, or `snowflake`. |
| `SINK_URL` | — | Sink connection URL. For StarRocks: `http://host:8030` (FE HTTP). For PostgreSQL: `postgres://...`. For Snowflake: ignored (use `SINK_SNOWFLAKE_ACCOUNT`). |
| `SINK_TYPE` | `starrocks` | Sink connector type: `starrocks`, `postgres`, `snowflake`, or `sql_server`. |
| `SINK_URL` | — | Sink connection URL. For StarRocks: `http://host:8030` (FE HTTP). For PostgreSQL: `postgres://...`. For Snowflake: ignored (use `SINK_SNOWFLAKE_ACCOUNT`). For SQL Server: `sqlserver://host:1433;database=...` or `Server=host,1433;Database=...`. |
| `SINK_PORT` | `9030` | Additional port when needed (e.g., StarRocks MySQL protocol port for DDL). |
| `SINK_DATABASE` | — (required) | Target database name. |
| `SINK_SCHEMA` | `public` | Target schema (PostgreSQL) or `PUBLIC` (Snowflake). StarRocks does not use this. |
Expand All @@ -44,6 +44,15 @@ every option documented inline. The default location is
| `SINK_SNOWFLAKE_FLUSH_FILES` | `20` | Trigger `COPY INTO` after accumulating this many staged Parquet files. For e2e testing, set to `1` for immediate flush. |
| `SINK_SNOWFLAKE_FLUSH_BYTES` | `104857600` | Trigger `COPY INTO` after accumulating this many bytes (default 100 MB). Whichever threshold (files or bytes) is reached first wins. |

## Sink (SQL Server-specific)

| Variable | Default | Description |
|---|---|---|
| `SINK_SQL_SERVER_TRUST_CERT` | `true` | Trust the server certificate without validation (useful for self-signed certs in dev). Set to `false` in production with a properly signed CA. |
| `SINK_SQL_SERVER_ENCRYPT` | `true` | Require encryption for the connection (TLS). |
| `SINK_SQL_SERVER_MERGE_INTERVAL_MS` | `30000` | Normalizer MERGE polling interval in ms. |
| `SINK_SQL_SERVER_BATCH_SIZE` | `1000` | Number of rows per batch INSERT to the raw table. |

## Pipeline / batching

| Variable | Default | Description |
Expand Down Expand Up @@ -98,6 +107,14 @@ loud with an actionable error message if they are missing.
| `CREATE` privilege on target database | dbmazz creates `_dbmazz` schema, raw table, metadata, schema-tracking, and target tables | `GRANT CREATE ON DATABASE <db> TO <role>` |
| (Existing PG sink behavior) target tables created by dbmazz | The PG sink **does** create target tables if they don't exist (legacy behavior; not symmetric with SR/SF) | No action needed |

### SQL Server

| Requirement | Why | How to satisfy |
|---|---|---|
| SQL Server 2017 or later | dbmazz uses `MERGE` (available since SQL Server 2008) and `ROW_NUMBER` for dedup; 2017+ is tested | Upgrade or use SQL Server 2017+ |
| `CREATE TABLE` permission on target database | dbmazz creates `_dbmazz` schema, raw table, metadata, and target tables | `GRANT CREATE TABLE TO <user>` |
| Target tables created by the operator (recommended) | dbmazz can create tables automatically, but operator-created tables give you control over indexes, partitioning, and filegroups | `CREATE TABLE ...` ahead of time |

## Observability

The HTTP control plane exposes runtime metrics at `GET /api/v1/cdc/metrics`. Notable counters:
Expand Down
42 changes: 39 additions & 3 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ pub enum SinkType {
StarRocks,
Postgres,
Snowflake,
SqlServer,
}

impl SinkType {
Expand All @@ -144,8 +145,9 @@ impl SinkType {
"starrocks" => Ok(SinkType::StarRocks),
"postgres" | "postgresql" => Ok(SinkType::Postgres),
"snowflake" => Ok(SinkType::Snowflake),
"sqlserver" | "sql_server" | "mssql" => Ok(SinkType::SqlServer),
other => anyhow::bail!(
"Unsupported sink type: '{}'. Supported: starrocks, postgres, snowflake",
"Unsupported sink type: '{}'. Supported: starrocks, postgres, snowflake, sqlserver",
other
),
}
Expand All @@ -158,6 +160,7 @@ impl std::fmt::Display for SinkType {
SinkType::StarRocks => write!(f, "starrocks"),
SinkType::Postgres => write!(f, "postgres"),
SinkType::Snowflake => write!(f, "snowflake"),
SinkType::SqlServer => write!(f, "sqlserver"),
}
}
}
Expand All @@ -171,6 +174,15 @@ pub struct PostgresSinkConfig {
pub job_name: String,
}

/// SQL Server target-specific sink configuration
#[derive(Debug, Clone)]
pub struct SqlServerSinkConfig {
/// Target schema (default: "dbo")
pub schema: String,
/// Job name for metadata tracking (defaults to slot_name)
pub job_name: String,
}

/// Generic sink configuration
#[derive(Clone)]
pub struct SinkConfig {
Expand All @@ -189,6 +201,7 @@ pub enum SinkSpecificConfig {
StarRocks,
Postgres(PostgresSinkConfig),
Snowflake,
SqlServer(SqlServerSinkConfig),
}

impl std::fmt::Debug for SinkConfig {
Expand All @@ -213,6 +226,14 @@ impl SinkConfig {
_ => anyhow::bail!("PostgresSinkConfig is required for postgres sink"),
}
}

/// Returns the SQL Server-specific config, or an error if this is not a SqlServer sink.
pub fn sqlserver_config(&self) -> Result<&SqlServerSinkConfig> {
match &self.specific {
SinkSpecificConfig::SqlServer(ss) => Ok(ss),
_ => anyhow::bail!("SqlServerSinkConfig is required for sqlserver sink"),
}
}
}

// =============================================================================
Expand Down Expand Up @@ -337,13 +358,21 @@ impl Config {
let sink_type = SinkType::from_str(&sink_type_str)?;

// SINK_URL is required for StarRocks/Postgres, but optional for Snowflake
// (auto-derived from SINK_SNOWFLAKE_ACCOUNT).
// (auto-derived from SINK_SNOWFLAKE_ACCOUNT). SqlServer uses SINK_HOST.
let sink_url = match sink_type {
SinkType::Snowflake => optional_env("SINK_URL", ""),
SinkType::SqlServer => {
// SqlServer CLI writes SINK_HOST, not SINK_URL
required_env("SINK_HOST")?
}
_ => required_env("SINK_URL")?,
};

let sink_port: u16 = optional_env("SINK_PORT", "9030").parse().unwrap_or(9030);
let sink_port: u16 = match sink_type {
SinkType::SqlServer => optional_env("SINK_PORT", "1433").parse().unwrap_or(1433),
SinkType::Snowflake => optional_env("SINK_PORT", "443").parse().unwrap_or(443),
_ => optional_env("SINK_PORT", "9030").parse().unwrap_or(9030),
};

let sink_database = required_env("SINK_DATABASE")?;

Expand All @@ -359,6 +388,10 @@ impl Config {
job_name: slot_name.clone(),
}),
SinkType::Snowflake => SinkSpecificConfig::Snowflake,
SinkType::SqlServer => SinkSpecificConfig::SqlServer(SqlServerSinkConfig {
schema: optional_env("SINK_SCHEMA", "dbo"),
job_name: slot_name.clone(),
}),
};

let sink = SinkConfig {
Expand Down Expand Up @@ -469,6 +502,9 @@ impl Config {
SinkType::Snowflake => {
info!("Sink: Snowflake (db: {})", self.sink.database);
}
SinkType::SqlServer => {
info!("Sink: SQL Server (db: {})", self.sink.database);
}
}

info!(
Expand Down
Loading