Skip to content

Conversation

@jssmith
Copy link
Contributor

@jssmith jssmith commented Jan 20, 2026

Summary

Follow-up to #132: Refactors version-dependent column logic into a reusable helper function.

What Changed

1. Added _get_pg_stat_statements_columns() helper

Extracts duplicated version logic from both get_top_queries_by_time() and get_top_resource_queries() into a single helper function.

Before: Version branching duplicated in 2 places
After: Single source of truth

Returns a PgStatStatementsColumns dataclass with all version-appropriate column names.

2. Added comprehensive documentation

def _get_pg_stat_statements_columns(pg_version: int) -> PgStatStatementsColumns:
    """Get pg_stat_statements column names based on PostgreSQL version.

    PostgreSQL 13 introduced pg_stat_statements v2.0 with breaking changes:
    - Renamed timing columns: *_time*_exec_time (total_timetotal_exec_time, etc.)
    - Added wal_bytes column for write-ahead log tracking

    This function provides version-appropriate column names to ensure compatibility
    with both old (PG12) and new (PG13) versions.

    See: https://www.postgresql.org/docs/13/release-13.html#id-1.11.6.11.4
    ...

Benefits

DRY - Eliminates duplication
Maintainable - One place to update if future PG versions change
Testable - Helper can be unit tested independently
Type-safe - Dataclass prevents typos in column names
Self-documenting - Clear structure shows version differences

Testing

  • ✅ All existing unit tests pass (8/8)
  • ✅ No behavior changes - pure refactor
  • ✅ Lint and type checks pass

Files Changed

  • src/postgres_mcp/top_queries/top_queries_calc.py - Extracted helper function

🤖 Generated with Claude Code

Improvements:
1. Add _get_pg_stat_statements_columns() helper function
   - Eliminates duplication between get_top_queries_by_time and get_top_resource_queries
   - Single source of truth for version-dependent column names
   - Returns PgStatStatementsColumns dataclass for type safety

2. Add comprehensive documentation
   - Documents PostgreSQL 13 breaking changes (v2.0 of pg_stat_statements)
   - Links to official PG13 release notes
   - Explains why version branching is necessary

Benefits:
- More maintainable: one place to update if future PG versions change
- More testable: helper can be unit tested independently
- Self-documenting: clear dataclass shows all version differences
- Type-safe: dataclass prevents typos in column names

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@jssmith jssmith merged commit d03cf9f into main Jan 20, 2026
1 check passed
@jssmith jssmith deleted the refactor/pg-stat-statements-helper branch January 20, 2026 00:49
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