PostgreSQL stores

The home store and other stores, connecting to them, founding a deployment, adding stores, moving partitions, poolers, and PostgreSQL's failover and recovery.

A store is a PostgreSQL database that holds a deployment's data: every document, its history and the outcomes of its transactions, as rows of the schema mimic. A deployment has one store or several. The first is the home, which also holds the deployment's records: users and keys, settings, the map of which store holds each partition, the registry of processes and operations, backups' records, ACME's records and the audit trail. The home is small beside the data; keep it on the store with the lowest latency to the processes.

Connecting

A process takes the home's URL from MIMIC_POSTGRES_URL (or --postgres-url, or postgres-url in its configuration file) and each other store as NAME=URL from MIMIC_STORE (or --store, once for each).

export MIMIC_POSTGRES_URL='postgres://mimicd:PASSWORD@db.example:5432/mimic?sslmode=verify-full'
  • A URL holding a password is refused as a flag, by serve and by every operator command, since other users of the host can read the process list. Give it in the variable, or in the configuration file (readable by the server's user alone), or give the password alone in PGPASSWORD or a PGPASSFILE.
  • For a store on another host, use sslmode=verify-full, which checks the server's certificate against the system's roots or the file sslrootcert= names.
  • Every process should connect as one role: see Requirements.

Founding a deployment

The first mimicd serve on a store founds the deployment there: it applies the schema, records the store as the deployment's home with the deployment's name (--deployment-name, mimic by default), and creates the bootstrap superuser if absent. A later process started with another deployment name refuses the store (exit code 5).

mimicd serve --init-only does the same and exits 0, for a deploy step or an init container. Run again, it changes nothing but creating a --bootstrap-admin that does not exist yet. Operator commands work only on a founded home.

Adding a store

A deployment of several stores grows in this order:

Found the home

Start mimicd serve on the home once without --store, or run mimicd serve --init-only.

Add each other store

MIMIC_STORE='eu2=postgres://mimicd@db2.example:5432/mimic?sslmode=verify-full' \
  PGPASSWORD=… mimicd admin stores add eu2

The command opens the store, checks its settings as a start does, applies the schema and records it on the home as active. Sent again for the same store it is done; for another database under a name in use it is refused. A store's name is not home and not digits alone.

Restart the processes with it

Give every process every other store (--store, or MIMIC_STORE). A process given a store the deployment has not added refuses to start (exit code 5). A process not given a store the home lists starts with a warning, and that store's partitions answer unavailable on it.

mimicd admin stores list (and GET /v1/admin/stores, with stores.read) lists the stores, their state and their partitions.

Where databases go

A new database is placed on the active store holding the fewest partitions, or on the store its creation names:

{ "budget_group": "default", "partitions": 4, "store": "eu2" }

A database imported from an archive is made on the home. mimicd admin database placement DB (and GET /v1/admin/databases/{db}/placement) shows each partition's store.

Moving partitions

Partitions move between stores while the deployment serves, by store operations, each under a key you choose. The same key with the same arguments is the same operation, so a command whose answer was lost is sent again as it was:

mimicd admin partition move --database DB --partition N --to STORE --key KEY
mimicd admin database move --database DB --to STORE --key KEY
mimicd admin store drain NAME --key KEY
mimicd admin moves show --key KEY
mimicd admin moves cancel --key KEY

Each takes the home's URL and --store NAME=URL for every other store. A command drives its operation until it ends, or leaves it to a server process with --detach, and exits 0 once done and 1 if it was cancelled, failed or needs a reconcile. The API starts the same operations (PUT /v1/admin/operations/{key}, POST /v1/admin/stores/{name}/drain, with stores.write).

  • A partition's move fences the partition at its source, copies it to the target, compares every row and every document's state hash, switches the map to the target, and lifts the fence there. While it is fenced the partition answers database_fenced, which a process waits out within a request, and clients retry. The source's rows are deleted 10 minutes after the switch.
  • A database's move moves each of its partitions, one at a time.
  • A store's drain moves each of its partitions to the active store holding the fewest, and ends with the store drained. Nothing deletes its PostgreSQL database.
  • Cancelling works only before the switch: the target's copy is dropped and the source's fence lifted. A switched move completes once the target answers.

Behind a pooler

A process keeps no state in a PostgreSQL session, so it is designed to reach a store through PgBouncer in transaction mode, with default_query_exec_mode=exec added to the URL (the driver's statement cache does not survive a pooler). This has not been tested or measured yet.

postgres://mimicd:PASSWORD@pgbouncer:6432/mimic?default_query_exec_mode=exec

Behind a pooler, PostgreSQL's keepalives watch the pooler, not the process: a pooler that keeps answering for a process that is gone keeps its session's locks until a takeover ends it. Give the pooler's own client connections the same keepalives (PgBouncer: tcp_keepalive = 1, tcp_keepidle = 10, tcp_keepintvl = 5, tcp_keepcnt = 3, and on Linux tcp_user_timeout = 30000).

Failover and recovery

PostgreSQL owns a store's durability, so its failover and recovery are PostgreSQL's tools: a standby promoted, or a base backup and its WAL replayed. Per-database backups are the server's (see Backups and restores).

  • A primary fails over. Processes reconnect. A commit whose answer was lost is never retried blind: the outcome it recorded settles it. With a synchronous standby no acknowledged write is lost.
  • A store is out of reach. Its partitions answer unavailable; the others are served, and /status names it (store_unreachable). Without its home a process is not ready.
  • A store is taken back to an earlier point (an asynchronous standby promoted, a point-in-time recovery). What was committed after that point is gone, acknowledged or not, and a retry of a transaction lost with it is evaluated as new. The processes notice and stay safe: a move the stores and the home disagree about parks until you reconcile it (mimicd admin stores reconcile --key KEY, alert mimic_store_map_behind). Then start a new full backup of each database on that store (mimicd admin backup start): the open chain holds what the store lost.
  • A store is lost for good. Its databases are restored from their backups into new databases on another store, each losing what was written after its last backup.

mimicd admin stores recount [--database DB [--partition N]] checks each partition's count of the bytes it keeps against its rows and corrects it (audited). It takes no lock, so it is safe while the deployment serves.