       EXCEPTION WHEN unique_violation THEN
       END;
   END CASE;
+  
+--  raise notice 'times time spent=%', clock_timestamp() - ts;
   RETURN times_token;
 END
 $$ LANGUAGE plpgsql SET search_path=provsql,pg_temp,public SECURITY DEFINER;

$$ LANGUAGE plpgsql SET search_path=provsql,pg_temp,public SECURITY DEFINER; 

CREATE OR REPLACE FUNCTION provenance_plus
  (state provenance_token, token provenance_token)
  RETURNS provenance_token AS
$$
DECLARE
  plus_token uuid;
BEGIN
  IF token = gate_zero() THEN
    return state;
  END IF;

  LOCK TABLE provenance_circuit_gate;
  IF state IS NULL THEN
    plus_token:=uuid_generate_v4();
    INSERT INTO provenance_circuit_gate VALUES(plus_token,'plus');
  ELSE
    plus_token:=state;
  END IF;
  INSERT INTO provenance_circuit_wire VALUES(plus_token,token);

  RETURN plus_token;
END
$$ LANGUAGE plpgsql SET search_path=provsql,pg_temp,public SECURITY DEFINER;

CREATE OR REPLACE FUNCTION provenance_plus_make_deterministic(state provenance_token)
  RETURNS provenance_token AS
$$
DECLARE
  c INTEGER;
  plus_token uuid;
BEGIN
  LOCK TABLE provenance_circuit_gate;
  SELECT COUNT(*) INTO c FROM provenance_circuit_wire WHERE f=state;

  IF c = 0 THEN
    plus_token := gate_zero();
  ELSIF c = 1 THEN
    SELECT t INTO STRICT plus_token FROM provenance_circuit_wire WHERE f=state;
    DELETE FROM provenance_circuit_wire WHERE f=state;
  ELSE
    SELECT uuid_generate_v5(uuid_ns_provsql(),concat('plus',uuid_provsql_agg(t)))
    INTO plus_token
    FROM provenance_circuit_wire
    WHERE f=state;

    BEGIN
      INSERT INTO provenance_circuit_gate VALUES(plus_token,'plus');
      UPDATE provenance_circuit_wire SET f=plus_token WHERE f=state;
    EXCEPTION WHEN unique_violation THEN
      DELETE FROM provenance_circuit_wire WHERE f=state;
    END;
  END IF;
  DELETE FROM provenance_circuit_gate WHERE gate=state;

  RETURN plus_token;
END
$$ LANGUAGE plpgsql STRICT SET search_path=provsql,pg_temp,public SECURITY DEFINER;

CREATE AGGREGATE provenance_agg(token provenance_token) (
  SFUNC = provenance_plus,
  STYPE = provenance_token,
  FINALFUNC = provenance_plus_make_deterministic
);

CREATE OR REPLACE FUNCTION trim_circuit()

  attribute RECORD;
BEGIN
  LOCK TABLE provenance_circuit_gate;
  INSERT INTO provenance_circuit_gate VALUES (NEW.provsql, 'input'); 
  RETURN NEW; 
END
$$ LANGUAGE plpgsql SET search_path=provsql,pg_temp SECURITY DEFINER;

