Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cbVkk-0008Uh-UN for pgsql-bugs@arkaria.postgresql.org; Wed, 08 Feb 2017 17:09:59 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1cbVkk-0008EV-E7 for pgsql-bugs@arkaria.postgresql.org; Wed, 08 Feb 2017 17:09: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.84_2) (envelope-from ) id 1cbVkj-0008EM-TD for pgsql-bugs@postgresql.org; Wed, 08 Feb 2017 17:09:58 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1cbVkf-0000lY-0q for pgsql-bugs@postgresql.org; Wed, 08 Feb 2017 17:09:56 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id v18H9o5j004488; Wed, 8 Feb 2017 12:09:50 -0500 From: Tom Lane To: Tiago Babo cc: Peter Geoghegan , pgsql-bugs@postgresql.org Subject: Re: BUG #14526: no unique or exclusion constraint matching the ON CONFLICT In-reply-to: <2205EBF5-5095-4389-9D6B-1469EE60A0AD@gmail.com> References: <20170202140701.1401.31196@wrigleys.postgresql.org> <166D0A9D-162D-4216-9490-3163533166C1@gmail.com> <14575.1486493671@sss.pgh.pa.us> <499844AD-291B-40F5-9892-E5591736FD2E@gmail.com> <22106.1486500247@sss.pgh.pa.us> <31235.1486511840@sss.pgh.pa.us> <2205EBF5-5095-4389-9D6B-1469EE60A0AD@gmail.com> Comments: In-reply-to Tiago Babo message dated "Wed, 08 Feb 2017 15:30:38 +0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <4486.1486573790.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Wed, 08 Feb 2017 12:09:50 -0500 Message-ID: <4487.1486573790@sss.pgh.pa.us> X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org Tiago Babo writes: > Hi, again. After some testing, I was able to reproduce this error with th= e following code: > CREATE UNIQUE INDEX uniq_id_test ON test USING btree (type, id) WHERE (ty= pe =3D 'Test'); > PREPARE test (text, int, text) AS > INSERT INTO test (type, id) > VALUES ($1, $2) > ON CONFLICT (type, id) WHERE type =3D $3 DO UPDATE SET id =3D EXCLUDED.= id; > EXECUTE test('Test', 1, 'Test'); > EXECUTE test('Test', 2, 'Test'); > EXECUTE test('Test', 3, 'Test'); > EXECUTE test('Test', 4, 'Test'); > EXECUTE test('Test', 5, 'Test'); > EXECUTE test('Test', 6, 'Test'); > It gives the error when trying to execute the last statement. Hm. So the problem here is that the prepared statement only matches the partial index as long as the actual parameter is substituted literally into the statement. As soon as the plancache tries to consider a generic plan, in which it's not apparent at plan time what $3 is, we can't prove the partial index to be matched so you get the error. If this is representative of what your application is actually doing, rather than what you were saying it does, then the answer is that you have to match the partial index clause exactly, not rely on substitution/ simplification to produce a match. Or don't use a partial index. That schema seems pretty bizarre to me anyway. Having said all that, I think this is a fine example of why relying on planner inferences for semantic decisions (rather than just optimization) is damn-fool design. If I'd been paying closer attention I would have objected loudly to the use of WHERE in ON CONFLICT for this purpose. I wonder whether it's too late to deprecate that feature. regards, tom lane --=20 Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs