| 556 | // The plans array contains the EXPLAIN output |
| 557 | // Convert it to a readable text format |
| 558 | const formatPlanNode = (node, indent = 0) => { |
| 559 | if (!node) return ''; |
| 560 | const prefix = ' '.repeat(indent); |
| 561 | let result = ''; |
| 562 | |
| 563 | // Format the node type and basic info |
| 564 | const nodeType = node['Node Type'] || ''; |
| 565 | const relationship = node['Parent Relationship'] ? ` (${node['Parent Relationship']})` : ''; |
| 566 | |
| 567 | let nodeInfo = `${prefix}-> ${nodeType}${relationship}`; |
| 568 | |
| 569 | // Add key metrics |
| 570 | const metrics = []; |
| 571 | if (node['Relation Name']) metrics.push(`on ${node['Relation Name']}`); |
| 572 | if (node['Index Name']) metrics.push(`using ${node['Index Name']}`); |
| 573 | if (node['Join Type']) metrics.push(`${node['Join Type']} Join`); |
| 574 | if (node['Hash Cond']) metrics.push(`Hash Cond: ${node['Hash Cond']}`); |
| 575 | if (node['Index Cond']) metrics.push(`Index Cond: ${node['Index Cond']}`); |
| 576 | if (node['Filter']) metrics.push(`Filter: ${node['Filter']}`); |
| 577 | |
| 578 | if (metrics.length > 0) { |
| 579 | nodeInfo += ` ${metrics.join(', ')}`; |
| 580 | } |
| 581 | |
| 582 | result += nodeInfo + '\n'; |
| 583 | |
| 584 | // Add cost and row info |
| 585 | const costInfo = []; |
| 586 | if (node['Startup Cost'] !== undefined) costInfo.push(`cost=${node['Startup Cost']}..${node['Total Cost']}`); |
| 587 | if (node['Plan Rows'] !== undefined) costInfo.push(`rows=${node['Plan Rows']}`); |
| 588 | if (node['Plan Width'] !== undefined) costInfo.push(`width=${node['Plan Width']}`); |
| 589 | |
| 590 | if (costInfo.length > 0) { |
| 591 | result += `${prefix} (${costInfo.join(' ')})\n`; |
| 592 | } |
| 593 | |
| 594 | // Add actual metrics if available (from EXPLAIN ANALYZE) |
| 595 | const actualInfo = []; |
| 596 | if (node['Actual Startup Time'] !== undefined) actualInfo.push(`actual time=${node['Actual Startup Time']}..${node['Actual Total Time']}`); |
| 597 | if (node['Actual Rows'] !== undefined) actualInfo.push(`rows=${node['Actual Rows']}`); |
| 598 | if (node['Actual Loops'] !== undefined) actualInfo.push(`loops=${node['Actual Loops']}`); |
| 599 | |
| 600 | if (actualInfo.length > 0) { |
| 601 | result += `${prefix} (${actualInfo.join(' ')})\n`; |
| 602 | } |
| 603 | |
| 604 | // Recursively format child plans |
| 605 | if (node['Plans'] && Array.isArray(node['Plans'])) { |
| 606 | for (const child of node['Plans']) { |
| 607 | result += formatPlanNode(child, indent + 1); |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | return result; |
| 612 | }; |
| 613 | |
| 614 | // Format each plan in the array |
| 615 | return plans.map((plan, idx) => { |