Skip to content

Instantly share code, notes, and snippets.

@felipem775
Created May 29, 2018 07:09
Show Gist options
  • Save felipem775/59ec943d284821b9ebfe724a6e3246f7 to your computer and use it in GitHub Desktop.
Save felipem775/59ec943d284821b9ebfe724a6e3246f7 to your computer and use it in GitHub Desktop.
-- original https://stackoverflow.com/a/19532132/2428230
CREATE FUNCTION make_uid() RETURNS text AS $$
DECLARE
new_uid text;
done bool;
BEGIN
done := false;
WHILE NOT done LOOP
new_uid := upper(substring(md5(''||now()::text||random()::text),1,8));
done := NOT exists(SELECT 1 FROM report_users WHERE confirm_key=new_uid);
END LOOP;
RETURN new_uid;
END;
$$ LANGUAGE PLPGSQL VOLATILE;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment