title: unique random timestamp in postgresql
body: <p>I am trying for many hours to do it. I need to create  100.000 rows and timestamp is primary key. thats why it must be unique.</p>
  
  <p>I wrote some code, it can generate 100 random timestamp but if i try it for more than 100 such as 1000 or 10000, i have an error, like that</p>
  
  <blockquote>
    <p>ERROR: duplicate key value violates unique constraint "position_pkey"
    SQL state: 23505 Detail: Key ("timestamp")=(2014-03-09
    04:03:16.843499) already exists.</p>
  </blockquote>
  
  <p>I dont know how to create 100.000 timestamp unique.</p>
  
  <p>this is my function</p>
  
  <pre><code>CREATE OR REPLACE FUNCTION generate_random(count integer DEFAULT 1)
    RETURNS SETOF timestamp AS
  $BODY$
      BEGIN
              RETURN QUERY SELECT distinct NOW()::timestamp + '10ms'::interval *RANDOM()*RANDOM()
  FROM generate_series(0,count);
  
              --SELECT (NOW() - '10000000'::INTERVAL * ROUND(RANDOM() * RANDOM()))::timestamp
                      --     FROM GENERATE_SERIES(1, count);
          END;
  $BODY$
    LANGUAGE plpgsql VOLATILE
    COST 1000;
  </code></pre>
  
  <p>and this is generate_random function that generates double numbers randomly.</p>
  
  <pre><code>CREATE OR REPLACE FUNCTION generate_random(c integer DEFAULT 1, min double precision DEFAULT 0.0, max double precision DEFAULT 1.0)
    RETURNS SETOF double precision AS
  $BODY$
          BEGIN
              RETURN QUERY SELECT min + (max - min) * RANDOM()
                           FROM GENERATE_SERIES(1, c);
          END;
      $BODY$
    LANGUAGE plpgsql VOLATILE STRICT
    COST 100
    ROWS 1000;
  </code></pre>
  
  <p>and I call this function like that in query</p>
  
  <pre><code>INSERT INTO "mytable"(
              "timestamp", x, y, z)
      VALUES (generate_random(1000), generate_random(1000, 0, 50), generate_random(1000, 0, 50), generate_random(1000, 0, 50));
  </code></pre>
  
  <p>So, I want to create 100.000 rows that are unique timestamps and some random double numbers (doesnt matter unique or not)</p>