I am creating a Postgres/postgis function that is suppose to cut an input-geometry according to a ‘hard-coded’ table’s geometry. The hard-coded table is called `j.borders`

and its geometry attribute is called `geom`

. If the input-geometry intersects several geometries (rows) from `j.borders`

, `j.borders`

geometry is collected into one geometry (using st_union), which is then used to cut the input-geometry using st_difference. If the input-geometry doesn’t intersects `j.borders`

geometry, the input-geometry is returned un-modified.

The function I have created behaves as i want it to, however:

**I dont understand why it behaves like this**

the function in question:

```
CREATE OR REPLACE FUNCTION public.clip_geom(
IN in_geom geometry,
OUT out_geom geometry)
-- can be called like SELECT public.clip_geom(geom) FROM a_table
RETURNS geometry AS
$BODY$
BEGIN
SELECT
st_multi( -- st_multi makes sure the geometry is always the same "type"
COALESCE(
ST_DIFFERENCE(in_geom,st_union(brdr.geom)), -- cutting the geometry
in_geom -- not cutting the geometry
))
INTO out_geom
FROM j.borders brdr
WHERE ST_INTERSECTS(in_geom, brdr.geom); -- if the two geometries intersects
END;
$BODY$
LANGUAGE plpgsql;
```

If you give this function a table of 5 rows with geometry and 2 of them intersects the border-geometry, it then returns all 5 rows, where 2 have been clipped to the border.

the select statement is followed by a where-clause which states the input-geometry and border-geometry have to intersect. If the two geometries do not intersect, nothing is returned, correct?

**but why does the function then return the unmodified geometry?** Does it have something to do with the `COALESCE`

?