Skip to content

Commit

Permalink
Fix primary keys retrieval query (#198)
Browse files Browse the repository at this point in the history
The previous query had some issues when used under clusters with many
schema objects, this one improves query performance
  • Loading branch information
exekias committed Nov 3, 2023
1 parent c029d5e commit 954746b
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions pkg/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,20 +143,20 @@ BEGIN
) c
),
'primaryKey', (
SELECT json_agg(kcu.column_name) AS primary_key_columns
FROM information_schema.table_constraints AS tc
JOIN information_schema.key_column_usage AS kcu
ON tc.constraint_name = kcu.constraint_name
WHERE tc.table_name = t.relname
AND tc.table_schema = schemaname
AND tc.constraint_type = 'PRIMARY KEY'
SELECT json_agg(pg_attribute.attname) AS primary_key_columns
FROM pg_index, pg_attribute
WHERE
indrelid = t.oid AND
nspname = schemaname AND
pg_attribute.attrelid = t.oid AND
pg_attribute.attnum = any(pg_index.indkey)
AND indisprimary
),
'indexes', (
SELECT json_object_agg(pi.indexrelid::regclass, json_build_object(
'name', pi.indexrelid::regclass
))
FROM pg_index pi
INNER JOIN pg_class pgc ON pi.indexrelid = pgc.oid
WHERE pi.indrelid = t.oid::regclass
)
)) FROM pg_class AS t
Expand Down

0 comments on commit 954746b

Please sign in to comment.