title: Returning JSON from Postgres is slow
body: <p>I have a table in Postgres with a JSONB column, each row of the table contains a large JSONB object (~4500 keys, JSON string is around 110 KB in a txt file). I want to query these rows and get the entire JSONB object.</p>
  
  <p>The query is fast -- when I run <code>EXPLAIN ANALYZE</code>, or omit the JSONB column, it returns in 100-300 ms. But when I execute the full query, it takes on the order of minutes. The exact same query on a previous version of the data was also fast (each JSONB was about half as large).</p>
  
  <p>Some notes:</p>
  
  <ul>
  <li><p>This ends up in Python (via SQLAlchemy/psycopg2). I'm worried that the query executor is converting JSONB to JSON, then it gets encoded to text for transfer over the wire, then gets JSON encoded again on the Python end. 
  Is this correct? If so how could I mitigate this issue? When I select the JSONB column as <code>::text</code>, the query is roughly twice as fast.</p></li>
  <li><p>I only need a small subset of the JSON (around 300 keys or 6% of keys). I tried methods of filtering the JSON output in the query but they caused a substantial further performance hit -- it ended up being faster to return the entire object.</p></li>
  </ul>