Re: FK v.s unique indexes - Mailing list pgsql-general

From David G. Johnston
Subject Re: FK v.s unique indexes
Date
Msg-id CAKFQuwatvdZ5xfLVa8h5P9ThaPuoKrUwYG5M2GErh+V7Pq18VA@mail.gmail.com
Whole thread Raw
In response to Re: FK v.s unique indexes  (Rafal Pietrak <rafal@ztk-rp.eu>)
Responses Re: FK v.s unique indexes  (Rafal Pietrak <rafal@ztk-rp.eu>)
List pgsql-general
On Thu, Jul 5, 2018 at 1:45 PM, Rafal Pietrak <rafal@ztk-rp.eu> wrote:
I was thinking, that when "add constraint" cannot choose appropriate
index, may be some explicit help (like ... using <index_name>;) would be
due.

​Basically all the FK trigger does is:

SELECT EXISTS(SELECT 1 FROM pk_table WHERE pk_col1 = val1 AND pk_col2 = val2)

And fails if query returns false.  The planner is still free to use the index or not to execute the query just as if you had written it by hand.  For a small table fully in memory it likely would prefer a sequential scan and it would be perfectly within its rights to do so.

David J.

pgsql-general by date:

Previous
From: Rafal Pietrak
Date:
Subject: Re: FK v.s unique indexes
Next
From: Rafal Pietrak
Date:
Subject: Re: FK v.s unique indexes