问题
Is it possible to return several result sets of different types from postgres function?
Something like:
CREATE OR REPLACE FUNCTION getUserById()
RETURNS setof ???
AS
$$
BEGIN
return query select id, name /* and other columns */ from users where id = 1;
return query select id, phone_number from user_phones where user_id = 1
END
$$
LANGUAGE plpgsql;
I don't want to use joins because several phones for user are possible. Also it would be great to avoid using cursors. It's possible in MS SQL and I want to do the same thing in postgres.
回答1:
After the seek, could not find any better solutions as use of cursors. I assume you have seen this one already: https://blog.dsl-platform.com/multiple-result-sets-alternatives-in-postgres-3/
CREATE FUNCTION load_page(_session INT) RETURNS setof refcursor AS
$$
DECLARE c_top_items refcursor;
DECLARE c_shopping_cart refcursor;
BEGIN
OPEN c_top_items FOR
SELECT t.name, t.description
FROM top_item t
ORDER BY t.popularity DESC
LIMIT 10;
RETURN NEXT c_top_items;
OPEN c_shopping_cart FOR
SELECT c.product_id, c.product_name, c.quantity
FROM shopping_cart c
WHERE c.session_id = _session
ORDER BY c.id;
RETURN NEXT c_shopping_cart;
END;
$$ LANGUAGE plpgsql;
And calling:
BEGIN;
SELECT load_page(mySession);
FETCH ALL IN "<server cursor 1>";
FETCH ALL IN "<server cursor 2>";
COMMIT;
回答2:
I don't want to use joins because several phones for user are possible.
This is not a reason to avoid JOIN
s in PostgreSQL. At all.
PostgreSQL allows you to aggregate the phone numbers into an array:
CREATE OR REPLACE FUNCTION getUserById()
RETURNS TABLE (
id INTEGER,
name TEXT,
/* and other columns */
phone_numbers TEXT[]
)
AS
$$
select
users.id,
users.name,
/* and other columns */
-- Remove NULL because you get an array containing just NULL
-- if user_phones doesn't contain any matching rows.
array_remove(array_agg(user_phones.phone_number), NULL) as phone_numbers
from users
left join user_phones on user_phones.user_id = users.id
where users.id = 1
-- Note that grouping by a table's primary key allows you to use
-- any column from that table in the select in PostgreSQL
group by users.id
;
$$
LANGUAGE SQL
STABLE
;
This is much simpler and more intuitive.
You can switch to an inner join if it's okay to give back zero rows for a user without a phone number. In that case, you could drop the array_remove
call.
I also added the STABLE
specification to the function (since it doesn't modify any table data) and switched it to SQL
instead of PGPLSQL
(since it's just a single query). This will allow PG to optimize better; in particular, it can inline the query and push filters down in some cases. You may not even need a function, actually.
来源:https://stackoverflow.com/questions/36717138/postgres-function-return-multiple-tables