title: PostgreSQL shuffle column values
body: <p>In a table with > 100k rows, how can I efficiently shuffle the values of a specific column?</p>
  
  <p>Table definition:</p>
  
  <pre><code>CREATE TABLE person
  (
    id integer NOT NULL,
    first_name character varying,
    last_name character varying,
   CONSTRAINT person_pkey PRIMARY KEY (id)
  )
  </code></pre>
  
  <p>In order to anonymize data, I have to shuffle the values of the 'first_name' column <strong>in place</strong> (I'm not allowed to create a new table).</p>
  
  <p>My try:</p>
  
  <pre><code>with
  first_names as (
  select row_number() over (order by random()),
         first_name as new_first_name
  from person
  ),
  ids as (
  select row_number() over (order by random()), 
         id as ref_id
  from person
  )
  update person
  set first_name = new_first_name
  from first_names, ids
  where id = ref_id;
  </code></pre>
  
  <p>It takes hours to complete.</p>
  
  <p>Is there an efficient way to do it?</p>