public inbox for [email protected]  
help / color / mirror / Atom feed
43.10 Trigger Functions one sentences kind of not easy to understand.
2+ messages / 2 participants
[nested] [flat]

* 43.10 Trigger Functions one sentences kind of not easy to understand.
@ 2022-04-27 11:39 Jian He <[email protected]>
  2022-04-27 11:46 ` Re: 43.10 Trigger Functions one sentences kind of not easy to understand. Alvaro Herrera <[email protected]>
  0 siblings, 1 reply; 2+ messages in thread

From: Jian He @ 2022-04-27 11:39 UTC (permalink / raw)
  To: [email protected]

https://www.postgresql.org/docs/current/plpgsql-trigger.html

>
> Row-level triggers fired BEFORE can return null to signal the trigger
> manager to skip the rest of the operation for this row (i.e., subsequent
> triggers are not fired, and the INSERT/UPDATE/DELETE does not occur for
> this row). If a nonnull value is returned then the operation proceeds with
> that row value. *Returning a row value different from the original value
> of NEW alters the row that will be inserted or updated. *Thus, if the
> trigger function wants the triggering action to succeed normally without
> altering the row value, NEW (or a value equal thereto) has to be
> returned. To alter the row to be stored, it is possible to replace single
> values directly in NEW and return the modified NEW, or to build a
> complete new record/row to return. In the case of a before-trigger on
> DELETE, the returned value has no direct effect, but it has to be nonnull
> to allow the trigger action to proceed. Note that NEW is null in DELETE
> triggers, so returning that is usually not sensible. The usual idiom in
> DELETE triggers is to return OLD.
>

I just want confirm the highlighted sentence  is equivalent as the
following sql code:

begin;
> CREATE TABLE documents (
>     docdesc text,
>     misc text,
>     modification_time timestamp with time zone DEFAULT now()
> );
> CREATE FUNCTION documents_update_mod_time() RETURNS trigger
> AS $$
>     begin
>     new.modification_time := now();
>
    return new;
>
    end
> $$
>     LANGUAGE plpgsql;
> CREATE TRIGGER documents_modification_time
>     BEFORE INSERT OR UPDATE ON documents
>     FOR EACH ROW
>     EXECUTE PROCEDURE documents_update_mod_time();
> commit;
>

as a non-native english speaker, I kind of feel this sentence quite hard to
understand....


^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: 43.10 Trigger Functions one sentences kind of not easy to understand.
  2022-04-27 11:39 43.10 Trigger Functions one sentences kind of not easy to understand. Jian He <[email protected]>
@ 2022-04-27 11:46 ` Alvaro Herrera <[email protected]>
  0 siblings, 0 replies; 2+ messages in thread

From: Alvaro Herrera @ 2022-04-27 11:46 UTC (permalink / raw)
  To: Jian He <[email protected]>; +Cc: [email protected]

On 2022-Apr-27, Jian He wrote:

> https://www.postgresql.org/docs/current/plpgsql-trigger.html
> 
> > *Returning a row value different from the original value
> > of NEW alters the row that will be inserted or updated.*

> I just want confirm the highlighted sentence  is equivalent as the
> following sql code:

Yes:

> begin;
> > CREATE TABLE documents (
> >     docdesc text,
> >     misc text,
> >     modification_time timestamp with time zone DEFAULT now()
> > );
> > CREATE FUNCTION documents_update_mod_time() RETURNS trigger
> > AS $$
> >     begin
> >     new.modification_time := now();
> >
>     return new;
> >
>     end
> > $$
> >     LANGUAGE plpgsql;
> > CREATE TRIGGER documents_modification_time
> >     BEFORE INSERT OR UPDATE ON documents
> >     FOR EACH ROW
> >     EXECUTE PROCEDURE documents_update_mod_time();
> > commit;

This is the usual way to modify the row being inserted.

> As a non-native english speaker, I kind of feel this sentence quite hard to
> understand....

Maybe you can split it up at periods and think about individual parts
separately.  The docs are intentionally packed very densely with
information, to avoid making them longer than they already are.

-- 
Álvaro Herrera         PostgreSQL Developer  —  https://www.EnterpriseDB.com/






^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2022-04-27 11:46 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2022-04-27 11:39 43.10 Trigger Functions one sentences kind of not easy to understand. Jian He <[email protected]>
2022-04-27 11:46 ` Alvaro Herrera <[email protected]>

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