By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
Help with an interesting Postgres question: Why isn't an Index Only Scan used on a partition accessed via the parent table?.
CREATE OR REPLACE FUNCTION myfunc(int)
RETURNS TABLE(val int, txt text) LANGUAGE 'plpgsql'
AS $$
BEGIN
CREATE TEMPORARY TABLE tmp(id int,res text) ON COMMIT DROP;
IF $1=0 THEN
INSERT INTO tmp VALUES ($1,'Invalid');
ELSE
FOR i IN 1..$1 LOOP
INSERT INTO tmp VALUES (i,'txt '||i);
END LOOP;
END IF;
RETURN QUERY SELECT id,res FROM tmp;
END;
$$;
SELECT * FROM myfunc(42);
val | txt |
---|---|
1 | txt 1 |
2 | txt 2 |
3 | txt 3 |
4 | txt 4 |
5 | txt 5 |
6 | txt 6 |
7 | txt 7 |
8 | txt 8 |
9 | txt 9 |
10 | txt 10 |
11 | txt 11 |
12 | txt 12 |
13 | txt 13 |
14 | txt 14 |
15 | txt 15 |
16 | txt 16 |
17 | txt 17 |
18 | txt 18 |
19 | txt 19 |
20 | txt 20 |
21 | txt 21 |
22 | txt 22 |
23 | txt 23 |
24 | txt 24 |
25 | txt 25 |
26 | txt 26 |
27 | txt 27 |
28 | txt 28 |
29 | txt 29 |
30 | txt 30 |
31 | txt 31 |
32 | txt 32 |
33 | txt 33 |
34 | txt 34 |
35 | txt 35 |
36 | txt 36 |
37 | txt 37 |
38 | txt 38 |
39 | txt 39 |
40 | txt 40 |
41 | txt 41 |
42 | txt 42 |