i have that working code, but i need to convert it to function with dynamic attribute tid=1645 where number 1645 will always change.
with recursive r as (
select tid, boss from titles where tid=1645
union
select titles.tid, titles.boss from titles join r on titles.tid = r.boss
)
select * from r
Now, i have the same:
DROP FUNCTION bosses_of_rsd_tids(integer);
CREATE OR REPLACE FUNCTION public.bosses_of_rsd_tids(rsd_tid int)
RETURNS table (c_tid int, c_boss int)
LANGUAGE plpgsql
AS $function$
begin
with recursive r as (
select tid, boss from titles where tid=rsd_tid
union
select titles.tid, titles.boss from titles join r on titles.boss = r.tid
)
select c_tid, c_boss;
end;
$function$
;
As a result i need table of results... I tried to return select c_tid, c_boss; but have error: error near return
CREATE OR REPLACE FUNCTION public.bosses_of_rsd_tids(rsd_tid int)
RETURNS TABLE (c_tid int, c_boss int) AS
$func$
BEGIN
RETURN QUERY
WITH RECURSIVE r AS (
SELECT tid, boss
FROM titles
WHERE tid = rsd_tid
UNION ALL -- ?!
SELECT t.tid, t.boss
FROM r
JOIN titles t ON t.tid = r.boss -- !
)
TABLE r; -- !
END
$func$ LANGUAGE plpgsql;
You want UNION ALL instead of UNION as it doesn't make sense to try and fold duplicates climbing the hierarchy. (A duplicate would initiate an endless loop.)
TABLE r is short for SELECT * FROM r. Your org. select c_tid, c_boss was wrong. See:
Is there a shortcut for SELECT * FROM?
Can also be a simpler SQL function:
CREATE OR REPLACE FUNCTION public.bosses_of_rsd_tids(rsd_tid int)
RETURNS TABLE (c_tid int, c_boss int) AS
$func$
WITH RECURSIVE r AS (
SELECT tid, boss
FROM titles
WHERE tid = rsd_tid
UNION ALL
SELECT t.tid, t.boss
FROM r
JOIN titles t ON t.tid = r.boss
)
TABLE r;
$func$ LANGUAGE sql;
See:
Difference between language sql and language plpgsql in PostgreSQL functions
You have to use "return query" on all the query (with included)
You forgot the "from r" in the main select
/* EDIT */
in you example you select c_tid and c_boss instead of tid and boss
and the test of the join is inverted
request updated:
DROP FUNCTION bosses_of_rsd_tids(integer);
CREATE OR REPLACE FUNCTION public.bosses_of_rsd_tids(rsd_tid int)
RETURNS table (c_tid int, c_boss int)
LANGUAGE plpgsql
AS $function$
begin
return query with recursive r as (
select tid, boss from titles where tid=rsd_tid
union
select titles.tid, titles.boss from titles join r on titles.tid = r.boss )
select tid, boss from r;
end;
$function$
;
Related
I am trying to find the maximum concecutive duration dor each room per weekday. For example (15B,1,8,10) : the room_id 15B has the longest duration on Monday from 8:00 to 10:00. Start_time and end_time are time values and i want to convert them to integer. Weekday is varchar and i convert it to integer(0->sunday, 1->monday etc).
Here is my code
create or replace function iweekday(weekday varchar(9))
returns int as $$
begin
select CASE weekday
WHEN 'Monday' THEN 1
WHEN 'Tuesday' THEN 2
WHEN 'Wednesday' THEN 3
WHEN 'Thursday' THEN 4
WHEN 'Friday' THEN 5
WHEN 'Saturday' THEN 6
WHEN 'Sunday' THEN 0
END;
end;
$$ language plpgsql;
drop function fourpointnine();
create or replace function fourpointnine()
returns table (room varchar(7), iw int, st int, et int) as $$
DECLARE iw INT;
begin
WITH RECURSIVE cte AS (
SELECT l.room_id, l.weekday, l.start_time, l.end_time
FROM learningactivity l
UNION ALL
SELECT l.room_id, l.weekday, c.start_time, l.end_time
FROM cte c
JOIN learningactivity l ON l.room_id = c.room_id
AND l.weekday = c.weekday
AND l.start_time = c.end_time
)
SELECT DISTINCT ON (1, 2)
c.room_id as room, iweekday(c.weekday) AS iw, extract (epoch from c.start_time/3600) as st, extract (epoch from c.end_time/3600) as et
FROM cte c
ORDER BY 1, 2, st - et;
end;
$$ language plpgsql;
select * from fourpointnine()
but i get this error:
ERROR: query has no destination for result data
HINT: If you want to discard the results of a SELECT, use PERFORM instead.
CONTEXT: PL/pgSQL function iweekday(character varying) line 3 at SQL statement
This is because you have run queries in your function, but you haven't done anything with the result. You need to have a RETURN clause in there with the resultset, or specifically, RETURN QUERY.
See the PostgreSQL for returning a result from a function: https://www.postgresql.org/docs/12/plpgsql-control-structures.html#PLPGSQL-STATEMENTS-RETURNING
For example:
CREATE OR REPLACE FUNCTION fourpointnine()
RETURNS TABLE (room varchar(7), iw int, st int, et int) AS $$
DECLARE iw INT;
BEGIN
RETURN QUERY WITH RECURSIVE cte AS (
SELECT l.room_id, l.weekday, l.start_time, l.end_time
FROM learningactivity l
UNION ALL
SELECT l.room_id, l.weekday, c.start_time, l.end_time
FROM cte c
JOIN learningactivity l ON l.room_id = c.room_id
AND l.weekday = c.weekday
AND l.start_time = c.end_time
)
SELECT DISTINCT ON (1, 2)
c.room_id AS room, iweekday(c.weekday) AS iw, extract (epoch FROM c.start_time/3600) AS st, extract (epoch FROM c.end_time/3600) AS et
FROM cte c
ORDER BY 1, 2, st - et;
END;
$$ LANGUAGE plpgsql;
I have multiple tables with each two rows of interest: connection_node_start_id and connection_node_end_id. My goal is to get a collection of all those IDs, either as a flat ARRAY or as a new TABLE consisting of one row.
Example output ARRAY:
result = {1,4,7,9,2,5}
Example output TABLE:
IDS
-------
1
4
7
9
2
5
My fist attempt is somewhat clumsy and does not work properly as the SELECT statement just returns one row. It seems there must be a simple way to do this, can someone point me into the right direction?
CREATE OR REPLACE FUNCTION get_connection_nodes(anyarray)
RETURNS anyarray AS
$$
DECLARE
table_name varchar;
result integer[];
sel integer[];
BEGIN
FOREACH table_name IN ARRAY $1
LOOP
RAISE NOTICE 'table_name(%)',table_name;
EXECUTE 'SELECT ARRAY[connection_node_end_id,
connection_node_start_id] FROM ' || table_name INTO sel;
RAISE NOTICE 'sel(%)',sel;
result := array_cat(result, sel);
END LOOP;
RETURN result;
END
$$
LANGUAGE 'plpgsql';
Test table:
connection_node_start_id | connection_node_end_id
--------------------------------------------------
1 | 4
7 | 9
Call:
SELECT get_connection_nodes(ARRAY['test_table']);
Result:
{1,4} -- only 1st row, rest is missing
For Postgres 9.3+
CREATE OR REPLACE FUNCTION get_connection_nodes(text[])
RETURNS TABLE (ids int) AS
$func$
DECLARE
_tbl text;
BEGIN
FOREACH _tbl IN ARRAY $1
LOOP
RETURN QUERY EXECUTE format('
SELECT t.id
FROM %I, LATERAL (VALUES (connection_node_start_id)
, (connection_node_end_id)) t(id)'
, _tbl);
END LOOP;
END
$func$ LANGUAGE plpgsql;
Related answer on dba.SE:
SELECT DISTINCT on multiple columns
Or drop the loop and concatenate a single query. Probably fastest:
CREATE OR REPLACE FUNCTION get_connection_nodes2(text[])
RETURNS TABLE (ids int) AS
$func$
BEGIN
RETURN QUERY EXECUTE (
SELECT string_agg(format(
'SELECT t.id FROM %I, LATERAL (VALUES (connection_node_start_id)
, (connection_node_end_id)) t(id)'
, tbl), ' UNION ALL ')
FROM unnest($1) tbl
);
END
$func$ LANGUAGE plpgsql;
Related:
Loop through like tables in a schema
LATERAL was introduced with Postgres 9.3.
For older Postgres
You can use the set-returning function unnest() in the SELECT list, too:
CREATE OR REPLACE FUNCTION get_connection_nodes2(text[])
RETURNS TABLE (ids int) AS
$func$
BEGIN
RETURN QUERY EXECUTE (
SELECT string_agg(
'SELECT unnest(ARRAY[connection_node_start_id
, connection_node_end_id]) FROM ' || tbl
, ' UNION ALL '
)
FROM (SELECT quote_ident(tbl) AS tbl FROM unnest($1) tbl) t
);
END
$func$ LANGUAGE plpgsql;
Should work with pg 8.4+ (or maybe even older). Works with current Postgres (9.4) as well, but LATERAL is much cleaner.
Or make it very simple:
CREATE OR REPLACE FUNCTION get_connection_nodes3(text[])
RETURNS TABLE (ids int) AS
$func$
BEGIN
RETURN QUERY EXECUTE (
SELECT string_agg(format(
'SELECT connection_node_start_id FROM %1$I
UNION ALL
SELECT connection_node_end_id FROM %1$I'
, tbl), ' UNION ALL ')
FROM unnest($1) tbl
);
END
$func$ LANGUAGE plpgsql;
format() was introduced with pg 9.1.
Might be a bit slower with big tables because each table is scanned once for every column (so 2 times here). Sort order in the result is different, too - but that does not seem to matter for you.
Be sure to sanitize escape identifiers to defend against SQL injection and other illegal syntax. Details:
Table name as a PostgreSQL function parameter
The EXECUTE ... INTO statement can only return data from a single row:
If multiple rows are returned, only the first will be assigned to the INTO variable.
In order to concatenate values from all rows you have to aggregate them first by column and then append the arrays:
EXECUTE 'SELECT array_agg(connection_node_end_id) ||
array_agg(connection_node_start_id) FROM ' || table_name INTO sel;
You're probably looking for something like this:
CREATE OR REPLACE FUNCTION d (tblname TEXT [])
RETURNS TABLE (c INTEGER) AS $$
DECLARE sql TEXT;
BEGIN
WITH x
AS (SELECT unnest(tblname) AS tbl),
y AS (
SELECT FORMAT('
SELECT connection_node_end_id
FROM %s
UNION ALL
SELECT connection_node_start_id
FROM %s
', tbl, tbl) AS s
FROM x)
SELECT string_agg(s, ' UNION ALL ')
INTO sql
FROM y;
RETURN QUERY EXECUTE sql;
END;$$
LANGUAGE plpgsql;
CREATE TABLE a (connection_node_end_id INTEGER, connection_node_start_id INTEGER);
INSERT INTO A VALUES (1,2);
CREATE TABLE b (connection_node_end_id INTEGER, connection_node_start_id INTEGER);
INSERT INTO B VALUES (100, 101);
SELECT * from d(array['a','b']);
c
-----
1
2
100
101
(4 rows)
I need to write a function that returns a table with unknown number of columns.
If i receive 'None' in column input parameter then that column shouldn't be included in the output. In postgres 9+ there is a solution for this problem.
something like below:
CREATE OR REPLACE FUNCTION data_of(id integer,col1 varchar,col2 varchar, col3 varchar)
RETURNS TABLE (count_rec, dimensions text[] ) AS
$func$
DECLARE
_dimensions text := 'col1, col2, col3'; -- If i receive 'None' in input param then i exclude that from column list
BEGIN
RETURN QUERY EXECUTE format('
SELECT count(*) as count_rec,
string_to_array($1) -- AS dimensions
FROM x
WHERE id = $2'
, _dimensions)
USING _dimensions , _id;
END
$func$ LANGUAGE plpgsql;
But in Greenplum (Postgres 8.2) i could not find any. Is there any similar solution?
thanks
You have 2 options to do it: use set-returning function returning "record" or returning your custom type.
First option:
create table test (a int, b int, c int, d varchar, e varchar, f varchar);
insert into test select id, id*2, id*3, (id*4)::varchar, (id*4)::varchar, (id*4)::varchar from generate_series(1,10) id;
create or replace function test_func(column_list varchar[]) returns setof record as $BODY$
declare
r record;
begin
for r in execute 'select ' || array_to_string(column_list, ',') || ' from test' loop
return next r;
end loop;
return;
end;
$BODY$
language plpgsql
volatile;
select * from test_func(array['a','c','e']) as f(a int, c int, e varchar);
Second option:
create table test (a int, b int, c int, d varchar, e varchar, f varchar);
insert into test select id, id*2, id*3, (id*4)::varchar, (id*4)::varchar, (id*4)::varchar from generate_series(1,10) id;
create type testtype as (
a int,
c int,
e varchar
);
create or replace function test_func() returns setof testtype as $BODY$
declare
r testtype;
begin
for r in execute 'select a,c,e from test' loop
return next r;
end loop;
return;
end;
$BODY$
language plpgsql
volatile;
select * from test_func();
But I'm 99% sure you're trying to do something wrong. In Greenplum the result of function execution cannot be used as a "table" in join conditions, because the function executes on the master. You even won't be able to create a table out of the last query returning the data from your function because of this limitation
In short, this is not a recommended way to work with data in Greenplum
Can we able to use SELECT statement within CASE conditional expression in function?
I have tried the following function to accomplish the above task.
Example: I have a function which is used to display table containing some rows.
create or replace function test(n integer)
returns table (name text,city text) as
$body$
begin
case n when 1 then
select * from table1
when 2 then
select * from table2
when 3 then
select * from view1
end;
end;
$body$
language plpgsql;
--Calling function
select * from test(1);
/*have to show details of table1*/
select * from test(2);
/*have to show details of table2*/
select * from test(3);
/*have to display details of view1*/
Actually, there is a plpgsql CASE statement, not to be confused with the SQL CASE expression:
CREATE OR REPLACE function test(n integer)
RETURNS TABLE (name text, city text) AS
$func$
BEGIN
CASE n
WHEN 1 THEN
RETURN QUERY SELECT t.name, t.city FROM table1 t;
WHEN 2 THEN
RETURN QUERY SELECT t.foo, t.bar FROM table2 t;
WHEN 3 THEN
RETURN QUERY SELECT t.bar, t.bamm FROM view1 t;
END CASE;
END
$func$ LANGUAGE plpgsql;
If you declare your function as RETURNS TABLE (name text, city text), then your SELECT statements should have a column list with matching types.
If on the other hand you want to SELECT *, declare the function as RETURNS SETOF table1 accordingly.
When naming columns in the return type, those variable are visible in the function body. Be sure to table-qualify column names that would conflict with same names. So t.name instead of just name.
Column names from queries inside the function are not visible outside. Only the declared return type. So names don't have to match, just data types.
Either way, I suggest to simplify things:
CREATE OR REPLACE function test(n integer)
RETURNS SETOF table1 AS
$func$
SELECT * FROM table1 t WHERE n = 1
UNION ALL
SELECT * FROM table2 t WHERE n = 2
UNION ALL
SELECT * FROM view1 t WHERE n = 3;
$func$ LANGUAGE sql;
Same result. Just as fast. SQL or PL/pgSQL function is a matter of taste and some other details. PL/pgSQL is probably faster for stand-alone calls. SQL can more easily be nested.
For this you need an IF statement and RETURN QUERY, e.g.
create or replace function test(n integer)
returns table (name text,city text) as
$body$
begin
IF n = 1 THEN
RETURN QUERY select * from table1;
ELIF n = 2 THEN
RETURN QUERY select * from table2;
ELIF n = 3 THEN
RETURN QUERY select * from view1;
END IF;
end;
$body$
language plpgsql;
It's a very weird thing to want to do, though.
The multirow subselect will be used in the right hand side of the in operator in the where clause:
create table t (a integer);
insert into t (a) values (1), (9);
drop function if exists f();
create function f()
returns void as $$
begin
execute '
select a
from t
where a in $1
' using (select 1 union select 2);
end;$$
language plpgsql;
select f();
ERROR: more than one row returned by a subquery used as an expression
CONTEXT: SQL statement "SELECT (select 1 union select 2)"
PL/pgSQL function "f" line 3 at EXECUTE statement
How to achieve what the above function would if it worked?
I see nothing in your question that couldn't more easily be solved with:
SELECT a
FROM t
JOIN (VALUES (1), (2)) x(a) USING (a); -- any query returning multiple int
Can you clarify the necessity in your example?
As a proof of concept, this would be simpler / faster:
CREATE OR REPLACE FUNCTION x.f1()
RETURNS SETOF integer AS
$BODY$
BEGIN
RETURN QUERY EXECUTE '
SELECT a
FROM t
WHERE a = ANY($1)'
USING ARRAY(VALUES (1), (2)); -- any query here
END;
$BODY$
LANGUAGE plpgsql;
Performance of IN () and = ANY()
Your observation is spot on. And there is reason for it. Try:
EXPLAIN ANALYZE SELECT * FROM tbl WHERE id IN (1,2,3);
The query plan will reveal:
Index Cond: (id = ANY ('{1,2,3}'::integer[]))
PostgreSQL transforms the id IN (..) construct into id = ANY(..) internally. These two perform identically - except for a negligible overhead penalty.
My code is only faster in building the statement - exactly as you diagnosed.
create function f()
returns setof integer as $$
begin
return query execute format('
select a
from t
where a in %s
', replace(replace(array(select 1 union select 2)::text, '{', '('), '}', ')'));
end$$
language plpgsql;