dynamic portion of plpgsql function

Viewed 67

I have a function that loops through all points and compares them to other points (yes i know this doesn't need to be done in plpgsql - this is a toy MWE). The function returns the points with the largest x coordinate:

create type point as (x integer, y integer);
create or replace function test() returns set of Point as
$$
declare
    p1 point;
    p2 point;
    bool integer;
begin
for p1 in select * from table loop
    bool := 0;
    for p2 in select * from table loop
        if p2.x > p1.x then bool :=1;
        exit;
        end if;
    end loop;
    if bool = 0 then return next p1;
    end if;
end loop;
end;
$$ language 'plpgsql';

which works. What I want to do is be able to have the table name as a parameter of the function, I am confused as to where to put the execute statement.

1 Answers
Related