Skip to content

Instantly share code, notes, and snippets.

@clarkbw
Last active April 12, 2022 22:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save clarkbw/acb0f15036ac46057085f1f5838110de to your computer and use it in GitHub Desktop.
Save clarkbw/acb0f15036ac46057085f1f5838110de to your computer and use it in GitHub Desktop.
quick postgres SQL for replacing an index
-- get existing index names and SQL definitions
SELECT indexname, indexdef
FROM pg_indexes
WHERE schemaname = 'public' AND tablename = 'table'
ORDER BY indexname;
-- create new tmp index (example)
CREATE INDEX tmp_name_of_the_index ON public.table USING btree (time ASC);
-- drop existing index
DROP INDEX name_of_the_index;
-- mv the tmp index to the previous name
ALTER INDEX tmp_name_of_the_index RENAME TO name_of_the_index;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment