
BEGIN
  IF UPPER(namespace) = 'USERENV' THEN
    CASE UPPER(attribute) 
	  WHEN 'SESSION_USER' 
	    THEN RETURN pg_catalog.session_user;
      WHEN 'CURRENT_USER' 
	    THEN RETURN pg_catalog.current_user;
      WHEN 'CURRENT_SCHEMA'
	    THEN RETURN pg_catalog.current_schema;
      WHEN 'HOST'
	    THEN RETURN pg_catalog.inet_client_addr;
      WHEN 'IP_ADDRESS'
	    THEN RETURN pg_catalog.inet_client_addr;
	  WHEN 'SERVER_HOST'
	    THEN RETURN pg_catalog.inet_server_addr;
	  WHEN 'ISDBA'
	    THEN RETURN (SELECT userenv('ISDBA'));
	  WHEN 'SID'
	    THEN RETURN (SELECT userenv('SID'));
	  WHEN 'LANGUAGE'
	    THEN RETURN (SELECT userenv('LANGUAGE'));
	  WHEN 'LANG'
	    THEN RETURN (SELECT userenv('LANG'));
	  WHEN 'TERMINAL'
	    THEN RETURN (SELECT userenv('TERMINAL'));
      ELSE 
	    RAISE 'invalid USERENV parameter';
    END CASE;
  ELSE
    RETURN NULL;
  END IF;
END