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