All files / server/explain postgres-parser.ts

81.03% Statements 47/58
71.42% Branches 30/42
100% Functions 2/2
80.7% Lines 46/57

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168                                                35x 35x 35x           35x   35x   85x 85x 9x 9x     76x 76x 5x 5x       71x 71x   69x     69x         69x         69x     69x 15x     69x   35x     34x 34x 22x   34x     69x       35x                 35x                         71x 71x               69x       69x                               69x 69x 71x   71x 71x 71x   71x               71x         69x 69x 1x     69x    
/**
 * PostgreSQL EXPLAIN output parser
 * Parses text format EXPLAIN output and normalizes to common structure
 */
 
import type { ExplainOperation, ExplainResult, ExplainSummary } from '../types/executor'
 
/**
 * Parse PostgreSQL EXPLAIN output (text format)
 *
 * Example input:
 * "Hash Join  (cost=1.09..2.19 rows=1 width=68)"
 * "  Hash Cond: (e.department_id = d.id)"
 * "  ->  Seq Scan on employees e  (cost=0.00..1.05 rows=5 width=44)"
 * "        Filter: (organisation_id = 'org-1'::text)"
 * "  ->  Hash  (cost=1.04..1.04 rows=4 width=36)"
 * "        ->  Seq Scan on departments d  (cost=0.00..1.04 rows=4 width=36)"
 * "Planning Time: 0.123 ms"
 * "Execution Time: 0.456 ms"  (only with ANALYZE)
 */
export function parsePostgresExplain(
  rawOutput: string[],
  sqlQuery: { sql: string; params?: unknown[] }
): ExplainResult {
  const operations: ExplainOperation[] = []
  const usedIndexes: string[] = []
  let hasSequentialScans = false
  let planningTime: number | undefined
  let executionTime: number | undefined
  let totalCost: number | undefined
 
  // Stack for building hierarchical structure based on indentation
  const stack: { indent: number; op: ExplainOperation }[] = []
 
  for (const line of rawOutput) {
    // Check for timing information
    const planningMatch = line.match(/Planning Time:\s*([\d.]+)\s*ms/i)
    if (planningMatch) {
      planningTime = parseFloat(planningMatch[1])
      continue
    }
 
    const executionMatch = line.match(/Execution Time:\s*([\d.]+)\s*ms/i)
    if (executionMatch) {
      executionTime = parseFloat(executionMatch[1])
      continue
    }
 
    // Parse operation lines
    const operation = parsePostgresOperationLine(line)
    if (operation) {
      // Track sequential scans and indexes
      Iif (operation.type.includes('Seq Scan')) {
        hasSequentialScans = true
      }
      Iif (operation.index) {
        usedIndexes.push(operation.index)
      }
 
      // Track total cost (from root operation)
      Iif (operations.length === 0 && operation.estimatedCost !== undefined) {
        totalCost = operation.estimatedCost
      }
 
      // Calculate indentation level
      const indent = line.search(/\S/)
 
      // Pop stack until we find a parent with less indentation
      while (stack.length > 0 && stack[stack.length - 1].indent >= indent) {
        stack.pop()
      }
 
      if (stack.length === 0) {
        // Root level operation
        operations.push(operation)
      } else {
        // Child operation
        const parent = stack[stack.length - 1].op
        if (!parent.children) {
          parent.children = []
        }
        parent.children.push(operation)
      }
 
      stack.push({ indent, op: operation })
    }
  }
 
  const summary: ExplainSummary = {
    database: 'postgres',
    planningTime,
    executionTime,
    totalCost,
    hasSequentialScans,
    usedIndexes: [...new Set(usedIndexes)], // Deduplicate
  }
 
  return {
    operations,
    summary,
    raw: rawOutput.join('\n'),
    sql: sqlQuery,
  }
}
 
/**
 * Parse a single PostgreSQL EXPLAIN line
 */
function parsePostgresOperationLine(line: string): ExplainOperation | null {
  // Skip empty lines and arrow prefixes
  const trimmed = line.replace(/^[\s->]+/, '').trim()
  if (!trimmed) return null
 
  // Match operation pattern: "Operation Type on table (cost=X..Y rows=N width=W)"
  // Examples:
  // "Seq Scan on employees e  (cost=0.00..1.05 rows=5 width=44)"
  // "Index Scan using idx_name on table  (cost=0.00..1.05 rows=5 width=44)"
  // "Hash Join  (cost=1.09..2.19 rows=1 width=68)"
 
  const opMatch = trimmed.match(
    /^([A-Za-z][A-Za-z0-9 ]+?)(?:\s+using\s+(\S+))?(?:\s+on\s+(\S+))?(?:\s+\w+)?(?:\s+\(cost=([\d.]+)\.\.([\d.]+)\s+rows=(\d+)(?:\s+width=\d+)?\))?(?:\s+\(actual time=([\d.]+)\.\.([\d.]+)\s+rows=(\d+)\s+loops=(\d+)\))?/i
  )
 
  Iif (!opMatch) {
    // Check for filter lines
    const filterMatch = trimmed.match(/^Filter:\s*(.+)$/i)
    if (filterMatch) {
      return null // Filters are attached to operations, not standalone
    }
 
    // Check for hash/join condition lines
    const condMatch = trimmed.match(/^(Hash Cond|Join Filter|Index Cond):\s*(.+)$/i)
    if (condMatch) {
      return null // Conditions are attached to operations
    }
 
    return null
  }
 
  const type = opMatch[1].trim()
  const index = opMatch[2] || undefined
  const table = opMatch[3] || undefined
  // opMatch[4] is startCost, opMatch[7] is actualTime - captured but not used currently
  const endCost = opMatch[5] ? parseFloat(opMatch[5]) : undefined
  const estimatedRows = opMatch[6] ? parseInt(opMatch[6], 10) : undefined
  const actualRows = opMatch[9] ? parseInt(opMatch[9], 10) : undefined
 
  const operation: ExplainOperation = {
    type,
    table,
    index,
    estimatedRows,
    estimatedCost: endCost, // Use end cost as the operation cost
  }
 
  Iif (actualRows !== undefined) {
    operation.actualRows = actualRows
  }
 
  // Look for filter in the same line or nearby
  const filterMatch = line.match(/Filter:\s*(.+?)(?:\)|$)/i)
  if (filterMatch) {
    operation.filter = filterMatch[1].trim()
  }
 
  return operation
}