I had this :
CREATE FUNCTION upsert_user(u_name text, u_fullname text, u_email text, u_suffix text) RETURNS integer
    LANGUAGE plpgsql
    AS $$
DECLARE
    userid users.id_user%TYPE;
BEGIN
    LOOP
        -- first try to update
        UPDATE users SET "fullname" = u_fullname, "email" = u_email, "suffix" = u_suffix WHERE "name" = u_name RETURNING "id_user" INTO userid;
        -- check if the row is found
        IF FOUND THEN
            RETURN userid;
        END IF;
        -- not found so insert the row
        BEGIN
            INSERT INTO users ("name", "fullname", "email", "suffix") VALUES (u_name, u_fullname, u_email, u_suffix) RETURNING "id_user" INTO userid;
            RETURN userid;
            EXCEPTION WHEN unique_violation THEN
                -- do nothing and loop
        END;
    END LOOP;
END;
$$;
CREATE TRIGGER link_entity
  BEFORE INSERT
  ON public.users
  FOR EACH ROW
  EXECUTE PROCEDURE public.link_entity();
CREATE FUNCTION link_entity() RETURNS trigger
    LANGUAGE plpgsql
    AS $$    DECLARE
        entityid integer;
    BEGIN
        INSERT INTO privileges_entities (name) VALUES (NEW.name) RETURNING privileges_entities.id_entity INTO entityid;
        IF NOT FOUND THEN
            RETURN NULL;
        END IF;
        NEW.ref_entity := entityid;
        RETURN NEW;
    END;
$$;
After updated postgresql to version 9.5, I modified the function upsert_user to use the new instruction ON CONFLICT:
CREATE FUNCTION upsert_user(u_name text, u_fullname text, u_email text, u_suffix text) RETURNS integer
    LANGUAGE sql
    AS $$
  INSERT INTO users (name, fullname, email, suffix)
  VALUES (u_name, u_fullname, u_email, u_suffix)
  ON CONFLICT (name) DO UPDATE SET name=EXCLUDED.name, fullname=EXCLUDED.fullname, email=EXCLUDED.email, suffix=EXCLUDED.suffix
  RETURNING id_user;
$$;
The problem is that, now, new rows are inserted in the privileges_entities table even if insertion into the users table fails. Is it possible to rollback the trigger if the insertion of the user leads to a conflict?
This is indeed a side-effect of using the new ON CONFLICT clause.
My solution here would be to add a check into the link_entity() function itself and prevent it from continuing if the user already exists. Like this:
CREATE FUNCTION link_entity() RETURNS trigger
    LANGUAGE plpgsql
    AS $$    DECLARE
        entityid integer;
        nameExists boolean;
    BEGIN
        EXECUTE format('SELECT EXISTS(SELECT 1 FROM %I.%I WHERE name = NEW.name)', TG_TABLE_SCHEMA, TG_TABLE_NAME) INTO nameExists;
        IF nameExists THEN
            RETURN NEW; -- just return, entity already linked
        END IF;
        INSERT INTO privileges_entities (name) VALUES (NEW.name) RETURNING privileges_entities.id_entity INTO entityid;
        IF NOT FOUND THEN
            RETURN NULL;
        END IF;
        NEW.ref_entity := entityid;
        RETURN NEW;
    END;
$$;
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With