In an OLTP database, a schema deployment is mostly metadata. You run the migration scripts and you're done. A streaming database is different, because a materialized view is a running job, not a stored query. It holds state for its joins and aggregates, and RisingWave can't rewrite that state in place: if a filter changes from age >= 18 to age >= 16, the rows it already dropped are gone, and the only fix is to recompute.
That doesn't mean schema changes have to be scary. This post follows a pipeline that has been in production for a while through four releases, each shipped through the same continuous integration and delivery (CI/CD) path. Our demo deploys with dbt and the dbt-risingwave adapter, but every step is plain SQL you can run from any migration tool. It's a synthetic fleet-telematics pipeline. Truck telemetry arrives from Kafka into a table. Materialized views (MVs) compute each truck's latest position, which shipments are at risk, and five-minute health stats. An alerts MV feeds a Kafka sink.
CREATE TABLE truck_telemetry (
truck_id VARCHAR,
event_ts TIMESTAMPTZ,
lat DOUBLE PRECISION,
lon DOUBLE PRECISION,
speed_kph DOUBLE PRECISION
)
INCLUDE timestamp AS kafka_ts
WITH (
connector = 'kafka',
topic = 'truck_telemetry',
properties.bootstrap.server = 'kafka:29092',
scan.startup.mode = 'earliest'
)
FORMAT PLAIN ENCODE JSON;
CREATE MATERIALIZED VIEW truck_latest AS
SELECT truck_id, event_ts, lat, lon, speed_kph
FROM (
SELECT truck_id, event_ts, lat, lon, speed_kph,
row_number() OVER (PARTITION BY truck_id ORDER BY kafka_ts DESC) AS rn
FROM truck_telemetry
) ranked
WHERE rn = 1;
CREATE MATERIALIZED VIEW shipment_eta_risk AS
SELECT s.shipment_id, s.dest_region, s.promised_eta,
l.event_ts AS last_seen, l.speed_kph < 5 AS at_risk
FROM shipments s
JOIN truck_latest l ON s.truck_id = l.truck_id;
Note that the raw events land in a table, not just a source. That choice pays off later: any new MV can backfill from history, and the docs recommend exactly this so you can recompute when logic changes.
Sort every change before it ships
Every change in this pipeline falls into one of three kinds, and CI should label each one before anything touches production:
| What changed | How it's applied | Backfill? |
|---|---|---|
| Columns on a table or source | In place, with ALTER TABLE / ALTER SOURCE | No |
| MV logic | Build a new MV beside the old one, then swap names | Yes, from the upstream table |
| A sink's upstream or settings | REPLACE SINK ... FROM [New in 3.1] | No, by design |
One thing teams coming from Postgres reach for first doesn't exist here: there's no CREATE OR REPLACE for MVs or sinks. On v3.1.1, CREATE OR REPLACE MATERIALIZED VIEW fails with "Feature is not yet implemented", and CREATE OR REPLACE SINK fails with "REPLACE SINK should be used instead of CREATE OR REPLACE SINK".
v1.1: an edge change, in place
The telematics vendor's new firmware adds fuel_pct to each reading. This is the cheap kind of change:
ALTER TABLE truck_telemetry ADD COLUMN fuel_pct DOUBLE PRECISION;
No streaming job is rebuilt, and SHOW JOBS stays empty. Existing rows read NULL, and new messages fill the column. Ship this before the producer starts sending the field.
There's a catch, and it sets up the next release: "Columns added by this command cannot be used by any existing materialized views or indexes. You must create new materialized views or indexes to reference it".
v1.2: change an MV, build beside it, then swap
truck_health_5m should ignore idle readings in its speed average, and report the new fuel column. The pattern: create the new version under a temporary name, let it backfill in the background, wait for it, then swap names.
SET BACKGROUND_DDL = true;
CREATE MATERIALIZED VIEW truck_health_5m_next AS
SELECT truck_id, window_start,
avg(speed_kph) FILTER (WHERE speed_kph > 0) AS avg_speed_kph,
avg(fuel_pct) AS avg_fuel_pct,
count(*) AS readings
FROM TUMBLE(truck_telemetry, event_ts, INTERVAL '5 minutes')
GROUP BY truck_id, window_start;
ALTER MATERIALIZED VIEW truck_health_5m_next SET BACKFILL_RATE_LIMIT = 5000;
WAIT MATERIALIZED VIEW truck_health_5m_next;
ALTER MATERIALIZED VIEW truck_health_5m SWAP WITH truck_health_5m_next;
ALTER MATERIALIZED VIEW truck_health_5m_next RENAME TO truck_health_5m_retired;
A few things make this safe to automate:
BACKGROUND_DDLmakesCREATEreturn immediately while the backfill runs, so the deploy step doesn't hold a session open for the whole backfill.BACKFILL_RATE_LIMITprotects production while the new MV catches up. Since 3.1, you can change rate limits while the job is still being created.WAITblocks until the job finishes. A bareWAITwaits for all background jobs, with a two-hour timeout. Gate the swap onWAIT, not on a progress number.SWAP WITHexchanges the names of the two MVs in one statement. In our demo, a dashboard-style query loop againsttruck_health_5mran through the whole deploy without a single error. The rename that follows is just a convention: retired versions always end in_retired, which makes cleanup and guards scriptable.
While the backfill runs, the deploy log can show progress:
SHOW JOBS;
SELECT * FROM rw_catalog.rw_ddl_progress;
Treat that percentage as a progress bar, not a gate: the docs call it "just an estimate".
Rollback is the same statement in reverse, for as long as you keep the retired version:
ALTER MATERIALIZED VIEW truck_health_5m SWAP WITH truck_health_5m_retired;
One more detail: privileges belong to the object, not the name. In our run, a role that could SELECT from the old truck_health_5m couldn't read the new one after the swap. Apply grants to the _next object before you swap. If you deploy with the dbt-risingwave adapter's zero-downtime mode, it applies the model's grants to the new object before the swap for you.
v1.3: change an MV that other MVs read from
Before touching truck_latest, it helps to know how RisingWave wires MVs together. When shipment_eta_risk is created on top of truck_latest, RisingWave records the dependency between the two objects, by their internal IDs, not by their names. You can see these links in the rw_catalog.rw_depend catalog, which holds "the dependency relationships between tables, indexes, views, materialized views, sources, and sinks". A name is just how you look an object up. Two things follow:
- Swapping or renaming an upstream MV doesn't repoint its readers.
SWAP WITHexchanges names.shipment_eta_riskkeeps reading the same object it always has, which now carries the other name. The dbt-risingwave docs describe the same behaviour: dependents "stay attached to it under its new name, as they do after a swap". - You can't drop an object that something still reads. "Before you can remove a materialized view, you must remove all its dependent materialized views", unless you opt in with
CASCADE.
v1.3 fixes a bug in truck_latest: it ordered readings by arrival time (kafka_ts), so buffered readings that a truck uploaded after reconnecting could look "latest". The fix orders by event_ts.
What happens if you rebuild and swap only truck_latest, as in v1.2: the live name gets the fix, but SHOW CREATE MATERIALIZED VIEW shipment_eta_risk now says JOIN truck_latest_retired. Shipment risk is still computed from the buggy version. Cleanup fails too: DROP MATERIALIZED VIEW truck_latest_retired is refused because "materialized view public.shipment_eta_risk depends on it". To catch this before anyone notices, CI runs a guard query after every deploy. Any row means a live object still reads a retired one, so the deploy fails:
SELECT dep.name AS live_object, old.name AS still_reads
FROM rw_catalog.rw_depend d
JOIN rw_catalog.rw_relations dep ON dep.id = d.objid
JOIN rw_catalog.rw_relations old ON old.id = d.refobjid
WHERE old.name LIKE '%\_retired'
AND dep.name NOT LIKE '%\_retired';
After the naive swap, it returned:
live_object | still_reads
-------------------+----------------------
shipment_eta_risk | truck_latest_retired
(1 row)
The right way follows the docs' recipe for dependent jobs: rebuild the upstream MV and everything downstream of it, then cut them all over:
SET BACKGROUND_DDL = true;
CREATE MATERIALIZED VIEW truck_latest_next AS
SELECT truck_id, event_ts, lat, lon, speed_kph
FROM (
SELECT truck_id, event_ts, lat, lon, speed_kph,
row_number() OVER (PARTITION BY truck_id ORDER BY event_ts DESC, kafka_ts DESC) AS rn
FROM truck_telemetry
) ranked
WHERE rn = 1;
WAIT MATERIALIZED VIEW truck_latest_next; -- dependents can't see it until it's created
CREATE MATERIALIZED VIEW shipment_eta_risk_next AS
SELECT s.shipment_id, s.dest_region, s.promised_eta,
l.event_ts AS last_seen, l.speed_kph < 5 AS at_risk
FROM shipments s
JOIN truck_latest_next l ON s.truck_id = l.truck_id;
WAIT MATERIALIZED VIEW shipment_eta_risk_next;
ALTER MATERIALIZED VIEW truck_latest SWAP WITH truck_latest_next;
ALTER MATERIALIZED VIEW shipment_eta_risk SWAP WITH shipment_eta_risk_next;
ALTER MATERIALIZED VIEW truck_latest_next RENAME TO truck_latest_retired;
ALTER MATERIALIZED VIEW shipment_eta_risk_next RENAME TO shipment_eta_risk_retired;
Note the WAIT between levels: in background mode, an MV that's still being created can't be referenced yet. Without that WAIT, the second CREATE failed with "table or source not found". After the cut-over, the guard returns no rows, and shipment_eta_risk reads the fixed truck_latest. Once the rollback window has passed, drop the retired versions, leaf first:
DROP MATERIALIZED VIEW shipment_eta_risk_retired;
DROP MATERIALIZED VIEW truck_latest_retired;
DROP MATERIALIZED VIEW truck_health_5m_retired;
v1.4: cut a sink over with REPLACE SINK
v1.4 adds a low-fuel rule and a severity column to fleet_alerts, which feeds the fleet_alerts_sink Kafka sink. The MV goes through the same build-and-swap. But by now you know the sink would keep reading the retired MV, so it has to move too:
SET BACKGROUND_DDL = true;
CREATE MATERIALIZED VIEW fleet_alerts_next AS
SELECT truck_id, event_ts, 'overspeed' AS rule,
CASE WHEN speed_kph > 130 THEN 'high' ELSE 'medium' END AS severity
FROM truck_telemetry
WHERE speed_kph > 110
UNION ALL
SELECT truck_id, event_ts, 'low_fuel' AS rule, 'medium' AS severity
FROM truck_telemetry
WHERE fuel_pct < 10;
WAIT MATERIALIZED VIEW fleet_alerts_next;
ALTER MATERIALIZED VIEW fleet_alerts SWAP WITH fleet_alerts_next;
ALTER MATERIALIZED VIEW fleet_alerts_next RENAME TO fleet_alerts_retired;
REPLACE SINK fleet_alerts_sink FROM fleet_alerts
WITH (
connector = 'kafka',
properties.bootstrap.server = 'kafka:29092',
topic = 'fleet_alerts'
)
FORMAT PLAIN ENCODE JSON (force_append_only = 'true');
DROP MATERIALIZED VIEW fleet_alerts_retired;
REPLACE SINK is new in 3.1. It keeps the sink's name, and the sink "starts from the cut-over barrier and only forwards new incremental changes from the new upstream". In our run, the new MV's backfill computed 12 alerts from readings that were already in the table: the 2 overspeed alerts the old sink had already delivered, plus 10 low-fuel alerts the old logic never raised. REPLACE SINK sent none of them. The topic only received the four alerts triggered by readings that arrived after the cut-over, each with the new severity field.
For an alerts topic, that's the behaviour you want. Those 12 rows describe conditions that are already in the past, and sending them at cut-over would fire a burst of stale or duplicate alerts. It's different if the sink mirrors state, for example an upsert sink that keeps a table of every alert. That target would never receive the 10 low-fuel rows, so plan a separate backfill for it.
Things to know before you rely on it:
- Restate the full
WITHandFORMAT ... ENCODEclauses. - It takes
FROMonly;AS SELECTis rejected. snapshot, if set, must befalse.- It can't replace exactly-once sinks,
CREATE SINK ... INTOsinks, or sinks with automatic schema change. - Grants on the old sink aren't migrated; re-grant them.
It's new, and its tracking issue is still open, so rehearse it in CI on your own connector first.
If you can't use it, the older pattern still works: create a second sink with snapshot = false, so it skips historical rows, and then drop the old one (CREATE SINK). Expect a short overlap while both sinks are live.
CREATE SINK fleet_alerts_sink_v2 FROM fleet_alerts
WITH (
connector = 'kafka',
properties.bootstrap.server = 'kafka:29092',
topic = 'fleet_alerts',
snapshot = false
)
FORMAT PLAIN ENCODE JSON (force_append_only = 'true');
DROP SINK fleet_alerts_sink;
Wiring it into CI/CD
The SQL above is the what. The pipeline's job is to decide which pattern each change needs, prove the change works before it reaches production, and refuse to finish a deploy that leaves something behind. Our demo does this with two shell scripts, a pull-request check and a deploy, both driving dbt. With the adapter's zero_downtime option, dbt-risingwave generates the temporary MV, the WAIT, the SWAP WITH and the REPLACE SINK you saw above. If you use plain migration files instead, the steps are the same; only the commands change.
Step 1: classify what changed
Every run starts by comparing the code with what's deployed now. With dbt, that's dbt ls -s state:modified --state <dir>, where the directory holds the manifest saved by the last successful deploy. With plain SQL migrations, it's the set of files the pull request touches. Each changed object then maps to exactly one action:
| Changed object | Action | Also rebuild |
|---|---|---|
| Table or source | ALTER ... ADD COLUMN, in place | Nothing |
| Materialized view | Build _next, WAIT, swap | Everything downstream of it |
| Sink | REPLACE SINK ... FROM | Nothing |
| Anything else | Fail the run; plan it by hand | — |
The "Also rebuild" column is the v1.3 lesson turned into a rule. In dbt, the difference between a broken deploy and a correct one is selecting truck_latest+ instead of truck_latest. The last row matters too: a pipeline that stops on a change it doesn't recognise is safer than one that guesses.
Step 2: rehearse every pull request
Each pull request gets a disposable environment. That can be a throwaway database on a staging cluster (what our demo uses) or a RisingWave container started inside the CI job. Two checks run there:
- Fresh build. Deploy the pull request into an empty database. This proves that a brand-new environment built from git matches what production is about to become.
- Migration rehearsal. Deploy the base branch, let some data flow, then deploy the pull request on top with the same deploy script production uses. A fresh build only tests the end state. The rehearsal tests the transition, which is where streaming changes break.
The rehearsal paid for itself at v1.4. A SQL comment in the sink model made the generated REPLACE SINK fail. The fresh build creates the sink from scratch and never runs REPLACE SINK, so only the rehearsal could catch it, and it did, before merge.
One practical detail: give CI sinks their own targets. Our demo appends the database name to the Kafka topic in CI, so a rehearsal never writes into a production topic.
Step 3: deploy behind gates
On merge or a release tag, the deploy job runs the same script against production, in this order:
- Apply in-place changes first. New columns must exist before new MVs read them.
- Build every
_nextobject in the background with a backfill rate limit, waiting after each dependency level. - Cut over: swap the MVs, then replace the sinks.
- Gate: run data tests against the live names, then the dependency guard from v1.3. A single guard row fails the deploy.
- Clean up: drop retired versions leaf first, or keep them for a rollback window.
- Record the baseline: save what was deployed (for dbt, the manifest) for the next run's comparison.
Allow only one deploy at a time; in GitHub Actions, a concurrency group does this. And because classification compares against the last successful deploy, re-running a deploy that failed partway picks up the same set of changes again.
Rollback
How you roll back depends on what changed:
- An MV: swap back to the retired version while it still exists. That's the reason for a rollback window.
- A sink: there's no "un-replace". Revert the commit and redeploy. A
REPLACE SINKback to the old upstream also skips anything produced in between. - An added column: leave it. Existing MVs can't see it, so it's harmless.
Checklist
- Keep raw history in tables, so any new MV can backfill.
- Classify every change: in place, swap, or replace sink. Fail on anything else.
- Add columns before producers send them. Remember that existing MVs can't see them.
- Build new MVs beside the old ones, in the background and rate-limited.
WAITbetween dependency levels. - Rebuild everything downstream of a swapped MV, then guard with
rw_depend. - Re-apply grants to new objects before swapping.
- Cut sinks over with
REPLACE SINK, knowing it never backfills. - Rehearse the migration from the base version in CI, not just a fresh build.
- Drop retired versions last, leaf first.
