> SELECT INTO cid count(*) FROM providers WHERE uid =child_provider;
Hey, hey.
Better :
SELECT blablah FROM providers WHERE uid = child_provider LIMIT 1;
IF NOT FOUND THEN exit with error ELSE do your stuff
Why scan more than 1 row when you just need existence ?
Or :
SELECT INTO cid parent_id FROM providers WHERE uid=cid;
WHILE FOUND
RETURN NEXT cid;
SELECT INTO cid parent_id FROM providers WHERE uid=cid;
END;
Not sure about the While syntax but you get the idea.