title: unnest() subquery that returns an array?
body: <p>I've encountered a rather unintuitive behaviour while <code>unnest</code>ing array results returned <code>FROM</code> sub-<code>SELECT</code>.</p>
  
  <pre><code>SELECT unnest(c).* 
  FROM (SELECT chat_messages[0 : array_length(chat_messages, 1)] 
        FROM Chats WHERE chat_id = 2) c
  </code></pre>
  
  <p>This was my original query. Postgres doens't like it:</p>
  
  <blockquote>
    <p>function unnest(record) does not exist</p>
  </blockquote>
  
  <p>But this seemingly equivalent query works:</p>
  
  <pre><code>SELECT * 
  FROM unnest((SELECT chat_messages[0 : array_length(chat_messages, 1)] 
               FROM Chats WHERE chat_id = 2)) c
  </code></pre>
  
  <p>This query doesn't work either with the same error message:</p>
  
  <pre><code>SELECT * 
  FROM (SELECT chat_messages[0 : array_length(chat_messages, 1)] 
        FROM Chats WHERE chat_id = 2) c,
       unnest(c) u
  </code></pre>
  
  <p>I'm pretty sure I'm missing something here. Why such behaviour? And how come subquery returns <code>record</code> type when it's defined composite type?</p>