Skip to content

Instantly share code, notes, and snippets.

@dexalex84
Created November 19, 2016 16:09
Show Gist options
  • Save dexalex84/4684eed68fd464a79651bb5a18d114dd to your computer and use it in GitHub Desktop.
Save dexalex84/4684eed68fd464a79651bb5a18d114dd to your computer and use it in GitHub Desktop.
PostgreSQL drop specific indexes on one specific schema
;
do
$$
DECLARE var_table_name character varying(1000);
var_table_schema character varying(1000);
var_index_name character varying(1000);
var_query character varying(1000);
var_is_debug boolean = false;
begin
drop table if exists temp_table_indexes_non_uniq;
create temp table if not exists temp_table_indexes_non_uniq as
with tt_indexes as
(
select
n.nspname "TableSchema",
c.relname as "TableName",
a.relname as "IndexName",
b.indisunique "IsUnique",
n.nspname||c.relname|| a.relname "UniqName"
from
pg_class a
inner join
pg_index b
on
a.oid = b.indexrelid
inner join
pg_class c
on
b.indrelid = c.oid
JOIN
pg_catalog.pg_namespace n ON
n.oid = a.relnamespace
where
a.relname not like 'pg_%'
and
n.nspname = 'tempdownload'
and
(
c.relname like 'safo%'
or
c.relname like 'ic%'
)
)
select "TableSchema",'"'||"TableName"||'"',"IndexName", 'drop index tempdownload."'||t1."IndexName"||'";' query
from
tt_indexes t1
where
t1."IsUnique" = false
and
exists (select 1 from tt_indexes t2 where t1."TableName" = t2."TableName" and t2."IsUnique" = true)
;
for var_query in select query from temp_table_indexes_non_uniq loop
raise notice 'Query to execute : %',var_query;
if (var_is_debug=false) THEN
execute var_query;
end if;
end loop;
end;
$$
language 'plpgsql';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment