Skip to content

feat: add schema comments and materialized view support - #160

Open
stephanhaeuslschmid wants to merge 3 commits into
crystaldba:mainfrom
stephanhaeuslschmid:feat/schema-comments-and-matviews
Open

stephanhaeuslschmid wants to merge 3 commits into
crystaldba:mainfrom
stephanhaeuslschmid:feat/schema-comments-and-matviews

Conversation

@stephanhaeuslschmid

Copy link
Copy Markdown

Summary

  • Schema comments: list_objects and get_object_details now include PostgreSQL COMMENT ON metadata via obj_description() and col_description() from pg_catalog
  • Materialized views: New object_type='materialized_view' for both tools, querying pg_class (since materialized views are not in information_schema)

Motivation

LLMs using this MCP server to explore databases cannot currently see schema documentation stored as COMMENT ON TABLE/VIEW/COLUMN statements — the standard PostgreSQL mechanism for documenting columns (units, enums, business logic). This makes it much harder for LLMs to correctly interpret data.

Similarly, materialized views (commonly used for analytics/reporting layers) are invisible because information_schema.tables does not include them.

Changes

list_objects:

  • Tables and views now include obj_description() as comment field
  • New object_type='materialized_view' queries pg_class WHERE relkind = 'm' with comments

get_object_details:

  • Column results now include col_description() as comment field
  • Object-level comment added to basic section via obj_description()
  • For materialized views, columns are queried from pg_attribute (not information_schema.columns)

Example

COMMENT ON TABLE raw.orders IS 'Customer orders. One row per order. Currency: EUR.';
COMMENT ON COLUMN raw.orders.amount IS 'Order total in EUR cents. Divide by 100 for EUR.';

These comments now appear in list_objects and get_object_details responses, giving LLMs the context they need to write correct queries.

Closes #71

…ools

Add PostgreSQL COMMENT support to list_objects and get_object_details:
- list_objects: include obj_description() for tables, views, and materialized views
- get_object_details: include col_description() for column comments and
  obj_description() for table/view/materialized view comments

Add materialized view as a new object_type:
- list_objects: query pg_class with relkind='m' for materialized views
- get_object_details: query pg_attribute for columns (since materialized
  views are not in information_schema.columns)

This enables LLMs using the MCP server to discover schema documentation
stored as COMMENT ON TABLE/VIEW/COLUMN statements, which is the standard
PostgreSQL mechanism for schema documentation.

Closes crystaldba#71
These pg_catalog functions are read-only and needed for the schema
comment feature to work in restricted mode.
Add "PostgreSQL" keyword to all tool descriptions so LLM search
can find them. Make get_object_details and list_objects descriptions
explicit about returned data (columns, comments, constraints, indexes).

@jssmith jssmith left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Review — concise

Verdict: Approve with comments. Solid feature addition — materialized view support and schema comments are both useful.

Strengths

  • Materialized view support in list_objects and get_object_details. Uses pg_class/pg_attribute for matview columns (correct — they're not in information_schema.columns).
  • Schema comments via obj_description() and col_description() added to both listing and detail views.
  • obj_description, col_description, shobj_description added to SafeSqlDriver allowlist. Correct.
  • Tool descriptions improved with better LLM-guidance language.

Issues to address

1. quote_ident in (quote_ident(...) || '.' || quote_ident(...))::regclass may fail on non-existent relations (non-blocking)
In list_objects, the obj_description call casts the schema-qualified name to regclass. If a table is dropped between the information_schema.tables query and the obj_description evaluation (unlikely but possible in concurrent environments), this will raise an error. Consider using pg_class.oid directly via a JOIN instead of the ::regclass cast.

2. Materialized view table_type value inconsistency (non-blocking)
list_objects returns "MATERIALIZED VIEW" for matviews but information_schema.tables returns "BASE TABLE" or "VIEW". The list_objects filter maps object_type to table_type values — verify the mapping for materialized_view is handled in the table_type parameter value. Looking at the code, the matview branch is separate (uses pg_class directly), which is correct, but the returned "type": "MATERIALIZED VIEW" doesn't match information_schema conventions. Document or standardize.

3. No tests for materialized view queries (non-blocking)
The diff adds no tests. Consider adding unit tests that mock the SQL driver and verify the correct queries are executed for object_type="materialized_view" in both list_objects and get_object_details.

This review was created by an AI agent (OpenHands) on behalf of @jssmith.

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.

Addition of Table/Column Comments to metadata tools?

2 participants