feat: add schema comments and materialized view support - #160
stephanhaeuslschmid wants to merge 3 commits into
Conversation
…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
left a comment
There was a problem hiding this comment.
Review — concise
Verdict: Approve with comments. Solid feature addition — materialized view support and schema comments are both useful.
Strengths
- Materialized view support in
list_objectsandget_object_details. Usespg_class/pg_attributefor matview columns (correct — they're not ininformation_schema.columns). - Schema comments via
obj_description()andcol_description()added to both listing and detail views. obj_description,col_description,shobj_descriptionadded toSafeSqlDriverallowlist. 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.
Summary
list_objectsandget_object_detailsnow include PostgreSQLCOMMENT ONmetadata viaobj_description()andcol_description()frompg_catalogobject_type='materialized_view'for both tools, queryingpg_class(since materialized views are not ininformation_schema)Motivation
LLMs using this MCP server to explore databases cannot currently see schema documentation stored as
COMMENT ON TABLE/VIEW/COLUMNstatements — 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.tablesdoes not include them.Changes
list_objects:obj_description()ascommentfieldobject_type='materialized_view'queriespg_class WHERE relkind = 'm'with commentsget_object_details:col_description()ascommentfieldbasicsection viaobj_description()pg_attribute(notinformation_schema.columns)Example
These comments now appear in
list_objectsandget_object_detailsresponses, giving LLMs the context they need to write correct queries.Closes #71