Return rows from INSERT with ON CONFLICT without needing to update
I have a situation where I very frequently need to get a row from a table with a unique constraint, and if none exists then create it and return. For example my table might be: CREATE TABLE names( id SERIAL PRIMARY KEY, name TEXT, CONSTRAINT names_name_key UNIQUE (name) ); And it contains: id | name 1 | bob 2 | alice Then I'd like to: INSERT INTO names(name) VALUES ('bob') ON CONFLICT DO NOTHING RETURNING id; Or perhaps: INSERT INTO names(name) VALUES ('bob') ON CONFLICT (name) DO NOTHING RETURNING id and have it return bob's id 1 . However, RETURNING only returns either inserted or updated