sql - Capture the result of multiple upsert statement into one output for Postgresql -


i running multiple upsert statements in single run. queries can add or update depending on data. is possible know if new insert occurred.

test_unique unique constraint column1 , column2 put together.

insert test(column1,column2) values(1,1) on conflict on constraint test_unique nothing; insert test(column1,column2) values(1,2) on conflict on constraint test_unique nothing; insert test(column1,column2) values(1,1) on conflict on constraint test_unique nothing; 

if take above queries

  • 1st query insert new row
  • 2nd query insert new row
  • 3rd query nothing

any client library when running these queries batch capture output of last query , return 0 rows.

is there way know single insert irrespective of position in queries?

thanks

you can use returning:

with i1 (       insert test(column1,column2) values(1,1)           on conflict on constraint test_unique nothing           returning *      ),      i2 (       insert test(column1,column2) values(1,2)           on conflict on constraint test_unique nothing           returning *      ),      i3 (       insert test(column1,column2) values(1,1)           on conflict on constraint test_unique nothing           returning *     ) select id i1 union select id i2 union select id i3; 

Comments