Database sources
SOURCE_DB() connects FeatureQL to the databases your services already read: PostgreSQL, MySQL and DynamoDB. Real-time features read their rows by key, without a data-fetching service in between.
Creating a database source
Establish a connection with a connection string; its scheme picks the database:
CREATE OR REPLACE FEATURE FM.TUTORIALS.EXTERNAL_JDBC.POSTGRES_CONN AS
SOURCE_DB(
'postgresql://featuremesh@host.docker.internal:25432/featuremesh?sslmode=disable'
WITH (
password = SECRET('env', 'FEATUREMESH_DEMOS_PG_PASSWORD'),
tables=ARRAY['serving_slt_customers', 'serving_slt_customer_orders'],
timeout='500ms'
)
);| feature_name VARCHAR | status VARCHAR | message VARCHAR |
|---|---|---|
| FM.TUTORIALS.EXTERNAL_JDBC.POSTGRES_CONN | CREATED | Feature created as not exists |
The tables array acts as an allowlist — only the listed tables and views are accessible through this connection. This is both a security measure and a documentation aid: anyone reading the feature definition can see exactly which tables it touches.
The password is not written in the definition: SECRET('env', 'FEATUREMESH_DEMOS_PG_PASSWORD') is read from the serving engine's environment when it connects. See Credentials .
In a local development process, synchronously install the persisted connection before querying it:
RECOMPILE SERVING;| FEATURE VARCHAR | KIND VARCHAR | STATUS VARCHAR | MESSAGE VARCHAR | SINCE VARCHAR |
|---|---|---|---|---|
| FM.TUTORIALS.EXTERNAL_JDBC.POSTGRES_CONN | SOURCE_DB | LOADED |
The recompile returns only after the DataFusion catalog can resolve the configured table.
Supported databases
The DSN's scheme picks the database:
| Database | DSN | Reads |
|---|---|---|
| PostgreSQL | postgresql://user@host:5432/db?sslmode=require | By key, children by key, and aggregations computed in the database |
| MySQL, MariaDB | mysql://user@host:3306/db?sslmode=required | By key, children by key, and aggregations computed in the database (integer keys, no time windows yet) |
| DynamoDB | dynamodb://aws?region=eu-west-1 | By partition key; aggregations in the serving engine |
A MySQL source is declared like a PostgreSQL one:
MYSQL_CONN := SOURCE_DB(
'mysql://featuremesh@db.internal:3306/shop?sslmode=required'
WITH (
password = SECRET('env', 'MYSQL_PASSWORD'),
tables = ARRAY('orders', 'customers')
)
)
DynamoDB
A DynamoDB table is read by its partition key: a table without a sort key with BatchGetItem (100 keys a request), a table with one with a Query per key, which returns all the items of the key. A read without keys fails rather than scan the table.
DYNAMO := SOURCE_DB(
'dynamodb://aws?region=eu-west-1'
WITH (tables = ARRAY('account_events', 'users'), timeout = '2s')
),
USR := EXTERNAL_COLUMNS(
user_id BIGINT#USERS BIND TO USER_ID,
segment VARCHAR
FROM TABLE(DYNAMO[users])
)
- The host
awsstands fordynamodb.<region>.amazonaws.com. Any other host is used as is, such as DynamoDB Local onlocalhost:8000(withtls=falseoutside localhost). - Credentials: the DSN's user and password are the access key and secret key (set them with
username = SECRET(...)andpassword = SECRET(...)). Without them, the serving engine reads the standardAWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEYandAWS_SESSION_TOKEN. The region isregion=, elseAWS_REGION, elseus-east-1. - Types: DynamoDB has no schema, so the serving engine samples the table.
SisVARCHAR;NisBIGINTwhen the sampled values are integers,DECIMALotherwise;BOOLisBOOLEAN; maps, lists, sets and binary values are their DynamoDB JSON asVARCHAR. A missing attribute isNULL. Declare the types you expect inEXTERNAL_COLUMNS().
Querying with EXTERNAL_COLUMNS()
Read a table with EXTERNAL_COLUMNS() and FROM TABLE(source[table]): the source feature, then the table in brackets. This maps database columns directly to feature fields, just like batch data source mappings:
DEFINE
customer_id := INPUT(BIGINT),
postgres_src := EXTERNAL_COLUMNS(
customer_id BIGINT BIND TO customer_id,
last_order_id BIGINT,
status VARCHAR
FROM TABLE(fm.tutorials.external_jdbc.postgres_conn[serving_slt_customers])
)
RETURN
customer_id,
last_order_id := postgres_src[last_order_id],
status := postgres_src[status]
FOR
customer_id := BIND_VALUES(ARRAY(1, 2))
;| CUSTOMER_ID BIGINT | LAST_ORDER_ID BIGINT | STATUS VARCHAR |
|---|---|---|
| 1 | 101 | active |
| 2 | 202 | inactive |
The BIND TO CUSTOMER_ID clause links the customer_id column to the input feature, so FeatureMesh knows how to parameterize the query.
The connection reads current database state. Updating customer 2 in PostgreSQL changes the next serving result without recreating the source:
DEFINE
customer_id := INPUT(BIGINT),
postgres_src := EXTERNAL_COLUMNS(
customer_id BIGINT BIND TO customer_id,
last_order_id BIGINT,
status VARCHAR
FROM TABLE(fm.tutorials.external_jdbc.postgres_conn[serving_slt_customers])
)
RETURN
customer_id,
last_order_id := postgres_src[last_order_id],
status := postgres_src[status]
FOR
customer_id := BIND_VALUE(2)
;| CUSTOMER_ID BIGINT | LAST_ORDER_ID BIGINT | STATUS VARCHAR |
|---|---|---|
| 2 | 303 | active |
Reading the children of a key
A customer has many orders. The orders table is keyed by order_id, but a feature of a customer wants the orders whose customer_id is the customer's: a read by a column that is not the table's key, returning several rows per customer. RELATED() aggregates them:
DEFINE
customers := ENTITY(),
orders := ENTITY(),
customer_id := INPUT(BIGINT#customers),
order_id := INPUT(BIGINT#orders),
ord := EXTERNAL_COLUMNS(
order_id BIGINT#orders BIND TO order_id,
customer_id BIGINT#customers,
amount DOUBLE
FROM TABLE(fm.tutorials.external_jdbc.postgres_conn[serving_slt_customer_orders])
),
of_customer := ord[customer_id] = customer_id
RETURN
customer_id,
n_orders := RELATED(COUNT() ON of_customer),
total := RELATED(SUM(ord[amount]) ON of_customer),
largest := RELATED(MAX(ord[amount]) ON of_customer)
FOR CROSS
customer_id := BIND_VALUES(ARRAY(1, 2, 3)),
NESTED order_id := @BIND_KEYSET(ord)
;| CUSTOMER_ID BIGINT | N_ORDERS BIGINT | TOTAL DOUBLE | LARGEST DOUBLE |
|---|---|---|---|
| 1 | 2 | 55.5 | 40.0 |
| 2 | 3 | 60.0 | 30.0 |
| 3 | 0 | NULL | NULL |
ORDmaps the orders table, keyed byorder_id. Itscustomer_idcolumn is annotated#CUSTOMERS.OF_CUSTOMERis the condition that ties an order to the customer. Its equality oncustomer_idis what the source is read by: one query for all the customers of the request,WHERE customer_id IN (...). Index the column.- Each
RELATED(aggregate ON OF_CUSTOMER)aggregates the orders of each customer. The three share their condition, so the orders are read once. NESTED ORDER_ID := @BIND_KEYSET(ORD)makes the orders a nested domain of each customer: the answer has one row per customer. A prepared statement makes this binding itself (see What a prepared statement may read ).
Customer 3 has no orders: the count is 0, the sum and the maximum are NULL.
Where aggregations run
In a prepared statement over PostgreSQL, the aggregations and everything they read (the filter, the expressions inside the aggregate) are computed by PostgreSQL: one GROUP BY query per call, one row per key back. Logic over the aggregates, joins with other sources, Redis and HTTP reads run in the serving engine. MySQL and MariaDB do the same for COUNT, COUNT_IF, SUM, MIN, MAX and AVG of DOUBLE values over a table read by an integer key; the call's keys reach the database as one JSON array, so MariaDB needs 10.6 or later. Declare the engine with the source property target (target = 'mysql/mariadb'). AVG of integers, text keys and time windows stay in the serving engine. The source property aggregate_pushdown = FALSE keeps the aggregations in the serving engine, which then reads the rows by key. DynamoDB is always read by key, its aggregations computed in the serving engine.
On a time window, with PostgreSQL, a prepared statement of Aggregating a key's recent history sends the aggregation to the database (a free-form query computes it in the serving engine). For each call, PostgreSQL receives one query with the accounts as a parameter and the 30-day bound already computed from the call's time. In spirit, it looks like this:
SELECT account_id, COUNT(*), SUM(amount), COUNT(*) FILTER (WHERE kind = 'refund')
FROM serving_slt_history_events
WHERE account_id = ANY($1) AND event_ts > $2
GROUP BY account_id Only one row per account crosses the network, and the index on (account_id, event_ts) serves the read. The three aggregates of LAST_30D share their condition, IN_LAST_30D, so they are computed by this one query rather than three.
With aggregate_pushdown = FALSE, the serving engine reads the account's rows (still by key, with the window filter) and aggregates them itself:
CREATE OR REPLACE FEATURES IN FM.TUTORIALS.HISTORY AS
PERSIST
POSTGRES_ROWS := SOURCE_DB(
'postgresql://featuremesh@host.docker.internal:25432/featuremesh?sslmode=disable'
WITH (
password = SECRET('env', 'FEATUREMESH_DEMOS_PG_PASSWORD'),
tables = ARRAY['serving_slt_history_events'],
timeout = '2s',
aggregate_pushdown = FALSE
)
),
EVT_ROWS := EXTERNAL_COLUMNS(
event_id BIGINT#EVENTS BIND TO EVENT_ID,
account_id BIGINT#ACCOUNTS,
event_ts TIMESTAMP,
amount DOUBLE,
kind VARCHAR
FROM TABLE(FM.TUTORIALS.HISTORY.POSTGRES_ROWS[serving_slt_history_events])
),
IN_LAST_30D_ROWS := EVT_ROWS[account_id] = ACCOUNT_ID AND EVT_ROWS[event_ts] > REQ_TS - INTERVAL '30 days',
LAST_30D_ENGINE := ROW(
RELATED(COUNT() ON IN_LAST_30D_ROWS) AS n_events,
RELATED(SUM(EVT_ROWS[amount]) ON IN_LAST_30D_ROWS) AS total,
RELATED(COUNT_IF(EVT_ROWS[kind] = 'refund') ON IN_LAST_30D_ROWS) AS n_refunds
),
LAST_30D_ENGINE_PS := PREPARED_STATEMENT(LAST_30D_ENGINE USING (ACCOUNT_ID, REQ_TS := REQUEST_TIME()));| feature_name VARCHAR | status VARCHAR | message VARCHAR |
|---|---|---|
| FM.TUTORIALS.HISTORY.EVT_ROWS | CREATED | Feature created as not exists |
| FM.TUTORIALS.HISTORY.IN_LAST_30D_ROWS | CREATED | Feature created as not exists |
| FM.TUTORIALS.HISTORY.LAST_30D_ENGINE | CREATED | Feature created as not exists |
| FM.TUTORIALS.HISTORY.LAST_30D_ENGINE_PS | CREATED | Feature created as not exists |
| FM.TUTORIALS.HISTORY.POSTGRES_ROWS | CREATED | Feature created as not exists |
The answer is the same:
FM.TUTORIALS.HISTORY.LAST_30D_ENGINE_PS{"input_1": [[1], [2], [3]]} | ACCOUNT_ID BIGINT | REQ_TS VARCHAR | LAST_30D_ENGINE_PS ROW |
|---|---|---|
| 1 | * | {n_events: 2, total: 35.0, n_refunds: 1} |
| 2 | * | {n_events: 1, total: 30.0, n_refunds: 0} |
| 3 | * | {n_events: 0, total: NULL, n_refunds: 0} |
When to turn pushdown off is in Choosing a pattern .
Redis holds no rows to aggregate, but a sorted set read as a history gives the same feature from a precomputed list.
Credentials
A source reads its password with SECRET(). The providers, what an unreadable secret does and when a rotated one applies are in Credentials .
Column types
Declare exact column types in EXTERNAL_COLUMNS(). Type mismatches between the database and FeatureQL cause runtime errors, not compile-time warnings.
How to prepare a database for serving, connections, load and indexes, is in Prepare the stores .