Last active
April 5, 2022 12:30
-
-
Save kilfu0701/6485abb501536964ab9fc4fb76106819 to your computer and use it in GitHub Desktop.
[postgres] delete a table's indexes function.
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
CREATE OR REPLACE FUNCTION rm_table_idx(tbl_name text) | |
RETURNS void AS | |
$$ | |
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 = tbl_name::regclass -- possibly schema-qualified | |
AND d.objid IS NULL -- no internal dependency | |
INTO _sql; | |
IF _sql IS NOT NULL THEN -- only if index(es) found | |
raise notice '%', _sql; | |
EXECUTE _sql; | |
END IF; | |
END; | |
$$ | |
LANGUAGE plpgsql; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage