Skip to content

Commit 73cdf2d

Browse files
fix(pg-meta): include schema in pg_indexes join to avoid cross-schema index collisions (supabase#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. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## 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. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: Charis Lam <26616127+charislam@users.noreply.github.com>
1 parent 29820d8 commit 73cdf2d

2 files changed

Lines changed: 33 additions & 1 deletion

File tree

packages/pg-meta/src/sql/indexes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export const INDEXES_SQL = /* SQL */ safeSql`
3939
JOIN pg_namespace n ON c.relnamespace = n.oid
4040
JOIN pg_am am ON c.relam = am.oid
4141
JOIN pg_attribute a ON a.attrelid = c.oid AND a.attnum = ANY(idx.indkey)
42-
JOIN pg_indexes ix ON c.relname = ix.indexname
42+
JOIN pg_indexes ix ON c.relname = ix.indexname AND n.nspname = ix.schemaname
4343
GROUP BY
4444
idx.indexrelid, idx.indrelid, n.nspname, idx.indnatts, idx.indnkeyatts, idx.indisunique,
4545
idx.indisprimary, idx.indisexclusion, idx.indimmediate, idx.indisclustered, idx.indisvalid,

packages/pg-meta/test/indexes.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,3 +166,35 @@ withTestDatabase('list with filters', async ({ executeQuery }) => {
166166
const offset = offsetZod.parse(await executeQuery(offsetSql))
167167
expect(offset).toHaveLength(withoutSystem.length - 1)
168168
})
169+
170+
withTestDatabase('handles same index name across schemas correctly', async ({ executeQuery }) => {
171+
// Create two schemas
172+
await executeQuery(`CREATE SCHEMA schema_a;`)
173+
await executeQuery(`CREATE SCHEMA schema_b;`)
174+
175+
// Create tables
176+
await executeQuery(`CREATE TABLE schema_a.test (id int);`)
177+
await executeQuery(`CREATE TABLE schema_b.test (id int);`)
178+
179+
// Create SAME index name in both schemas
180+
await executeQuery(`CREATE INDEX idx_test ON schema_a.test (id);`)
181+
await executeQuery(`CREATE INDEX idx_test ON schema_b.test (id);`)
182+
183+
// Fetch indexes
184+
const { sql, zod } = await pgMeta.indexes.list({ includeSystemSchemas: true })
185+
const indexes = zod.parse(await executeQuery(sql))
186+
187+
const schemaAIndex = indexes.find(
188+
(i) => i.schema === 'schema_a' && i.index_definition.includes('schema_a.test')
189+
)
190+
191+
const schemaBIndex = indexes.find(
192+
(i) => i.schema === 'schema_b' && i.index_definition.includes('schema_b.test')
193+
)
194+
195+
expect(schemaAIndex).toBeDefined()
196+
expect(schemaBIndex).toBeDefined()
197+
198+
expect(schemaAIndex!.index_definition).toContain('schema_a.test')
199+
expect(schemaBIndex!.index_definition).toContain('schema_b.test')
200+
})

0 commit comments

Comments
 (0)