Skip to content

Instantly share code, notes, and snippets.

@pdbradley
Created March 6, 2021 14:01
Show Gist options
  • Save pdbradley/84b491637c2ddcbeb33f706439df57fd to your computer and use it in GitHub Desktop.
Save pdbradley/84b491637c2ddcbeb33f706439df57fd to your computer and use it in GitHub Desktop.
find_or_create SQL function
CREATE
OR REPLACE FUNCTION public.find_or_create_user(user_email text) RETURNS SETOF users LANGUAGE sql STABLE AS $ function $
INSERT INTO
users (email)
VALUES
(user_email) ON CONFLICT (email) DO NOTHING;
SELECT
*
FROM
users
WHERE
users.email = user_email;
$ function $
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment