Skip to content

Instantly share code, notes, and snippets.

@redsfyre
Created November 9, 2021 10:58
Show Gist options
  • Save redsfyre/18dec03354203cd54d04f9a663c38d80 to your computer and use it in GitHub Desktop.
Save redsfyre/18dec03354203cd54d04f9a663c38d80 to your computer and use it in GitHub Desktop.
Query that drops all indexes in the given table. Note: unique keys are not affected by this operation.
DO
$do$
DECLARE
_sql text;
BEGIN
SELECT 'DROP INDEX ' || string_agg(indexrelid::regclass::text, ', ')
FROM pg_index i
LEFT JOIN pg_depend d ON d.objid = i.indexrelid
AND d.deptype = 'i'
WHERE i.indrelid = 'TABLE_NAME'::regclass -- Replace the TABLE_NAME with your table's name
AND d.objid IS NULL
INTO _sql;
IF _sql IS NOT NULL THEN
EXECUTE _sql;
END IF;
END
$do$;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment