Skip to content

Conversation

@shitalm
Copy link
Contributor

@shitalm shitalm commented Nov 3, 2025

Problem

The get_database_schema resource was failing with query validation errors:

  1. SQL query used ::regclass which only works for tables/materialized views, not regular views
  2. obj_description function was not in SafeSqlDriver's allowed functions list, causing validation failures

Solution

  • Updated SQL query in database_resources.py to use pg_class directly via LEFT JOINs instead of ::regclass
    • Works for all table types: tables, views, and materialized views
    • Uses proper schema matching with pg_namespace and pg_class
  • Added obj_description, col_description, and shobj_description to SafeSqlDriver allowed functions
    • These are safe read-only functions needed for retrieving object comments
    • Required for the schema resource to work correctly

Changes

  • src/postgres_mcp/resources/database_resources.py: Fixed SQL query to handle all table types
  • src/postgres_mcp/sql/safe_sql.py: Added description functions to allowed functions list

Testing

  • get_database_schema resource now works correctly for databases with tables and views
  • No more 'Error validating query' messages when accessing database://schema resource

- Changed runtime image from python:3.13-slim-bookworm to python:3.12-slim-bookworm
- Matches builder stage Python version (python3.12)
- Fixes ModuleNotFoundError when running Docker containers due to venv incompatibility
- Fixed SQL query in database_resources.py to use pg_class directly instead of ::regclass
  - This avoids errors when querying views (::regclass only works for tables/materialized views)
  - Changed to LEFT JOIN with pg_namespace and pg_class for compatibility
- Added obj_description, col_description, and shobj_description to SafeSqlDriver allowed functions
  - These functions are needed for retrieving table/object comments in schema queries
  - Fixes 'Error validating query' when get_database_schema resource is accessed

Resolves query validation errors that prevented the database://schema resource from working correctly.
@neverinfamous neverinfamous merged commit e9948c7 into neverinfamous:main Nov 3, 2025
3 checks passed
@neverinfamous
Copy link
Owner

Thank you so much for this excellent contribution, @shitalm! 🎉

This fix addresses a real issue that was causing the database://schema resource to fail when databases contain views. Your solution is clean and well-implemented:

Smart Query Refactoring: Using pg_class with LEFT JOINs instead of ::regclass ensures compatibility with all table types (tables, views, and materialized views)

Security-Conscious: Adding the description functions (obj_description, col_description, shobj_description) to the SafeSqlDriver allowed list maintains security while enabling proper functionality

Well-Documented: Clear explanation of the problem and solution in your PR description

This is exactly the kind of quality contribution that makes open source great. The fix has been merged and will be included in the next release.

Thanks again for taking the time to identify and fix this issue! 🙏

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.

2 participants