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
ParameterTypeRequiredDescription
dsnVARCHARYesConnection string; its scheme picks the database: 'postgresql://db:5432/prod', 'mysql://db:3306/shop', 'dynamodb://aws?region=eu-west-1'
timeoutDECIMALNoWITH property: connection timeout, such as '500ms'
tablesARRAY<VARCHAR>YesWITH property: tables the source exposes
aggregate_pushdownBOOLEANNoWITH property: the database computes the aggregations prepared statements read from it (PostgreSQL, MySQL / MariaDB; default TRUE); FALSE leaves them to the serving engine
usernameVARCHARNoWITH property: user, in place of the DSN's
passwordVARCHARNoWITH property: password, in place of the DSN's; write it as SECRET(...) so the registry never holds it
targetVARCHARNoWITH 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
  • tables limits 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

StatusBackends
Availableserving

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 VARCHARstatus VARCHARmessage VARCHAR
FM.TUTORIALS.EXTERNAL_JDBC.POSTGRES_CONNCREATEDFeature 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 BIGINTLAST_ORDER_ID BIGINTSTATUS VARCHAR
1101active
2202inactive