From: Rafal Pietrak <rafal@ztk-rp.eu>
To: David G. Johnston <david.g.johnston@gmail.com>
Cc: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
Subject: Re: FK v.s unique indexes
Date: Thu, 5 Jul 2018 23:36:45 +0200
Message-ID: <ffd0c4bf-ce15-2268-3ef2-585d7863ddcc@ztk-rp.eu> (raw)
In-Reply-To: <CAKFQuwatvdZ5xfLVa8h5P9ThaPuoKrUwYG5M2GErh+V7Pq18VA@mail.gmail.com>
References: <d9f85c60-3cdd-2504-cc6e-83fee0e19384@ztk-rp.eu>
<CAKFQuwZLmmG5p-DDBPu6oeLe2UkbRNps4HKPv3Bc8Moo-Gh16w@mail.gmail.com>
<69628a66-c6a6-339a-f2e3-1d2df4612350@ztk-rp.eu>
<CAKFQuwbc3famN5vrn6MCm6n4tkK9T6B37qiz8mAo2HCH5VPzrg@mail.gmail.com>
<e9fc35e1-14c3-feb1-9448-c78812eb7b3d@ztk-rp.eu>
<CAKFQuwatvdZ5xfLVa8h5P9ThaPuoKrUwYG5M2GErh+V7Pq18VA@mail.gmail.com>
W dniu 05.07.2018 o 23:04, David G. Johnston pisze:
> On Thu, Jul 5, 2018 at 1:45 PM, Rafal Pietrak <rafal@ztk-rp.eu
> <mailto: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.
>
I must assume, that the above "WHERE pk_col1 - val1..." is not the same
place as the one you've mentioned earlier, where "FK don't have where
clausures".
Thus, the bulk of code that maintains FK consistency "does not use where
clausures", would break on partial indexes. But in the above SELECT,
with partial indexed, that select will never fail. So it is not the
blocker... on the other hand, I would rather rewrite it along the lines of:
SELECT 1 = (SELECT count(1) FROM pk_table WHERE pk_col1 = val1 AND
pk_col2 = val2)
Naturally, if the planner choses to do a seq scan, everything would
break down - inappropriate rows would get hit; but that's a different story:
1. one may think, doing a "SELECT * FROM pk_table WHERE... INTO current"
and making all constraint trigger functions use that "current" instead
of making more lookups, could solve the problem.
2. or with some syntax help during FK creation (aka: ... add constraint
... using <index_name>) one could force the planner to always use
indicated index.
Still, whatever way to go, it is well beyond my level.
-R
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: rafal@ztk-rp.eu, david.g.johnston@gmail.com
Subject: Re: FK v.s unique indexes
In-Reply-To: <ffd0c4bf-ce15-2268-3ef2-585d7863ddcc@ztk-rp.eu>
References: <d9f85c60-3cdd-2504-cc6e-83fee0e19384@ztk-rp.eu> <CAKFQuwZLmmG5p-DDBPu6oeLe2UkbRNps4HKPv3Bc8Moo-Gh16w@mail.gmail.com> <69628a66-c6a6-339a-f2e3-1d2df4612350@ztk-rp.eu> <CAKFQuwbc3famN5vrn6MCm6n4tkK9T6B37qiz8mAo2HCH5VPzrg@mail.gmail.com> <e9fc35e1-14c3-feb1-9448-c78812eb7b3d@ztk-rp.eu> <CAKFQuwatvdZ5xfLVa8h5P9ThaPuoKrUwYG5M2GErh+V7Pq18VA@mail.gmail.com> <ffd0c4bf-ce15-2268-3ef2-585d7863ddcc@ztk-rp.eu>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox