Foreign Connection Functions¶
The
sql_tvf_connect/sql_tvf_disconnectandesql_tvf_connect/esql_tvf_disconnectbuiltins manage the per-session cache of foreign connections used bysql_tvf,esql_tvf, and theENGINE = SQL|ESQLexternal tables. One session caches at most 16 distinct connections.
Description¶
Foreign connections are cached per session and live until an explicit disconnect or the session closes. Handles are derived from the connection config, so reconnecting with the same config reuses the cached entry. All cached connections are closed when the session ends.
Syntax¶
sql_tvf_connect(config)
sql_tvf_disconnect(handle)
esql_tvf_connect(config)
esql_tvf_disconnect(handle)
Arguments¶
Argument |
Description |
|---|---|
|
A JSON string describing the foreign source. SQL connections carry |
|
The connection handle returned by the matching |
Functions¶
Function |
Description |
|---|---|
|
Opens or reuses a SQL connection and returns a handle. |
|
Detaches and closes the SQL connection; returns |
|
Opens or reuses an Elasticsearch connection and returns a handle. |
|
Detaches and closes the Elasticsearch connection. |
The connection limit¶
A session caches at most 16 distinct foreign connections. The 17th distinct config is rejected with an error, and disconnecting a handle frees a slot. Whitespace differences in an otherwise identical config canonicalize to the same handle and do not consume another slot.
Usage Notes¶
Handles are deterministic per config; two concurrent scans with the same config reuse the first entry.
Connections are bound to the session’s CN and are not transferred by connection migration.
Disconnecting a handle that an external table is implicitly using removes it from the cache; the next scan reconnects.
Examples¶
SET @h1 = sql_tvf_connect('{"driver":"mysql","dsn":"user:pass@tcp(host:port)/db"}');
SET @h2 = esql_tvf_connect('{"addresses":["https://es:9200"]}');
SELECT sql_tvf_disconnect(@h1); -- true
SELECT sql_tvf_disconnect(@h1); -- false
These examples require reachable foreign sources and are not paste-and-run scripts.