Skip to content

SQL

The SQL source reads mappings with a query against a relational database. It suits deployments where the record of which account lives on which backend already exists in a database, such as a provisioning system or a migration tracking table. The proxy then reads that record directly. PostgreSQL, MySQL and SQLite are supported, selected by the URL scheme.

[mapping]
source = "sql"
normalize = "lowercase"
[mapping.sql]
url = "postgres://proxy:[email protected]/maildb"
query = "SELECT destination FROM mailbox_routes WHERE identifier = $1"
pool_size = 16
KeyDefaultDescription
urlrequiredConnection URL. The scheme selects the driver: postgres://, mysql:// or sqlite://. A SQLite database file set through the web interface or the API must lie directly inside the mappings/ directory next to the configuration file, as described under path confinement.
queryrequiredLookup query, with the identifier as its only parameter, returning the destination.
pool_size16Maximum number of pooled connections.
upsert_queryunsetStatement that creates or replaces a mapping. Required for writes.
delete_queryunsetStatement that removes a mapping. Required for writes.
list_queryunsetQuery that returns one page of mappings. Required for listing.
count_queryunsetQuery that returns the number of mappings per destination.

The query runs with the normalised identifier as its first parameter, and the first column of the first row is the destination. A query that returns no row, or an empty or null value, means that the account has no mapping. The placeholder depends on the database: $1 for PostgreSQL, and ? for MySQL and SQLite:

[mapping.sql]
url = "mysql://proxy:[email protected]/maildb"
query = "SELECT destination FROM mailbox_routes WHERE identifier = ?"

The query may join tables, or apply any logic that the database supports, as long as it takes one identifier and returns one destination. Its result is cached, so it runs only on cache misses.

A SQL store is read-only unless both upsert_query and delete_query are set. The management API refuses writes to a read-only store with 501 Not Implemented and mapping store is read-only.

The upsert statement receives the identifier and the destination as its first and second parameters, and creates or replaces the mapping. The delete statement receives the identifier:

[mapping.sql]
url = "postgres://proxy:[email protected]/maildb"
query = "SELECT destination FROM mailbox_routes WHERE identifier = $1"
upsert_query = "INSERT INTO mailbox_routes (identifier, destination) VALUES ($1, $2) ON CONFLICT (identifier) DO UPDATE SET destination = excluded.destination"
delete_query = "DELETE FROM mailbox_routes WHERE identifier = $1"

Leaving the write statements unset is appropriate when the table belongs to another system and the proxy only reads it.

To tell a new mapping from a replaced one, the proxy runs the lookup query for each identifier before the upsert statement. A batch of writes runs in one transaction.

The management API can browse the mappings and show how many accounts each destination holds. A SQL store supports this only when it is given the queries for it, because the proxy cannot derive them from the lookup query.

The list_query returns one page of mappings as rows of identifier and destination, ordered by identifier. It receives three parameters. The first is a LIKE pattern that matches the identifiers starting with the requested prefix, with %, _ and \ escaped by a backslash, so the query declares ESCAPE '\'. The second is the last identifier of the previous page, or an empty string for the first page, and the query returns only identifiers after it. The third is the maximum number of rows. The proxy asks for one row more than the page holds, to learn whether another page follows, and rows with an empty or null destination are skipped:

[mapping.sql]
list_query = "SELECT identifier, destination FROM mailbox_routes WHERE identifier LIKE $1 ESCAPE '\\' AND identifier > $2 ORDER BY identifier LIMIT $3"
count_query = "SELECT destination, COUNT(*) FROM mailbox_routes WHERE destination IS NOT NULL GROUP BY destination"

The count_query takes no parameter and returns rows of destination and count. The total number of mappings is the sum of these counts, and rows naming an undeclared destination are left out of the per-destination figures. Without list_query the store is reported as not listable, and without count_query the counts are reported as unknown; lookups and writes work either way.