Skip to content

An org reads the SQL surface from its own Postgres through pg_clickhouse - #461

Merged
zlareb1 merged 6 commits into
mainfrom
feat/pg-clickhouse-guide
Nov 10, 2026
Merged

zlareb1 merged 6 commits into
mainfrom
feat/pg-clickhouse-guide

Conversation

@zlareb1

@zlareb1 zlareb1 commented Nov 10, 2026 •

Copy link
Copy Markdown
Member

Related: #450

Changelog category (leave one):

  • Documentation (changelog entry is not required)

Changelog entry (one or two sentences a user of Loom can read without the diff; the release pull request collects these into the release notes):

A guide shows how an org reads Loom's SQL surface from its own Postgres through the pg_clickhouse extension and joins it with application tables, and CI tests that path on Postgres 16 and 18.

Why

The SQL surface (#450) gives an org's SQL user read access to its memories, conversations and request ledger. Many apps keep their own data in Postgres, so the useful join is in Postgres: Loom's facts about a user next to the app's row for that user. pg_clickhouse imports ClickHouse tables and views as foreign tables and pushes queries down. This PR documents that path and holds it to a CI test, so it keeps working as the views evolve.

Design

  • Guide. docs/guides/query-from-postgres.md covers five steps:
    1. make a SQL user with loom sql-access grant;
    2. install the extension;
    3. CREATE SERVER … FOREIGN DATA WRAPPER clickhouse_fdw OPTIONS (driver 'http', …), a user mapping for the owner and one for the reader, and the reader's session settings;
    4. IMPORT FOREIGN SCHEMA … LIMIT TO (the five views) and the reader's grants;
    5. query and join.
  • Roles. Three roles take part.
    • A superuser runs CREATE EXTENSION, because the extension's control file does not mark it trusted.
    • An owner with CREATEROLE, CREATE on the database and USAGE on the wrapper runs steps 3 and 4. It creates the reader role, so it may set that role's defaults. It needs its own user mapping, because IMPORT FOREIGN SCHEMA connects as the role that runs it.
    • The application's reader role holds only its mapping and the grants on the imported schema.
  • Session settings. The role carries exactly the four settings a SQL user of the surface may change: join_use_nulls, group_by_use_nulls, final, transform_null_in. These are the extension's defaults, written down so a later release's defaults cannot add one. Any other setting fails with READONLY in ClickHouse, whatever the role says, because the SQL user's profile refuses it.
  • Scale. Every reader session logs in as the same SQL user, so the application's pool must stay under the user's concurrency ceiling. Postgres runs a join between a foreign table and a local one, so it fetches every view row the pushed-down filters leave. A pushed-down filter narrows what ClickHouse sends, not what it reads (each view reads the grant's scope). A statement past the SQL user's ceilings fails rather than slowing down.
  • Erasure. Foreign tables hold nothing, so an erasure in Loom reaches them at once. A copy taken in this Postgres (CREATE TABLE … AS SELECT, a materialized view, a dump) belongs to the org and is outside Loom's erasure.
  • Kept out of Loom's write path. This Postgres is the app's, never Loom's LOOM_PG_DSN, and Loom's own database needs no extension. Neither pg_clickhouse nor Managed Postgres CDC takes part in Loom's writes; that was decided earlier, for vectors, erasure fencing and failover.
  • Version. pg_clickhouse 0.11.0 (GitHub release of 2026-09-30) through the official images ghcr.io/clickhouse/pg_clickhouse:{16,18}-0.11.0. 0.11.1 came out the day this was written and is not pinned yet. The extension reports extversion 0.11, and the test checks it against the pinned release.

Scope and risk

  • Touches:
    • the guide;
    • a link from the SQL surface reference;
    • dev/ci_start_pg_clickhouse.sh: host network, listening on 127.0.0.1:5433 only, with test_web_exposure checking that;
    • tests/integration/test_pg_clickhouse_live_ch.py;
    • the integration-postgres CI job, which now also starts the image and runs this test plus test_sql_surface_live_ch.py. The latter now also runs the SQL surface under the Postgres memory authority, the deployment shape; both files passed in that job's memory step on an earlier head of this PR;
    • docs/development/testing.md.
  • No server code changes.
  • Risk: CI now pulls ghcr.io/clickhouse/pg_clickhouse for each Postgres major. A registry outage would fail that job's start step.

Compatibility, rollout and rollback

  • Wire: none.
  • Operator steps: none for Loom. The guide's steps run on the org's own Postgres.
  • Rollback: revert. Nothing is stored.

Tests

  • tests/integration/test_pg_clickhouse_live_ch.py runs against pg_clickhouse 16-0.11.0 locally and passes. It runs the guide's roles: steps 3 and 4 as a non-superuser owner on its own connection, step 5 as the reader. It skips without LOOM_PGCH_DSN. It covers:
    • two orgs' memories in ClickHouse;
    • a SQL user granted one org and namespace;
    • the views imported as foreign tables;
    • a join with a local accounts table that returns only that tenant's rows, with the high value and its hash NULL;
    • the four allowed session settings accepted and max_threads 2 refused with READONLY;
    • after loom sql-access revoke, the next query failing to authenticate.
  • What CI does not cover: TLS to a ClickHouse Cloud host (the extension's documented default of TLS on 8443). The test uses plain HTTP to a local server, and the guide says so.
  • make format lint typecheck, test_web_exposure, test_doc_links, test_public_copy, test_agent_instructions and test_integration_suite_targets pass.

AI assistance in the code (leave one; writing this description with AI is fine):

  • AI-generated (an agent wrote most of the diff): Claude Code. The design went through an architect review to zero findings. The integration test was run by hand against the pinned image.

  • I can explain every line of this diff and how it interacts with the rest of Loom.

Checklist

  • make format-check, make lint, make typecheck and make test-unit pass; the integration suite passes if server behaviour changed.
  • Generated files are regenerated in this PR, not by hand: the SDK op tables, docs/reference/configuration.md and the docs/reference/ tables (python scripts/codegen/docs_reference.py), the reach manifest, schemas/schema.sql (make schema-dump) and the Codex plugin copies (python3 scripts/codegen/agent_plugin_sync.py).
  • A new migration is additive and numbered after the latest on main; no merged migration is edited.
  • A behaviour change to any file a plugin ships bumps both plugin manifests' version, and no other open PR uses that version.
  • The frontend builds (npm --prefix frontend run build) and its tests pass, if frontend/ changed.
  • Docs under docs/ describe the system as it now is, and this description names any operator steps and wire changes.
  • Repository text has no dates, people, host names, internal namespaces, ticket or decision numbers; the history goes in this description (tests/unit/test_public_copy.py).
  • Commits are signed off (git commit -s), as CONTRIBUTING.md asks.

🤖 Generated with Claude Code

zlareb1 and others added 6 commits November 10, 2026 01:07
…nd the test runs as the reader

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…d the test runs the owner steps without a superuser

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…ssion shares and what a managed service must allow

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…d its examples carry neutral facts

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…s written

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
@zlareb1
zlareb1 merged commit 5ba35e8 into main Nov 10, 2026
67 of 68 checks passed
@zlareb1
zlareb1 deleted the feat/pg-clickhouse-guide branch November 10, 2026 03:37
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.

1 participant