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

FeatureQL
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')
        )
    )
;
Result
feature_name VARCHARstatus VARCHARmessage VARCHAR
FM.TUTORIALS.MV_REVENUE.KS_MERCHANTSCREATEDFeature created as not exists
FM.TUTORIALS.MV_REVENUE.MERCHANTSCREATEDFeature created as not exists
FM.TUTORIALS.MV_REVENUE.MERCHANT_IDCREATEDFeature created as not exists
FM.TUTORIALS.MV_REVENUE.ORDCREATEDFeature created as not exists
FM.TUTORIALS.MV_REVENUE.ORDERSCREATEDFeature created as not exists
FM.TUTORIALS.MV_REVENUE.ORDER_IDCREATEDFeature created as not exists
FM.TUTORIALS.MV_REVENUE.PGCREATEDFeature created as not exists
FM.TUTORIALS.MV_REVENUE.REQ_TIMECREATEDFeature created as not exists
FM.TUTORIALS.MV_REVENUE.REVENUE_30DCREATEDFeature created as not exists
FM.TUTORIALS.MV_REVENUE.REVENUE_LATESTCREATEDFeature 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);
sql

The view

FeatureQL
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);
Result
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 ).

FeatureQL
RECOMPILE SERVING;
Result
FEATURE VARCHARKIND VARCHARSTATUS VARCHARMESSAGE VARCHARSINCE VARCHAR
FM.TUTORIALS.MV_REVENUE.REVENUEMATERIALIZED_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.

FeatureQL
RECOMPUTE MATERIALIZED VIEW FM.TUTORIALS.MV_REVENUE.REVENUE;
Result
VIEW VARCHARSLOT VARCHARRUN VARCHARSTATUS 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:

Postgres
SELECT merchant_id, COALESCE(revenue_30d::text, 'NULL'), request_time::text FROM tut_mv_revenue ORDER BY merchant_id;
Result
MERCHANT_ID BIGINTREVENUE VARCHARREQUEST_TIME VARCHAR
1102026-01-01 00:00:00
272026-01-01 00:00:00
3NULL2026-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:

Postgres
DELETE FROM tut_mv_merchants;
Result

Run the view again:

FeatureQL
RECOMPUTE MATERIALIZED VIEW FM.TUTORIALS.MV_REVENUE.REVENUE;
Result
VIEW VARCHARSLOT VARCHARRUN VARCHARSTATUS VARCHAR
FM.TUTORIALS.MV_REVENUE.REVENUE**STARTED

The run published an empty snapshot, and the table lost every row:

Postgres
SELECT merchant_id, COALESCE(revenue_30d::text, 'NULL'), request_time::text FROM tut_mv_revenue ORDER BY merchant_id;
Result
merchant_id BIGINTrevenue VARCHARrequest_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 VIEW starts a run at once, and answers before it lands.
  • Each run replaces the snapshot: an empty keyset empties the sink.

Next