proposal: searching in array function - array_position

From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: proposal: searching in array function - array_position
Date: 2015-01-16 09:39:03
Message-ID: CAFj8pRBYDvz-qFfSwGZqQoYLBXWapeOXpyh-d=bHZB575zHGmQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi

I am proposing a simple function, that returns a position of element in
array.

FUNCTION array_position(anyarray, anyelement) RETURNS int

Implementation is simple (plpgsql code)

CREATE OR REPLACE FUNCTION array_position(anyarray, anyelement)
RETURNS int AS $$
DECLARE i int := 0;
BEGIN
FOREACH a IN ARRAY $1
LOOP
IF a = $1 THEN
RETURN i;
END IF;
i := i + 1;
END LOOP;
RETURN NULL;
END;
$$ LANGUAGE plpgsql IMMUTABLE STRICT;

A possible benefits:

1. speed in plpgsql applications
2. reduced length of SQL functions

Ideas, comments, notices?

Regards

Pavel

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Pavel Stehule 2015-01-16 09:45:46 proposal: row_to_array function
Previous Message Noah Misch 2015-01-16 08:25:00 hamerkop is stuck