Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
升级到PostgreSQL 11:在这种情况下不允许使用set返回函数_Sql_Postgresql_Lateral - Fatal编程技术网

升级到PostgreSQL 11:在这种情况下不允许使用set返回函数

升级到PostgreSQL 11:在这种情况下不允许使用set返回函数,sql,postgresql,lateral,Sql,Postgresql,Lateral,从PostgreSQL 9.6升级到11时,以下查询停止工作: 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' w

从PostgreSQL 9.6升级到11时,以下查询停止工作:

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;
像往常一样,
联合将两个值列表放在一起,两个列表可以有零个、一个或多个元素

第一个
select
可以返回零,一个或多个,因为这是
users
表中的内容

第二个
选择
始终扫描
文档
表中的一行,但根据
案例
的决定返回零、一行或多行

PostgreSQL 9.6按预期运行,PostgreSQL 11表示:

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.

我很感激你的建议,但我不知道如何在这里使用
侧边。

这里的提示有点误导。正如上面所说的,向集合返回函数中添加横向连接可能会有所帮助(一般来说),但我认为这在您的情况下没有多大意义

通过更改
CASE
表达式以返回数组,然后取消对结果的测试,可以轻松解决此问题:

...
select 
  unnest(
    case 
      when doc.reader = user_name then array['read']
      when doc.owner = user_name then array['read','write']
      else array[]::text[]
    end
  )
from doc;