title: Upgrading to PostgreSQL 11: set-returning functions are not allowed in CASE
body: <p>The following query stopped working when upgrading from PostgreSQL 9.6 to 11:</p>
  
  <pre><code>with doc as (select * from documents where name = doc_id)
  
  select jsonb_array_elements_text(permissions)
  from users
  where users.name = user_name
  
  union
  
  select 
    case 
      when doc.reader = user_name then 'read'
      when doc.owner = user_name then unnest(array['read','write'])
      else unnest(array[]::text[])
      end 
  from doc;
  </code></pre>
  
  <p>The <code>union</code> as usual <em>puts together</em> two lists of values, both lists can have zero, one or more elements. </p>
  
  <p>The first <code>select</code> can return zero, one or more just because that's what's in the <code>users</code> table.</p>
  
  <p>The second <code>select</code> always scans one row from the <code>documents</code> table, but returns zero, one or more rows depending on what the <code>case</code> decides. </p>
  
  <p>PostgreSQL 9.6 was working as expected, PostgreSQL 11 says:</p>
  
  <pre><code>ERROR:  set-returning functions are not allowed in CASE
  LINE 56:    else unnest(array[]::text[])
                   ^
  HINT:  You might be able to move the set-returning function into a LATERAL FROM item.
  </code></pre>
  
  <p>I appreciate the advice, but I can't figure out how to use a <code>LATERAL FROM</code> here.</p>