Skip to content

fix(clickhouse): cap the system log tables - #13872

Merged
levivannoort merged 7 commits into
mainfrom
fix/clickhouse-system-log-ttl
Oct 1, 2026
Merged

levivannoort merged 7 commits into
mainfrom
fix/clickhouse-system-log-ttl

Conversation

@levivannoort

@levivannoort levivannoort commented Sep 24, 2026 •

Copy link
Copy Markdown
Member

What does this PR do?

Caps ClickHouse's own system.* log tables, which grow without bound today.

A self-hosted 2.x report: a 10GB ClickHouse volume filled in two weeks on an install whose Appwrite data was ~1.5GB. The cause is not our schema — utopia-php/usage already applies a retention TTL to the usage tables (_APP_MAINTENANCE_RETENTION_USAGE_TTL, 180 days by default, wired in app/init/resources.php). It is ClickHouse's own system logs, which nothing on our side touches.

Two defaults combine badly:

Nothing has retention. Every <ttl> element in the image's shipped config.xml sits inside an XML comment, including the 30-day one that reads like a default for query_log:

$ docker run --rm --entrypoint sh clickhouse/clickhouse-server:26.4.3-alpine \
    -c 'sed -n 1179,1187p /etc/clickhouse-server/config.xml'
        <!--
            Table TTL specification: ...
        <ttl>event_date + INTERVAL 30 DAY DELETE</ttl>
        -->

The metric collectors write regardless of traffic. metric_log, histogram_metric_log and query_metric_log each sample every second (collect_interval_milliseconds = 1000), and metric_log carries a column per profile event, so its rows are wide. Measured on a stock container serving nothing at all, over 490s:

table size projected
metric_log 1.43 MiB 252 MiB/day
trace_log 801 KiB 138 MiB/day
text_log 465 KiB 80 MiB/day
asynchronous_metric_log 112 KiB 19 MiB/day
part_log, histogram_metric_log, query_log, processors_profile_log 22 MiB/day

≈510 MiB/day on an idle instance, forever. (Projections come from early-boot unmerged parts, so they read high, but the shape is what the report describes.)

This adds clickhouse-config.xml, bind-mounted into /etc/clickhouse-server/config.d/appwrite.xml, which:

  • gives every unbounded log a window — 3 days for the chatty text_log/trace_log/asynchronous_insert_log, 7 days for the metric and query logs plus part_log/processors_profile_log/background_schedule_pool_log, 14 days for error_log so a late report is still covered. crash_log is deliberately left alone: tiny, and worth keeping.
  • samples the three per-second collectors every 10 seconds instead, which still reads an incident fine and is the difference between a bounded-but-large metric_log and a small one.

Measured with the override, same image, same idle workload, 510s: metric_log 53 rows instead of 492 (10x fewer, one per 10s as configured) and 80 MiB/day instead of 252; histogram_metric_log 1.2 instead of 5.8. Idle total drops to ≈330 MiB/day, and with the windows above the steady-state ceiling is roughly 1.3GB rather than unbounded.

trace_log is the biggest remaining writer at ~140 MiB/day idle, capped at 3 days here. Trimming it further means turning down the query profiler in a users.d profile, which costs real debuggability — deliberately out of scope.

Existing installs need no manual cleanup: ClickHouse applies the configured <ttl> when it creates a log table, and on restart issues the ALTER TABLE … MODIFY TTL itself when the configured window differs from the table's. Old rows past the window are then dropped by TTL merges, so no TRUNCATE step is required.

The file ships like the existing Mongo sidecars — COPY'd into both /usr/src/code and /usr/local/share/appwrite (the two roots Install::buildFromProjectPath() searches), copied next to the generated compose file at install time, and listed in Generator::HOST_PATH_REWRITABLE_BINDS so the relative bind becomes an absolute host path in generated compose files. copyMongoFilesIfNeeded() became copyConfigFiles(array $files) to carry both sets.

One caveat for reviewers: an upgrade run against an existing compose file (--use-existing-config) keeps that file, so it will not pick up the mount. Those installs need the bind added by hand, or the ALTER TABLE system.<log> MODIFY TTL … statements run once.

Test Plan

1. ClickHouse applies the windows to tables that already exist without one — the upgrade path. Start the pinned image on a volume with no override, confirm no TTL, then restart the same volume with the override mounted:

$ docker run -d --name ch -v ch:/var/lib/clickhouse clickhouse/clickhouse-server:26.4.3-alpine
$ docker exec ch clickhouse-client -q "SELECT 1; SYSTEM FLUSH LOGS"
metric_log: (no TTL)   query_log: (no TTL)   trace_log: (no TTL)
text_log:   (no TTL)   error_log: (no TTL)   part_log:  (no TTL)

# restart, same volume, now with the override mounted
metric_log:                   TTL event_date + toIntervalDay(7)
query_log:                    TTL event_date + toIntervalDay(7)
trace_log:                    TTL event_date + toIntervalDay(3)
text_log:                     TTL event_date + toIntervalDay(3)
error_log:                    TTL event_date + toIntervalDay(14)
part_log:                     TTL event_date + toIntervalDay(7)
asynchronous_metric_log:      TTL event_date + toIntervalDay(7)
histogram_metric_log:         TTL event_date + toIntervalDay(7)
query_metric_log:             TTL event_date + toIntervalDay(7)
query_views_log:              TTL event_date + toIntervalDay(7)
query_thread_log:             TTL event_date + toIntervalDay(7)
processors_profile_log:       TTL event_date + toIntervalDay(7)
background_schedule_pool_log: TTL event_date + toIntervalDay(7)
asynchronous_insert_log:      TTL event_date + toIntervalDay(3)

No <Error> lines in the server log on either boot.

2. Both settings merge onto the base <metric_log> block rather than replacing it — TTL and interval together, table still where it belongs:

metric_log: TTL event_date + toIntervalDay(7)
SELECT count(), min(event_time), max(event_time) FROM system.metric_log
55   2026-09-24 06:36:27   2026-09-24 06:45:17    -- 55 rows over 530s

3. Growth, with and without — the two measurements above, same image, same idle container, comparable uptime (490s stock vs 510s capped).

4. Fresh start with the compose service's environment (CLICKHOUSE_DB/USER/PASSWORD/DEFAULT_ACCESS_MANAGEMENT) — windows present on first creation, server log clean.

5. Compose generation — docker compose -f docker-compose.yml config --quiet passes, and tests/unit/Docker/Compose/GeneratorTest.php is green (11 tests, 108 assertions). No new assertions: testLeavesNoRelativeBindMountOnPublishedVersions already covers the change, and it fails naming the mount when the entry is dropped from HOST_PATH_REWRITABLE_BINDS:

1) GeneratorTest::testLeavesNoRelativeBindMountOnPublishedVersions
clickhouse mounts ./clickhouse-config.xml:/etc/clickhouse-server/config.d/appwrite.xml:ro,
which resolves against wherever the generated file is run rather than the installation

6. query_metric_log honours the interval — query_metric_log_interval defaults to -1, meaning "use collect_interval_milliseconds from the query_metric_log config". Same 25s query on both: stock samples 26 rows over 24s, with the override 3 rows over 15s.

7. Static analysis — Pint and PHPStan clean on the changed PHP files.

Related PRs and Issues

  • Reported by a self-hosted user on 2.x: 10GB ClickHouse volume filled in two weeks against ~1.5GB of Appwrite data.

Checklist

  • Have you read the Contributing Guidelines on issues?
  • If the PR includes a change to an API's metadata (desc, label, params, etc.), does it also include updated API specs and example docs?

ClickHouse's own system logs ship without a TTL -- every `<ttl>` in the
image's config.xml sits inside a comment -- and metric_log,
histogram_metric_log, query_metric_log and error_log each collect a row
per second regardless of traffic. On an install with ~1.5GB of usage data
they filled a 10GB volume in two weeks.

Mount a config.d override that gives each of them a retention window: 3
days for the chatty text/trace logs, 7 for the metric and query logs, 14
for error_log. ClickHouse applies the TTL when it creates a log table
and issues the ALTER on restart when the configured window differs, so
existing installs shrink on upgrade without a manual TRUNCATE.
@greptile-apps

greptile-apps Bot commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

The PR appears safe to merge; no outstanding finding or new actionable issue remains.

Summary

The PR adds retention and reduced sampling for ClickHouse system logs, packages the override in the image, and wires its bind mount through Compose generation and installation. The change since the previous review removes a machine-specific vendor symlink.

Reviews (4) · Last reviewed commit: "chore: drop the stray vendor symlink"

Comment thread src/Appwrite/Platform/Tasks/Install.php Resolved
@github-actions

github-actions Bot commented Sep 24, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → fix/clickhouse-system-log-ttl (after).

Metric Before After Change
🚀 Requests/sec 200.06 206.59 ⚪ +3.3%
⏱️ Latency P50 87.21 ms 83.05 ms ⚪ -4.8%
⏱️ Latency P95 200.06 ms 196.97 ms ⚪ -1.5%
Per-scenario breakdown & investigation details

Metrics below reflect the current branch (after). Δ P95 compares against the base.

Scenario P50 (ms) P95 (ms) Requests RPS Δ P95 (ms)
API total 83.05 196.97 12,939 206.59 -3.1
Account 161.94 300.64 681 11.31 -9.02
TablesDB 79.73 149.35 7,037 114.6 -13.06
Storage 76.56 163.28 3,405 57.28 +1.5
Functions 123.62 252.02 1,816 31.25 +15.01

Top API waits (after)

API request Max wait (ms)
account.prefs.update 444
account.name.update 414.4
functions.create 388.01
functions.delete 374.73
tablesdb.rows.delete 373.57

An idle container writes ~500MiB of system logs a day, and metric_log is
half of that on its own: it samples every second and carries a column
per profile event. Retention alone leaves 7 days of that on disk, so
sample it, histogram_metric_log and query_metric_log every 10 seconds
instead -- fine grained enough to read an incident from, and it takes
metric_log from 252 to 80 MiB/day on an otherwise idle instance.

Also ship the override the way the Mongo sidecars are shipped. The
installer reads config files from /usr/src/code or
/usr/local/share/appwrite, so without both COPY lines the copy next to
the generated compose file silently skips it and Docker creates the bind
target as a directory. crash_log keeps every row: it is tiny and worth
having.
Comment thread clickhouse-config.xml Resolved
testLeavesNoRelativeBindMountOnPublishedVersions already fails, with the
mount named in the message, when a bind is missing from
HOST_PATH_REWRITABLE_BINDS -- checked by removing the entry -- so the two
assertions repeating the literal mount only restated docker-compose.yml.
Comment thread vendor Resolved
Committed by accident while running the unit test against the vendor
directory of another checkout. .gitignore lists /vendor/, which does not
match a symlink of that name.

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your trial has ended. Reactivate Greptile to resume code reviews.

@ghost

ghost commented Sep 30, 2026 •

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

Adds retention windows to ClickHouse system logs and reduces sampling frequency for three metric collectors. Bundles and mounts the override through Docker and generated Compose installations. The installer now copies the configuration alongside Compose while skipping copies when source and destination resolve to the same file.

Latest changes: The newest commit skips config-file copying when source and destination resolve to the same file, fixing local installs that write into the project root.

Verdict New comments Fixed Still open
✅ Approved 0 0 0
📂 Walkthrough · 5
File Change
Dockerfile Bundles the ClickHouse override in both installer template roots.
clickhouse-config.xml Sets system-log retention windows and ten-second metric collection intervals.
docker-compose.yml Mounts the override read-only into ClickHouse's configuration directory.
src/Appwrite/Docker/Compose/Generator.php Allows the new bind mount to be rewritten to an absolute host path.
src/Appwrite/Platform/Tasks/Install.php Copies ClickHouse and database sidecar configs, skipping copies onto the same file.

Reviewed the commits since 11656da · Details · Comment @hansi-codes review to re-run, or mention @hansi-codes with a question.

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Tier B · 1 blocking finding to address. Summary

Comment thread src/Appwrite/Platform/Tasks/Install.php Resolved

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Tier S · Looks good to merge. Summary

@levivannoort
levivannoort merged commit 27307e2 into main Oct 1, 2026
55 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants