{"id":2193,"date":"2025-02-19T15:21:13","date_gmt":"2025-02-19T08:21:13","guid":{"rendered":"https:\/\/www.pgtutorial.com\/?page_id=2193"},"modified":"2025-03-07T10:45:42","modified_gmt":"2025-03-07T03:45:42","slug":"postgresql-index","status":"publish","type":"page","link":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-index\/","title":{"rendered":"PostgreSQL Index"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to use PostgreSQL Index to increase the query performance.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='where-postgresql-stores-the-data'>Where PostgreSQL stores the data <a href=\"#where-postgresql-stores-the-data\" class=\"anchor\" id=\"where-postgresql-stores-the-data\" title=\"Anchor for Where PostgreSQL stores the data\">#<\/a><\/h2>\n\n\n\n<p>First, connect to the <code>inventory<\/code> database on the local PostgreSQL using <code>psql<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\">psql -U postgres -d inventory<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>It&#8217;ll prompt you to enter a password for the user <code>postgres<\/code>.<\/p>\n\n\n\n<p>Second, show the location of the data directory using the following <code>SHOW<\/code> command:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SHOW<\/span> data_directory;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>If you use windows, you&#8217;ll see the output like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\">           data_directory\n<span class=\"hljs-comment\">-------------------------------------<\/span>\n C:\/Program Files\/PostgreSQL\/<span class=\"hljs-number\">17<\/span>\/data<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"note\">If you use macOS or Linux, you&#8217;ll see a different path. Additionally, if you don&#8217;t use the PostgreSQL 17, you&#8217;ll get the different version number (17) in the returned path.<\/p>\n\n\n\n<p>The data directory is a directory that PostgreSQL stores database files, configurations, and logs.<\/p>\n\n\n\n<p>Third, view the directories of the <code>base<\/code> directory:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\">C:\/Program Files\/PostgreSQL\/<span class=\"hljs-number\">17<\/span>\/data\/base\/<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>There will be directories whose names are numbers. Each directory represents a database in your PostgreSQL server.<\/p>\n\n\n\n<p>Fourth, retrieve the <code>oid<\/code> and database name:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  <span class=\"hljs-type\">oid<\/span>,\n  datname\n<span class=\"hljs-keyword\">FROM<\/span>\n  pg_database;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\">  <span class=\"hljs-type\">oid<\/span>  |    datname\n<span class=\"hljs-comment\">-------+----------------<\/span>\n     <span class=\"hljs-number\">5<\/span> | postgres\n     <span class=\"hljs-number\">1<\/span> | template1\n     <span class=\"hljs-number\">4<\/span> | template0\n <span class=\"hljs-number\">46769<\/span> | inventory\n...<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>An <code>oid<\/code> (object identifier) is a unique identifier that PostgreSQL assigns to a database object, such as a database or a table.<\/p>\n\n\n\n<p>The oid <code>46769<\/code> corresponds to the directory that stores the data of the <code>inventory<\/code> database.<\/p>\n\n\n\n<p>Fifth, find the <code>oid<\/code> of the tables in the <code>inventory<\/code> database:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  c.oid,\n  c.relname\n<span class=\"hljs-keyword\">FROM<\/span>\n  pg_class c\n  <span class=\"hljs-keyword\">JOIN<\/span> pg_namespace n <span class=\"hljs-keyword\">ON<\/span> n.oid = c.relnamespace\n<span class=\"hljs-keyword\">WHERE<\/span>\n  c.relkind = <span class=\"hljs-string\">'r'<\/span>\n  <span class=\"hljs-keyword\">AND<\/span> n.nspname <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-keyword\">IN<\/span> (<span class=\"hljs-string\">'pg_catalog'<\/span>, <span class=\"hljs-string\">'information_schema'<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\">  <span class=\"hljs-type\">oid<\/span>  |      relname\n<span class=\"hljs-comment\">-------+-------------------<\/span>\n <span class=\"hljs-number\">46908<\/span> | users\n <span class=\"hljs-number\">46917<\/span> | profiles\n <span class=\"hljs-number\">46936<\/span> | categories\n <span class=\"hljs-number\">46928<\/span> | brands\n <span class=\"hljs-number\">46949<\/span> | products\n <span class=\"hljs-number\">46977<\/span> | product_tags\n <span class=\"hljs-number\">46970<\/span> | tags\n <span class=\"hljs-number\">46999<\/span> | inventories\n <span class=\"hljs-number\">46993<\/span> | warehouses\n <span class=\"hljs-number\">47022<\/span> | transactions\n <span class=\"hljs-number\">47429<\/span> | product_groups\u00a0\u00a0<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='how-postgresql-stores-the-data'>How PostgreSQL Stores the data <a href=\"#how-postgresql-stores-the-data\" class=\"anchor\" id=\"how-postgresql-stores-the-data\" title=\"Anchor for How PostgreSQL Stores the data\">#<\/a><\/h2>\n\n\n\n<p>PostgreSQL stores data of a table using a heap file storage model. Each table corresponds to a <strong>heap file<\/strong>.<\/p>\n\n\n\n<p>For example, PostgreSQL stores the data of the <code>products<\/code> table in a heap file with the name <code>46949<\/code>. You can find the files in the database directory:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\">C:\/Program Files\/PostgreSQL\/<span class=\"hljs-number\">17<\/span>\/data\/base\/<span class=\"hljs-number\">46949<\/span>\/<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Each heap file consists of <strong>blocks<\/strong> (or <strong>pages<\/strong>). Typically, a block has a size of 8KB by default. Each block contains multiple <strong>tuples<\/strong> (or rows).<\/p>\n\n\n\n<p>Additionally, PostgreSQL maintains other information that determines which rows are accessible.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='full-table-scan'>Full table scan <a href=\"#full-table-scan\" class=\"anchor\" id=\"full-table-scan\" title=\"Anchor for Full table scan\">#<\/a><\/h2>\n\n\n\n<p>When retrieving data from a table, PostgreSQL has to read data from the heap file and load it to the memory for filtering the rows.<\/p>\n\n\n\n<p>A full table scan means that PostgreSQL must read all rows from a heap file (table) to memory and search for rows.<\/p>\n\n\n\n<p>Most of the time, a full table scan is not efficient because PostgreSQL must read every block, even if only a few rows match the condition.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-postgresql-index'>Introduction to PostgreSQL Index <a href=\"#introduction-to-postgresql-index\" class=\"anchor\" id=\"introduction-to-postgresql-index\" title=\"Anchor for Introduction to PostgreSQL Index\">#<\/a><\/h2>\n\n\n\n<p>An index is a separate data structure that allows PostgreSQL to locate rows quickly without scanning the table.<\/p>\n\n\n\n<p class=\"note\">An index is similar to an index in a book, which allows you to find information quickly without reading every page.<\/p>\n\n\n\n<p>Here&#8217;s how you would create an index:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Step 1<\/strong>. Identify one or more columns you want to have fast lookups on.<\/li>\n\n\n\n<li><strong>Step 2<\/strong>. Extract data from specified columns and the corresponding block for each.<\/li>\n\n\n\n<li><strong>Step 3<\/strong>. Sort the data. For example, sort the text data alphabetically, numbers from low to high, etc.<\/li>\n\n\n\n<li><strong>Step 4<\/strong>. Organize data into a B-tree structure, evenly distributing data in leaf nodes from left to right.<\/li>\n\n\n\n<li><strong>Step 5<\/strong>. Use the index to locate the exact rows.<\/li>\n<\/ul>\n\n\n\n<p>We&#8217;ll create an index to speed up the following query:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  product_name,\n  price\n<span class=\"hljs-keyword\">FROM<\/span>\n  products\n<span class=\"hljs-keyword\">WHERE<\/span>\n  product_name = <span class=\"hljs-string\">'Xperia 1 VI'<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><strong>Step 1.<\/strong> The column that we want to have a fast lookup on the <code>products<\/code> table is <code>product_name<\/code>:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2025\/02\/postgresql-index.svg\" alt=\"PostgreSQL Index\" class=\"wp-image-2197\"\/><\/figure>\n\n\n\n<p><strong>Step 2.<\/strong> Extract data from the <code>product_name<\/code> column. Assuming that the table has four rows with the following product names:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Galaxy Z Fold 5<\/li>\n\n\n\n<li>AirPods Pro 3<\/li>\n\n\n\n<li>Xperia 1 VI<\/li>\n\n\n\n<li>Watch Series 9<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2025\/02\/postgresql-index-extracting.svg\" alt=\"PostgreSQL index - extracting\" class=\"wp-image-2198\"\/><\/figure>\n\n\n\n<p><strong>Step 3.<\/strong> Sort the data. For example, sort the text data alphabetically, numbers from low to high, etc.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>AirPods Pro 3<\/li>\n\n\n\n<li>Galaxy Z Fold 5<\/li>\n\n\n\n<li>Watch Series 9<\/li>\n\n\n\n<li>Xperia 1 VI<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2025\/02\/postgresql-index-sort-rows.svg\" alt=\"PostgreSQL index - sort rows\" class=\"wp-image-2200\"\/><\/figure>\n\n\n\n<p><strong>Step 4.<\/strong> Organize the data in the B-tree data structure for fast lookup.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2025\/02\/postgresql-index-btree.svg\" alt=\"PostgreSQL Index BTree\" class=\"wp-image-2201\"\/><\/figure>\n\n\n\n<p><strong>Step 5.<\/strong> When searching for a product with the name <code>\"Xperia 1 VI\"<\/code>, PostgreSQL will start from the root node, go to the leaf node on the right, and find block #2 and index #1.<\/p>\n\n\n\n<p>Instead of loading all blocks from the heap file, PostgreSQL can directly read the row from block #2 and index #1.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='create-index-statement'>CREATE INDEX statement <a href=\"#create-index-statement\" class=\"anchor\" id=\"create-index-statement\" title=\"Anchor for CREATE INDEX statement\">#<\/a><\/h2>\n\n\n\n<p>To <a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-create-index\/\">create an index<\/a> in PostgreSQL, you use the <code><a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-create-index\/\">CREATE INDEX<\/a><\/code> statement:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">INDEX<\/span> &#91;index_name] \n<span class=\"hljs-keyword\">ON<\/span> <span class=\"hljs-built_in\">table_name<\/span> (column1, column2);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this syntax:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, specify an optional name for the index. If you omit it, PostgreSQL automatically generates a name with the naming convention <code>table_column_idx<\/code>.<\/li>\n\n\n\n<li>Second, provide the table name followed by one or more columns you want to have a fast lookup.<\/li>\n<\/ul>\n\n\n\n<p>For example, the following <code>CREATE INDEX<\/code> statement creates an index on the <code>product_name<\/code> column of the <code>products<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">INDEX<\/span> <span class=\"hljs-keyword\">ON<\/span> products(product_name);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>CREATE INDEX<\/code> statement does the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, extract values from the <code>product_name<\/code> column.<\/li>\n\n\n\n<li>Second, sort them alphabetically.<\/li>\n\n\n\n<li>Third, store it on disk as a separate data structure.<\/li>\n<\/ul>\n\n\n\n<p>When you find products by product names, PostgreSQL has a specific software component called a query optimizer that decides whether it should use the index or perform a full table scan.<\/p>\n\n\n\n<p>If the number of rows is relatively small, the query optimizer performs a full table scan because it is more efficient than reading the index and locating the data.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='benchmarking-queries'>Benchmarking queries <a href=\"#benchmarking-queries\" class=\"anchor\" id=\"benchmarking-queries\" title=\"Anchor for Benchmarking queries\">#<\/a><\/h2>\n\n\n\n<p>To check if a query performs a full table scan or utilizes an index, you can use the <code>EXPLAIN ANALYZE<\/code> keywords before a query:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">EXPLAIN<\/span> <span class=\"hljs-keyword\">ANALYZE<\/span> query;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>EXPLAIN ANALYZE<\/code> returns the planned and actual execution time of the query.<\/p>\n\n\n\n<p>For example, the following statement returns the planned and actual execution time of the query that finds a product with the name <code>'Xperia 1 VI'<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">EXPLAIN<\/span> <span class=\"hljs-keyword\">ANALYZE<\/span>\n<span class=\"hljs-keyword\">SELECT<\/span>\n  product_name,\n  price\n<span class=\"hljs-keyword\">FROM<\/span>\n  products\n<span class=\"hljs-keyword\">WHERE<\/span>\n  product_name = <span class=\"hljs-string\">'Xperia 1 VI'<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\">                                             QUERY PLAN\n<span class=\"hljs-comment\">----------------------------------------------------------------------------------------------------<\/span>\n Seq Scan <span class=\"hljs-keyword\">on<\/span> products  (<span class=\"hljs-keyword\">cost<\/span>=<span class=\"hljs-number\">0.00<\/span>.<span class=\"hljs-number\">.25<\/span><span class=\"hljs-number\">.83<\/span> <span class=\"hljs-keyword\">rows<\/span>=<span class=\"hljs-number\">1<\/span> width=<span class=\"hljs-number\">17<\/span>) (actual <span class=\"hljs-type\">time<\/span>=<span class=\"hljs-number\">0.035<\/span>.<span class=\"hljs-number\">.0<\/span><span class=\"hljs-number\">.036<\/span> <span class=\"hljs-keyword\">rows<\/span>=<span class=\"hljs-number\">0<\/span> loops=<span class=\"hljs-number\">1<\/span>)\n   <span class=\"hljs-keyword\">Filter<\/span>: ((product_name)::<span class=\"hljs-type\">text<\/span> = <span class=\"hljs-string\">'Xperia 1 VI'<\/span>::<span class=\"hljs-type\">text<\/span>)\n   <span class=\"hljs-keyword\">Rows<\/span> Removed <span class=\"hljs-keyword\">by<\/span> <span class=\"hljs-keyword\">Filter<\/span>: <span class=\"hljs-number\">26<\/span>\n Planning <span class=\"hljs-type\">Time<\/span>: <span class=\"hljs-number\">0.101<\/span> ms\n Execution <span class=\"hljs-type\">Time<\/span>: <span class=\"hljs-number\">0.054<\/span> ms<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The output reveals that the query performs a full table scan (Seq Scan on products).<\/p>\n\n\n\n<p class=\"note\">Note that you&#8217;ll learn how to read the output of the <code>EXPLAIN ANALYZE<\/code> statement in detail later.<\/p>\n\n\n\n<p>The <code>products<\/code> the table needs more rows to utilize the index.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id='generating-dummy-products'>Generating dummy products <a href=\"#generating-dummy-products\" class=\"anchor\" id=\"generating-dummy-products\" title=\"Anchor for Generating dummy products\">#<\/a><\/h3>\n\n\n\n<p>First, <a href=\"https:\/\/www.pgtutorial.com\/plpgsql\/plpgsql-functions\/\" target=\"_blank\" rel=\"noreferrer noopener\">create a user-defined function<\/a> that inserts a specified number of dummy products (<code>n<\/code>) into the <code>products<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-16\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">OR REPLACE<\/span> <span class=\"hljs-keyword\">FUNCTION<\/span> insert_products(n <span class=\"hljs-type\">INT<\/span>) \n<span class=\"hljs-keyword\">RETURNS<\/span> <span class=\"hljs-type\">VOID<\/span> \n<span class=\"hljs-keyword\">AS<\/span> $$<span class=\"pgsql\">\n<span class=\"hljs-keyword\">BEGIN<\/span>\n    <span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> products (product_name, price, safety_stock, gross_weight, brand_id, category_id, description)\n    <span class=\"hljs-keyword\">SELECT<\/span> \n        <span class=\"hljs-string\">'Product '<\/span> || i,\n        trunc(random() * <span class=\"hljs-number\">1000<\/span> + <span class=\"hljs-number\">1<\/span>)::<span class=\"hljs-type\">numeric<\/span>(<span class=\"hljs-number\">11<\/span>,<span class=\"hljs-number\">2<\/span>),\n        <span class=\"hljs-number\">0<\/span>,k\n        trunc(random() * <span class=\"hljs-number\">50<\/span> + <span class=\"hljs-number\">1<\/span>)::<span class=\"hljs-type\">numeric<\/span>(<span class=\"hljs-number\">10<\/span>,<span class=\"hljs-number\">2<\/span>),\n        trunc(random() * <span class=\"hljs-number\">10<\/span> + <span class=\"hljs-number\">1<\/span>)::<span class=\"hljs-type\">int<\/span>,\n        trunc(random() * <span class=\"hljs-number\">12<\/span> + <span class=\"hljs-number\">1<\/span>)::<span class=\"hljs-type\">int<\/span>,\n        <span class=\"hljs-string\">'Description for Product '<\/span> || i\n    <span class=\"hljs-keyword\">FROM<\/span> generate_series(<span class=\"hljs-number\">1<\/span>, n) <span class=\"hljs-keyword\">AS<\/span> i;\n<span class=\"hljs-keyword\">END<\/span>;\n$$<\/span> <span class=\"hljs-keyword\">LANGUAGE<\/span> plpgsql;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, insert 1,000 rows into the <code>products<\/code> table by calling the user-defined function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-17\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span> insert_products(<span class=\"hljs-number\">1000<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-17\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Third, retrieve the number of rows from the <code>products<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-18\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  COUNT(*)\n<span class=\"hljs-keyword\">FROM<\/span>\n  products;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-18\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-19\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"> count\n<span class=\"hljs-comment\">-------<\/span>\n  <span class=\"hljs-number\">1026<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-19\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\" id='utilizing-index'>Utilizing index <a href=\"#utilizing-index\" class=\"anchor\" id=\"utilizing-index\" title=\"Anchor for Utilizing index\">#<\/a><\/h3>\n\n\n\n<p>The following query finds the product with the name <code>'Xperia 1 VI'<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-20\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">EXPLAIN<\/span> <span class=\"hljs-keyword\">ANALYZE<\/span>\n<span class=\"hljs-keyword\">SELECT<\/span>\n  product_name,\n  price\n<span class=\"hljs-keyword\">FROM<\/span>\n  products\n<span class=\"hljs-keyword\">WHERE<\/span>\n  product_name = <span class=\"hljs-string\">'Xperia 1 VI'<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-20\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-21\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\">                                                             QUERY PLAN\n<span class=\"hljs-comment\">-------------------------------------------------------------------------------------------------------------------------------------<\/span>\n <span class=\"hljs-keyword\">Index<\/span> Scan <span class=\"hljs-keyword\">using<\/span> products_product_name_idx <span class=\"hljs-keyword\">on<\/span> products  (<span class=\"hljs-keyword\">cost<\/span>=<span class=\"hljs-number\">0.28<\/span>.<span class=\"hljs-number\">.8<\/span><span class=\"hljs-number\">.29<\/span> <span class=\"hljs-keyword\">rows<\/span>=<span class=\"hljs-number\">1<\/span> width=<span class=\"hljs-number\">17<\/span>) (actual <span class=\"hljs-type\">time<\/span>=<span class=\"hljs-number\">0.030<\/span>.<span class=\"hljs-number\">.0<\/span><span class=\"hljs-number\">.030<\/span> <span class=\"hljs-keyword\">rows<\/span>=<span class=\"hljs-number\">0<\/span> loops=<span class=\"hljs-number\">1<\/span>)\n   <span class=\"hljs-keyword\">Index<\/span> Cond: ((product_name)::<span class=\"hljs-type\">text<\/span> = <span class=\"hljs-string\">'Xperia 1 VI'<\/span>::<span class=\"hljs-type\">text<\/span>)\n Planning <span class=\"hljs-type\">Time<\/span>: <span class=\"hljs-number\">5.062<\/span> ms\n Execution <span class=\"hljs-type\">Time<\/span>: <span class=\"hljs-number\">0.045<\/span> ms<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-21\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The output indicates that the query utilizes the index to find the product named <code>'Xperia 1 VI'<\/code> (<code>Index Scan using products_product_name_idx on products<\/code>). It took <code>0.045ms<\/code> to find the product.<\/p>\n\n\n\n<p class=\"note\">Note that the number may be different depending on your server&#8217;s performance.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id='drop-index'>Drop index <a href=\"#drop-index\" class=\"anchor\" id=\"drop-index\" title=\"Anchor for Drop index\">#<\/a><\/h3>\n\n\n\n<p>To compare the performance of the query without an index, you can remove the index using the following <code><a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-drop-index\/\">DROP INDEX<\/a><\/code> statement:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-22\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">DROP<\/span> <span class=\"hljs-keyword\">INDEX<\/span> products_product_name_idx;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-22\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>And find the product without the index:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-23\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">EXPLAIN<\/span> <span class=\"hljs-keyword\">ANALYZE<\/span>\n<span class=\"hljs-keyword\">SELECT<\/span>\n  product_name,\n  price\n<span class=\"hljs-keyword\">FROM<\/span>\n  products\n<span class=\"hljs-keyword\">WHERE<\/span>\n  product_name = <span class=\"hljs-string\">'Xperia 1 VI'<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-23\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-24\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\">                                             QUERY PLAN\n<span class=\"hljs-comment\">----------------------------------------------------------------------------------------------------<\/span>\n Seq Scan <span class=\"hljs-keyword\">on<\/span> products  (<span class=\"hljs-keyword\">cost<\/span>=<span class=\"hljs-number\">0.00<\/span>.<span class=\"hljs-number\">.25<\/span><span class=\"hljs-number\">.83<\/span> <span class=\"hljs-keyword\">rows<\/span>=<span class=\"hljs-number\">1<\/span> width=<span class=\"hljs-number\">17<\/span>) (actual <span class=\"hljs-type\">time<\/span>=<span class=\"hljs-number\">0.140<\/span>.<span class=\"hljs-number\">.0<\/span><span class=\"hljs-number\">.140<\/span> <span class=\"hljs-keyword\">rows<\/span>=<span class=\"hljs-number\">0<\/span> loops=<span class=\"hljs-number\">1<\/span>)\n   <span class=\"hljs-keyword\">Filter<\/span>: ((product_name)::<span class=\"hljs-type\">text<\/span> = <span class=\"hljs-string\">'Xperia 1 VI'<\/span>::<span class=\"hljs-type\">text<\/span>)\n   <span class=\"hljs-keyword\">Rows<\/span> Removed <span class=\"hljs-keyword\">by<\/span> <span class=\"hljs-keyword\">Filter<\/span>: <span class=\"hljs-number\">1026<\/span>\n Planning <span class=\"hljs-type\">Time<\/span>: <span class=\"hljs-number\">2.591<\/span> ms\n Execution <span class=\"hljs-type\">Time<\/span>: <span class=\"hljs-number\">0.170<\/span> ms<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-24\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The query performs a full table scan (<code>Seq Scan on products<\/code>) and it took <code>0.170ms<\/code> to complete.<\/p>\n\n\n\n<p>As a result, the query with index is <code>3.7<\/code> times (<code>~0.170\/0.045<\/code>) faster than the one without an index.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='index-types'>Index types <a href=\"#index-types\" class=\"anchor\" id=\"index-types\" title=\"Anchor for Index types\">#<\/a><\/h2>\n\n\n\n<p>PostgreSQL supports various index types, each has a specific usage:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Index Type<\/th><th>Usage<\/th><\/tr><\/thead><tbody><tr><td><strong>B-Tree<\/strong><\/td><td>B-tree is the default index type. You can use B-tree index types for equality (<code>=<\/code>) and range queries (<code>&lt;<\/code>, <code>&lt;=<\/code>, <code>&gt;<\/code>, <code>&gt;=<\/code>). <\/td><\/tr><tr><td><strong>Hash<\/strong><\/td><td>Hash index type is optimized for simple equality lookups (<code>=<\/code>).<\/td><\/tr><tr><td><strong>GIN (Generalized Inverted Index)<\/strong><\/td><td>You use the GIN index types for indexing composite data types, such as <a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-json\/\">JSONB<\/a>, <a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-array\/\">arrays<\/a>, and <a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-full-text-search\/\">full-text search<\/a>.<\/td><\/tr><tr><td><strong>GiST (Generalized Search Tree)<\/strong><\/td><td>The GiST index type is useful for indexing complex data types, including ranges, geometric data, and full-text search.<\/td><\/tr><tr><td><strong>SP-GiST (Space-Partitioned GiST)<\/strong><\/td><td>The P-GiST index type is efficient for non-balanced tree structures, like hierarchical or spatial data.<\/td><\/tr><tr><td><strong>BRIN (Block Range INdex)<\/strong><\/td><td>The BRIN index type is suitable for very large tables with naturally ordered data, reducing storage and maintenance costs.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>PostgreSQL stores tables as heap files. Each heap file contains blocks. Each block has multiple tuples (rows).<\/li>\n\n\n\n<li>A full table scan means loading data from a heap file to memory to find matching rows.<\/li>\n\n\n\n<li>Use indexes to speed up a query.<\/li>\n\n\n\n<li>Use the <code>CREATE INDEX<\/code> statement to create an index.<\/li>\n\n\n\n<li>Use the <code>DROP INDEX<\/code> statement to drop an index.<\/li>\n\n\n\n<li>Use the <code>EXPLAIN ANALYZE<\/code> statement to provide the planned and actual execution time of a query.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='quiz'>Quiz <a href=\"#quiz\" class=\"anchor\" id=\"quiz\" title=\"Anchor for Quiz\">#<\/a><\/h2>\n\n\n\n<iframe loading=\"lazy\"\n  name=\"quiz\"\n  src=\"\/quiz\/?quiz=index\"\n  height=\"700\"\n  width=\"600\"\n  class=\"iframe\"\n><\/iframe>\n\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"2193\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-index\/\"\n\t\t\t\tdata-post-title=\"PostgreSQL Index\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"2193\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-index\/\"\n\t\t\t\tdata-post-title=\"PostgreSQL Index\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn how to use PostgreSQL Index to increase the query performance significantly.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":13,"menu_order":82,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-2193","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>PostgreSQL Index<\/title>\n<meta name=\"description\" content=\"In this tutorial, you&#039;ll learn how to use PostgreSQL Index to increase the query performance significantly.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-index\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PostgreSQL Index\" \/>\n<meta property=\"og:description\" content=\"In this tutorial, you&#039;ll learn how to use PostgreSQL Index to increase the query performance significantly.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-index\/\" \/>\n<meta property=\"og:site_name\" content=\"PostgreSQL Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2025-03-07T03:45:42+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-index\\\/\",\"url\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-index\\\/\",\"name\":\"PostgreSQL Index\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-index\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-index\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.pgtutorial.com\\\/wp-content\\\/uploads\\\/2025\\\/02\\\/postgresql-index.svg\",\"datePublished\":\"2025-02-19T08:21:13+00:00\",\"dateModified\":\"2025-03-07T03:45:42+00:00\",\"description\":\"In this tutorial, you'll learn how to use PostgreSQL Index to increase the query performance significantly.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-index\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-index\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-index\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.pgtutorial.com\\\/wp-content\\\/uploads\\\/2025\\\/02\\\/postgresql-index.svg\",\"contentUrl\":\"https:\\\/\\\/www.pgtutorial.com\\\/wp-content\\\/uploads\\\/2025\\\/02\\\/postgresql-index.svg\",\"caption\":\"PostgreSQL Index\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-index\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.pgtutorial.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PostgreSQL Tutorial\",\"item\":\"https:\\\/\\\/www.pgtutorial.com\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"PostgreSQL Index\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/#website\",\"url\":\"https:\\\/\\\/www.pgtutorial.com\\\/\",\"name\":\"PostgreSQL Tutorial\",\"description\":\"Learn PostgreSQL from Scratch\",\"alternateName\":\"PostgreSQL\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.pgtutorial.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"PostgreSQL Index","description":"In this tutorial, you'll learn how to use PostgreSQL Index to increase the query performance significantly.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-index\/","og_locale":"en_US","og_type":"article","og_title":"PostgreSQL Index","og_description":"In this tutorial, you'll learn how to use PostgreSQL Index to increase the query performance significantly.","og_url":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-index\/","og_site_name":"PostgreSQL Tutorial","article_modified_time":"2025-03-07T03:45:42+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-index\/","url":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-index\/","name":"PostgreSQL Index","isPartOf":{"@id":"https:\/\/www.pgtutorial.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-index\/#primaryimage"},"image":{"@id":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-index\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2025\/02\/postgresql-index.svg","datePublished":"2025-02-19T08:21:13+00:00","dateModified":"2025-03-07T03:45:42+00:00","description":"In this tutorial, you'll learn how to use PostgreSQL Index to increase the query performance significantly.","breadcrumb":{"@id":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-index\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-index\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-index\/#primaryimage","url":"https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2025\/02\/postgresql-index.svg","contentUrl":"https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2025\/02\/postgresql-index.svg","caption":"PostgreSQL Index"},{"@type":"BreadcrumbList","@id":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-index\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pgtutorial.com\/"},{"@type":"ListItem","position":2,"name":"PostgreSQL Tutorial","item":"https:\/\/www.pgtutorial.com\/"},{"@type":"ListItem","position":3,"name":"PostgreSQL Index"}]},{"@type":"WebSite","@id":"https:\/\/www.pgtutorial.com\/#website","url":"https:\/\/www.pgtutorial.com\/","name":"PostgreSQL Tutorial","description":"Learn PostgreSQL from Scratch","alternateName":"PostgreSQL","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.pgtutorial.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/2193","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/comments?post=2193"}],"version-history":[{"count":11,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/2193\/revisions"}],"predecessor-version":[{"id":2257,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/2193\/revisions\/2257"}],"up":[{"embeddable":true,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/13"}],"wp:attachment":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/media?parent=2193"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}