Skip to content

Instantly share code, notes, and snippets.

@abaldwin88
Last active November 1, 2019 09:57
Show Gist options
  • Save abaldwin88/6e2a53bf8b9b3f501e7b7d5d403f9b5b to your computer and use it in GitHub Desktop.
Save abaldwin88/6e2a53bf8b9b3f501e7b7d5d403f9b5b to your computer and use it in GitHub Desktop.
-- DO NOT RUN IN PRODUCTION
-- Supported on PSQL 9.6+.
-- Note: If all your tables aren’t located in the public schema
-- be sure to modify the specified schemaname
do $$
declare
selectrow record;
begin
for selectrow in
select
'
ALTER TABLE '|| T.mytable || ' ADD COLUMN IF NOT EXISTS _rand decimal DEFAULT random();
CREATE INDEX IF NOT EXISTS _rand_index_'|| T.mytable || ' on '|| T.mytable || '(_rand);
CLUSTER '|| T.mytable || ' USING _rand_index_'|| T.mytable || ';
' as script
from
(
select tablename as mytable from pg_tables where schemaname ='public' --your schema name here
) t
loop
execute selectrow.script;
end loop;
end;
$$;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment