title: How to add a conditional unique index on PostgreSQL
body: <p>I have a <code>line_items</code> table with following columns:</p>
  
  <pre><code>product_id
  variant_id
  </code></pre>
  
  <p><code>variant_id</code> is nullable.</p>
  
  <p>Here is the condition:</p>
  
  <ul>
  <li>If <code>variant_id</code> is NULL then <code>product_id</code> should be unique. </li>
  <li>If <code>variant_id</code> has a value then combination of <code>product_id</code> and <code>variant_id</code> should be unique.</li>
  </ul>
  
  <p>Is that possible in PostgreSQL?</p>