postgresql - Performance and index usage for function based queries and function calls inside functions -
i'm trying improve performance of postgresql functions , function calls inside functions.
in our application, postgresql (9.6) queries totally based on functions. example, in order fetch lot of "items" code lines below might used:
create or replace function public.item_read_one( "itemid" integer) returns jsonb $body$ declare outputvariable jsonb; begin select row_to_json (my_subquery) ( select id, "simplefield1", "simplefield2", item_status(id,"crucialfield") status item_table id = $1 , deleted not true) my_subquery outputvariable ; return outputvariable; end; $body$ language plpgsql volatile cost 100; alter function public.item_read_one(integer) owner some_user;
you see, inside inner select
there - beside simple fields - function call. these inner functions plv8 based , contain lot of javascript logic, including database queries. example might such:
create or replace function public.item_status( "itemid" integer, "crucialfield" text) returns jsonb $body$ var returnstatus = {}; var myvariable; if(crucialfield == 'foo') { myvariable = plv8.execute('select other_table "itemid" = $1',[itemid]); } else if(crucialfield == 'sweetkitten') { myvariable = plv8.execute('select "somethingelse" kitten "itemid" = $1',[itemid]); } /* lot more javascript logic here. idea, hope. */ return returnstatus; $body$ language plv8 immutable cost 100; alter function public.item_status(integer, text) owner some_user;
aside question, whether design makes sense, problem is: how improve performance?
the data returned functions item_status pretty stable, make sense put index, don't know how.
postgresql version used 9.6.
Comments
Post a Comment