why SPI_execute_with_args don't use known values for optimalisation

From: "Pavel Stehule" <pavel(dot)stehule(at)gmail(dot)com>
To: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Cc: "Tom Lane" <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Subject: why SPI_execute_with_args don't use known values for optimalisation
Date: 2008-05-22 06:42:11
Message-ID: 162867790805212342y19bf1594h9b026f04ecdc0ee6@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hello

I would to use EXECUTE USING for optimalisation query

SELECT * FROM tab WHERE column = param OR param IS NULL

(if param isn't null then use param else ignore this params)

Planner can do it:

postgres=# explain select * from test where a = 100 or 100 is null;
QUERY PLAN
---------------------------------------------------------------------
Index Scan using test_idx on test (cost=0.00..8.28 rows=1 width=4)
Index Cond: (a = 100)
(2 rows)

postgres=# explain select * from test where a = null or null is null;
QUERY PLAN
------------------------------------------------------------
Seq Scan on test (cost=0.00..1393.00 rows=100000 width=4)
(1 row)

but:

create or replace function test_using(p integer)
returns int as $$
declare r record;
begin
for r in execute 'explain analyze select a from test where a = $1 or
$1 is null' using p loop
raise notice '%', r;
end loop;
return 0; end;
$$ language plpgsql;

postgres=# select test_using(100);
NOTICE: ("Seq Scan on test (cost=0.00..1643.00 rows=501 width=4)
(actual time=0.076..26.546 rows=1 loops=1)")
NOTICE: (" Filter: ((a = $1) OR ($1 IS NULL))")
NOTICE: ("Total runtime: 26.596 ms")
test_using
------------
0
(1 row)

Regards
Pavel Stehule

Browse pgsql-hackers by date

  From Date Subject
Next Message Pierre-Yves Strub 2008-05-22 11:31:55 Re: Fragments in tsearch2 headline
Previous Message Thomas H 2008-05-22 03:08:45 BUG #4186: set lc_messages does not work