pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: guyren@relevantlogic.com
Cc: PG-General Mailing List <pgsql-general@postgresql.org>
Subject: Re: Advice on setting cost for function
Date: Thu, 13 Jun 2019 14:46:19 -0400
Message-ID: <11556.1560451579@sss.pgh.pa.us> (raw)
In-Reply-To: <27621de6-c05b-493c-bf53-49539cb04577@Spark>
References: <73351881-0cd6-4331-817b-2c67747940f5@Spark>
	<27621de6-c05b-493c-bf53-49539cb04577@Spark>

guyren@relevantlogic.com writes:
> The documentation in CREATE FUNCTION is fine as far is it goes regarding the COST setting, but that isn’t very far, and I haven’t had any luck finding good advice.

Fair complaint.  A quick data dump:

procost = 1 is supposed to represent the cost of a simple built-in
function --- think addition or comparison.  Expensive C functions,
such as tsvector parsing, are supposed to have procosts like 10 or 100.
Keep in mind that procost is actually scaled by cpu_operator_cost
(0.0025 typically) before being folded into a plan cost estimate.

PL functions are inevitably going to be way more expensive than a simple
built-in function.  It's likely that the default procost setting of 100
is a drastic underestimate in most cases, and that a saner default might
be more like 1000.  I'm hesitant to change that for fear of causing
surprising plan changes, though.

Both the CREATE FUNCTION man page and the pg_proc catalog documentation
claim that for a set-returning function, procost is per-output-row.
That may have been true when written but it seems to be horsepucky now;
it's only charged once regardless.  We should change the docs.

> If I have a function that looks up a single value from an index, should that be lower than 100? 20, say?

Well, if you accept the planner's default estimate that a single-row
indexed lookup costs around 8 cost units (twice random_page_cost),
then a function embodying that should also cost that much; dividing by
cpu_operator_cost leads to the conclusion that its procost should be
3200.  There are lots of reasons to be more optimistic than that, but
for sure I wouldn't think that a function that embodies database access
should have procost less than 100.

> I can imagine that a SQL function which is just inlined might ignore the COST estimate. Is that so? Are there other such considerations?

Yeah, once it gets inlined its procost marking is no longer a factor;
we'll look at the substituted expression instead.

> If I have a function that calls multiple other functions, each of which does a reasonable amount of work, should I set the caller to a higher COST, or will Postgres use the costs of the functions it calls?

Nope, you'll need to adjust the cost of the calling function.
Except for the inlined case, PG will just take the cost marking
at face value.

I believe that the PostGIS people just recently increased the
cost markings on all their expensive functions to better reflect
reality.  You might want to go dig in their git repo to see what
they did (I don't think those changes are released yet).

> In general, a section in the CREATE FUNCTION documentation of two or three paragraphs with a few examples and general guidelines would be very helpful. I would be happy to write it if someone will explain it to me.

Have at it ...

			regards, tom lane





view thread (2+ messages)

Message-ID: <11556.1560451579@sss.pgh.pa.us>
Permalink:  ../11556.1560451579@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/11556.1560451579@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: tgl@sss.pgh.pa.us, guyren@relevantlogic.com
  Subject: Re: Advice on setting cost for function
  In-Reply-To: <11556.1560451579@sss.pgh.pa.us>
  References: <73351881-0cd6-4331-817b-2c67747940f5@Spark> <27621de6-c05b-493c-bf53-49539cb04577@Spark> <11556.1560451579@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox