When I insert an item into the table, I only need to supply the name and PostgreSQL will set the id and created fields. PostgreSQL 9.5: Insert IF not Exists, Update IF Exists (Insert ON CONFLICT option) In above article, I used only one single key in ON CONFLICT clause. The simple solution has its appeal, the side effects may be less important. Postgresql 9.5 introduced the long-waited for upsert. But if there are conflicts it doesn't return any rows: | id | I want to return the new id columns if there are no conflicts or return the existing id columns of the conflicting columns. ON CONFLICT DO SELECT" syntax is still an INSERT statement, not a SELECT, so a user should not expect rows returned from it to be available for UPDATE/DELETE in another part of a wCTE. INSERT INTO customer (cust_id, name, address) SELECT cust_id, name, address FROM customer_stage ON CONFLICT (cust_id) DO UPDATE SET address = excluded.address; As your comment is too much old and I’ve tried it today, there are chances that ON CONFLICT clause might have got some improvements. SERIAL data type allows you to automatically generate unique integer numbers (IDs, identity, auto-increment, sequence) for a column. Using this feature, one can ask Postgres to return essentially any value you want; returning the ID of the newly inserted … RETURN (INSERT INTO configuration_dates ( weekly_date_configuration_id, "from", "to", price, activity_configuration_id ) VALUES ( wdc_id, from_ts, from_ts + wdc.duration, wdc.price, wdc.activity_configuration_id ) RETURNING id); But I haven't found how to … There are cases where instead of actually updating when the row already exists what you want is to return the primary key (or the whole row for that matter). For all other cases, though, do not update identical rows without need. Maybe a pseudo-column can be added so that it can be used in the returning statement: insert into foobar(id, other_col) values(2, '2') on conflict (id) update set other_col=excluded.other_col returning id, pseudo.was_updated; This would ensure that users could check for each primary key value if the row was updated or inserted. The currently accepted answer seems ok for a single conflict target, few conflicts, small tuples and no triggers. insert into "catalog" ("name", "sku", "price") values ('foo', 'BAR', 34.89) returning "product_id" The returning at the end is a nice add-on that allows us to get the ID of the newly added row. Reply Cancel And it avoids concurrency issue 1 (see below) with brute force. I'm trying to write a query to insert filenames to the following table table path. A day before yesterday, I got an email like, does it require to add a unique index on those columns which we require to be in ON CONFLICT … By using the RETURNING keyword on the end of my insert query, I can have PostgreSQL return those new values to me as part of the same operation. Postgresql has a SQL extension called RETURNING that lets you get the values just inserted or updated but you can’t just DO NOTHING and get the value back.