Skip to content

Conversation

@jonathan-fulton
Copy link

Summary

This PR adds a new API endpoint GET /dependency-graph that queries PostgreSQL system catalogs to return database object dependencies as nodes and edges, suitable for graph visualization.

Motivation

Understanding database schema dependencies is challenging in complex projects. This endpoint enables building visual tools that show:

  • Which tables a view depends on
  • What tables a function references
  • Which triggers fire on which tables
  • Foreign key relationships at a glance
  • RLS policies and their target tables
  • Index coverage across tables

This is the backend foundation for an interactive Schema Graph feature in Supabase Studio.

Changes

New Files

  • src/lib/PostgresMetaDependencyGraph.ts - Main class that queries and transforms dependency data
  • src/lib/sql/dependency_graph.sql.ts - SQL queries for nodes and edges
  • src/server/routes/dependency-graph.ts - Fastify route handler

Modified Files

  • src/lib/PostgresMeta.ts - Register dependency graph class
  • src/lib/index.ts - Export new types
  • src/lib/types.ts - Add TypeScript types and schemas
  • src/server/routes/index.ts - Register route

API

Endpoint

GET /dependency-graph

Query Parameters

Parameter Type Description
include_system_schemas boolean Include pg_catalog, information_schema, etc.
included_schemas string Comma-separated list of schemas to include
excluded_schemas string Comma-separated list of schemas to exclude
included_types string Comma-separated list of object types to include

Response

{
  nodes: Array<{
    id: number,        // OID
    name: string,      // Object name
    schema: string,    // Schema name
    type: 'table' | 'view' | 'materialized_view' | 'function' | 
          'trigger' | 'policy' | 'index' | 'sequence' | 'type',
    comment: string | null
  }>,
  edges: Array<{
    id: string,
    source: number,    // Source node OID
    target: number,    // Target node OID
    type: 'fk' | 'trigger_table' | 'trigger_function' | 'policy' |
          'index' | 'view_dependency' | 'function_table' | 'sequence_owned',
    label: string | null
  }>
}

Node Types Supported

  • Tables (including partitioned tables)
  • Views
  • Materialized views
  • Functions and procedures
  • Triggers
  • RLS policies
  • Indexes
  • Sequences
  • Custom types (enum, composite, domain)

Edge Types Supported

  • fk - Foreign key constraints
  • trigger_table - Trigger attached to table
  • trigger_function - Trigger calls function
  • policy - RLS policy on table
  • index - Index on table
  • view_dependency - View depends on table/view
  • function_table - Function references table
  • sequence_owned - Sequence owned by table column

Testing

Build passes:

npm run build

The SQL queries have been tested against databases with complex schemas including multiple schemas, cross-schema references, triggers, policies, and materialized views.

Future Improvements

  • Add filtering by specific object names
  • Include column-level dependencies
  • Add constraint dependencies beyond FK
  • Performance optimization for very large schemas

Add a new endpoint GET /dependency-graph that returns database object
dependencies for visualization. Queries PostgreSQL system catalogs to
build a complete graph of relationships between:

- Tables (including partitioned tables)
- Views and materialized views
- Functions and procedures
- Triggers
- RLS policies
- Indexes
- Sequences
- Custom types (enum, composite, domain)

Edge types include:
- Foreign key relationships
- Trigger → table and trigger → function
- Policy → table
- Index → table
- View dependencies
- Function → table usage
- Sequence ownership

The endpoint supports filtering by schema and object type.
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