Stack Exchange
PostgreSQL Function and IF STATEMENT gives: "WARNING: there is no transaction in progress COMMIT"
I'm running PostgreSQL 13.0
This is what the SQL query gives when I try to execute it. It doesn't run.

the sql query:
CREATE OR REPLACE FUNCTION cut_and_paste_hobby_to_interest(integer, text) RETURNS BOOL 
LANGUAGE plpgsql AS
BEGIN
        IF (SELECT COUNT(*) FROM interests WHERE interest_name = (SELECT hname FROM hobbies WHERE id = $1)) = 0
        THEN
         INSERT INTO interests (interest_name, interest_type, inserted_at, updated_at) VALUES ((SELECT hname FROM hobbies WHERE id = $1), $2, '2021-07-02 12:23:22', '2021-07-02 12:23:22');
    
         UPDATE user_things 
         SET 
            thingable_type = $2,
            thingable_id = (SELECT currval('things_id_seq')),
            inserted_at = '2021-07-02 12:23:22',
            updated_at = '2021-07-02 12:23:22'
         WHERE thingable_id IN ($1) AND thingable_type = 'Hobby';
    
         DELETE FROM hobbies_tags
         WHERE hobby_id IN ($1);
    
         DELETE FROM hobbies
         WHERE id IN ($1);
    
         DELETE FROM user_things
         WHERE thingable_id IN ($1) AND thingable_type = 'Hobby';
    
         SELECT TRUE;
        ELSE 
          SELECT FALSE;
        END IF;
END;



IF (SELECT COUNT(*) FROM interests WHERE interest_name = (SELECT hname FROM hobbies WHERE id = $1)) = 0 <<-- this could have been a WHERE exists(...) clause.

this worked
CREATE OR REPLACE FUNCTION cut_and_paste_hobby_to_interest(integer, text) returns int as $$
BEGIN
  IF (SELECT COUNT(*) FROM interests WHERE interest_name = (SELECT hname FROM hobbies WHERE id = $1)) = 0 THEN

    INSERT INTO interests (interest_name, interest_type, inserted_at, updated_at) VALUES ((SELECT hname FROM hobbies WHERE id = $1), $2, '2021-07-02 12:23:22', '2021-07-02 12:23:22');
    
    UPDATE user_things SET 
        thingable_type = $2,
        thingable_id = (SELECT currval('interests_id_seq')),
        inserted_at = '2021-07-02 12:23:22',
        updated_at = '2021-07-02 12:23:22'
    WHERE thingable_id IN ($1) AND thingable_type = 'Hobby';
    
    DELETE FROM hobbies_tags
    WHERE hobby_id IN ($1);
    
    DELETE FROM hobbies
    WHERE id IN ($1);
    
    DELETE FROM user_things
    WHERE thingable_id IN ($1) AND thingable_type = 'Hobby';
    
    return 1;
  ELSE 
    return 2;
  END IF;
END; $$ language plpgsql;


thingable_id = (SELECT currval('interests_id_seq')), can be simplified to thingable_id = currval('interests_id_seq'),
