Re: FW: view derived from view doesn't use indexes

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Russell Keane <Russell(dot)Keane(at)inps(dot)co(dot)uk>
Cc: "pgsql-sql(at)postgresql(dot)org" <pgsql-sql(at)postgresql(dot)org>
Subject: Re: FW: view derived from view doesn't use indexes
Date: 2012-07-26 15:51:40
Message-ID: 22988.1343317900@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Russell Keane <Russell(dot)Keane(at)inps(dot)co(dot)uk> writes:
> Using PG 9.0 and given the following definitions:

> CREATE OR REPLACE FUNCTION status_to_flag(status character)
> RETURNS integer AS
> $BODY$
> ...
> $BODY$
> LANGUAGE plpgsql

> CREATE OR REPLACE VIEW test_view1 AS
> SELECT status_to_flag(test_table.status) AS flag,
> test_table.code_id
> FROM test_table;

> CREATE OR REPLACE VIEW test_view2 AS
> SELECT *
> FROM test_view1
> WHERE test_view1.flag = 1;

I think the reason why the planner is afraid to flatten this is that the
function is (by default) marked VOLATILE. Volatile functions in the
select list are an optimization fence. That particular function looks
like it should be IMMUTABLE instead, since it depends on no database
state. If it does look at database state, you can probably use STABLE.

http://www.postgresql.org/docs/9.0/static/xfunc-volatility.html

regards, tom lane

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Russell Keane 2012-07-27 10:33:07 Re: FW: view derived from view doesn't use indexes
Previous Message Russell Keane 2012-07-26 14:59:32 FW: view derived from view doesn't use indexes