The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
perf(studio): virtualize rendering, memoize nodes/edges, and O(1) FK lookup in Schema Visualizer (#46235)
Charis6dabfde2967b
diff --git a/apps/studio/components/interfaces/Database/Schemas/DefaultEdge.tsx b/apps/studio/components/interfaces/Database/Schemas/DefaultEdge.tsxindex 692a98db907..8c17aac26c7 100644--- a/apps/studio/components/interfaces/Database/Schemas/DefaultEdge.tsx+++ b/apps/studio/components/interfaces/Database/Schemas/DefaultEdge.tsx@@ -8,7 +8,7 @@ import {useReactFlow,} from '@xyflow/react'import { ArrowLeft, ArrowRight } from 'lucide-react'-import { useState } from 'react'+import { memo, useState } from 'react'import { Badge, cn } from 'ui'import { useSchemaGraphContext } from './SchemaGraphContext'@@ -16,7 +16,7 @@ import { EdgeData } from './Schemas.constants'import { useQuerySchemaState } from '@/hooks/misc/useSchemaQueryState'import { useStaticEffectEvent } from '@/hooks/useStaticEffectEvent'-export const DefaultEdge = ({+const DefaultEdgeComponent = ({id,animated,data,@@ -73,6 +73,8 @@ export const DefaultEdge = ({)}+export const DefaultEdge = memo(DefaultEdgeComponent)+const EdgeRelationInfo = ({data,source,diff --git a/apps/studio/components/interfaces/Database/Schemas/SchemaGraph.tsx b/apps/studio/components/interfaces/Database/Schemas/SchemaGraph.tsxindex 450ea08914a..1632d7323a9 100644--- a/apps/studio/components/interfaces/Database/Schemas/SchemaGraph.tsx+++ b/apps/studio/components/interfaces/Database/Schemas/SchemaGraph.tsx@@ -187,14 +187,17 @@ export const SchemaGraph = () => {}const selectedNodeIds = new Set(params.nodes.map((n) => n.id))- reactFlowInstance.setEdges(- reactFlowInstance.getEdges().map((edge) => ({- ...edge,- animated:- selectedNodeIds.size > 0 &&- (selectedNodeIds.has(edge.source) || selectedNodeIds.has(edge.target)),- }))- )+ const currentEdges = reactFlowInstance.getEdges()+ let hasChanges = false+ const nextEdges = currentEdges.map((edge) => {+ const shouldAnimate =+ selectedNodeIds.size > 0 &&+ (selectedNodeIds.has(edge.source) || selectedNodeIds.has(edge.target))+ if (edge.animated === shouldAnimate) return edge+ hasChanges = true+ return { ...edge, animated: shouldAnimate }+ })+ if (hasChanges) reactFlowInstance.setEdges(nextEdges)})@@ -253,16 +256,7 @@ export const SchemaGraph = () => {}})}- }, [- isSuccessTables,- isSuccessSchemas,- tables,- reactFlowInstance,- ref,- resolvedTheme,- schemas,- selectedSchema,- ])+ }, [isSuccessTables, isSuccessSchemas, tables, reactFlowInstance, ref, schemas, selectedSchema])const schemaGraphContext = useMemo<SchemaGraphContextType>(() => ({@@ -470,6 +464,7 @@ export const SchemaGraph = () => {fitViewminZoom={0.8}maxZoom={1.8}+ onlyRenderVisibleElementsproOptions={{ hideAttribution: true }}onNodeDragStop={saveNodePositions}onSelectionChange={handleSelectionChange}diff --git a/apps/studio/components/interfaces/Database/Schemas/SchemaTableNode.tsx b/apps/studio/components/interfaces/Database/Schemas/SchemaTableNode.tsxindex 1d296f1f116..46b0451df17 100644--- a/apps/studio/components/interfaces/Database/Schemas/SchemaTableNode.tsx+++ b/apps/studio/components/interfaces/Database/Schemas/SchemaTableNode.tsx@@ -13,6 +13,7 @@ import {Table2,} from 'lucide-react'import { useRouter } from 'next/router'+import { memo } from 'react'import { toast } from 'sonner'import {Button,@@ -41,13 +42,15 @@ import { formatSql } from '@/lib/formatSql'export const TABLE_NODE_WIDTH = 320export const TABLE_NODE_ROW_HEIGHT = 40-export const TableNode = ({+type TableNodeOwnProps = NodeProps<Node<TableNodeData>> & { placeholder?: boolean }++const TableNodeComponent = ({id,data,targetPosition,sourcePosition,placeholder,-}: NodeProps<Node<TableNodeData>> & { placeholder?: boolean }) => {+}: TableNodeOwnProps) => {// Important styles is a nasty hack to use Handles (required for edges calculations), but do not show them in the UI.// ref: https://github.com/wbkd/react-flow/discussions/2698const hiddenNodeConnector = 'h-px! w-px! min-w-0! min-h-0! cursor-grab! border-0! opacity-0!'@@ -358,3 +361,17 @@ export const TableNode = ({</article>)}++// Custom comparator: xyflow re-renders nodes on selection/drag with new prop+// objects; only the listed props affect rendered output. Selection-driven styling+// (highlighted edges) is read from context inside the component, so we can safely+// ignore xyflow's `selected`/`dragging`/etc. here.+export const TableNode = memo(+ TableNodeComponent,+ (prev, next) =>+ prev.id === next.id &&+ prev.data === next.data &&+ prev.targetPosition === next.targetPosition &&+ prev.sourcePosition === next.sourcePosition &&+ prev.placeholder === next.placeholder+)diff --git a/apps/studio/components/interfaces/Database/Schemas/Schemas.utils.ts b/apps/studio/components/interfaces/Database/Schemas/Schemas.utils.tsindex 7089fae8093..b7a72e7825a 100644--- a/apps/studio/components/interfaces/Database/Schemas/Schemas.utils.ts+++ b/apps/studio/components/interfaces/Database/Schemas/Schemas.utils.ts@@ -66,6 +66,25 @@ export async function getGraphDataFromTables('id')+ // Precompute name → { tableId, columnsByName } lookup so each relationship+ // resolves its source/target handles in O(1) instead of scanning every table+column.+ const tablesByName = new Map<string, { tableId: number; columnsByName: Map<string, string> }>()+ for (const table of tables) {+ const columnsByName = new Map<string, string>()+ for (const column of table.columns || []) {+ columnsByName.set(column.name, column.id)+ }+ tablesByName.set(table.name, { tableId: table.id, columnsByName })+ }++ const findHandleIds = (tableName: string, columnName: string): [string?, string?] => {+ const entry = tablesByName.get(tableName)+ if (!entry) return []+ const columnId = entry.columnsByName.get(columnName)+ if (columnId === undefined) return []+ return [String(entry.tableId), columnId]+ }+for (const rel of uniqueRelationships) {// TODO: Support [external->this] relationship?if (rel.source_schema !== currentSchema) {@@ -96,11 +115,7 @@ export async function getGraphDataFromTables(})}- const [source, sourceHandle] = findTablesHandleIds(- tables,- rel.source_table_name,- rel.source_column_name- )+ const [source, sourceHandle] = findHandleIds(rel.source_table_name, rel.source_column_name)if (source) {edges.push({@@ -124,16 +139,8 @@ export async function getGraphDataFromTables(continue}- const [source, sourceHandle] = findTablesHandleIds(- tables,- rel.source_table_name,- rel.source_column_name- )- const [target, targetHandle] = findTablesHandleIds(- tables,- rel.target_table_name,- rel.target_column_name- )+ const [source, sourceHandle] = findHandleIds(rel.source_table_name, rel.source_column_name)+ const [target, targetHandle] = findHandleIds(rel.target_table_name, rel.target_column_name)// We do not support [external->this] flow currently.if (source && target) {@@ -165,24 +172,6 @@ export async function getGraphDataFromTables(: getLayoutedElementsViaDagre(nodes, edges)}-function findTablesHandleIds(- tables: PGTable[],- table_name: string,- column_name: string-): [string?, string?] {- for (const table of tables) {- if (table_name !== table.name) continue-- for (const column of table.columns || []) {- if (column_name !== column.name) continue-- return [String(table.id), column.id]- }- }-- return []-}-export const getLayoutedElementsViaDagre = (nodes: Node<TableNodeData>[], edges: Edge[]) => {const dagreGraph = new dagre.graphlib.Graph()dagreGraph.setDefaultEdgeLabel(() => ({}))