Skip to content

fix(postgres): carry tree-sitter-sql in the postgres extra and surface grammar errors - #3072

Closed
Azeem1985 wants to merge 1 commit into
Graphify-Labs:v8from
TelB-io:upstream/postgres-sql-grammar
Closed

Azeem1985 wants to merge 1 commit into
Graphify-Labs:v8from
TelB-io:upstream/postgres-sql-grammar

Conversation

@Azeem1985

Copy link
Copy Markdown
Contributor

Replaces #3048. Same fix, but that PR was opened from a branch carrying our whole fork — 62 files and 41 commits for a change that is two lines plus a test. Nobody should have to review that, and I am sorry for sending it in that shape. This branch is cut from v8 and carries only the fix. #3048 is being closed with a pointer here.

Fixes #3047.

The defect

pip install "graphifyy[postgres]" — the command the README gives — installs psycopg but not tree-sitter-sql.

introspect_postgres() reconstructs the catalog as DDL text and hands it to extract_sql(). Without the grammar, extract_sql() returns {"error": ..., "nodes": [], "edges": []}. introspect_postgres() returned that dict through unchanged, so the CLI printed:

PostgreSQL: 0 nodes, 0 edges

and exited 0 — indistinguishable from pointing at an empty database.

Reproduced

postgres:17-alpine, graphifyy 0.9.48, installed exactly as documented. Database with 3 tables, foreign keys between them, one view and one function. Result: 0 nodes, 0 edges, exit 0.

The change

  1. pyproject.toml: the postgres extra now lists tree-sitter-sql alongside psycopg[binary], so the documented install works.
  2. graphify/pg_introspect.py: a grammar error coming back from extract_sql raises ImportError with the install hint, instead of being reported as an empty database.

The sql and all extras already carried the grammar — only postgres was short, which is probably why this went unnoticed. The second half is there because even with the extra fixed, an environment that has the driver and not the grammar should say so rather than report zero.

Diff

4 files: pyproject.toml (+5/-1), graphify/pg_introspect.py (+8), tests/test_pg_introspect.py (+20), CHANGELOG.md (+4). 9 tests pass.

…e grammar errors

`pip install "graphifyy[postgres]"` — the command the README gives —
installs the psycopg driver but not the tree-sitter-sql grammar.
`introspect_postgres` reconstructs the catalog as DDL and hands it to
`extract_sql()`, which without that grammar returns `{"error": ..., nodes:
[], edges: []}`. `introspect_postgres` returned that dict unchanged, so
the CLI printed "PostgreSQL: 0 nodes, 0 edges" and exited 0.

Reproduced on postgres:17-alpine with graphifyy 0.9.48: a database with
3 tables, foreign keys, a view and a function reported 0 nodes and
exited 0 — indistinguishable from an empty database.

Two lines of fix:
- the `postgres` extra now lists `tree-sitter-sql` alongside `psycopg`,
  so the documented install works;
- a grammar error from `extract_sql` raises with the install hint instead
  of being reported as an empty database.

The `sql` and `all` extras already carried the grammar, which is why this
went unnoticed — only the `postgres` extra was short.

Tests: 9 covering the empty-result surfacing and the existing catalog paths.

@graphify-labs graphify-labs Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Graphify reviewed this change.

Worth a look — the grounded gate found no coupling regressions or blocking issues, but 1 advisory finding(s) below merit a look before merge.

Formal verification. No changes could be formally verified in this run.


Graphify review — findings

Adds tree-sitter-sql to the postgres extra so --postgres introspection actually has the SQL grammar it feeds reconstructed DDL into. Makes introspect_postgres raise an ImportError with the pip install 'graphifyy[postgres]' hint when extract_sql reports a missing-grammar error, instead of silently returning an empty graph and printing "PostgreSQL: 0 nodes" as if the database had no tables.

Worth a look

  • introspect_postgres now raises ImportError for any extract_sql error — graphify/pg_introspect.py:160 · Escalate · medium
    • agreed by 2 of 2 members but NOT verified (no proof, no reproducing execution) — consensus is not a verdict; needs human review
Analysis details — impact, health, verification

Impact & health

Graphify review

Impact — 379 functions depend on the 224 functions this change touches.

Health — this change adds coupling hotspots:

  • new: dispatch_command() — 2 callers, 122 callees
  • new: test_poisoned_manifest_is_healed() — 0 callers, 6 callees

Verification — 379 functions in the blast radius were not formally verified this run (proofs are advisory here).

Gate & verification

graphify gate

PASS — objectively clean (no health regressions, tests not run — proofs not run this pass (advisory)). Grounded, not self-assessed.

Advisory (not blocking):

  • verification_scope: 229 function(s) in the blast radius were not formally verified this run

Formal verification

Could not verify: Could not verify introspect\_postgres.

The verifier did not have enough to check introspect\_postgres, so it is saying so rather than guessing. No false assurance is the whole point.

Guarantee: No guarantee either way, this is an honest abstention, not a pass.

Note: Reason: not verifiable: all 24 sampled inputs raised on both versions — the function never executed, so 'no divergence' would be vacuous (mostly ProgrammingError — names the real obstacle, not a sampling gap)

· 2 more finding(s) on lines outside this diff (see the check run).

@Azeem1985

Copy link
Copy Markdown
Contributor Author

Re "introspect_postgres now raises ImportError for any extract_sql error" (pg_introspect.py:160, marked "NOT verified"): the guard does key on any error value — but every value extract_sql can put there is an environment/grammar failure, so the ImportError classification holds for each reachable path.

The error key has exactly three writers in graphify/extractors/sql.py:

  1. tree_sitter_sql not installed (the missing-grammar case this PR is about),
  2. tree_sitter_sql installed but failed to load (e.g. a wrong-ABI C extension, the .sql files skipped as "tree_sitter_sql not installed" while the module loads and parses fine #2602 case),
  3. the except Exception around Language(...)/Parser(...)/parse(...) — grammar/parser initialization.

Data-dependent failures cannot reach it: tree-sitter is error-tolerant, so malformed SQL yields ERROR nodes in the tree rather than raising, and on this path the DDL arrives in-memory (content=ddl_string), so the path.read_bytes() branch is never taken. The raise also embeds the original error string first, so cases 2 and 3 surface their real cause rather than being masked by the install hint.

The one cosmetic wrinkle is that a case-3 failure would carry the pip install 'graphifyy[postgres]' suffix even though installing would not help. If you'd rather the hint decorate only the two import branches and case 3 re-raise as a plain RuntimeError, say the word and I'll push that tightening.

@safishamsi

Copy link
Copy Markdown
Member

Shipped in v0.9.50 via authorship-preserving cherry-pick so you keep contributor-graph credit. Thanks @Azeem1985! Release: https://github.com/Graphify-Labs/graphify/releases/tag/v0.9.50

@safishamsi safishamsi closed this Aug 25, 2026
@Azeem1985

Copy link
Copy Markdown
Contributor Author

Thanks — confirmed both halves are in the v0.9.50 tag (the extra carries the grammar, and grammar errors now surface as ImportError). Appreciate the credit-preserving cherry-pick.

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.

postgres extra lacks tree-sitter-sql: --postgres on a live DB silently reports 0 nodes, exit 0

2 participants