Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace function used in index

What happens to an index built on a function when I change the underlying function?

Say, I have a function clean_name() defined as:

CREATE OR REPLACE FUNCTION clean_name(n text)
RETURNS TEXT AS
$BODY$
DECLARE
 rec TEXT;
BEGIN
 EXECUTE
  'SELECT Regexp_replace(' || quote_literal(n) || ', ''[a-z]'', '''', ''g'');'
 INTO rec;
RETURN rec;
END;
$BODY$ LANGUAGE plpgsql IMMUTABLE
;

Then create an index:

CREATE INDEX my_table_upper_name_btree
ON schema.my_table USING GIST (my_text_field);

But then I decide to redefine the function to remove upper case letters instead. What happens to the index I created? Does it change on its own? Do I DROP and CREATE again? VACUUM [ANALYZE] [FULL]?

(The function in question is similar, using instead a rather lengthy series of replacements that are still being tweaked but expected to be stable.)

like image 986
Dylan Hettinger Avatar asked Jul 11 '13 19:07

Dylan Hettinger


2 Answers

IMMUTABLE means "does not change" or "unchangeable". What you must do to strictly avoid violating that rule is drop the function and everything that depends on it then re-create it and the indexes that use it.

If you replace the function in-place you're taking responsibility for the consequences. Personally I think PostgreSQL should disallow OR REPLACE for IMMUTABLE functions for this reason, forcing you to jump through an extra hoop like setting ignore_immutable_checks_even_though_it_might_cause_incorrect_queries configuration option.

If you change an immutable function's behaviour then indexes based on the function are invalid. The server can't tell if the function's behaviour has changed or not; you might just have replaced it with an optimized version that has identical behaviour in every respect. So it won't invalidate the indexes for you, though perhaps it should, since if your function's behaviour does differ you can get incorrect query results for queries based on the function.

like image 58
Craig Ringer Avatar answered Sep 23 '22 18:09

Craig Ringer


If you change the function you will have to rebuild the index.

create table t (i integer);
insert into t (i)
select generate_series(1, 100000);
analyze t;

A simple function to return the opposite integer:

create or replace function f(i integer)
returns integer as $$
select i * -1;
$$ immutable language sql;

And the index on it:

create index t_i_index on t(f(i));

The index is used:

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)

Now the function is changed to return the integer itself:

create or replace function f(i integer)
returns integer as $$
select i;
$$ immutable language sql;

And the index is not used anymore:

explain select * from t order by f(i);
                          QUERY PLAN                           
---------------------------------------------------------------
 Sort  (cost=11116.32..11366.32 rows=100000 width=4)
   Sort Key: i
   ->  Seq Scan on t  (cost=0.00..1443.00 rows=100000 width=4)

If the index is rebuilt

reindex index t_i_index;

It is used again:

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)
like image 38
Clodoaldo Neto Avatar answered Sep 26 '22 18:09

Clodoaldo Neto