Skip to content

Instantly share code, notes, and snippets.

@bastjan
Last active August 29, 2015 14:24
Show Gist options
  • Save bastjan/56f8848a0e3449fcfd87 to your computer and use it in GitHub Desktop.
Save bastjan/56f8848a0e3449fcfd87 to your computer and use it in GitHub Desktop.
order by subquery postgres
CREATE OR REPLACE FUNCTION idx(anyarray, anyelement)
RETURNS int AS
$$
SELECT i FROM (
SELECT generate_series(array_lower($1,1),array_upper($1,1))
) g(i)
WHERE $1[i] = $2
LIMIT 1;
$$ LANGUAGE sql IMMUTABLE;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment