Skip site navigation (1) Skip section navigation (2)

array_to_column function

From: David Fetter <david(at)fetter(dot)org>
To: PG Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: array_to_column function
Date: 2004-10-30 19:54:40
Message-ID: 20041030195439.GA13884@fetter.org (view raw or flat)
Thread:
Lists: pgsql-hackers
Kind people,

Here's something I came up with, having accidentally discovered the
ARRAY() constructor (BTW, I think at least some pointer to it should
be in the array section of functions & operators).

CREATE OR REPLACE FUNCTION array_to_column (ANYARRAY)
RETURNS SETOF ANYELEMENT
IMMUTABLE
LANGUAGE plpgsql
AS $$
BEGIN
    IF (position('][' IN array_dims($1)) <> 0)
    THEN
        RAISE EXCEPTION 'Only 1-dimensional arrays are allowed!';
    END IF;
    FOR i IN array_lower($1, 1) .. array_upper($1, 1)
    LOOP
        RETURN NEXT $1[i];
    END LOOP;
    RETURN;
END;
$$;

Thanks to Markus Bertheau aka ska-fan for help with removing an
unneeded regex compare and with spelling. :)

Cheers,
D
-- 
David Fetter david(at)fetter(dot)org http://fetter.org/
phone: +1 510 893 6100   mobile: +1 415 235 3778

Remember to vote!

Responses

pgsql-hackers by date

Next:From: Tom LaneDate: 2004-10-30 20:45:22
Subject: Re: 8.0b4: COMMIT outside of a transaction echoes ROLLBACK
Previous:From: Tom LaneDate: 2004-10-30 19:53:39
Subject: Re: Signature change for SPI_cursor_open

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group