title: Postgres EAV to JSON
body: <p>I have an entity, attribute, value table in which the definition of the attributes also exists as entities. In the example below it stores the firstname and lastname attributes of entities. The attr 33 is "known" and defines an attribute, the value is the name of the attribute. So the third row in the example has attr=1 which points to entity=1 of the first row.</p>
  
  <pre><code>entity | attr | value
  -------+------+------
       1 |   33 | FirstName
       2 |   33 | LastName
       3 |    1 | John
       3 |    2 | Doe
       4 |    1 | Bruce
       4 |    2 | Willis
       5 |    1 | Mark
       5 |    2 | Hamill
  </code></pre>
  
  <p>I want to retrieve this table without the overhead of repeating the entity and attr ids in the result (as in the real world there are many rows and the ids are uuid). So for this reason I want to retrieve the above table in the JSON format below:</p>
  
  <pre><code>[
      [{'Id': 1, 'name': 'FirstName'}, {'Id': 2, 'name': 'Lastname'}],
      [3, 'John', 'Doe'],
      [4, 'Bruce','Willis'],
      [5, 'Mark', 'Hamill']
  ]   
  </code></pre>
  
  <p>Is there any performant way of doing this in postgres 9.5?</p>