HiveBrain v1.2.0
Get Started
← Back to all entries
snippetsqlMinor

Postgresql - Do Foreign Key Constraints Automatically Create Indexes?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
postgresqlconstraintscreateautomaticallyforeignindexeskey

Problem

Per the PGSQL 9.3 docs:


Because this is not always needed, and there are many choices available on how to index, declaration of a foreign key constraint does not automatically create an index on the referencing columns.

Yet after I create such a constraint, when I run \d I see that an index does in fact get created:

Indexes:
"table_primary_key" PRIMARY KEY, btree (id)
"fki_table_product_foreign_key" btree (product)


What could explain why this gets created? Do I still need to create another index on my product column to improve performance of queries on it?

Solution

I discovered that pgAdmin III does this by default. When you create a new foreign key constraint using the GUI, there is a checkbox "Auto FK Index" on the Definition tab. I hadn't noticed this until just now so it explains why the index was being created.

Context

StackExchange Database Administrators Q#75894, answer score: 5

Revisions (0)

No revisions yet.