0

Say I have 2 functions using third one. Say function check_permission(user_id) is used by get_company(user_id) and get_location(user_id).

How would execution plan cache work? I mean would it be making separate execution plans for check_permission and get_company functions or would it be one plan for get_company? There is a chance that execution plan will be more efficient if it is built for get_company and get_location individually even if they are both using check_permission function.

2

1 Answer 1

1

"It depends".

It could do either or both.

If check_permission is a LANGUAGE sql function that satisfies the requirements for inlining, and get_company and/or get_location are also LANGUAGE sql or use it as part of non-trivial queries, it might get inlined and planned twice, once for each caller, as part of the calling query.

Otherwise, it will generally get planned once, when first called by either caller.

By the way, consider using views instead of functions when practical. They're more efficient and give the planner more options. But there's less benefit if they need to be SECURITY_BARRIER views to guard against information leaks.

I wonder if you might be making a real mistake by focusing on planning time without (as far as you have shown) looking into how much planning time is actually impacting your performance. And you don't seem to be considering the significant overheads of doing work via the fmgr and plpgsql procedure handlers etc, vs raw queries, and weighing that against planning costs. I strongly advise you not to pursue this further until you've made sure you're optimally using prepared statements across the app, at least. Then measure relative costs, for your workload.

Sign up to request clarification or add additional context in comments.

8 Comments

In my knowledge views don't get their execution plan cached.
Views don't, but queries do, including queries that incorporate views. And overall you'll tend to get better results that way than by going through the fmgr interface for function calls, unless your function has an insanely complex query plan that's also very cheap to actually execute and cannot ever significantly benefit from inlining.
Queries get their execution plan cached? Thanks, good to know. It gives me more questions though. Please have a look at my other qustions here
I have herd that in plpgsql functions would cache query plans. Ad-hoc SQL doesn't, unless you explicitly make use of prepared queries. here
Yes, that's right. But presumably you're already using prepared statements if you're even thinking about stuff like this.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.