Skip to main content
Glama
sbfulfil

PostgreSQL MCP Server

by sbfulfil

get_indexes

Retrieve index information for a PostgreSQL table to analyze database performance and structure.

Instructions

Get indexes for a specific table

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
table_nameYesName of the table
schemaNoSchema name (default: public)public

Implementation Reference

  • The main handler function for the 'get_indexes' tool. It connects to the PostgreSQL database, executes a query to retrieve indexes for the specified table and schema from pg_indexes, formats the results into a text response including index names, types, and definitions, and handles the case where no indexes are found.
    async getIndexes(tableName, schema = 'public') {
      const client = await this.connectToDatabase();
      
      try {
        const query = `
          SELECT 
            i.indexname,
            i.indexdef,
            am.amname AS index_type
          FROM pg_indexes i
          JOIN pg_class c ON c.relname = i.indexname
          JOIN pg_am am ON am.oid = c.relam
          WHERE i.schemaname = $1 AND i.tablename = $2
          ORDER BY i.indexname;
        `;
        
        const result = await client.query(query, [schema, tableName]);
        
        return {
          content: [
            {
              type: 'text',
              text: result.rows.length > 0 ? 
                `Indexes for table "${schema}.${tableName}":\n\n` + 
                result.rows.map(row => 
                  `${row.indexname} (${row.index_type}):\n  ${row.indexdef}`
                ).join('\n\n') :
                `No indexes found for table "${schema}.${tableName}"`
            },
          ],
        };
      } finally {
        await client.end();
      }
    }
  • Input schema validation for the 'get_indexes' tool, defining properties for table_name (required string) and schema (optional string with default 'public').
    inputSchema: {
      type: 'object',
      properties: {
        table_name: {
          type: 'string',
          description: 'Name of the table',
        },
        schema: {
          type: 'string',
          description: 'Schema name (default: public)',
          default: 'public'
        }
      },
      required: ['table_name'],
    },
  • src/index.js:117-135 (registration)
    Tool registration in the ListTools response, defining the name 'get_indexes', description, and input schema.
    {
      name: 'get_indexes',
      description: 'Get indexes for a specific table',
      inputSchema: {
        type: 'object',
        properties: {
          table_name: {
            type: 'string',
            description: 'Name of the table',
          },
          schema: {
            type: 'string',
            description: 'Schema name (default: public)',
            default: 'public'
          }
        },
        required: ['table_name'],
      },
    },

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/sbfulfil/pg-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server