Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 818997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T02:14:12+00:00 2026-05-15T02:14:12+00:00

I am working with a large PostgreSQL database, and I am trying to tune

  • 0

I am working with a large PostgreSQL database, and I am trying to tune it to get more performance.

Our queries and updates seem to be doing a lot of lookups using foreign keys.

What I would like is a relatively simple way to add Indexes to all of our foreign keys without having to go through every table (~140) and doing it manually.

In researching this, I’ve come to find that there is no way to have Postgres do this for you automatically (like MySQL does), but I would be happy to hear otherwise there, too.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T02:14:13+00:00Added an answer on May 15, 2026 at 2:14 am

    EDIT: so, I wrote the query below and then thought… “hang on, Postgresql requires that foreign key targets have to have unique indices.” So I guess I misunderstood what you meant? You can use the below query to check that the source of your foreign keys have indices by substituing “conrelid” for “confrelid” and “conkey” for “confkey” (yeah, yeah, no aliases in the query…)

    Well, I guess it should be possible to go through the system catalogues… As usual, the best guide to the system catalogues is to use psql and do “\set ECHO_HIDDEN 1” and then see what SQL it generates for interesting “\d” commands. Here’s the SQL used to find the foreign keys for a table (“\d tablename”) :

    -- $1 is the table OID, e.g. 'tablename'::regclass
    SELECT conname, conrelid::pg_catalog.regclass,
      pg_catalog.pg_get_constraintdef(c.oid, true) as condef
    FROM pg_catalog.pg_constraint c
    WHERE c.confrelid = $1 AND c.contype = 'f' ORDER BY 1;
    

    Seems that pg_constraint has columns conkey and confkey that look like they could be the column numbers that the key is defined across. Probably confkey is the column numbers in the foreign table since it’s only non-null for foreign keys. Also, took me a while to realise this is the SQL to show foreign keys referencing the given table. Which is what we want anyway.

    So something this query shows the data beginning to take shape:

    select confrelid, conname, column_index, attname
    from pg_attribute
         join (select confrelid::regclass, conname, unnest(confkey) as column_index
               from pg_constraint
               where confrelid = 'ticket_status'::regclass) fkey
              on fkey.confrelid = pg_attribute.attrelid
                 and fkey.column_index = pg_attribute.attnum
    

    I’m going to be using 8.4 features like unnest… you might be able to get along without.

    I ended up with:

    select pg_index.indexrelid::regclass, 'create index ' || relname || '_' ||
             array_to_string(column_name_list, '_') || '_idx on ' || confrelid ||
             ' (' || array_to_string(column_name_list, ',') || ')'
    from (select distinct
           confrelid,
           array_agg(attname) column_name_list,
           array_agg(attnum) as column_list
         from pg_attribute
              join (select confrelid::regclass,
                     conname,
                     unnest(confkey) as column_index
                    from (select distinct
                            confrelid, conname, confkey
                          from pg_constraint
                            join pg_class on pg_class.oid = pg_constraint.confrelid
                            join pg_namespace on pg_namespace.oid = pg_class.relnamespace
                          where nspname !~ '^pg_' and nspname <> 'information_schema'
                          ) fkey
                   ) fkey
                   on fkey.confrelid = pg_attribute.attrelid
                      and fkey.column_index = pg_attribute.attnum
         group by confrelid, conname
         ) candidate_index
    join pg_class on pg_class.oid = candidate_index.confrelid
    left join pg_index on pg_index.indrelid = confrelid
                          and indkey::text = array_to_string(column_list, ' ')
    

    OK, this monstrosity prints out the candidate index commands and tries to match them up with existing indices. So you can simply add “where indexrelid is null” on the end to get the commands to create indices that don’t seem to exist.

    This query doesn’t deal with multi-column foreign keys very well; but imho if you’re using those, you deserve trouble.

    LATER EDIT: here’s the query with the proposed edits up at the top put in. So this shows the commands to create indices that don’t exist, on columns that are the source of a foreign key (not its target).

    select pg_index.indexrelid::regclass, 'create index ' || relname || '_' ||
             array_to_string(column_name_list, '_') || '_idx on ' || conrelid ||
             ' (' || array_to_string(column_name_list, ',') || ')'
    from (select distinct
           conrelid,
           array_agg(attname) column_name_list,
           array_agg(attnum) as column_list
         from pg_attribute
              join (select conrelid::regclass,
                     conname,
                     unnest(conkey) as column_index
                    from (select distinct
                            conrelid, conname, conkey
                          from pg_constraint
                            join pg_class on pg_class.oid = pg_constraint.conrelid
                            join pg_namespace on pg_namespace.oid = pg_class.relnamespace
                          where nspname !~ '^pg_' and nspname <> 'information_schema'
                          ) fkey
                   ) fkey
                   on fkey.conrelid = pg_attribute.attrelid
                      and fkey.column_index = pg_attribute.attnum
         group by conrelid, conname
         ) candidate_index
    join pg_class on pg_class.oid = candidate_index.conrelid
    left join pg_index on pg_index.indrelid = conrelid
                          and indkey::text = array_to_string(column_list, ' ')
    where indexrelid is null
    

    My experience is that this isn’t really all that useful. It suggests creating indices for things like reference codes that really don’t need to be indexed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 478k
  • Answers 478k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You need to call abort() method: var request = $.ajax({… May 16, 2026 at 5:24 am
  • Editorial Team
    Editorial Team added an answer you can try to use css3pie. It is very easy… May 16, 2026 at 5:24 am
  • Editorial Team
    Editorial Team added an answer I cannot find any in the Windows API. However, I… May 16, 2026 at 5:24 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.