-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathduplicated_foreign_keys.sql
53 lines (50 loc) · 1.81 KB
/
duplicated_foreign_keys.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
* Copyright (c) 2019-2025. Ivan Vakhrushev and others.
* https://github.com/mfvanek/pg-index-health-sql
*
* Licensed under the Apache License 2.0
*/
-- Finds completely identical foreign keys
--
-- Based on query from https://habr.com/ru/articles/803841/
with
fk_with_attributes as (
select
c.conname as constraint_name,
c.conrelid as table_oid,
c.confrelid as foreign_table_oid,
u.attposition,
col.attname,
col.attnotnull
from
pg_catalog.pg_constraint c
inner join pg_catalog.pg_namespace nsp on nsp.oid = c.connamespace
inner join lateral unnest(c.conkey) with ordinality u(attnum, attposition) on true
inner join pg_catalog.pg_attribute col on col.attrelid = c.conrelid and col.attnum = u.attnum
where
c.contype = 'f' and
nsp.nspname = :schema_name_param::text
),
fk_with_attributes_grouped as (
select
constraint_name,
table_oid,
foreign_table_oid,
array_agg(attname::text || ',' || attnotnull::text order by attposition) as columns
from fk_with_attributes
group by constraint_name, table_oid, foreign_table_oid
)
select
c1.table_oid::regclass::text as table_name,
c1.constraint_name,
c1.columns,
c2.constraint_name as duplicate_constraint_name,
c2.columns as duplicate_constraint_columns
from
fk_with_attributes_grouped c1
inner join fk_with_attributes_grouped c2
on c2.constraint_name > c1.constraint_name and /* to prevent duplicated rows in output */
c2.table_oid = c1.table_oid and
c2.foreign_table_oid = c1.foreign_table_oid and
c2.columns = c1.columns
order by table_name, c1.constraint_name, c2.constraint_name;