<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Pg_index on Postgres Scripts</title><link>https://www.postgresscripts.com/tags/pg_index/</link><description>Recent content in Pg_index on Postgres Scripts</description><generator>Hugo -- gohugo.io</generator><language>en</language><copyright>PostgresScripts.com</copyright><lastBuildDate>Sat, 04 Apr 2026 00:00:00 +0000</lastBuildDate><atom:link href="https://www.postgresscripts.com/tags/pg_index/index.xml" rel="self" type="application/rss+xml"/><item><title>List PostgreSQL Object Comments with SQL</title><link>https://www.postgresscripts.com/post/list-postgresql-object-comments/</link><pubDate>Sat, 04 Apr 2026 00:00:00 +0000</pubDate><guid>https://www.postgresscripts.com/post/list-postgresql-object-comments/</guid><description>
&lt;h2 id="list-postgresql-object-comments-with-sql"&gt;List PostgreSQL Object Comments with SQL&lt;/h2&gt;
&lt;p&gt;PostgreSQL allows you to attach plain-text comments to tables, columns, indexes, functions, and other database objects using the &lt;code&gt;COMMENT ON&lt;/code&gt; command. These comments are stored in the system catalog and are visible in psql, pgAdmin, and any tool that reads &lt;code&gt;pg_description&lt;/code&gt;. They are one of the most underused features for keeping a schema self-documenting.&lt;/p&gt;
&lt;p&gt;This SQL query lists comments on every column across all tables in your database, showing the schema, table name, column name, and the comment text. It is useful for auditing schema documentation, onboarding new team members, and verifying that comments are in place before a schema handover.&lt;/p&gt;</description></item><item><title>Find PostgreSQL Index Bloat and Wasted Space</title><link>https://www.postgresscripts.com/post/find-postgresql-index-bloat-and-wasted-space/</link><pubDate>Tue, 31 Mar 2026 00:00:00 +0000</pubDate><guid>https://www.postgresscripts.com/post/find-postgresql-index-bloat-and-wasted-space/</guid><description>
&lt;h2 id="how-to-find-index-bloat-and-wasted-space-in-postgresql"&gt;How to Find Index Bloat and Wasted Space in PostgreSQL&lt;/h2&gt;
&lt;p&gt;Just like tables, PostgreSQL indexes can become bloated over time. When rows are updated or deleted, the old index entries are not removed immediately. They stay in the index as dead entries, wasting space and slowing down index scans.&lt;/p&gt;
&lt;p&gt;B-tree indexes are the most common type in PostgreSQL and the most prone to bloat. This query estimates how much space is wasted in each B-tree index, so you can decide which ones need to be rebuilt.&lt;/p&gt;</description></item><item><title>Identify Large PostgreSQL Indexes for Optimization</title><link>https://www.postgresscripts.com/post/identify-large-indexes-for-postgresql-database-optimization-with-sql/</link><pubDate>Sun, 17 Mar 2024 00:00:00 +0000</pubDate><guid>https://www.postgresscripts.com/post/identify-large-indexes-for-postgresql-database-optimization-with-sql/</guid><description>
&lt;h2 id="unveiling-your-postgresql-indexes-a-deep-dive-with-code"&gt;Unveiling Your PostgreSQL Indexes: A Deep Dive with Code&lt;/h2&gt;
&lt;p&gt;In the realm of relational databases, efficient data retrieval is paramount. PostgreSQL achieves this partly through the magic of indexes. But have you ever wondered what lurks beneath the surface of these indexes? This post delves into a powerful Postgres Database code snippet that empowers you to unlock the secrets hidden within your indexes.&lt;/p&gt;
&lt;script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1012089347386563"
crossorigin="anonymous"&gt;&lt;/script&gt;
&lt;ins class="adsbygoogle"
style="display:block"
data-ad-client="ca-pub-1012089347386563"
data-ad-slot="7945792173"
data-ad-format="auto"
data-full-width-responsive="true"&gt;&lt;/ins&gt;
&lt;script&gt;
(adsbygoogle = window.adsbygoogle || []).push({});
&lt;/script&gt;
&lt;h2 id="sample-code"&gt;Sample Code&lt;/h2&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-fallback" data-lang="fallback"&gt;&lt;span class="line"&gt;&lt;span class="ln"&gt; 1&lt;/span&gt;&lt;span class="cl"&gt;SELECT n.nspname AS schemaname,
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="ln"&gt; 2&lt;/span&gt;&lt;span class="cl"&gt; c.relname AS tablename,
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="ln"&gt; 3&lt;/span&gt;&lt;span class="cl"&gt; i.relname AS indexname,
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="ln"&gt; 4&lt;/span&gt;&lt;span class="cl"&gt; t.spcname AS tablespace,
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="ln"&gt; 5&lt;/span&gt;&lt;span class="cl"&gt; pg_get_indexdef(i.oid) AS indexdef,
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="ln"&gt; 6&lt;/span&gt;&lt;span class="cl"&gt; pg_size_pretty(pg_total_relation_size(i.oid)) AS index_size
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="ln"&gt; 7&lt;/span&gt;&lt;span class="cl"&gt;FROM pg_index x
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="ln"&gt; 8&lt;/span&gt;&lt;span class="cl"&gt; JOIN pg_class c ON c.oid = x.indrelid
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="ln"&gt; 9&lt;/span&gt;&lt;span class="cl"&gt; JOIN pg_class i ON i.oid = x.indexrelid
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="ln"&gt;10&lt;/span&gt;&lt;span class="cl"&gt; LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="ln"&gt;11&lt;/span&gt;&lt;span class="cl"&gt; LEFT JOIN pg_tablespace t ON t.oid = i.reltablespace
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="ln"&gt;12&lt;/span&gt;&lt;span class="cl"&gt;WHERE (c.relkind = ANY (ARRAY[&amp;#39;r&amp;#39;::&amp;#34;char&amp;#34;, &amp;#39;m&amp;#39;::&amp;#34;char&amp;#34;, &amp;#39;p&amp;#39;::&amp;#34;char&amp;#34;]))
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="ln"&gt;13&lt;/span&gt;&lt;span class="cl"&gt; AND (i.relkind = ANY (ARRAY[&amp;#39;i&amp;#39;::&amp;#34;char&amp;#34;, &amp;#39;I&amp;#39;::&amp;#34;char&amp;#34;]))
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="ln"&gt;14&lt;/span&gt;&lt;span class="cl"&gt; order by index_size asc;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id="unveiling-your-postgresql-indexes-a-deep-dive-with-code-1"&gt;Unveiling Your PostgreSQL Indexes: A Deep Dive with Code&lt;/h2&gt;
&lt;p&gt;In the realm of relational databases, efficient data retrieval is paramount. PostgreSQL achieves this partly through the magic of indexes. But have you ever wondered what lurks beneath the surface of these indexes? This post delves into a powerful Postgres Database code snippet that empowers you to unlock the secrets hidden within your indexes.&lt;/p&gt;</description></item></channel></rss>