-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-- Se chequea la existencia del lenguaje plpgsql. Si no existe, se lo crea
-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

CREATE OR REPLACE FUNCTION create_language_plpgsql()
RETURNS BOOLEAN AS $$
    CREATE LANGUAGE plpgsql;
    SELECT TRUE;
$$ LANGUAGE SQL;

SELECT CASE WHEN NOT
(
	SELECT  TRUE AS exists
	FROM    pg_language
	WHERE   lanname = 'plpgsql'
	UNION
	SELECT  FALSE AS exists
	ORDER BY exists DESC
	LIMIT 1
)
THEN
    create_language_plpgsql()
ELSE
    FALSE
END AS plpgsql_created;

DROP FUNCTION create_language_plpgsql();

-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


