Created
March 12, 2014 17:46
-
-
Save rdeavila/9512225 to your computer and use it in GitHub Desktop.
PostgreSQL: Como criar um índice, apenas quando ele não existe.
This file contains 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
-- Cria o índice, se ele não existe | |
DO $$ BEGIN IF NOT EXISTS ( | |
SELECT | |
1 | |
FROM | |
pg_class c | |
JOIN | |
pg_namespace n | |
ON n.oid = c.relnamespace | |
WHERE | |
c.relname = 'nome_do_indice' | |
AND n.nspname = 'public' | |
) THEN CREATE INDEX nome_do_indice ON nome_tabela(nome_coluna); | |
END IF; END$$; | |
-- Remove o índice, se ele existe | |
DO $$ BEGIN IF EXISTS ( | |
SELECT | |
1 | |
FROM | |
pg_class c | |
JOIN | |
pg_namespace n | |
ON n.oid = c.relnamespace | |
WHERE | |
c.relname = 'nome_do_indice' | |
AND n.nspname = 'public' | |
) THEN DROP INDEX nome_do_indice; | |
END IF; END$$; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment