New External resources were introduced in DuckDB 2.0.0.
The CREATE EXTERNAL RESOURCE statement provisions an underlying resource, such as a database server, of a registered external resource type, and creates a resource registration for it under a name. The other statements on external resources are:
REGISTER EXTERNAL RESOURCEcreates a resource registration for an underlying resource from its handle.ATTACH TO EXTERNAL RESOURCEandCONNECT TO EXTERNAL RESOURCEuse the endpoint of an underlying resource.SHOW EXTERNAL RESOURCESlists the resource registrations.DESTROY EXTERNAL RESOURCEtears down an underlying resource and removes its resource registration.
Note
CREATE EXTERNAL RESOURCEis refused whenenable_external_accessisfalse. See Restrictions.
Syntax
CREATE EXTERNAL RESOURCE type AS name [(option [value], ...)];
typeis the name of a registered external resource type, written as described in External Resource Type Names.nameis the name of the resource registration. Resource registration names are unique within the DuckDB instance.- The options and their values are passed to the create callback of the external resource type, as a
MAP(VARCHAR, VARCHAR)with the option names in lowercase. See Lifecycle Callbacks.
CREATE EXTERNAL RESOURCE calls the create callback of the external resource type, then its status callback until it reports ready, and creates a resource registration with the handle and the endpoint they reported.
Examples
Register an external resource type with table macros as its lifecycle callbacks, create a resource registration of that external resource type, and list it:
CREATE MACRO dt_create(p) AS TABLE SELECT MAP {'id': 'd1'} AS handle;
CREATE MACRO dt_status(h) AS TABLE
SELECT 'ready' AS state, MAP {'uri': ':memory:', 'attached_db_type': 'duckdb'} AS result;
CREATE MACRO dt_destroy(h) AS TABLE SELECT 'gone' AS status;
CALL register_external_resource_type(
'test.dotted.type',
kind := 'catalog',
create_function := 'dt_create',
status_function := 'dt_status',
destroy_function := 'dt_destroy'
);
CREATE EXTERNAL RESOURCE test.dotted.type AS created;
SELECT name, type FROM (SHOW EXTERNAL RESOURCES);
| name | type |
|---|---|
| created | test.dotted.type |
Errors
The name is required:
CREATE EXTERNAL RESOURCE 'mem@local';
Binder Error: EXTERNAL RESOURCE requires a name (AS <name>)
An unquoted external resource type name is folded to lowercase, and a name that is not registered is an error:
CREATE EXTERNAL RESOURCE Nobody.Provides.THIS AS r;
… unknown resource type "nobody.provides.this" …
A name with characters other than letters, digits, _, . and - must be written as a string literal:
CREATE EXTERNAL RESOURCE aws:cloudformation:stack AS r;
Parser Error: …
A resource registration name that is already registered is an error:
CREATE MACRO n_create(p) AS TABLE SELECT MAP {'id': 'n1'} AS handle;
CREATE MACRO n_status(h) AS TABLE
SELECT 'ready' AS state, MAP {'uri': ':memory:', 'attached_db_type': 'duckdb'} AS result;
CALL register_external_resource_type(
'mem@local',
kind := 'catalog',
create_function := 'n_create',
status_function := 'n_status'
);
CREATE EXTERNAL RESOURCE 'mem@local' AS r;
CREATE EXTERNAL RESOURCE 'mem@local' AS r;
Invalid Input Error: external resource "r" is already registered
An external resource type without a status callback cannot provision an underlying resource:
CREATE MACRO s_create(p) AS TABLE SELECT MAP {'id': 's1'} AS handle;
CALL register_external_resource_type('mem@local', kind := 'catalog', create_function := 's_create');
CREATE EXTERNAL RESOURCE 'mem@local' AS r;
… resource type "mem@local" has no status function (required to await readiness)
For a catalog external resource type, a status callback whose result lacks the uri is an error:
CREATE MACRO c_create(p) AS TABLE SELECT MAP {'id': 'c1'} AS handle;
CREATE MACRO c_status(h) AS TABLE SELECT 'ready' AS state, MAP {'foo': 'bar'} AS result;
CALL register_external_resource_type(
'cat@local',
kind := 'catalog',
create_function := 'c_create',
status_function := 'c_status'
);
CREATE EXTERNAL RESOURCE 'cat@local' AS r;
… catalog resource "cat@local" is missing 'uri' in its status result
When enable_external_access is false, CREATE EXTERNAL RESOURCE is refused:
CREATE MACRO mock_create(p) AS TABLE SELECT MAP {'id': 'r1'} AS handle;
CALL register_external_resource_type(
'mock@local',
kind := 'catalog',
create_function := 'mock_create'
);
SET enable_external_access = false;
CREATE EXTERNAL RESOURCE 'mock@local' AS res;
Permission Error: External resources are disabled through configuration