PostgreSQL foreign keys and indexes
If you’re a frequent user of MySQL, you may be familiar with the fact that all MySQL table constraints automatically create indexes for you. This is true of the InnoDB foreign key constraints, for which “an index is created on the referencing table automatically if it does not exist.”
If you’re switching or considering a switch to PostgreSQL, you should be aware that not all PostgreSQL table constraints will automatically create indexes for for you. In PostgreSQL, a UNIQUE or PRIMARY KEY constraint on one or more fields will implicitly create an index for you. However, in PostgreSQL a FOREIGN KEY constraint will not automatically create an index for you.
For each of your foreign key constraints, you should evaluate whether you want to create an index. You may want to do this for optimizing your own queries, but be aware that it can also help to speed up DELETE queries on the referenced table and UPDATE queries on the referenced field. This is because any foreign key reference must be located to enforce whatever ON DELETE and ON UPDATE behavior is in effect for the constraint.