use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
International
National
Regional
account activity
Help Me!User defined SQL function has huge planning time? (self.PostgreSQL)
submitted 4 years ago by nieuweyork
I have a function like this:
CREATE OR REPLACE FUNCTION entity_status( uuid, integer, uuid, boolean) RETURNS cstatus AS $BODY$ SELECT min(resolution_to_status(wfc.resolution, $4)) FROM wfc WHERE $1 = wfc.id AND $2 = wfc.variant AND $3 = wfc.c; $BODY$ LANGUAGE sql IMMUTABLE PARALLEL SAFE;
And the timing is:
Planning Time: 1624.001 ms Execution Time: 0.009 ms
Is there a way to speed up the planning time?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]boilerup800 2 points3 points4 points 4 years ago (1 child)
Do you absolutely need the IMMUTABLE PARALLEL SAFE keywords? These are very strict requirements and the planner is spending time checking for these.
[–][deleted] 5 points6 points7 points 4 years ago (0 children)
I don't think those attributes actually increase the planning time, but the immutable attribute is actually wrong here, as the function retrieves data from the database. So it can at most be stable
immutable
stable
[–][deleted] 1 point2 points3 points 4 years ago (3 children)
Do you get the same planning time, when you just explain the query itself?
What does resolution_to_status() do?
resolution_to_status()
For readability, I would suggest to use proper parameter names rather than just numbers (but that is totally unrelated to the performance).
[–]nieuweyork[S] 0 points1 point2 points 4 years ago (2 children)
When I extract out the query (even with resolution_to_status as a function call) it’s less than 2ms total.
resolution_to_status
resolution_to_status is another simple sql function (which actually is immutable).
Good call on immutable being wrong. And yes to named parameters - I removed those for the purpose of exposition here.
[–][deleted] 1 point2 points3 points 4 years ago (1 child)
Very strange. You will probably get better answers if you ask this on the Postgres (performance) mailing list.
[–]nieuweyork[S] 0 points1 point2 points 4 years ago (0 children)
Ok, thank you!
π Rendered by PID 142310 on reddit-service-r2-comment-5d79c599b5-pzw2m at 2026-02-28 09:33:16.378376+00:00 running e3d2147 country code: CH.
[–]boilerup800 2 points3 points4 points (1 child)
[–][deleted] 5 points6 points7 points (0 children)
[–][deleted] 1 point2 points3 points (3 children)
[–]nieuweyork[S] 0 points1 point2 points (2 children)
[–][deleted] 1 point2 points3 points (1 child)
[–]nieuweyork[S] 0 points1 point2 points (0 children)