Re: Bug Report: INSERT ON CONFLICT sometimes does not work with partial index

From: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
To: Danylo Miroshnichenko <miroshnik(dot)dan(at)gmail(dot)com>
Cc: pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: Bug Report: INSERT ON CONFLICT sometimes does not work with partial index
Date: 2023-02-05 17:09:40
Message-ID: CAKFQuwa1oJs6aNdpEJyT9K=o6k-frkAM8VUuxEtOjjtq8Zjv3g@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

On Sun, Feb 5, 2023 at 9:51 AM Danylo Miroshnichenko <
miroshnik(dot)dan(at)gmail(dot)com> wrote:

> CREATE UNIQUE INDEX IF NOT EXISTS uniq_id_test
> ON test USING btree (type, id)
> WHERE (type = 'Test');
> PREPARE test (text, int, text) AS
> INSERT INTO test (type, id)
> VALUES ($1, $2)
> ON CONFLICT (type, id)
> WHERE type = $3 DO
> UPDATE
> SET id = EXCLUDED.id;
>
>

> EXECUTE test('Test', 6, 'Test');
>
> The last EXECUTE statement always throws an error:
>
> [42P10] ERROR: there is no unique or exclusion constraint matching the ON CONFLICT specification
>
> All of it works fine if we replace $3 parameter with a constant value 'Test' (no errors):
>
> PREPARE test (text, int, text) AS
> INSERT INTO test (type, id)
> VALUES ($1, $2)
> ON CONFLICT (type, id)
> WHERE type = 'Test' DO
> UPDATE
> SET id = EXCLUDED.id;
>
>
This is a known deficiency that the first query ever works at all. It
should error every time.

https://www.postgresql.org/message-id/flat/CAKFQuwao4tL8gAww%3D%3DJXV66UB14GhhKDbQPg7CB8_JkyfMx%2Buw%40mail.gmail.com#3c60b77748d9b3ee9291f4632eb9a007
https://www.postgresql.org/message-id/flat/20170202140701.1401.31196%40wrigleys.postgresql.org

In short, consider the WHERE clause a part of query structure that has to
statically match one partial index specification.

David J.

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message David G. Johnston 2023-02-05 17:15:09 Re: BUG #17771: add a "status" column to the pg_rules system view
Previous Message PG Bug reporting form 2023-02-05 16:02:58 BUG #17771: add a "status" column to the pg_rules system view