Use INSERT ... ON CONFLICT DO NOTHING RETURNING failed rows
<p>Suppose I have the following table:  </p>

<pre><code>CREATE TABLE tags (
    id int PK,
    name varchar(255),
    CONSTRAINT name_unique UNIQUE(name)
)
</code></pre>

<p>I need a query that will insert tags that do not exists and return ids for all requested tags. Consider the following:</p>

<pre><code>INSERT INTO tags (name) values ('tag10'), ('tag6'), ('tag11') ON CONFLICT DO NOTHING returning id, name
</code></pre>

<p>The output of this query is:</p>

<pre><code>+---------------+
|  id   |  name |
|---------------|
|  208  | tag10 |
|---------------|
|  209  | tag11 |
+---------------+
</code></pre>

<p>What I need is to have <code>tag6</code> in the output.</p>