简体   繁体   中英

CDC insert statement in postgres

I am trying to implement a statement where I update some timestamps and an active field in before inserting the new row. In case the information is identical, it should not do anything.

My statement looks like this:

with inputdata (id, name, inPrison)
as (
    SELECT * FROM (
        VALUES (
            1, 'egon',TRUE), (2, 'benny',FALSE), (3, 'kjeld',FALSE)) AS t (id,name,inPrison)
)
INSERT INTO public.cdc_test
           (id, NAME, inPrison,validfrom,validto,valid)
SELECT inputdata.id, inputdata.NAME, inputdata.inPrison, cdc.start, cdc.end, cdc.active
FROM inputdata 
cross JOIN (select now() as start, make_timestamptz(9999, 12, 31, 8, 00, 00) as end, TRUE as active) cdc
ON CONFLICT (id) WHERE md5(row(id,name)::text) != md5(row(public.cdc_test.id,public.cdc_test.name)::text)
DO UPDATE 
  SET valid = FALSE, 
      validto = excluded.validfrom 
      where active = TRUE

and the public.cdc_test table is defined as:

CREATE TABLE public.cdc_test (
   id int NOT NULL,
   name char(100),
   inPrison BOOLEAN,
   validfrom DATE,
   validto DATE,
   valid BOOLEAN
);

I can run my statement without the on conflict, but the on conflict keeps giving me trouble and i feel like I exhausted the ideas I had to make this work.

Can anyone see what i am doing wrong? When i run it, i get this error: invalid reference to FROM-clause entry for table "cdc_test"

Your ON CONFLICT should be after the WHERE clause, and the WHERE clause is related of the SELECT statement, not to the conflict handling

Here, your conflict handling is based on the id column (presumably a primary key on that table).

If you want to be able to handle ON CONFLICT on md5(row(id,name)::text) , then you need to create a UNIQUE INDEX on md5(row(id,name)::text) and use that expression on ON CONFLICT (md5(row(id,name)::text)) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM