Trigger or Function

From: alan <alan(dot)miller3(at)gmail(dot)com>
To: pgsql-performance(at)postgresql(dot)org
Subject: Trigger or Function
Date: 2011-07-12 07:41:03
Message-ID: cf38a540-20ef-45d7-b593-2a6ec3337a95@j15g2000yqf.googlegroups.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

Hello,
I'm a postgres newbie and am wondering what's the best way to do this.

I am gathering some data and will be inserting to a table once daily.
The table is quite simple but I want the updates to be as efficient as
possible since
this db is part of a big data project.

Say I have a table with these columns:
| Date | Hostname | DayVal | WeekAvg | MonthAvg |

When I insert a new row I have the values for Date, Hostname, DayVal.
Is it possible to define the table is such a way that the WeekAvg and
MonthAvg
are automatically updated as follows?
WeekAvg = current rows DayVal plus the sum of DayVal for the
previous 6 rows.
MonthAvg = current row's DayVal plus the sum of DayVal for the
previous 29 rows.

Should I place the logic in a Trigger or in a Function?
Does someone have an example or a link showing how I could set this
up?

Regards,
Alan

Responses

Browse pgsql-performance by date

  From Date Subject
Next Message Ivan Voras 2011-07-12 13:22:46 Re: UPDATEDs slowing SELECTs in a fully cached database
Previous Message lars 2011-07-12 04:42:45 Re: UPDATEDs slowing SELECTs in a fully cached database