title: Indexing Postgresql JSONB arrays for element existence and unicity
body: <p>I have a Postgresql 11.8 table named <code>posts</code> where I would like to define a column <code>slugs</code> of type JSONB, which would contain arrays of strings such as <code>[&quot;my-first-post&quot;, &quot;another-slug-for-my-first-post&quot;]</code>.</p>
  <p>I can find a post having a specific slug using the <code>?</code> existence operator: <code>SELECT * FROM posts WHERE slugs ? 'some-slug'</code>.</p>
  <p>Each post is expected to only have a handful of slugs but the amount of posts is expected to grow.</p>
  <p>Considering the above query where <code>some-slug</code> could be any string:</p>
  <ul>
  <li>How can I define an index to have a reasonably performant query (no full table scan)?</li>
  <li>How can I ensure the same slug cannot appear multiple times (across and within the different arrays)?</li>
  </ul>
  <p>I am primarily looking for a solution for Postgresql 11 but also would be interested to know solutions in future versions, if any.</p>
  <p>The database is used in a Rails 6.0 app so I am also interested by the Rails migration syntax.</p>