Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you change the function you will have to rebuild the index.</p> <pre><code>create table t (i integer); insert into t (i) select generate_series(1, 100000); analyze t; </code></pre> <p>A simple function to return the opposite integer:</p> <pre><code>create or replace function f(i integer) returns integer as $$ select i * -1; $$ immutable language sql; </code></pre> <p>And the index on it:</p> <pre><code>create index t_i_index on t(f(i)); </code></pre> <p>The index is used:</p> <pre><code>explain select * from t order by f(i); QUERY PLAN --------------------------------------------------------------------------- Index Scan using t_i_index on t (cost=0.00..3300.26 rows=100000 width=4) </code></pre> <p>Now the function is changed to return the integer itself:</p> <pre><code>create or replace function f(i integer) returns integer as $$ select i; $$ immutable language sql; </code></pre> <p>And the index is not used anymore:</p> <pre><code>explain select * from t order by f(i); QUERY PLAN --------------------------------------------------------------- Sort (cost=11116.32..11366.32 rows=100000 width=4) Sort Key: i -&gt; Seq Scan on t (cost=0.00..1443.00 rows=100000 width=4) </code></pre> <p>If the index is rebuilt</p> <pre><code>reindex index t_i_index; </code></pre> <p>It is used again:</p> <pre><code>explain select * from t order by f(i); QUERY PLAN --------------------------------------------------------------------------- Index Scan using t_i_index on t (cost=0.00..4376.26 rows=100000 width=4) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload