Skip to content

Read Postgres tables through pg_hydradb with a read-only store #706

Done

Description

@adityamishra-hydradb

Self Graph cannot read data that already lives in Postgres tables. Today a user with people, companies, and an employment table has to copy the rows out and insert them as text or facts. The copy goes stale on the next write.

pg_hydradb already keeps an index of the links between registered tables, updated by triggers in the writer's transaction. It answers "what is one hop from this row" in one SQL call. Self Graph can read through it without owning the data.

Proposal

Add a read-only store, PgHydraDBStore, behind a new selfgraph[postgres] extra.

  • A row of a registered vertex table is an entity (person:1). A row of an edge table is a current relation with one version (relation:works_at:10). Types come from the labels, as Schema.from_tables names a table (person is Person, works_at is WORKS_AT). draft_schema() returns them as a schema.
  • The store implements the read primitives that retrieve, neighbors, find_path, entities.get, entities.find, and relations.get use. Each hop is one hydradb.reachable_edges call.
  • Every write primitive raises a coded read_only_store error. A read that lists or counts every row, or runs Cypher, raises unsupported_read. A graph that is missing, paused, or unreadable fails initialize with graph_unavailable.
  • Seeding: retrieve today scans every entity page. A store can now supply seed candidates through an optional seed_candidates(ns, terms) read. The Postgres store runs a fixed WHERE <name> ILIKE $1 LIMIT $2 per table and query word, and BM25 ranks the hits as today. _ops stays backend-unaware.
  • Partial reads: a store that hits an engine limit calls mark_partial(). The retrieve, neighbors, and find_path result then has truncated: true.

Database access contract

  • Every connection starts with default_transaction_read_only=on and a statement_timeout. The pool runs no reset statement.
  • Only fixed, parameterised statements: registry reads, hydradb.reachable_edges, SELECT <cols> FROM <registered table> WHERE <key> = ANY(...), and the ILIKE lookup.
  • Identifiers come only from the pg_hydradb registry and are quoted. Values are always bound. No DDL, no writes, no LLM- or user-written SQL. No password or URI in any error.

Plan

One stack of PRs, each under 1,000 changed lines:

  1. Store read hooks and the three error codes, in both SDKs.
  2. The Python store, its tests, and the docs.
  3. The TypeScript store.
  4. store=postgres in the runtime, CLI, and MCP server of both SDKs.

Out of scope

Writes, fact history, and contradiction handling over Postgres (the tables are the source of truth). Postgres as Self Graph's own storage is a separate, larger change.

Decisions

The maintainers decided the open questions on 2026-12-05:

  • CI: #729 adds the CI job postgres. It builds pg_hydradb from a pinned hydradb commit, caches the build, and runs the gated tests of both SDKs on Postgres 16 and 17. No image goes to a registry.
  • Drivers: asyncpg (asyncpg>=0.30) for Python, node-postgres pg (optional peer dependency) for TypeScript.
  • check_answerable stays refused on this store with unsupported_read; the docs page states this limit.
  • AGENTS.md: a schema draft may read a store's own catalog and registry, never user rows.

Activity

  1. adityamishra-hydradb commented on Dec 4, 2026

    @adityamishra-hydradb
    CollaboratorAuthor

    /approved

  2. added
    approved-for-pr
    on Dec 4, 2026
  3. github-actions commented on Dec 4, 2026

    @github-actions

    Approved by @adityamishra-hydradb. @adityamishra-hydradb or an assignee of this issue can now open a PR that links it with Closes #706 (max 10 open PRs at a time). Anyone else can comment /take on this issue to be assigned.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    approved-for-pr

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions