Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgres: Using ON CONFLICT together with CASE

Tags:

postgresql

I'm trying to nail down the exact syntax that I should use to do an ON CONFLIC together with CASE.

I have a table (ignore id, it's not relevant here):

CREATE TABLE public.test (
    id integer NOT NULL,
    mykey integer,
    t text,
);

with the constraint that mykey must be unique

ALTER TABLE test
ADD CONSTRAINT test_constraint UNIQUE (mykey);

Lets populate it with some play entries:

INSERT INTO test (mykey,t) VALUES (123,'my first value');
INSERT INTO test (mykey,t) VALUES (13,'second value');

Now I want to craft an INSERT statment which uses an ON CONFLICT and a CASE so that: if the new value of t is equal to the old one, do nothing, else update.

Here's what I have so far:

INSERT INTO test (mykey,t) VALUES (123,'a new value') 
ON CONFLICT ON CONSTRAINT test_constraint 
DO CASE t
WHEN excluded.t THEN NOTHING
ELSE UPDATE SET t=excluded.t;
END;

I get an error message

ERROR:  syntax error at or near "case"
LINE 3: do case t
           ^
WARNING:  there is no transaction in progress

I'm not surprised that I get an error message, because wouldn't it be amazing if I got an improvised statement right on the first try? ;-)

However, it should be possible to either do this (provided the syntax is correct), or something else to the same effect. I know how to do ON CONFLICT DO NOTHING, ON CONFLICT DO UPDATE, and CASE/WHEN/ELSE. It should be possible to use them all together.

EDIT: I'll be updating with variations that I've tried and not worked

INSERT INTO test (mykey,t) VALUES (123,'a new value') 
ON CONFLICT ON CONSTRAINT test_constraint 
CASE t
WHEN excluded.t THEN DO NOTHING
ELSE DO UPDATE SET t=excluded.t;
END;

(same error)


1 Answers

You may use the condition in a WHERE clause. IS DISTINCT FROM does the comparison

INSERT INTO test (mykey, t) 
VALUES (123,'a new value') 
    ON CONFLICT ON CONSTRAINT test_constraint 
    DO  
       UPDATE SET t = excluded.t 
       WHERE test.t is distinct from excluded.t;

Demo

like image 172
Kaushik Nayak Avatar answered Jan 04 '26 00:01

Kaushik Nayak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!