Skip to content

Commit

Permalink
Use the optimised idx() from contrib/intarray.
Browse files Browse the repository at this point in the history
This only works for 32-bit integers, so if/when we move to bigints we'll
lose the optimisation.
  • Loading branch information
mauzo committed Dec 3, 2011
1 parent 1ed11f9 commit 8ac7335
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions btree.sql
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,26 @@ GRANT ALL ON SEQUENCE btree_id_seq TO PUBLIC;

-- array functions

CREATE FUNCTION ix(anyarray, anyelement) RETURNS integer
CREATE FUNCTION idx(anyarray, anyelement) RETURNS integer
LANGUAGE sql IMMUTABLE
AS $fn$
SELECT n
FROM generate_series(1, array_length($1, 1)) s (n)
WHERE $1[n] = $2
$fn$;

-- use the optimised version from contrib/intarray if we've got it
SELECT _do($do$
BEGIN
CREATE FUNCTION idx (integer[], integer) RETURNS integer
LANGUAGE C STRICT IMMUTABLE
AS '$libdir/_int';
EXCEPTION
WHEN undefined_file THEN
NULL;
END;
$do$);

-- metapage functions

CREATE TYPE btree_meta AS (
Expand Down Expand Up @@ -87,7 +99,7 @@ CREATE FUNCTION btree_x (btree, integer) RETURNS btree_x
AS $fn$
SELECT btmeta(), $1.id,
coalesce(array_length($1.kids, 1), 0), $1.kids,
$1.leaf, ix($1.kids, $2);
$1.leaf, idx($1.kids, $2);
$fn$;

CREATE FUNCTION btree_x (btree) RETURNS btree_x
Expand Down

0 comments on commit 8ac7335

Please sign in to comment.