title: Postgresql: select rows by OR condition - including going through json array
body: <p>I have a table created by following query:</p>
  
  <pre><code>create table data
  (
      id integer not null unique,
      owner text,
      users jsonb not null
  );
  </code></pre>
  
  <p>The table looks like this:</p>
  
  <pre><code>+----+-------+---------------------------------------------+
  | id | owner |                    users                    |
  +----+-------+---------------------------------------------+
  |  1 | alice | []                                          |
  |  2 | bob   | [{"accountId": "alice", "role": "manager"}] |
  |  3 | john  | [{"accounId": "bob", "role": "guest"}]      |
  +----+-------+---------------------------------------------+
  </code></pre>
  
  <p>I need to get rows 1 and 2 on behalf of Alice.</p>
  
  <p>Getting owner-based rows works perfect:</p>
  
  <pre><code>SELECT *
  FROM data
  WHERE owner = 'alice'
  </code></pre>
  
  <p>Getting jsonb-based rows is a little trickier though managable:</p>
  
  <pre><code>SELECT *
  FROM data, jsonb_array_elements(users) x
  WHERE (x -&gt;&gt; 'accountId') = 'alice'
  </code></pre>
  
  <p>But getting them together gets me just the jsonb-based ones:</p>
  
  <pre><code>SELECT *
  FROM data, jsonb_array_elements(users) x
  WHERE owner = 'alice' OR (x -&gt;&gt; 'accountId') = 'alice'
  </code></pre>
  
  <p>How do I get the selection that looks like following?</p>
  
  <pre><code>+----+-------+---------------------------------------------+
  | id | owner |                    users                    |
  +----+-------+---------------------------------------------+
  |  1 | alice | []                                          |
  |  2 | bob   | [{"accountId": "alice", "role": "manager"}] |
  +----+-------+---------------------------------------------+
  </code></pre>
  
  <p>Even better if I can get a selection that looks like this</p>
  
  <pre><code>+----+----------+
  | id |   role   |
  +----+----------+
  |  1 | owner    |
  |  2 | manager  |
  +----+----------+
  </code></pre>