SOURCE_DB()
All functions > SOURCE > SOURCE_DB()
Creates a connection to an operational database (PostgreSQL, MySQL, DynamoDB), picked by the DSN's scheme
Signatures
Returns: Database source for online reads
SOURCE_DB(
dsn
WITH ( tables = ARRAY['table', ...] [, timeout = duration ] [, aggregate_pushdown = boolean ]
[, username = varchar ] [, password = varchar ] [, target = varchar ] )
) → DBSOURCE| Parameter | Type | Required | Description |
|---|---|---|---|
dsn | VARCHAR | Yes | Connection string; its scheme picks the database: 'postgresql://db:5432/prod', 'mysql://db:3306/shop', 'dynamodb://aws?region=eu-west-1' |
timeout | DECIMAL | No | WITH property: connection timeout, such as '500ms' |
tables | ARRAY<VARCHAR> | Yes | WITH property: tables the source exposes |
aggregate_pushdown | BOOLEAN | No | WITH property: the database computes the aggregations prepared statements read from it (PostgreSQL, MySQL / MariaDB; default TRUE); FALSE leaves them to the serving engine |
username | VARCHAR | No | WITH property: user, in place of the DSN's |
password | VARCHAR | No | WITH property: password, in place of the DSN's; write it as SECRET(...) so the registry never holds it |
target | VARCHAR | No | WITH property: the engine and version behind the DSN, as a target ('mysql/mariadb', 'postgres'); the aggregations it computes are compiled for it. Default: the DSN scheme's family |
Notes
- Connects to an operational database: PostgreSQL, MySQL or DynamoDB, by the DSN's scheme
- Used for real-time feature serving
- Connection pooled and reused across requests
tableslimits which tables queries can reach- Online-only restriction (for low-latency serving)
- Read a table with
EXTERNAL_COLUMNS(... FROM TABLE(source[table])) - Credentials:
password = SECRET('env', 'PG_PASSWORD')keeps the password out of the registry; a password written in the DSN or as a literal raises the warning SOURCE-INLINE-PASSWORD
Availability
| Status | Backends |
|---|---|
| Available | serving |
See also
Examples
Persist a database source
FeatureQL
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'
)
);Result
| feature_name VARCHAR | status VARCHAR | message VARCHAR |
|---|---|---|
| FM.TUTORIALS.EXTERNAL_JDBC.POSTGRES_CONN | CREATED | Feature created as not exists |
Read a table of the source with EXTERNAL_COLUMNS(... FROM TABLE(source[table]))
FeatureQL
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))
;Result
| CUSTOMER_ID BIGINT | LAST_ORDER_ID BIGINT | STATUS VARCHAR |
|---|---|---|
| 1 | 101 | active |
| 2 | 202 | inactive |