Skip to content

Workspace Databases & Connection Pools ​

How a workspace reaches its data, why managed databases have a five-connection ceiling, and the failure mode that took the API down on 16 Aug 2026.

Two kinds of workspace database ​

KindProvisioned byWhere it livesNamed
Bring your ownThe customer, via the database formWherever they run Postgres/MySQLn/a
Managed (sample data)DemoProvisioningServiceThe dedicated demo Postgres serversample_<16 hex>, one per workspace (earlier: sample_<organisation id>, demo_<orgSlug>)
Agent sandboxDemoProvisioningService.provisionSandboxDatabaseThe same server, one login per databasesbx_<16 hex>

Managed databases are created by CREATE DATABASE … TEMPLATE acme_store — a template clone, so provisioning is close to instant. Workspace.managed records which kind a workspace is.

One managed database per workspace ​

java
String dbName = "sample_" + HexFormat.of().formatHex(randomBytes(8));

Since 20 Nov 2026 every sample-data workspace gets a database of its own, created from the template, never reused. Before, it was one database per organisation, reused when it existed: an organisation's sample-data workspaces shared their tables and the five-connection budget, and because the name came from the organisation's slug (demo_<orgSlug>), and deleting an organisation frees its slug, a new organisation that took the slug was handed the old one's database, data and all. (For a few hours on 20 Nov the name was sample_<organisation id>; sample_301 is one.) Existing workspaces keep the database their config names.

Downloading a hosted database ​

GET /api/workspaces/{org}/{uuid}/database-dump (owners and admins, managed workspaces only; 409 otherwise) runs pg_dump --no-owner --no-privileges --format=plain with the workspace's own credentials into a temporary file (HostedDatabaseDump), then sends it and deletes it when the response ends. pg_dump comes from postgresql16-client in Dockerfile.metadata-rest-jvm; the native image has none and answers 503. It must be the server's major version or newer: the demo server runs 15.

When a managed database is dropped ​

Nothing dropped a sample-data database before 20 Nov 2026. Now the hourly hosted-database sweep (AgentSignupService.dropOrphanedHostedDatabases, run by AgentSandboxJob) lists every sbx_…, sample_… and demo_… database on the demo server and drops the ones no workspace_database_config row names, on the second run in a row that finds them so. A soft-deleted workspace still has its config row, so its database stays through the purge grace (seven days, thirty for a closed agent sandbox) and goes within two hours of the purge. The drop methods refuse any name outside their own pattern.

Deleting an organisation soft-deletes its workspaces and disables their MCP keys in the same transaction (OrganisationWorkspaceCloser, through the organisation module's OrganisationDeletionHandler SPI). Before 20 Nov 2026 it marked only the organisation row, so its databases were never purged.

The five-connection ceiling ​

DemoProvisioningService caps managed databases:

java
stmt.execute("ALTER DATABASE " + quoteIdent(dbName) + " CONNECTION LIMIT 5");

That protects the shared demo server from one organisation monopolising it. It also means pool sizing has to respect it. WorkspaceDatabaseService sizes pools accordingly:

java
DEFAULT_WORKSPACE_POOL_SIZE = 10   // bring-your-own — the customer owns the limits
MANAGED_WORKSPACE_POOL_SIZE = 2    // managed — must stay under CONNECTION LIMIT 5

Until 17 Aug every pool was 10, so a single managed workspace could never fill its own pool and a second workspace on the same database was guaranteed to be refused.

Pool lifecycle ​

Pools are cached per workspace UUID:

java
private final Map<UUID, Pool> workspacePools = new ConcurrentHashMap<>();

getWorkspacePool populates the cache with computeIfAbsent; closeWorkspacePool removes and closes. Deleting a workspace must call closeWorkspacePool, which deleteByOrganisationSlugAndUuid now does after the soft delete is persisted. It deliberately never fails the caller: the delete has already succeeded by then, and a pool that will not close should not turn it into an error.

The 16 Aug outage — worth reading before changing any of this ​

closeWorkspacePool existed but nothing called it. Pools for deleted workspaces stayed cached and kept their connections open. The sequence:

  1. Synthetic runs created and deleted workspaces in the synthetics org, each leaving a pool behind.
  2. Five idle pools — with no workspace behind them — exhausted demo_synthetics.
  3. Every query against it failed with 53300 too_many_connections, which fell through the exception mapper as "Unmapped PgException returned as 500", and reached users as "An unexpected error occurred".
  4. Vert.x then logged IllegalStateException: Result is already complete repeatedly — the pool's connection-acquisition timer racing a promise that had already completed. Its stack is entirely inside SqlConnectionPool; it is a symptom of starvation, not a separate fault.
  5. Hours later metadata-rest could not obtain a connection for its own health check (FailingStreak: 715) and the API was down for about two hours until restarted.

Three fixes followed: release pools on delete, size managed pools below the database limit, and map 53300 to a 503 with DATABASE_CONNECTION_LIMIT and retryable: true.

Diagnosing it again ​

bash
# Limits vs actual usage
docker exec docker-postgres-demo-1 psql -U <admin> -d postgres -c \
  "select datname, datconnlimit,
          (select count(*) from pg_stat_activity a where a.datname=d.datname)
   from pg_database d where datname ~ '^(sample|demo|sbx)_';"

# Who holds them, and for how long — idle pools with no workspace are the tell
docker exec docker-postgres-demo-1 psql -U <admin> -d postgres -c \
  "select usename, application_name, state, count(*), max(now()-state_change)
   from pg_stat_activity where datname='<database>' group by 1,2,3;"

application_name = vertx-pg-client in idle state for many minutes means leaked pools. To recover immediately:

sql
select pg_terminate_backend(pid) from pg_stat_activity
where datname='<database>' and state='idle';

What healthy looks like ​

A synthetic run should spike to at most MANAGED_WORKSPACE_POOL_SIZE and return to zero once the workspace is deleted. Connections that persist after a run, climbing hourly, mean the release path has regressed.

Caveat on the status page ​

SystemStatusService.checkPostgres() probes the metadata database, not workspace databases. No status check exercises a managed database, so this entire failure mode is invisible there — it was the synthetics that failed at 03:12 and 04:01, not the status page.

SchemaStack Internal Developer Documentation