--
-- util__view_exists/1
--
create or replace function util__view_exists(
  name text
) returns bool as $$

DECLARE
 v_schema    varchar;
 v_tablename varchar;
BEGIN
    IF (position('.' in name) = 0) THEN
	--
	-- view without a schema name
	--
	return exists (
	   select 1 from pg_views where viewname = name);
    ELSE
	--
	-- table with schema name
	--
	SELECT split_part(name, '.', 1) into v_schema;
	SELECT split_part(name, '.', 2) into v_tablename;
	return exists (
	    select 1 from information_schema.views
	    where table_name  = lower(v_tablename)
	    and table_schema = v_schema);
    END IF;
END;
$$ language plpgsql;