SQL
Declare a SQL database, bind it to a Function, and run SQL statements on it from the handler through ctx.sql.
This page lists the spec and status fields of SQLDatabase, each with its type, its limits and its description. The Platform page explains the concept, and the metadata of every kind is the same.
SQLDatabase
apiVersion: funcd.io/v1alpha1
kind: SQLDatabase
metadata:
name: notes-db
namespace: default
resourceGroup: rg1
spec:
owner:
kind: Function
name: notes
quota:
maxSize: 1Gi
resources:
memory: 512Mi| apiVersion | kind |
|---|---|
funcd.io/v1alpha1 | SQLDatabase |
SQLDatabase.spec
| Field | Type and limits | Description |
|---|---|---|
owner | object, SQLDatabase.spec.owner | The one Function, Lake or Job that may write the database, in its namespace, named with its kind, besides the writers that a RolesAssignment names. In a database that a Function or a Job declares by name, it defaults to that workload, and in a Workflow's sql a Function owner names a step and a Lake owner a lake that the Workflow declares. A Lake writes no SQL, so a database a Lake owns is written by those writers only. With no owner, only those writers can write it. |
quota | object, SQLDatabase.spec.quota | A cap on the database's total size, the bytes of its pages in use. Once it is reached, an execute gets 507 unless each of its statements is a DELETE or a DROP. A change keeps the engine running. |
resources | object, SQLDatabase.spec.resources | The cpu and memory of the database's engine. An empty memory means 256Mi, and an empty cpu sets no cpu limit. A change replaces the running engine. |
SQLDatabase.spec.owner
| Field | Type and limits | Description |
|---|---|---|
kind | string, required, one of Function, Lake, Job | The workload's kind. A Function, a Lake and a Job may share a name, so the kind says which one it is; each field names the kinds it accepts. |
name | string, required, a DNS-1123 label | The workload's name, in the object's namespace. |
SQLDatabase.spec.quota
| Field | Type and limits | Description |
|---|---|---|
maxBytes | integer, at least 0 | The same cap as a number of bytes. Set maxSize or maxBytes, not both. |
maxSize | string, a size: 1 to 15 digits and an optional binary unit, Ki, Mi, Gi or Ti, with or without a B (512Ki, 2GiB) | The cap on the total size held, above zero. Set maxSize or maxBytes, not both; neither means no cap. A write that would pass the cap gets 507 in a KV store or a Bucket; a SQL execute call gets 507 once the database holds the cap. |
SQLDatabase.spec.resources
| Field | Type and limits | Description |
|---|---|---|
cpu | string | The CPU cap of the worker or engine it sizes, as a number of cores of at least 0.01, such as 0.5 or 2. Empty takes the default of the resource that declares it, or no cap when there is none. Only the containerd runtime enforces it. |
memory | string | The memory cap of the worker or engine it sizes, such as 256Mi or 4GiB: 1 to 15 digits and an optional binary unit, Ki, Mi, Gi or Ti, with or without a B, above zero. Empty takes the default of the resource that declares it, or no cap when there is none. Only the containerd runtime enforces it, and there a worker that uses more memory than the cap is killed. |
SQLDatabase.status
| Field | Type and limits | Description |
|---|---|---|
bindings | integer | The number of Functions whose spec.bindings.sql names the database. |
conditions | array of Condition | The conditions that explain the phase. |
observedGeneration | integer | The generation of the spec that funcd last acted on. |
phase | string, one of Pending, Deploying, Ready, Idle, Degraded, Failed, Terminating | Where the object is in its lifecycle. |
Handler API
The methods a handler calls on its context's clients, in each language funcd ships a shim for.
ctx.sql
The SQL databases that the function binds, by alias: query reads rows, queryValues keeps every column of a join, and execute writes in one transaction. A call fails with the HTTP status 503 while the database's engine is not Ready, or when a statement runs past funcd's SQL call timeout, 30 seconds by default.
| Method | What it does |
|---|---|
query(alias: string, sql: string, params?: SQLParams): Promise<SQLRow[]> | Runs one statement that reads, and returns its rows as objects keyed by column name; with a duplicate column name, the last one wins. params fill the statement's ? placeholders in order, or its placeholders from one object. A statement that writes, or that the database refuses, fails with the HTTP status 400. |
queryValues(alias: string, sql: string, params?: SQLParams): Promise<SQLValues> | Runs one statement that reads, and returns its column names and its rows as arrays of values in column order, so every column of a join is kept. params and failures are as for query. |
execute(alias: string, sql: string, params?: SQLParams): Promise<SQLExecResult> or execute(alias: string, statements: SQLStatement[]): Promise<SQLExecResult[]> | Runs one statement, or a list of up to 500 statements, in one transaction that applies all or none of them. Returns the statement's result, or one result for each statement of a list: rowsAffected and lastInsertId, with columns and values for a RETURNING statement. A statement that the database refuses fails the call with the HTTP status 400. Only the database's owner and the writers that a RolesAssignment names can execute. |
Each method here that calls funcd can also fail with the status 500 and the problem type urn:funcd:problem:internal, when funcd meets an error that it did not expect; funcd logs its cause.
When funcd refuses a call, the method fails with an error that holds funcd's status and problem type. This table gives them for each method, with when each happens and what to do.
| Method | Status | Problem type | When | What to do |
|---|---|---|---|---|
query | 400 | urn:funcd:problem:invalid | A statement is not a list whose first item is its SQL text, or that text is empty. | Send each statement as ["SQL", params...], with its SQL text. |
query | 400 | urn:funcd:problem:invalid | A parameter of a statement is a list or an object, other than the one object of its named parameters. | Pass each parameter as a string, a number, a boolean or null. |
query | 400 | urn:funcd:problem:invalid | The database refused a statement, as when its SQL is not valid, it breaks a constraint or a query writes; an execute then applies none of its statements. | Correct the statement that the detail names, with the database's error. |
query | 403 | urn:funcd:problem:forbidden | The calling Function no longer exists, so it binds no database. | Call from a Function that exists; a deleted Function's worker stops. |
query | 403 | urn:funcd:problem:forbidden | The Function's spec.bindings.sql has no entry for the alias. | Add the binding to spec.bindings.sql, or call an alias that the Function binds. |
query | 403 | urn:funcd:problem:forbidden | A Policy forbids the Function to read the database. | Change or remove the Policy that forbids sql::read on the database. |
query | 413 | urn:funcd:problem:payload-too-large | The request's body is larger than the local API takes for the call. | Send a smaller body. |
query | 413 | urn:funcd:problem:payload-too-large | The engine's answer to the call is larger than funcd reads for one call. | Read fewer rows or columns in one call, as with LIMIT. |
query | 503 | urn:funcd:problem:unavailable | The database's engine is not Ready yet; a query also gets this answer when the database that its binding names does not exist. | Wait until the SQLDatabase is Ready, then retry; create it if it does not exist. |
query | 503 | urn:funcd:problem:unavailable | The database's engine does not answer, its answer broke off, or it answered with an error. | Retry the call; if it keeps failing, read the SQLDatabase's status and funcd's log. |
query | 503 | urn:funcd:problem:unavailable | The database's engine did not answer in time, as when a statement runs past sql.callTimeout. | Make the statements faster, or raise sql.callTimeout. |
queryValues | 400 | urn:funcd:problem:invalid | A statement is not a list whose first item is its SQL text, or that text is empty. | Send each statement as ["SQL", params...], with its SQL text. |
queryValues | 400 | urn:funcd:problem:invalid | A parameter of a statement is a list or an object, other than the one object of its named parameters. | Pass each parameter as a string, a number, a boolean or null. |
queryValues | 400 | urn:funcd:problem:invalid | The database refused a statement, as when its SQL is not valid, it breaks a constraint or a query writes; an execute then applies none of its statements. | Correct the statement that the detail names, with the database's error. |
queryValues | 403 | urn:funcd:problem:forbidden | The calling Function no longer exists, so it binds no database. | Call from a Function that exists; a deleted Function's worker stops. |
queryValues | 403 | urn:funcd:problem:forbidden | The Function's spec.bindings.sql has no entry for the alias. | Add the binding to spec.bindings.sql, or call an alias that the Function binds. |
queryValues | 403 | urn:funcd:problem:forbidden | A Policy forbids the Function to read the database. | Change or remove the Policy that forbids sql::read on the database. |
queryValues | 413 | urn:funcd:problem:payload-too-large | The request's body is larger than the local API takes for the call. | Send a smaller body. |
queryValues | 413 | urn:funcd:problem:payload-too-large | The engine's answer to the call is larger than funcd reads for one call. | Read fewer rows or columns in one call, as with LIMIT. |
queryValues | 503 | urn:funcd:problem:unavailable | The database's engine is not Ready yet; a query also gets this answer when the database that its binding names does not exist. | Wait until the SQLDatabase is Ready, then retry; create it if it does not exist. |
queryValues | 503 | urn:funcd:problem:unavailable | The database's engine does not answer, its answer broke off, or it answered with an error. | Retry the call; if it keeps failing, read the SQLDatabase's status and funcd's log. |
queryValues | 503 | urn:funcd:problem:unavailable | The database's engine did not answer in time, as when a statement runs past sql.callTimeout. | Make the statements faster, or raise sql.callTimeout. |
execute | 400 | urn:funcd:problem:invalid | The call holds no statement. | Send at least one statement. |
execute | 400 | urn:funcd:problem:invalid | A statement is not a list whose first item is its SQL text, or that text is empty. | Send each statement as ["SQL", params...], with its SQL text. |
execute | 400 | urn:funcd:problem:invalid | A parameter of a statement is a list or an object, other than the one object of its named parameters. | Pass each parameter as a string, a number, a boolean or null. |
execute | 400 | urn:funcd:problem:invalid | The database refused a statement, as when its SQL is not valid, it breaks a constraint or a query writes; an execute then applies none of its statements. | Correct the statement that the detail names, with the database's error. |
execute | 403 | urn:funcd:problem:forbidden | The calling Function no longer exists, so it binds no database. | Call from a Function that exists; a deleted Function's worker stops. |
execute | 403 | urn:funcd:problem:forbidden | The Function's spec.bindings.sql has no entry for the alias. | Add the binding to spec.bindings.sql, or call an alias that the Function binds. |
execute | 403 | urn:funcd:problem:forbidden | A Policy forbids the Function to read the database. | Change or remove the Policy that forbids sql::read on the database. |
execute | 403 | urn:funcd:problem:forbidden | The Function does not own the database, no RolesAssignment makes it a writer, a Policy forbids the write, or the database that the binding names does not exist. | Execute from the database's owner, or grant the Function sql::write with a RolesAssignment, such as the SQL Data Writer role; create the database if it does not exist. |
execute | 413 | urn:funcd:problem:payload-too-large | The request's body is larger than the local API takes for the call. | Send a smaller body. |
execute | 413 | urn:funcd:problem:payload-too-large | The call holds more than 500 statements. | Send at most 500 statements in one call. |
execute | 413 | urn:funcd:problem:payload-too-large | The engine's answer to the call is larger than funcd reads for one call. | Read fewer rows or columns in one call, as with LIMIT. |
execute | 503 | urn:funcd:problem:unavailable | The database's engine is not Ready yet; a query also gets this answer when the database that its binding names does not exist. | Wait until the SQLDatabase is Ready, then retry; create it if it does not exist. |
execute | 503 | urn:funcd:problem:unavailable | The database's engine does not answer, its answer broke off, or it answered with an error. | Retry the call; if it keeps failing, read the SQLDatabase's status and funcd's log. |
execute | 503 | urn:funcd:problem:unavailable | The database's engine did not answer in time, as when a statement runs past sql.callTimeout. | Make the statements faster, or raise sql.callTimeout. |
execute | 503 | urn:funcd:problem:unavailable | funcd could not read the store's quota or measure what the store holds. | Retry the write. |
execute | 507 | urn:funcd:problem:insufficient-storage | funcd's data filesystem is below storage.minFreeSize, or the write would take it there. | Free space on the data filesystem, then retry; reads and deletes still work. |
execute | 507 | urn:funcd:problem:insufficient-storage | The write would take what a store or a prefix of a Bucket holds past its quota; the detail names which one. | Delete data to make room, or raise that quota: the store's spec.quota, or the quota of the prefix in its Bucket's spec.prefixes. |
HTTP API
The table lists each described operation of these kinds, with its method and path. The operation's page in the API reference gives its parameters, its body, its authentication and every answer, with when it happens and what to do.
| Method | Path | Operation |
|---|---|---|
GET | /apis/funcd.io/v1alpha1/namespaces/{namespace}/sqldatabases | List the SQLDatabases of a namespace |
POST | /apis/funcd.io/v1alpha1/namespaces/{namespace}/sqldatabases | Create a SQLDatabase |
GET | /apis/funcd.io/v1alpha1/namespaces/{namespace}/sqldatabases/{name} | Read a SQLDatabase |
PUT | /apis/funcd.io/v1alpha1/namespaces/{namespace}/sqldatabases/{name} | Replace a SQLDatabase |
DELETE | /apis/funcd.io/v1alpha1/namespaces/{namespace}/sqldatabases/{name} | Delete a SQLDatabase |
GET | /apis/funcd.io/v1alpha1/namespaces/{namespace}/sqldatabases/{name}/logs | Read the logs of a SQLDatabase's engine |