Re: Allow ON CONFLICT DO UPDATE to return EXCLUDED values

2025-10-18 Thread v...@viktorh.net
(I realised I created a new thread by mistake, hopefully now I’ll get things 
back into the main one)

On 7 Oct 2025 at 23:52 +0200, Dean Rasheed , wrote:
On Tue, 7 Oct 2025 at 14:56, Viktor Holmberg  wrote:

I’ve looked through this patch. As far as I can tell, everything looks good, 
working and well commented.
The only nitpick I could find is a mispelling "EXLCUDED" → "EXCLUDED" in 
src/test/regress/expected/returning.out:464.

Thanks for looking. I'm also glad to see that you picked up the INSERT
... ON CONFLICT DO SELECT patch, because I think these 2 features
should work well together. I'll take another look at that one, but I'm
not going to have any time this week.
Agree. It’d be great if you could have a look, but no rush - I’m going on 
holiday now for a week anyway.

A maybe bigger question, is it nice that EXCLUDED is null when no conflict 
occurred? I can see the logic, but I think ergonomics wise it’d be nicer to 
have the proposed values in EXCLUDED, no matter what happened later. Then one 
can check EXCLUDED.value = NEW.value to see if one’s changes were added, for 
example.

Hmm, I'm not sure. I think it would be counter-intuitive to have
non-null EXCLUDED values for rows that weren't excluded, and I think
it's just as easy to check what values were added either way.
I see the point - I guess I think about EXCLUDED more as “PROPOSED”. I don’t 
have any examples at hand that would substantiate my point of view so it’s not 
a strong objection. In my opinion this patch adds value regardless, and you’re 
right that adapting the code to either case isn’t a big deal.



Re: INSERT ... ON CONFLICT DO SELECT [FOR ...] take 2

2025-09-03 Thread v...@viktorh.net
Hello, I was working on my own patch for the same thing, until I found this was 
already there. 
I think this would be very useful for a lot of people.
Do you need any help moving this forward Anderas? I have both tests and docs 
written, although not for the FOR UPDATE part.

> On 25 Jun 2025, at 13:39, Dean Rasheed  wrote:
> 
> On Mon, 12 May 2025 at 19:33, Andreas Karlsson  wrote:
>> 
>> I have fixed that one and some other issues locally and will submit a
>> new version in a while after I have added more tests because you are
>> very correct in that a big issue with my last version of the patch was
>> the big lack of tests and lack of making sure all features which
>> interact with UPSERT actually worked with my changes. Plus some islation
>> tests would be nice to have.
>> 
> 
> +1. Don't forget to move the CF entry to the next open CF.
> 
> FYI, over on [1] I proposed more tests and doc updates for RLS. I
> think those updates might make it easier to test and document the RLS
> aspects of this patch.
> 
> [1] 
> https://www.postgresql.org/message-id/flat/CAEZATCWqnfeChjK=n1v_dyzt4rt4mnq+ybf9c0qxdytvmsy...@mail.gmail.com
> 
> Regards,
> Dean
> 
> 
> 





Re: ON CONFLICT DO SELECT (take 3)

2025-11-17 Thread v...@viktorh.net
Thanks for the review Jian. Much appreciated. Apologies for the multiple email 
threads - just my email client mucking up the threads. This should hopefully 
bring them back to the mail thread.
I’ll go over it and make changes this week.
One question - why break out the OnConflictSet/ActionState rename to a separate 
commit? Previously, it only did Set (in update) so it’s naming did make sense.

> On 15 Nov 2025, at 12:11, jian he  wrote:
> 
> On Sat, Nov 15, 2025 at 5:24 AM jian he  wrote:
>> 
>> On Fri, Nov 14, 2025 at 10:34 PM Viktor Holmberg  wrote:
>>> 
>>> Here are some updates that needed to be done after the improvements to the 
>>> RLS docs / tests in 7dc4fa & 2e8424.
>>> 
> 
> hi.
> 
> I did some simple tests, found out that
> SELECT FOR UPDATE, the lock mechanism seems to be working as intended.
> We can add some tests on contrib/pgrowlocks to demonstrate that.
> 
> 
> infer_arbiter_indexes
>ereport(ERROR,
>(errcode(ERRCODE_WRONG_OBJECT_TYPE),
> errmsg("ON CONFLICT DO UPDATE not supported
> with exclusion constraints")));
> I guess this works for ON CONFLICT SELECT?
> we can leave some comments on the function infer_arbiter_indexes,
> and also add some tests on src/test/regress/sql/constraints.sql after line 
> 570.
> 
> 
> changing
> OnConflictSetState
> to
> OnConflictActionState
> could make it a separate patch.
> 
> all these 3 patches can be merged together, I think.
> 
> typedef struct OnConflictExpr
> {
>NodeTagtype;
>OnConflictAction action;/* DO NOTHING or UPDATE? */
> 
> "/* DO NOTHING or UPDATE? */"
> this comment needs to be changed?
> 
> src/backend/rewrite/rewriteHandler.c
> parsetree->onConflict->action == ONCONFLICT_UPDATE
> maybe we also need to do some logic to the ONCONFLICT_SELECT
> (I didn't check this part deeply)
> 
> src/test/regress/sql/updatable_views.sql, there are many occurence of
> "on conflict".
> I think we also need tests for ON CONFLICT DO SELECT.
> 
> CREATE TABLE base_tbl (a int PRIMARY KEY, b text DEFAULT 'Unspecified');
> INSERT INTO base_tbl SELECT i, 'Row ' || i FROM generate_series(-2, 2) g(i);
> CREATE VIEW rw_view15 AS SELECT a, upper(b) FROM base_tbl;
> INSERT INTO rw_view15 (a) VALUES (3);
> truncate base_tbl;
> INSERT INTO rw_view15 (a) VALUES (3);
> INSERT INTO rw_view15 (a) VALUES (3) ON CONFLICT (a) DO SELECT WHERE
> excluded.upper = 'UNSPECIFIED' RETURNING *;
> INSERT INTO rw_view15 (a) VALUES (3) ON CONFLICT (a) DO UPDATE SET a =
> excluded.a WHERE excluded.upper = 'UNSPECIFIED' RETURNING *;
> INSERT INTO rw_view15 (a) VALUES (3) ON CONFLICT (a) DO SELECT WHERE
> excluded.upper = 'Unspecified' RETURNING *;
> INSERT INTO rw_view15 (a) VALUES (3) ON CONFLICT (a) DO UPDATE SET a =
> excluded.a WHERE excluded.upper = 'Unspecified' RETURNING *;
> 
> If you compare it with the result above, it seems the updatable view behaves
> inconsistent with ON CONFLICT DO SELECT versus ON CONFLICT DO UPDATE.
> 
> --
> jian
> https://www.enterprisedb.com/

> On 10 Nov 2025, at 11:18, Viktor Holmberg  wrote:
> 
> Ah, must’ve been that I added the previous thread for referene on the 
> commitfest entry. Thanks for sorting that out.
> Looking forward to your review!
> 
> /Viktor
> On 10 Nov 2025 at 10:21 +0100, Dean Rasheed , wrote:
>> On Tue, 7 Oct 2025 at 12:57, Viktor Holmberg  wrote:
>>> 
>>> This patch implements ON CONFLICT DO SELECT.
>>> I’ve kept the patches proposed there separate, in case any of the people 
>>> involved back then would like to pick it up again.
>>> 
>>> Grateful in advance to anyone who can help reviewing!
>> 
>> Thanks for picking this up. I haven't looked at it yet, but I'm
>> planning to do so.
>> 
>> In the meantime, I noticed that the cfbot didn't pick up your latest
>> patches, and is still running the v7 patches, presumably based on
>> their names. So here they are as v8 (rebased, plus a couple of
>> indentation fixes in 0003, but no other changes).
>> 
>> Regards,
>> Dean