title: Postgresql: How to efficient populate table with 10 millions rows with random string
body: <p>I have table with schema:</p>
  
  <pre><code>test1 (id integer primary key , padding text)
  </code></pre>
  
  <p>(there is index on id)</p>
  
  <p>I want to have 10 millions of rows with random padding (padding length smaller than 1024 signs).
  How to generate it fast and insert it into table?</p>
  
  <p>I'm trying right now this solution:</p>
  
  <pre><code>insert into test1 (select *, random_string(1024) from generate_series(0, 10000000));
  </code></pre>
  
  <p>where random_string is a function:</p>
  
  <pre><code>create or replace function random_string(length integer) returns text as 
  $$
  declare
    chars text[] := '{0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z}';
    result text := '';
    i integer := 0;
    length2 integer := (select trunc(random() * length + 1));
  begin
    if length2 &lt; 0 then
      raise exception 'Given length cannot be less than 0';
    end if;
    for i in 1..length2 loop
      result := result || chars[1+random()*(array_length(chars, 1)-1)];
    end loop;
    return result;
  end;
  $$ language plpgsql;
  </code></pre>
  
  <p>It seems that postgresql is creating temporary table and only generating of this temporary table will take 2 hours.</p>