Postgres function with list argument and in clause - sql

How to create a function which takes as argument integer[] parameter and executing query with IN clause with this parameter in loop.
In loop I want execute next select and result of this query I would like return.
Something like that:
CREATE OR REPLACE FUNCTION function_which_i_want(my_argument integer[]) RETURNS my_schema_and_table[] AS
$BODY$
DECLARE
result my_schema_and_table[];
BEGIN
FOR l IN SELECT * FROM table2 WHERE id in (my_argument) LOOP
SELECT * FROM my_schema_and_table;
END LOOP;
END;
...
I want to get union of each select in loop. one huge joined result.
Is this possible? Please help.

PL/pgSQL function
It could look like this:
CREATE OR REPLACE FUNCTION func1(_arr integer[])
RETURNS SETOF my_schema_and_table
LANGUAGE plpgsql AS
$func$
DECLARE
l record;
BEGIN
FOR l IN
SELECT *
FROM lookup_table
WHERE some_id = ANY(_arr)
LOOP
RETURN QUERY
SELECT *
FROM my_schema_and_table
WHERE link_id = l.link_id;
END LOOP;
END
$func$;
Assuming you actually want a SET of rows from your my_schema_and_table, not an array? To return the result of SELECT * FROM my_schema_and_table, declare the function as RETURNS SETOF my_schema_and_table
Rewrite the IN construct to = ANY(_arr). That's the way to use an array parameter directly. Logically equivalent.
Or use unnest() and join to the resulting table like #Clodoaldo demonstrates. That can be faster with long arrays.
Simplify to plain SQL function
This simple SQL function does the same:
CREATE OR REPLACE FUNCTION func2(_arr integer[])
RETURNS SETOF my_schema_and_table
LANGUAGE sql AS
$func$
SELECT t.*
FROM (SELECT unnest($1) AS some_id) x
JOIN lookup_table l USING (some_id)
JOIN my_schema_and_table t USING (link_id);
$func$
Assuming both tables have link_id.
Call:
SELECT * FROM func2('{21,31}'::int[]);

CREATE OR REPLACE FUNCTION function_which_i_want(my_argument integer[])
RETURNS my_schema_and_table[] AS
$BODY$
DECLARE
result my_schema_and_table[];
BEGIN
for l in
select t.*
from
table2 t
inner join
unnest(my_argument) m(id) on m.id = t.id
loop
SELECT * FROM my_schema_and_table;
END LOOP;
END;

Related

Multiple ALTER TABLE ADD COLUMN in one SQL function call

I came across some weird behaviour I'd like to understand.
I create a plpgsql function doing nothing except of ALTER TABLE ADD COLUMN. I call it 2 times on the same table:
A) In a single SELECT sentence
B) In a SQL function with same SELECT as in A)
Results are different: A) creates two columns, while B) creates only one column. Why?
Code:
CREATE FUNCTION add_text_column(table_name text, column_name text) RETURNS VOID
LANGUAGE plpgsql
AS $fff$
BEGIN
EXECUTE '
ALTER TABLE ' || table_name || '
ADD COLUMN ' || column_name || ' text;
';
END;
$fff$
;
-- this function is called only in B
CREATE FUNCTION add_many_text_columns(table_name text) RETURNS VOID
LANGUAGE SQL
AS $fff$
WITH
col_names (col_name) AS (
VALUES
( 'col_1' ),
( 'col_2' )
)
SELECT add_text_column(table_name, col_name)
FROM col_names
;
$fff$
;
-- A)
CREATE TABLE a (id integer);
WITH
col_names (col_name) AS (
VALUES
( 'col_1' ),
( 'col_2' )
)
SELECT add_text_column('a', col_name)
FROM col_names
;
SELECT * FROM a;
-- B)
CREATE TABLE b (id integer);
SELECT add_many_text_columns('b');
SELECT * FROM b;
Result:
CREATE FUNCTION
CREATE FUNCTION
CREATE TABLE
add_text_column
-----------------
(2 rows)
id | col_1 | col_2
----+-------+-------
(0 rows)
CREATE TABLE
add_many_text_columns
-----------------------
(1 row)
id | col_1
----+-------
(0 rows)
I'm using PostgreSQL 10.4. Please note that this is only a minimal working example, not the full functionality I need.
CREATE OR REPLACE FUNCTION g(i INTEGER)
RETURNS VOID AS $$
BEGIN
RAISE NOTICE 'g called with %', i;
END
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION t(i INTEGER)
RETURNS VOID AS $$
SELECT g(id)
FROM generate_series(1, i) id;
$$ LANGUAGE SQL;
What do you think happens when I run SELECT t(4)? The only statement printed from g() is g called with 1.
The reason for this is your add_many_text_columns function returns a single result (void). Because it's SQL and is simply returning the result of a SELECT statement, it seems to stop executing after getting the first result, which makes sense if you think of it - it can only return one result after all.
Now change the function to:
CREATE OR REPLACE FUNCTION t(i INTEGER)
RETURNS SETOF VOID AS $$
SELECT g(id)
FROM generate_series(1, i) id;
$$ LANGUAGE SQL;
And run SELECT t(4) again, and now this is printed:
g called with 1
g called with 2
g called with 3
g called with 4
Because the function now returns SETOF VOID, it doesn't stop after the first result and executes it fully.
So back to your functions, you could change your SQL function to return SETOF VOID, but it doesn't really make much sense - better I think to change it to plpgsql and have it do a PERFORM:
CREATE OR REPLACE FUNCTION t(i INTEGER)
RETURNS VOID AS $$
BEGIN
PERFORM g(id)
FROM generate_series(1, i) id;
END
$$ LANGUAGE plpgsql;
That will execute the statement fully and it still returns a single VOID.
eurotrash provided a good explanation.
Alternative solution 1
CREATE OR REPLACE FUNCTION t(i INTEGER)
RETURNS VOID AS
$func$
SELECT g(id)
FROM generate_series(1, i) id;
SELECT null::void;
$func$ LANGUAGE sql;
Because, quoting the manual:
SQL functions execute an arbitrary list of SQL statements, returning
the result of the last query in the list. In the simple (non-set)
case, the first row of the last query's result will be returned.
By adding a dummy SELECT at the end we avoid that Postgres stops after processing the the first row of the query with multiple rows.
Alternative solution 2
CREATE OR REPLACE FUNCTION t(i INTEGER)
RETURNS VOID AS
$func$
SELECT count(g(id))
FROM generate_series(1, i) id;
$func$ LANGUAGE sql;
By using an aggregate function, all underlying rows are processed in any case. The function returns bigint (that's what count() returns), so we get the number of rows as result.
Alternative solution 3
If you need to return void for some unknown reason, you can cast:
CREATE OR REPLACE FUNCTION t(i INTEGER)
RETURNS VOID AS
$func$
SELECT count(g(id))::text::void
FROM generate_series(1, i) id;
$func$ LANGUAGE sql;
The cast to text is a stepping stone because the cast from bigint to void is not defined.

Postgres: Conditional statement in with clause

I am trying to migrate Oracle to Postgres.
Wherever in Oracle we have bulk collect function, I have created plain SQL function:
like
CREATE OR REPLACE FUNCTION get_pub_access_tax(a_person_id mobile_user.id%TYPE)
RETURNS TABLE (tax_id integer) AS
$$
WITH v_pub_tax AS (SELECT tax_id FROM mobile_pub_tax)
select tax_id from v_pub_tax ;
$$ LANGUAGE sql;
But there is a scenario in Oracle where data are fetched on conditional basis: (see below example)
FUNCTION get_st_tax(
a_st_type IN VARCHAR2)
RETURN mmt_array
IS
v_tax_list mmt_array := mmt_array();
BEGIN
IF a_st_type = 'top-stories' THEN
SELECT t.tax_id BULK COLLECT INTO v_tax_list
FROM mobile_st_tax t;
RETURN v_tax_list;
ELSE
SELECT t.tax_id BULK COLLECT INTO v_tax_list
FROM mobile_st_tax t WHERE t.stream_type=a_stream_type;
RETURN v_tax_list;
END IF;
END;
How can we convert above Oracle code to Postgres?
I tried using if condition but getting syntax error(below code).
Below code I have tried executing in Postgres, which gives me syntax error.
CREATE OR REPLACE FUNCTION get_st_tax ( a_st_type IN VARCHAR)
RETURNS TABLE (tax_id integer) AS
$$
IF a_stream_type = 'top-stories' THEN
WITH v_tax_list AS (
SELECT t.tax_id FROM mobile_st_tax t)
SELECT tax_id FROM v_tax_list;
ELSE
WITH v_tax_list AS (
SELECT t.tax_id FROM mobile_st_tax t WHERE t.stream_type=a_stream_type)
SELECT tax_id FROM v_tax_list;
END IF;
$$ LANGUAGE SQL;
Kindly help.
The second function can be done in plain sql:
create function get_st_tax(a_st_type text)
returns int[] as $$
select array_agg(t.tax_id)
from mobile_st_tax t
where
t.stream_type = a_stream_type and a_st_type <> 'top_stories'
or
a_st_type = 'top-stories'
;
$$ language sql;

How to declare returning type for function, returning unnamed table

I want to create a function, that returns a table with 2 columns:
i integer -- or bigint?
arr integer[] -- array of integer
What should I write instead of ??? in this function:
CREATE OR REPLACE FUNCTION test()
RETURNS ???
LANGUAGE plpgsql
AS $$
BEGIN
RETURN QUERY (
SELECT i, (ARRAY[11,22,33])[i]
FROM generate_series(
1,
array_upper(ARRAY[11,22,33],1)
) i
);
END;
$$;
see the answer for this question,
example of function return table:
CREATE OR REPLACE FUNCTION foo(a int)
RETURNS TABLE(b int, c int) AS $$
BEGIN
RETURN QUERY SELECT i, i+1 FROM generate_series(1, a) g(i);
END;
$$ LANGUAGE plpgsql;
You don't need plpgsql for this. A simple SQL function would do the job:
CREATE OR REPLACE FUNCTION test(_arr anyarray)
RETURNS TABLE (idx int, elem anyelement)
$func$
SELECT i, _arr[i] FROM generate_subscripts(_arr, 1) i
$func$ LANGUAGE sql AS
Call:
SELECT * FROM test(ARRAY[11,22,33]::int[]); -- Cast to declare type for literals
The polymorphic parameter anyarray works for arrays of any base type.
How to write a function that returns text or integer values?
Use generate_subscripts() to simplify the task.
More about returning from a function:
How to return result of a SELECT inside a function in PostgreSQL?
Postgres 9.4
There is a shiny new trick in upcoming Postgres 9.4: WITH ORDINALITY. Details in this related answer:
PostgreSQL unnest() with element number
Simplify to (no additional function):
SELECT * FROM unnest(ARRAY[11,22,33]::int[]) WITH ORDINALITY AS x (elem, idx)

Return a query from a function?

I am using PostgreSQL 8.4 and I want to create a function that returns a query with many rows.
The following function does not work:
create function get_names(varchar) returns setof record AS $$
declare
tname alias for $1;
res setof record;
begin
select * into res from mytable where name = tname;
return res;
end;
$$ LANGUAGE plpgsql;
The type record only allows single row.
How to return an entire query? I want to use functions as query templates.
CREATE OR REPLACE FUNCTION get_names(_tname varchar)
RETURNS TABLE (col_a integer, col_b text) AS
$func$
BEGIN
RETURN QUERY
SELECT t.col_a, t.col_b -- must match RETURNS TABLE
FROM mytable t
WHERE t.name = _tname;
END
$func$ LANGUAGE plpgsql;
Call like this:
SELECT * FROM get_names('name')
Major points:
Use RETURNS TABLE, so you don't have to provide a list of column names with every call.
Use RETURN QUERY, much simpler.
Table-qualify column names to avoid naming conflicts with identically named OUT parameters (including columns declared with RETURNS TABLE).
Use a named variable instead of ALIAS. Simpler, doing the same, and it's the preferred way.
A simple function like this could also be written in LANGUAGE sql:
CREATE OR REPLACE FUNCTION get_names(_tname varchar)
RETURNS TABLE (col_a integer, col_b text) AS
$func$
SELECT t.col_a, t.col_b --, more columns - must match RETURNS above
FROM mytable t
WHERE t.name = $1;
$func$ LANGUAGE sql;

How do I pass in the array output of SQL query into a PostgreSQL (PL/pgSQL) function?

I am able to do the following in SQL where an "array" of user_ids are passed into the where clause of a SQL query.
select * from users where id in (select user_id from profiles);
I would like to do the same thing but pass the "array" into a PostgreSQL (PL/pgSQL) function as shown below. How do I declare the function and work with the "array" within the function?
select * from users_function(select user_id from profiles);
CREATE OR REPLACE FUNCTION users_function(....)
RETURNS void AS
$BODY$
....
Declare an array datatype [] in the function then use the aggregate function array_agg to transform the select statement into an array.
CREATE OR REPLACE FUNCTION users_function(myints integer[])
$$
BEGIN
-- you need to find the bounds with array_lower and array_upper
FOR i in array_lower(myints, 1) .. array_upper(myints, 1) LOOP
Raise Notice '%', myints[i]::integer;
END LOOP;
END;
$$
select * from users_function(array_agg((select user_id from profiles)));
I could not get the nate c's array_agg approach as I described above. This is an option:
select * from test_array('{1,2}');
CREATE OR REPLACE FUNCTION test_array(user_ids integer[])
RETURNS void AS
$$
declare
begin
FOR i in array_lower(user_ids, 1) .. array_upper(user_ids, 1) LOOP
RAISE NOTICE '%', user_ids[i]::integer;
END LOOP;
end
$$
LANGUAGE plpgsql;