From 4f0a367a2332122bdc39b905645550fe907ca1df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carl-Gerhard=20Lindesva=CC=88rd?= Date: Mon, 26 Oct 2026 09:47:01 +0100 Subject: [PATCH 1/2] fix(db): keep inserts synchronous and let migration 14 run on ClickHouse 26.7+ Two things that have to land before ClickHouse is upgraded past 26.1: Migration 14 creates profile_event_property_summary_mv with property_value outside the sorting key. AggregatingMergeTree rejects that since 26.7, so a fresh install, CI run or new worktree stopped at migration 14 on a newer server. Existing installs are unaffected because migration 24 already drops the view. The CREATE now sets allow_dimensions_outside_sorting_key = 1, and the full chain was run in cluster mode against 26.8.34.13 with default server settings. ClickHouse 26.3 changed the async_insert default to 1. OpenPanel batches in its own buffers and inserts synchronously, so the shared client now pins async_insert = 0; a buffer that opts in still sends async_insert = 1 per query. Self-hosters are unaffected until the compose template moves past 26.2; after that this keeps insert behaviour unchanged. --- packages/db/src/clickhouse/client.ts | 3 +++ .../code-migrations/14-profile-event-property-summary-mv.ts | 3 +++ 2 files changed, 6 insertions(+) diff --git a/packages/db/src/clickhouse/client.ts b/packages/db/src/clickhouse/client.ts index 9b7a0df43..3856599c6 100644 --- a/packages/db/src/clickhouse/client.ts +++ b/packages/db/src/clickhouse/client.ts @@ -100,6 +100,9 @@ function getClickhouseSettings(): ClickHouseSettings { return { distributed_product_mode: 'allow', date_time_input_format: 'best_effort', + // ClickHouse 26.3+ defaults async_insert to 1. The buffers already batch + // client-side, so inserts stay synchronous; a buffer can still opt in per query. + async_insert: 0, ...(process.env.CLICKHOUSE_SETTINGS_REMOVE_CONVERT_ANY_JOIN ? {} : { diff --git a/packages/db/src/code-migrations/14-profile-event-property-summary-mv.ts b/packages/db/src/code-migrations/14-profile-event-property-summary-mv.ts index 6c4b4438c..efe820650 100644 --- a/packages/db/src/code-migrations/14-profile-event-property-summary-mv.ts +++ b/packages/db/src/code-migrations/14-profile-event-property-summary-mv.ts @@ -38,6 +38,9 @@ export async function up(env: CodeMigrationEnv) { 'event_date', ], partitionBy: 'toYYYYMM(event_date)', + // property_value is grouped on but sits outside the sorting key, which + // ClickHouse 26.7+ rejects for AggregatingMergeTree unless allowed. + settings: { allow_dimensions_outside_sorting_key: 1 }, query: `SELECT project_id, profile_id, From 55dc47ca2ae1f1340777a86aad17ae7fa51d14de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carl-Gerhard=20Lindesva=CC=88rd?= Date: Mon, 26 Oct 2026 10:17:08 +0100 Subject: [PATCH 2/2] fix(db): migration 14 only sets allow_dimensions_outside_sorting_key where ClickHouse knows it The setting exists from ClickHouse 26.7. Older servers, including the 26.1 that CI and the compose template run, reject the CREATE with "Unknown setting 'allow_dimensions_outside_sorting_key'", so a fresh install on them stopped at migration 14. The migration now asks system.merge_tree_settings whether the server knows the setting and only then adds it. The full chain was run on 25.10.2.65, 26.1.3.52 and 26.8.13.2. Review caught the unconditional setting on the first commit. --- packages/db/src/clickhouse/migration.ts | 13 +++++++++++++ .../14-profile-event-property-summary-mv.ts | 13 ++++++++++--- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/packages/db/src/clickhouse/migration.ts b/packages/db/src/clickhouse/migration.ts index e8544cf65..fb2b5099e 100644 --- a/packages/db/src/clickhouse/migration.ts +++ b/packages/db/src/clickhouse/migration.ts @@ -194,6 +194,19 @@ export async function getExistingTables() { } } +// A MergeTree setting that is unknown to the server fails the CREATE, so a +// migration checks before it sets one that only newer releases know. +export async function hasMergeTreeSetting(name: string): Promise { + const result = await chMigrationClient.query({ + query: + 'SELECT count() AS total FROM system.merge_tree_settings WHERE name = {name:String}', + query_params: { name }, + format: 'JSONEachRow', + }); + const [row] = await result.json<{ total: string }>(); + return Number(row?.total ?? 0) > 0; +} + export interface TableKeys { sortingKey: string[]; partitionKey: string; diff --git a/packages/db/src/code-migrations/14-profile-event-property-summary-mv.ts b/packages/db/src/code-migrations/14-profile-event-property-summary-mv.ts index efe820650..cb03a205f 100644 --- a/packages/db/src/code-migrations/14-profile-event-property-summary-mv.ts +++ b/packages/db/src/code-migrations/14-profile-event-property-summary-mv.ts @@ -3,6 +3,7 @@ import { createMaterializedView, dropTable, getExistingTables, + hasMergeTreeSetting, runClickhouseMigrationCommands, } from '../clickhouse/migration'; import { type CodeMigrationEnv, getIsCluster, writeSqlDump } from './helpers'; @@ -14,6 +15,12 @@ export async function up(env: CodeMigrationEnv) { const replicatedVersion = '1'; const existingTables = await getExistingTables(); const isClustered = getIsCluster(env); + // property_value is grouped on but sits outside the sorting key. ClickHouse + // 26.7+ rejects that for AggregatingMergeTree unless allowed; older servers + // do not know the setting and would reject the CREATE for naming it. + const allowDimensionsOutsideSortingKey = await hasMergeTreeSetting( + 'allow_dimensions_outside_sorting_key' + ); const sqls: string[] = []; @@ -38,9 +45,9 @@ export async function up(env: CodeMigrationEnv) { 'event_date', ], partitionBy: 'toYYYYMM(event_date)', - // property_value is grouped on but sits outside the sorting key, which - // ClickHouse 26.7+ rejects for AggregatingMergeTree unless allowed. - settings: { allow_dimensions_outside_sorting_key: 1 }, + settings: allowDimensionsOutsideSortingKey + ? { allow_dimensions_outside_sorting_key: 1 } + : {}, query: `SELECT project_id, profile_id,