A materialized view
This tutorial keeps a snapshot of every merchant's 30-day revenue, refreshed on a schedule, then empties it with an empty keyset. A materialized view is a FeatureQL query over a keyset. featuremeshd runs it on a cron schedule or on demand, and lands its result in sinks that serving, or anything else, reads. It is a scheduled query, created with CREATE MATERIALIZED VIEW: the registry lists it among the features, but a query reads what it computes from its sinks, not from the view.
Each run replaces the previous snapshot. A daily view is a table refreshed daily, not a table with a partition per day. Every statement, clause and rule is in the Materialized views reference .
You need a test featuremeshd and PostgreSQL ( Tutorial settings ).
The features
CREATE OR REPLACE FEATURES IN FM.TUTORIALS.MV_REVENUE AS
PERSIST
pg := SOURCE_DB(
'postgresql://featuremesh@host.docker.internal:25432/featuremesh?sslmode=disable'
WITH (
TABLES = ARRAY('tut_mv_orders', 'tut_mv_merchants'),
PASSWORD = SECRET('env', 'FEATUREMESH_DEMOS_PG_PASSWORD')
)
),
merchants := ENTITY(),
orders := ENTITY(),
merchant_id := INPUT(BIGINT#merchants),
order_id := INPUT(BIGINT#orders),
req_time := INPUT(TIMESTAMP),
ks_merchants := KEYSET(
merchants,
'SELECT merchant_id FROM PG[tut_mv_merchants]'
),
ord := EXTERNAL_COLUMNS(
order_id BIGINT#orders BIND TO order_id,
merchant_id BIGINT#merchants,
amount DOUBLE,
order_time TIMESTAMP
FROM TABLE(pg[tut_mv_orders])
),
revenue_30d := RELATED(
SUM(ord[amount]) ON ord[merchant_id] = merchant_id
AND ord[order_time] > req_time - INTERVAL '30 DAY'
AND ord[order_time] <= req_time
),
revenue_latest := SINK(
'postgresql://featuremesh@host.docker.internal:25432/featuremesh?sslmode=disable'
TABLE 'tut_mv_revenue' KEY (MERCHANT_ID)
WITH (
MODE = 'latest',
PASSWORD = SECRET('env', 'FEATUREMESH_DEMOS_PG_PASSWORD')
)
)
;| feature_name VARCHAR | status VARCHAR | message VARCHAR |
|---|---|---|
| FM.TUTORIALS.MV_REVENUE.KS_MERCHANTS | CREATED | Feature created as not exists |
| FM.TUTORIALS.MV_REVENUE.MERCHANTS | CREATED | Feature created as not exists |
| FM.TUTORIALS.MV_REVENUE.MERCHANT_ID | CREATED | Feature created as not exists |
| FM.TUTORIALS.MV_REVENUE.ORD | CREATED | Feature created as not exists |
| FM.TUTORIALS.MV_REVENUE.ORDERS | CREATED | Feature created as not exists |
| FM.TUTORIALS.MV_REVENUE.ORDER_ID | CREATED | Feature created as not exists |
| FM.TUTORIALS.MV_REVENUE.PG | CREATED | Feature created as not exists |
| FM.TUTORIALS.MV_REVENUE.REQ_TIME | CREATED | Feature created as not exists |
| FM.TUTORIALS.MV_REVENUE.REVENUE_30D | CREATED | Feature created as not exists |
| FM.TUTORIALS.MV_REVENUE.REVENUE_LATEST | CREATED | Feature created as not exists |
REVENUE_30D is an ordinary feature, as of REQ_TIME. The keyset KS_MERCHANTS says which merchants a run covers. The sink's table is created before the view runs:
CREATE TABLE tut_mv_revenue (merchant_id BIGINT PRIMARY KEY, revenue_30d DOUBLE PRECISION,
request_time TIMESTAMP(6), compute_time TIMESTAMP(6), datakey TEXT, run BIGINT); The view
CREATE OR REPLACE MATERIALIZED VIEW FM.TUTORIALS.MV_REVENUE.REVENUE
TO FM.TUTORIALS.MV_REVENUE.REVENUE_LATEST
USING FM.TUTORIALS.MV_REVENUE.REQ_TIME := SCHEDULED_TIME()
WITH (SCHEDULE = '0 0 1 1 *')
AS
RETURN FM.TUTORIALS.MV_REVENUE.MERCHANT_ID AS merchant_id, FM.TUTORIALS.MV_REVENUE.REVENUE_30D AS revenue_30d
FOR FM.TUTORIALS.MV_REVENUE.MERCHANT_ID := @BIND_KEYSET(FM.TUTORIALS.MV_REVENUE.KS_MERCHANTS);| MESSAGE VARCHAR |
|---|
| ok |
The body is a FeatureQL query over the keyset. SCHEDULE = '0 0 1 1 *' runs it once a year, on 1 January. SCHEDULED_TIME(), bound to REQ_TIME, is the slot the run is for, 2026-01-01 00:00. The example counts the orders of the 30 days up to the slot, whenever the run executes ( Schedule and slots ).
RECOMPILE SERVING;| FEATURE VARCHAR | KIND VARCHAR | STATUS VARCHAR | MESSAGE VARCHAR | SINCE VARCHAR |
|---|---|---|---|---|
| FM.TUTORIALS.MV_REVENUE.REVENUE | MATERIALIZED_VIEW |
Running it now
RECOMPUTE MATERIALIZED VIEW runs the latest due slot as a new run. featuremeshd answers at once with the run it started, and runs it in a task of its own ( RECOMPUTE ).
A view loaded for the first time runs its latest due slot at once. A RECOMPUTE sent right after RECOMPILE SERVING can find that run in progress: wait for it, then ask again.
RECOMPUTE MATERIALIZED VIEW FM.TUTORIALS.MV_REVENUE.REVENUE;| VIEW VARCHAR | SLOT VARCHAR | RUN VARCHAR | STATUS VARCHAR |
|---|---|---|---|
| FM.TUTORIALS.MV_REVENUE.REVENUE | * | * | STARTED |
Once the run lands, the sink holds one row per merchant of the keyset. Its request_time is the slot. Merchant 1's order of November is outside the 30 days, and merchant 3 has no order:
SELECT merchant_id, COALESCE(revenue_30d::text, 'NULL'), request_time::text FROM tut_mv_revenue ORDER BY merchant_id;| MERCHANT_ID BIGINT | REVENUE VARCHAR | REQUEST_TIME VARCHAR |
|---|---|---|
| 1 | 10 | 2026-01-01 00:00:00 |
| 2 | 7 | 2026-01-01 00:00:00 |
| 3 | NULL | 2026-01-01 00:00:00 |
When the keyset is empty
A run replaces the whole snapshot, so a run over no key empties it. Remove every merchant from the keyset's table:
DELETE FROM tut_mv_merchants;Run the view again:
RECOMPUTE MATERIALIZED VIEW FM.TUTORIALS.MV_REVENUE.REVENUE;| VIEW VARCHAR | SLOT VARCHAR | RUN VARCHAR | STATUS VARCHAR |
|---|---|---|---|
| FM.TUTORIALS.MV_REVENUE.REVENUE | * | * | STARTED |
The run published an empty snapshot, and the table lost every row:
SELECT merchant_id, COALESCE(revenue_30d::text, 'NULL'), request_time::text FROM tut_mv_revenue ORDER BY merchant_id;| merchant_id BIGINT | revenue VARCHAR | request_time VARCHAR |
|---|
A keyset query that fails to find its keys, or a WHERE that keeps none, does the same. Run a view from an orchestrator guards against it.
What you learned
- A materialized view runs a FeatureQL query over a keyset, on a schedule, and lands the result in its sinks.
SCHEDULED_TIME()binds the slot a run is for, whenever the run executes.RECOMPUTE MATERIALIZED VIEWstarts a run at once, and answers before it lands.- Each run replaces the snapshot: an empty keyset empties the sink.
Next
- Precompute in a Python process runs the same kind of hook inside your own program.
- Materialized views reference : how a run lands, reading a view, lifecycle.
- Run a view from an orchestrator : trigger over HTTP, follow the outcome, guard against an empty keyset.
- Limits .