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/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 6c4b4438c..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,6 +45,9 @@ export async function up(env: CodeMigrationEnv) { 'event_date', ], partitionBy: 'toYYYYMM(event_date)', + settings: allowDimensionsOutsideSortingKey + ? { allow_dimensions_outside_sorting_key: 1 } + : {}, query: `SELECT project_id, profile_id,