'PostGIS in PLSQL; Properly defining function; resolving conflicting or redundant options

As you can figure from the code below, I am new to PLSQL. I try to allocate a point to a polygon, but only upon a function call. To this end I wanted to use PostGIS. However, I fail in generating the function in general. Could someone be so kind and explain the construction of such a function for replication purposes? I jsut want to return the resulting view

CREATE OR REPLACE FUNCTION public.point_polygon_allocation(user_id)

RETURNS var_view 

AS

$$


DECLARE
var_view VIEW;

BEGIN
RETURNS SELECT * INTO var_view
FROM polygon_relation AS poly
INNER JOIN user_table AS loc ON ST_Contains(poly."polygon", loc."location")
WHERE user_table.id = user_id;
RETURN var_view;

END;

$$
;


Solution 1:[1]

I actually came to a solution. First of all, I noticed that I used PlPgSQL and not PlSQL.

Regarding the solution, it turned out considerably shorter than I imagined:

CREATE OR REPLACE FUNCTION public.point_polygon_allocation(lat numeric, lon numeric)
RETURNS TABLE (
  id int,
  polygon_id int
) 
AS
$$
DECLARE 
  lat numeric;
  lon numeric;

BEGIN
  RETURN QUERY
    SELECT poly.id, poly.polygon_id
    FROM polygon_relation AS poly
    WHERE ST_Contains(poly."polygon", ST_MakePoint(lat,lon));
END;
$$
LANGUAGE plpgsql;

SELECT *
FROM point_polygon_allocation(52.520008, 13.404954);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ouflak