How to Use drop_indexes To, Uh, Drop Your Indexes

4 Comments

The open source drop_indexes function in our open source Postgres Bag of Tricks (Github) lets you pass in a schema and table, like this:

And it automatically drops all of the indexes on that table, like this:

Why, you might ask? Well, because for my Postgres performance tuning classes, I needed a way to rapidly reset tables after each training module so that we were working with just the bare heap.

Dr. Daniel Ducharme, Ph.D. (GithubLinkedIn) was kind enough to write this, so a warm round of applause for him!

Parameters and What They Do

v_schema_name and v_table_name: required, self-explanatory.

v_drop_primary_keys boolean default false: if you pass in true, we’ll delete the table’s primary keys as well. Defaults to false because in index tuning classes, the primary key isn’t usually up for debate or reassignment.

v_force_drop_with_constraints boolean default false: I don’t use this in the training classes, but Dr. Ducharme was nice enough to code it, so in it stays!

v_drop_concurrently boolean default false: I don’t use this in the training classes, but Dr. Ducharme was nice enough to code it, so in it stays!

v_list_indexes_being_dropped boolean default true: if false, the only output result just says “Success”, and has one row rather than one row per index.

v_print_drops_but_dont_execute boolean default false: if true, that means you don’t trust our code or you want to run the drops yourself. Neither of those hurts my feelings.

For Support or Changes

Use Github Issues in the Box of Tricks repo. When filing an issue, make sure to include your Postgres version and any screenshots to help explain the problem you’re running into.

For feature requests, please be prepared to contribute the code yourself. Open source doesn’t mean free consulting or development from others – it means being willing to roll up your sleeves if there’s something you really want included. Don’t be scared – the source code on this one is really simple! Hop in there and get started.

Hope drop_indexes makes your job & training easier. Cheers!

Previous Post
Announcing Fundamentals of Python: The Course for Data Professionals
Next Post
How to Use check_indexes to Learn About Your Postgres Database’s Indexes

4 Comments. Leave new

  • Russell Johnson
    April 4, 2025 9:45 am

    Is there a technical reason this was written as a table function instead of a stored procedure?

    Reply
    • Great question! The answer I point you to will depend on your experience level – I’m going to guess that you don’t use Postgres regularly, and aren’t a member of my Early Access training where folks watch me live code this stuff?

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed