Created
May 10, 2018 17:15
-
-
Save cellis/36bb9ebbee45e604b50ef5b929286d05 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe('database', () => { | |
describe('performance', () => { | |
it('has indexes on all foreign keys', async () => { | |
const query = ` | |
WITH indexed_tables AS ( | |
select | |
ns.nspname, | |
t.relname as table_name, | |
i.relname as index_name, | |
array_to_string(array_agg(a.attname), ', ') as column_names, | |
ix.indrelid, | |
ix.indkey | |
FROM pg_class i | |
JOIN pg_index ix ON i.OID = ix.indrelid | |
JOIN pg_class t ON ix.indrelid = t.oid | |
JOIN pg_namespace ns ON ns.oid = t.relnamespace | |
JOIN pg_attribute a ON a.attrelid = t.oid | |
where | |
a.attnum = ANY(ix.indkey) | |
and t.relkind = 'r' | |
and nspname in ('your-namespace-1','your-ns-2') | |
group by | |
ns.nspname, | |
t.relname, | |
i.relname, | |
ix.indrelid, | |
ix.indkey | |
order by | |
ns.nspname, | |
t.relname, | |
i.relname, | |
ix.indrelid, | |
ix.indkey | |
) SELECT conrelid::regclass | |
,conname | |
,reltuples::bigint | |
FROM pg_constraint pgc | |
JOIN pg_class ON (conrelid = pg_class.oid) | |
WHERE contype = 'f' | |
AND NOT EXISTS( | |
SELECT 1 FROM indexed_tables WHERE indrelid = conrelid | |
AND public.sortarray(conkey) = public.sortarray(indkey) | |
OR (array_length(indkey, 1) > 1 AND indkey::smallint[] @> conkey) | |
) | |
ORDER BY reltuples DESC; | |
`; | |
const results = await db.query(query); | |
const { rows } = results; | |
expect(rows.length).toBe(0); | |
}); | |
}); | |
}); | |
find-missing-fks.js
And it still found no fucks to give...
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @cellis
Can you provide the PG version of your database that this script works? I think the public.sortarray is a custom function because this doesn't work on PG 9.6.x.
Thanks!