title: Postgres 9.6 - Unique constraint on array of JSONB key for "contains" operator
body: <p>We have a Postgres (9.6) table <code>my_table</code> with a row <code>email_address jsonb[] NOT NULL</code>:</p>
  
  <pre><code>DROP TABLE my_table;
  CREATE TABLE my_table (email_address jsonb NOT NULL);
  </code></pre>
  
  <p>We want to store an array of email addresses as JSONB (for the sake of this example, I'll only keep the <code>address</code> key):</p>
  
  <pre><code>INSERT INTO my_table (email_address)
  VALUES (ARRAY[ $${"address": "A"}$$::jsonb,
                 $${"address": "B"}$$::jsonb,
                 $${"address": "C"}$$::jsonb ]);
  
  INSERT INTO my_table (email_address)
  VALUES (ARRAY[ $${"address": "D"}$$::jsonb ]);
  </code></pre>
  
  <p>We want to make sure that each of the element we add to the array have a unique set of <code>address</code>.</p>
  
  <pre><code>-- ["A", "B"] &lt;@ ["A", "B", "C"], SHOULD FAIL
  INSERT INTO my_table (email_address)
  VALUES (ARRAY[ $${"address": "A"}$$::jsonb, $${"address": "B"}$$::jsonb ]
  
  -- ["A", "D"] IS UNIQUE SET, SHOULD SUCCEED
  INSERT INTO my_table (email_address)
  VALUES (ARRAY[ $${"address": "A"}$$::jsonb, $${"address": "D"}$$::jsonb ];
  </code></pre>
  
  <p>We were thinking about a exclusion constraint using a function maybe? We are currently trying to "map" the <code>jsonb[]</code> into a <code>text[]</code> (<code>[{address: "A"}, {address: "B"}]</code> into <code>["A", "B"]</code>) to perform a simple <code>@&gt;</code> operation but we struggle to obtain any convincing result.</p>
  
  <p>Our question is: Is such a constraint possible (if yes any help on the code would be greatly appreciated), and if not what would be the best way to implement such a system without having to create another table?</p>