Last active
November 1, 2019 09:57
-
-
Save abaldwin88/6e2a53bf8b9b3f501e7b7d5d403f9b5b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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