Finding foreign keys that are missing indexes - Mailing list pgsql-general

From plu tard
Subject Finding foreign keys that are missing indexes
Date
Msg-id BLU115-W30DAE79E6FD8AB13D1E970A6E90@phx.gbl
Whole thread Raw
Responses Re: Finding foreign keys that are missing indexes  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-general
I'm aware that if you create a foreign key constraint, no indexes are automatically created.

I would like to find a way to programatically inspect all my foreign keys and identify possibly missing indexes on either table (either the table defining the constraint or the table being referenced).

I wasn't able to find anything searching Google or the pg archives.

Attached is a first attempt. Just run the missing-fk-indexes.sql through psql. e.g.,

  psql -q mydb -f missing-fk-indexes.sql

I know the output can be improved, but is this headed toward the right direction and/or is there already a simpler way to accomplish this?

Briefly, it finds all the unique tables/columns referenced by foreign keys. Then it examines all the indexes, looking for any that are a prefix of the fk columns. It writes out any tables/columns where no indexes are found, followed by a list of the fk's that reference those tables/columns.

Also attached is a trivial test schema to run it against.



Life on your PC is safer, easier, and more enjoyable with Windows Vista®. See how
Attachment

pgsql-general by date:

Previous
From: Tom Lane
Date:
Subject: Re: Weird query sort
Next
From: "Jais Mathew"
Date:
Subject: having two database clusters?