How return dynamic number of columns in function? - sql

In PostgreSQL 11 database I have table with 6 column. Next function return static number of defined columns.
CREATE FUNCTION CALCULATION(INTEGER)
RETURNS TABLE(
ORGANIZATION_ID INT4,
ORGANIZATION_NAME VARCHAR,
ORGANIZATION_RANG INT4,
PARENT_ORGANIZATION_ID INT4,
PARENT_ORGANIZATION_NAME VARCHAR,
PARENT_ORGANIZATION_RANG INT4
) AS $$
SELECT * FROM ANALYTICS;
$$ LANGUAGE SQL;
How can I make an SQL function in Postgres 11 which return a result set with dynamic number of columns according to a parameter passed in?
For example if I call SELECT * FROM CALCULATION(2);, function return first 2 columns.
If this is not possible with an SQL function, is it possible with a PL/pgSQL function?

This is possible for RECORD returning functions.
CREATE FUNCTION calculation(how_many integer) RETURNS SETOF RECORD
LANGUAGE plpgsql
AS $fff$
BEGIN
IF how_many = 1
THEN RETURN QUERY SELECT 'foo'::text;
ELSIF how_many = 2
THEN RETURN QUERY SELECT 'foo'::text, 'bar'::text;
END IF;
END;
$fff$
;
And now you can do:
jbet=> SELECT * FROM calculation(1) AS f(first_col text);
first_col
-----------
foo
(1 row)
jbet=> SELECT * FROM calculation(2) AS f(first_col text, second_col text);
first_col | second_col
-----------+------------
foo | bar
(1 row)
The very serious downside is that each time you call the function you have to define set of returned columns, so I don't think you'll find this answer useful : )
Anyway, Postgresql needs to know returned type of each SELECT before it runs the query, so one or other way you have to define the columns.
JSON return value could be a reasonable answer if you just want the data and don't care if there are separate columns or not.

Backing up a step, why not use a standard select to get the columns you want from your set-returning function?
select organization_name,
organization_rang,
parent_organization_name,
parent_organization_rang
from calculation();
That's easy to follow and flexible. I'm guessing that you've written a simplified example and have a good reason for what you're asking...but I figured I'd double-check.

Related

Wrap PostgreSQL functions in another to conditionally combine results

I have a function get_oversight(int) that returns a single column:
person_id
----------
100
101
102
103
104
And another function get_unfiltered_responsibility(int) that returns the same structure:
person_id
----------
100
103
104
I need a 3rd function that evaluates and returns a subset of the above. Here's some pseudo code:
def function get_responsibility(person_id int):
oversight = get_oversight(person_id)
unfiltered_responsibility = get_responsibility(person_id)
if number_of_records(unfiltered_responsibility) == 0:
return oversight
else
return intersection(unfiltered_responsibility, oversight)
# only ids from unfiltered_responsibility that are ALSO IN oversight
What would that 3rd function look like? (using v9.6)
Assuming that both functions never return duplicates. Else you'll have to define exactly how to deal with those.
In a plpgsql function you can conveniently use the special variable FOUND
CREATE OR REPLACE FUNCTION get_combo_plpgsql(int)
RETURNS TABLE(person_id int) LANGUAGE plpgsql AS
$func$
BEGIN
RETURN QUERY
SELECT *
FROM get_oversight($1)
JOIN get_unfiltered_responsibility($1) USING (person_id);
IF NOT FOUND THEN
RETURN QUERY
SELECT * FROM get_oversight($1);
END IF;
END
$func$;
This only works on the assumption that get_unfiltered_responsibility() always returns a subset of get_oversight(), like your sample data seems to suggest. Then, if the join returns no rows, we can conclude that get_unfiltered_responsibility() came up empty.
Related:
Why is IS NOT NULL false when checking a row type?
Alternatively, this CTE wrapped in a simple SQL function works in any case, subset or not (can be a plpgsql function too, if needed):
CREATE OR REPLACE FUNCTION get_combo_sql(int)
RETURNS TABLE(person_id int) LANGUAGE sql AS
$func$
WITH cte AS (SELECT * FROM get_unfiltered_responsibility($1))
SELECT *
FROM get_oversight($1) o
WHERE EXISTS (
SELECT FROM cte
WHERE person_id = o.person_id
)
OR NOT EXISTS (TABLE cte)
$func$;
Related:
Is there a shortcut for SELECT * FROM?
db<>fiddle here

PL/PGSQL always returns array or list of arrays

Given the simple pl/pgsql function:
CREATE OR REPLACE FUNCTION foo (point geometry
, OUT _street text
, OUT _gid int
, OUT distance real)
AS $$
BEGIN
SELECT min(distance(point,geom)) as dist, gid, name into distance, _gid, _street
from streets
where geometria && Expand(point,0.001) group by gid, name order by dist limit 1;
END;
$$ LANGUAGE plpgsql;
results in something akin to:
geobase=# select foo(GeomFromText('POINT(-99.124191496999 19.3490666368031)',4326));
foo
-------------------------------------------------
("PASEO DE LOS FRAMBOYANES",345483,0.000118338)
Which is fine, except for the fact that I would expect something more akin to this:
_street | _gid | distance
--------------------------+--------+-------------
PASEO DE LOS FRAMBOYANES | 345483 | 0.000118338
I have tried variants with the RETURN clause, defining it as a rowtype, record and even table, but I always get a tuple or array as indicated in the example. Any clues as to how to have the result in a way similar to a table?
The question asked has been answered many times. To decompose the returned row type:
SELECT * FROM foo( ... );
Like here:
How to get individual columns from table returned from a function?
But there is more.
Your function with less confusing syntax, less error prone and simplified:
CREATE OR REPLACE FUNCTION foo (_point geometry)
RETURNS TABLE (street text, gid int, distance real)
LANGUAGE plpgsql AS
$func$
BEGIN
RETRUN QUERY
SELECT s.name, s.gid, distance(_point, s.geom)
FROM streets s
WHERE s.geometria && expand(_point, 0.001)
ORDER BY 3
LIMIT 1;
END
$func$;
Since you have ORDER BY ... LIMIT 1, you do not need min() and GROUP BY at all.
Note how I table-qualified all columns to avoid naming conflicts.
There is one subtle difference: If the query finds no row your original returns a row with NULL values instead, while my function actually returns no row. Typically, you'll want the latter.
The same as equivalent simple SQL function:
CREATE OR REPLACE FUNCTION foo (_point geometry)
RETURNS TABLE (street text, gid int, distance real)
LANGUAGE sql AS
$func$
SELECT s.name, s.gid, distance(_point, s.geom)
FROM streets s
WHERE s.geometria && expand(_point, 0.001)
ORDER BY 3
LIMIT 1;
$func$;
Still not very efficient with big tables. You'll be interested in KNN (k-nearest neighbour) search in PostGis:
Multicolumn index on 3 fields with heterogenous data types
Improving performance with a Similarity Postgres fuzzy self join query
try select * from foo(GeomFromText('POINT(-99.124191496999 19.3490666368031)',4326)); ?..

Shouldn't this PostgreSQL function return zero rows?

Given the schema
CREATE TABLE users (
id bigserial PRIMARY KEY,
email varchar(254) NOT NULL
);
CREATE UNIQUE INDEX on users (lower(email));
CREATE FUNCTION all_users() RETURNS users AS $$
SELECT * FROM users;
$$ LANGUAGE SQL STABLE;
, shouldn't SELECT * FROM all_users() (assuming the users table is empty) return no rows, not a row with all null values?
See the SQL Fiddle here: http://sqlfiddle.com/#!15/b5ba8/2
That's because your function is broken by design. It should be:
CREATE FUNCTION all_users() RETURNS SETOF users AS
'SELECT * FROM users' LANGUAGE sql STABLE;
Or alternatively, the more flexible form RETURNS TABLE (...) like #Clodoaldo posted. But it's generally wiser to use RETURNS SETOF users for a query with SELECT * FROM users.
Your original function always returns a single value (a composite type), it has been declared that way. It will break in a more spectacular fashion if you insert some rows.
Consider this SQL Fiddle demo.
For better understanding, your function call does the same as this plain SELECT query:
SELECT (SELECT u from users u).*;
Returns:
id | email
-------+------
<NULL> | <NULL>
The difference: Plain SQL will raise an exception if the subquery returns more than one row, while a function will just return the first row and discard the rest.
As always, details in the manual.
Your function returns records. So it must return at least one record. If you want an empty result set do return a table:
CREATE or replace FUNCTION all_users()
RETURNS table (id bigint, email varchar(254)) AS $$
SELECT id, email FROM users;
$$ LANGUAGE SQL STABLE;

Retrieving more than one value from the function

TableA
Id imge
-- ----
1 1.jpeg
2 2.jpeg
1 1.jpeg
1 1.jpeg
o/p needed
id image
------------
1 1.jpeg
1.jpeg
1.jpeg
I created a function,
create or replace function(vid in integer,vimg out varchar) returns setof record as
$$
declare
im varchar;
begin
select image into im from tablea wher id=$1;
return query
select im;
$$
end;
plpgsql
But it's not working. I need to retrieve the images without using the arrays and loops.
You are declaring your function as setof record meaning that it will return any number of rows spanning. You need to redeclare the function and change internal select's to match returning type.
Or I'm wrong and I just miss what you are trying to do.
I think simple function like this is better to write in language sql instead of plpgsql:
create or replace function func(vid in integer)
returns table(vimg varchar)
as
$$
select imge from tablea where id=$1;
$$ language sql;
Anyway, to return multiple records from function your can return either table or setof record.
sql fiddle demo
You might be looking for GROUP_CONCAT()
SELECT GROUP_CONCAT(imge) as images
FROM TableA GROUP BY Id;
Oh I missed. You were in PostgreSQL, huh?
No worries. There is an equivalent for group_concat in PostgreSQL: array_agg
SELECT id, array_agg(imge)
FROM TableA GROUP BY Id;

Passing a ResultSet into a Postgresql Function

Is it possible to pass the results of a postgres query as an input into another function?
As a very contrived example, say I have one query like
SELECT id, name
FROM users
LIMIT 50
and I want to create a function my_function that takes the resultset of the first query and returns the minimum id. Is this possible in pl/pgsql?
SELECT my_function(SELECT id, name FROM Users LIMIT 50); --returns 50
You could use a cursor, but that very impractical for computing a minimum.
I would use a temporary table for that purpose, and pass the table name for use in dynamic SQL:
CREATE OR REPLACE FUNCTION f_min_id(_tbl regclass, OUT min_id int) AS
$func$
BEGIN
EXECUTE 'SELECT min(id) FROM ' || _tbl
INTO min_id;
END
$func$ LANGUAGE plpgsql;
Call:
CREATE TEMP TABLE foo ON COMMIT DROP AS
SELECT id, name
FROM users
LIMIT 50;
SELECT f_min_id('foo');
Major points
The first parameter is of type regclass to prevent SQL injection. More info in this related answer on dba.SE.
I made the temp table ON COMMIT DROP to limit its lifetime to the current transaction. May or may not be what you want.
You can extend this example to take more parameters. Search for code examples for dynamic SQL with EXECUTE.
-> SQLfiddle demo
I would take the problem on the other side, calling an aggregate function for each record of the result set. It's not as flexible but can gives you an hint to work on.
As an exemple to follow your sample problem:
CREATE OR REPLACE FUNCTION myMin ( int,int ) RETURNS int AS $$
SELECT CASE WHEN $1 < $2 THEN $1 ELSE $2 END;
$$ LANGUAGE SQL STRICT IMMUTABLE;
CREATE AGGREGATE my_function ( int ) (
SFUNC = myMin, STYPE = int, INITCOND = 2147483647 --maxint
);
SELECT my_function(id) from (SELECT * FROM Users LIMIT 50) x;
It is not possible to pass an array of generic type RECORD to a plpgsql function which is essentially what you are trying to do.
What you can do is pass in an array of a specific user defined TYPE or of a particular table row type. In the example below you could also swap out the argument data type for the table name users[] (though this would obviously mean getting all data in the users table row).
CREATE TYPE trivial {
"ID" integer,
"NAME" text
}
CREATE OR REPLACE FUNCTION trivial_func(data trivial[])
RETURNS integer AS
$BODY$
DECLARE
BEGIN
--Implementation here using data
return 1;
END$BODY$
LANGUAGE 'plpgsql' VOLATILE;
I think there's no way to pass recordset or table into function (but I'd be glad if i'm wrong). Best I could suggest is to pass array:
create or replace function my_function(data int[])
returns int
as
$$
select min(x) from unnest(data) as x
$$
language SQL;
sql fiddle demo