Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

What is a fastest way to unwrap array into rows in PostgreSQL? i.e.

We have:

a
-
{1,2}
{2,3,4}

And we need:

b
- 
1
2
2
3
4

I'm use:

select explode_array(a) as a from a_table;

where explode_array is:

create or replace function explode_array(in_array anyarray) returns setof anyelement as
$$
    select ($1)[s] from generate_series(1,array_upper($1, 1)) as s;
$$

Is any better way?

share|improve this question

1 Answer

up vote 8 down vote accepted

Use unnest. For example:

CREATE OR REPLACE FUNCTION test( p_test text[] )
  RETURNS void AS
$BODY$
BEGIN
  SELECT id FROM unnest( p_test ) AS id;
END;
$BODY$
  LANGUAGE plpgsql IMMUTABLE
  COST 1;
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.