Re: BUG #18502: Upsert on view returns 42P10 error when condition is an expression

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: michael(dot)wanghai(dot)a(at)outlook(dot)com
Cc: Peter Geoghegan <pg(at)bowt(dot)ie>, pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: BUG #18502: Upsert on view returns 42P10 error when condition is an expression
Date: 2024-06-11 20:54:31
Message-ID: 1153168.1718139271@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

PG Bug reporting form <noreply(at)postgresql(dot)org> writes:
> I have a table, index and view like following:
> ```
> CREATE TABLE my_table
> (
> id uuid primary key,
> data jsonb not null
> );
> CREATE UNIQUE INDEX ON my_table ((data ->> 'key'));
> CREATE VIEW my_view AS SELECT * FROM my_table;
> ```
> The upsert on view returns 42P10 error when I execute the following SQL
> ```
> INSERT INTO my_view (id, data)
> VALUES ('990cc75c-2e60-4c0d-8bec-9ac976dc03bc'::uuid,
> '{
> "key": "value"
> }'::jsonb)
> ON CONFLICT ((data ->> 'key'))
> DO NOTHING;
> ```

For the archives' sake: the error being complained of is

ERROR: there is no unique or exclusion constraint matching the ON CONFLICT specification

Thanks for the report. The fault lies with infer_arbiter_indexes(),
which assumes that it can match the output of
RelationGetIndexExpressions or RelationGetIndexPredicate
directly to the query without adjusting varnos. That works
most of the time, because the INSERT target typically has
varno 1, matching the way these trees are stored in the catalogs.
But not so much when we've flattened an updatable view;
so the match fails even when it should succeed.

The attached seems to be enough to fix it.

regards, tom lane

Attachment Content-Type Size
adjust-varnos-in-infer_arbiter_indexes.patch text/x-diff 6.1 KB

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Thomas Munro 2024-06-12 00:59:08 Re: BUG #18503: Reproducible 'Segmentation fault' in 16.3 on ARM64
Previous Message PG Bug reporting form 2024-06-11 20:22:39 BUG #18505: while installation of postgresql ,getting warning by selecting postresql 16 on port 5432