create or replace function foo3(int,text) returns setof iii as '
declare
CITY_ID ALIAS FOR $1;
IARTICUL_MASK ALIAS FOR $2;
r iii%rowtype;
c int;
BEGIN
IF IARTICUL_MASK IS NULL THEN
IARTICUL_MASK := ''%'';
END IF;
BEGIN
prepare foo3_inner(int,text) as select
i.id as id, i.eo_name as name, i.articul as articul, (case when group_id=57 then 2 else 1 end) as euro, i.fasovka as fasovka, i.site_path as site_path, ip.popular as popular, i.iarticul as iarticul, i.raspr as raspr from items i, items_popularity ip where i.del!=1 and ip.del!=1 and (i.min_col ISNULL or i.min_col != -1) and i.group_id in (4,5,7,16,17,18) and i.eo_show=1 and ip.item_id=
i.id and ip.city_id=$1 and i.iarticul like $2 limit 10;
RAISE NOTICE ''foo3_inner not exist and create'';
EXCEPTION WHEN duplicate_prepared_statement THEN
RAISE NOTICE ''foo3_inner exist'';
END;
for r in execute $$execute foo3_inner($$||CITY_ID||$$,''$$||IARTICUL_MASK||$$'')$$ loop
return next r;
end loop;
-- DEALLOCATE foo3_inner;
return;
EXCEPTION WHEN syntax_error THEN
-- в тех случаях, что я ловил, этого хватит, чтобы убить
-- DEALLOCATE foo3_inner;
RAISE NOTICE ''Error!!!!!!!!!!!!!!! % '',SQLSTATE;
return;
END
' language 'plpgsql';
SELECT * from foo3(1,'1072000001000_');
SELECT * from foo3(1,'1072000001000_');