From 73cdf2ddd19e8504e0f08feac577254ba0f79a00 Mon Sep 17 00:00:00 2001 From: Akash Santra Date: Mon, 4 May 2026 21:22:40 +0530 Subject: [PATCH] fix(pg-meta): include schema in pg_indexes join to avoid cross-schema index collisions (#45374) ## I have read the CONTRIBUTING.md file. YES ## What kind of change does this PR introduce? Bug fix ## What is the current behavior? The query in `sql/indexes.ts` joins `pg_class` with `pg_indexes` using only the index name: ```sql JOIN pg_indexes ix ON c.relname = ix.indexname ``` This can lead to incorrect results when multiple schemas contain indexes with the same name. PostgreSQL allows identical index names across different schemas, so this join may return the wrong index_definition. ## What is the new behavior? The join condition now includes the schema name: ```sql JOIN pg_indexes ix ON c.relname = ix.indexname AND n.nspname = ix.schemaname ``` This ensures the correct index_definition is retrieved for each index, even when duplicate index names exist across schemas. ## Additional context - Added a test case to verify behavior when the same index name exists in multiple schemas. - This change prevents cross-schema collisions and ensures accurate index metadata retrieval. ## Summary by CodeRabbit * **Bug Fixes** * Improved index listing so indexes that share the same name in different schemas are correctly distinguished and reported. * **Tests** * Added a regression test ensuring indexes with identical names in separate schemas are both detected and contain expected schema-qualified references. --------- Co-authored-by: Charis Lam <26616127+charislam@users.noreply.github.com> --- packages/pg-meta/src/sql/indexes.ts | 2 +- packages/pg-meta/test/indexes.test.ts | 32 +++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/packages/pg-meta/src/sql/indexes.ts b/packages/pg-meta/src/sql/indexes.ts index db66dc9ab25..84705e97159 100644 --- a/packages/pg-meta/src/sql/indexes.ts +++ b/packages/pg-meta/src/sql/indexes.ts @@ -39,7 +39,7 @@ export const INDEXES_SQL = /* SQL */ safeSql` JOIN pg_namespace n ON c.relnamespace = n.oid JOIN pg_am am ON c.relam = am.oid JOIN pg_attribute a ON a.attrelid = c.oid AND a.attnum = ANY(idx.indkey) - JOIN pg_indexes ix ON c.relname = ix.indexname + JOIN pg_indexes ix ON c.relname = ix.indexname AND n.nspname = ix.schemaname GROUP BY idx.indexrelid, idx.indrelid, n.nspname, idx.indnatts, idx.indnkeyatts, idx.indisunique, idx.indisprimary, idx.indisexclusion, idx.indimmediate, idx.indisclustered, idx.indisvalid, diff --git a/packages/pg-meta/test/indexes.test.ts b/packages/pg-meta/test/indexes.test.ts index be1e02425dd..88a16c51995 100644 --- a/packages/pg-meta/test/indexes.test.ts +++ b/packages/pg-meta/test/indexes.test.ts @@ -166,3 +166,35 @@ withTestDatabase('list with filters', async ({ executeQuery }) => { const offset = offsetZod.parse(await executeQuery(offsetSql)) expect(offset).toHaveLength(withoutSystem.length - 1) }) + +withTestDatabase('handles same index name across schemas correctly', async ({ executeQuery }) => { + // Create two schemas + await executeQuery(`CREATE SCHEMA schema_a;`) + await executeQuery(`CREATE SCHEMA schema_b;`) + + // Create tables + await executeQuery(`CREATE TABLE schema_a.test (id int);`) + await executeQuery(`CREATE TABLE schema_b.test (id int);`) + + // Create SAME index name in both schemas + await executeQuery(`CREATE INDEX idx_test ON schema_a.test (id);`) + await executeQuery(`CREATE INDEX idx_test ON schema_b.test (id);`) + + // Fetch indexes + const { sql, zod } = await pgMeta.indexes.list({ includeSystemSchemas: true }) + const indexes = zod.parse(await executeQuery(sql)) + + const schemaAIndex = indexes.find( + (i) => i.schema === 'schema_a' && i.index_definition.includes('schema_a.test') + ) + + const schemaBIndex = indexes.find( + (i) => i.schema === 'schema_b' && i.index_definition.includes('schema_b.test') + ) + + expect(schemaAIndex).toBeDefined() + expect(schemaBIndex).toBeDefined() + + expect(schemaAIndex!.index_definition).toContain('schema_a.test') + expect(schemaBIndex!.index_definition).toContain('schema_b.test') +})