Skip to content

Instantly share code, notes, and snippets.

@jpoehnelt
Created March 1, 2016 00:35
Show Gist options
  • Save jpoehnelt/3cc4bcabaaa72552e47f to your computer and use it in GitHub Desktop.
Save jpoehnelt/3cc4bcabaaa72552e47f to your computer and use it in GitHub Desktop.
Postgres Array Bounds
CREATE OR REPLACE FUNCTION array_bounds(actual integer[], upper integer[], lower integer[]) RETURNS boolean
AS $$
DECLARE
length integer;
BEGIN
IF actual is NULL THEN
RETURN FALSE;
END IF;
SELECT array_length(actual, 1) into length;
IF length <> array_length(upper, 1) AND length <> array_length(upper, 1)
THEN
RETURN FALSE;
ELSE
FOR i IN 1 .. length
LOOP
IF actual[i] is null or actual[i] < lower[i] or actual[i] > upper[i]
THEN
RETURN FALSE;
END IF;
END LOOP;
RETURN TRUE;
END IF;
RETURN TRUE;
END;
$$
LANGUAGE plpgsql
IMMUTABLE;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment