 DECLARE
   deleted_ids INTEGER[] := '{}';
   class_ids INTEGER[];
-  waterboundary_sur_ids int[] := '{}';
+  waterboundary_srf_ids int[] := '{}';
 BEGIN
   -- fetch objectclass_ids if not set
   IF array_length($2, 1) IS NULL THEN

DECLARE
  deleted_id INTEGER;
  class_id INTEGER;
  waterboundary_sur_ids int[] := '{}';
BEGIN
  -- fetch objectclass_id if not set
  IF $2 = 0 THEN

  SELECT
    array_agg(waterboundary_surface_id)
  INTO
    waterboundary_srf_ids
  FROM
    delete_waterboundary_sur_refs;

  -- delete waterboundary_surface(s) not being referenced any more
  IF -1 = ALL(waterboundary_srf_ids) IS NOT NULL THEN
    PERFORM
      delete_waterbnd_surface(array_agg(a.a_id))
    FROM
      (SELECT DISTINCT unnest(waterboundary_srf_ids) AS a_id) a
    LEFT JOIN
      waterbod_to_waterbnd_srf n1
      ON n1.waterboundary_surface_id = a.a_id
