Per-user shards
Per-user shards let the enterprise gateway put each customer on a server of its own while every customer connects to the same address. A login (the database and role a client names) is mapped to one shard, and its whole session runs there. The customer's tables need no tenant column and no shard key: its database simply lives on its shard. Use it for a database-as-a-service platform with more customers than one server holds, or to keep departments on separate machines.
It needs the enterprise gateway in front of the shards and the enterprise server on each shard. Set up the gateway and the shards first as in Installing and running the gateway; this page covers what per-user shards add.
How it works
- A client connects to the gateway and names its database and role.
- The gateway looks the login up in its routes. For a login routed to a shard, it checks the client's password against that shard's roles, so the role needs to exist only there.
- The session is locked to that shard for its life:
- every statement goes to that shard, without looking for a route key;
SET rdb.route_key,SET LOCAL rdb.route_keyand a nativeroute=option are refused with42501;- a statement is never fanned out to other shards.
- The shard opens the session only if it hosts that role: the role is listed in its enterprise configuration (
pinned_role) or was added withSHARD HOST ROLE. Otherwise it refuses withrole "<role>" of "<database>" is not placed on this shard: refresh the user's route. A stale or wrong route therefore reaches no data.
A login routed to refuse is turned away after its password is checked, with 28000. A login with no route is routed by statement as usual, unless the gateway requires a route for every login.
Routes are read at login: changing one affects the role's next login, not its open sessions.
Turn it on
On each shard, run the enterprise server with an enterprise configuration file that lets it host roles. A run-time list is what the configuration API needs:
# enterprise.conf on every shard
operator_database postgres
pinned_roles_file /var/lib/rasterizeddb/rdb_pinned_rolesAdd tenant_mode strict as well to isolate the customers on each shard from each other (Tenants).
On the gateway, set user_routing in its configuration file and give it routes, from a file or from the configuration API:
# gateway.conf (only the lines this page needs)
user_routing = preferuser_routing | A routed login | A login routed to refuse | A login with no route |
|---|---|---|---|
off (default) | routes are ignored | routes are ignored | routed by statement |
prefer | locked to its shard | refused | routed by statement |
require | locked to its shard | refused | refused: role "<role>" is not mapped to a shard on this gateway |
The enterprise flag --user-routes-require also refuses a login with no route (role "<role>" has no shard for "<database>"), whatever user_routing says other than off.
user_routing = prefer or require needs the enterprise gateway with --user-routes FILE or a config_dir; without either it stops at start with user_routing needs a build with per-user shards.
Routes from a file
Start the gateway with --user-routes:
rasterizeddb-gateway-enterprise --config /etc/rasterizeddb/gateway.conf --user-routes /etc/rasterizeddb/user-routesThe file has one database role shard line per login, or database role refuse:
# database role shard
acme acme_app 1
globex globex_app 2
globex intern refuseThe gateway rereads the file within 5 seconds of a change. A file with a bad line is not loaded: at start the gateway stops, later it logs the error and keeps its old routes. The configuration file page has the format in full.
With --user-routes, the configuration API shows the routes but does not change them (it answers 409).
Host a role on a shard
A shard admits a routed session only for a role it hosts. Host roles at run time with these statements, run on the shard as a superuser of its operator database:
SHARD HOST ROLE 'acme' 'acme_app';
SHARD UNHOST ROLE 'acme' 'acme_app';The first argument is the database, the second the role, both as string literals. Each statement is written to pinned_roles_file before it takes effect, so the list survives a restart; hosting a role already hosted, or unhosting one that is not, changes nothing. Without pinned_roles_file both are refused (this shard keeps no run-time list of hosted roles). A name with a space or # is refused, since the file could not hold it.
Roles that never change can instead be listed in the enterprise configuration file, one line each:
pinned_role acme acme_appThese need a restart to change, and SHARD UNHOST ROLE does not remove them.
Add a customer by hand
Customer acme, login acme_app, on shard 1. On shard 1, connected to its operator database postgres as a superuser:
CREATE DATABASE acme;Then, connected to acme on shard 1 as the same superuser:
CREATE ROLE acme_app LOGIN PASSWORD 'Acme-app-pass-2026';
GRANT ALL ON DATABASE acme TO acme_app;
GRANT CREATE ON SCHEMA public TO acme_app;Back in postgres on shard 1:
SHARD HOST ROLE 'acme' 'acme_app';Add the route on the gateway (to the routes file, or with the API below):
acme acme_app 1The customer connects to the gateway (6432 is the gateway's default port):
psql "host=gateway.example.com port=6432 dbname=acme user=acme_app"On a shard in strict tenant mode, CREATE TENANT can make the database and an owner role in one statement instead (Tenants); host the owner role with SHARD HOST ROLE and route it in the same way.
The configuration API
The gateway's HTTP configuration API can keep the routes for you and provision a customer in one call. Turn it on with the gateway keys api_listen, api_token_file and config_dir, and leave --user-routes off: the routes are then kept in the gateway's configuration store and loaded at every start. See Gateway administration for the API's own paths (health, map, settings, audit).
# gateway.conf
user_routing = require
api_listen = 10.0.0.5:7000
api_token_file = /etc/rasterizeddb/api.token
config_dir = /var/lib/rasterizeddb-gatewayEvery request carries the token from api_token_file; bodies are JSON. The API is plain HTTP with no TLS of its own: let only the operator's own network reach its port. Every request that changes something is recorded in the API's audit log (GET /v1/audit), without its body.
TOKEN="$(head -n 1 /etc/rasterizeddb/api.token)"
API=http://10.0.0.5:7000Paths
| Method | Path | Body | Success |
|---|---|---|---|
GET | /v1/routes | none | 200, every route |
PUT | /v1/routes/{database}/{role} | {"shard": N} or {"refuse": true} | 200, the route stored and live at the role's next login |
DELETE | /v1/routes/{database}/{role} | none | 200, the route removed |
POST | /v1/customers | {"database", "role", "password", "shard"} | 201, the database, its role, the hosting and the route made |
GET | /v1/clients | none | 200, the client statistics |
Errors answer {"error": "…"} with the status:
| Status | When |
|---|---|
400 | A body that is not JSON, or not the shape the path takes; a shard that is not in the map; for customers, a bad name or a short password. |
401 | No token, or the wrong one. |
404 | DELETE of a route that does not exist; /v1/clients with the statistics off; an unknown path. |
409 | The routes come from a --user-routes file; user_routing is off (for PUT and POST); for customers, the shard could not create the database. |
500 | The store or a shard failed part-way; the message says what was already done. |
List the routes
curl -s -H "Authorization: Bearer $TOKEN" "$API/v1/routes"{"from_file":false,"routes":[{"database":"acme","role":"acme_app","shard":1},{"database":"globex","refuse":true,"role":"intern"}]}from_file is true when the routes come from --user-routes.
Set or remove a route
curl -s -X PUT -H "Authorization: Bearer $TOKEN" -d '{"shard": 1}' "$API/v1/routes/acme/acme_app"
curl -s -X PUT -H "Authorization: Bearer $TOKEN" -d '{"refuse": true}' "$API/v1/routes/globex/intern"
curl -s -X DELETE -H "Authorization: Bearer $TOKEN" "$API/v1/routes/globex/intern"{"database":"acme","role":"acme_app","route":{"shard":1}}
{"database":"globex","role":"intern","route":{"refuse":true}}
{"database":"globex","removed":true,"role":"intern"}A PUT only stores the route. The database and role must already exist on that shard, and the shard must host the role.
Provision a customer
curl -s -X POST -H "Authorization: Bearer $TOKEN" \
-d '{"database": "acme", "role": "acme_app", "password": "Acme-app-pass-2026", "shard": 1}' \
"$API/v1/customers"{"database":"acme","role":"acme_app","shard":1}The gateway runs these on the shard, over its own link to the shard, as the role --api-shard-role names (rasterized by default):
- in
postgres:CREATE DATABASE acme; - in
acme:CREATE ROLE acme_app LOGIN PASSWORD '…',GRANT ALL ON DATABASE acme TO acme_appandGRANT CREATE ON SCHEMA public TO acme_app; - in
postgres:SHARD HOST ROLE 'acme' 'acme_app';
and then stores the route acme acme_app 1.
What it needs:
- the routes kept by the API (no
--user-routes) anduser_routingset topreferorrequire; databaseandrolemade of letters, digits and_, starting with a letter, at most 63 bytes; apasswordof at least 12 characters; ashardin the map;- on the shard: the enterprise server with
pinned_roles_file,postgresas its operator database (operator_database postgresunder strict tenant mode), and the--api-shard-rolerole as a superuser there.
The steps are not one transaction. If one fails, the ones before it stay done, and the 500 message says which (for example the database acme was made on shard 1, its role was not: …). Finish by hand, or drop what was made and call again.
Client statistics as JSON
curl -s -H "Authorization: Bearer $TOKEN" "$API/v1/clients"The answer is {"clients": [...]}, one object per client with database, role, client, application, sessions_open, sessions, statements, errors, p50_us, p95_us, p99_us, max_us, client_bytes_in, client_bytes_out, shard_calls, shard_bytes_out, shard_bytes_out_wire, shard_bytes_in, shard_bytes_in_wire and last_seen. Latencies are in microseconds here. The token is the only permission check on this path.
Client statistics
The enterprise gateway measures every statement of every session, per client. A client is a database, a role, the client's IP address (without the port, which changes with every connection) and its application_name. The statistics are on by default; the flags set who may read them, how many clients are tracked and where the history is kept.
They name other clients, so only the roles in --client-stats-roles may read them:
rasterizeddb-gateway-enterprise --config /etc/rasterizeddb/gateway.conf \
--client-stats-roles ops,monitor \
--client-stats-dir /var/lib/rasterizeddb-gateway/statsThrough the gateway, as one of those roles, send these as simple queries (as psql does):
| Statement | Answers |
|---|---|
SHOW CLIENT STATS | Every client since the gateway started or the last reset, busiest first. |
SHOW CLIENT STATS HISTORY | The history's intervals of the last 60 minutes, newest first. |
SHOW CLIENT STATS HISTORY 1440 | The same for the last 1440 minutes. |
RESET CLIENT STATS | Forgets every client without an open session, and starts the counts of the others again. |
A role not on the list gets 42501. SHOW CLIENT STATS HISTORY needs a history: --client-stats-dir, or a config_dir, where it is kept automatically.
SHOW CLIENT STATS columns:
| Column | Meaning |
|---|---|
database, role, client, application | The client. |
sessions_open | Sessions open now. |
sessions, statements, errors | Counts since the start or the reset. A statement that failed counts as an error. |
avg_ms, p50_ms, p95_ms, p99_ms, max_ms | Statement latency in milliseconds, from the first byte the gateway read to the last byte of the reply. The percentiles are histogram bucket edges, accurate to about a quarter of their value. |
client_bytes_in, client_bytes_out | Bytes read from and written to the client. |
shard_calls | Requests sent to shards. |
shard_bytes_out, shard_bytes_in | Bytes sent to and received from shards, before compression. |
shard_bytes_out_wire, shard_bytes_in_wire | The same bytes as they went on the wire, after compression when shard_compression is on. |
last_seen | Unix time, in seconds, of the client's last statement or session event. |
SHOW CLIENT STATS HISTORY has a row per client per interval, with at (the interval's Unix time), the client's four columns, statements, errors, p50_ms, p95_ms, p99_ms and the six byte and call columns, each counting that interval only. An interval is written every --client-stats-flush-secs (60) and kept for --client-stats-retention-hours (168); clients with nothing in an interval get no row.
Past --client-stats-max-clients (4096) clients, new ones are counted together in one row whose database and role are * and whose client is (other).
Prometheus
With the gateway's admin = host:port set, GET /metrics there carries these lines for each client, labelled database, role, client and application:
| Metric | Meaning |
|---|---|
rdb_gateway_client_statements_total | Statements. |
rdb_gateway_client_errors_total | Failed statements. |
rdb_gateway_client_sessions_open | Sessions open now. |
rdb_gateway_client_latency_p50_seconds, rdb_gateway_client_latency_p99_seconds | Latency percentiles, in seconds. |
rdb_gateway_client_bytes_in_total, rdb_gateway_client_bytes_out_total | Bytes from and to the client. |
rdb_gateway_client_shard_bytes_plain_total | Bytes to and from shards, before compression. |
rdb_gateway_client_shard_bytes_wire_total | The same, on the wire. |
The metrics endpoint has no permission check of its own: keep its port private.
Limits
- Per-user routes apply to PostgreSQL-protocol clients of the gateway. The gateway's native front end does not use them.
- A customer's data does not follow a route. Pointing a route at another shard sends the role's next login there, where its database and role must already exist with the data; there is no built-in move of a whole customer database.
POST /v1/customersalways works in the shard'spostgresdatabase and cannot be pointed at another operator database.- Client statistics are answered only for simple-protocol queries, not for prepared statements.
- The statistics' history interval keeps the percentiles but not each interval's own maximum.