Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

feat: move bk to its own ch user (#95201)

Alex Vfc975700018d
diff --git a/posthog/clickhouse/client/connection.py b/posthog/clickhouse/client/connection.py
index 390ec05315b2..9c51d7944034 100644
--- a/posthog/clickhouse/client/connection.py
+++ b/posthog/clickhouse/client/connection.py
@@ -94,6 +94,7 @@ class ClickHouseUser(StrEnum):
ERROR_TRACKING = "error_tracking"
ENDPOINTS = "endpoints"
BILLING = "billing"
+ BUSINESS_KNOWLEDGE = "business_knowledge"
REPLAY_VISION = "replay_vision"
# Session replay surfacing scoring sweep
SURFACING_SCORING = "surfacing_scoring"
@@ -163,8 +164,9 @@ def get_clickhouse_creds(user: ClickHouseUser) -> ClickHouseCredentials:
Retrieve ClickHouse credentials for the specified user.
This function retrieves the credentials associated with a given ClickHouse
- user. If the specified user is not found, it will fall back to the default
- user credentials.
+ user. Most missing dedicated users fall back to the default user credentials.
+ Business Knowledge fails closed so a deployment cannot silently bypass its
+ isolated query limit.
The user and password must be properly passed as ENVs:
CLICKHOUSE_<USER_NAME>_USER
@@ -177,7 +179,14 @@ def get_clickhouse_creds(user: ClickHouseUser) -> ClickHouseCredentials:
global __user_dict
if not __user_dict:
__user_dict = init_clickhouse_users()
- return __user_dict.get(user, __user_dict[ClickHouseUser.DEFAULT])
+ if creds := __user_dict.get(user):
+ return creds
+ if user == ClickHouseUser.BUSINESS_KNOWLEDGE:
+ raise RuntimeError(
+ "Business Knowledge ClickHouse credentials are missing; set "
+ "CLICKHOUSE_BUSINESS_KNOWLEDGE_USER and CLICKHOUSE_BUSINESS_KNOWLEDGE_PASSWORD"
+ )
+ return __user_dict[ClickHouseUser.DEFAULT]
class ProxyClient:
diff --git a/posthog/clickhouse/client/test/test_connection.py b/posthog/clickhouse/client/test/test_connection.py
index 641dc8d1e62d..2e1bdbe2039e 100644
--- a/posthog/clickhouse/client/test/test_connection.py
+++ b/posthog/clickhouse/client/test/test_connection.py
@@ -9,6 +9,7 @@ from posthog.clickhouse.client.connection import (
ClickHouseUser,
RefreshingChPool,
Workload,
+ get_clickhouse_creds,
get_http_client,
get_http_kwargs,
get_pool,
@@ -117,6 +118,14 @@ def test_password_file_env_registers_file_backed_user(monkeypatch, tmp_path):
assert creds.read_password() == "cohorts-token-value"
+def test_business_knowledge_credentials_do_not_fall_back_to_default(monkeypatch):
+ default_creds = ClickHouseCredentials(user="default", password="default-password")
+ monkeypatch.setattr(connection, "__user_dict", {ClickHouseUser.DEFAULT: default_creds})
+
+ with pytest.raises(RuntimeError, match="CLICKHOUSE_BUSINESS_KNOWLEDGE_USER"):
+ get_clickhouse_creds(ClickHouseUser.BUSINESS_KNOWLEDGE)
+
+
def test_file_backed_pool_is_stable_across_credential_rotation(settings, monkeypatch, tmp_path):
settings.CLICKHOUSE_OFFLINE_CLUSTER_HOST = None
token = tmp_path / "token"
diff --git a/products/business_knowledge/backend/temporal/coordinator.py b/products/business_knowledge/backend/temporal/coordinator.py
index ed950c0e3f38..51a71d645c9b 100644
--- a/products/business_knowledge/backend/temporal/coordinator.py
+++ b/products/business_knowledge/backend/temporal/coordinator.py
@@ -34,6 +34,7 @@ from posthog.hogql import ast
from posthog.hogql.query import execute_hogql_query
from posthog.api.embedding_worker import emit_embedding_request
+from posthog.clickhouse.client.connection import ClickHouseUser, Workload
from posthog.models.team import Team
from posthog.sync import database_sync_to_async
from posthog.temporal.common.base import PostHogWorkflow
@@ -219,6 +220,9 @@ def _present_chunk_ids_in_clickhouse(team_id: int, chunk_ids: list[UUID]) -> set
result = execute_hogql_query(
query=query,
team=team,
+ query_type="BusinessKnowledgeReconcileEmbeddings",
+ workload=Workload.OFFLINE,
+ ch_user=ClickHouseUser.BUSINESS_KNOWLEDGE,
placeholders={
"product": ast.Constant(value=BK_EMBEDDING_PRODUCT),
"document_type": ast.Constant(value=BK_EMBEDDING_DOCUMENT_TYPE),
diff --git a/products/business_knowledge/backend/tests/test_background_refresh.py b/products/business_knowledge/backend/tests/test_background_refresh.py
index 2bf62c555652..f2a9c0acfeb5 100644
--- a/products/business_knowledge/backend/tests/test_background_refresh.py
+++ b/products/business_knowledge/backend/tests/test_background_refresh.py
@@ -1,9 +1,13 @@
import datetime
+from uuid import uuid4
from posthog.test.base import BaseTest
+from unittest.mock import MagicMock, patch
+from django.test import SimpleTestCase
from django.utils import timezone
+from posthog.clickhouse.client.connection import ClickHouseUser, Workload
from posthog.models.scoping import team_scope
from products.business_knowledge.backend import logic
@@ -14,7 +18,11 @@ from products.business_knowledge.backend.models import (
SourceStatus,
SourceType,
)
-from products.business_knowledge.backend.temporal.coordinator import RefreshSourceInputs, _host_serialized_batches
+from products.business_knowledge.backend.temporal.coordinator import (
+ RefreshSourceInputs,
+ _host_serialized_batches,
+ _present_chunk_ids_in_clickhouse,
+)
class TestDueRefreshSelection(BaseTest):
@@ -104,6 +112,23 @@ class TestHostSerializedBatches(BaseTest):
assert batches == [[RefreshSourceInputs(team_id=7, source_id="x")]]
+class TestEmbeddingReconciliation(SimpleTestCase):
+ def test_uses_dedicated_offline_clickhouse_user(self) -> None:
+ chunk_id = uuid4()
+ with (
+ patch("products.business_knowledge.backend.temporal.coordinator.Team.objects.get"),
+ patch(
+ "products.business_knowledge.backend.temporal.coordinator.execute_hogql_query",
+ return_value=MagicMock(results=[[str(chunk_id)]]),
+ ) as execute_hogql_query,
+ ):
+ present = _present_chunk_ids_in_clickhouse(1, [chunk_id])
+
+ assert present == {str(chunk_id)}
+ assert execute_hogql_query.call_args.kwargs["ch_user"] == ClickHouseUser.BUSINESS_KNOWLEDGE
+ assert execute_hogql_query.call_args.kwargs["workload"] == Workload.OFFLINE
+
+
class TestTombstoneSweep(BaseTest):
def _doc(self, *, source: KnowledgeSource, tombstoned_days_ago: int | None) -> KnowledgeDocument:
tombstoned_at = (