title: PostgreSQL JSONB - JSON Array vs JSON Object performance
body: <p>The each object (instance) has its own privacy settings per user. These settings are defined via <code>privacy JSONB field</code> in object table row. I'm curious which way is better regards to the performance? Or better aproach at all?</p>
  
  <p>Explanation:</p>
  
  <ul>
  <li>User with ID 2 can read and write to the object</li>
  <li>User with ID 8 can only read the object</li>
  <li>SQL queries: get records the User with ID 2 can read (assume tasks table)</li>
  </ul>
  
  <p><strong>Case one</strong> - using array (note: the first key is User ID)</p>
  
  <pre class="lang-js prettyprint-override"><code>privacy: {
    "2": ["read","write"],
    "8": ["read"]
  }
  </code></pre>
  
  <pre class="lang-sql prettyprint-override"><code>SELECT * FROM "tasks" WHERE (privacy -&gt; '2' ? 'read')
  </code></pre>
  
  <p><strong>Case two</strong> - using object (not sure in terminology, it is more like hash for me)</p>
  
  <pre class="lang-js prettyprint-override"><code>privacy: {
    "2": {"read":true, "write":true},
    "8": {"read":true, "write":false}
  }
  </code></pre>
  
  <pre class="lang-sql prettyprint-override"><code>SELECT * FROM "tasks" WHERE (privacy -&gt; '2' -&gt; 'read' = true)
  </code></pre>
  
  <p>Any suggestions are welcome.</p>