Re: Calculating a moving average (Coding style)

From: mstory(at)uchicago(dot)edu
To: Russell Smith <mr-russ(at)pws(dot)com(dot)au>
Cc: Alban Hertroys <alban(at)magproductions(dot)nl>, pgsql-general(at)postgresql(dot)org
Subject: Re: Calculating a moving average (Coding style)
Date: 2005-01-24 20:48:18
Message-ID: 1106599698.41f55f12d476c@churlish.uchicago.edu
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

I personally use 3 seperate triggers on most occasions, depending on how
different the action for each seperate action is, it's just easier for me and my
people to logically distinguish the functions that way, but the example in the
7.4 documentation for triggers is given using the form that i wrote this
function in, and is useful when the trigger procedures for all 3 actions do
roughly the same thing, it also makes editing the code a little easier in this
situation.

regards,
matt

Quoting Russell Smith <mr-russ(at)pws(dot)com(dot)au>:

> On Mon, 24 Jan 2005 08:32 pm, Alban Hertroys wrote:
> > mstory(at)uchicago(dot)edu wrote:
> > > CREATE OR REPLACE FUNCTION get_bar_avg() RETURNS TRIGGER AS '
> > > DECLARE
> > > bar_record RECORD;
> > > x INTEGER;
> > > y DOUBLE PRECISION := 0;
> > > BEGIN
> > > IF TG_OP = ''INSERT'' THEN
> > > y := y + NEW.bar;
> > ...
> > > RETURN NEW;
> > > ELSIF TG_OP = ''DELETE'' THEN
> > > x := 0;
> > ...
> > > RETURN OLD;
> > > ELSE
> > > y := y + NEW.bar;
> > ...
> > > RETURN NEW;
> > > END IF;
> > > END;
> > > ' LANGUAGE plpgsql;
> >
> > I see people do this from time to time. Just out of curiosity, is this
> > considered good coding style, or is it considered "lazyness"? I'm not
> > sure what to think of it.
> >
> > If I would have written this, there would have been 3 triggers w/o the
> > check on TG_OP. Is there an important drawback to doing so? Is there any
> > document on "preferred" coding style in PL/PGSQL?
> >
> > Yes, I'm a bit of a purist...
> >
> Given you have to define a function for each trigger, my view is why write
> more functions.
>
> Along with this. As a C programmer, I would do a few more IF tests in a
> function, rather than
> write another one. I find that triggers like this are one functional block
> and all go together.
> Then when you update the function, it's all in one place.
>
> Others may have "better" reasons for why they do it the way they do. But
> they are mine.
>
> Regards
>
> Russell Smith.
>
> ---------------------------(end of broadcast)---------------------------
> TIP 7: don't forget to increase your free space map settings
>

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Martijn van Oosterhout 2005-01-24 22:08:31 Re: PGresult pointer and memory usage
Previous Message Jeff Davis 2005-01-24 20:14:09 PGresult pointer and memory usage