NEW RasterizedDB 1.1.0 is out: sharding and the gateway, the message broker, roles and row-level security, compression, .NET on NuGet, and the enterprise edition. What's new ›
DOCS / ENTERPRISE / PER-USER SHARDS

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

  1. A client connects to the gateway and names its database and role.
  2. 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.
  3. 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_key and a native route= option are refused with 42501;
    • a statement is never fanned out to other shards.
  4. The shard opens the session only if it hosts that role: the role is listed in its enterprise configuration (pinned_role) or was added with SHARD HOST ROLE. Otherwise it refuses with role "<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:

text
# enterprise.conf on every shard
operator_database postgres
pinned_roles_file /var/lib/rasterizeddb/rdb_pinned_roles

Add 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:

text
# gateway.conf (only the lines this page needs)
user_routing = prefer
user_routingA routed loginA login routed to refuseA login with no route
off (default)routes are ignoredroutes are ignoredrouted by statement
preferlocked to its shardrefusedrouted by statement
requirelocked to its shardrefusedrefused: 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:

sh
rasterizeddb-gateway-enterprise --config /etc/rasterizeddb/gateway.conf --user-routes /etc/rasterizeddb/user-routes

The file has one database role shard line per login, or database role refuse:

text
# database  role        shard
acme        acme_app    1
globex      globex_app  2
globex      intern      refuse

The 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:

sql
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:

text
pinned_role acme acme_app

These 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:

sql
CREATE DATABASE acme;

Then, connected to acme on shard 1 as the same superuser:

sql
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:

sql
SHARD HOST ROLE 'acme' 'acme_app';

Add the route on the gateway (to the routes file, or with the API below):

text
acme acme_app 1

The customer connects to the gateway (6432 is the gateway's default port):

sh
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).

text
# gateway.conf
user_routing = require
api_listen = 10.0.0.5:7000
api_token_file = /etc/rasterizeddb/api.token
config_dir = /var/lib/rasterizeddb-gateway

Every 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.

sh
TOKEN="$(head -n 1 /etc/rasterizeddb/api.token)"
API=http://10.0.0.5:7000

Paths

MethodPathBodySuccess
GET/v1/routesnone200, 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}none200, the route removed
POST/v1/customers{"database", "role", "password", "shard"}201, the database, its role, the hosting and the route made
GET/v1/clientsnone200, the client statistics

Errors answer {"error": "…"} with the status:

StatusWhen
400A 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.
401No token, or the wrong one.
404DELETE of a route that does not exist; /v1/clients with the statistics off; an unknown path.
409The routes come from a --user-routes file; user_routing is off (for PUT and POST); for customers, the shard could not create the database.
500The store or a shard failed part-way; the message says what was already done.

List the routes

sh
curl -s -H "Authorization: Bearer $TOKEN" "$API/v1/routes"
text
{"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

sh
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"
text
{"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

sh
curl -s -X POST -H "Authorization: Bearer $TOKEN" \
  -d '{"database": "acme", "role": "acme_app", "password": "Acme-app-pass-2026", "shard": 1}' \
  "$API/v1/customers"
text
{"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):

  1. in postgres: CREATE DATABASE acme;
  2. in acme: CREATE ROLE acme_app LOGIN PASSWORD '…', GRANT ALL ON DATABASE acme TO acme_app and GRANT CREATE ON SCHEMA public TO acme_app;
  3. 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) and user_routing set to prefer or require;
  • database and role made of letters, digits and _, starting with a letter, at most 63 bytes; a password of at least 12 characters; a shard in the map;
  • on the shard: the enterprise server with pinned_roles_file, postgres as its operator database (operator_database postgres under strict tenant mode), and the --api-shard-role role 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

sh
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:

sh
rasterizeddb-gateway-enterprise --config /etc/rasterizeddb/gateway.conf \
  --client-stats-roles ops,monitor \
  --client-stats-dir /var/lib/rasterizeddb-gateway/stats

Through the gateway, as one of those roles, send these as simple queries (as psql does):

StatementAnswers
SHOW CLIENT STATSEvery client since the gateway started or the last reset, busiest first.
SHOW CLIENT STATS HISTORYThe history's intervals of the last 60 minutes, newest first.
SHOW CLIENT STATS HISTORY 1440The same for the last 1440 minutes.
RESET CLIENT STATSForgets 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:

ColumnMeaning
database, role, client, applicationThe client.
sessions_openSessions open now.
sessions, statements, errorsCounts since the start or the reset. A statement that failed counts as an error.
avg_ms, p50_ms, p95_ms, p99_ms, max_msStatement 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_outBytes read from and written to the client.
shard_callsRequests sent to shards.
shard_bytes_out, shard_bytes_inBytes sent to and received from shards, before compression.
shard_bytes_out_wire, shard_bytes_in_wireThe same bytes as they went on the wire, after compression when shard_compression is on.
last_seenUnix 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:

MetricMeaning
rdb_gateway_client_statements_totalStatements.
rdb_gateway_client_errors_totalFailed statements.
rdb_gateway_client_sessions_openSessions open now.
rdb_gateway_client_latency_p50_seconds, rdb_gateway_client_latency_p99_secondsLatency percentiles, in seconds.
rdb_gateway_client_bytes_in_total, rdb_gateway_client_bytes_out_totalBytes from and to the client.
rdb_gateway_client_shard_bytes_plain_totalBytes to and from shards, before compression.
rdb_gateway_client_shard_bytes_wire_totalThe 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/customers always works in the shard's postgres database 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.
✓ page rendered in 161 µs axum + tera + htmx · no other javascript