# ClickHouse

> Connect ClickHouse so failed or degraded services, ClickHouse notifications and failed queries open incidents, and Ace can start, stop or resize a ClickHouse Cloud service after approval.

Source: https://tryace.dev/docs/integrations/clickhouse

Ace watches ClickHouse Cloud services through the Cloud API, and reads a Cloud service or a self-managed server through a read-only SQL user. It never queries a service that isn't running, so it never wakes an idle one.

| | |
| --- | --- |
| Category | Database |
| Ace reads | On ClickHouse Cloud: service state and scaling, activities, ClickHouse notifications, Prometheus metrics. With a SQL user, required on a self-managed server: failed and slow queries, mutations, parts, replication and running queries |
| Ace can change | On ClickHouse Cloud only: service start and stop, replica count, memory per replica, idle scaling. A person always approves. |
| Opens incidents | Yes, for workloads that route ClickHouse |
| Setup | A ClickHouse Cloud API key and an optional read-only SQL user, or a SQL user on a self-managed server |

## What it reads
Ace reads one ClickHouse Cloud organization, the API key's, or one self-managed server. It reads them while it investigates a workload that lists ClickHouse in its **Observability** settings or routes its alerts.

With the API key, on ClickHouse Cloud:

- Every service the key can see, as it is now: state, tier, cloud and region, replicas, memory per replica, idle scaling, warehouse, version and release channel.
- Activities in the incident window, such as starts, stops, scaling, upgrades, maintenance and failovers, with who made each one.
- ClickHouse's own notifications, such as too many parts, failed mutations, failed backups and ClickPipe failures.
- A service's Prometheus metrics per replica: queries and failed queries, inserts, merges, parts, memory, CPU, connections and ClickPipes status.

With a SQL user, Ace reads the server's system tables, across every replica of a Cloud service:

- Failed queries in the incident window, by error code and query shape.
- Slow query shapes, with p95 time, rows read and peak memory.
- Unfinished or failing mutations, tables with the most active parts, and replicas that are read-only or behind.
- The queries running now.
- One read-only statement of its own per request, as the next section describes.

Ace reads query text as normalized shapes, and removes literals and secrets from every text it returns. The SQL user can read only system tables, so Ace never sees table data.

For the Blueprint, Ace reads the organization, its services and the hosts they serve, and the ClickPipes of the first 20 services. On those services that it may query in the background, it also lists up to 200 databases and the 200 largest tables. A self-managed server is one service with its schema.

### Idle services stay idle

A query wakes an idle ClickHouse Cloud service, and you pay for the compute. So Ace sends SQL only to a service that is running, degraded or partially running, and never to one that is idle or stopped. The service list and Prometheus metrics never wake a service.

Background reads go further. Polls, the Blueprint, cost scans and recovery checks query only a running service with idle scaling off. A query every few minutes would keep an idling service up.

A service with idle scaling on is queried only during an investigation, and only while it is already running.

### Limits on SQL

Every statement runs with `readonly=2`, so it can't write or change a table. Each one stops after 20 seconds and returns at most 1,000 rows and 4 MiB. A longer result is cut and marked as cut.

The statement Ace writes itself must be a single `SELECT`, `WITH`, `SHOW`, `DESCRIBE`, `EXPLAIN` or `EXISTS`. Ace refuses a `FORMAT` or `SETTINGS` clause and `INTO OUTFILE`. Values such as the incident window go as typed query parameters, never as text in the statement.

The SQL user's grants are the real boundary, so grant it only what [Credentials and permissions](#credentials-and-permissions) lists.

### Cost findings

Every day, Ace checks the last 7 days of each running ClickHouse Cloud service that never idles and costs at least $20 of compute a month. It reports two kinds of waste, with the calculation, under [**Cost opportunities**](https://app.tryace.dev/settings/integrations#cost-opportunities).

- **Idle scaling is off on a mostly quiet ClickHouse service**: the service ran queries in at most half of the hours. Turn on idle scaling.
- **ClickHouse service replicas have far more memory than they use**: peak memory stayed below 35% of each replica's memory. Lower the minimum memory per replica.

The scan needs `control-plane:organization:view` on the key and a SQL user. Ace prices ClickHouse Credits at the pay-as-you-go rate of one US dollar each.

## What it can change
Every ClickHouse action needs a person's approval, whatever your policy says. Ace proposes them only after an admin turns on **Actions**. See [Actions and approvals](https://tryace.dev/docs/concepts/actions-and-approvals).

| Action | What it does | Needs | Undo |
| --- | --- | --- | --- |
| Service start | Starts a stopped service. It serves queries again once it is running. | `control-plane:service:manage` | Yes. Stops it again. |
| Service stop | Stops a running or idle service. Every query and insert to it fails until it starts again. Its data is kept, and its compute stops being billed. | `control-plane:service:manage` | Yes. Starts it again. |
| Service resize | Changes a service's replica count (1 to 50) or its minimum and maximum memory per replica (8 to 356 GiB, in steps of 4 GiB). The service must be running, idle or stopped. | `control-plane:service:manage-scaling-config` | Yes. Restores the previous size. |
| Idle scaling | Turns idle scaling on or off, or changes the idle timeout (5 to 1,440 minutes). | `control-plane:service:manage-scaling-config` | Yes. Restores the previous settings. |

Safeguards:

- Ace reads the service again before it writes. It refuses if the state, size or idle settings changed since approval.
- A resize replaces replicas one at a time, so queries on a replica being replaced can fail. Ace doesn't resize a development service or one that scales horizontally. On a fixed-size service, the minimum and maximum memory stay equal.
- A fixed-size service can't idle, so Ace never turns idle scaling on for one.
- Undo of a stop Ace made still runs after **Stop all actions**, because it brings back what the stop took down.
- Ace treats a development-tier service as the development environment, and every other service as production. So Ace proposes a change to a development service only for an incident in development.

Ace sends no other write. It never resets a password, deletes a service, changes settings, or kills a query or mutation. A self-managed connection has no actions.

Ace checks `control-plane:service:manage` by listing a service's JWT providers. ClickHouse may refuse that read outside its Enterprise plan, so Settings can show the start and stop actions as **Checked when it runs**. No read shows the scaling permission, so resize and idle scaling always show **Checked when it runs**.

## What opens an incident

Problems open incidents only on workloads that route this connection. ClickHouse has no webhook, so Ace polls every 2 minutes, whether or not a workload routes it. A poll never wakes a service.

| Condition | Where | Severity |
| --- | --- | --- |
| Service failed | ClickHouse Cloud | Critical |
| Service degraded | ClickHouse Cloud | High |
| Service partially running | ClickHouse Cloud | Medium |
| Service stopped by ClickHouse, for example because its encryption key is inaccessible or after a tier change | ClickHouse Cloud | High |
| ClickHouse notification about a service or ClickPipe, at critical or warning | ClickHouse Cloud | Critical, or medium for a warning |
| Failed queries | A self-managed server, or a running Cloud service with idle scaling off | Medium |

A self-managed server opens incidents only from failed queries.

A failed service or a stop by ClickHouse opens an incident at once. A degraded or partially running service opens one after two checks in a row. These incidents resolve when the service is running or idle again.

A service that a person or an API key stopped opens nothing, because the stop was on purpose.

Notifications and failed queries are events, not states. ClickHouse sends no all-clear, so Ace measures recovery on the service itself.

Failed queries are grouped by error code and query shape. Ace leaves out its own queries and cancelled ones. A mistake in the query itself, such as a syntax error, an unknown table or a refused grant, only adds context until it fails 50 times between checks.

Info notifications and organization notifications, such as billing, only add context.

On ClickHouse Cloud, service activities join the change ledger with who made each one: a person, an API key, ClickHouse or ClickHouse support. Ace records them even before a workload routes the connection. Idling and waking are left out.

## Before you start

- The **admin** role in Ace.
- For ClickHouse Cloud, permission to create roles and API keys in the organization.
- For a SQL user, a database user that can create users and grant privileges, such as `default`, on each service Ace should query.
- A self-managed server needs its HTTPS interface reachable from the internet.

For SQL, a Cloud service needs a public HTTPS endpoint. A service whose IP access list doesn't allow Ace refuses its SQL.

## Credentials and permissions

| Credential | Permissions | Why Ace needs it |
| --- | --- | --- |
| API key, ClickHouse Cloud only | `control-plane:service:view`, `control-plane:organization:view-activities` | Required. Services, their state and metrics, activities and notifications. |
| Same key | `control-plane:organization:view` | Optional. Cost scans. |
| Same key, only for actions | `control-plane:service:manage` | Start and stop a service. |
| Same key, only for actions | `control-plane:service:manage-scaling-config` | Resize a service and change idle scaling. |
| SQL user | `SELECT` on `system.*` and `SHOW` | Optional on ClickHouse Cloud, required on a self-managed server. Failed queries, mutations, parts, replication and the Blueprint's databases and tables. |
| Same SQL user, ClickHouse Cloud only | `REMOTE` | Ace reads every replica through `clusterAllReplicas`. |

The key's role can cover only the services Ace should watch. One SQL user and password serves every service, so create the same user on each one. Ace stores the key and the SQL password encrypted.

## Set up

### 1. Create the API key

Skip this step for a self-managed server.

1. In the [ClickHouse Cloud console](https://console.clickhouse.cloud), choose your organization name in the sidebar, then **Users and roles**. On the **Roles** tab, choose **Create new role**.
2. Name it Ace and allow the permissions in [Credentials and permissions](#credentials-and-permissions). Limit it to the services Ace should watch, or leave it on all services.
3. From the same menu, open **API Keys** and choose **New API Key**. Name it Ace and give it the Ace role.
4. Choose **Generate API Key**. Copy the **Key ID** and the **Key secret**. ClickHouse shows the secret only once.

### 2. Create the SQL user

This step is optional on ClickHouse Cloud. Without a SQL user, Ace reads only the Cloud API.

1. On ClickHouse Cloud, open the [SQL console](https://clickhouse.com/docs/cloud/get-started/sql-console) of each service Ace should query. On a self-managed server, connect as a user that can grant privileges.
2. [Create a user](https://clickhouse.com/docs/sql-reference/statements/create/user) named `ace`. ClickHouse Cloud requires a password of at least 12 characters.

   ```sql
   CREATE USER ace IDENTIFIED BY '<password>';
   GRANT SELECT ON system.* TO ace;
   GRANT SHOW ON *.* TO ace;
   GRANT REMOTE ON *.* TO ace;
   ```

   `REMOTE` is needed only on ClickHouse Cloud. Use the same password on every service.

### 3. Connect ClickHouse

1. Open [**Settings** > **Integrations** > **ClickHouse**](https://app.tryace.dev/settings/integrations/clickhouse).
2. Fill in the form:

   | Field | What to enter |
   | --- | --- |
   | **Deployment** | **ClickHouse Cloud** or **Self-managed ClickHouse**. |
   | **Key ID** and **Key secret** | ClickHouse Cloud only. The key from step 1. |
   | **Server URL** | Self-managed only. The server's HTTPS interface, such as `clickhouse.example.com:8443`. Ace connects only over HTTPS. |
   | **SQL user** and **SQL password** | The user from step 2, or leave both empty on ClickHouse Cloud. |

3. Select **Connect ClickHouse**. Ace checks the key and the SQL user before it saves.

Ace finds the organization from the key. A self-managed URL must use HTTPS and be reachable from the internet. Ace doesn't connect to private network addresses.

### 4. Route alerts to a workload

In the workload's settings, open **Alerts** (`https://app.tryace.dev/workloads/<workload-id>/settings/alerts`), tick ClickHouse under **Sources** and turn on **Investigate alerts**.

### 5. Optional: allow actions

In the [**Actions** section](https://app.tryace.dev/settings/integrations/clickhouse#actions), turn on the switch. Actions need ClickHouse Cloud.

## Set up with an agent

Copy this prompt into a coding agent, or open it in one with the logos below it. The agent does the ClickHouse side in your terminal and asks you for each decision and each step in a browser. See [how agent setup works](https://tryace.dev/docs/integrations#set-up-with-an-agent).

Give your coding agent this prompt:

```text
Help me connect ClickHouse to Ace. Read https://tryace.dev/docs/integrations/clickhouse/agent.md and follow it step by step. Ask me whenever you need a decision or something only I can do.
```

## Confirm it's working

The connection card shows **Synced** with a time. **Sync now** reruns the checks: the key, its services and activities on ClickHouse Cloud, and the SQL user. A failed check shows under the card:

- "This API key belongs to no organization Ace can read. Check the key ID and secret."
- "The key sees no services. Give its role control-plane:service:view on the services Ace should watch."
- "The provider rejected the credentials. Check the key or token is current and copied in full." Check the key, or the SQL user's password.
- "The key is missing SELECT on system.* and REMOTE." Grant the SQL user the privileges in step 2.

Ace checks the SQL user only on a running service. If no service is running, Ace skips the SQL check and keeps the SQL reads. Select **Sync now** while a service is running to check the SQL user.

If a poll keeps failing for 10 minutes, the page shows **Needs attention** and the card says why.

## Disconnect

1. In Ace, open [**Settings** > **Integrations** > **ClickHouse**](https://app.tryace.dev/settings/integrations/clickhouse), select **Disconnect**, then **Disconnect ClickHouse**. To stop only actions, turn **Actions** off instead.
2. In the [ClickHouse Cloud console](https://console.clickhouse.cloud), delete the Ace key under **API Keys** and the Ace role under **Users and roles**.
3. If you created a SQL user, run `DROP USER ace` on each service or on your server.

## Next steps

- [Neon](https://tryace.dev/docs/integrations/neon)
- [PlanetScale](https://tryace.dev/docs/integrations/planetscale)
- [Actions and approvals](https://tryace.dev/docs/concepts/actions-and-approvals)
- [All integrations](https://tryace.dev/docs/integrations)
