This instruction:
SELECT array_append(emails_array, new_user_email);
should fail because array_append returns the changed array and ignoring the selection result is not allowed.
If you want to add to the source array, this should be:
SELECT array_append(emails_array, new_user_email) INTO emails_array;
However, this is not even necessary. You can simplify the body of your function:
BEGIN INSERT INTO users (login,password,emails,first_name,last_name) SELECT new_user_login,new_user_password,array[new_user_email],new_user_first_name,new_user_last_name WHERE NOT EXISTS (select 1 FROM users WHERE users.login = new_user_login) RETURNING id INTO new_user_id; RETURN coalesce(new_user_id,0); END;
Daniel VΓ©ritΓ©
source share