3

I am trying to initialize an array array_entries. Tries array_fill as array_fill(0,array_entries) but dint work.

create or replace function vin_temp_test1(k date,x varchar) RETURNS float AS $$
    declare
    array_entries int [];
    curs4  CURSOR FOR  select * from temp_table;
    record_type1 record;

    fetch curs4 into record_type1;
            exit when not found;
    loop
    -- trying to intialize the array array_entries here
        loop
     --filling the array inside this loop.
        end loop;
    end loop;

1 Answer 1

2

Probably you have NULL in array_entries

postgres=# select array_fill(0, NULL);
ERROR:  dimension array or low bound array cannot be null
postgres=# select array_fill(0, ARRAY[10]);
      array_fill       
-----------------------
{0,0,0,0,0,0,0,0,0,0}
(1 row)

Attention!

Is good to know, so update of large array (larger than 20000 fields) is pretty slow. So much faster than repeatable update is using ARRAY(subselect) constructor

postgres=# DO $$ DECLARE x int[]; 
           begin  
              x := array_fill(0,ARRAY[100000]); 
              for i in 1..100000 loop 
                x[i] := 1; 
           end loop; end $$;
DO
Time: 5533.581 ms
postgres=# DO $$ DECLARE x int[]; 
           begin  x := ARRAY(SELECT 1 FROM generate_series(1,100000)); end $$;
DO
Time: 36.590 ms
1
  • No actually I wanted the array as it is after the declare section. I mean an empty array with no values in it. Commented Feb 26, 2014 at 17:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.