I need to call a function several times for every select row, because function have several OUT parameters, all of which I need
E.g.
SELECT
a, b,
(SELECT out1 from func(a)),
(SELECT out2 from func(a))
FROM
table1
To call this function only once I use lateral join
SELECT
a, b,
lat.out1,
lat.out2
LEFT OUTER JOIN LATERAL (
SELECT out1, out2 FROM func(a)
) lat ON (TRUE)
The problem is in case a
is nullable. func
throws exception being called with null. Then without joins I would do it like this
SELECT
a, b,
CASE WHEN a IS NOT NULL
THEN out1 from func(a)
END,
CASE WHEN a IS NOT NULL
THEN out2 from func(a)
END
FROM
table1
But how could I implement it using lateral joins
? Is there a way to use CASE inside lateral join? Or is there any other way to call the procedure only once?