5

In plpgSql, I need to add number to empty numeric array, this empty numeric array is variable.

What I do is this:

DECLARE
    new_arr INTEGER[];
BEGIN
    SELECT array_append(new_arr, 4) INTO new_arr;

This works, but I am not sure, may be exists better way for this?

4
  • 2
    new_arr := new_arr || 4 is probably faster: adpgtech.blogspot.de/2014/11/assignment-beats-select-into.html Commented Nov 5, 2014 at 10:49
  • @a_horse_with_no_name: no, it is equal - in this case. look to plpgsql source code. Andrew's measuring is correct, but investigation is wrong, and comments are out too. What is important - how much columns are in result - when one, then plpgsql call executor directly, when more, then SPI is used (it is slower). Commented Nov 5, 2014 at 20:53
  • @PavelStehule: but the assignment is still faster than the select. Plus I personally find := more consistent within a procedural language than using a select to assign a value. Commented Nov 5, 2014 at 21:29
  • I agree, so := should be used there. But any assignment produce SELECT .. hidden, but SELECT. a := 1 and SELEC1 1 INTO a are same. Commented Nov 5, 2014 at 21:35

1 Answer 1

18

You can do it, but this style is little bit obscure

use a assign statement, but you don't need a forget a correct initialization. In your example a new_arr doesn't hold a empty array. It holds a NULL. Do:

DECLARE new_arr int[] DEFAULT '{}';
BEGIN
  new_arr := new_arr || 4;
  -- or
  new_arr := array_append(new_arr, 4);

  -- both are equal

SELECT INTO should be used if you do query to some relation.

Sign up to request clarification or add additional context in comments.

6 Comments

Something does not seem correct with this answer. In 9.5.1, RAISE NOTICE 'Array % has length of %', new_arr, array_length(new_arr, 1); prints Array {} has length of <NULL>. So does it really create an empty array, does it just rely on some other obscure reason to work, or is it a bug in array_length()?
In addition prior to any assignment ASSERT new_arr IS NULL; asserts which means that it is not null in spite of what array_length() thinks.
@Neil - probably you forgot initialize a array to '{}'.
Nope, here is the test case CREATE OR REPLACE FUNCTION test_empty_array_length() RETURNS integer AS $$ DECLARE new_arr integer[] DEFAULT '{}'; BEGIN -- http://stackoverflow.com/questions/26755195/add-element-to-empty-array RAISE NOTICE 'Array % has length %', new_arr, array_length(new_arr, 1); return array_length(new_arr, 1); END; $$ LANGUAGE plpgsql;
You and Tom had some discussions about this a couple of years ago, which is why I did not raise a bug report yet. Hopefully it is something I don't understand.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.