Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Wfv7b-0007yi-Fe for pgsql-bugs@arkaria.postgresql.org; Thu, 01 May 2014 17:50:11 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1Wfv7a-0004yv-Dz for pgsql-bugs@arkaria.postgresql.org; Thu, 01 May 2014 17:50:10 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Wfn0V-0002oA-Nc for pgsql-bugs@postgresql.org; Thu, 01 May 2014 09:10:19 +0000 Received: from wrigleys.postgresql.org ([204.145.124.246]) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Wfn0H-00020Q-G6 for pgsql-bugs@postgresql.org; Thu, 01 May 2014 09:10:15 +0000 MIME-Version: 1.0 Subject: BUG #10194: Stable function in select clause cann't be optimized to one call? To: pgsql-bugs@postgresql.org From: digoal@126.com Date: Thu, 01 May 2014 09:09:04 +0000 Message-ID: <20140501090904.1398.86628@wrigleys.postgresql.org> X-Pg-Spam-Score: -1.9 (-) Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org The following bug has been logged on the website: Bug reference: 10194 Logged by: digoal.zhou Email address: digoal@126.com PostgreSQL version: 9.3.4 Operating system: CentOS 6.5 x64 Description: When i use stable function and constant parameters in select clause, the function call many times(when use seq scan), but when i change this function to immutable, it call one time(it's insteaded to result by planner). And the same function in where clause , the stable and immutable function same call one time. Why stable function in select clause cann't optimized to one time call? Can we optimized it? See exp : digoal=# create table t7(id int); digoal=# insert into t7 values (1),(2),(3); digoal=# create or replace function f_t7(i int) returns int as $$ declare begin raise notice 'called'; return i; end; $$ language plpgsql stable; digoal=# select f_t7(1),* from t7; NOTICE: called NOTICE: called NOTICE: called f_t7 | id ------+---- 1 | 1 1 | 2 1 | 3 digoal=# alter function f_t7(int) immutable; digoal=# select f_t7(1),* from t7; NOTICE: called f_t7 | id ------+---- 1 | 1 1 | 2 1 | 3 (3 rows) digoal=# alter function f_t7(int) stable; digoal=# select * from t7 where id=f_t7(1); NOTICE: called NOTICE: called NOTICE: called NOTICE: called id ---- 1 digoal=# explain select * from t7 where id=f_t7(1); NOTICE: called QUERY PLAN -------------------------------------------------- Seq Scan on t7 (cost=0.00..1.79 rows=1 width=4) Filter: (id = f_t7(1)) (2 rows) digoal=# select * from t7 where f_t7(1)=1; NOTICE: called id ---- 1 2 3 (3 rows) digoal=# explain select * from t7 where f_t7(1)=1; QUERY PLAN -------------------------------------------------------- Result (cost=0.25..1.28 rows=3 width=4) One-Time Filter: (f_t7(1) = 1) -> Seq Scan on t7 (cost=0.25..1.28 rows=3 width=4) (3 rows) digoal=# select * from t7 where f_t7(1)=id; NOTICE: called NOTICE: called id ---- 1 (1 row) digoal=# explain select * from t7 where f_t7(1)=id; NOTICE: called QUERY PLAN ------------------------------------------------------------------------- Index Only Scan using idx_t7_id on t7 (cost=0.38..8.40 rows=1 width=4) Index Cond: (id = f_t7(1)) (2 rows) digoal=# alter function f_t7(int) immutable; ALTER FUNCTION digoal=# explain select * from t7 where f_t7(1)=id; NOTICE: called QUERY PLAN ------------------------------------------------------------------------- Index Only Scan using idx_t7_id on t7 (cost=0.13..8.15 rows=1 width=4) Index Cond: (id = 1) (2 rows) digoal=# explain select * from t7 where f_t7(1)=1; NOTICE: called QUERY PLAN -------------------------------------------------------------------------- Index Only Scan using idx_t7_id on t7 (cost=0.13..12.18 rows=3 width=4) (1 row) digoal=# set enable_seqscan=on; SET digoal=# explain select * from t7 where f_t7(1)=1; NOTICE: called QUERY PLAN -------------------------------------------------- Seq Scan on t7 (cost=0.00..1.03 rows=3 width=4) (1 row) digoal=# select * from t7 where f_t7(1)=1; NOTICE: called id ---- 1 2 3 (3 rows) digoal=# select * from t7 where f_t7(1)=id; NOTICE: called id ---- 1 (1 row) -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs