Re: Documenting inlining SQL functions

Поиск
Список
Период
Сортировка
Искать

Re: Documenting inlining SQL functions

От:
"David G. Johnston" <david.g.johnston@gmail.com>
Дата:
On Sunday, July 6, 2025, Paul Jungwirth <pj@illuminatedcomputing.com> wrote:
The second patch adds a new <sect2> explaining how we inline SQL functions: both single-result and set-returning. Since this happens automatically, it makes a nice progression with the (easy) declarative annotations and the (hard) support functions.

The fact that attaching a set clause to the function definition (i.e., proconfig) prevents inlining is missing from this description.

David J.

Re: Documenting inlining SQL functions

От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
"David G. Johnston"  writes:
> On Sunday, July 6, 2025, Paul Jungwirth  wrote:
>> The second patch adds a new  explaining how we inline SQL
>> functions: both single-result and set-returning. Since this happens
>> automatically, it makes a nice progression with the (easy) declarative
>> annotations and the (hard) support functions.

> The fact that attaching a set clause to the function definition (i.e.,
> proconfig) prevents inlining is missing from this description.

TBH, I think trying to document this behavior at this level of detail
will be a disaster.  Our track record for keeping documentation in
sync with code is awful, and what exactly will make this area better
than average?  Even if this text is 100% accurate today, I'll bet a
good lunch that it will be wrong in two or three releases.

Our attitude for questions at the level of detail that this is
trying to cover has mostly been "someone who cares can go read
the code".  I grant that that's not a great answer, but it's
largely stood the test of time.

I think it's reasonable to document the fact that we can do SQL
function inlining in some cases, but not to try to specify which
cases those are in exhaustive detail.

			regards, tom lane


Re: Documenting inlining SQL functions

От:
"David G. Johnston" <david.g.johnston@gmail.com>
Дата:
On Friday, July 18, 2025, Tom Lane <tgl@sss.pgh.pa.us> wrote:
"David G. Johnston" <david.g.johnston@gmail.com> writes:
> On Sunday, July 6, 2025, Paul Jungwirth <pj@illuminatedcomputing.com> wrote:
>> The second patch adds a new <sect2> explaining how we inline SQL
>> functions: both single-result and set-returning. Since this happens
>> automatically, it makes a nice progression with the (easy) declarative
>> annotations and the (hard) support functions.

> The fact that attaching a set clause to the function definition (i.e.,
> proconfig) prevents inlining is missing from this description.

TBH, I think trying to document this behavior at this level of detail
will be a disaster.  Our track record for keeping documentation in
sync with code is awful, and what exactly will make this area better
than average?  Even if this text is 100% accurate today, I'll bet a
good lunch that it will be wrong in two or three releases.

You would have said the same three releases ago and been wrong.  Seems like you’d have to go back like 10 years when we figured out we messed up “case” and 5 beyond that when we added the hooks.


Our attitude for questions at the level of detail that this is
trying to cover has mostly been "someone who cares can go read
the code".  I grant that that's not a great answer, but it's
largely stood the test of time.

Then maybe we should use this patch to update the code comment (the part about hooks is 15 years old but missed updating the code comment)  Then, make this patch: see backend/optimizer/util/clauses.c#inline_function for a description of the algorithm.

But I’d still be inclined to have a user-facing document for this, add a note to the C code comment that it exists, and at least hopefully either both C and Docs are updated or neither are.

David J.

Re: Documenting inlining SQL functions

От:
Pavel Stehule <pavel.stehule@gmail.com>
Дата:
Hi

so 19. 7. 2025 v 5:08 odesílatel Tom Lane <tgl@sss.pgh.pa.us> napsal:
"David G. Johnston" <david.g.johnston@gmail.com> writes:
> On Sunday, July 6, 2025, Paul Jungwirth <pj@illuminatedcomputing.com> wrote:
>> The second patch adds a new <sect2> explaining how we inline SQL
>> functions: both single-result and set-returning. Since this happens
>> automatically, it makes a nice progression with the (easy) declarative
>> annotations and the (hard) support functions.

> The fact that attaching a set clause to the function definition (i.e.,
> proconfig) prevents inlining is missing from this description.

TBH, I think trying to document this behavior at this level of detail
will be a disaster.  Our track record for keeping documentation in
sync with code is awful, and what exactly will make this area better
than average?  Even if this text is 100% accurate today, I'll bet a
good lunch that it will be wrong in two or three releases.

Our attitude for questions at the level of detail that this is
trying to cover has mostly been "someone who cares can go read
the code".  I grant that that's not a great answer, but it's
largely stood the test of time.

I think it's reasonable to document the fact that we can do SQL
function inlining in some cases, but not to try to specify which
cases those are in exhaustive detail.

I agree so this can be fragile. But inlining has too high an impact on performance so I don't think a description somewhere on wiki is enough.

Now, the SQL functions can use plan cache too, so the overhead of execution of non-inlined SQL functions can be less, but still it is very important from performance perspective and often a source of performance issue. There can be notes, so described rules can be changed in the time, but I think the described behaviour is mostly very stable.

Regards

Pavel

 

                        regards, tom lane


Re: Documenting inlining SQL functions

От:
Merlin Moncure <mmoncure@gmail.com>
Дата:
On Fri, Jul 18, 2025 at 9:08 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:
"David G. Johnston" <david.g.johnston@gmail.com> writes:
> On Sunday, July 6, 2025, Paul Jungwirth <pj@illuminatedcomputing.com> wrote:
>> The second patch adds a new <sect2> explaining how we inline SQL
>> functions: both single-result and set-returning. Since this happens
>> automatically, it makes a nice progression with the (easy) declarative
>> annotations and the (hard) support functions.

> The fact that attaching a set clause to the function definition (i.e.,
> proconfig) prevents inlining is missing from this description.

TBH, I think trying to document this behavior at this level of detail
will be a disaster.  Our track record for keeping documentation in
sync with code is awful, and what exactly will make this area better
than average?  Even if this text is 100% accurate today, I'll bet a
good lunch that it will be wrong in two or three releases.


Yeah.  
I'm still contemplating my goofy answer to the inlining gripe on a recent thread..oh well :).

Maybe it might be more helpful and future proof to, rather than bullet point list the mechanics that prevent inlining, provide some lightly sanded down advice on how certain mechanics can prevent inlining along with some advice on how to detect if a particular function is inlined based on explain output.

merlin  
FAQ