首页 > 解决方案 > PostgreSQL 将数组类型的变量联合到查询的结果集

问题描述

是否可以联合 a 以将变量联合到 PostgreSQL 中的 select 语句?我现在有一个递归函数,它本质上是这样做的:

create or replace function call_recurrsive_function(ids bigint[])
.
.
select id from x where y
union call_recurrsive_function(select id from x where y)
.

我最近进行了一些更改,这些更改大大增加了选择的复杂性,为了提高性能,我希望每个函数调用只运行一次该查询并执行类似的操作

var = select id from x where y 
union call_recurrsive_function(var)

标签: sqlpostgresql

解决方案


您可以尝试使用 CTE(通用表表达式)。例如:

with
r as (
  select id from x where y
)
select *
from (
  select id from r
  union call_recurrsive_function(select id from r)
) x

推荐阅读