From dc8f1a52c9d074b8de2c3151904cef6cc72f51ae Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Mon, 28 Mar 2022 09:29:13 -0500 Subject: [PATCH] Add Get The Size On Disk Of An Index as a Postgres til --- README.md | 3 +- postgres/get-the-size-on-disk-of-an-index.md | 29 ++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 postgres/get-the-size-on-disk-of-an-index.md diff --git a/README.md b/README.md index c80ba91..13b5c15 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ pairing with smart people at Hashrocket. For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186). -_1188 TILs and counting..._ +_1189 TILs and counting..._ --- @@ -592,6 +592,7 @@ _1188 TILs and counting..._ - [Generate Series Of Numbers](postgres/generate-series-of-numbers.md) - [Generating UUIDs With pgcrypto](postgres/generating-uuids-with-pgcrypto.md) - [Get A Quick Approximate Count Of A Table](postgres/get-a-quick-approximate-count-of-a-table.md) +- [Get The Size On Disk of An Index](postgres/get-the-size-on-disk-of-an-index.md) - [Get The Size Of A Database](postgres/get-the-size-of-a-database.md) - [Get The Size Of A Table](postgres/get-the-size-of-a-table.md) - [Get The Size Of An Index](postgres/get-the-size-of-an-index.md) diff --git a/postgres/get-the-size-on-disk-of-an-index.md b/postgres/get-the-size-on-disk-of-an-index.md new file mode 100644 index 0000000..07ade2b --- /dev/null +++ b/postgres/get-the-size-on-disk-of-an-index.md @@ -0,0 +1,29 @@ +# Get The Size On Disk Of An Index + +Indexes, when added to the right columns, can provide massive performance gains +for certain queries. Indexes aren't free though. It is worth noting that they +take up disk space. The amount of disk space they take is generally irrelevant, +but it is at least worth being aware of. Especially if you're deal with a +massive table. + +You can check the current size of an index on disk with [PostgreSQL's +`pg_relation_size` +function](https://www.postgresql.org/docs/current/functions-admin.html). + +First, you'll want to look up the name of the index you're curious about. +Running `\d table_name` (replacing `table_name` with the name of the table the +index is on) will show you everything about the table including its indexes and +their names. + +Then run the following query: + +```sql +select pg_size_pretty(pg_relation_size('index_users_on_email')); + pg_size_pretty +---------------- + 41 MB +(1 row) +``` + +This one is pretty small. They can get pretty big though. I've seen some that +take up over 1GB on disk.