title: What's the proper index for querying structures in arrays in Postgres jsonb?
body: <p>I'm experimenting with keeping values like the following in a Postgres <code>jsonb</code> field in Postgres 9.4:</p>
  
  <pre><code>[{"event_slug":"test_1","start_time":"2014-10-08","end_time":"2014-10-12"},
   {"event_slug":"test_2","start_time":"2013-06-24","end_time":"2013-07-02"},
   {"event_slug":"test_3","start_time":"2014-03-26","end_time":"2014-03-30"}]
  </code></pre>
  
  <p>I'm executing queries like:</p>
  
  <pre><code>SELECT * FROM locations
  WHERE EXISTS (
    SELECT 1 FROM jsonb_array_elements(events) AS e
    WHERE (
      e-&gt;&gt;'event_slug' = 'test_1' AND
      (
        e-&gt;&gt;'start_time' &gt;= '2014-10-30 14:04:06 -0400' OR
        e-&gt;&gt;'end_time' &gt;= '2014-10-30 14:04:06 -0400'
      )
    )
  )
  </code></pre>
  
  <p>How would I create an index on that data for queries like the above to utilize? Does this sound reasonable design for a few million rows that each contain ~10 events in that column?</p>
  
  <p>Worth noting that it seems I'm still getting sequential scans with:</p>
  
  <pre><code>CREATE INDEX events_gin_idx ON some_table USING GIN (events);
  </code></pre>
  
  <p>which I'm guessing is because the first thing I'm doing in the query is converting data to json array elements.</p>