CREATE FUNCTION array_find(text[], text, bool)
RETURNS bool AS
$$
  SELECT coalesce(CASE i WHEN 'on' THEN true WHEN 'off' THEN false ELSE i::bool END, $3) FROM 
  (SELECT substring(i from E'\\W*=(.*)') AS i FROM unnest($1) AS t(i) WHERE i LIKE $2 || '=%') AS t
$$
LANGUAGE sql IMMUTABLE STRICT;

CREATE FUNCTION array_find(text[], text, integer)
RETURNS integer AS
$$SELECT substring(i from E'\\W*=(.*)')::integer FROM unnest($1) AS t(i) WHERE i LIKE $2 || '=%' UNION ALL SELECT $3 LIMIT 1$$
LANGUAGE sql IMMUTABLE STRICT;

CREATE FUNCTION array_find(text[], text, real)
RETURNS real AS
$$SELECT substring(i from E'\\W*=(.*)')::real FROM unnest($1) AS t(i) WHERE i LIKE $2 || '=%' UNION ALL SELECT $3 LIMIT 1$$
LANGUAGE sql IMMUTABLE STRICT;


CREATE VIEW pg_autovacuum AS
  SELECT oid,
         array_find(reloptions, 'autovacuum_enabled', true) AS enabled,
         array_find(reloptions, 'autovacuum_vacuum_threshold', -1) AS vac_base_thresh,
         array_find(reloptions, 'autovacuum_vacuum_scale_factor', -1::real) AS vac_scale_factor,
         array_find(reloptions, 'autovacuum_analyze_threshold', -1) AS anl_base_thresh,
         array_find(reloptions, 'autovacuum_analyze_scale_factor', -1::real) AS anl_scale_factor,
         array_find(reloptions, 'autovacuum_vacuum_cost_delay', -1) AS vac_cost_delay,
         array_find(reloptions, 'autovacuum_vacuum_cost_limit', -1) AS vac_cost_limit,
         array_find(reloptions, 'autovacuum_freeze_min_age', -1) AS freeze_min_age,
         array_find(reloptions, 'autovacuum_freeze_max_age', -1) AS freeze_max_age
  FROM pg_class
 WHERE reloptions IS NOT NULL;

-- TODO: CREATE RULE on pg_autovacuum for INSERT, UPDATE and DELETE.
