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.

fix(data-imports): point MotherDuck's DuckDB connection at a writable home directory (#80898)

Tom Owerse3a5c7d7a136
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/motherduck/motherduck.py b/products/warehouse_sources/backend/temporal/data_imports/sources/motherduck/motherduck.py
index e32c5482f358..330842455419 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/motherduck/motherduck.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/motherduck/motherduck.py
@@ -14,7 +14,9 @@ materializes at once (an Arrow record-batch reader rather than a full fetch).
from __future__ import annotations
+import os
import re
+import tempfile
import collections
from collections.abc import Iterator
from contextlib import contextmanager
@@ -70,7 +72,14 @@ DEFAULT_MOTHERDUCK_FETCH_SIZE = 5_000
# DuckDB otherwise sizes itself against the whole host (80% of RAM, one thread per core), which is
# the wrong budget for a library sharing a worker with the rest of the import pipeline.
-DUCKDB_LOCAL_CONFIG: dict[str, Any] = {"memory_limit": "2GB", "threads": 2}
+#
+# `home_directory` overrides where DuckDB persists extensions/secrets, which otherwise defaults to
+# `~/.duckdb` — unwritable in a locked-down worker container regardless of who owns the process.
+DUCKDB_LOCAL_CONFIG: dict[str, Any] = {
+ "memory_limit": "2GB",
+ "threads": 2,
+ "home_directory": os.path.join(tempfile.gettempdir(), "posthog-duckdb-home"),
+}
# The database name is interpolated into the `md:` connection string, so anything outside this
# allowlist could smuggle extra connection parameters in alongside the token.
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/motherduck/tests/test_motherduck.py b/products/warehouse_sources/backend/temporal/data_imports/sources/motherduck/tests/test_motherduck.py
index 05cbfebab43b..f23263704a5c 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/motherduck/tests/test_motherduck.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/motherduck/tests/test_motherduck.py
@@ -1,3 +1,5 @@
+import os
+
import pytest
from unittest.mock import MagicMock, patch
@@ -129,6 +131,14 @@ class TestMotherDuck:
# DuckDB shares this process, so it must not size itself against the whole host.
assert mock_connect.call_args.kwargs["config"] == DUCKDB_LOCAL_CONFIG
+ def test_connect_uses_a_writable_home_directory(self):
+ # Regression: DuckDB defaults extension/secrets storage to `~/.duckdb`, which raised
+ # `IOException: Failed to create directory "/root/.duckdb": Permission denied` in a
+ # worker container where the real home directory isn't writable.
+ home_directory = DUCKDB_LOCAL_CONFIG["home_directory"]
+ os.makedirs(home_directory, exist_ok=True)
+ assert os.access(home_directory, os.W_OK)
+
def test_connect_closes_on_exit(self, impl):
with patch(_CONNECT_PATH) as mock_connect:
with impl.connect(_make_config()):