 AS $$
 BEGIN
 
-RETURN QUERY
-EXECUTE
-format($string$
-  SELECT geoid_ct.colname As geoid_colname,
-         tablename,
-         geom_ct.colname As geom_colname
-  FROM observatory.obs_column_table As geoid_ct,
-       observatory.obs_table As geom_t,
-       observatory.obs_column_table As geom_ct,
-       observatory.obs_column As geom_c
-  WHERE geoid_ct.column_id
-     IN (
-       SELECT source_id
-       FROM observatory.obs_column_to_column
-       WHERE reltype = 'geom_ref'
-         AND target_id = '%s'
-       )
-    AND geoid_ct.table_id = geom_t.id and
-        geom_t.id = geom_ct.table_id and
-        geom_ct.column_id = geom_c.id and
-        geom_c.type ILIKE 'geometry'
-    -- TODO: filter by clipped vs. not so appropriate tablename are unique
-    --       so the limit 1 can be removed
-    LIMIT 1
-  $string$, boundary_id);
-END;
-$$ LANGUAGE plpgsql;
+  RETURN QUERY
+  EXECUTE
+  format($string$
+    SELECT geoid_ct.colname As geoid_colname,
+           tablename,
+           geom_ct.colname As geom_colname
+    FROM observatory.obs_column_table As geoid_ct,
+         observatory.obs_table As geom_t,
+         observatory.obs_column_table As geom_ct,
+         observatory.obs_column As geom_c
+    WHERE geoid_ct.column_id
+       IN (
+         SELECT source_id
+         FROM observatory.obs_column_to_column
+         WHERE reltype = 'geom_ref'
+           AND target_id = '%s'
+         )
+      AND geoid_ct.table_id = geom_t.id and
+          geom_t.id = geom_ct.table_id and
+          geom_ct.column_id = geom_c.id and
+          geom_c.type ILIKE 'geometry'
+      -- TODO: filter by clipped vs. not so appropriate tablename are unique
+      --       so the limit 1 can be removed
+      LIMIT 1
+    $string$, boundary_id);
+
+END;
+$$ LANGUAGE plpgsql;

  target_table text;
BEGIN

-- check inputs
IF lower(overlap_type) NOT IN ('contains', 'intersects', 'within')
THEN
  -- recognized overlap type (map to ST_Contains, ST_Intersects, and ST_Within)
  RAISE EXCEPTION 'Overlap type ''%'' is not an accepted type (choose contains, within, intersects)', overlap_type;
ELSIF ST_GeometryType(geom) NOT IN ('ST_Polygon', 'ST_MultiPolygon')
THEN
    RAISE EXCEPTION 'Invalid geometry type (%), expecting ''ST_MultiPolygon'' or ''ST_Polygon''', ST_GeometryType(geom);
END IF;

-- TODO: add timespan in search
-- TODO: add overlap info in search
-- TODO: turn this into a function _OBS_GetGeometryMetadata(geo_ref_name, tablename, and geom_colname)
--   SELECT geo_ref_name, tablename, geom_colname INTO a, b, c
--     FROM _OBS_GetGeometryMetadata
SELECT * INTO geoid_colname, target_table, geom_colname
FROM cdb_observatory._OBS_GetGeometryMetadata(boundary_id);

  -- if no tables are found, raise notice and return null
  IF target_table IS NULL

  geom_colname text;
BEGIN

  SELECT * INTO geoid_colname, target_table, geom_colname
  FROM cdb_observatory._OBS_GetGeometryMetadata(boundary_id);

  RAISE NOTICE '%', target_table;

