A collection of useful PL/pgSQL functions for PostgreSQL 16.14.
These are functions I've developed and used many times throughout my professional career. Most of them solve recurring administrative tasks, data maintenance, troubleshooting, and database inspection, making them useful for both DBAs and PL developers.
Obs: Last tested on PostgreSQL 16.14.
Truncates all user tables and restarts all sequences.
SELECT public.killer_tables();Disables triggers for all user tables.
SELECT public.disable_all_triggers();Enables triggers for all user tables.
SELECT public.enable_all_triggers();Synchronizes all sequences with the highest value found in their respective tables.
SELECT public.rebuild_all_sequences();Searches a value in all searchable columns within a schema.
SELECT *
FROM public.search_in_tables('%john%', 'public');Searches a value across all user schemas.
SELECT *
FROM public.search_all_database('%john%');Finds columns matching a given name.
SELECT *
FROM public.find_column('%customer%');Returns the exact number of rows for every user table.
SELECT *
FROM public.table_row_count();Generates a summary report of the current database.
SELECT *
FROM public.generate_database_report();Lists indexes that have never been used.
SELECT *
FROM public.find_unused_indexes();Finds duplicate indexes.
SELECT *
FROM public.find_duplicate_indexes();Lists tables that do not have a primary key.
SELECT *
FROM public.tables_without_primary_key();Lists foreign keys that do not have a supporting index.
SELECT *
FROM public.tables_without_index_fk();Runs VACUUM (or VACUUM ANALYZE) for every user table.
CALL public.vacuum_database();- PostgreSQL 16.14