Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fbBvx-0003Xh-3D for pgsql-general@arkaria.postgresql.org; Thu, 05 Jul 2018 21:37:01 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fbBvu-0007lK-QI for pgsql-general@arkaria.postgresql.org; Thu, 05 Jul 2018 21:36:58 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fbBvu-0007lD-FK for pgsql-general@lists.postgresql.org; Thu, 05 Jul 2018 21:36:58 +0000 Received: from hax2-04.wsisiz.edu.pl ([213.135.44.188] helo=zorro.ztk-rp.eu) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1fbBvq-00085H-OD for pgsql-general@lists.postgresql.org; Thu, 05 Jul 2018 21:36:57 +0000 Received: from public-gprs405910.centertel.pl ([37.47.216.215] helo=[192.168.43.32]) by zorro.ztk-rp.eu with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.89) (envelope-from ) id 1fbBvj-0004w7-Ms; Thu, 05 Jul 2018 23:36:49 +0200 To: "David G. Johnston" Cc: "pgsql-general@lists.postgresql.org" References: <69628a66-c6a6-339a-f2e3-1d2df4612350@ztk-rp.eu> From: Rafal Pietrak Openpgp: preference=signencrypt Autocrypt: addr=rafal@ztk-rp.eu; prefer-encrypt=mutual; keydata= xsFNBFWmzBQBEADJdGVU97LFaJkXRUt99vWen/4SmHf4SzAAwPojxWDJgVYnnIgCxWN6R26t UbeDew7LCC68gxYPDE3cqxZWNa6jmq5xoHeOntmKT1bJNnVmA5r3OM58QdPLiaKo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LKW4RpwZK+NVeA3GNE52mGEty xagl65mnYX3Du1fRijgzlGMxyazQ+ssxLgMUIr7tJi2F52yBUngdks/xdEQ75w8YUx5C+V4M Oj0FBiF+gbzdnhadZEJOBmI0OOQ49vKqahg3eOHPPM7dOa2b2mRuZ2Z+CF/BZT7CIcF/1mt3 SBk63sVMnn7STdTVXCq1Jh0ZiydVq5nAbOVHbON9WxnJgrD5xxdKiD4vcPjgdV+7qgrB7EJ7 N7VM4jz+71o0E8vI3QRcUGT0JyoW1YK0eR92ph6XEmLGNZdHinMLJmlnuRdBbJtL7tYKTre1 hS/T2EH3oSLRPJFO8eOp4EFTb7XYEaiUDeOCZcrOotSjITqY036SRfOd9sCCIJ/i1/b3qby/ w1XV8kGPYwYX1CZpEiREkOuLzDXXLqZsUPJAARlCMHq3F0l0R/EjWLNpAJ73cYyMfs8drsZl iVrAx8l8iIX8Skj8V35MV6Fi5hFqWG6Zyt+wz2AIgd03yJJvpPXOdN9UCY0GFR6VIm0/YmkE yoTi1TIEbKWmSLr7m/a6R8S0Xhy3QJRNXVOOH0J/JByMFpVmGzZQQwfoERXuGcFMGvHrYnZO dXhF+K4aFoY9qVi2GZhJLzUZNwgA6pUJscXCp/5YvjorU71KTTIgd26XGWLWISHiVAv8vJc3 uBYVjEY= Message-ID: Date: Thu, 5 Jul 2018 23:36:45 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.8.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 8bit X-SA-Exim-Connect-IP: 37.47.216.215 X-SA-Exim-Mail-From: rafal@ztk-rp.eu X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on zorro.ztk-rp.eu X-Spam-Level: X-Spam-Status: No, score=-1.0 required=5.0 tests=ALL_TRUSTED autolearn=ham autolearn_force=no version=3.4.1 Subject: Re: FK v.s unique indexes X-SA-Exim-Version: 4.2.1 (built Tue, 02 Aug 2016 21:08:31 +0000) X-SA-Exim-Scanned: Yes (on zorro.ztk-rp.eu) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk W dniu 05.07.2018 o 23:04, David G. Johnston pisze: > On Thu, Jul 5, 2018 at 1:45 PM, Rafal Pietrak >wrote: > > I was thinking, that when "add constraint" cannot choose appropriate > index, may be some explicit help (like ... using ;) 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 ) one could force the planner to always use indicated index. Still, whatever way to go, it is well beyond my level. -R