pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedstable for each row before insert trigger
7+ messages / 4 participants
[nested] [flat]
* stable for each row before insert trigger
@ 2019-10-17 23:31 Олег Самойлов <splarv@ya.ru>
0 siblings, 2 replies; 7+ messages in thread
From: Олег Самойлов @ 2019-10-17 23:31 UTC (permalink / raw)
To: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
Hi all.
According to documentation the words "VOLATILE, STABLE, IMMUTABLE" is somehow useful with trigger functions, for instance mentioned that the AFTER INSERT trigger should be VOLATILE. The question is how this words affect a for each row before insert trigger? Can be some optimisation here?
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: stable for each row before insert trigger
@ 2019-10-17 23:41 Adrian Klaver <adrian.klaver@aklaver.com>
parent: Олег Самойлов <splarv@ya.ru>
1 sibling, 1 reply; 7+ messages in thread
From: Adrian Klaver @ 2019-10-17 23:41 UTC (permalink / raw)
To: Олег Самойлов <splarv@ya.ru>; pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
On 10/17/19 4:31 PM, Олег Самойлов wrote:
> Hi all.
>
> According to documentation the words "VOLATILE, STABLE, IMMUTABLE" is somehow useful with trigger functions, for instance mentioned that the AFTER INSERT trigger should be VOLATILE. The question is how this words affect a for each row before insert trigger? Can be some optimisation here?
>
>
https://www.postgresql.org/docs/11/xfunc-volatility.html
--
Adrian Klaver
adrian.klaver@aklaver.com
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: stable for each row before insert trigger
@ 2019-10-18 06:55 Олег Самойлов <splarv@ya.ru>
parent: Adrian Klaver <adrian.klaver@aklaver.com>
0 siblings, 0 replies; 7+ messages in thread
From: Олег Самойлов @ 2019-10-18 06:55 UTC (permalink / raw)
To: Adrian Klaver <adrian.klaver@aklaver.com>; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
Eh, stupid answer. Of cause, I read the documentation. But what about you? Do you know what is a trigger function? (for each row before insert)
A trigger function don't have parameters, instead it get special variables. OLD and NEW for instance or TG_ARGV[].
If trigger function depends only on NEW is it IMMUTABLE? (With the same NEW it must return the same changed NEW). If trigger function makes SELECTs and change only NEW is it can be declared as STABLE? And etc.
> 18 окт. 2019 г., в 2:41, Adrian Klaver <adrian.klaver@aklaver.com> написал(а):
>
> On 10/17/19 4:31 PM, Олег Самойлов wrote:
>> Hi all.
>> According to documentation the words "VOLATILE, STABLE, IMMUTABLE" is somehow useful with trigger functions, for instance mentioned that the AFTER INSERT trigger should be VOLATILE. The question is how this words affect a for each row before insert trigger? Can be some optimisation here?
>
> https://www.postgresql.org/docs/11/xfunc-volatility.html
>
> --
> Adrian Klaver
> adrian.klaver@aklaver.com
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: stable for each row before insert trigger
@ 2019-10-18 07:15 Tom Lane <tgl@sss.pgh.pa.us>
parent: Олег Самойлов <splarv@ya.ru>
1 sibling, 2 replies; 7+ messages in thread
From: Tom Lane @ 2019-10-18 07:15 UTC (permalink / raw)
To: Олег Самойлов <splarv@ya.ru>; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
=?utf-8?B?0J7Qu9C10LMg0KHQsNC80L7QudC70L7Qsg==?= <splarv@ya.ru> writes:
> According to documentation the words "VOLATILE, STABLE, IMMUTABLE" is somehow useful with trigger functions, for instance mentioned that the AFTER INSERT trigger should be VOLATILE. The question is how this words affect a for each row before insert trigger? Can be some optimisation here?
Where did you read that? There's no optimization that considers the
volatility of trigger functions --- they'll be called exactly when
specified, no more or less.
There are some PLs that might behave differently depending on whether they
think the function is volatile or not, but that's independent of whether
the function is a trigger.
regards, tom lane
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: stable for each row before insert trigger
@ 2019-10-18 08:09 Luca Ferrari <fluca1978@gmail.com>
parent: Tom Lane <tgl@sss.pgh.pa.us>
1 sibling, 0 replies; 7+ messages in thread
From: Luca Ferrari @ 2019-10-18 08:09 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Олег Самойлов <splarv@ya.ru>; pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
On Fri, Oct 18, 2019 at 9:16 AM Tom Lane <tgl@sss.pgh.pa.us> wrote:
>
> =?utf-8?B?0J7Qu9C10LMg0KHQsNC80L7QudC70L7Qsg==?= <splarv@ya.ru> writes:
> > According to documentation the words "VOLATILE, STABLE, IMMUTABLE" is somehow useful with trigger functions, for instance mentioned that the AFTER INSERT trigger should be VOLATILE. The question is how this words affect a for each row before insert trigger? Can be some optimisation here?
>
> Where did you read that? There's no optimization that considers the
> volatility of trigger functions --- they'll be called exactly when
> specified, no more or less.
I suspect this sentence could have raised the OP doubt:
"For best optimization results, you should label your functions with
the strictest volatility category that is valid for them."
(from <https://www.postgresql.org/docs/11/xfunc-volatility.html;).
However, here <https://www.postgresql.org/docs/11/trigger-datachanges.html;
it is specified that:
"If your trigger function is written in any of the standard procedural
languages, then the above statements apply only if the function is
declared VOLATILE. Functions that are declared STABLE or IMMUTABLE
will not see changes made by the calling command in any case."
which I hope is the answer to the original question.
Luca
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: stable for each row before insert trigger
@ 2019-10-18 09:18 Олег Самойлов <splarv@ya.ru>
parent: Tom Lane <tgl@sss.pgh.pa.us>
1 sibling, 1 reply; 7+ messages in thread
From: Олег Самойлов @ 2019-10-18 09:18 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
Luca, I also read this section before ask the question.
> 18 окт. 2019 г., в 10:15, Tom Lane <tgl@sss.pgh.pa.us> написал(а):
>
> =?utf-8?B?0J7Qu9C10LMg0KHQsNC80L7QudC70L7Qsg==?= <splarv@ya.ru> writes:
>> According to documentation the words "VOLATILE, STABLE, IMMUTABLE" is somehow useful with trigger functions, for instance mentioned that the AFTER INSERT trigger should be VOLATILE. The question is how this words affect a for each row before insert trigger? Can be some optimisation here?
>
> Where did you read that? There's no optimization that considers the
> volatility of trigger functions --- they'll be called exactly when
> specified, no more or less.
Good to see this. :) But there is somehow optimisation for triggers, which is somehow mentioned in the documentation, but not clearly defined.
https://www.postgresql.org/docs/current/sql-createfunction.html
> STABLE indicates .... (It is inappropriate for AFTER triggers that wish to query rows modified by the current command.)
So, STABLE is inappropriate for such trigger, but is appropriate for BEFORE trigger?
Luca correctly pointed to:
https://www.postgresql.org/docs/current/trigger-datachanges.html
> If your trigger function is written in any of the standard procedural languages, then the above statements apply only if the function is declared VOLATILE. Functions that are declared STABLE or IMMUTABLE will not see changes made by the calling command in any case.
So will be good put inside right section
https://www.postgresql.org/docs/current/xfunc-volatility.html
Exact definition how "VOLATILE, STABLE, IMMUTABLE" affect a trigger function.
For instance, I expect that the FOR EACH ROW BEFORE trigger marked as STABLE will be faster than VOLATILE without important negative side effects. I observed 3% benefit. IMMUTABLE trigger is slightly slower then STABLE, but I am not sure, too low difference.
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: stable for each row before insert trigger
@ 2019-10-18 11:24 Luca Ferrari <fluca1978@gmail.com>
parent: Олег Самойлов <splarv@ya.ru>
0 siblings, 0 replies; 7+ messages in thread
From: Luca Ferrari @ 2019-10-18 11:24 UTC (permalink / raw)
To: Олег Самойлов <splarv@ya.ru>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
On Fri, Oct 18, 2019 at 11:18 AM Олег Самойлов <splarv@ya.ru> wrote:
> > STABLE indicates .... (It is inappropriate for AFTER triggers that wish to query rows modified by the current command.)
>
> So, STABLE is inappropriate for such trigger, but is appropriate for BEFORE trigger?
>
Well, a before trigger will not see data changes in any way, so I read
it as "it does not matter for before triggers".
And I would speculate that, being in a trigger, the function is
invoked every time, so there should not be any caching that produces
performance boosts.
> For instance, I expect that the FOR EACH ROW BEFORE trigger marked as STABLE will be faster than VOLATILE without important negative side effects. I observed 3% benefit. IMMUTABLE trigger is slightly slower then STABLE, but I am not sure, too low difference.
It could be faster, but I would not speculate on that rather than an
inling of the code.
Luca
^ permalink raw reply [nested|flat] 7+ messages in thread
end of thread, other threads:[~2019-10-18 11:24 UTC | newest]
Thread overview: 7+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2019-10-17 23:31 stable for each row before insert trigger Олег Самойлов <splarv@ya.ru>
2019-10-17 23:41 ` Adrian Klaver <adrian.klaver@aklaver.com>
2019-10-18 06:55 ` Олег Самойлов <splarv@ya.ru>
2019-10-18 07:15 ` Tom Lane <tgl@sss.pgh.pa.us>
2019-10-18 08:09 ` Luca Ferrari <fluca1978@gmail.com>
2019-10-18 09:18 ` Олег Самойлов <splarv@ya.ru>
2019-10-18 11:24 ` Luca Ferrari <fluca1978@gmail.com>
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