Book Image

PostgreSQL Cookbook

By : Chitij Chauhan
Book Image

PostgreSQL Cookbook

By: Chitij Chauhan

Overview of this book

<p>PostgreSQL is an open source database management system. It is used for a wide variety of development practices such as software and web design, as well as for handling large datasets (big data).</p> <p>With the goal of teaching you the skills to master PostgreSQL, the book begins by giving you a glimpse of the unique features of PostgreSQL and how to utilize them to solve real-world problems. With the aid of practical examples, the book will then show you how to create and manage databases. You will learn how to secure PostgreSQL, perform administration and maintenance tasks, implement high availability features, and provide replication. The book will conclude by teaching you how to migrate information from other databases to PostgreSQL.</p>
Table of Contents (19 chapters)
PostgreSQL Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Finding unused indexes


It becomes necessary to check for unused indexes because indexes end up consuming a significant chunk of disk space, and if not monitored closely, they can consume unnecessary CPU cycles, more so in the case of them becoming fragmented.

Getting ready

In order to be able to find unused indexes in PostgreSQL, we need to ensure that the track_activities and track_counts configuration parameters are enabled in the postgresql.conf file. It is only when statistics are collected that we will be able to identify the unused indexes.

How to do it...

We can use the following query to identify unused indexes in PostgreSQL:

SELECT
    relid::regclass AS table, 
    indexrelid::regclass AS index, 
    pg_size_pretty(pg_relation_size(indexrelid::regclass)) AS index_size, 
    idx_tup_read, 
    idx_tup_fetch, 
    idx_scan
FROM pg_stat_user_indexes 
JOIN pg_index USING (indexrelid) 
WHERE idx_scan = 0 
AND indisunique IS FALSE;

   table    |        index         | index_size | idx_tup_read...