Skip to content

Instantly share code, notes, and snippets.

@beargiles
Created October 25, 2021 23:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save beargiles/1691fce83ad964f91b9bc43e34dec5f9 to your computer and use it in GitHub Desktop.
Save beargiles/1691fce83ad964f91b9bc43e34dec5f9 to your computer and use it in GitHub Desktop.
PostgreSQL function checking whether a parameter is in a valid set of values
--
-- Function that needs to explicitly check whether the 'color' value is valid
--
CREATE OR REPLACE FUNCTION get_gluon(color text) RETURNS TEXT AS $$
#print_strict_params on
DECLARE
duck TEXT;
BEGIN
IF color not in ('red', 'green', 'blue') THEN
RAISE EXCEPTION 'unknown color %', color;
END IF;
-- ....
return duck;
END;
$$ LANGUAGE plpgsql
IMMUTABLE
RETURNS NULL ON NULL INPUT
PARALLEL SAFE;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment