Skip to content

CREATE POLICY is silently dropped from SQL graphs — grammar has no rule, and the ERROR fallback regex only covers FUNCTION/PROCEDURE #3401

Description

@kwunyanlee-cmd

Summary

On a Postgres project that uses Row Level Security, no policy ever becomes a
node
, and nothing warns you. On my repository that is 59 create policy
statements (45 distinct policy names) that exist in the source and are absent
from graph.json.

This matters more than a normal missing-node-type bug, because of what
policies are. In a Supabase/Postgres app the policy is the authorisation
rule — it is the answer to "who may read this row". A graph that silently omits
every policy will happily answer questions about access control, and it will
answer them wrong. A traversal that returns nothing reads as "no rule exists"
rather than "this tool cannot see rules".

Version: graphify 0.9.48, with graphifyy[sql] installed (tree_sitter_sql
importable).

Root cause — two layers, and the first one is the real blocker

1. The tree-sitter-sql grammar has no rule for CREATE POLICY.

Parsing a migration containing seven policies produces no create_policy node
of any kind. The statements disintegrate into loose top-level tokens, plus an
ERROR node:

L96    keyword_create
L96    keyword_on
L96    .
L97    keyword_for      keyword_update      keyword_to
L98    keyword_using    (  .  (  )  keyword_and  .  (  )  )
L99    keyword_with     keyword_check  ( ... )
L99    ;

So there is nothing for walk() in extractors/sql.py to dispatch on. Adding
an elif t == "create_policy": branch would not help — that node type is never
produced.

2. The ERROR fallback in extractors/sql.py only rescues functions and
procedures.

sql.py already knows the grammar gives up on PL/pgSQL and has a regex
recovery path for it:

elif t == "ERROR":
    # tree-sitter-sql cannot parse PL/pgSQL CREATE FUNCTION/PROCEDURE
    # bodies (OUT/INOUT params, tagged dollar quotes, PERFORM, :=) and
    # emits an ERROR node instead, silently dropping the object.
    # Regex-scan the raw text as fallback ...
    for m in re.finditer(
        r"CREATE\s+(?:OR\s+REPLACE\s+)?(?:FUNCTION|PROCEDURE)\s+" ...

Policies land in exactly the same place — ERROR nodes and loose tokens — but
the alternation is FUNCTION|PROCEDURE, so they are dropped.

3. pg_introspect.py (the --postgres path) does not cover it either. It
queries information_schema tables/views/routines plus pg_constraint, and
never pg_policy — so live introspection has the same hole as static
extraction.

Reproduce

-- policies.sql
create table public.employees (id uuid primary key, home_branch_id uuid);

create or replace function app.is_admin() returns boolean
  language sql stable as $$ select true $$;

create policy employees_select on public.employees
  for select to authenticated
  using (app.is_admin());

create policy employees_update on public.employees
  for update to authenticated
  using (app.is_admin()) with check (app.is_admin());

Activity

  1. AromalBiju1 commented on Sep 7, 2026

    @AromalBiju1
    Contributor

    Checked and confirmed — create_policy isn't a node type tree-sitter-sql's grammar produces at all, so it's not a walk() dispatch gap, it's the same ERROR-node dead end FUNCTION/PROCEDURE bodies hit. The existing regex fallback just doesn't cover it.

    Patch attached: extends the ERROR fallback in sql.py with a CREATE POLICY regex (captures name, table, command, permissive/restrictive, roles, and pulls function refs out of the USING/WITH CHECK bodies as references edges — so e.g. employees_select → app.is_admin() shows up in the graph). Also added a pg_policy query to pg_introspect.py for the --postgres path, using pg_get_expr() to resolve the qual/check expressions properly instead of regexing raw SQL.

    Ran it against the repro migration — both policies show up as policy nodes with applies_to edges to public.employees and references edges to app.is_admin.

  2. lrafasouza commented on Sep 10, 2026

    @lrafasouza

    Corroborating this from a production codebase, with numbers, because the impact is larger than a
    missing node type and I think it strengthens the case for merging #3456.

    Measured on a Next.js + Supabase app with row-level security as its primary authorization mechanism:
    166 tracked .sql files, comments and single-quoted strings stripped before counting, each
    declaration name then matched against node labels in the same source file.

    category declared in graph
    CREATE TABLE 139 136
    CREATE VIEW 42 38
    CREATE FUNCTION 188 188
    CREATE TRIGGER 122 113
    CREATE POLICY 440 0

    All 440 absent, across 47 files. Two things about this were worse than I expected:

    1. Per-file coverage reports a clean pass. All 166 .sql files produced at least one node, so
    every file-level coverage check says SQL is fully mapped while the entire authorization layer is
    missing. The output gives no signal that anything was dropped.

    2. The graph does not go quiet about the missing entities — it substitutes. Asking
    graphify query "which RLS policies protect the profiles table" returned 394 nodes, drawn from test
    files, a Drizzle schema symbol, UI components and prose documentation, citing zero .sql sources
    and zero policy names. An agent reading that answer would reasonably conclude the policies it names
    are the relevant ones. A plausible substitute is harder to notice than an empty result.

    There is a nice irony that argues the fix belongs at the statement level rather than the name level:
    the graph does contain public.is_endorsed_member(), the helper function those policies call, with
    a correct reads_from [EXTRACTED] edge. It has the function and none of the policies that invoke it,
    so the call graph around authorization is real but headless.

    One adjacent finding while measuring this: CREATE INDEX is dropped by the same mechanism — 218
    declared, 0 in the graph, reproducible in a 19-line file where TABLE, VIEW and FUNCTION all land.
    I filed it as #3467 rather than piling it here, but flagging it because #3456 fixes policies only,
    and merging it would leave the index half of the same hole open.

    Environment: graphify 0.9.57, Python 3.12.14, macOS arm64, tree_sitter_sql verified importable in
    the tool env before measuring.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions