Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
193 views
in Technique[技术] by (71.8m points)

sql - PostgreSQL union a variable of type array to the result set of a query

Is it possible to union a to union a variable to a select statement in PostgreSQL? I have a recursive function at the moment that in essence does this:

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)
.

I've recently made some changes that increase the complexity of the select by a lot, and to increase performance I'd like to run that query only once per function call and do something like

var = select id from x where y 
union call_recurrsive_function(var)
question from:https://stackoverflow.com/questions/65829019/postgresql-union-a-variable-of-type-array-to-the-result-set-of-a-query

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can try using a CTE (Common Table Expression). For example:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

Just Browsing Browsing

2.1m questions

2.1m answers

60 comments

57.0k users

...